Handle account removal correctly on all platforms.
[chromium-blink-merge.git] / gpu / command_buffer / service / in_process_command_buffer.h
blob8b34d4b91394a41578836e93bc3e7bbf79bcb9ab
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/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"
27 namespace base {
28 class SequenceChecker;
31 namespace gfx {
32 class GLContext;
33 class GLShareGroup;
34 class GLSurface;
35 class Size;
38 #if defined(OS_ANDROID)
39 namespace gfx {
40 class SurfaceTexture;
42 namespace gpu {
43 class StreamTextureManagerInProcess;
45 #endif
47 namespace gpu {
49 namespace gles2 {
50 class GLES2Decoder;
51 class MailboxManager;
52 class ShaderTranslatorCache;
55 class CommandBufferServiceBase;
56 class GpuScheduler;
57 class TransferBufferManagerInterface;
59 // TODO(reveman): Remove this interface when InProcessCommandBuffer doesn't need
60 // a custom factory interface and android_webview implementation of GPU memory
61 // buffers can use the same mechanism for buffer allocation as what's used for
62 // out of process GPU service.
63 class GPU_EXPORT InProcessGpuMemoryBufferFactory {
64 public:
65 virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
66 size_t width,
67 size_t height,
68 unsigned internalformat,
69 unsigned usage) = 0;
70 virtual scoped_refptr<gfx::GLImage> CreateImageForGpuMemoryBuffer(
71 const gfx::GpuMemoryBufferHandle& handle,
72 const gfx::Size& size,
73 unsigned internalformat) = 0;
75 protected:
76 virtual ~InProcessGpuMemoryBufferFactory() {}
79 // This class provides a thread-safe interface to the global GPU service (for
80 // example GPU thread) when being run in single process mode.
81 // However, the behavior for accessing one context (i.e. one instance of this
82 // class) from different client threads is undefined.
83 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
84 public GpuControl {
85 public:
86 class Service;
87 explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
88 virtual ~InProcessCommandBuffer();
90 static void SetGpuMemoryBufferFactory(
91 InProcessGpuMemoryBufferFactory* factory);
93 // If |surface| is not NULL, use it directly; in this case, the command
94 // buffer gpu thread must be the same as the client thread. Otherwise create
95 // a new GLSurface.
96 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
97 bool is_offscreen,
98 gfx::AcceleratedWidget window,
99 const gfx::Size& size,
100 const std::vector<int32>& attribs,
101 gfx::GpuPreference gpu_preference,
102 const base::Closure& context_lost_callback,
103 InProcessCommandBuffer* share_group);
104 void Destroy();
106 // CommandBuffer implementation:
107 virtual bool Initialize() OVERRIDE;
108 virtual State GetLastState() OVERRIDE;
109 virtual int32 GetLastToken() OVERRIDE;
110 virtual void Flush(int32 put_offset) OVERRIDE;
111 virtual void WaitForTokenInRange(int32 start, int32 end) OVERRIDE;
112 virtual void WaitForGetOffsetInRange(int32 start, int32 end) OVERRIDE;
113 virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
114 virtual scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
115 int32* id) OVERRIDE;
116 virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
117 virtual gpu::error::Error GetLastError() OVERRIDE;
119 // GpuControl implementation:
120 virtual gpu::Capabilities GetCapabilities() OVERRIDE;
121 virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(size_t width,
122 size_t height,
123 unsigned internalformat,
124 unsigned usage,
125 int32* id) OVERRIDE;
126 virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
127 virtual uint32 InsertSyncPoint() OVERRIDE;
128 virtual uint32 InsertFutureSyncPoint() OVERRIDE;
129 virtual void RetireSyncPoint(uint32 sync_point) OVERRIDE;
130 virtual void SignalSyncPoint(uint32 sync_point,
131 const base::Closure& callback) OVERRIDE;
132 virtual void SignalQuery(uint32 query_id,
133 const base::Closure& callback) OVERRIDE;
134 virtual void SetSurfaceVisible(bool visible) OVERRIDE;
135 virtual void Echo(const base::Closure& callback) OVERRIDE;
136 virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
138 // The serializer interface to the GPU service (i.e. thread).
139 class Service {
140 public:
141 Service();
142 virtual ~Service();
144 virtual void AddRef() const = 0;
145 virtual void Release() const = 0;
147 // Queues a task to run as soon as possible.
148 virtual void ScheduleTask(const base::Closure& task) = 0;
150 // Schedules |callback| to run at an appropriate time for performing idle
151 // work.
152 virtual void ScheduleIdleWork(const base::Closure& task) = 0;
154 virtual bool UseVirtualizedGLContexts() = 0;
155 virtual scoped_refptr<gles2::ShaderTranslatorCache>
156 shader_translator_cache() = 0;
157 scoped_refptr<gles2::MailboxManager> mailbox_manager();
159 private:
160 scoped_refptr<gles2::MailboxManager> mailbox_manager_;
163 #if defined(OS_ANDROID)
164 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
165 uint32 stream_id);
166 #endif
168 private:
169 struct InitializeOnGpuThreadParams {
170 bool is_offscreen;
171 gfx::AcceleratedWidget window;
172 const gfx::Size& size;
173 const std::vector<int32>& attribs;
174 gfx::GpuPreference gpu_preference;
175 gpu::Capabilities* capabilities; // Ouptut.
176 InProcessCommandBuffer* context_group;
178 InitializeOnGpuThreadParams(bool is_offscreen,
179 gfx::AcceleratedWidget window,
180 const gfx::Size& size,
181 const std::vector<int32>& attribs,
182 gfx::GpuPreference gpu_preference,
183 gpu::Capabilities* capabilities,
184 InProcessCommandBuffer* share_group)
185 : is_offscreen(is_offscreen),
186 window(window),
187 size(size),
188 attribs(attribs),
189 gpu_preference(gpu_preference),
190 capabilities(capabilities),
191 context_group(share_group) {}
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 void SignalQueryOnGpuThread(unsigned query_id, const base::Closure& callback);
208 void DestroyTransferBufferOnGpuThread(int32 id);
209 void RegisterGpuMemoryBufferOnGpuThread(
210 int32 id,
211 const gfx::GpuMemoryBufferHandle& handle,
212 size_t width,
213 size_t height,
214 unsigned internalformat);
215 void UnregisterGpuMemoryBufferOnGpuThread(int32 id);
217 // Callbacks:
218 void OnContextLost();
219 void OnResizeView(gfx::Size size, float scale_factor);
220 bool GetBufferChanged(int32 transfer_buffer_id);
221 void PumpCommands();
222 void PerformIdleWork();
224 static scoped_refptr<Service> GetDefaultService();
226 // Members accessed on the gpu thread (possibly with the exception of
227 // creation):
228 bool context_lost_;
229 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
230 scoped_ptr<GpuScheduler> gpu_scheduler_;
231 scoped_ptr<gles2::GLES2Decoder> decoder_;
232 scoped_refptr<gfx::GLContext> context_;
233 scoped_refptr<gfx::GLSurface> surface_;
234 base::Closure context_lost_callback_;
235 bool idle_work_pending_; // Used to throttle PerformIdleWork.
237 // Members accessed on the client thread:
238 State last_state_;
239 int32 last_put_offset_;
240 gpu::Capabilities capabilities_;
241 typedef base::ScopedPtrHashMap<int32, gfx::GpuMemoryBuffer>
242 GpuMemoryBufferMap;
243 GpuMemoryBufferMap gpu_memory_buffers_;
245 // Accessed on both threads:
246 scoped_ptr<CommandBufferServiceBase> command_buffer_;
247 base::Lock command_buffer_lock_;
248 base::WaitableEvent flush_event_;
249 scoped_refptr<Service> service_;
250 State state_after_last_flush_;
251 base::Lock state_after_last_flush_lock_;
252 scoped_refptr<gfx::GLShareGroup> gl_share_group_;
254 #if defined(OS_ANDROID)
255 scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
256 #endif
258 // Only used with explicit scheduling and the gpu thread is the same as
259 // the client thread.
260 scoped_ptr<base::SequenceChecker> sequence_checker_;
262 base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
263 base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
265 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
268 } // namespace gpu
270 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_