in_process_command_buffer.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2013 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 GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
6#define GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
7
8#include <map>
9#include <vector>
10
11#include "base/callback.h"
12#include "base/compiler_specific.h"
13#include "base/containers/scoped_ptr_hash_map.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/scoped_ptr.h"
16#include "base/memory/weak_ptr.h"
17#include "base/synchronization/lock.h"
18#include "base/synchronization/waitable_event.h"
19#include "gpu/command_buffer/client/gpu_control.h"
20#include "gpu/command_buffer/common/command_buffer.h"
21#include "gpu/gpu_export.h"
22#include "ui/gfx/gpu_memory_buffer.h"
23#include "ui/gfx/native_widget_types.h"
24#include "ui/gl/gl_surface.h"
25#include "ui/gl/gpu_preference.h"
26
27namespace base {
28class SequenceChecker;
29}
30
31namespace gfx {
32class GLContext;
33class GLShareGroup;
34class GLSurface;
35class Size;
36}
37
38#if defined(OS_ANDROID)
39namespace gfx {
40class SurfaceTexture;
41}
42namespace gpu {
43class StreamTextureManagerInProcess;
44}
45#endif
46
47namespace gpu {
48
49namespace gles2 {
50class GLES2Decoder;
51class ShaderTranslatorCache;
52}
53
54class CommandBufferServiceBase;
55class GpuScheduler;
56class TransferBufferManagerInterface;
57
58// TODO(reveman): Remove this interface when InProcessCommandBuffer doesn't need
59// a custom factory interface and android_webview implementation of GPU memory
60// buffers can use the same mechanism for buffer allocation as what's used for
61// out of process GPU service.
62class GPU_EXPORT InProcessGpuMemoryBufferFactory {
63 public:
64  virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
65      size_t width,
66      size_t height,
67      unsigned internalformat,
68      unsigned usage) = 0;
69  virtual scoped_refptr<gfx::GLImage> CreateImageForGpuMemoryBuffer(
70      const gfx::GpuMemoryBufferHandle& handle,
71      const gfx::Size& size,
72      unsigned internalformat) = 0;
73
74 protected:
75  virtual ~InProcessGpuMemoryBufferFactory() {}
76};
77
78// This class provides a thread-safe interface to the global GPU service (for
79// example GPU thread) when being run in single process mode.
80// However, the behavior for accessing one context (i.e. one instance of this
81// class) from different client threads is undefined.
82class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
83                                          public GpuControl {
84 public:
85  class Service;
86  explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
87  virtual ~InProcessCommandBuffer();
88
89  static void SetGpuMemoryBufferFactory(
90      InProcessGpuMemoryBufferFactory* factory);
91
92  // If |surface| is not NULL, use it directly; in this case, the command
93  // buffer gpu thread must be the same as the client thread. Otherwise create
94  // a new GLSurface.
95  bool Initialize(scoped_refptr<gfx::GLSurface> surface,
96                  bool is_offscreen,
97                  gfx::AcceleratedWidget window,
98                  const gfx::Size& size,
99                  const std::vector<int32>& attribs,
100                  gfx::GpuPreference gpu_preference,
101                  const base::Closure& context_lost_callback,
102                  InProcessCommandBuffer* share_group);
103  void Destroy();
104
105  // CommandBuffer implementation:
106  virtual bool Initialize() OVERRIDE;
107  virtual State GetLastState() OVERRIDE;
108  virtual int32 GetLastToken() OVERRIDE;
109  virtual void Flush(int32 put_offset) OVERRIDE;
110  virtual void WaitForTokenInRange(int32 start, int32 end) OVERRIDE;
111  virtual void WaitForGetOffsetInRange(int32 start, int32 end) OVERRIDE;
112  virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
113  virtual scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
114                                                          int32* id) OVERRIDE;
115  virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
116  virtual gpu::error::Error GetLastError() OVERRIDE;
117
118  // GpuControl implementation:
119  virtual gpu::Capabilities GetCapabilities() OVERRIDE;
120  virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(size_t width,
121                                                      size_t height,
122                                                      unsigned internalformat,
123                                                      unsigned usage,
124                                                      int32* id) OVERRIDE;
125  virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
126  virtual uint32 InsertSyncPoint() OVERRIDE;
127  virtual uint32 InsertFutureSyncPoint() OVERRIDE;
128  virtual void RetireSyncPoint(uint32 sync_point) OVERRIDE;
129  virtual void SignalSyncPoint(uint32 sync_point,
130                               const base::Closure& callback) OVERRIDE;
131  virtual void SignalQuery(uint32 query_id,
132                           const base::Closure& callback) OVERRIDE;
133  virtual void SetSurfaceVisible(bool visible) OVERRIDE;
134  virtual void Echo(const base::Closure& callback) OVERRIDE;
135  virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
136
137  // The serializer interface to the GPU service (i.e. thread).
138  class Service {
139   public:
140    Service();
141    virtual ~Service();
142
143    virtual void AddRef() const = 0;
144    virtual void Release() const = 0;
145
146    // Queues a task to run as soon as possible.
147    virtual void ScheduleTask(const base::Closure& task) = 0;
148
149    // Schedules |callback| to run at an appropriate time for performing idle
150    // work.
151    virtual void ScheduleIdleWork(const base::Closure& task) = 0;
152
153    virtual bool UseVirtualizedGLContexts() = 0;
154    virtual scoped_refptr<gles2::ShaderTranslatorCache>
155        shader_translator_cache() = 0;
156  };
157
158#if defined(OS_ANDROID)
159  scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
160      uint32 stream_id);
161#endif
162
163 private:
164  struct InitializeOnGpuThreadParams {
165    bool is_offscreen;
166    gfx::AcceleratedWidget window;
167    const gfx::Size& size;
168    const std::vector<int32>& attribs;
169    gfx::GpuPreference gpu_preference;
170    gpu::Capabilities* capabilities;  // Ouptut.
171    InProcessCommandBuffer* context_group;
172
173    InitializeOnGpuThreadParams(bool is_offscreen,
174                                gfx::AcceleratedWidget window,
175                                const gfx::Size& size,
176                                const std::vector<int32>& attribs,
177                                gfx::GpuPreference gpu_preference,
178                                gpu::Capabilities* capabilities,
179                                InProcessCommandBuffer* share_group)
180        : is_offscreen(is_offscreen),
181          window(window),
182          size(size),
183          attribs(attribs),
184          gpu_preference(gpu_preference),
185          capabilities(capabilities),
186          context_group(share_group) {}
187  };
188
189  bool InitializeOnGpuThread(const InitializeOnGpuThreadParams& params);
190  bool DestroyOnGpuThread();
191  void FlushOnGpuThread(int32 put_offset);
192  void ScheduleIdleWorkOnGpuThread();
193  uint32 CreateStreamTextureOnGpuThread(uint32 client_texture_id);
194  bool MakeCurrent();
195  base::Closure WrapCallback(const base::Closure& callback);
196  State GetStateFast();
197  void QueueTask(const base::Closure& task) { service_->ScheduleTask(task); }
198  void CheckSequencedThread();
199  void RetireSyncPointOnGpuThread(uint32 sync_point);
200  void SignalSyncPointOnGpuThread(uint32 sync_point,
201                                  const base::Closure& callback);
202  void SignalQueryOnGpuThread(unsigned query_id, const base::Closure& callback);
203  void DestroyTransferBufferOnGpuThread(int32 id);
204  void RegisterGpuMemoryBufferOnGpuThread(
205      int32 id,
206      const gfx::GpuMemoryBufferHandle& handle,
207      size_t width,
208      size_t height,
209      unsigned internalformat);
210  void UnregisterGpuMemoryBufferOnGpuThread(int32 id);
211
212  // Callbacks:
213  void OnContextLost();
214  void OnResizeView(gfx::Size size, float scale_factor);
215  bool GetBufferChanged(int32 transfer_buffer_id);
216  void PumpCommands();
217  void PerformIdleWork();
218
219  static scoped_refptr<Service> GetDefaultService();
220
221  // Members accessed on the gpu thread (possibly with the exception of
222  // creation):
223  bool context_lost_;
224  scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
225  scoped_ptr<GpuScheduler> gpu_scheduler_;
226  scoped_ptr<gles2::GLES2Decoder> decoder_;
227  scoped_refptr<gfx::GLContext> context_;
228  scoped_refptr<gfx::GLSurface> surface_;
229  base::Closure context_lost_callback_;
230  bool idle_work_pending_;  // Used to throttle PerformIdleWork.
231
232  // Members accessed on the client thread:
233  State last_state_;
234  int32 last_put_offset_;
235  gpu::Capabilities capabilities_;
236  typedef base::ScopedPtrHashMap<int32, gfx::GpuMemoryBuffer>
237      GpuMemoryBufferMap;
238  GpuMemoryBufferMap gpu_memory_buffers_;
239
240  // Accessed on both threads:
241  scoped_ptr<CommandBufferServiceBase> command_buffer_;
242  base::Lock command_buffer_lock_;
243  base::WaitableEvent flush_event_;
244  scoped_refptr<Service> service_;
245  State state_after_last_flush_;
246  base::Lock state_after_last_flush_lock_;
247  scoped_refptr<gfx::GLShareGroup> gl_share_group_;
248
249#if defined(OS_ANDROID)
250  scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
251#endif
252
253  // Only used with explicit scheduling and the gpu thread is the same as
254  // the client thread.
255  scoped_ptr<base::SequenceChecker> sequence_checker_;
256
257  base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
258  base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
259
260  DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
261};
262
263}  // namespace gpu
264
265#endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
266