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_
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 "gpu/command_buffer/client/gpu_control.h"
21 #include "gpu/command_buffer/common/command_buffer.h"
22 #include "gpu/gpu_export.h"
23 #include "ui/gfx/gpu_memory_buffer.h"
24 #include "ui/gfx/native_widget_types.h"
25 #include "ui/gl/gl_surface.h"
26 #include "ui/gl/gpu_preference.h"
29 class SequenceChecker;
39 #if defined(OS_ANDROID)
44 class StreamTextureManagerInProcess;
53 class ShaderTranslatorCache;
56 class CommandBufferServiceBase;
57 class GpuMemoryBufferManager;
60 class TransferBufferManagerInterface;
62 // This class provides a thread-safe interface to the global GPU service (for
63 // example GPU thread) when being run in single process mode.
64 // However, the behavior for accessing one context (i.e. one instance of this
65 // class) from different client threads is undefined.
66 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
70 explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
71 ~InProcessCommandBuffer() override;
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
76 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
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 GpuMemoryBufferManager* gpu_memory_buffer_manager,
85 ImageFactory* image_factory);
88 // CommandBuffer implementation:
89 bool Initialize() override;
90 State GetLastState() override;
91 int32 GetLastToken() override;
92 void Flush(int32 put_offset) override;
93 void WaitForTokenInRange(int32 start, int32 end) override;
94 void WaitForGetOffsetInRange(int32 start, int32 end) override;
95 void SetGetBuffer(int32 shm_id) override;
96 scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
98 void DestroyTransferBuffer(int32 id) override;
99 gpu::error::Error GetLastError() override;
101 // GpuControl implementation:
102 gpu::Capabilities GetCapabilities() override;
103 int32 CreateImage(ClientBuffer buffer,
106 unsigned internalformat) override;
107 void DestroyImage(int32 id) override;
108 int32 CreateGpuMemoryBufferImage(size_t width,
110 unsigned internalformat,
111 unsigned usage) override;
112 uint32 InsertSyncPoint() override;
113 uint32 InsertFutureSyncPoint() override;
114 void RetireSyncPoint(uint32 sync_point) override;
115 void SignalSyncPoint(uint32 sync_point,
116 const base::Closure& callback) override;
117 void SignalQuery(uint32 query_id, const base::Closure& callback) override;
118 void SetSurfaceVisible(bool visible) override;
119 uint32 CreateStreamTexture(uint32 texture_id) override;
121 // The serializer interface to the GPU service (i.e. thread).
127 virtual void AddRef() const = 0;
128 virtual void Release() const = 0;
130 // Queues a task to run as soon as possible.
131 virtual void ScheduleTask(const base::Closure& task) = 0;
133 // Schedules |callback| to run at an appropriate time for performing idle
135 virtual void ScheduleIdleWork(const base::Closure& task) = 0;
137 virtual bool UseVirtualizedGLContexts() = 0;
138 virtual scoped_refptr<gles2::ShaderTranslatorCache>
139 shader_translator_cache() = 0;
140 scoped_refptr<gles2::MailboxManager> mailbox_manager();
143 scoped_refptr<gles2::MailboxManager> mailbox_manager_;
146 #if defined(OS_ANDROID)
147 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
152 struct InitializeOnGpuThreadParams {
154 gfx::AcceleratedWidget window;
155 const gfx::Size& size;
156 const std::vector<int32>& attribs;
157 gfx::GpuPreference gpu_preference;
158 gpu::Capabilities* capabilities; // Ouptut.
159 InProcessCommandBuffer* context_group;
160 ImageFactory* image_factory;
162 InitializeOnGpuThreadParams(bool is_offscreen,
163 gfx::AcceleratedWidget window,
164 const gfx::Size& size,
165 const std::vector<int32>& attribs,
166 gfx::GpuPreference gpu_preference,
167 gpu::Capabilities* capabilities,
168 InProcessCommandBuffer* share_group,
169 ImageFactory* image_factory)
170 : is_offscreen(is_offscreen),
174 gpu_preference(gpu_preference),
175 capabilities(capabilities),
176 context_group(share_group),
177 image_factory(image_factory) {}
180 bool InitializeOnGpuThread(const InitializeOnGpuThreadParams& params);
181 bool DestroyOnGpuThread();
182 void FlushOnGpuThread(int32 put_offset);
183 void ScheduleIdleWorkOnGpuThread();
184 uint32 CreateStreamTextureOnGpuThread(uint32 client_texture_id);
186 base::Closure WrapCallback(const base::Closure& callback);
187 State GetStateFast();
188 void QueueTask(const base::Closure& task) { service_->ScheduleTask(task); }
189 void CheckSequencedThread();
190 void RetireSyncPointOnGpuThread(uint32 sync_point);
191 void SignalSyncPointOnGpuThread(uint32 sync_point,
192 const base::Closure& callback);
193 bool WaitSyncPointOnGpuThread(uint32 sync_point);
194 void SignalQueryOnGpuThread(unsigned query_id, const base::Closure& callback);
195 void DestroyTransferBufferOnGpuThread(int32 id);
196 void CreateImageOnGpuThread(int32 id,
197 const gfx::GpuMemoryBufferHandle& handle,
198 const gfx::Size& size,
199 gfx::GpuMemoryBuffer::Format format,
200 uint32 internalformat);
201 void DestroyImageOnGpuThread(int32 id);
204 void OnContextLost();
205 void OnResizeView(gfx::Size size, float scale_factor);
206 bool GetBufferChanged(int32 transfer_buffer_id);
208 void PerformIdleWork();
210 static scoped_refptr<Service> GetDefaultService();
212 // Members accessed on the gpu thread (possibly with the exception of
215 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
216 scoped_ptr<GpuScheduler> gpu_scheduler_;
217 scoped_ptr<gles2::GLES2Decoder> decoder_;
218 scoped_refptr<gfx::GLContext> context_;
219 scoped_refptr<gfx::GLSurface> surface_;
220 base::Closure context_lost_callback_;
221 bool idle_work_pending_; // Used to throttle PerformIdleWork.
222 ImageFactory* image_factory_;
224 // Members accessed on the client thread:
226 int32 last_put_offset_;
227 gpu::Capabilities capabilities_;
228 GpuMemoryBufferManager* gpu_memory_buffer_manager_;
229 base::AtomicSequenceNumber next_image_id_;
231 // Accessed on both threads:
232 scoped_ptr<CommandBufferServiceBase> command_buffer_;
233 base::Lock command_buffer_lock_;
234 base::WaitableEvent flush_event_;
235 scoped_refptr<Service> service_;
236 State state_after_last_flush_;
237 base::Lock state_after_last_flush_lock_;
238 scoped_refptr<gfx::GLShareGroup> gl_share_group_;
240 #if defined(OS_ANDROID)
241 scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
244 // Only used with explicit scheduling and the gpu thread is the same as
245 // the client thread.
246 scoped_ptr<base::SequenceChecker> sequence_checker_;
248 base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
249 base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
251 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
256 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_