Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / common / gpu / image_transport_surface.h
blobfea2d4f0980fa62df442a63ef31321247a03835a
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_
8 #include <vector>
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;
27 namespace gfx {
28 class GLSurface;
31 namespace gpu {
32 class GpuScheduler;
33 class PreemptionFlag;
34 namespace gles2 {
35 class GLES2Decoder;
39 namespace content {
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 {
57 public:
58 ImageTransportSurface();
60 #if defined(OS_MACOSX)
61 virtual void OnBufferPresented(
62 const AcceleratedSurfaceMsg_BufferPresented_Params& params) = 0;
63 #endif
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);
77 #endif
79 virtual gfx::Size GetSize() = 0;
81 protected:
82 virtual ~ImageTransportSurface();
84 private:
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);
102 #endif
104 DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface);
107 class ImageTransportHelper
108 : public IPC::Listener,
109 public base::SupportsWeakPtr<ImageTransportHelper> {
110 public:
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;
118 bool Initialize();
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);
127 void SendUpdateVSyncParameters(
128 base::TimeTicks timebase, base::TimeDelta interval);
130 void SwapBuffersCompleted(const std::vector<ui::LatencyInfo>& latency_info);
132 // Whether or not we should execute more commands.
133 void SetScheduled(bool is_scheduled);
135 void DeferToFence(base::Closure task);
137 void SetPreemptByFlag(
138 scoped_refptr<gpu::PreemptionFlag> preemption_flag);
140 // Make the surface's context current.
141 bool MakeCurrent();
143 // Set the default swap interval on the surface.
144 static void SetSwapInterval(gfx::GLContext* context);
146 GpuChannelManager* manager() const { return manager_; }
147 GpuCommandBufferStub* stub() const { return stub_.get(); }
149 private:
150 gpu::GpuScheduler* Scheduler();
151 gpu::gles2::GLES2Decoder* Decoder();
153 // IPC::Message handlers.
154 #if defined(OS_MACOSX)
155 void OnBufferPresented(
156 const AcceleratedSurfaceMsg_BufferPresented_Params& params);
157 #endif
158 void OnWakeUpGpu();
160 // Backbuffer resize callback.
161 void Resize(gfx::Size size, float scale_factor);
163 void SetLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
165 // Weak pointers that point to objects that outlive this helper.
166 ImageTransportSurface* surface_;
167 GpuChannelManager* manager_;
169 base::WeakPtr<GpuCommandBufferStub> stub_;
170 int32 route_id_;
171 gfx::PluginWindowHandle handle_;
173 DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper);
176 // An implementation of ImageTransportSurface that implements GLSurface through
177 // GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
178 class PassThroughImageTransportSurface
179 : public gfx::GLSurfaceAdapter,
180 public ImageTransportSurface {
181 public:
182 PassThroughImageTransportSurface(GpuChannelManager* manager,
183 GpuCommandBufferStub* stub,
184 gfx::GLSurface* surface);
186 // GLSurface implementation.
187 bool Initialize() override;
188 void Destroy() override;
189 bool SwapBuffers() override;
190 bool PostSubBuffer(int x, int y, int width, int height) override;
191 bool OnMakeCurrent(gfx::GLContext* context) override;
193 // ImageTransportSurface implementation.
194 #if defined(OS_MACOSX)
195 void OnBufferPresented(
196 const AcceleratedSurfaceMsg_BufferPresented_Params& params) override;
197 #endif
198 void OnResize(gfx::Size size, float scale_factor) override;
199 gfx::Size GetSize() override;
200 void SetLatencyInfo(
201 const std::vector<ui::LatencyInfo>& latency_info) override;
202 void WakeUpGpu() override;
204 protected:
205 ~PassThroughImageTransportSurface() override;
207 // If updated vsync parameters can be determined, send this information to
208 // the browser.
209 virtual void SendVSyncUpdateIfAvailable();
211 ImageTransportHelper* GetHelper() { return helper_.get(); }
213 private:
214 scoped_ptr<ImageTransportHelper> helper_;
215 bool did_set_swap_interval_;
216 std::vector<ui::LatencyInfo> latency_info_;
218 DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface);
221 } // namespace content
223 #endif // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_