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
;
54 class ShaderTranslatorCache
;
55 class SubscriptionRefSet
;
58 class CommandBufferServiceBase
;
59 class GpuMemoryBufferManager
;
62 class TransferBufferManagerInterface
;
64 // This class provides a thread-safe interface to the global GPU service (for
65 // example GPU thread) when being run in single process mode.
66 // However, the behavior for accessing one context (i.e. one instance of this
67 // class) from different client threads is undefined.
68 class GPU_EXPORT InProcessCommandBuffer
: public CommandBuffer
,
72 explicit InProcessCommandBuffer(const scoped_refptr
<Service
>& service
);
73 ~InProcessCommandBuffer() override
;
75 // If |surface| is not NULL, use it directly; in this case, the command
76 // buffer gpu thread must be the same as the client thread. Otherwise create
78 bool Initialize(scoped_refptr
<gfx::GLSurface
> surface
,
80 gfx::AcceleratedWidget window
,
81 const gfx::Size
& size
,
82 const std::vector
<int32
>& attribs
,
83 gfx::GpuPreference gpu_preference
,
84 const base::Closure
& context_lost_callback
,
85 InProcessCommandBuffer
* share_group
,
86 GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
87 ImageFactory
* image_factory
);
90 // CommandBuffer implementation:
91 bool Initialize() override
;
92 State
GetLastState() override
;
93 int32
GetLastToken() override
;
94 void Flush(int32 put_offset
) override
;
95 void OrderingBarrier(int32 put_offset
) override
;
96 void WaitForTokenInRange(int32 start
, int32 end
) override
;
97 void WaitForGetOffsetInRange(int32 start
, int32 end
) override
;
98 void SetGetBuffer(int32 shm_id
) override
;
99 scoped_refptr
<gpu::Buffer
> CreateTransferBuffer(size_t size
,
101 void DestroyTransferBuffer(int32 id
) override
;
102 gpu::error::Error
GetLastError() override
;
104 // GpuControl implementation:
105 gpu::Capabilities
GetCapabilities() override
;
106 int32
CreateImage(ClientBuffer buffer
,
109 unsigned internalformat
) override
;
110 void DestroyImage(int32 id
) override
;
111 int32
CreateGpuMemoryBufferImage(size_t width
,
113 unsigned internalformat
,
114 unsigned usage
) override
;
115 uint32
InsertSyncPoint() override
;
116 uint32
InsertFutureSyncPoint() override
;
117 void RetireSyncPoint(uint32 sync_point
) override
;
118 void SignalSyncPoint(uint32 sync_point
,
119 const base::Closure
& callback
) override
;
120 void SignalQuery(uint32 query_id
, const base::Closure
& callback
) override
;
121 void SetSurfaceVisible(bool visible
) override
;
122 uint32
CreateStreamTexture(uint32 texture_id
) override
;
123 void SetLock(base::Lock
*) override
;
125 // The serializer interface to the GPU service (i.e. thread).
131 virtual void AddRef() const = 0;
132 virtual void Release() const = 0;
134 // Queues a task to run as soon as possible.
135 virtual void ScheduleTask(const base::Closure
& task
) = 0;
137 // Schedules |callback| to run at an appropriate time for performing idle
139 virtual void ScheduleIdleWork(const base::Closure
& task
) = 0;
141 virtual bool UseVirtualizedGLContexts() = 0;
142 virtual scoped_refptr
<gles2::ShaderTranslatorCache
>
143 shader_translator_cache() = 0;
144 scoped_refptr
<gfx::GLShareGroup
> share_group();
145 scoped_refptr
<gles2::MailboxManager
> mailbox_manager();
146 scoped_refptr
<gles2::SubscriptionRefSet
> subscription_ref_set();
147 scoped_refptr
<gpu::ValueStateMap
> pending_valuebuffer_state();
150 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
151 scoped_refptr
<gles2::MailboxManager
> mailbox_manager_
;
152 scoped_refptr
<gles2::SubscriptionRefSet
> subscription_ref_set_
;
153 scoped_refptr
<gpu::ValueStateMap
> pending_valuebuffer_state_
;
156 #if defined(OS_ANDROID)
157 scoped_refptr
<gfx::SurfaceTexture
> GetSurfaceTexture(
162 struct InitializeOnGpuThreadParams
{
164 gfx::AcceleratedWidget window
;
165 const gfx::Size
& size
;
166 const std::vector
<int32
>& attribs
;
167 gfx::GpuPreference gpu_preference
;
168 gpu::Capabilities
* capabilities
; // Ouptut.
169 InProcessCommandBuffer
* context_group
;
170 ImageFactory
* image_factory
;
172 InitializeOnGpuThreadParams(bool is_offscreen
,
173 gfx::AcceleratedWidget window
,
174 const gfx::Size
& size
,
175 const std::vector
<int32
>& attribs
,
176 gfx::GpuPreference gpu_preference
,
177 gpu::Capabilities
* capabilities
,
178 InProcessCommandBuffer
* share_group
,
179 ImageFactory
* image_factory
)
180 : is_offscreen(is_offscreen
),
184 gpu_preference(gpu_preference
),
185 capabilities(capabilities
),
186 context_group(share_group
),
187 image_factory(image_factory
) {}
190 bool InitializeOnGpuThread(const InitializeOnGpuThreadParams
& params
);
191 bool DestroyOnGpuThread();
192 void FlushOnGpuThread(int32 put_offset
);
193 void ScheduleIdleWorkOnGpuThread();
194 uint32
CreateStreamTextureOnGpuThread(uint32 client_texture_id
);
196 base::Closure
WrapCallback(const base::Closure
& callback
);
197 State
GetStateFast();
198 void QueueTask(const base::Closure
& task
) { service_
->ScheduleTask(task
); }
199 void CheckSequencedThread();
200 void RetireSyncPointOnGpuThread(uint32 sync_point
);
201 void SignalSyncPointOnGpuThread(uint32 sync_point
,
202 const base::Closure
& callback
);
203 bool WaitSyncPointOnGpuThread(uint32 sync_point
);
204 void SignalQueryOnGpuThread(unsigned query_id
, const base::Closure
& callback
);
205 void DestroyTransferBufferOnGpuThread(int32 id
);
206 void CreateImageOnGpuThread(int32 id
,
207 const gfx::GpuMemoryBufferHandle
& handle
,
208 const gfx::Size
& size
,
209 gfx::GpuMemoryBuffer::Format format
,
210 uint32 internalformat
);
211 void DestroyImageOnGpuThread(int32 id
);
214 void OnContextLost();
215 void OnResizeView(gfx::Size size
, float scale_factor
);
216 bool GetBufferChanged(int32 transfer_buffer_id
);
218 void PerformIdleWork();
220 // Members accessed on the gpu thread (possibly with the exception of
223 scoped_ptr
<TransferBufferManagerInterface
> transfer_buffer_manager_
;
224 scoped_ptr
<GpuScheduler
> gpu_scheduler_
;
225 scoped_ptr
<gles2::GLES2Decoder
> decoder_
;
226 scoped_refptr
<gfx::GLContext
> context_
;
227 scoped_refptr
<gfx::GLSurface
> surface_
;
228 base::Closure context_lost_callback_
;
229 bool idle_work_pending_
; // Used to throttle PerformIdleWork.
230 ImageFactory
* image_factory_
;
232 // Members accessed on the client thread:
234 int32 last_put_offset_
;
235 gpu::Capabilities capabilities_
;
236 GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
237 base::AtomicSequenceNumber next_image_id_
;
239 // Accessed on both threads:
240 scoped_ptr
<CommandBufferServiceBase
> command_buffer_
;
241 base::Lock command_buffer_lock_
;
242 base::WaitableEvent flush_event_
;
243 scoped_refptr
<Service
> service_
;
244 State state_after_last_flush_
;
245 base::Lock state_after_last_flush_lock_
;
246 scoped_refptr
<gfx::GLShareGroup
> gl_share_group_
;
248 #if defined(OS_ANDROID)
249 scoped_ptr
<StreamTextureManagerInProcess
> stream_texture_manager_
;
252 // Only used with explicit scheduling and the gpu thread is the same as
253 // the client thread.
254 scoped_ptr
<base::SequenceChecker
> sequence_checker_
;
256 base::WeakPtr
<InProcessCommandBuffer
> gpu_thread_weak_ptr_
;
257 base::WeakPtrFactory
<InProcessCommandBuffer
> gpu_thread_weak_ptr_factory_
;
259 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer
);
264 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_