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