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/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
);
127 void SetPreemptByFlag(
128 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
130 // Make the surface's context current.
133 // Set the default swap interval on the surface.
134 static void SetSwapInterval(gfx::GLContext
* context
);
136 GpuChannelManager
* manager() const { return manager_
; }
137 GpuCommandBufferStub
* stub() const { return stub_
.get(); }
140 gpu::gles2::GLES2Decoder
* Decoder();
142 // IPC::Message handlers.
143 #if defined(OS_MACOSX)
144 void OnBufferPresented(
145 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
);
149 // Backbuffer resize callback.
150 void Resize(gfx::Size size
, float scale_factor
);
152 void SetLatencyInfo(const std::vector
<ui::LatencyInfo
>& latency_info
);
154 // Weak pointers that point to objects that outlive this helper.
155 ImageTransportSurface
* surface_
;
156 GpuChannelManager
* manager_
;
158 base::WeakPtr
<GpuCommandBufferStub
> stub_
;
160 gfx::PluginWindowHandle handle_
;
162 DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper
);
165 // An implementation of ImageTransportSurface that implements GLSurface through
166 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
167 class PassThroughImageTransportSurface
168 : public gfx::GLSurfaceAdapter
,
169 public ImageTransportSurface
{
171 PassThroughImageTransportSurface(GpuChannelManager
* manager
,
172 GpuCommandBufferStub
* stub
,
173 gfx::GLSurface
* surface
);
175 // GLSurface implementation.
176 bool Initialize() override
;
177 void Destroy() override
;
178 bool SwapBuffers() override
;
179 bool PostSubBuffer(int x
, int y
, int width
, int height
) override
;
180 bool OnMakeCurrent(gfx::GLContext
* context
) override
;
182 // ImageTransportSurface implementation.
183 #if defined(OS_MACOSX)
184 void OnBufferPresented(
185 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) override
;
187 void OnResize(gfx::Size size
, float scale_factor
) override
;
188 gfx::Size
GetSize() override
;
190 const std::vector
<ui::LatencyInfo
>& latency_info
) override
;
191 void WakeUpGpu() override
;
194 ~PassThroughImageTransportSurface() override
;
196 // If updated vsync parameters can be determined, send this information to
198 virtual void SendVSyncUpdateIfAvailable();
199 void SwapBuffersCallBack();
201 ImageTransportHelper
* GetHelper() { return helper_
.get(); }
204 scoped_ptr
<ImageTransportHelper
> helper_
;
205 bool did_set_swap_interval_
;
206 std::vector
<ui::LatencyInfo
> latency_info_
;
207 base::WeakPtrFactory
<PassThroughImageTransportSurface
> weak_ptr_factory_
;
209 DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface
);
212 } // namespace content
214 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_