OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/capturer.h" | 5 #include "remoting/host/capturer.h" |
6 | 6 |
7 #include <ApplicationServices/ApplicationServices.h> | 7 #include <ApplicationServices/ApplicationServices.h> |
8 #include <OpenGL/CGLMacro.h> | 8 #include <OpenGL/CGLMacro.h> |
9 #include <OpenGL/OpenGL.h> | 9 #include <OpenGL/OpenGL.h> |
10 | 10 |
11 #include <stddef.h> | 11 #include <stddef.h> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/mac/mac_util.h" | 14 #include "base/mac/mac_util.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/synchronization/waitable_event.h" |
16 #include "remoting/base/util.h" | 17 #include "remoting/base/util.h" |
17 #include "remoting/host/capturer_helper.h" | 18 #include "remoting/host/capturer_helper.h" |
18 #include "skia/ext/skia_utils_mac.h" | 19 #include "skia/ext/skia_utils_mac.h" |
19 | 20 |
20 namespace remoting { | 21 namespace remoting { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
| 25 // The amount of time allowed for displays to reconfigure. |
| 26 const int64 kDisplayReconfigurationTimeoutInSeconds = 10; |
| 27 |
24 class scoped_pixel_buffer_object { | 28 class scoped_pixel_buffer_object { |
25 public: | 29 public: |
26 scoped_pixel_buffer_object(); | 30 scoped_pixel_buffer_object(); |
27 ~scoped_pixel_buffer_object(); | 31 ~scoped_pixel_buffer_object(); |
28 | 32 |
29 bool Init(CGLContextObj cgl_context, int size_in_bytes); | 33 bool Init(CGLContextObj cgl_context, int size_in_bytes); |
30 void Release(); | 34 void Release(); |
31 | 35 |
32 GLuint get() const { return pixel_buffer_object_; } | 36 GLuint get() const { return pixel_buffer_object_; } |
33 | 37 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 124 |
121 DISALLOW_COPY_AND_ASSIGN(VideoFrameBuffer); | 125 DISALLOW_COPY_AND_ASSIGN(VideoFrameBuffer); |
122 }; | 126 }; |
123 | 127 |
124 // A class to perform capturing for mac. | 128 // A class to perform capturing for mac. |
125 class CapturerMac : public Capturer { | 129 class CapturerMac : public Capturer { |
126 public: | 130 public: |
127 CapturerMac(); | 131 CapturerMac(); |
128 virtual ~CapturerMac(); | 132 virtual ~CapturerMac(); |
129 | 133 |
130 // Enable or disable capturing. Capturing should be disabled while a screen | |
131 // reconfiguration is in progress, otherwise reading from the screen base | |
132 // address is likely to segfault. | |
133 void EnableCapture(bool enable); | |
134 | |
135 // Capturer interface. | 134 // Capturer interface. |
136 virtual void ScreenConfigurationChanged() OVERRIDE; | 135 virtual void ScreenConfigurationChanged() OVERRIDE; |
137 virtual media::VideoFrame::Format pixel_format() const OVERRIDE; | 136 virtual media::VideoFrame::Format pixel_format() const OVERRIDE; |
138 virtual void ClearInvalidRegion() OVERRIDE; | 137 virtual void ClearInvalidRegion() OVERRIDE; |
139 virtual void InvalidateRegion(const SkRegion& invalid_region) OVERRIDE; | 138 virtual void InvalidateRegion(const SkRegion& invalid_region) OVERRIDE; |
140 virtual void InvalidateScreen(const gfx::Size& size) OVERRIDE; | 139 virtual void InvalidateScreen(const gfx::Size& size) OVERRIDE; |
141 virtual void InvalidateFullScreen() OVERRIDE; | 140 virtual void InvalidateFullScreen() OVERRIDE; |
142 virtual void CaptureInvalidRegion(CaptureCompletedCallback* callback) | 141 virtual void CaptureInvalidRegion(CaptureCompletedCallback* callback) |
143 OVERRIDE; | 142 OVERRIDE; |
144 virtual const gfx::Size& size_most_recent() const OVERRIDE; | 143 virtual const gfx::Size& size_most_recent() const OVERRIDE; |
145 | 144 |
146 private: | 145 private: |
147 void GlBlitFast(const VideoFrameBuffer& buffer, const SkRegion& region); | 146 void GlBlitFast(const VideoFrameBuffer& buffer, const SkRegion& region); |
148 void GlBlitSlow(const VideoFrameBuffer& buffer); | 147 void GlBlitSlow(const VideoFrameBuffer& buffer); |
149 void CgBlit(const VideoFrameBuffer& buffer, const SkRegion& region); | 148 void CgBlit(const VideoFrameBuffer& buffer, const SkRegion& region); |
150 void CaptureRegion(const SkRegion& region, | 149 void CaptureRegion(const SkRegion& region, |
151 CaptureCompletedCallback* callback); | 150 CaptureCompletedCallback* callback); |
152 | 151 |
153 void ScreenRefresh(CGRectCount count, const CGRect *rect_array); | 152 void ScreenRefresh(CGRectCount count, const CGRect *rect_array); |
154 void ScreenUpdateMove(CGScreenUpdateMoveDelta delta, | 153 void ScreenUpdateMove(CGScreenUpdateMoveDelta delta, |
155 size_t count, | 154 size_t count, |
156 const CGRect *rect_array); | 155 const CGRect *rect_array); |
| 156 void DisplaysReconfigured(CGDirectDisplayID display, |
| 157 CGDisplayChangeSummaryFlags flags); |
157 static void ScreenRefreshCallback(CGRectCount count, | 158 static void ScreenRefreshCallback(CGRectCount count, |
158 const CGRect *rect_array, | 159 const CGRect *rect_array, |
159 void *user_parameter); | 160 void *user_parameter); |
160 static void ScreenUpdateMoveCallback(CGScreenUpdateMoveDelta delta, | 161 static void ScreenUpdateMoveCallback(CGScreenUpdateMoveDelta delta, |
161 size_t count, | 162 size_t count, |
162 const CGRect *rect_array, | 163 const CGRect *rect_array, |
163 void *user_parameter); | 164 void *user_parameter); |
164 static void DisplaysReconfiguredCallback(CGDirectDisplayID display, | 165 static void DisplaysReconfiguredCallback(CGDirectDisplayID display, |
165 CGDisplayChangeSummaryFlags flags, | 166 CGDisplayChangeSummaryFlags flags, |
166 void *user_parameter); | 167 void *user_parameter); |
(...skipping 14 matching lines...) Expand all Loading... |
181 // The previous buffer into which we captured, or NULL for the first capture | 182 // The previous buffer into which we captured, or NULL for the first capture |
182 // for a particular screen resolution. | 183 // for a particular screen resolution. |
183 uint8* last_buffer_; | 184 uint8* last_buffer_; |
184 | 185 |
185 // Contains an invalid region from the previous capture. | 186 // Contains an invalid region from the previous capture. |
186 SkRegion last_invalid_region_; | 187 SkRegion last_invalid_region_; |
187 | 188 |
188 // Format of pixels returned in buffer. | 189 // Format of pixels returned in buffer. |
189 media::VideoFrame::Format pixel_format_; | 190 media::VideoFrame::Format pixel_format_; |
190 | 191 |
191 bool capturing_; | 192 // Acts as a critical section around our display configuration data |
| 193 // structures. Specifically cgl_context_ and pixel_buffer_object_. |
| 194 base::WaitableEvent display_configuration_capture_event_; |
192 | 195 |
193 DISALLOW_COPY_AND_ASSIGN(CapturerMac); | 196 DISALLOW_COPY_AND_ASSIGN(CapturerMac); |
194 }; | 197 }; |
195 | 198 |
196 CapturerMac::CapturerMac() | 199 CapturerMac::CapturerMac() |
197 : cgl_context_(NULL), | 200 : cgl_context_(NULL), |
198 current_buffer_(0), | 201 current_buffer_(0), |
199 last_buffer_(NULL), | 202 last_buffer_(NULL), |
200 pixel_format_(media::VideoFrame::RGB32), | 203 pixel_format_(media::VideoFrame::RGB32), |
201 capturing_(true) { | 204 display_configuration_capture_event_(false, true) { |
202 // TODO(dmaclach): move this initialization out into session_manager, | 205 // TODO(dmaclach): move this initialization out into session_manager, |
203 // or at least have session_manager call into here to initialize it. | 206 // or at least have session_manager call into here to initialize it. |
204 CGError err = | 207 CGError err = |
205 CGRegisterScreenRefreshCallback(CapturerMac::ScreenRefreshCallback, | 208 CGRegisterScreenRefreshCallback(CapturerMac::ScreenRefreshCallback, |
206 this); | 209 this); |
207 DCHECK_EQ(err, kCGErrorSuccess); | 210 DCHECK_EQ(err, kCGErrorSuccess); |
208 err = CGScreenRegisterMoveCallback(CapturerMac::ScreenUpdateMoveCallback, | 211 err = CGScreenRegisterMoveCallback(CapturerMac::ScreenUpdateMoveCallback, |
209 this); | 212 this); |
210 DCHECK_EQ(err, kCGErrorSuccess); | 213 DCHECK_EQ(err, kCGErrorSuccess); |
211 err = CGDisplayRegisterReconfigurationCallback( | 214 err = CGDisplayRegisterReconfigurationCallback( |
212 CapturerMac::DisplaysReconfiguredCallback, this); | 215 CapturerMac::DisplaysReconfiguredCallback, this); |
213 DCHECK_EQ(err, kCGErrorSuccess); | 216 DCHECK_EQ(err, kCGErrorSuccess); |
214 ScreenConfigurationChanged(); | 217 ScreenConfigurationChanged(); |
215 } | 218 } |
216 | 219 |
217 CapturerMac::~CapturerMac() { | 220 CapturerMac::~CapturerMac() { |
218 ReleaseBuffers(); | 221 ReleaseBuffers(); |
219 CGUnregisterScreenRefreshCallback(CapturerMac::ScreenRefreshCallback, this); | 222 CGUnregisterScreenRefreshCallback(CapturerMac::ScreenRefreshCallback, this); |
220 CGScreenUnregisterMoveCallback(CapturerMac::ScreenUpdateMoveCallback, this); | 223 CGScreenUnregisterMoveCallback(CapturerMac::ScreenUpdateMoveCallback, this); |
221 CGDisplayRemoveReconfigurationCallback( | 224 CGDisplayRemoveReconfigurationCallback( |
222 CapturerMac::DisplaysReconfiguredCallback, this); | 225 CapturerMac::DisplaysReconfiguredCallback, this); |
223 } | 226 } |
224 | 227 |
225 void CapturerMac::EnableCapture(bool enable) { | |
226 capturing_ = enable; | |
227 } | |
228 | |
229 void CapturerMac::ReleaseBuffers() { | 228 void CapturerMac::ReleaseBuffers() { |
230 if (cgl_context_) { | 229 if (cgl_context_) { |
231 pixel_buffer_object_.Release(); | 230 pixel_buffer_object_.Release(); |
232 CGLDestroyContext(cgl_context_); | 231 CGLDestroyContext(cgl_context_); |
233 cgl_context_ = NULL; | 232 cgl_context_ = NULL; |
234 } | 233 } |
235 // The buffers might be in use by the encoder, so don't delete them here. | 234 // The buffers might be in use by the encoder, so don't delete them here. |
236 // Instead, mark them as "needs update"; next time the buffers are used by | 235 // Instead, mark them as "needs update"; next time the buffers are used by |
237 // the capturer, they will be recreated if necessary. | 236 // the capturer, they will be recreated if necessary. |
238 for (int i = 0; i < kNumBuffers; ++i) { | 237 for (int i = 0; i < kNumBuffers; ++i) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 290 |
292 void CapturerMac::InvalidateScreen(const gfx::Size& size) { | 291 void CapturerMac::InvalidateScreen(const gfx::Size& size) { |
293 helper_.InvalidateScreen(size); | 292 helper_.InvalidateScreen(size); |
294 } | 293 } |
295 | 294 |
296 void CapturerMac::InvalidateFullScreen() { | 295 void CapturerMac::InvalidateFullScreen() { |
297 helper_.InvalidateFullScreen(); | 296 helper_.InvalidateFullScreen(); |
298 } | 297 } |
299 | 298 |
300 void CapturerMac::CaptureInvalidRegion(CaptureCompletedCallback* callback) { | 299 void CapturerMac::CaptureInvalidRegion(CaptureCompletedCallback* callback) { |
| 300 // Only allow captures when the display configuration is not occurring. |
301 scoped_refptr<CaptureData> data; | 301 scoped_refptr<CaptureData> data; |
302 if (capturing_) { | |
303 SkRegion region; | |
304 helper_.SwapInvalidRegion(®ion); | |
305 VideoFrameBuffer& current_buffer = buffers_[current_buffer_]; | |
306 current_buffer.Update(); | |
307 | 302 |
308 bool flip = true; // GL capturers need flipping. | 303 // Critical section shared with DisplaysReconfigured(...). |
309 if (cgl_context_) { | 304 CHECK(display_configuration_capture_event_.TimedWait( |
310 if (pixel_buffer_object_.get() != 0) { | 305 base::TimeDelta::FromSeconds(kDisplayReconfigurationTimeoutInSeconds))); |
311 GlBlitFast(current_buffer, region); | 306 SkRegion region; |
312 } else { | 307 helper_.SwapInvalidRegion(®ion); |
313 // See comment in scoped_pixel_buffer_object::Init about why the slow | 308 VideoFrameBuffer& current_buffer = buffers_[current_buffer_]; |
314 // path is always used on 10.5. | 309 current_buffer.Update(); |
315 GlBlitSlow(current_buffer); | 310 |
316 } | 311 bool flip = true; // GL capturers need flipping. |
| 312 if (cgl_context_) { |
| 313 if (pixel_buffer_object_.get() != 0) { |
| 314 GlBlitFast(current_buffer, region); |
317 } else { | 315 } else { |
318 CgBlit(current_buffer, region); | 316 // See comment in scoped_pixel_buffer_object::Init about why the slow |
319 flip = false; | 317 // path is always used on 10.5. |
| 318 GlBlitSlow(current_buffer); |
320 } | 319 } |
| 320 } else { |
| 321 CgBlit(current_buffer, region); |
| 322 flip = false; |
| 323 } |
321 | 324 |
322 DataPlanes planes; | 325 DataPlanes planes; |
323 planes.data[0] = current_buffer.ptr(); | 326 planes.data[0] = current_buffer.ptr(); |
324 planes.strides[0] = current_buffer.bytes_per_row(); | 327 planes.strides[0] = current_buffer.bytes_per_row(); |
325 if (flip) { | 328 if (flip) { |
326 planes.strides[0] = -planes.strides[0]; | 329 planes.strides[0] = -planes.strides[0]; |
327 planes.data[0] += | 330 planes.data[0] += |
328 (current_buffer.size().height() - 1) * current_buffer.bytes_per_row(); | 331 (current_buffer.size().height() - 1) * current_buffer.bytes_per_row(); |
329 } | 332 } |
330 | 333 |
331 data = new CaptureData(planes, gfx::Size(current_buffer.size()), | 334 data = new CaptureData(planes, gfx::Size(current_buffer.size()), |
332 pixel_format()); | 335 pixel_format()); |
333 data->mutable_dirty_region() = region; | 336 data->mutable_dirty_region() = region; |
334 | 337 |
335 current_buffer_ = (current_buffer_ + 1) % kNumBuffers; | 338 current_buffer_ = (current_buffer_ + 1) % kNumBuffers; |
336 helper_.set_size_most_recent(data->size()); | 339 helper_.set_size_most_recent(data->size()); |
337 } | 340 display_configuration_capture_event_.Signal(); |
338 | 341 |
339 callback->Run(data); | 342 callback->Run(data); |
340 delete callback; | 343 delete callback; |
341 } | 344 } |
342 | 345 |
343 void CapturerMac::GlBlitFast(const VideoFrameBuffer& buffer, | 346 void CapturerMac::GlBlitFast(const VideoFrameBuffer& buffer, |
344 const SkRegion& region) { | 347 const SkRegion& region) { |
| 348 const int buffer_height = buffer.size().height(); |
| 349 const int buffer_width = buffer.size().width(); |
| 350 |
| 351 // Clip to the size of our current screen. |
| 352 SkIRect clip_rect = SkIRect::MakeWH(buffer_width, buffer_height); |
345 if (last_buffer_) { | 353 if (last_buffer_) { |
346 // We are doing double buffer for the capture data so we just need to copy | 354 // We are doing double buffer for the capture data so we just need to copy |
347 // the invalid region from the previous capture in the current buffer. | 355 // the invalid region from the previous capture in the current buffer. |
348 // TODO(hclam): We can reduce the amount of copying here by subtracting | 356 // TODO(hclam): We can reduce the amount of copying here by subtracting |
349 // |capturer_helper_|s region from |last_invalid_region_|. | 357 // |capturer_helper_|s region from |last_invalid_region_|. |
350 // http://crbug.com/92354 | 358 // http://crbug.com/92354 |
351 | 359 |
352 // Since the image obtained from OpenGL is upside-down, need to do some | 360 // Since the image obtained from OpenGL is upside-down, need to do some |
353 // magic here to copy the correct rectangle. | 361 // magic here to copy the correct rectangle. |
354 const int y_offset = (buffer.size().height() - 1) * buffer.bytes_per_row(); | 362 const int y_offset = (buffer_height - 1) * buffer.bytes_per_row(); |
355 for(SkRegion::Iterator i(last_invalid_region_); !i.done(); i.next()) { | 363 for(SkRegion::Iterator i(last_invalid_region_); !i.done(); i.next()) { |
| 364 SkIRect copy_rect = i.rect(); |
| 365 copy_rect.intersect(clip_rect); |
356 CopyRect(last_buffer_ + y_offset, | 366 CopyRect(last_buffer_ + y_offset, |
357 -buffer.bytes_per_row(), | 367 -buffer.bytes_per_row(), |
358 buffer.ptr() + y_offset, | 368 buffer.ptr() + y_offset, |
359 -buffer.bytes_per_row(), | 369 -buffer.bytes_per_row(), |
360 4, // Bytes for pixel for RGBA. | 370 4, // Bytes for pixel for RGBA. |
361 i.rect()); | 371 copy_rect); |
362 } | 372 } |
363 } | 373 } |
364 last_buffer_ = buffer.ptr(); | 374 last_buffer_ = buffer.ptr(); |
365 last_invalid_region_ = region; | 375 last_invalid_region_ = region; |
366 | 376 |
367 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_; | 377 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_; |
368 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pixel_buffer_object_.get()); | 378 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pixel_buffer_object_.get()); |
369 glReadPixels(0, 0, buffer.size().width(), buffer.size().height(), | 379 glReadPixels(0, 0, buffer_width, buffer_height, GL_BGRA, GL_UNSIGNED_BYTE, 0); |
370 GL_BGRA, GL_UNSIGNED_BYTE, 0); | |
371 GLubyte* ptr = static_cast<GLubyte*>( | 380 GLubyte* ptr = static_cast<GLubyte*>( |
372 glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB)); | 381 glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB)); |
373 if (ptr == NULL) { | 382 if (ptr == NULL) { |
374 // If the buffer can't be mapped, assume that it's no longer valid and | 383 // If the buffer can't be mapped, assume that it's no longer valid and |
375 // release it. | 384 // release it. |
376 pixel_buffer_object_.Release(); | 385 pixel_buffer_object_.Release(); |
377 } else { | 386 } else { |
378 // Copy only from the dirty rects. Since the image obtained from OpenGL is | 387 // Copy only from the dirty rects. Since the image obtained from OpenGL is |
379 // upside-down we need to do some magic here to copy the correct rectangle. | 388 // upside-down we need to do some magic here to copy the correct rectangle. |
380 const int y_offset = (buffer.size().height() - 1) * buffer.bytes_per_row(); | 389 const int y_offset = (buffer_height - 1) * buffer.bytes_per_row(); |
381 for(SkRegion::Iterator i(region); !i.done(); i.next()) { | 390 for(SkRegion::Iterator i(region); !i.done(); i.next()) { |
| 391 SkIRect copy_rect = i.rect(); |
| 392 copy_rect.intersect(clip_rect); |
382 CopyRect(ptr + y_offset, | 393 CopyRect(ptr + y_offset, |
383 -buffer.bytes_per_row(), | 394 -buffer.bytes_per_row(), |
384 buffer.ptr() + y_offset, | 395 buffer.ptr() + y_offset, |
385 -buffer.bytes_per_row(), | 396 -buffer.bytes_per_row(), |
386 4, // Bytes for pixel for RGBA. | 397 4, // Bytes for pixel for RGBA. |
387 i.rect()); | 398 copy_rect); |
388 } | 399 } |
389 } | 400 } |
390 if (!glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB)) { | 401 if (!glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB)) { |
391 // If glUnmapBuffer returns false, then the contents of the data store are | 402 // If glUnmapBuffer returns false, then the contents of the data store are |
392 // undefined. This might be because the screen mode has changed, in which | 403 // undefined. This might be because the screen mode has changed, in which |
393 // case it will be recreated in ScreenConfigurationChanged, but releasing | 404 // case it will be recreated in ScreenConfigurationChanged, but releasing |
394 // the object here is the best option. Capturing will fall back on | 405 // the object here is the best option. Capturing will fall back on |
395 // GlBlitSlow until such time as the pixel buffer object is recreated. | 406 // GlBlitSlow until such time as the pixel buffer object is recreated. |
396 pixel_buffer_object_.Release(); | 407 pixel_buffer_object_.Release(); |
397 } | 408 } |
398 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); | 409 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); |
399 } | 410 } |
400 | 411 |
401 void CapturerMac::GlBlitSlow(const VideoFrameBuffer& buffer) { | 412 void CapturerMac::GlBlitSlow(const VideoFrameBuffer& buffer) { |
402 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_; | 413 CGLContextObj CGL_MACRO_CONTEXT = cgl_context_; |
403 glReadBuffer(GL_FRONT); | 414 glReadBuffer(GL_FRONT); |
404 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); | 415 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); |
405 glPixelStorei(GL_PACK_ALIGNMENT, 4); // Force 4-byte alignment. | 416 glPixelStorei(GL_PACK_ALIGNMENT, 4); // Force 4-byte alignment. |
406 glPixelStorei(GL_PACK_ROW_LENGTH, 0); | 417 glPixelStorei(GL_PACK_ROW_LENGTH, 0); |
407 glPixelStorei(GL_PACK_SKIP_ROWS, 0); | 418 glPixelStorei(GL_PACK_SKIP_ROWS, 0); |
408 glPixelStorei(GL_PACK_SKIP_PIXELS, 0); | 419 glPixelStorei(GL_PACK_SKIP_PIXELS, 0); |
409 // Read a block of pixels from the frame buffer. | 420 // Read a block of pixels from the frame buffer. |
410 glReadPixels(0, 0, buffer.size().width(), buffer.size().height(), | 421 glReadPixels(0, 0, buffer.size().width(), buffer.size().height(), |
411 GL_BGRA, GL_UNSIGNED_BYTE, buffer.ptr()); | 422 GL_BGRA, GL_UNSIGNED_BYTE, buffer.ptr()); |
412 glPopClientAttrib(); | 423 glPopClientAttrib(); |
413 } | 424 } |
414 | 425 |
415 void CapturerMac::CgBlit(const VideoFrameBuffer& buffer, | 426 void CapturerMac::CgBlit(const VideoFrameBuffer& buffer, |
416 const SkRegion& region) { | 427 const SkRegion& region) { |
| 428 const int buffer_height = buffer.size().height(); |
| 429 const int buffer_width = buffer.size().width(); |
| 430 |
| 431 // Clip to the size of our current screen. |
| 432 SkIRect clip_rect = SkIRect::MakeWH(buffer_width, buffer_height); |
| 433 |
417 if (last_buffer_) | 434 if (last_buffer_) |
418 memcpy(buffer.ptr(), last_buffer_, | 435 memcpy(buffer.ptr(), last_buffer_, |
419 buffer.bytes_per_row() * buffer.size().height()); | 436 buffer.bytes_per_row() * buffer_height); |
420 last_buffer_ = buffer.ptr(); | 437 last_buffer_ = buffer.ptr(); |
421 CGDirectDisplayID main_display = CGMainDisplayID(); | 438 CGDirectDisplayID main_display = CGMainDisplayID(); |
422 uint8* display_base_address = | 439 uint8* display_base_address = |
423 reinterpret_cast<uint8*>(CGDisplayBaseAddress(main_display)); | 440 reinterpret_cast<uint8*>(CGDisplayBaseAddress(main_display)); |
424 int src_bytes_per_row = CGDisplayBytesPerRow(main_display); | 441 int src_bytes_per_row = CGDisplayBytesPerRow(main_display); |
425 int src_bytes_per_pixel = CGDisplayBitsPerPixel(main_display) / 8; | 442 int src_bytes_per_pixel = CGDisplayBitsPerPixel(main_display) / 8; |
426 // TODO(hclam): We can reduce the amount of copying here by subtracting | 443 // TODO(hclam): We can reduce the amount of copying here by subtracting |
427 // |capturer_helper_|s region from |last_invalid_region_|. | 444 // |capturer_helper_|s region from |last_invalid_region_|. |
428 // http://crbug.com/92354 | 445 // http://crbug.com/92354 |
429 for(SkRegion::Iterator i(region); !i.done(); i.next()) { | 446 for(SkRegion::Iterator i(region); !i.done(); i.next()) { |
| 447 SkIRect copy_rect = i.rect(); |
| 448 copy_rect.intersect(clip_rect); |
430 CopyRect(display_base_address, | 449 CopyRect(display_base_address, |
431 src_bytes_per_row, | 450 src_bytes_per_row, |
432 buffer.ptr(), | 451 buffer.ptr(), |
433 buffer.bytes_per_row(), | 452 buffer.bytes_per_row(), |
434 src_bytes_per_pixel, | 453 src_bytes_per_pixel, |
435 i.rect()); | 454 copy_rect); |
436 } | 455 } |
437 } | 456 } |
438 | 457 |
439 const gfx::Size& CapturerMac::size_most_recent() const { | 458 const gfx::Size& CapturerMac::size_most_recent() const { |
440 return helper_.size_most_recent(); | 459 return helper_.size_most_recent(); |
441 } | 460 } |
442 | 461 |
443 void CapturerMac::ScreenRefresh(CGRectCount count, const CGRect *rect_array) { | 462 void CapturerMac::ScreenRefresh(CGRectCount count, const CGRect *rect_array) { |
444 SkIRect skirect_array[count]; | 463 SkIRect skirect_array[count]; |
445 for (CGRectCount i = 0; i < count; ++i) { | 464 for (CGRectCount i = 0; i < count; ++i) { |
(...skipping 11 matching lines...) Expand all Loading... |
457 for (CGRectCount i = 0; i < count; ++i) { | 476 for (CGRectCount i = 0; i < count; ++i) { |
458 CGRect rect = rect_array[i]; | 477 CGRect rect = rect_array[i]; |
459 rect = CGRectOffset(rect, delta.dX, delta.dY); | 478 rect = CGRectOffset(rect, delta.dX, delta.dY); |
460 skirect_new_array[i] = gfx::CGRectToSkIRect(rect); | 479 skirect_new_array[i] = gfx::CGRectToSkIRect(rect); |
461 } | 480 } |
462 SkRegion region; | 481 SkRegion region; |
463 region.setRects(skirect_new_array, count); | 482 region.setRects(skirect_new_array, count); |
464 InvalidateRegion(region); | 483 InvalidateRegion(region); |
465 } | 484 } |
466 | 485 |
| 486 void CapturerMac::DisplaysReconfigured(CGDirectDisplayID display, |
| 487 CGDisplayChangeSummaryFlags flags) { |
| 488 if (display == CGMainDisplayID()) { |
| 489 if (flags & kCGDisplayBeginConfigurationFlag) { |
| 490 // Wait on |display_configuration_capture_event_| to prevent more |
| 491 // captures from occurring on a different thread while the displays |
| 492 // are being reconfigured. |
| 493 CHECK(display_configuration_capture_event_.TimedWait( |
| 494 base::TimeDelta::FromSeconds( |
| 495 kDisplayReconfigurationTimeoutInSeconds))); |
| 496 } else { |
| 497 ScreenConfigurationChanged(); |
| 498 // Now that the configuration has changed, signal the event. |
| 499 display_configuration_capture_event_.Signal(); |
| 500 } |
| 501 } |
| 502 } |
| 503 |
467 void CapturerMac::ScreenRefreshCallback(CGRectCount count, | 504 void CapturerMac::ScreenRefreshCallback(CGRectCount count, |
468 const CGRect *rect_array, | 505 const CGRect *rect_array, |
469 void *user_parameter) { | 506 void *user_parameter) { |
470 CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter); | 507 CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter); |
471 capturer->ScreenRefresh(count, rect_array); | 508 capturer->ScreenRefresh(count, rect_array); |
472 } | 509 } |
473 | 510 |
474 void CapturerMac::ScreenUpdateMoveCallback(CGScreenUpdateMoveDelta delta, | 511 void CapturerMac::ScreenUpdateMoveCallback(CGScreenUpdateMoveDelta delta, |
475 size_t count, | 512 size_t count, |
476 const CGRect *rect_array, | 513 const CGRect *rect_array, |
477 void *user_parameter) { | 514 void *user_parameter) { |
478 CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter); | 515 CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter); |
479 capturer->ScreenUpdateMove(delta, count, rect_array); | 516 capturer->ScreenUpdateMove(delta, count, rect_array); |
480 } | 517 } |
481 | 518 |
482 void CapturerMac::DisplaysReconfiguredCallback( | 519 void CapturerMac::DisplaysReconfiguredCallback( |
483 CGDirectDisplayID display, | 520 CGDirectDisplayID display, |
484 CGDisplayChangeSummaryFlags flags, | 521 CGDisplayChangeSummaryFlags flags, |
485 void *user_parameter) { | 522 void *user_parameter) { |
486 if (display == CGMainDisplayID()) { | 523 CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter); |
487 CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter); | 524 capturer->DisplaysReconfigured(display, flags); |
488 if (flags & kCGDisplayBeginConfigurationFlag) { | |
489 capturer->EnableCapture(false); | |
490 } else { | |
491 capturer->EnableCapture(true); | |
492 capturer->ScreenConfigurationChanged(); | |
493 } | |
494 } | |
495 } | 525 } |
496 | 526 |
497 } // namespace | 527 } // namespace |
498 | 528 |
499 // static | 529 // static |
500 Capturer* Capturer::Create() { | 530 Capturer* Capturer::Create() { |
501 return new CapturerMac(); | 531 return new CapturerMac(); |
502 } | 532 } |
503 | 533 |
504 } // namespace remoting | 534 } // namespace remoting |
OLD | NEW |