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 OnResize(gfx::Size size
, float scale_factor
) = 0;
64 virtual void SetLatencyInfo(
65 const std::vector
<ui::LatencyInfo
>& latency_info
) = 0;
66 virtual void WakeUpGpu() = 0;
68 // Creates a surface with the given attributes.
69 static scoped_refptr
<gfx::GLSurface
> CreateSurface(
70 GpuChannelManager
* manager
,
71 GpuCommandBufferStub
* stub
,
72 const gfx::GLSurfaceHandle
& handle
);
74 #if defined(OS_MACOSX)
75 CONTENT_EXPORT
static void SetAllowOSMesaForTesting(bool allow
);
78 virtual gfx::Size
GetSize() = 0;
81 virtual ~ImageTransportSurface();
84 // Creates the appropriate native surface depending on the GL implementation.
85 // This will be implemented separately by each platform.
87 // This will not be called for texture transport surfaces which are
88 // cross-platform. The platform implementation should only create the
89 // surface and should not initialize it. On failure, a null scoped_refptr
90 // should be returned.
91 static scoped_refptr
<gfx::GLSurface
> CreateNativeSurface(
92 GpuChannelManager
* manager
,
93 GpuCommandBufferStub
* stub
,
94 const gfx::GLSurfaceHandle
& handle
);
96 DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface
);
99 class ImageTransportHelper
100 : public IPC::Listener
,
101 public base::SupportsWeakPtr
<ImageTransportHelper
> {
103 // Takes weak pointers to objects that outlive the helper.
104 ImageTransportHelper(ImageTransportSurface
* surface
,
105 GpuChannelManager
* manager
,
106 GpuCommandBufferStub
* stub
,
107 gfx::PluginWindowHandle handle
);
108 virtual ~ImageTransportHelper();
113 // IPC::Listener implementation:
114 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
116 // Helper send functions. Caller fills in the surface specific params
117 // like size and surface id. The helper fills in the rest.
118 void SendAcceleratedSurfaceBuffersSwapped(
119 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params
);
120 void SendAcceleratedSurfacePostSubBuffer(
121 GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params
);
122 void SendAcceleratedSurfaceRelease();
123 void SendUpdateVSyncParameters(
124 base::TimeTicks timebase
, base::TimeDelta interval
);
126 void SendLatencyInfo(const std::vector
<ui::LatencyInfo
>& latency_info
);
128 // Whether or not we should execute more commands.
129 void SetScheduled(bool is_scheduled
);
131 void DeferToFence(base::Closure task
);
133 void SetPreemptByFlag(
134 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
136 // Make the surface's context current.
139 // Set the default swap interval on the surface.
140 static void SetSwapInterval(gfx::GLContext
* context
);
144 GpuChannelManager
* manager() const { return manager_
; }
145 GpuCommandBufferStub
* stub() const { return stub_
.get(); }
148 gpu::GpuScheduler
* Scheduler();
149 gpu::gles2::GLES2Decoder
* Decoder();
151 // IPC::Message handlers.
152 void OnBufferPresented(
153 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
);
156 // Backbuffer resize callback.
157 void Resize(gfx::Size size
, float scale_factor
);
159 void SetLatencyInfo(const std::vector
<ui::LatencyInfo
>& latency_info
);
161 // Weak pointers that point to objects that outlive this helper.
162 ImageTransportSurface
* surface_
;
163 GpuChannelManager
* manager_
;
165 base::WeakPtr
<GpuCommandBufferStub
> stub_
;
167 gfx::PluginWindowHandle handle_
;
169 DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper
);
172 // An implementation of ImageTransportSurface that implements GLSurface through
173 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
174 class PassThroughImageTransportSurface
175 : public gfx::GLSurfaceAdapter
,
176 public ImageTransportSurface
{
178 PassThroughImageTransportSurface(GpuChannelManager
* manager
,
179 GpuCommandBufferStub
* stub
,
180 gfx::GLSurface
* surface
);
182 // GLSurface implementation.
183 virtual bool Initialize() OVERRIDE
;
184 virtual void Destroy() OVERRIDE
;
185 virtual bool SwapBuffers() OVERRIDE
;
186 virtual bool PostSubBuffer(int x
, int y
, int width
, int height
) OVERRIDE
;
187 virtual bool OnMakeCurrent(gfx::GLContext
* context
) OVERRIDE
;
189 // ImageTransportSurface implementation.
190 virtual void OnBufferPresented(
191 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) OVERRIDE
;
192 virtual void OnResize(gfx::Size size
, float scale_factor
) OVERRIDE
;
193 virtual gfx::Size
GetSize() OVERRIDE
;
194 virtual void SetLatencyInfo(
195 const std::vector
<ui::LatencyInfo
>& latency_info
) OVERRIDE
;
196 virtual void WakeUpGpu() OVERRIDE
;
199 virtual ~PassThroughImageTransportSurface();
201 // If updated vsync parameters can be determined, send this information to
203 virtual void SendVSyncUpdateIfAvailable();
205 ImageTransportHelper
* GetHelper() { return helper_
.get(); }
208 scoped_ptr
<ImageTransportHelper
> helper_
;
209 bool did_set_swap_interval_
;
210 std::vector
<ui::LatencyInfo
> latency_info_
;
212 DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface
);
215 } // namespace content
217 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_