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/geometry/rect.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gfx/swap_result.h"
23 #include "ui/gl/gl_surface.h"
25 struct AcceleratedSurfaceMsg_BufferPresented_Params
;
26 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
;
40 class GpuChannelManager
;
41 class GpuCommandBufferStub
;
43 // The GPU process is agnostic as to how it displays results. On some platforms
44 // it renders directly to window. On others it renders offscreen and transports
45 // the results to the browser process to display. This file provides a simple
46 // framework for making the offscreen path seem more like the onscreen path.
48 // The ImageTransportSurface class defines an simple interface for events that
49 // should be responded to. The factory returns an offscreen surface that looks
50 // a lot like an onscreen surface to the GPU process.
52 // The ImageTransportSurfaceHelper provides some glue to the outside world:
53 // making sure outside events reach the ImageTransportSurface and
54 // allowing the ImageTransportSurface to send events to the outside world.
56 class ImageTransportSurface
{
58 ImageTransportSurface();
60 #if defined(OS_MACOSX)
61 virtual void OnBufferPresented(
62 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) = 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 #if defined(OS_ANDROID)
98 static scoped_refptr
<gfx::GLSurface
> CreateTransportSurface(
99 GpuChannelManager
* manager
,
100 GpuCommandBufferStub
* stub
,
101 const gfx::GLSurfaceHandle
& handle
);
104 DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface
);
107 class ImageTransportHelper
108 : public IPC::Listener
,
109 public base::SupportsWeakPtr
<ImageTransportHelper
> {
111 // Takes weak pointers to objects that outlive the helper.
112 ImageTransportHelper(ImageTransportSurface
* surface
,
113 GpuChannelManager
* manager
,
114 GpuCommandBufferStub
* stub
,
115 gfx::PluginWindowHandle handle
);
116 ~ImageTransportHelper() override
;
120 // IPC::Listener implementation:
121 bool OnMessageReceived(const IPC::Message
& message
) override
;
123 // Helper send functions. Caller fills in the surface specific params
124 // like size and surface id. The helper fills in the rest.
125 void SendAcceleratedSurfaceBuffersSwapped(
126 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params
);
128 void SetPreemptByFlag(
129 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
131 // Make the surface's context current.
134 // Set the default swap interval on the surface.
135 static void SetSwapInterval(gfx::GLContext
* context
);
137 GpuChannelManager
* manager() const { return manager_
; }
138 GpuCommandBufferStub
* stub() const { return stub_
.get(); }
141 gpu::gles2::GLES2Decoder
* Decoder();
143 // IPC::Message handlers.
144 #if defined(OS_MACOSX)
145 void OnBufferPresented(
146 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
);
150 // Backbuffer resize callback.
151 void Resize(gfx::Size size
, float scale_factor
);
153 void SetLatencyInfo(const std::vector
<ui::LatencyInfo
>& latency_info
);
155 // Weak pointers that point to objects that outlive this helper.
156 ImageTransportSurface
* surface_
;
157 GpuChannelManager
* manager_
;
159 base::WeakPtr
<GpuCommandBufferStub
> stub_
;
161 gfx::PluginWindowHandle handle_
;
163 DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper
);
166 // An implementation of ImageTransportSurface that implements GLSurface through
167 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
168 class PassThroughImageTransportSurface
169 : public gfx::GLSurfaceAdapter
,
170 public ImageTransportSurface
{
172 PassThroughImageTransportSurface(GpuChannelManager
* manager
,
173 GpuCommandBufferStub
* stub
,
174 gfx::GLSurface
* surface
);
176 // GLSurface implementation.
177 bool Initialize() override
;
178 void Destroy() override
;
179 gfx::SwapResult
SwapBuffers() override
;
180 gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
) override
;
181 bool OnMakeCurrent(gfx::GLContext
* context
) override
;
183 // ImageTransportSurface implementation.
184 #if defined(OS_MACOSX)
185 void OnBufferPresented(
186 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) override
;
188 void OnResize(gfx::Size size
, float scale_factor
) override
;
189 gfx::Size
GetSize() override
;
191 const std::vector
<ui::LatencyInfo
>& latency_info
) override
;
192 void WakeUpGpu() override
;
195 ~PassThroughImageTransportSurface() override
;
197 // If updated vsync parameters can be determined, send this information to
199 virtual void SendVSyncUpdateIfAvailable();
200 void SwapBuffersCallBack(std::vector
<ui::LatencyInfo
>* latency_info_ptr
,
201 gfx::SwapResult result
);
203 ImageTransportHelper
* GetHelper() { return helper_
.get(); }
206 scoped_ptr
<ImageTransportHelper
> helper_
;
207 bool did_set_swap_interval_
;
208 std::vector
<ui::LatencyInfo
> latency_info_
;
209 base::WeakPtrFactory
<PassThroughImageTransportSurface
> weak_ptr_factory_
;
211 DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface
);
214 } // namespace content
216 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_