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 "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"
30 class SequenceChecker
;
40 #if defined(OS_ANDROID)
45 class StreamTextureManagerInProcess
;
50 class SyncPointManager
;
54 class FramebufferCompletenessCache
;
58 class ShaderTranslatorCache
;
59 class SubscriptionRefSet
;
62 class CommandBufferServiceBase
;
63 class GpuMemoryBufferManager
;
66 class TransferBufferManagerInterface
;
68 // This class provides a thread-safe interface to the global GPU service (for
69 // example GPU thread) when being run in single process mode.
70 // However, the behavior for accessing one context (i.e. one instance of this
71 // class) from different client threads is undefined.
72 class GPU_EXPORT InProcessCommandBuffer
: public CommandBuffer
,
76 explicit InProcessCommandBuffer(const scoped_refptr
<Service
>& service
);
77 ~InProcessCommandBuffer() override
;
79 // If |surface| is not NULL, use it directly; in this case, the command
80 // buffer gpu thread must be the same as the client thread. Otherwise create
82 bool Initialize(scoped_refptr
<gfx::GLSurface
> surface
,
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 InProcessCommandBuffer
* share_group
,
90 GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
91 ImageFactory
* image_factory
);
94 // CommandBuffer implementation:
95 bool Initialize() override
;
96 State
GetLastState() override
;
97 int32
GetLastToken() override
;
98 void Flush(int32 put_offset
) override
;
99 void OrderingBarrier(int32 put_offset
) override
;
100 void WaitForTokenInRange(int32 start
, int32 end
) override
;
101 void WaitForGetOffsetInRange(int32 start
, int32 end
) override
;
102 void SetGetBuffer(int32 shm_id
) override
;
103 scoped_refptr
<gpu::Buffer
> CreateTransferBuffer(size_t size
,
105 void DestroyTransferBuffer(int32 id
) override
;
106 gpu::error::Error
GetLastError() override
;
108 // GpuControl implementation:
109 gpu::Capabilities
GetCapabilities() override
;
110 int32
CreateImage(ClientBuffer buffer
,
113 unsigned internalformat
) override
;
114 void DestroyImage(int32 id
) override
;
115 int32
CreateGpuMemoryBufferImage(size_t width
,
117 unsigned internalformat
,
118 unsigned usage
) override
;
119 uint32
InsertSyncPoint() override
;
120 uint32
InsertFutureSyncPoint() override
;
121 void RetireSyncPoint(uint32 sync_point
) override
;
122 void SignalSyncPoint(uint32 sync_point
,
123 const base::Closure
& callback
) override
;
124 void SignalQuery(uint32 query_id
, const base::Closure
& callback
) override
;
125 void SetSurfaceVisible(bool visible
) override
;
126 uint32
CreateStreamTexture(uint32 texture_id
) override
;
127 void SetLock(base::Lock
*) override
;
128 bool IsGpuChannelLost() override
;
129 CommandBufferNamespace
GetNamespaceID() const override
;
130 uint64_t GetCommandBufferID() const override
;
132 // The serializer interface to the GPU service (i.e. thread).
138 virtual void AddRef() const = 0;
139 virtual void Release() const = 0;
141 // Queues a task to run as soon as possible.
142 virtual void ScheduleTask(const base::Closure
& task
) = 0;
144 // Schedules |callback| to run at an appropriate time for performing delayed
146 virtual void ScheduleDelayedWork(const base::Closure
& task
) = 0;
148 virtual bool UseVirtualizedGLContexts() = 0;
149 virtual scoped_refptr
<gles2::ShaderTranslatorCache
>
150 shader_translator_cache() = 0;
151 virtual scoped_refptr
<gles2::FramebufferCompletenessCache
>
152 framebuffer_completeness_cache() = 0;
153 virtual SyncPointManager
* sync_point_manager() = 0;
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();
158 gpu::gles2::ProgramCache
* program_cache();
161 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
162 scoped_refptr
<gles2::MailboxManager
> mailbox_manager_
;
163 scoped_refptr
<gles2::SubscriptionRefSet
> subscription_ref_set_
;
164 scoped_refptr
<gpu::ValueStateMap
> pending_valuebuffer_state_
;
165 scoped_ptr
<gpu::gles2::ProgramCache
> program_cache_
;
168 #if defined(OS_ANDROID)
169 scoped_refptr
<gfx::SurfaceTexture
> GetSurfaceTexture(
174 struct InitializeOnGpuThreadParams
{
176 gfx::AcceleratedWidget window
;
177 const gfx::Size
& size
;
178 const std::vector
<int32
>& attribs
;
179 gfx::GpuPreference gpu_preference
;
180 gpu::Capabilities
* capabilities
; // Ouptut.
181 InProcessCommandBuffer
* context_group
;
182 ImageFactory
* image_factory
;
184 InitializeOnGpuThreadParams(bool is_offscreen
,
185 gfx::AcceleratedWidget window
,
186 const gfx::Size
& size
,
187 const std::vector
<int32
>& attribs
,
188 gfx::GpuPreference gpu_preference
,
189 gpu::Capabilities
* capabilities
,
190 InProcessCommandBuffer
* share_group
,
191 ImageFactory
* image_factory
)
192 : is_offscreen(is_offscreen
),
196 gpu_preference(gpu_preference
),
197 capabilities(capabilities
),
198 context_group(share_group
),
199 image_factory(image_factory
) {}
202 bool InitializeOnGpuThread(const InitializeOnGpuThreadParams
& params
);
203 bool DestroyOnGpuThread();
204 void FlushOnGpuThread(int32 put_offset
);
205 void ScheduleDelayedWorkOnGpuThread();
206 uint32
CreateStreamTextureOnGpuThread(uint32 client_texture_id
);
208 base::Closure
WrapCallback(const base::Closure
& callback
);
209 State
GetStateFast();
210 void QueueTask(const base::Closure
& task
) { service_
->ScheduleTask(task
); }
211 void CheckSequencedThread();
212 void RetireSyncPointOnGpuThread(uint32 sync_point
);
213 void SignalSyncPointOnGpuThread(uint32 sync_point
,
214 const base::Closure
& callback
);
215 bool WaitSyncPointOnGpuThread(uint32 sync_point
);
216 void SignalQueryOnGpuThread(unsigned query_id
, const base::Closure
& callback
);
217 void DestroyTransferBufferOnGpuThread(int32 id
);
218 void CreateImageOnGpuThread(int32 id
,
219 const gfx::GpuMemoryBufferHandle
& handle
,
220 const gfx::Size
& size
,
221 gfx::BufferFormat format
,
222 uint32 internalformat
);
223 void DestroyImageOnGpuThread(int32 id
);
224 void SetGetBufferOnGpuThread(int32 shm_id
, base::WaitableEvent
* completion
);
227 void OnContextLost();
228 void OnResizeView(gfx::Size size
, float scale_factor
);
229 bool GetBufferChanged(int32 transfer_buffer_id
);
231 void PerformDelayedWork();
233 const uint64_t command_buffer_id_
;
235 // Members accessed on the gpu thread (possibly with the exception of
238 scoped_refptr
<TransferBufferManagerInterface
> transfer_buffer_manager_
;
239 scoped_ptr
<GpuScheduler
> gpu_scheduler_
;
240 scoped_ptr
<gles2::GLES2Decoder
> decoder_
;
241 scoped_refptr
<gfx::GLContext
> context_
;
242 scoped_refptr
<gfx::GLSurface
> surface_
;
243 base::Closure context_lost_callback_
;
244 bool delayed_work_pending_
; // Used to throttle PerformDelayedWork.
245 ImageFactory
* image_factory_
;
247 // Members accessed on the client thread:
249 int32 last_put_offset_
;
250 gpu::Capabilities capabilities_
;
251 GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
252 base::AtomicSequenceNumber next_image_id_
;
254 // Accessed on both threads:
255 scoped_ptr
<CommandBufferServiceBase
> command_buffer_
;
256 base::Lock command_buffer_lock_
;
257 base::WaitableEvent flush_event_
;
258 scoped_refptr
<Service
> service_
;
259 State state_after_last_flush_
;
260 base::Lock state_after_last_flush_lock_
;
261 scoped_refptr
<gfx::GLShareGroup
> gl_share_group_
;
263 #if defined(OS_ANDROID)
264 scoped_ptr
<StreamTextureManagerInProcess
> stream_texture_manager_
;
267 // Only used with explicit scheduling and the gpu thread is the same as
268 // the client thread.
269 scoped_ptr
<base::SequenceChecker
> sequence_checker_
;
271 base::WeakPtr
<InProcessCommandBuffer
> gpu_thread_weak_ptr_
;
272 base::WeakPtrFactory
<InProcessCommandBuffer
> gpu_thread_weak_ptr_factory_
;
274 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer
);
277 // Default Service class when a null service is used.
278 class GPU_EXPORT GpuInProcessThread
279 : public base::Thread
,
280 public NON_EXPORTED_BASE(InProcessCommandBuffer::Service
),
281 public base::RefCountedThreadSafe
<GpuInProcessThread
> {
283 explicit GpuInProcessThread(SyncPointManager
* sync_point_manager
);
285 void AddRef() const override
;
286 void Release() const override
;
287 void ScheduleTask(const base::Closure
& task
) override
;
288 void ScheduleDelayedWork(const base::Closure
& callback
) override
;
289 bool UseVirtualizedGLContexts() override
;
290 scoped_refptr
<gles2::ShaderTranslatorCache
> shader_translator_cache()
292 scoped_refptr
<gles2::FramebufferCompletenessCache
>
293 framebuffer_completeness_cache() override
;
294 SyncPointManager
* sync_point_manager() override
;
297 ~GpuInProcessThread() override
;
298 friend class base::RefCountedThreadSafe
<GpuInProcessThread
>;
300 SyncPointManager
* sync_point_manager_
; // Non-owning.
301 scoped_refptr
<gpu::gles2::ShaderTranslatorCache
> shader_translator_cache_
;
302 scoped_refptr
<gpu::gles2::FramebufferCompletenessCache
>
303 framebuffer_completeness_cache_
;
304 DISALLOW_COPY_AND_ASSIGN(GpuInProcessThread
);
309 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_