Fix image flipping for OpenGL-based screen capturer on Mac.

I broke captured image flipping when refactoring this code while it was
still in chromium. Previously we had CaptureData that was returned from
capturers with correctly inverted stride, but frames were still stored
with positive stride. CaptureData was removed and so the returned frames
always had positive stride, which is not correct. Now ScreenCapturerMac
uses frames with inverted stride when capturing using OpenGL.

R=wez@chromium.org

Review URL: https://webrtc-codereview.appspot.com/2105004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@4621 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/desktop_capture/screen_capturer_mac.mm b/webrtc/modules/desktop_capture/screen_capturer_mac.mm
index b652477..89d9776 100644
--- a/webrtc/modules/desktop_capture/screen_capturer_mac.mm
+++ b/webrtc/modules/desktop_capture/screen_capturer_mac.mm
@@ -219,6 +219,29 @@
   DISALLOW_COPY_AND_ASSIGN(ScreenCapturerMac);
 };
 
+// DesktopFrame wrapper that flips wrapped frame upside down by inverting
+// stride.
+class InvertedDesktopFrame : public DesktopFrame {
+ public:
+  // Takes ownership of |frame|.
+  InvertedDesktopFrame(DesktopFrame* frame)
+      : DesktopFrame(
+            frame->size(), -frame->stride(),
+            frame->data() - (frame->size().height() - 1) * frame->stride(),
+            frame->shared_memory()),
+        original_frame_(frame) {
+    set_dpi(frame->dpi());
+    set_capture_time_ms(frame->capture_time_ms());
+    mutable_updated_region()->Swap(frame->mutable_updated_region());
+  }
+  virtual ~InvertedDesktopFrame() {}
+
+ private:
+  scoped_ptr<DesktopFrame> original_frame_;
+
+  DISALLOW_COPY_AND_ASSIGN(InvertedDesktopFrame);
+};
+
 DesktopFrame* CreateFrame(
     const MacDesktopConfiguration& desktop_config) {
 
@@ -364,16 +387,12 @@
     CgBlitPreLion(*current_frame, region);
   }
 
-  uint8_t* buffer = current_frame->data();
-  int stride = current_frame->stride();
-  if (flip) {
-    stride = -stride;
-    buffer += (current_frame->size().height() - 1) * current_frame->stride();
-  }
-
   DesktopFrame* new_frame = queue_.current_frame()->Share();
   *new_frame->mutable_updated_region() = region;
 
+  if (flip)
+    new_frame = new InvertedDesktopFrame(new_frame);
+
   helper_.set_size_most_recent(new_frame->size());
 
   // Signal that we are done capturing data from the display framebuffer,