in_process_command_buffer.h revision 116680a4aac90f2aa7413d9095a592090648e557
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/memory/linked_ptr.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 GpuControlService;
56class GpuMemoryBufferFactory;
57class GpuScheduler;
58class TransferBufferManagerInterface;
59
60// This class provides a thread-safe interface to the global GPU service (for
61// example GPU thread) when being run in single process mode.
62// However, the behavior for accessing one context (i.e. one instance of this
63// class) from different client threads is undefined.
64class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
65                                          public GpuControl {
66 public:
67  class Service;
68  explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
69  virtual ~InProcessCommandBuffer();
70
71  static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory* factory);
72
73  // If |surface| is not NULL, use it directly; in this case, the command
74  // buffer gpu thread must be the same as the client thread. Otherwise create
75  // a new GLSurface.
76  bool Initialize(scoped_refptr<gfx::GLSurface> surface,
77                  bool is_offscreen,
78                  gfx::AcceleratedWidget window,
79                  const gfx::Size& size,
80                  const std::vector<int32>& attribs,
81                  gfx::GpuPreference gpu_preference,
82                  const base::Closure& context_lost_callback,
83                  InProcessCommandBuffer* share_group);
84  void Destroy();
85
86  // CommandBuffer implementation:
87  virtual bool Initialize() OVERRIDE;
88  virtual State GetLastState() OVERRIDE;
89  virtual int32 GetLastToken() OVERRIDE;
90  virtual void Flush(int32 put_offset) OVERRIDE;
91  virtual void WaitForTokenInRange(int32 start, int32 end) OVERRIDE;
92  virtual void WaitForGetOffsetInRange(int32 start, int32 end) OVERRIDE;
93  virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
94  virtual scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
95                                                          int32* id) OVERRIDE;
96  virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
97  virtual gpu::error::Error GetLastError() OVERRIDE;
98
99  // GpuControl implementation:
100  virtual gpu::Capabilities GetCapabilities() OVERRIDE;
101  virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(size_t width,
102                                                      size_t height,
103                                                      unsigned internalformat,
104                                                      unsigned usage,
105                                                      int32* id) OVERRIDE;
106  virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
107  virtual uint32 InsertSyncPoint() OVERRIDE;
108  virtual uint32 InsertFutureSyncPoint() OVERRIDE;
109  virtual void RetireSyncPoint(uint32 sync_point) OVERRIDE;
110  virtual void SignalSyncPoint(uint32 sync_point,
111                               const base::Closure& callback) OVERRIDE;
112  virtual void SignalQuery(uint32 query,
113                           const base::Closure& callback) OVERRIDE;
114  virtual void SetSurfaceVisible(bool visible) OVERRIDE;
115  virtual void Echo(const base::Closure& callback) OVERRIDE;
116  virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
117
118  // The serializer interface to the GPU service (i.e. thread).
119  class Service {
120   public:
121    Service();
122    virtual ~Service();
123
124    virtual void AddRef() const = 0;
125    virtual void Release() const = 0;
126
127    // Queues a task to run as soon as possible.
128    virtual void ScheduleTask(const base::Closure& task) = 0;
129
130    // Schedules |callback| to run at an appropriate time for performing idle
131    // work.
132    virtual void ScheduleIdleWork(const base::Closure& task) = 0;
133
134    virtual bool UseVirtualizedGLContexts() = 0;
135    virtual scoped_refptr<gles2::ShaderTranslatorCache>
136        shader_translator_cache() = 0;
137  };
138
139#if defined(OS_ANDROID)
140  scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
141      uint32 stream_id);
142#endif
143
144 private:
145  struct InitializeOnGpuThreadParams {
146    bool is_offscreen;
147    gfx::AcceleratedWidget window;
148    const gfx::Size& size;
149    const std::vector<int32>& attribs;
150    gfx::GpuPreference gpu_preference;
151    gpu::Capabilities* capabilities;  // Ouptut.
152    InProcessCommandBuffer* context_group;
153
154    InitializeOnGpuThreadParams(bool is_offscreen,
155                                gfx::AcceleratedWidget window,
156                                const gfx::Size& size,
157                                const std::vector<int32>& attribs,
158                                gfx::GpuPreference gpu_preference,
159                                gpu::Capabilities* capabilities,
160                                InProcessCommandBuffer* share_group)
161        : is_offscreen(is_offscreen),
162          window(window),
163          size(size),
164          attribs(attribs),
165          gpu_preference(gpu_preference),
166          capabilities(capabilities),
167          context_group(share_group) {}
168  };
169
170  bool InitializeOnGpuThread(const InitializeOnGpuThreadParams& params);
171  bool DestroyOnGpuThread();
172  void FlushOnGpuThread(int32 put_offset);
173  uint32 CreateStreamTextureOnGpuThread(uint32 client_texture_id);
174  bool MakeCurrent();
175  base::Closure WrapCallback(const base::Closure& callback);
176  State GetStateFast();
177  void QueueTask(const base::Closure& task) { service_->ScheduleTask(task); }
178  void CheckSequencedThread();
179  void RetireSyncPointOnGpuThread(uint32 sync_point);
180  void SignalSyncPointOnGpuThread(uint32 sync_point,
181                                  const base::Closure& callback);
182  void DestroyTransferBufferOnGputhread(int32 id);
183
184  // Callbacks:
185  void OnContextLost();
186  void OnResizeView(gfx::Size size, float scale_factor);
187  bool GetBufferChanged(int32 transfer_buffer_id);
188  void PumpCommands();
189  void ScheduleMoreIdleWork();
190
191  static scoped_refptr<Service> GetDefaultService();
192
193  // Members accessed on the gpu thread (possibly with the exception of
194  // creation):
195  bool context_lost_;
196  scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
197  scoped_ptr<GpuScheduler> gpu_scheduler_;
198  scoped_ptr<gles2::GLES2Decoder> decoder_;
199  scoped_refptr<gfx::GLContext> context_;
200  scoped_refptr<gfx::GLSurface> surface_;
201  base::Closure context_lost_callback_;
202
203  // Members accessed on the client thread:
204  State last_state_;
205  int32 last_put_offset_;
206  gpu::Capabilities capabilities_;
207  typedef std::map<int32, linked_ptr<gfx::GpuMemoryBuffer> > GpuMemoryBufferMap;
208  GpuMemoryBufferMap gpu_memory_buffers_;
209
210  // Accessed on both threads:
211  scoped_ptr<CommandBufferServiceBase> command_buffer_;
212  base::Lock command_buffer_lock_;
213  base::WaitableEvent flush_event_;
214  scoped_refptr<Service> service_;
215  State state_after_last_flush_;
216  base::Lock state_after_last_flush_lock_;
217  scoped_ptr<GpuControlService> gpu_control_;
218  scoped_refptr<gfx::GLShareGroup> gl_share_group_;
219
220#if defined(OS_ANDROID)
221  scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
222#endif
223
224  // Only used with explicit scheduling and the gpu thread is the same as
225  // the client thread.
226  scoped_ptr<base::SequenceChecker> sequence_checker_;
227
228  base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
229  base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
230
231  DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
232};
233
234}  // namespace gpu
235
236#endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
237