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
;
39 class GpuChannelManager
;
40 class GpuCommandBufferStub
;
42 // The GPU process is agnostic as to how it displays results. On some platforms
43 // it renders directly to window. On others it renders offscreen and transports
44 // the results to the browser process to display. This file provides a simple
45 // framework for making the offscreen path seem more like the onscreen path.
47 // The ImageTransportSurface class defines an simple interface for events that
48 // should be responded to. The factory returns an offscreen surface that looks
49 // a lot like an onscreen surface to the GPU process.
51 // The ImageTransportSurfaceHelper provides some glue to the outside world:
52 // making sure outside events reach the ImageTransportSurface and
53 // allowing the ImageTransportSurface to send events to the outside world.
55 class ImageTransportSurface
{
57 ImageTransportSurface();
59 #if defined(OS_MACOSX)
60 virtual void OnBufferPresented(
61 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 #if defined(OS_ANDROID)
97 static scoped_refptr
<gfx::GLSurface
> CreateTransportSurface(
98 GpuChannelManager
* manager
,
99 GpuCommandBufferStub
* stub
,
100 const gfx::GLSurfaceHandle
& handle
);
103 DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface
);
106 class ImageTransportHelper
107 : public IPC::Listener
,
108 public base::SupportsWeakPtr
<ImageTransportHelper
> {
110 // Takes weak pointers to objects that outlive the helper.
111 ImageTransportHelper(ImageTransportSurface
* surface
,
112 GpuChannelManager
* manager
,
113 GpuCommandBufferStub
* stub
,
114 gfx::PluginWindowHandle handle
);
115 ~ImageTransportHelper() override
;
119 // IPC::Listener implementation:
120 bool OnMessageReceived(const IPC::Message
& message
) override
;
122 // Helper send functions. Caller fills in the surface specific params
123 // like size and surface id. The helper fills in the rest.
124 void SendAcceleratedSurfaceBuffersSwapped(
125 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params
);
126 void SendUpdateVSyncParameters(
127 base::TimeTicks timebase
, base::TimeDelta interval
);
129 void SwapBuffersCompleted(const std::vector
<ui::LatencyInfo
>& latency_info
);
131 void SetPreemptByFlag(
132 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
134 // Make the surface's context current.
137 // Set the default swap interval on the surface.
138 static void SetSwapInterval(gfx::GLContext
* context
);
140 GpuChannelManager
* manager() const { return manager_
; }
141 GpuCommandBufferStub
* stub() const { return stub_
.get(); }
144 gpu::gles2::GLES2Decoder
* Decoder();
146 // IPC::Message handlers.
147 #if defined(OS_MACOSX)
148 void OnBufferPresented(
149 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
);
153 // Backbuffer resize callback.
154 void Resize(gfx::Size size
, float scale_factor
);
156 void SetLatencyInfo(const std::vector
<ui::LatencyInfo
>& latency_info
);
158 // Weak pointers that point to objects that outlive this helper.
159 ImageTransportSurface
* surface_
;
160 GpuChannelManager
* manager_
;
162 base::WeakPtr
<GpuCommandBufferStub
> stub_
;
164 gfx::PluginWindowHandle handle_
;
166 DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper
);
169 // An implementation of ImageTransportSurface that implements GLSurface through
170 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
171 class PassThroughImageTransportSurface
172 : public gfx::GLSurfaceAdapter
,
173 public ImageTransportSurface
{
175 PassThroughImageTransportSurface(GpuChannelManager
* manager
,
176 GpuCommandBufferStub
* stub
,
177 gfx::GLSurface
* surface
);
179 // GLSurface implementation.
180 bool Initialize() override
;
181 void Destroy() override
;
182 bool SwapBuffers() override
;
183 bool PostSubBuffer(int x
, int y
, int width
, int height
) override
;
184 bool OnMakeCurrent(gfx::GLContext
* context
) override
;
186 // ImageTransportSurface implementation.
187 #if defined(OS_MACOSX)
188 void OnBufferPresented(
189 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) override
;
191 void OnResize(gfx::Size size
, float scale_factor
) override
;
192 gfx::Size
GetSize() override
;
194 const std::vector
<ui::LatencyInfo
>& latency_info
) override
;
195 void WakeUpGpu() override
;
198 ~PassThroughImageTransportSurface() override
;
200 // If updated vsync parameters can be determined, send this information to
202 virtual void SendVSyncUpdateIfAvailable();
203 void SwapBuffersCallBack();
205 ImageTransportHelper
* GetHelper() { return helper_
.get(); }
208 scoped_ptr
<ImageTransportHelper
> helper_
;
209 bool did_set_swap_interval_
;
210 std::vector
<ui::LatencyInfo
> latency_info_
;
211 base::WeakPtrFactory
<PassThroughImageTransportSurface
> weak_ptr_factory_
;
213 DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface
);
216 } // namespace content
218 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_