| 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/common/gpu/image_transport_surface.h" | 5 #include "content/common/gpu/image_transport_surface.h" |
| 6 | 6 |
| 7 #include "base/mac/scoped_cftyperef.h" | 7 #include "base/mac/scoped_cftyperef.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "content/common/gpu/gpu_command_buffer_stub.h" |
| 9 #include "content/common/gpu/gpu_messages.h" | 10 #include "content/common/gpu/gpu_messages.h" |
| 10 #include "ui/gfx/native_widget_types.h" | 11 #include "ui/gfx/native_widget_types.h" |
| 11 #include "ui/gl/gl_bindings.h" | 12 #include "ui/gl/gl_bindings.h" |
| 12 #include "ui/gl/gl_context.h" | 13 #include "ui/gl/gl_context.h" |
| 13 #include "ui/gl/gl_implementation.h" | 14 #include "ui/gl/gl_implementation.h" |
| 14 #include "ui/gl/gl_surface_cgl.h" | 15 #include "ui/gl/gl_surface_cgl.h" |
| 15 #include "ui/gl/gl_surface_osmesa.h" | 16 #include "ui/gl/gl_surface_osmesa.h" |
| 16 #include "ui/surface/io_surface_support_mac.h" | 17 #include "ui/surface/io_surface_support_mac.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // IOSurface dimensions will be rounded up to a multiple of this value in order | 22 // IOSurface dimensions will be rounded up to a multiple of this value in order |
| 22 // to reduce memory thrashing during resize. This must be a power of 2. | 23 // to reduce memory thrashing during resize. This must be a power of 2. |
| 23 const uint32 kIOSurfaceDimensionRoundup = 64; | 24 const uint32 kIOSurfaceDimensionRoundup = 64; |
| 24 | 25 |
| 25 int RoundUpSurfaceDimension(int number) { | 26 int RoundUpSurfaceDimension(int number) { |
| 26 DCHECK(number >= 0); | 27 DCHECK(number >= 0); |
| 27 // Cast into unsigned space for portable bitwise ops. | 28 // Cast into unsigned space for portable bitwise ops. |
| 28 uint32 unsigned_number = static_cast<uint32>(number); | 29 uint32 unsigned_number = static_cast<uint32>(number); |
| 29 uint32 roundup_sub_1 = kIOSurfaceDimensionRoundup - 1; | 30 uint32 roundup_sub_1 = kIOSurfaceDimensionRoundup - 1; |
| 30 unsigned_number = (unsigned_number + roundup_sub_1) & ~roundup_sub_1; | 31 unsigned_number = (unsigned_number + roundup_sub_1) & ~roundup_sub_1; |
| 31 return static_cast<int>(unsigned_number); | 32 return static_cast<int>(unsigned_number); |
| 32 } | 33 } |
| 33 | 34 |
| 34 // We are backed by an offscreen surface for the purposes of creating | 35 // We are backed by an offscreen surface for the purposes of creating |
| 35 // a context, but use FBOs to render to texture backed IOSurface | 36 // a context, but use FBOs to render to texture backed IOSurface |
| 36 class IOSurfaceImageTransportSurface : public gfx::NoOpGLSurfaceCGL, | 37 class IOSurfaceImageTransportSurface |
| 37 public ImageTransportSurface { | 38 : public gfx::NoOpGLSurfaceCGL, |
| 39 public ImageTransportSurface, |
| 40 public GpuCommandBufferStub::DestructionObserver { |
| 38 public: | 41 public: |
| 39 IOSurfaceImageTransportSurface(GpuChannelManager* manager, | 42 IOSurfaceImageTransportSurface(GpuChannelManager* manager, |
| 40 GpuCommandBufferStub* stub, | 43 GpuCommandBufferStub* stub, |
| 41 gfx::PluginWindowHandle handle); | 44 gfx::PluginWindowHandle handle); |
| 42 | 45 |
| 43 // GLSurface implementation | 46 // GLSurface implementation |
| 44 virtual bool Initialize() OVERRIDE; | 47 virtual bool Initialize() OVERRIDE; |
| 45 virtual void Destroy() OVERRIDE; | 48 virtual void Destroy() OVERRIDE; |
| 46 virtual bool DeferDraws() OVERRIDE; | 49 virtual bool DeferDraws() OVERRIDE; |
| 47 virtual bool IsOffscreen() OVERRIDE; | 50 virtual bool IsOffscreen() OVERRIDE; |
| 48 virtual bool SwapBuffers() OVERRIDE; | 51 virtual bool SwapBuffers() OVERRIDE; |
| 49 virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE; | 52 virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE; |
| 50 virtual std::string GetExtensions() OVERRIDE; | 53 virtual std::string GetExtensions() OVERRIDE; |
| 51 virtual gfx::Size GetSize() OVERRIDE; | 54 virtual gfx::Size GetSize() OVERRIDE; |
| 52 virtual bool OnMakeCurrent(gfx::GLContext* context) OVERRIDE; | 55 virtual bool OnMakeCurrent(gfx::GLContext* context) OVERRIDE; |
| 53 virtual unsigned int GetBackingFrameBufferObject() OVERRIDE; | 56 virtual unsigned int GetBackingFrameBufferObject() OVERRIDE; |
| 54 virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE; | 57 virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE; |
| 55 virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE; | 58 virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE; |
| 56 | 59 |
| 57 protected: | 60 protected: |
| 58 // ImageTransportSurface implementation | 61 // ImageTransportSurface implementation |
| 59 virtual void OnBufferPresented( | 62 virtual void OnBufferPresented( |
| 60 const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE; | 63 const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE; |
| 61 virtual void OnResizeViewACK() OVERRIDE; | 64 virtual void OnResizeViewACK() OVERRIDE; |
| 62 virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE; | 65 virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE; |
| 63 virtual void SetLatencyInfo(const ui::LatencyInfo&) OVERRIDE; | 66 virtual void SetLatencyInfo(const ui::LatencyInfo&) OVERRIDE; |
| 64 | 67 |
| 68 // GpuCommandBufferStub::DestructionObserver implementation. |
| 69 virtual void OnWillDestroyStub() OVERRIDE; |
| 70 |
| 65 private: | 71 private: |
| 66 virtual ~IOSurfaceImageTransportSurface() OVERRIDE; | 72 virtual ~IOSurfaceImageTransportSurface() OVERRIDE; |
| 67 | 73 |
| 68 void AdjustBufferAllocation(); | 74 void AdjustBufferAllocation(); |
| 69 void UnrefIOSurface(); | 75 void UnrefIOSurface(); |
| 70 void CreateIOSurface(); | 76 void CreateIOSurface(); |
| 71 | 77 |
| 72 // Tracks the current buffer allocation state. | 78 // Tracks the current buffer allocation state. |
| 73 bool backbuffer_suggested_allocation_; | 79 bool backbuffer_suggested_allocation_; |
| 74 bool frontbuffer_suggested_allocation_; | 80 bool frontbuffer_suggested_allocation_; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 io_surface_handle_(0), | 137 io_surface_handle_(0), |
| 132 context_(NULL), | 138 context_(NULL), |
| 133 scale_factor_(1.f), | 139 scale_factor_(1.f), |
| 134 made_current_(false), | 140 made_current_(false), |
| 135 is_swap_buffers_pending_(false), | 141 is_swap_buffers_pending_(false), |
| 136 did_unschedule_(false) { | 142 did_unschedule_(false) { |
| 137 helper_.reset(new ImageTransportHelper(this, manager, stub, handle)); | 143 helper_.reset(new ImageTransportHelper(this, manager, stub, handle)); |
| 138 } | 144 } |
| 139 | 145 |
| 140 IOSurfaceImageTransportSurface::~IOSurfaceImageTransportSurface() { | 146 IOSurfaceImageTransportSurface::~IOSurfaceImageTransportSurface() { |
| 141 Destroy(); | |
| 142 } | 147 } |
| 143 | 148 |
| 144 bool IOSurfaceImageTransportSurface::Initialize() { | 149 bool IOSurfaceImageTransportSurface::Initialize() { |
| 145 // Only support IOSurfaces if the GL implementation is the native desktop GL. | 150 // Only support IOSurfaces if the GL implementation is the native desktop GL. |
| 146 // IO surfaces will not work with, for example, OSMesa software renderer | 151 // IO surfaces will not work with, for example, OSMesa software renderer |
| 147 // GL contexts. | 152 // GL contexts. |
| 148 if (gfx::GetGLImplementation() != gfx::kGLImplementationDesktopGL && | 153 if (gfx::GetGLImplementation() != gfx::kGLImplementationDesktopGL && |
| 149 gfx::GetGLImplementation() != gfx::kGLImplementationAppleGL) | 154 gfx::GetGLImplementation() != gfx::kGLImplementationAppleGL) |
| 150 return false; | 155 return false; |
| 151 | 156 |
| 152 if (!helper_->Initialize()) | 157 if (!helper_->Initialize()) |
| 153 return false; | 158 return false; |
| 154 return NoOpGLSurfaceCGL::Initialize(); | 159 |
| 160 if (!NoOpGLSurfaceCGL::Initialize()) { |
| 161 helper_->Destroy(); |
| 162 return false; |
| 163 } |
| 164 |
| 165 helper_->stub()->AddDestructionObserver(this); |
| 166 return true; |
| 155 } | 167 } |
| 156 | 168 |
| 157 void IOSurfaceImageTransportSurface::Destroy() { | 169 void IOSurfaceImageTransportSurface::Destroy() { |
| 158 UnrefIOSurface(); | 170 UnrefIOSurface(); |
| 159 | 171 |
| 160 helper_->Destroy(); | 172 helper_->Destroy(); |
| 161 NoOpGLSurfaceCGL::Destroy(); | 173 NoOpGLSurfaceCGL::Destroy(); |
| 162 } | 174 } |
| 163 | 175 |
| 164 bool IOSurfaceImageTransportSurface::DeferDraws() { | 176 bool IOSurfaceImageTransportSurface::DeferDraws() { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 scale_factor_ = scale_factor; | 318 scale_factor_ = scale_factor; |
| 307 | 319 |
| 308 CreateIOSurface(); | 320 CreateIOSurface(); |
| 309 } | 321 } |
| 310 | 322 |
| 311 void IOSurfaceImageTransportSurface::SetLatencyInfo( | 323 void IOSurfaceImageTransportSurface::SetLatencyInfo( |
| 312 const ui::LatencyInfo& latency_info) { | 324 const ui::LatencyInfo& latency_info) { |
| 313 latency_info_ = latency_info; | 325 latency_info_ = latency_info; |
| 314 } | 326 } |
| 315 | 327 |
| 328 void IOSurfaceImageTransportSurface::OnWillDestroyStub() { |
| 329 helper_->stub()->RemoveDestructionObserver(this); |
| 330 Destroy(); |
| 331 } |
| 332 |
| 316 void IOSurfaceImageTransportSurface::UnrefIOSurface() { | 333 void IOSurfaceImageTransportSurface::UnrefIOSurface() { |
| 317 // If we have resources to destroy, then make sure that we have a current | 334 // If we have resources to destroy, then make sure that we have a current |
| 318 // context which we can use to delete the resources. | 335 // context which we can use to delete the resources. |
| 319 if (context_ || fbo_id_ || texture_id_) { | 336 if (context_ || fbo_id_ || texture_id_) { |
| 320 DCHECK(gfx::GLContext::GetCurrent() == context_); | 337 DCHECK(gfx::GLContext::GetCurrent() == context_); |
| 321 DCHECK(context_->IsCurrent(this)); | 338 DCHECK(context_->IsCurrent(this)); |
| 322 DCHECK(CGLGetCurrentContext()); | 339 DCHECK(CGLGetCurrentContext()); |
| 323 } | 340 } |
| 324 | 341 |
| 325 if (fbo_id_) { | 342 if (fbo_id_) { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 manager, stub, surface.get(), false)); | 501 manager, stub, surface.get(), false)); |
| 485 } | 502 } |
| 486 } | 503 } |
| 487 | 504 |
| 488 // static | 505 // static |
| 489 void ImageTransportSurface::SetAllowOSMesaForTesting(bool allow) { | 506 void ImageTransportSurface::SetAllowOSMesaForTesting(bool allow) { |
| 490 g_allow_os_mesa = allow; | 507 g_allow_os_mesa = allow; |
| 491 } | 508 } |
| 492 | 509 |
| 493 } // namespace content | 510 } // namespace content |
| OLD | NEW |