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
;
56 class ShaderTranslatorCache
;
57 class SubscriptionRefSet
;
60 class CommandBufferServiceBase
;
61 class GpuMemoryBufferManager
;
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
,
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
80 bool Initialize(scoped_refptr
<gfx::GLSurface
> surface
,
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
);
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
,
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
,
111 unsigned internalformat
) override
;
112 void DestroyImage(int32 id
) override
;
113 int32
CreateGpuMemoryBufferImage(size_t width
,
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).
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
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();
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(
166 struct InitializeOnGpuThreadParams
{
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
),
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
);
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
);
219 void OnContextLost();
220 void OnResizeView(gfx::Size size
, float scale_factor
);
221 bool GetBufferChanged(int32 transfer_buffer_id
);
223 void PerformIdleWork();
225 // Members accessed on the gpu thread (possibly with the exception of
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:
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_
;
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
> {
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()
282 SyncPointManager
* sync_point_manager() override
;
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
);
295 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_