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/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"
28 class SequenceChecker
;
38 #if defined(OS_ANDROID)
43 class StreamTextureManagerInProcess
;
51 class ShaderTranslatorCache
;
54 class CommandBufferServiceBase
;
56 class TransferBufferManagerInterface
;
58 // TODO(reveman): Remove this interface when InProcessCommandBuffer doesn't need
59 // a custom factory interface and android_webview implementation of GPU memory
60 // buffers can use the same mechanism for buffer allocation as what's used for
61 // out of process GPU service.
62 class GPU_EXPORT InProcessGpuMemoryBufferFactory
{
64 virtual scoped_ptr
<gfx::GpuMemoryBuffer
> AllocateGpuMemoryBuffer(
67 unsigned internalformat
,
69 virtual scoped_refptr
<gfx::GLImage
> CreateImageForGpuMemoryBuffer(
70 const gfx::GpuMemoryBufferHandle
& handle
,
71 const gfx::Size
& size
,
72 unsigned internalformat
) = 0;
75 virtual ~InProcessGpuMemoryBufferFactory() {}
78 // This class provides a thread-safe interface to the global GPU service (for
79 // example GPU thread) when being run in single process mode.
80 // However, the behavior for accessing one context (i.e. one instance of this
81 // class) from different client threads is undefined.
82 class GPU_EXPORT InProcessCommandBuffer
: public CommandBuffer
,
86 explicit InProcessCommandBuffer(const scoped_refptr
<Service
>& service
);
87 virtual ~InProcessCommandBuffer();
89 static void SetGpuMemoryBufferFactory(
90 InProcessGpuMemoryBufferFactory
* factory
);
92 // If |surface| is not NULL, use it directly; in this case, the command
93 // buffer gpu thread must be the same as the client thread. Otherwise create
95 bool Initialize(scoped_refptr
<gfx::GLSurface
> surface
,
97 gfx::AcceleratedWidget window
,
98 const gfx::Size
& size
,
99 const std::vector
<int32
>& attribs
,
100 gfx::GpuPreference gpu_preference
,
101 const base::Closure
& context_lost_callback
,
102 InProcessCommandBuffer
* share_group
);
105 // CommandBuffer implementation:
106 virtual bool Initialize() OVERRIDE
;
107 virtual State
GetLastState() OVERRIDE
;
108 virtual int32
GetLastToken() OVERRIDE
;
109 virtual void Flush(int32 put_offset
) OVERRIDE
;
110 virtual void WaitForTokenInRange(int32 start
, int32 end
) OVERRIDE
;
111 virtual void WaitForGetOffsetInRange(int32 start
, int32 end
) OVERRIDE
;
112 virtual void SetGetBuffer(int32 shm_id
) OVERRIDE
;
113 virtual scoped_refptr
<gpu::Buffer
> CreateTransferBuffer(size_t size
,
115 virtual void DestroyTransferBuffer(int32 id
) OVERRIDE
;
116 virtual gpu::error::Error
GetLastError() OVERRIDE
;
118 // GpuControl implementation:
119 virtual gpu::Capabilities
GetCapabilities() OVERRIDE
;
120 virtual gfx::GpuMemoryBuffer
* CreateGpuMemoryBuffer(size_t width
,
122 unsigned internalformat
,
125 virtual void DestroyGpuMemoryBuffer(int32 id
) OVERRIDE
;
126 virtual uint32
InsertSyncPoint() OVERRIDE
;
127 virtual uint32
InsertFutureSyncPoint() OVERRIDE
;
128 virtual void RetireSyncPoint(uint32 sync_point
) OVERRIDE
;
129 virtual void SignalSyncPoint(uint32 sync_point
,
130 const base::Closure
& callback
) OVERRIDE
;
131 virtual void SignalQuery(uint32 query_id
,
132 const base::Closure
& callback
) OVERRIDE
;
133 virtual void SetSurfaceVisible(bool visible
) OVERRIDE
;
134 virtual void Echo(const base::Closure
& callback
) OVERRIDE
;
135 virtual uint32
CreateStreamTexture(uint32 texture_id
) OVERRIDE
;
137 // The serializer interface to the GPU service (i.e. thread).
143 virtual void AddRef() const = 0;
144 virtual void Release() const = 0;
146 // Queues a task to run as soon as possible.
147 virtual void ScheduleTask(const base::Closure
& task
) = 0;
149 // Schedules |callback| to run at an appropriate time for performing idle
151 virtual void ScheduleIdleWork(const base::Closure
& task
) = 0;
153 virtual bool UseVirtualizedGLContexts() = 0;
154 virtual scoped_refptr
<gles2::ShaderTranslatorCache
>
155 shader_translator_cache() = 0;
158 #if defined(OS_ANDROID)
159 scoped_refptr
<gfx::SurfaceTexture
> GetSurfaceTexture(
164 struct InitializeOnGpuThreadParams
{
166 gfx::AcceleratedWidget window
;
167 const gfx::Size
& size
;
168 const std::vector
<int32
>& attribs
;
169 gfx::GpuPreference gpu_preference
;
170 gpu::Capabilities
* capabilities
; // Ouptut.
171 InProcessCommandBuffer
* context_group
;
173 InitializeOnGpuThreadParams(bool is_offscreen
,
174 gfx::AcceleratedWidget window
,
175 const gfx::Size
& size
,
176 const std::vector
<int32
>& attribs
,
177 gfx::GpuPreference gpu_preference
,
178 gpu::Capabilities
* capabilities
,
179 InProcessCommandBuffer
* share_group
)
180 : is_offscreen(is_offscreen
),
184 gpu_preference(gpu_preference
),
185 capabilities(capabilities
),
186 context_group(share_group
) {}
189 bool InitializeOnGpuThread(const InitializeOnGpuThreadParams
& params
);
190 bool DestroyOnGpuThread();
191 void FlushOnGpuThread(int32 put_offset
);
192 void ScheduleIdleWorkOnGpuThread();
193 uint32
CreateStreamTextureOnGpuThread(uint32 client_texture_id
);
195 base::Closure
WrapCallback(const base::Closure
& callback
);
196 State
GetStateFast();
197 void QueueTask(const base::Closure
& task
) { service_
->ScheduleTask(task
); }
198 void CheckSequencedThread();
199 void RetireSyncPointOnGpuThread(uint32 sync_point
);
200 void SignalSyncPointOnGpuThread(uint32 sync_point
,
201 const base::Closure
& callback
);
202 void SignalQueryOnGpuThread(unsigned query_id
, const base::Closure
& callback
);
203 void DestroyTransferBufferOnGpuThread(int32 id
);
204 void RegisterGpuMemoryBufferOnGpuThread(
206 const gfx::GpuMemoryBufferHandle
& handle
,
209 unsigned internalformat
);
210 void UnregisterGpuMemoryBufferOnGpuThread(int32 id
);
213 void OnContextLost();
214 void OnResizeView(gfx::Size size
, float scale_factor
);
215 bool GetBufferChanged(int32 transfer_buffer_id
);
217 void PerformIdleWork();
219 static scoped_refptr
<Service
> GetDefaultService();
221 // Members accessed on the gpu thread (possibly with the exception of
224 scoped_ptr
<TransferBufferManagerInterface
> transfer_buffer_manager_
;
225 scoped_ptr
<GpuScheduler
> gpu_scheduler_
;
226 scoped_ptr
<gles2::GLES2Decoder
> decoder_
;
227 scoped_refptr
<gfx::GLContext
> context_
;
228 scoped_refptr
<gfx::GLSurface
> surface_
;
229 base::Closure context_lost_callback_
;
230 bool idle_work_pending_
; // Used to throttle PerformIdleWork.
232 // Members accessed on the client thread:
234 int32 last_put_offset_
;
235 gpu::Capabilities capabilities_
;
236 typedef base::ScopedPtrHashMap
<int32
, gfx::GpuMemoryBuffer
>
238 GpuMemoryBufferMap gpu_memory_buffers_
;
240 // Accessed on both threads:
241 scoped_ptr
<CommandBufferServiceBase
> command_buffer_
;
242 base::Lock command_buffer_lock_
;
243 base::WaitableEvent flush_event_
;
244 scoped_refptr
<Service
> service_
;
245 State state_after_last_flush_
;
246 base::Lock state_after_last_flush_lock_
;
247 scoped_refptr
<gfx::GLShareGroup
> gl_share_group_
;
249 #if defined(OS_ANDROID)
250 scoped_ptr
<StreamTextureManagerInProcess
> stream_texture_manager_
;
253 // Only used with explicit scheduling and the gpu thread is the same as
254 // the client thread.
255 scoped_ptr
<base::SequenceChecker
> sequence_checker_
;
257 base::WeakPtr
<InProcessCommandBuffer
> gpu_thread_weak_ptr_
;
258 base::WeakPtrFactory
<InProcessCommandBuffer
> gpu_thread_weak_ptr_factory_
;
260 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer
);
265 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_