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.
4
5#ifndef CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_
6#define CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_
7
8#include <vector>
9
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"
23
24struct AcceleratedSurfaceMsg_BufferPresented_Params;
25struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
26struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
27
28namespace gfx {
29class GLSurface;
30}
31
32namespace gpu {
33class GpuScheduler;
34class PreemptionFlag;
35namespace gles2 {
36class GLES2Decoder;
37}
38}
39
40namespace content {
41class GpuChannelManager;
42class GpuCommandBufferStub;
43
44// The GPU process is agnostic as to how it displays results. On some platforms
45// it renders directly to window. On others it renders offscreen and transports
46// the results to the browser process to display. This file provides a simple
47// framework for making the offscreen path seem more like the onscreen path.
48//
49// The ImageTransportSurface class defines an simple interface for events that
50// should be responded to. The factory returns an offscreen surface that looks
51// a lot like an onscreen surface to the GPU process.
52//
53// The ImageTransportSurfaceHelper provides some glue to the outside world:
54// making sure outside events reach the ImageTransportSurface and
55// allowing the ImageTransportSurface to send events to the outside world.
56
57class ImageTransportSurface {
58 public:
59  ImageTransportSurface();
60
61  virtual void OnBufferPresented(
62      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;
67
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);
73
74#if defined(OS_MACOSX)
75  CONTENT_EXPORT static void SetAllowOSMesaForTesting(bool allow);
76#endif
77
78  virtual gfx::Size GetSize() = 0;
79
80 protected:
81  virtual ~ImageTransportSurface();
82
83 private:
84  // Creates the appropriate native surface depending on the GL implementation.
85  // This will be implemented separately by each platform.
86  //
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);
95
96  DISALLOW_COPY_AND_ASSIGN(ImageTransportSurface);
97};
98
99class ImageTransportHelper
100    : public IPC::Listener,
101      public base::SupportsWeakPtr<ImageTransportHelper> {
102 public:
103  // Takes weak pointers to objects that outlive the helper.
104  ImageTransportHelper(ImageTransportSurface* surface,
105                       GpuChannelManager* manager,
106                       GpuCommandBufferStub* stub,
107                       gfx::PluginWindowHandle handle);
108  virtual ~ImageTransportHelper();
109
110  bool Initialize();
111  void Destroy();
112
113  // IPC::Listener implementation:
114  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
115
116  // Helper send functions. Caller fills in the surface specific params
117  // like size and surface id. The helper fills in the rest.
118  void SendAcceleratedSurfaceBuffersSwapped(
119      GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params);
120  void SendAcceleratedSurfacePostSubBuffer(
121      GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params);
122  void SendAcceleratedSurfaceRelease();
123  void SendUpdateVSyncParameters(
124      base::TimeTicks timebase, base::TimeDelta interval);
125
126  void SendLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
127
128  // Whether or not we should execute more commands.
129  void SetScheduled(bool is_scheduled);
130
131  void DeferToFence(base::Closure task);
132
133  void SetPreemptByFlag(
134      scoped_refptr<gpu::PreemptionFlag> preemption_flag);
135
136  // Make the surface's context current.
137  bool MakeCurrent();
138
139  // Set the default swap interval on the surface.
140  static void SetSwapInterval(gfx::GLContext* context);
141
142  void Suspend();
143
144  GpuChannelManager* manager() const { return manager_; }
145  GpuCommandBufferStub* stub() const { return stub_.get(); }
146
147 private:
148  gpu::GpuScheduler* Scheduler();
149  gpu::gles2::GLES2Decoder* Decoder();
150
151  // IPC::Message handlers.
152  void OnBufferPresented(
153      const AcceleratedSurfaceMsg_BufferPresented_Params& params);
154  void OnWakeUpGpu();
155
156  // Backbuffer resize callback.
157  void Resize(gfx::Size size, float scale_factor);
158
159  void SetLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
160
161  // Weak pointers that point to objects that outlive this helper.
162  ImageTransportSurface* surface_;
163  GpuChannelManager* manager_;
164
165  base::WeakPtr<GpuCommandBufferStub> stub_;
166  int32 route_id_;
167  gfx::PluginWindowHandle handle_;
168
169  DISALLOW_COPY_AND_ASSIGN(ImageTransportHelper);
170};
171
172// An implementation of ImageTransportSurface that implements GLSurface through
173// GLSurfaceAdapter, thereby forwarding GLSurface methods through to it.
174class PassThroughImageTransportSurface
175    : public gfx::GLSurfaceAdapter,
176      public ImageTransportSurface {
177 public:
178  PassThroughImageTransportSurface(GpuChannelManager* manager,
179                                   GpuCommandBufferStub* stub,
180                                   gfx::GLSurface* surface);
181
182  // GLSurface implementation.
183  virtual bool Initialize() OVERRIDE;
184  virtual void Destroy() OVERRIDE;
185  virtual bool SwapBuffers() OVERRIDE;
186  virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
187  virtual bool OnMakeCurrent(gfx::GLContext* context) OVERRIDE;
188
189  // ImageTransportSurface implementation.
190  virtual void OnBufferPresented(
191      const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE;
192  virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE;
193  virtual gfx::Size GetSize() OVERRIDE;
194  virtual void SetLatencyInfo(
195      const std::vector<ui::LatencyInfo>& latency_info) OVERRIDE;
196  virtual void WakeUpGpu() OVERRIDE;
197
198 protected:
199  virtual ~PassThroughImageTransportSurface();
200
201  // If updated vsync parameters can be determined, send this information to
202  // the browser.
203  virtual void SendVSyncUpdateIfAvailable();
204
205  ImageTransportHelper* GetHelper() { return helper_.get(); }
206
207 private:
208  scoped_ptr<ImageTransportHelper> helper_;
209  bool did_set_swap_interval_;
210  std::vector<ui::LatencyInfo> latency_info_;
211
212  DISALLOW_COPY_AND_ASSIGN(PassThroughImageTransportSurface);
213};
214
215}  // namespace content
216
217#endif  // CONTENT_COMMON_GPU_IMAGE_TRANSPORT_SURFACE_H_
218