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/memory/linked_ptr.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
;
55 class GpuControlService
;
56 class GpuMemoryBufferFactory
;
58 class TransferBufferManagerInterface
;
60 // This class provides a thread-safe interface to the global GPU service (for
61 // example GPU thread) when being run in single process mode.
62 // However, the behavior for accessing one context (i.e. one instance of this
63 // class) from different client threads is undefined.
64 class GPU_EXPORT InProcessCommandBuffer
: public CommandBuffer
,
68 explicit InProcessCommandBuffer(const scoped_refptr
<Service
>& service
);
69 virtual ~InProcessCommandBuffer();
71 static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory
* factory
);
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
);
86 // CommandBuffer implementation:
87 virtual bool Initialize() OVERRIDE
;
88 virtual State
GetLastState() OVERRIDE
;
89 virtual int32
GetLastToken() OVERRIDE
;
90 virtual void Flush(int32 put_offset
) OVERRIDE
;
91 virtual void WaitForTokenInRange(int32 start
, int32 end
) OVERRIDE
;
92 virtual void WaitForGetOffsetInRange(int32 start
, int32 end
) OVERRIDE
;
93 virtual void SetGetBuffer(int32 shm_id
) OVERRIDE
;
94 virtual scoped_refptr
<gpu::Buffer
> CreateTransferBuffer(size_t size
,
96 virtual void DestroyTransferBuffer(int32 id
) OVERRIDE
;
97 virtual gpu::error::Error
GetLastError() OVERRIDE
;
99 // GpuControl implementation:
100 virtual gpu::Capabilities
GetCapabilities() OVERRIDE
;
101 virtual gfx::GpuMemoryBuffer
* CreateGpuMemoryBuffer(size_t width
,
103 unsigned internalformat
,
106 virtual void DestroyGpuMemoryBuffer(int32 id
) OVERRIDE
;
107 virtual uint32
InsertSyncPoint() OVERRIDE
;
108 virtual void SignalSyncPoint(uint32 sync_point
,
109 const base::Closure
& callback
) OVERRIDE
;
110 virtual void SignalQuery(uint32 query
,
111 const base::Closure
& callback
) OVERRIDE
;
112 virtual void SetSurfaceVisible(bool visible
) OVERRIDE
;
113 virtual void Echo(const base::Closure
& callback
) OVERRIDE
;
114 virtual uint32
CreateStreamTexture(uint32 texture_id
) OVERRIDE
;
116 // The serializer interface to the GPU service (i.e. thread).
122 virtual void AddRef() const = 0;
123 virtual void Release() const = 0;
125 // Queues a task to run as soon as possible.
126 virtual void ScheduleTask(const base::Closure
& task
) = 0;
128 // Schedules |callback| to run at an appropriate time for performing idle
130 virtual void ScheduleIdleWork(const base::Closure
& task
) = 0;
132 virtual bool UseVirtualizedGLContexts() = 0;
133 virtual scoped_refptr
<gles2::ShaderTranslatorCache
>
134 shader_translator_cache() = 0;
137 #if defined(OS_ANDROID)
138 scoped_refptr
<gfx::SurfaceTexture
> GetSurfaceTexture(
143 struct InitializeOnGpuThreadParams
{
145 gfx::AcceleratedWidget window
;
146 const gfx::Size
& size
;
147 const std::vector
<int32
>& attribs
;
148 gfx::GpuPreference gpu_preference
;
149 gpu::Capabilities
* capabilities
; // Ouptut.
150 InProcessCommandBuffer
* context_group
;
152 InitializeOnGpuThreadParams(bool is_offscreen
,
153 gfx::AcceleratedWidget window
,
154 const gfx::Size
& size
,
155 const std::vector
<int32
>& attribs
,
156 gfx::GpuPreference gpu_preference
,
157 gpu::Capabilities
* capabilities
,
158 InProcessCommandBuffer
* share_group
)
159 : is_offscreen(is_offscreen
),
163 gpu_preference(gpu_preference
),
164 capabilities(capabilities
),
165 context_group(share_group
) {}
168 bool InitializeOnGpuThread(const InitializeOnGpuThreadParams
& params
);
169 bool DestroyOnGpuThread();
170 void FlushOnGpuThread(int32 put_offset
);
171 uint32
CreateStreamTextureOnGpuThread(uint32 client_texture_id
);
173 base::Closure
WrapCallback(const base::Closure
& callback
);
174 State
GetStateFast();
175 void QueueTask(const base::Closure
& task
) { service_
->ScheduleTask(task
); }
176 void CheckSequencedThread();
177 void RetireSyncPointOnGpuThread(uint32 sync_point
);
178 void SignalSyncPointOnGpuThread(uint32 sync_point
,
179 const base::Closure
& callback
);
180 void DestroyTransferBufferOnGputhread(int32 id
);
183 void OnContextLost();
184 void OnResizeView(gfx::Size size
, float scale_factor
);
185 bool GetBufferChanged(int32 transfer_buffer_id
);
187 void ScheduleMoreIdleWork();
189 static scoped_refptr
<Service
> GetDefaultService();
191 // Members accessed on the gpu thread (possibly with the exception of
194 scoped_ptr
<TransferBufferManagerInterface
> transfer_buffer_manager_
;
195 scoped_ptr
<GpuScheduler
> gpu_scheduler_
;
196 scoped_ptr
<gles2::GLES2Decoder
> decoder_
;
197 scoped_refptr
<gfx::GLContext
> context_
;
198 scoped_refptr
<gfx::GLSurface
> surface_
;
199 base::Closure context_lost_callback_
;
201 // Members accessed on the client thread:
203 int32 last_put_offset_
;
204 gpu::Capabilities capabilities_
;
205 typedef std::map
<int32
, linked_ptr
<gfx::GpuMemoryBuffer
> > GpuMemoryBufferMap
;
206 GpuMemoryBufferMap gpu_memory_buffers_
;
208 // Accessed on both threads:
209 scoped_ptr
<CommandBufferServiceBase
> command_buffer_
;
210 base::Lock command_buffer_lock_
;
211 base::WaitableEvent flush_event_
;
212 scoped_refptr
<Service
> service_
;
213 State state_after_last_flush_
;
214 base::Lock state_after_last_flush_lock_
;
215 scoped_ptr
<GpuControlService
> gpu_control_
;
216 scoped_refptr
<gfx::GLShareGroup
> gl_share_group_
;
218 #if defined(OS_ANDROID)
219 scoped_ptr
<StreamTextureManagerInProcess
> stream_texture_manager_
;
222 // Only used with explicit scheduling and the gpu thread is the same as
223 // the client thread.
224 scoped_ptr
<base::SequenceChecker
> sequence_checker_
;
226 base::WeakPtr
<InProcessCommandBuffer
> gpu_thread_weak_ptr_
;
227 base::WeakPtrFactory
<InProcessCommandBuffer
> gpu_thread_weak_ptr_factory_
;
229 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer
);
234 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_