| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/video_frame_capturer.h" | 5 #include "remoting/host/video_frame_capturer.h" |
| 6 | 6 |
| 7 #include <ApplicationServices/ApplicationServices.h> | 7 #include <ApplicationServices/ApplicationServices.h> |
| 8 #include <Cocoa/Cocoa.h> | 8 #include <Cocoa/Cocoa.h> |
| 9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
| 10 #include <IOKit/pwr_mgt/IOPMLib.h> | 10 #include <IOKit/pwr_mgt/IOPMLib.h> |
| 11 #include <OpenGL/CGLMacro.h> | 11 #include <OpenGL/CGLMacro.h> |
| 12 #include <OpenGL/OpenGL.h> | 12 #include <OpenGL/OpenGL.h> |
| 13 #include <stddef.h> | 13 #include <stddef.h> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/mac/mac_util.h" | 16 #include "base/mac/mac_util.h" |
| 17 #include "base/mac/scoped_cftyperef.h" | 17 #include "base/mac/scoped_cftyperef.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
| 20 #include "base/time.h" | 20 #include "base/time.h" |
| 21 #include "remoting/base/capture_data.h" | 21 #include "remoting/base/capture_data.h" |
| 22 #include "remoting/base/util.h" | 22 #include "remoting/base/util.h" |
| 23 #include "remoting/host/mac/scoped_pixel_buffer_object.h" |
| 23 #include "remoting/host/video_frame_capturer_helper.h" | 24 #include "remoting/host/video_frame_capturer_helper.h" |
| 24 #include "remoting/proto/control.pb.h" | 25 #include "remoting/proto/control.pb.h" |
| 25 | 26 |
| 26 namespace remoting { | 27 namespace remoting { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 SkIRect CGRectToSkIRect(const CGRect& rect) { | 31 SkIRect CGRectToSkIRect(const CGRect& rect) { |
| 31 SkIRect sk_rect = { | 32 SkIRect sk_rect = { |
| 32 SkScalarRound(rect.origin.x), | 33 SkScalarRound(rect.origin.x), |
| 33 SkScalarRound(rect.origin.y), | 34 SkScalarRound(rect.origin.y), |
| 34 SkScalarRound(rect.origin.x + rect.size.width), | 35 SkScalarRound(rect.origin.x + rect.size.width), |
| 35 SkScalarRound(rect.origin.y + rect.size.height) | 36 SkScalarRound(rect.origin.y + rect.size.height) |
| 36 }; | 37 }; |
| 37 return sk_rect; | 38 return sk_rect; |
| 38 } | 39 } |
| 39 | 40 |
| 40 // The amount of time allowed for displays to reconfigure. | 41 // The amount of time allowed for displays to reconfigure. |
| 41 const int64 kDisplayReconfigurationTimeoutInSeconds = 10; | 42 const int64 kDisplayReconfigurationTimeoutInSeconds = 10; |
| 42 | 43 |
| 43 class scoped_pixel_buffer_object { | |
| 44 public: | |
| 45 scoped_pixel_buffer_object(); | |
| 46 ~scoped_pixel_buffer_object(); | |
| 47 | |
| 48 bool Init(CGLContextObj cgl_context, int size_in_bytes); | |
| 49 void Release(); | |
| 50 | |
| 51 GLuint get() const { return pixel_buffer_object_; } | |
| 52 | |
| 53 private: | |
| 54 CGLContextObj cgl_context_; | |
| 55 GLuint pixel_buffer_object_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(scoped_pixel_buffer_object); | |
| 58 }; | |
| 59 | |
| 60 scoped_pixel_buffer_object::scoped_pixel_buffer_object() | |
| 61 : cgl_context_(NULL), | |
| 62 pixel_buffer_object_(0) { | |
| 63 } | |
| 64 | |
| 65 scoped_pixel_buffer_object::~scoped_pixel_buffer_object() { | |
| 66 Release(); | |
| 67 } | |
| 68 | |
| 69 bool scoped_pixel_buffer_object::Init(CGLContextObj cgl_context, | |
| 70 int size_in_bytes) { | |
| 71 cgl_context_ = cgl_context; | |
| 72 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_; | |
| 73 glGenBuffersARB(1, &pixel_buffer_object_); | |
| 74 if (glGetError() == GL_NO_ERROR) { | |
| 75 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pixel_buffer_object_); | |
| 76 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, size_in_bytes, NULL, | |
| 77 GL_STREAM_READ_ARB); | |
| 78 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); | |
| 79 if (glGetError() != GL_NO_ERROR) { | |
| 80 Release(); | |
| 81 } | |
| 82 } else { | |
| 83 cgl_context_ = NULL; | |
| 84 pixel_buffer_object_ = 0; | |
| 85 } | |
| 86 return pixel_buffer_object_ != 0; | |
| 87 } | |
| 88 | |
| 89 void scoped_pixel_buffer_object::Release() { | |
| 90 if (pixel_buffer_object_) { | |
| 91 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_; | |
| 92 glDeleteBuffersARB(1, &pixel_buffer_object_); | |
| 93 cgl_context_ = NULL; | |
| 94 pixel_buffer_object_ = 0; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 // A class representing a full-frame pixel buffer. | 44 // A class representing a full-frame pixel buffer. |
| 99 class VideoFrameBuffer { | 45 class VideoFrameBuffer { |
| 100 public: | 46 public: |
| 101 VideoFrameBuffer() : | 47 VideoFrameBuffer() : |
| 102 size_(SkISize::Make(0, 0)), | 48 size_(SkISize::Make(0, 0)), |
| 103 dpi_(SkIPoint::Make(0, 0)), | 49 dpi_(SkIPoint::Make(0, 0)), |
| 104 bytes_per_row_(0), | 50 bytes_per_row_(0), |
| 105 needs_update_(true) { | 51 needs_update_(true) { |
| 106 } | 52 } |
| 107 | 53 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 const CGRect *rect_array, | 134 const CGRect *rect_array, |
| 189 void *user_parameter); | 135 void *user_parameter); |
| 190 static void DisplaysReconfiguredCallback(CGDirectDisplayID display, | 136 static void DisplaysReconfiguredCallback(CGDirectDisplayID display, |
| 191 CGDisplayChangeSummaryFlags flags, | 137 CGDisplayChangeSummaryFlags flags, |
| 192 void *user_parameter); | 138 void *user_parameter); |
| 193 | 139 |
| 194 void ReleaseBuffers(); | 140 void ReleaseBuffers(); |
| 195 | 141 |
| 196 CGLContextObj cgl_context_; | 142 CGLContextObj cgl_context_; |
| 197 static const int kNumBuffers = 2; | 143 static const int kNumBuffers = 2; |
| 198 scoped_pixel_buffer_object pixel_buffer_object_; | 144 ScopedPixelBufferObject pixel_buffer_object_; |
| 199 VideoFrameBuffer buffers_[kNumBuffers]; | 145 VideoFrameBuffer buffers_[kNumBuffers]; |
| 200 | 146 |
| 201 // A thread-safe list of invalid rectangles, and the size of the most | 147 // A thread-safe list of invalid rectangles, and the size of the most |
| 202 // recently captured screen. | 148 // recently captured screen. |
| 203 VideoFrameCapturerHelper helper_; | 149 VideoFrameCapturerHelper helper_; |
| 204 | 150 |
| 205 // Callback notified whenever the cursor shape is changed. | 151 // Callback notified whenever the cursor shape is changed. |
| 206 CursorShapeChangedCallback cursor_shape_changed_callback_; | 152 CursorShapeChangedCallback cursor_shape_changed_callback_; |
| 207 | 153 |
| 208 // Image of the last cursor that we sent to the client. | 154 // Image of the last cursor that we sent to the client. |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 bool flip = false; // GL capturers need flipping. | 296 bool flip = false; // GL capturers need flipping. |
| 351 if (base::mac::IsOSLionOrLater()) { | 297 if (base::mac::IsOSLionOrLater()) { |
| 352 // Lion requires us to use their new APIs for doing screen capture. These | 298 // Lion requires us to use their new APIs for doing screen capture. These |
| 353 // APIS currently crash on 10.6.8 if there is no monitor attached. | 299 // APIS currently crash on 10.6.8 if there is no monitor attached. |
| 354 CgBlitPostLion(current_buffer, region); | 300 CgBlitPostLion(current_buffer, region); |
| 355 } else if (cgl_context_) { | 301 } else if (cgl_context_) { |
| 356 flip = true; | 302 flip = true; |
| 357 if (pixel_buffer_object_.get() != 0) { | 303 if (pixel_buffer_object_.get() != 0) { |
| 358 GlBlitFast(current_buffer, region); | 304 GlBlitFast(current_buffer, region); |
| 359 } else { | 305 } else { |
| 360 // See comment in scoped_pixel_buffer_object::Init about why the slow | 306 // See comment in ScopedPixelBufferObject::Init about why the slow |
| 361 // path is always used on 10.5. | 307 // path is always used on 10.5. |
| 362 GlBlitSlow(current_buffer); | 308 GlBlitSlow(current_buffer); |
| 363 } | 309 } |
| 364 } else { | 310 } else { |
| 365 CgBlitPreLion(current_buffer, region); | 311 CgBlitPreLion(current_buffer, region); |
| 366 } | 312 } |
| 367 | 313 |
| 368 DataPlanes planes; | 314 DataPlanes planes; |
| 369 planes.data[0] = current_buffer.ptr(); | 315 planes.data[0] = current_buffer.ptr(); |
| 370 planes.strides[0] = current_buffer.bytes_per_row(); | 316 planes.strides[0] = current_buffer.bytes_per_row(); |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 VideoFrameCapturer* VideoFrameCapturer::Create() { | 710 VideoFrameCapturer* VideoFrameCapturer::Create() { |
| 765 VideoFrameCapturerMac* capturer = new VideoFrameCapturerMac(); | 711 VideoFrameCapturerMac* capturer = new VideoFrameCapturerMac(); |
| 766 if (!capturer->Init()) { | 712 if (!capturer->Init()) { |
| 767 delete capturer; | 713 delete capturer; |
| 768 capturer = NULL; | 714 capturer = NULL; |
| 769 } | 715 } |
| 770 return capturer; | 716 return capturer; |
| 771 } | 717 } |
| 772 | 718 |
| 773 } // namespace remoting | 719 } // namespace remoting |
| OLD | NEW |