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 "content/browser/renderer_host/compositing_iosurface_mac.h" | 5 #include "content/browser/renderer_host/compositing_iosurface_mac.h" |
6 | 6 |
7 #include <OpenGL/CGLRenderers.h> | 7 #include <OpenGL/CGLRenderers.h> |
8 #include <OpenGL/OpenGL.h> | 8 #include <OpenGL/OpenGL.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 base::Bind(&CompositingIOSurfaceMac::CheckIfAllCopiesAreFinished, | 250 base::Bind(&CompositingIOSurfaceMac::CheckIfAllCopiesAreFinished, |
251 base::Unretained(this), | 251 base::Unretained(this), |
252 false), | 252 false), |
253 true), | 253 true), |
254 gl_error_(GL_NO_ERROR) { | 254 gl_error_(GL_NO_ERROR) { |
255 CHECK(offscreen_context_); | 255 CHECK(offscreen_context_); |
256 } | 256 } |
257 | 257 |
258 CompositingIOSurfaceMac::~CompositingIOSurfaceMac() { | 258 CompositingIOSurfaceMac::~CompositingIOSurfaceMac() { |
259 FailAllCopies(); | 259 FailAllCopies(); |
260 CGLSetCurrentContext(offscreen_context_->cgl_context()); | 260 { |
261 DestroyAllCopyContextsWithinContext(); | 261 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( |
262 UnrefIOSurfaceWithContextCurrent(); | 262 offscreen_context_->cgl_context()); |
263 CGLSetCurrentContext(0); | 263 DestroyAllCopyContextsWithinContext(); |
| 264 UnrefIOSurfaceWithContextCurrent(); |
| 265 } |
264 offscreen_context_ = NULL; | 266 offscreen_context_ = NULL; |
265 } | 267 } |
266 | 268 |
267 bool CompositingIOSurfaceMac::SetIOSurfaceWithContextCurrent( | 269 bool CompositingIOSurfaceMac::SetIOSurfaceWithContextCurrent( |
268 scoped_refptr<CompositingIOSurfaceContext> current_context, | 270 scoped_refptr<CompositingIOSurfaceContext> current_context, |
269 uint64 io_surface_handle, | 271 uint64 io_surface_handle, |
270 const gfx::Size& size, | 272 const gfx::Size& size, |
271 float scale_factor) { | 273 float scale_factor) { |
272 pixel_io_surface_size_ = size; | 274 pixel_io_surface_size_ = size; |
273 scale_factor_ = scale_factor; | 275 scale_factor_ = scale_factor; |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 kOpaque_SkAlphaType); | 435 kOpaque_SkAlphaType); |
434 | 436 |
435 if (!output->allocPixels()) { | 437 if (!output->allocPixels()) { |
436 DLOG(ERROR) << "Failed to allocate SkBitmap pixels!"; | 438 DLOG(ERROR) << "Failed to allocate SkBitmap pixels!"; |
437 callback.Run(false, *output); | 439 callback.Run(false, *output); |
438 return; | 440 return; |
439 } | 441 } |
440 DCHECK_EQ(output->rowBytesAsPixels(), dst_pixel_size.width()) | 442 DCHECK_EQ(output->rowBytesAsPixels(), dst_pixel_size.width()) |
441 << "Stride is required to be equal to width for GPU readback."; | 443 << "Stride is required to be equal to width for GPU readback."; |
442 | 444 |
443 CGLSetCurrentContext(offscreen_context_->cgl_context()); | 445 base::Closure copy_done_callback; |
444 const base::Closure copy_done_callback = CopyToSelectedOutputWithinContext( | 446 { |
445 src_pixel_subrect, gfx::Rect(dst_pixel_size), false, | 447 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( |
446 output.get(), NULL, | 448 offscreen_context_->cgl_context()); |
447 base::Bind(&ReverseArgumentOrder, callback, base::Passed(&output))); | 449 copy_done_callback = CopyToSelectedOutputWithinContext( |
448 CGLSetCurrentContext(0); | 450 src_pixel_subrect, gfx::Rect(dst_pixel_size), false, |
| 451 output.get(), NULL, |
| 452 base::Bind(&ReverseArgumentOrder, callback, base::Passed(&output))); |
| 453 } |
449 if (!copy_done_callback.is_null()) | 454 if (!copy_done_callback.is_null()) |
450 copy_done_callback.Run(); | 455 copy_done_callback.Run(); |
451 } | 456 } |
452 | 457 |
453 void CompositingIOSurfaceMac::CopyToVideoFrame( | 458 void CompositingIOSurfaceMac::CopyToVideoFrame( |
454 const gfx::Rect& src_pixel_subrect, | 459 const gfx::Rect& src_pixel_subrect, |
455 const scoped_refptr<media::VideoFrame>& target, | 460 const scoped_refptr<media::VideoFrame>& target, |
456 const base::Callback<void(bool)>& callback) { | 461 const base::Callback<void(bool)>& callback) { |
457 CGLSetCurrentContext(offscreen_context_->cgl_context()); | 462 base::Closure copy_done_callback; |
458 const base::Closure copy_done_callback = CopyToVideoFrameWithinContext( | 463 { |
459 src_pixel_subrect, false, target, callback); | 464 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( |
460 CGLSetCurrentContext(0); | 465 offscreen_context_->cgl_context()); |
| 466 copy_done_callback = CopyToVideoFrameWithinContext( |
| 467 src_pixel_subrect, false, target, callback); |
| 468 } |
461 if (!copy_done_callback.is_null()) | 469 if (!copy_done_callback.is_null()) |
462 copy_done_callback.Run(); | 470 copy_done_callback.Run(); |
463 } | 471 } |
464 | 472 |
465 base::Closure CompositingIOSurfaceMac::CopyToVideoFrameWithinContext( | 473 base::Closure CompositingIOSurfaceMac::CopyToVideoFrameWithinContext( |
466 const gfx::Rect& src_pixel_subrect, | 474 const gfx::Rect& src_pixel_subrect, |
467 bool called_within_draw, | 475 bool called_within_draw, |
468 const scoped_refptr<media::VideoFrame>& target, | 476 const scoped_refptr<media::VideoFrame>& target, |
469 const base::Callback<void(bool)>& callback) { | 477 const base::Callback<void(bool)>& callback) { |
470 gfx::Rect region_in_frame = media::ComputeLetterboxRegion( | 478 gfx::Rect region_in_frame = media::ComputeLetterboxRegion( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 GetAndSaveGLError(); | 543 GetAndSaveGLError(); |
536 if (gl_error_ != GL_NO_ERROR) { | 544 if (gl_error_ != GL_NO_ERROR) { |
537 LOG(ERROR) << "GL error in MapIOSurfaceToTexture: " << gl_error_; | 545 LOG(ERROR) << "GL error in MapIOSurfaceToTexture: " << gl_error_; |
538 UnrefIOSurfaceWithContextCurrent(); | 546 UnrefIOSurfaceWithContextCurrent(); |
539 return false; | 547 return false; |
540 } | 548 } |
541 return true; | 549 return true; |
542 } | 550 } |
543 | 551 |
544 void CompositingIOSurfaceMac::UnrefIOSurface() { | 552 void CompositingIOSurfaceMac::UnrefIOSurface() { |
545 CGLSetCurrentContext(offscreen_context_->cgl_context()); | 553 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( |
| 554 offscreen_context_->cgl_context()); |
546 UnrefIOSurfaceWithContextCurrent(); | 555 UnrefIOSurfaceWithContextCurrent(); |
547 CGLSetCurrentContext(0); | |
548 } | 556 } |
549 | 557 |
550 void CompositingIOSurfaceMac::DrawQuad(const SurfaceQuad& quad) { | 558 void CompositingIOSurfaceMac::DrawQuad(const SurfaceQuad& quad) { |
551 TRACE_EVENT0("gpu", "CompositingIOSurfaceMac::DrawQuad"); | 559 TRACE_EVENT0("gpu", "CompositingIOSurfaceMac::DrawQuad"); |
552 | 560 |
553 glEnableClientState(GL_VERTEX_ARRAY); CHECK_AND_SAVE_GL_ERROR(); | 561 glEnableClientState(GL_VERTEX_ARRAY); CHECK_AND_SAVE_GL_ERROR(); |
554 glEnableClientState(GL_TEXTURE_COORD_ARRAY); CHECK_AND_SAVE_GL_ERROR(); | 562 glEnableClientState(GL_TEXTURE_COORD_ARRAY); CHECK_AND_SAVE_GL_ERROR(); |
555 | 563 |
556 glVertexPointer(2, GL_FLOAT, sizeof(SurfaceVertex), &quad.verts_[0].x_); | 564 glVertexPointer(2, GL_FLOAT, sizeof(SurfaceVertex), &quad.verts_[0].x_); |
557 glTexCoordPointer(2, GL_FLOAT, sizeof(SurfaceVertex), &quad.verts_[0].tx_); | 565 glTexCoordPointer(2, GL_FLOAT, sizeof(SurfaceVertex), &quad.verts_[0].tx_); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 base::Bind(&MapBufferToSkBitmap, bitmap_output) : | 748 base::Bind(&MapBufferToSkBitmap, bitmap_output) : |
741 base::Bind(&MapBufferToVideoFrame, video_frame_output, dst_pixel_rect); | 749 base::Bind(&MapBufferToVideoFrame, video_frame_output, dst_pixel_rect); |
742 } | 750 } |
743 | 751 |
744 void CompositingIOSurfaceMac::CheckIfAllCopiesAreFinished( | 752 void CompositingIOSurfaceMac::CheckIfAllCopiesAreFinished( |
745 bool block_until_finished) { | 753 bool block_until_finished) { |
746 if (copy_requests_.empty()) | 754 if (copy_requests_.empty()) |
747 return; | 755 return; |
748 | 756 |
749 std::vector<base::Closure> done_callbacks; | 757 std::vector<base::Closure> done_callbacks; |
750 CGLContextObj previous_context = CGLGetCurrentContext(); | 758 { |
751 CGLSetCurrentContext(offscreen_context_->cgl_context()); | 759 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( |
752 CheckIfAllCopiesAreFinishedWithinContext( | 760 offscreen_context_->cgl_context()); |
753 block_until_finished, &done_callbacks); | 761 CheckIfAllCopiesAreFinishedWithinContext( |
754 CGLSetCurrentContext(previous_context); | 762 block_until_finished, &done_callbacks); |
| 763 } |
755 for (size_t i = 0; i < done_callbacks.size(); ++i) | 764 for (size_t i = 0; i < done_callbacks.size(); ++i) |
756 done_callbacks[i].Run(); | 765 done_callbacks[i].Run(); |
757 } | 766 } |
758 | 767 |
759 void CompositingIOSurfaceMac::CheckIfAllCopiesAreFinishedWithinContext( | 768 void CompositingIOSurfaceMac::CheckIfAllCopiesAreFinishedWithinContext( |
760 bool block_until_finished, | 769 bool block_until_finished, |
761 std::vector<base::Closure>* done_callbacks) { | 770 std::vector<base::Closure>* done_callbacks) { |
762 while (!copy_requests_.empty()) { | 771 while (!copy_requests_.empty()) { |
763 CopyContext* const copy_context = copy_requests_.front(); | 772 CopyContext* const copy_context = copy_requests_.front(); |
764 | 773 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 } | 927 } |
919 | 928 |
920 GLenum CompositingIOSurfaceMac::GetAndSaveGLError() { | 929 GLenum CompositingIOSurfaceMac::GetAndSaveGLError() { |
921 GLenum gl_error = glGetError(); | 930 GLenum gl_error = glGetError(); |
922 if (gl_error_ == GL_NO_ERROR) | 931 if (gl_error_ == GL_NO_ERROR) |
923 gl_error_ = gl_error; | 932 gl_error_ = gl_error; |
924 return gl_error; | 933 return gl_error; |
925 } | 934 } |
926 | 935 |
927 } // namespace content | 936 } // namespace content |
OLD | NEW |