1 // Copyright 2014 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.
5 #ifndef CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_FBO_MAC_H_
6 #define CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_FBO_MAC_H_
8 #include "base/mac/scoped_cftyperef.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "content/common/gpu/gpu_command_buffer_stub.h"
11 #include "content/common/gpu/image_transport_surface.h"
12 #include "ui/gl/gl_bindings.h"
16 // We are backed by an offscreen surface for the purposes of creating
17 // a context, but use FBOs to render to texture. The texture may be backed by
18 // an IOSurface, or it may be presented to the screen via a CALayer, depending
19 // on the StorageProvider class specified.
20 class ImageTransportSurfaceFBO
21 : public gfx::GLSurface,
22 public ImageTransportSurface,
23 public GpuCommandBufferStub::DestructionObserver {
25 // The interface through which storage for the color buffer of the FBO is
27 class StorageProvider {
29 virtual ~StorageProvider() {}
30 // IOSurfaces cause too much address space fragmentation if they are
31 // allocated on every resize. This gets a rounded size for allocation.
32 virtual gfx::Size GetRoundedSize(gfx::Size size) = 0;
34 // Allocate the storage for the color buffer. The specified context is
35 // current, and there is a texture bound to GL_TEXTURE_RECTANGLE_ARB.
36 virtual bool AllocateColorBufferStorage(
37 CGLContextObj context, GLuint texture,
38 gfx::Size size, float scale_factor) = 0;
40 // Free the storage allocated in the AllocateColorBufferStorage call. The
41 // GL texture that was bound has already been deleted by the caller.
42 virtual void FreeColorBufferStorage() = 0;
44 // Swap buffers and return the handle for the surface to send to the browser
45 // process to display.
46 virtual void SwapBuffers(const gfx::Size& size, float scale_factor) = 0;
48 // Indicate that the backbuffer will be written to.
49 virtual void WillWriteToBackbuffer() = 0;
51 // Indicate that the backbuffer has been discarded and should not be seen
53 virtual void DiscardBackbuffer() = 0;
55 // Called once for every SwapBuffers call when the IPC for the present has
56 // been processed by the browser. |disable_throttling| is set if the
57 // browser suspects that GPU back-pressure should be disabled.
58 virtual void SwapBuffersAckedByBrowser(bool disable_throttling) = 0;
61 ImageTransportSurfaceFBO(GpuChannelManager* manager,
62 GpuCommandBufferStub* stub,
63 gfx::PluginWindowHandle handle);
65 // GLSurface implementation
66 bool Initialize() override;
67 void Destroy() override;
68 bool DeferDraws() override;
69 bool IsOffscreen() override;
70 bool SwapBuffers() override;
71 bool PostSubBuffer(int x, int y, int width, int height) override;
72 bool SupportsPostSubBuffer() override;
73 gfx::Size GetSize() override;
74 void* GetHandle() override;
75 void* GetDisplay() override;
76 bool OnMakeCurrent(gfx::GLContext* context) override;
77 unsigned int GetBackingFrameBufferObject() override;
78 bool SetBackbufferAllocation(bool allocated) override;
79 void SetFrontbufferAllocation(bool allocated) override;
81 // Called when the context may continue to make forward progress after a swap.
82 void SendSwapBuffers(uint64 surface_handle,
83 const gfx::Size pixel_size,
85 void SetRendererID(int renderer_id);
88 // ImageTransportSurface implementation
89 void OnBufferPresented(
90 const AcceleratedSurfaceMsg_BufferPresented_Params& params) override;
91 void OnResize(gfx::Size pixel_size, float scale_factor) override;
92 void SetLatencyInfo(const std::vector<ui::LatencyInfo>&) override;
93 void WakeUpGpu() override;
95 // GpuCommandBufferStub::DestructionObserver implementation.
96 void OnWillDestroyStub() override;
99 ~ImageTransportSurfaceFBO() override;
101 void AdjustBufferAllocation();
102 void DestroyFramebuffer();
103 void AllocateOrResizeFramebuffer(
104 const gfx::Size& pixel_size, float scale_factor);
106 scoped_ptr<StorageProvider> storage_provider_;
108 // Tracks the current buffer allocation state.
109 bool backbuffer_suggested_allocation_;
110 bool frontbuffer_suggested_allocation_;
114 GLuint depth_stencil_renderbuffer_id_;
115 bool has_complete_framebuffer_;
117 // Weak pointer to the context that this was last made current to.
118 gfx::GLContext* context_;
120 gfx::Size pixel_size_;
121 gfx::Size rounded_pixel_size_;
124 // Whether or not we've successfully made the surface current once.
127 // Whether a SwapBuffers IPC needs to be sent to the browser.
128 bool is_swap_buffers_send_pending_;
129 std::vector<ui::LatencyInfo> latency_info_;
131 scoped_ptr<ImageTransportHelper> helper_;
133 DISALLOW_COPY_AND_ASSIGN(ImageTransportSurfaceFBO);
136 } // namespace content
138 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_MAC_H_