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 #if defined(OS_MACOSX)
26 struct AcceleratedSurfaceMsg_BufferPresented_Params
;
27 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
;
42 class GpuChannelManager
;
43 class GpuCommandBufferStub
;
45 // The GPU process is agnostic as to how it displays results. On some platforms
46 // it renders directly to window. On others it renders offscreen and transports
47 // the results to the browser process to display. This file provides a simple
48 // framework for making the offscreen path seem more like the onscreen path.
50 // The ImageTransportSurface class defines an simple interface for events that
51 // should be responded to. The factory returns an offscreen surface that looks
52 // a lot like an onscreen surface to the GPU process.
54 // The ImageTransportSurfaceHelper provides some glue to the outside world:
55 // making sure outside events reach the ImageTransportSurface and
56 // allowing the ImageTransportSurface to send events to the outside world.
58 class ImageTransportSurface
{
60 ImageTransportSurface();
62 #if defined(OS_MACOSX)
63 virtual void OnBufferPresented(
64 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) = 0;
66 virtual void OnResize(gfx::Size size
, float scale_factor
) = 0;
67 virtual void SetLatencyInfo(
68 const std::vector
<ui::LatencyInfo
>& latency_info
) = 0;
69 virtual void WakeUpGpu() = 0;
71 // Creates a surface with the given attributes.
72 static scoped_refptr
<gfx::GLSurface
> CreateSurface(
73 GpuChannelManager
* manager
,
74 GpuCommandBufferStub
* stub
,
75 const gfx::GLSurfaceHandle
& handle
);
77 #if defined(OS_MACOSX)
78 CONTENT_EXPORT
static void SetAllowOSMesaForTesting(bool allow
);
81 virtual gfx::Size
GetSize() = 0;
84 virtual ~ImageTransportSurface();
87 // Creates the appropriate native surface depending on the GL implementation.
88 // This will be implemented separately by each platform.
90 // This will not be called for texture transport surfaces which are
91 // cross-platform. The platform implementation should only create the
92 // surface and should not initialize it. On failure, a null scoped_refptr
93 // should be returned.
94 static scoped_refptr
<gfx::GLSurface
> CreateNativeSurface(
95 GpuChannelManager
* manager
,
96 GpuCommandBufferStub
* stub
,
97 const gfx::GLSurfaceHandle
& handle
);
99 #if defined(OS_ANDROID)
100 static scoped_refptr
<gfx::GLSurface
> CreateTransportSurface(
101 GpuChannelManager
* manager
,
102 GpuCommandBufferStub
* stub
,
103 const gfx::GLSurfaceHandle
& handle
);
106 DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface
);
109 class ImageTransportHelper
110 : public IPC::Listener
,
111 public base::SupportsWeakPtr
<ImageTransportHelper
> {
113 // Takes weak pointers to objects that outlive the helper.
114 ImageTransportHelper(ImageTransportSurface
* surface
,
115 GpuChannelManager
* manager
,
116 GpuCommandBufferStub
* stub
,
117 gfx::PluginWindowHandle handle
);
118 ~ImageTransportHelper() override
;
122 // IPC::Listener implementation:
123 bool OnMessageReceived(const IPC::Message
& message
) override
;
125 // Helper send functions. Caller fills in the surface specific params
126 // like size and surface id. The helper fills in the rest.
127 #if defined(OS_MACOSX)
128 void SendAcceleratedSurfaceBuffersSwapped(
129 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params
);
132 void SetPreemptByFlag(
133 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
135 // Make the surface's context current.
138 // Set the default swap interval on the surface.
139 static void SetSwapInterval(gfx::GLContext
* context
);
141 GpuChannelManager
* manager() const { return manager_
; }
142 GpuCommandBufferStub
* stub() const { return stub_
.get(); }
145 gpu::gles2::GLES2Decoder
* Decoder();
147 // IPC::Message handlers.
148 #if defined(OS_MACOSX)
149 void OnBufferPresented(
150 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
);
154 // Backbuffer resize callback.
155 void Resize(gfx::Size size
, float scale_factor
);
157 void SetLatencyInfo(const std::vector
<ui::LatencyInfo
>& latency_info
);
159 // Weak pointers that point to objects that outlive this helper.
160 ImageTransportSurface
* surface_
;
161 GpuChannelManager
* manager_
;
163 base::WeakPtr
<GpuCommandBufferStub
> stub_
;
165 gfx::PluginWindowHandle handle_
;
167 DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper
);
170 // An implementation of ImageTransportSurface that implements GLSurface through
171 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
172 class PassThroughImageTransportSurface
173 : public gfx::GLSurfaceAdapter
,
174 public ImageTransportSurface
{
176 PassThroughImageTransportSurface(GpuChannelManager
* manager
,
177 GpuCommandBufferStub
* stub
,
178 gfx::GLSurface
* surface
);
180 // GLSurface implementation.
181 bool Initialize() override
;
182 void Destroy() override
;
183 gfx::SwapResult
SwapBuffers() override
;
184 gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
) override
;
185 bool OnMakeCurrent(gfx::GLContext
* context
) override
;
187 // ImageTransportSurface implementation.
188 #if defined(OS_MACOSX)
189 void OnBufferPresented(
190 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) override
;
192 void OnResize(gfx::Size size
, float scale_factor
) override
;
193 gfx::Size
GetSize() override
;
195 const std::vector
<ui::LatencyInfo
>& latency_info
) override
;
196 void WakeUpGpu() override
;
199 ~PassThroughImageTransportSurface() override
;
201 // If updated vsync parameters can be determined, send this information to
203 virtual void SendVSyncUpdateIfAvailable();
204 void SwapBuffersCallBack(std::vector
<ui::LatencyInfo
>* latency_info_ptr
,
205 gfx::SwapResult result
);
207 ImageTransportHelper
* GetHelper() { return helper_
.get(); }
210 scoped_ptr
<ImageTransportHelper
> helper_
;
211 bool did_set_swap_interval_
;
212 std::vector
<ui::LatencyInfo
> latency_info_
;
213 base::WeakPtrFactory
<PassThroughImageTransportSurface
> weak_ptr_factory_
;
215 DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface
);
218 } // namespace content
220 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_