Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / service / in_process_command_buffer.h
blob4dfee1984fa48cd23c1b7431a85e7a9575e52481
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.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
8 #include <map>
9 #include <vector>
11 #include "base/atomic_sequence_num.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/synchronization/lock.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "base/threading/thread.h"
21 #include "gpu/command_buffer/client/gpu_control.h"
22 #include "gpu/command_buffer/common/command_buffer.h"
23 #include "gpu/gpu_export.h"
24 #include "ui/gfx/gpu_memory_buffer.h"
25 #include "ui/gfx/native_widget_types.h"
26 #include "ui/gl/gl_surface.h"
27 #include "ui/gl/gpu_preference.h"
29 namespace base {
30 class SequenceChecker;
33 namespace gfx {
34 class GLContext;
35 class GLShareGroup;
36 class GLSurface;
37 class Size;
40 #if defined(OS_ANDROID)
41 namespace gfx {
42 class SurfaceTexture;
44 namespace gpu {
45 class StreamTextureManagerInProcess;
47 #endif
49 namespace gpu {
50 class SyncPointManager;
51 class ValueStateMap;
53 namespace gles2 {
54 class GLES2Decoder;
55 class MailboxManager;
56 class ShaderTranslatorCache;
57 class SubscriptionRefSet;
60 class CommandBufferServiceBase;
61 class GpuMemoryBufferManager;
62 class GpuScheduler;
63 class ImageFactory;
64 class TransferBufferManagerInterface;
66 // This class provides a thread-safe interface to the global GPU service (for
67 // example GPU thread) when being run in single process mode.
68 // However, the behavior for accessing one context (i.e. one instance of this
69 // class) from different client threads is undefined.
70 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
71 public GpuControl {
72 public:
73 class Service;
74 explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
75 ~InProcessCommandBuffer() override;
77 // If |surface| is not NULL, use it directly; in this case, the command
78 // buffer gpu thread must be the same as the client thread. Otherwise create
79 // a new GLSurface.
80 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
81 bool is_offscreen,
82 gfx::AcceleratedWidget window,
83 const gfx::Size& size,
84 const std::vector<int32>& attribs,
85 gfx::GpuPreference gpu_preference,
86 const base::Closure& context_lost_callback,
87 InProcessCommandBuffer* share_group,
88 GpuMemoryBufferManager* gpu_memory_buffer_manager,
89 ImageFactory* image_factory);
90 void Destroy();
92 // CommandBuffer implementation:
93 bool Initialize() override;
94 State GetLastState() override;
95 int32 GetLastToken() override;
96 void Flush(int32 put_offset) override;
97 void OrderingBarrier(int32 put_offset) override;
98 void WaitForTokenInRange(int32 start, int32 end) override;
99 void WaitForGetOffsetInRange(int32 start, int32 end) override;
100 void SetGetBuffer(int32 shm_id) override;
101 scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
102 int32* id) override;
103 void DestroyTransferBuffer(int32 id) override;
104 gpu::error::Error GetLastError() override;
106 // GpuControl implementation:
107 gpu::Capabilities GetCapabilities() override;
108 int32 CreateImage(ClientBuffer buffer,
109 size_t width,
110 size_t height,
111 unsigned internalformat) override;
112 void DestroyImage(int32 id) override;
113 int32 CreateGpuMemoryBufferImage(size_t width,
114 size_t height,
115 unsigned internalformat,
116 unsigned usage) override;
117 uint32 InsertSyncPoint() override;
118 uint32 InsertFutureSyncPoint() override;
119 void RetireSyncPoint(uint32 sync_point) override;
120 void SignalSyncPoint(uint32 sync_point,
121 const base::Closure& callback) override;
122 void SignalQuery(uint32 query_id, const base::Closure& callback) override;
123 void SetSurfaceVisible(bool visible) override;
124 uint32 CreateStreamTexture(uint32 texture_id) override;
125 void SetLock(base::Lock*) override;
126 bool IsGpuChannelLost() override;
128 // The serializer interface to the GPU service (i.e. thread).
129 class Service {
130 public:
131 Service();
132 virtual ~Service();
134 virtual void AddRef() const = 0;
135 virtual void Release() const = 0;
137 // Queues a task to run as soon as possible.
138 virtual void ScheduleTask(const base::Closure& task) = 0;
140 // Schedules |callback| to run at an appropriate time for performing idle
141 // work.
142 virtual void ScheduleIdleWork(const base::Closure& task) = 0;
144 virtual bool UseVirtualizedGLContexts() = 0;
145 virtual scoped_refptr<gles2::ShaderTranslatorCache>
146 shader_translator_cache() = 0;
147 virtual SyncPointManager* sync_point_manager() = 0;
148 scoped_refptr<gfx::GLShareGroup> share_group();
149 scoped_refptr<gles2::MailboxManager> mailbox_manager();
150 scoped_refptr<gles2::SubscriptionRefSet> subscription_ref_set();
151 scoped_refptr<gpu::ValueStateMap> pending_valuebuffer_state();
153 private:
154 scoped_refptr<gfx::GLShareGroup> share_group_;
155 scoped_refptr<gles2::MailboxManager> mailbox_manager_;
156 scoped_refptr<gles2::SubscriptionRefSet> subscription_ref_set_;
157 scoped_refptr<gpu::ValueStateMap> pending_valuebuffer_state_;
160 #if defined(OS_ANDROID)
161 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
162 uint32 stream_id);
163 #endif
165 private:
166 struct InitializeOnGpuThreadParams {
167 bool is_offscreen;
168 gfx::AcceleratedWidget window;
169 const gfx::Size& size;
170 const std::vector<int32>& attribs;
171 gfx::GpuPreference gpu_preference;
172 gpu::Capabilities* capabilities; // Ouptut.
173 InProcessCommandBuffer* context_group;
174 ImageFactory* image_factory;
176 InitializeOnGpuThreadParams(bool is_offscreen,
177 gfx::AcceleratedWidget window,
178 const gfx::Size& size,
179 const std::vector<int32>& attribs,
180 gfx::GpuPreference gpu_preference,
181 gpu::Capabilities* capabilities,
182 InProcessCommandBuffer* share_group,
183 ImageFactory* image_factory)
184 : is_offscreen(is_offscreen),
185 window(window),
186 size(size),
187 attribs(attribs),
188 gpu_preference(gpu_preference),
189 capabilities(capabilities),
190 context_group(share_group),
191 image_factory(image_factory) {}
194 bool InitializeOnGpuThread(const InitializeOnGpuThreadParams& params);
195 bool DestroyOnGpuThread();
196 void FlushOnGpuThread(int32 put_offset);
197 void ScheduleIdleWorkOnGpuThread();
198 uint32 CreateStreamTextureOnGpuThread(uint32 client_texture_id);
199 bool MakeCurrent();
200 base::Closure WrapCallback(const base::Closure& callback);
201 State GetStateFast();
202 void QueueTask(const base::Closure& task) { service_->ScheduleTask(task); }
203 void CheckSequencedThread();
204 void RetireSyncPointOnGpuThread(uint32 sync_point);
205 void SignalSyncPointOnGpuThread(uint32 sync_point,
206 const base::Closure& callback);
207 bool WaitSyncPointOnGpuThread(uint32 sync_point);
208 void SignalQueryOnGpuThread(unsigned query_id, const base::Closure& callback);
209 void DestroyTransferBufferOnGpuThread(int32 id);
210 void CreateImageOnGpuThread(int32 id,
211 const gfx::GpuMemoryBufferHandle& handle,
212 const gfx::Size& size,
213 gfx::BufferFormat format,
214 uint32 internalformat);
215 void DestroyImageOnGpuThread(int32 id);
216 void SetGetBufferOnGpuThread(int32 shm_id, base::WaitableEvent* completion);
218 // Callbacks:
219 void OnContextLost();
220 void OnResizeView(gfx::Size size, float scale_factor);
221 bool GetBufferChanged(int32 transfer_buffer_id);
222 void PumpCommands();
223 void PerformIdleWork();
225 // Members accessed on the gpu thread (possibly with the exception of
226 // creation):
227 bool context_lost_;
228 scoped_refptr<TransferBufferManagerInterface> transfer_buffer_manager_;
229 scoped_ptr<GpuScheduler> gpu_scheduler_;
230 scoped_ptr<gles2::GLES2Decoder> decoder_;
231 scoped_refptr<gfx::GLContext> context_;
232 scoped_refptr<gfx::GLSurface> surface_;
233 base::Closure context_lost_callback_;
234 bool idle_work_pending_; // Used to throttle PerformIdleWork.
235 ImageFactory* image_factory_;
237 // Members accessed on the client thread:
238 State last_state_;
239 int32 last_put_offset_;
240 gpu::Capabilities capabilities_;
241 GpuMemoryBufferManager* gpu_memory_buffer_manager_;
242 base::AtomicSequenceNumber next_image_id_;
244 // Accessed on both threads:
245 scoped_ptr<CommandBufferServiceBase> command_buffer_;
246 base::Lock command_buffer_lock_;
247 base::WaitableEvent flush_event_;
248 scoped_refptr<Service> service_;
249 State state_after_last_flush_;
250 base::Lock state_after_last_flush_lock_;
251 scoped_refptr<gfx::GLShareGroup> gl_share_group_;
253 #if defined(OS_ANDROID)
254 scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
255 #endif
257 // Only used with explicit scheduling and the gpu thread is the same as
258 // the client thread.
259 scoped_ptr<base::SequenceChecker> sequence_checker_;
261 base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
262 base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
264 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
267 // Default Service class when a null service is used.
268 class GPU_EXPORT GpuInProcessThread
269 : public base::Thread,
270 public NON_EXPORTED_BASE(InProcessCommandBuffer::Service),
271 public base::RefCountedThreadSafe<GpuInProcessThread> {
272 public:
273 explicit GpuInProcessThread(SyncPointManager* sync_point_manager);
275 void AddRef() const override;
276 void Release() const override;
277 void ScheduleTask(const base::Closure& task) override;
278 void ScheduleIdleWork(const base::Closure& callback) override;
279 bool UseVirtualizedGLContexts() override;
280 scoped_refptr<gles2::ShaderTranslatorCache> shader_translator_cache()
281 override;
282 SyncPointManager* sync_point_manager() override;
284 private:
285 ~GpuInProcessThread() override;
286 friend class base::RefCountedThreadSafe<GpuInProcessThread>;
288 SyncPointManager* sync_point_manager_; // Non-owning.
289 scoped_refptr<gpu::gles2::ShaderTranslatorCache> shader_translator_cache_;
290 DISALLOW_COPY_AND_ASSIGN(GpuInProcessThread);
293 } // namespace gpu
295 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_