Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: remoting/host/video_frame_capturer_mac.mm

Issue 10696134: remoting/host: Rename Capturer to VideoFrameCapturer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "remoting/host/video_frame_capturer.h"
6
7 #include <ApplicationServices/ApplicationServices.h>
8 #include <Cocoa/Cocoa.h>
9 #include <dlfcn.h>
10 #include <IOKit/pwr_mgt/IOPMLib.h>
11 #include <OpenGL/CGLMacro.h>
12 #include <OpenGL/OpenGL.h>
13 #include <stddef.h>
14
15 #include "base/logging.h"
16 #include "base/mac/mac_util.h"
17 #include "base/mac/scoped_cftyperef.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "base/time.h"
21 #include "remoting/base/capture_data.h"
22 #include "remoting/base/util.h"
23 #include "remoting/host/video_frame_capturer_helper.h"
24 #include "remoting/proto/control.pb.h"
25
26 #if !defined(MAC_OS_X_VERSION_10_6) || \
27 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
28
29 @interface NSCursor (SnowLeopardSDKDeclarations)
30 + (NSCursor*)currentSystemCursor;
31 @end
32
33 @interface NSImage (SnowLeopardSDKDeclarations)
34 - (CGImageRef)CGImageForProposedRect:(NSRect*)proposedDestRect
35 context:(NSGraphicsContext*)referenceContext
36 hints:(NSDictionary*)hints;
37 @end
38
39 #endif // MAC_OS_X_VERSION_10_6
40
41 namespace remoting {
42
43 namespace {
44
45 SkIRect CGRectToSkIRect(const CGRect& rect) {
46 SkIRect sk_rect = {
47 SkScalarRound(rect.origin.x),
48 SkScalarRound(rect.origin.y),
49 SkScalarRound(rect.origin.x + rect.size.width),
50 SkScalarRound(rect.origin.y + rect.size.height)
51 };
52 return sk_rect;
53 }
54
55 #if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5)
56 // Possibly remove VideoFrameCapturerMac::CgBlitPreLion as well depending on
57 // performance of VideoFrameCapturerMac::CgBlitPostLion on 10.6.
58 #error No longer need to import CGDisplayCreateImage.
59 #else
60 // Declared here because CGDisplayCreateImage does not exist in the 10.5 SDK,
61 // which we are currently compiling against, and it is required on 10.7 to do
62 // screen capture.
63 typedef CGImageRef (*CGDisplayCreateImageFunc)(CGDirectDisplayID displayID);
64 #endif
65
66 // The amount of time allowed for displays to reconfigure.
67 const int64 kDisplayReconfigurationTimeoutInSeconds = 10;
68
69 class scoped_pixel_buffer_object {
70 public:
71 scoped_pixel_buffer_object();
72 ~scoped_pixel_buffer_object();
73
74 bool Init(CGLContextObj cgl_context, int size_in_bytes);
75 void Release();
76
77 GLuint get() const { return pixel_buffer_object_; }
78
79 private:
80 CGLContextObj cgl_context_;
81 GLuint pixel_buffer_object_;
82
83 DISALLOW_COPY_AND_ASSIGN(scoped_pixel_buffer_object);
84 };
85
86 scoped_pixel_buffer_object::scoped_pixel_buffer_object()
87 : cgl_context_(NULL),
88 pixel_buffer_object_(0) {
89 }
90
91 scoped_pixel_buffer_object::~scoped_pixel_buffer_object() {
92 Release();
93 }
94
95 bool scoped_pixel_buffer_object::Init(CGLContextObj cgl_context,
96 int size_in_bytes) {
97 // The PBO path is only done on 10.6 (SnowLeopard) and above due to
98 // a driver issue that was found on 10.5
99 // (specifically on a NVIDIA GeForce 7300 GT).
100 // http://crbug.com/87283
101 if (base::mac::IsOSLeopardOrEarlier()) {
102 return false;
103 }
104 cgl_context_ = cgl_context;
105 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_;
106 glGenBuffersARB(1, &pixel_buffer_object_);
107 if (glGetError() == GL_NO_ERROR) {
108 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pixel_buffer_object_);
109 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, size_in_bytes, NULL,
110 GL_STREAM_READ_ARB);
111 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
112 if (glGetError() != GL_NO_ERROR) {
113 Release();
114 }
115 } else {
116 cgl_context_ = NULL;
117 pixel_buffer_object_ = 0;
118 }
119 return pixel_buffer_object_ != 0;
120 }
121
122 void scoped_pixel_buffer_object::Release() {
123 if (pixel_buffer_object_) {
124 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_;
125 glDeleteBuffersARB(1, &pixel_buffer_object_);
126 cgl_context_ = NULL;
127 pixel_buffer_object_ = 0;
128 }
129 }
130
131 // A class representing a full-frame pixel buffer.
132 class VideoFrameBuffer {
133 public:
134 VideoFrameBuffer() : bytes_per_row_(0), needs_update_(true) { }
135
136 // If the buffer is marked as needing to be updated (for example after the
137 // screen mode changes) and is the wrong size, then release the old buffer
138 // and create a new one.
139 void Update() {
140 if (needs_update_) {
141 needs_update_ = false;
142 CGDirectDisplayID mainDevice = CGMainDisplayID();
143 int width = CGDisplayPixelsWide(mainDevice);
144 int height = CGDisplayPixelsHigh(mainDevice);
145 if (width != size_.width() || height != size_.height()) {
146 size_.set(width, height);
147 bytes_per_row_ = width * sizeof(uint32_t);
148 size_t buffer_size = width * height * sizeof(uint32_t);
149 ptr_.reset(new uint8[buffer_size]);
150 }
151 }
152 }
153
154 SkISize size() const { return size_; }
155 int bytes_per_row() const { return bytes_per_row_; }
156 uint8* ptr() const { return ptr_.get(); }
157
158 void set_needs_update() { needs_update_ = true; }
159
160 private:
161 SkISize size_;
162 int bytes_per_row_;
163 scoped_array<uint8> ptr_;
164 bool needs_update_;
165
166 DISALLOW_COPY_AND_ASSIGN(VideoFrameBuffer);
167 };
168
169 // A class to perform video frame capturing for mac.
170 class VideoFrameCapturerMac : public VideoFrameCapturer {
171 public:
172 VideoFrameCapturerMac();
173 virtual ~VideoFrameCapturerMac();
174
175 bool Init();
176
177 // Overridden from VideoFrameCapturer:
178 virtual void Start(const CursorShapeChangedCallback& callback) OVERRIDE;
179 virtual void Stop() OVERRIDE;
180 virtual void ScreenConfigurationChanged() OVERRIDE;
181 virtual media::VideoFrame::Format pixel_format() const OVERRIDE;
182 virtual void ClearInvalidRegion() OVERRIDE;
183 virtual void InvalidateRegion(const SkRegion& invalid_region) OVERRIDE;
184 virtual void InvalidateScreen(const SkISize& size) OVERRIDE;
185 virtual void InvalidateFullScreen() OVERRIDE;
186 virtual void CaptureInvalidRegion(
187 const CaptureCompletedCallback& callback) OVERRIDE;
188 virtual const SkISize& size_most_recent() const OVERRIDE;
189
190 private:
191 void CaptureCursor();
192
193 void GlBlitFast(const VideoFrameBuffer& buffer, const SkRegion& region);
194 void GlBlitSlow(const VideoFrameBuffer& buffer);
195 void CgBlitPreLion(const VideoFrameBuffer& buffer, const SkRegion& region);
196 void CgBlitPostLion(const VideoFrameBuffer& buffer, const SkRegion& region);
197 void CaptureRegion(const SkRegion& region,
198 const CaptureCompletedCallback& callback);
199
200 void ScreenRefresh(CGRectCount count, const CGRect *rect_array);
201 void ScreenUpdateMove(CGScreenUpdateMoveDelta delta,
202 size_t count,
203 const CGRect *rect_array);
204 void DisplaysReconfigured(CGDirectDisplayID display,
205 CGDisplayChangeSummaryFlags flags);
206 static void ScreenRefreshCallback(CGRectCount count,
207 const CGRect *rect_array,
208 void *user_parameter);
209 static void ScreenUpdateMoveCallback(CGScreenUpdateMoveDelta delta,
210 size_t count,
211 const CGRect *rect_array,
212 void *user_parameter);
213 static void DisplaysReconfiguredCallback(CGDirectDisplayID display,
214 CGDisplayChangeSummaryFlags flags,
215 void *user_parameter);
216
217 void ReleaseBuffers();
218
219 CGLContextObj cgl_context_;
220 static const int kNumBuffers = 2;
221 scoped_pixel_buffer_object pixel_buffer_object_;
222 VideoFrameBuffer buffers_[kNumBuffers];
223
224 // A thread-safe list of invalid rectangles, and the size of the most
225 // recently captured screen.
226 VideoFrameCapturerHelper helper_;
227
228 // Callback notified whenever the cursor shape is changed.
229 CursorShapeChangedCallback cursor_shape_changed_callback_;
230
231 // Image of the last cursor that we sent to the client.
232 base::mac::ScopedCFTypeRef<CGImageRef> current_cursor_;
233
234 // The current buffer with valid data for reading.
235 int current_buffer_;
236
237 // The previous buffer into which we captured, or NULL for the first capture
238 // for a particular screen resolution.
239 uint8* last_buffer_;
240
241 // Contains an invalid region from the previous capture.
242 SkRegion last_invalid_region_;
243
244 // Format of pixels returned in buffer.
245 media::VideoFrame::Format pixel_format_;
246
247 // Acts as a critical section around our display configuration data
248 // structures. Specifically cgl_context_ and pixel_buffer_object_.
249 base::WaitableEvent display_configuration_capture_event_;
250
251 // Will be non-null on lion.
252 CGDisplayCreateImageFunc display_create_image_func_;
253
254 // Power management assertion to prevent the screen from sleeping.
255 IOPMAssertionID power_assertion_id_display_;
256
257 // Power management assertion to indicate that the user is active.
258 IOPMAssertionID power_assertion_id_user_;
259
260 DISALLOW_COPY_AND_ASSIGN(VideoFrameCapturerMac);
261 };
262
263 VideoFrameCapturerMac::VideoFrameCapturerMac()
264 : cgl_context_(NULL),
265 current_buffer_(0),
266 last_buffer_(NULL),
267 pixel_format_(media::VideoFrame::RGB32),
268 display_configuration_capture_event_(false, true),
269 display_create_image_func_(NULL),
270 power_assertion_id_display_(kIOPMNullAssertionID),
271 power_assertion_id_user_(kIOPMNullAssertionID) {
272 }
273
274 VideoFrameCapturerMac::~VideoFrameCapturerMac() {
275 ReleaseBuffers();
276 CGUnregisterScreenRefreshCallback(
277 VideoFrameCapturerMac::ScreenRefreshCallback, this);
278 CGScreenUnregisterMoveCallback(
279 VideoFrameCapturerMac::ScreenUpdateMoveCallback, this);
280 CGError err = CGDisplayRemoveReconfigurationCallback(
281 VideoFrameCapturerMac::DisplaysReconfiguredCallback, this);
282 if (err != kCGErrorSuccess) {
283 LOG(ERROR) << "CGDisplayRemoveReconfigurationCallback " << err;
284 }
285 }
286
287 bool VideoFrameCapturerMac::Init() {
288 CGError err = CGRegisterScreenRefreshCallback(
289 VideoFrameCapturerMac::ScreenRefreshCallback, this);
290 if (err != kCGErrorSuccess) {
291 LOG(ERROR) << "CGRegisterScreenRefreshCallback " << err;
292 return false;
293 }
294
295 err = CGScreenRegisterMoveCallback(
296 VideoFrameCapturerMac::ScreenUpdateMoveCallback, this);
297 if (err != kCGErrorSuccess) {
298 LOG(ERROR) << "CGScreenRegisterMoveCallback " << err;
299 return false;
300 }
301 err = CGDisplayRegisterReconfigurationCallback(
302 VideoFrameCapturerMac::DisplaysReconfiguredCallback, this);
303 if (err != kCGErrorSuccess) {
304 LOG(ERROR) << "CGDisplayRegisterReconfigurationCallback " << err;
305 return false;
306 }
307
308 if (base::mac::IsOSLionOrLater()) {
309 display_create_image_func_ =
310 reinterpret_cast<CGDisplayCreateImageFunc>(
311 dlsym(RTLD_NEXT, "CGDisplayCreateImage"));
312 if (!display_create_image_func_) {
313 LOG(ERROR) << "Unable to load CGDisplayCreateImage on Lion";
314 return false;
315 }
316 }
317 ScreenConfigurationChanged();
318 return true;
319 }
320
321 void VideoFrameCapturerMac::ReleaseBuffers() {
322 if (cgl_context_) {
323 pixel_buffer_object_.Release();
324 CGLDestroyContext(cgl_context_);
325 cgl_context_ = NULL;
326 }
327 // The buffers might be in use by the encoder, so don't delete them here.
328 // Instead, mark them as "needs update"; next time the buffers are used by
329 // the capturer, they will be recreated if necessary.
330 for (int i = 0; i < kNumBuffers; ++i) {
331 buffers_[i].set_needs_update();
332 }
333 }
334
335 void VideoFrameCapturerMac::Start(const CursorShapeChangedCallback& callback) {
336 cursor_shape_changed_callback_ = callback;
337
338 // Create power management assertions to wake the display and prevent it from
339 // going to sleep on user idle.
340 IOPMAssertionCreate(kIOPMAssertionTypeNoDisplaySleep,
341 kIOPMAssertionLevelOn,
342 &power_assertion_id_display_);
343 IOPMAssertionCreate(CFSTR("UserIsActive"),
344 kIOPMAssertionLevelOn,
345 &power_assertion_id_user_);
346 }
347
348 void VideoFrameCapturerMac::Stop() {
349 if (power_assertion_id_display_ != kIOPMNullAssertionID) {
350 IOPMAssertionRelease(power_assertion_id_display_);
351 power_assertion_id_display_ = kIOPMNullAssertionID;
352 }
353 if (power_assertion_id_user_ != kIOPMNullAssertionID) {
354 IOPMAssertionRelease(power_assertion_id_user_);
355 power_assertion_id_user_ = kIOPMNullAssertionID;
356 }
357 }
358
359 void VideoFrameCapturerMac::ScreenConfigurationChanged() {
360 ReleaseBuffers();
361 helper_.ClearInvalidRegion();
362 last_buffer_ = NULL;
363
364 CGDirectDisplayID mainDevice = CGMainDisplayID();
365 int width = CGDisplayPixelsWide(mainDevice);
366 int height = CGDisplayPixelsHigh(mainDevice);
367 InvalidateScreen(SkISize::Make(width, height));
368
369 if (!CGDisplayUsesOpenGLAcceleration(mainDevice)) {
370 VLOG(3) << "OpenGL support not available.";
371 return;
372 }
373
374 if (display_create_image_func_ != NULL) {
375 // No need for any OpenGL support on Lion
376 return;
377 }
378
379 CGLPixelFormatAttribute attributes[] = {
380 kCGLPFAFullScreen,
381 kCGLPFADisplayMask,
382 (CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(mainDevice),
383 (CGLPixelFormatAttribute)0
384 };
385 CGLPixelFormatObj pixel_format = NULL;
386 GLint matching_pixel_format_count = 0;
387 CGLError err = CGLChoosePixelFormat(attributes,
388 &pixel_format,
389 &matching_pixel_format_count);
390 DCHECK_EQ(err, kCGLNoError);
391 err = CGLCreateContext(pixel_format, NULL, &cgl_context_);
392 DCHECK_EQ(err, kCGLNoError);
393 CGLDestroyPixelFormat(pixel_format);
394 CGLSetFullScreen(cgl_context_);
395 CGLSetCurrentContext(cgl_context_);
396
397 size_t buffer_size = width * height * sizeof(uint32_t);
398 pixel_buffer_object_.Init(cgl_context_, buffer_size);
399 }
400
401 media::VideoFrame::Format VideoFrameCapturerMac::pixel_format() const {
402 return pixel_format_;
403 }
404
405 void VideoFrameCapturerMac::ClearInvalidRegion() {
406 helper_.ClearInvalidRegion();
407 }
408
409 void VideoFrameCapturerMac::InvalidateRegion(const SkRegion& invalid_region) {
410 helper_.InvalidateRegion(invalid_region);
411 }
412
413 void VideoFrameCapturerMac::InvalidateScreen(const SkISize& size) {
414 helper_.InvalidateScreen(size);
415 }
416
417 void VideoFrameCapturerMac::InvalidateFullScreen() {
418 helper_.InvalidateFullScreen();
419 }
420
421 void VideoFrameCapturerMac::CaptureInvalidRegion(
422 const CaptureCompletedCallback& callback) {
423 // Only allow captures when the display configuration is not occurring.
424 scoped_refptr<CaptureData> data;
425
426 // Critical section shared with DisplaysReconfigured(...).
427 CHECK(display_configuration_capture_event_.TimedWait(
428 base::TimeDelta::FromSeconds(kDisplayReconfigurationTimeoutInSeconds)));
429 SkRegion region;
430 helper_.SwapInvalidRegion(&region);
431 VideoFrameBuffer& current_buffer = buffers_[current_buffer_];
432 current_buffer.Update();
433
434 bool flip = false; // GL capturers need flipping.
435 if (display_create_image_func_ != NULL) {
436 // Lion requires us to use their new APIs for doing screen capture.
437 CgBlitPostLion(current_buffer, region);
438 } else if (cgl_context_) {
439 flip = true;
440 if (pixel_buffer_object_.get() != 0) {
441 GlBlitFast(current_buffer, region);
442 } else {
443 // See comment in scoped_pixel_buffer_object::Init about why the slow
444 // path is always used on 10.5.
445 GlBlitSlow(current_buffer);
446 }
447 } else {
448 CgBlitPreLion(current_buffer, region);
449 }
450
451 DataPlanes planes;
452 planes.data[0] = current_buffer.ptr();
453 planes.strides[0] = current_buffer.bytes_per_row();
454 if (flip) {
455 planes.strides[0] = -planes.strides[0];
456 planes.data[0] +=
457 (current_buffer.size().height() - 1) * current_buffer.bytes_per_row();
458 }
459
460 data = new CaptureData(planes, current_buffer.size(), pixel_format());
461 data->mutable_dirty_region() = region;
462
463 current_buffer_ = (current_buffer_ + 1) % kNumBuffers;
464 helper_.set_size_most_recent(data->size());
465 display_configuration_capture_event_.Signal();
466
467 CaptureCursor();
468
469 callback.Run(data);
470 }
471
472 void VideoFrameCapturerMac::CaptureCursor() {
473 if (cursor_shape_changed_callback_.is_null()) {
474 return;
475 }
476
477 NSCursor* cursor = [NSCursor currentSystemCursor];
478 if (cursor == nil) {
479 return;
480 }
481
482 NSImage* nsimage = [cursor image];
483 NSPoint hotspot = [cursor hotSpot];
484 NSSize size = [nsimage size];
485 CGImageRef image = [nsimage CGImageForProposedRect:NULL
486 context:nil
487 hints:nil];
488 if (image == nil) {
489 return;
490 }
491
492 if (CGImageGetBitsPerPixel(image) != 32 ||
493 CGImageGetBytesPerRow(image) != (size.width * 4) ||
494 CGImageGetBitsPerComponent(image) != 8) {
495 return;
496 }
497
498 // Compare the current cursor with the last one we sent to the client
499 // and exit if the cursor is the same.
500 if (current_cursor_.get() != NULL) {
501 CGImageRef current = current_cursor_.get();
502 if (CGImageGetWidth(image) == CGImageGetWidth(current) &&
503 CGImageGetHeight(image) == CGImageGetHeight(current) &&
504 CGImageGetBitsPerPixel(image) == CGImageGetBitsPerPixel(current) &&
505 CGImageGetBytesPerRow(image) == CGImageGetBytesPerRow(current) &&
506 CGImageGetBitsPerComponent(image) ==
507 CGImageGetBitsPerComponent(current)) {
508 CGDataProviderRef provider_new = CGImageGetDataProvider(image);
509 base::mac::ScopedCFTypeRef<CFDataRef> data_ref_new(
510 CGDataProviderCopyData(provider_new));
511 CGDataProviderRef provider_current = CGImageGetDataProvider(current);
512 base::mac::ScopedCFTypeRef<CFDataRef> data_ref_current(
513 CGDataProviderCopyData(provider_current));
514
515 if (data_ref_new.get() != NULL && data_ref_current.get() != NULL) {
516 int data_size = CFDataGetLength(data_ref_new);
517 CHECK(data_size == CFDataGetLength(data_ref_current));
518 const uint8* data_new = CFDataGetBytePtr(data_ref_new);
519 const uint8* data_current = CFDataGetBytePtr(data_ref_current);
520 if (memcmp(data_new, data_current, data_size) == 0) {
521 return;
522 }
523 }
524 }
525 }
526
527 VLOG(3) << "Sending cursor: " << size.width << "x" << size.height;
528
529 CGDataProviderRef provider = CGImageGetDataProvider(image);
530 base::mac::ScopedCFTypeRef<CFDataRef> image_data_ref(
531 CGDataProviderCopyData(provider));
532 if (image_data_ref.get() == NULL) {
533 return;
534 }
535 const uint8* cursor_src_data = CFDataGetBytePtr(image_data_ref);
536 int data_size = CFDataGetLength(image_data_ref);
537
538 // Create a CursorShapeInfo proto that describes the cursor and pass it to
539 // the client.
540 scoped_ptr<protocol::CursorShapeInfo> cursor_proto(
541 new protocol::CursorShapeInfo());
542 cursor_proto->mutable_data()->resize(data_size);
543 uint8* cursor_tgt_data = const_cast<uint8*>(reinterpret_cast<const uint8*>(
544 cursor_proto->mutable_data()->data()));
545
546 memcpy(cursor_tgt_data, cursor_src_data, data_size);
547
548 cursor_proto->set_width(size.width);
549 cursor_proto->set_height(size.height);
550 cursor_proto->set_hotspot_x(hotspot.x);
551 cursor_proto->set_hotspot_y(hotspot.y);
552 cursor_shape_changed_callback_.Run(cursor_proto.Pass());
553
554 // Record the last cursor image that we sent.
555 current_cursor_.reset(CGImageCreateCopy(image));
556 }
557
558 void VideoFrameCapturerMac::GlBlitFast(const VideoFrameBuffer& buffer,
559 const SkRegion& region) {
560 const int buffer_height = buffer.size().height();
561 const int buffer_width = buffer.size().width();
562
563 // Clip to the size of our current screen.
564 SkIRect clip_rect = SkIRect::MakeWH(buffer_width, buffer_height);
565 if (last_buffer_) {
566 // We are doing double buffer for the capture data so we just need to copy
567 // the invalid region from the previous capture in the current buffer.
568 // TODO(hclam): We can reduce the amount of copying here by subtracting
569 // |capturer_helper_|s region from |last_invalid_region_|.
570 // http://crbug.com/92354
571
572 // Since the image obtained from OpenGL is upside-down, need to do some
573 // magic here to copy the correct rectangle.
574 const int y_offset = (buffer_height - 1) * buffer.bytes_per_row();
575 for(SkRegion::Iterator i(last_invalid_region_); !i.done(); i.next()) {
576 SkIRect copy_rect = i.rect();
577 if (copy_rect.intersect(clip_rect)) {
578 CopyRect(last_buffer_ + y_offset,
579 -buffer.bytes_per_row(),
580 buffer.ptr() + y_offset,
581 -buffer.bytes_per_row(),
582 4, // Bytes for pixel for RGBA.
583 copy_rect);
584 }
585 }
586 }
587 last_buffer_ = buffer.ptr();
588 last_invalid_region_ = region;
589
590 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_;
591 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pixel_buffer_object_.get());
592 glReadPixels(0, 0, buffer_width, buffer_height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
593 GLubyte* ptr = static_cast<GLubyte*>(
594 glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB));
595 if (ptr == NULL) {
596 // If the buffer can't be mapped, assume that it's no longer valid and
597 // release it.
598 pixel_buffer_object_.Release();
599 } else {
600 // Copy only from the dirty rects. Since the image obtained from OpenGL is
601 // upside-down we need to do some magic here to copy the correct rectangle.
602 const int y_offset = (buffer_height - 1) * buffer.bytes_per_row();
603 for(SkRegion::Iterator i(region); !i.done(); i.next()) {
604 SkIRect copy_rect = i.rect();
605 if (copy_rect.intersect(clip_rect)) {
606 CopyRect(ptr + y_offset,
607 -buffer.bytes_per_row(),
608 buffer.ptr() + y_offset,
609 -buffer.bytes_per_row(),
610 4, // Bytes for pixel for RGBA.
611 copy_rect);
612 }
613 }
614 }
615 if (!glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB)) {
616 // If glUnmapBuffer returns false, then the contents of the data store are
617 // undefined. This might be because the screen mode has changed, in which
618 // case it will be recreated in ScreenConfigurationChanged, but releasing
619 // the object here is the best option. Capturing will fall back on
620 // GlBlitSlow until such time as the pixel buffer object is recreated.
621 pixel_buffer_object_.Release();
622 }
623 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
624 }
625
626 void VideoFrameCapturerMac::GlBlitSlow(const VideoFrameBuffer& buffer) {
627 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_;
628 glReadBuffer(GL_FRONT);
629 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
630 glPixelStorei(GL_PACK_ALIGNMENT, 4); // Force 4-byte alignment.
631 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
632 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
633 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
634 // Read a block of pixels from the frame buffer.
635 glReadPixels(0, 0, buffer.size().width(), buffer.size().height(),
636 GL_BGRA, GL_UNSIGNED_BYTE, buffer.ptr());
637 glPopClientAttrib();
638 }
639
640 void VideoFrameCapturerMac::CgBlitPreLion(const VideoFrameBuffer& buffer,
641 const SkRegion& region) {
642 const int buffer_height = buffer.size().height();
643 const int buffer_width = buffer.size().width();
644
645 // Clip to the size of our current screen.
646 SkIRect clip_rect = SkIRect::MakeWH(buffer_width, buffer_height);
647
648 if (last_buffer_)
649 memcpy(buffer.ptr(), last_buffer_, buffer.bytes_per_row() * buffer_height);
650 last_buffer_ = buffer.ptr();
651 CGDirectDisplayID main_display = CGMainDisplayID();
652 uint8* display_base_address =
653 reinterpret_cast<uint8*>(CGDisplayBaseAddress(main_display));
654 int src_bytes_per_row = CGDisplayBytesPerRow(main_display);
655 int src_bytes_per_pixel = CGDisplayBitsPerPixel(main_display) / 8;
656 // TODO(hclam): We can reduce the amount of copying here by subtracting
657 // |capturer_helper_|s region from |last_invalid_region_|.
658 // http://crbug.com/92354
659 for(SkRegion::Iterator i(region); !i.done(); i.next()) {
660 SkIRect copy_rect = i.rect();
661 if (copy_rect.intersect(clip_rect)) {
662 CopyRect(display_base_address,
663 src_bytes_per_row,
664 buffer.ptr(),
665 buffer.bytes_per_row(),
666 src_bytes_per_pixel,
667 copy_rect);
668 }
669 }
670 }
671
672 void VideoFrameCapturerMac::CgBlitPostLion(const VideoFrameBuffer& buffer,
673 const SkRegion& region) {
674 const int buffer_height = buffer.size().height();
675 const int buffer_width = buffer.size().width();
676
677 // Clip to the size of our current screen.
678 SkIRect clip_rect = SkIRect::MakeWH(buffer_width, buffer_height);
679
680 if (last_buffer_)
681 memcpy(buffer.ptr(), last_buffer_, buffer.bytes_per_row() * buffer_height);
682 last_buffer_ = buffer.ptr();
683 CGDirectDisplayID display = CGMainDisplayID();
684 base::mac::ScopedCFTypeRef<CGImageRef> image(
685 display_create_image_func_(display));
686 if (image.get() == NULL)
687 return;
688 CGDataProviderRef provider = CGImageGetDataProvider(image);
689 base::mac::ScopedCFTypeRef<CFDataRef> data(CGDataProviderCopyData(provider));
690 if (data.get() == NULL)
691 return;
692 const uint8* display_base_address = CFDataGetBytePtr(data);
693 int src_bytes_per_row = CGImageGetBytesPerRow(image);
694 int src_bytes_per_pixel = CGImageGetBitsPerPixel(image) / 8;
695 // TODO(hclam): We can reduce the amount of copying here by subtracting
696 // |capturer_helper_|s region from |last_invalid_region_|.
697 // http://crbug.com/92354
698 for(SkRegion::Iterator i(region); !i.done(); i.next()) {
699 SkIRect copy_rect = i.rect();
700 if (copy_rect.intersect(clip_rect)) {
701 CopyRect(display_base_address,
702 src_bytes_per_row,
703 buffer.ptr(),
704 buffer.bytes_per_row(),
705 src_bytes_per_pixel,
706 copy_rect);
707 }
708 }
709 }
710
711 const SkISize& VideoFrameCapturerMac::size_most_recent() const {
712 return helper_.size_most_recent();
713 }
714
715 void VideoFrameCapturerMac::ScreenRefresh(CGRectCount count,
716 const CGRect* rect_array) {
717 SkIRect skirect_array[count];
718 for (CGRectCount i = 0; i < count; ++i) {
719 skirect_array[i] = CGRectToSkIRect(rect_array[i]);
720 }
721 SkRegion region;
722 region.setRects(skirect_array, count);
723 InvalidateRegion(region);
724 }
725
726 void VideoFrameCapturerMac::ScreenUpdateMove(CGScreenUpdateMoveDelta delta,
727 size_t count,
728 const CGRect* rect_array) {
729 SkIRect skirect_new_array[count];
730 for (CGRectCount i = 0; i < count; ++i) {
731 CGRect rect = rect_array[i];
732 rect = CGRectOffset(rect, delta.dX, delta.dY);
733 skirect_new_array[i] = CGRectToSkIRect(rect);
734 }
735 SkRegion region;
736 region.setRects(skirect_new_array, count);
737 InvalidateRegion(region);
738 }
739
740 void VideoFrameCapturerMac::DisplaysReconfigured(
741 CGDirectDisplayID display,
742 CGDisplayChangeSummaryFlags flags) {
743 if (display == CGMainDisplayID()) {
744 if (flags & kCGDisplayBeginConfigurationFlag) {
745 // Wait on |display_configuration_capture_event_| to prevent more
746 // captures from occurring on a different thread while the displays
747 // are being reconfigured.
748 CHECK(display_configuration_capture_event_.TimedWait(
749 base::TimeDelta::FromSeconds(
750 kDisplayReconfigurationTimeoutInSeconds)));
751 } else {
752 ScreenConfigurationChanged();
753 // Now that the configuration has changed, signal the event.
754 display_configuration_capture_event_.Signal();
755 }
756 }
757 }
758
759 void VideoFrameCapturerMac::ScreenRefreshCallback(CGRectCount count,
760 const CGRect* rect_array,
761 void* user_parameter) {
762 VideoFrameCapturerMac* capturer = reinterpret_cast<VideoFrameCapturerMac*>(
763 user_parameter);
764 capturer->ScreenRefresh(count, rect_array);
765 }
766
767 void VideoFrameCapturerMac::ScreenUpdateMoveCallback(
768 CGScreenUpdateMoveDelta delta,
769 size_t count,
770 const CGRect* rect_array,
771 void* user_parameter) {
772 VideoFrameCapturerMac* capturer = reinterpret_cast<VideoFrameCapturerMac*>(
773 user_parameter);
774 capturer->ScreenUpdateMove(delta, count, rect_array);
775 }
776
777 void VideoFrameCapturerMac::DisplaysReconfiguredCallback(
778 CGDirectDisplayID display,
779 CGDisplayChangeSummaryFlags flags,
780 void* user_parameter) {
781 VideoFrameCapturerMac* capturer = reinterpret_cast<VideoFrameCapturerMac*>(
782 user_parameter);
783 capturer->DisplaysReconfigured(display, flags);
784 }
785
786 } // namespace
787
788 // static
789 VideoFrameCapturer* VideoFrameCapturer::Create() {
790 VideoFrameCapturerMac* capturer = new VideoFrameCapturerMac();
791 if (!capturer->Init()) {
792 delete capturer;
793 capturer = NULL;
794 }
795 return capturer;
796 }
797
798 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698