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
3 // found in the LICENSE file.
5 #ifndef CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_
6 #define CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/common/content_export.h"
16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_message.h"
18 #include "ui/events/latency_info.h"
19 #include "ui/gfx/native_widget_types.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/gfx/size.h"
22 #include "ui/gl/gl_surface.h"
24 struct AcceleratedSurfaceMsg_BufferPresented_Params;
25 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
26 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
41 class GpuChannelManager;
42 class GpuCommandBufferStub;
44 // The GPU process is agnostic as to how it displays results. On some platforms
45 // it renders directly to window. On others it renders offscreen and transports
46 // the results to the browser process to display. This file provides a simple
47 // framework for making the offscreen path seem more like the onscreen path.
49 // The ImageTransportSurface class defines an simple interface for events that
50 // should be responded to. The factory returns an offscreen surface that looks
51 // a lot like an onscreen surface to the GPU process.
53 // The ImageTransportSurfaceHelper provides some glue to the outside world:
54 // making sure outside events reach the ImageTransportSurface and
55 // allowing the ImageTransportSurface to send events to the outside world.
57 class ImageTransportSurface {
59 ImageTransportSurface();
61 virtual void OnBufferPresented(
62 const AcceleratedSurfaceMsg_BufferPresented_Params& params) = 0;
63 virtual void OnResizeViewACK() = 0;
64 virtual void OnResize(gfx::Size size, float scale_factor) = 0;
65 virtual void SetLatencyInfo(
66 const std::vector<ui::LatencyInfo>& latency_info) = 0;
67 virtual void WakeUpGpu() = 0;
69 // Creates a surface with the given attributes.
70 static scoped_refptr<gfx::GLSurface> CreateSurface(
71 GpuChannelManager* manager,
72 GpuCommandBufferStub* stub,
73 const gfx::GLSurfaceHandle& handle);
75 #if defined(OS_MACOSX)
76 CONTENT_EXPORT static void SetAllowOSMesaForTesting(bool allow);
79 virtual gfx::Size GetSize() = 0;
82 virtual ~ImageTransportSurface();
85 // Creates the appropriate native surface depending on the GL implementation.
86 // This will be implemented separately by each platform.
88 // This will not be called for texture transport surfaces which are
89 // cross-platform. The platform implementation should only create the
90 // surface and should not initialize it. On failure, a null scoped_refptr
91 // should be returned.
92 static scoped_refptr<gfx::GLSurface> CreateNativeSurface(
93 GpuChannelManager* manager,
94 GpuCommandBufferStub* stub,
95 const gfx::GLSurfaceHandle& handle);
97 DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface);
100 class ImageTransportHelper
101 : public IPC::Listener,
102 public base::SupportsWeakPtr<ImageTransportHelper> {
104 // Takes weak pointers to objects that outlive the helper.
105 ImageTransportHelper(ImageTransportSurface* surface,
106 GpuChannelManager* manager,
107 GpuCommandBufferStub* stub,
108 gfx::PluginWindowHandle handle);
109 virtual ~ImageTransportHelper();
114 // IPC::Listener implementation:
115 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
117 // Helper send functions. Caller fills in the surface specific params
118 // like size and surface id. The helper fills in the rest.
119 void SendAcceleratedSurfaceBuffersSwapped(
120 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params);
121 void SendAcceleratedSurfacePostSubBuffer(
122 GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params);
123 void SendAcceleratedSurfaceRelease();
124 void SendResizeView(const gfx::Size& size);
125 void SendUpdateVSyncParameters(
126 base::TimeTicks timebase, base::TimeDelta interval);
128 void SendLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
130 // Whether or not we should execute more commands.
131 void SetScheduled(bool is_scheduled);
133 void DeferToFence(base::Closure task);
135 void SetPreemptByFlag(
136 scoped_refptr<gpu::PreemptionFlag> preemption_flag);
138 // Make the surface's context current.
141 // Set the default swap interval on the surface.
142 static void SetSwapInterval(gfx::GLContext* context);
146 GpuChannelManager* manager() const { return manager_; }
147 GpuCommandBufferStub* stub() const { return stub_.get(); }
150 gpu::GpuScheduler* Scheduler();
151 gpu::gles2::GLES2Decoder* Decoder();
153 // IPC::Message handlers.
154 void OnBufferPresented(
155 const AcceleratedSurfaceMsg_BufferPresented_Params& params);
156 void OnResizeViewACK();
159 // Backbuffer resize callback.
160 void Resize(gfx::Size size, float scale_factor);
162 void SetLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
164 // Weak pointers that point to objects that outlive this helper.
165 ImageTransportSurface* surface_;
166 GpuChannelManager* manager_;
168 base::WeakPtr<GpuCommandBufferStub> stub_;
170 gfx::PluginWindowHandle handle_;
172 DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper);
175 // An implementation of ImageTransportSurface that implements GLSurface through
176 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
177 class PassThroughImageTransportSurface
178 : public gfx::GLSurfaceAdapter,
179 public ImageTransportSurface {
181 PassThroughImageTransportSurface(GpuChannelManager* manager,
182 GpuCommandBufferStub* stub,
183 gfx::GLSurface* surface,
186 // GLSurface implementation.
187 virtual bool Initialize() OVERRIDE;
188 virtual void Destroy() OVERRIDE;
189 virtual bool DeferDraws() OVERRIDE;
190 virtual bool SwapBuffers() OVERRIDE;
191 virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
192 virtual bool OnMakeCurrent(gfx::GLContext* context) OVERRIDE;
194 // ImageTransportSurface implementation.
195 virtual void OnBufferPresented(
196 const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE;
197 virtual void OnResizeViewACK() OVERRIDE;
198 virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE;
199 virtual gfx::Size GetSize() OVERRIDE;
200 virtual void SetLatencyInfo(
201 const std::vector<ui::LatencyInfo>& latency_info) OVERRIDE;
202 virtual void WakeUpGpu() OVERRIDE;
205 virtual ~PassThroughImageTransportSurface();
207 // If updated vsync parameters can be determined, send this information to
209 virtual void SendVSyncUpdateIfAvailable();
211 ImageTransportHelper* GetHelper() { return helper_.get(); }
214 scoped_ptr<ImageTransportHelper> helper_;
217 bool did_set_swap_interval_;
218 bool did_unschedule_;
219 bool is_swap_buffers_pending_;
220 std::vector<ui::LatencyInfo> latency_info_;
222 DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface);
225 } // namespace content
227 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_