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