in_process_command_buffer.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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 GLSurface;
32class Size;
33}
34
35#if defined(OS_ANDROID)
36namespace gfx {
37class SurfaceTexture;
38}
39namespace gpu {
40class StreamTextureManagerInProcess;
41}
42#endif
43
44namespace gpu {
45
46namespace gles2 {
47class GLES2Decoder;
48}
49
50class GpuMemoryBufferFactory;
51class GpuScheduler;
52class TransferBufferManagerInterface;
53
54// This class provides a thread-safe interface to the global GPU service (for
55// example GPU thread) when being run in single process mode.
56// However, the behavior for accessing one context (i.e. one instance of this
57// class) from different client threads is undefined.
58class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
59                                          public GpuControl {
60 public:
61  InProcessCommandBuffer();
62  virtual ~InProcessCommandBuffer();
63
64  // Used to override the GPU thread with explicit scheduling.
65  // (By default an internal GPU thread will be spawned to handle all GL work
66  // and the two functions are unused.)
67  // The callback will be called from different client threads. After the
68  // callback is issued, the client is expected to eventually call
69  // ProcessGpuWorkOnCurrentThread(). The latter cannot be called from different
70  // threads.
71  // The callback needs to be set before any context is created.
72  static void SetScheduleCallback(const base::Closure& callback);
73  static void ProcessGpuWorkOnCurrentThread();
74
75  static void EnableVirtualizedContext();
76  static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory* factory);
77
78  // If |surface| is not NULL, use it directly; in this case, the command
79  // buffer gpu thread must be the same as the client thread. Otherwise create
80  // a new GLSurface.
81  bool Initialize(scoped_refptr<gfx::GLSurface> surface,
82                  bool is_offscreen,
83                  bool share_resources,
84                  gfx::AcceleratedWidget window,
85                  const gfx::Size& size,
86                  const std::vector<int32>& attribs,
87                  gfx::GpuPreference gpu_preference,
88                  const base::Closure& context_lost_callback,
89                  unsigned int share_group_id);
90  void Destroy();
91
92  // CommandBuffer implementation:
93  virtual bool Initialize() OVERRIDE;
94  virtual State GetState() OVERRIDE;
95  virtual State GetLastState() OVERRIDE;
96  virtual int32 GetLastToken() OVERRIDE;
97  virtual void Flush(int32 put_offset) OVERRIDE;
98  virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
99  virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
100  virtual void SetGetOffset(int32 get_offset) OVERRIDE;
101  virtual gpu::Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
102  virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
103  virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
104  virtual void SetToken(int32 token) OVERRIDE;
105  virtual void SetParseError(gpu::error::Error error) OVERRIDE;
106  virtual void SetContextLostReason(
107      gpu::error::ContextLostReason reason) OVERRIDE;
108  virtual gpu::error::Error GetLastError() OVERRIDE;
109
110  // GpuControl implementation:
111  virtual bool SupportsGpuMemoryBuffer() OVERRIDE;
112  virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
113      size_t width,
114      size_t height,
115      unsigned internalformat,
116      int32* id) OVERRIDE;
117  virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
118  virtual bool GenerateMailboxNames(unsigned num,
119                                    std::vector<gpu::Mailbox>* names) OVERRIDE;
120  virtual uint32 InsertSyncPoint() OVERRIDE;
121  virtual void SignalSyncPoint(uint32 sync_point,
122                               const base::Closure& callback) OVERRIDE;
123  virtual void SignalQuery(uint32 query,
124                           const base::Closure& callback) OVERRIDE;
125
126  // The serializer interface to the GPU service (i.e. thread).
127  class SchedulerClient {
128   public:
129     virtual ~SchedulerClient() {}
130
131     // Queues a task to run as soon as possible.
132     virtual void QueueTask(const base::Closure& task) = 0;
133
134     // Schedules |callback| to run at an appropriate time for performing idle
135     // work.
136     virtual void ScheduleIdleWork(const base::Closure& task) = 0;
137  };
138
139#if defined(OS_ANDROID)
140  scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
141      uint32 stream_id);
142#endif
143
144 private:
145  bool InitializeOnGpuThread(bool is_offscreen,
146                             gfx::AcceleratedWidget window,
147                             const gfx::Size& size,
148                             const std::vector<int32>& attribs,
149                             gfx::GpuPreference gpu_preference);
150  bool DestroyOnGpuThread();
151  void FlushOnGpuThread(int32 put_offset);
152  bool MakeCurrent();
153  bool IsContextLost();
154  base::Closure WrapCallback(const base::Closure& callback);
155  State GetStateFast();
156  void QueueTask(const base::Closure& task) { queue_->QueueTask(task); }
157  void CheckSequencedThread();
158
159  // Callbacks:
160  void OnContextLost();
161  void OnResizeView(gfx::Size size, float scale_factor);
162  bool GetBufferChanged(int32 transfer_buffer_id);
163  void PumpCommands();
164  void ScheduleMoreIdleWork();
165
166  // Members accessed on the gpu thread (possibly with the exception of
167  // creation):
168  bool context_lost_;
169  bool share_resources_;
170  scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
171  scoped_ptr<GpuScheduler> gpu_scheduler_;
172  scoped_ptr<gles2::GLES2Decoder> decoder_;
173  scoped_refptr<gfx::GLContext> context_;
174  scoped_refptr<gfx::GLSurface> surface_;
175  base::Closure context_lost_callback_;
176  unsigned int share_group_id_;
177
178  // Members accessed on the client thread:
179  State last_state_;
180  int32 last_put_offset_;
181  bool supports_gpu_memory_buffer_;
182
183  // Accessed on both threads:
184  scoped_ptr<CommandBuffer> command_buffer_;
185  base::Lock command_buffer_lock_;
186  base::WaitableEvent flush_event_;
187  scoped_ptr<SchedulerClient> queue_;
188  State state_after_last_flush_;
189  base::Lock state_after_last_flush_lock_;
190  scoped_ptr<GpuControl> gpu_control_;
191
192#if defined(OS_ANDROID)
193  scoped_refptr<StreamTextureManagerInProcess> stream_texture_manager_;
194#endif
195
196  // Only used with explicit scheduling and the gpu thread is the same as
197  // the client thread.
198  scoped_ptr<base::SequenceChecker> sequence_checker_;
199
200  base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
201  base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
202
203  DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
204};
205
206}  // namespace gpu
207
208#endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
209