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_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "gpu/command_buffer/common/command_buffer.h"
18 #include "gpu/command_buffer/common/gpu_control.h"
19 #include "gpu/gpu_export.h"
20 #include "ui/gfx/gpu_memory_buffer.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gl/gl_surface.h"
23 #include "ui/gl/gpu_preference.h"
26 class SequenceChecker;
36 #if defined(OS_ANDROID)
41 class StreamTextureManagerInProcess;
51 class GpuMemoryBufferFactory;
53 class TransferBufferManagerInterface;
55 // This class provides a thread-safe interface to the global GPU service (for
56 // example GPU thread) when being run in single process mode.
57 // However, the behavior for accessing one context (i.e. one instance of this
58 // class) from different client threads is undefined.
59 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
63 explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
64 virtual ~InProcessCommandBuffer();
66 static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory* factory);
68 // If |surface| is not NULL, use it directly; in this case, the command
69 // buffer gpu thread must be the same as the client thread. Otherwise create
71 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
73 gfx::AcceleratedWidget window,
74 const gfx::Size& size,
75 const std::vector<int32>& attribs,
76 gfx::GpuPreference gpu_preference,
77 const base::Closure& context_lost_callback,
78 InProcessCommandBuffer* share_group);
81 // CommandBuffer implementation:
82 virtual bool Initialize() OVERRIDE;
83 virtual State GetState() OVERRIDE;
84 virtual State GetLastState() OVERRIDE;
85 virtual int32 GetLastToken() OVERRIDE;
86 virtual void Flush(int32 put_offset) OVERRIDE;
87 virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
88 virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
89 virtual void SetGetOffset(int32 get_offset) OVERRIDE;
90 virtual gpu::Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
91 virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
92 virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
93 virtual void SetToken(int32 token) OVERRIDE;
94 virtual void SetParseError(gpu::error::Error error) OVERRIDE;
95 virtual void SetContextLostReason(
96 gpu::error::ContextLostReason reason) OVERRIDE;
97 virtual gpu::error::Error GetLastError() OVERRIDE;
99 // GpuControl implementation:
100 virtual gpu::Capabilities GetCapabilities() OVERRIDE;
101 virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
104 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 SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
115 virtual void Echo(const base::Closure& callback) OVERRIDE;
116 virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
118 // The serializer interface to the GPU service (i.e. thread).
124 virtual void AddRef() const = 0;
125 virtual void Release() const = 0;
127 // Queues a task to run as soon as possible.
128 virtual void ScheduleTask(const base::Closure& task) = 0;
130 // Schedules |callback| to run at an appropriate time for performing idle
132 virtual void ScheduleIdleWork(const base::Closure& task) = 0;
134 virtual bool UseVirtualizedGLContexts() = 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();
179 void OnContextLost();
180 void OnResizeView(gfx::Size size, float scale_factor);
181 bool GetBufferChanged(int32 transfer_buffer_id);
183 void ScheduleMoreIdleWork();
185 static scoped_refptr<Service> GetDefaultService();
187 // Members accessed on the gpu thread (possibly with the exception of
190 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
191 scoped_ptr<GpuScheduler> gpu_scheduler_;
192 scoped_ptr<gles2::GLES2Decoder> decoder_;
193 scoped_refptr<gfx::GLContext> context_;
194 scoped_refptr<gfx::GLSurface> surface_;
195 base::Closure context_lost_callback_;
197 // Members accessed on the client thread:
199 int32 last_put_offset_;
200 gpu::Capabilities capabilities_;
202 // Accessed on both threads:
203 scoped_ptr<CommandBuffer> command_buffer_;
204 base::Lock command_buffer_lock_;
205 base::WaitableEvent flush_event_;
206 scoped_refptr<Service> service_;
207 State state_after_last_flush_;
208 base::Lock state_after_last_flush_lock_;
209 scoped_ptr<GpuControl> gpu_control_;
210 scoped_refptr<gfx::GLShareGroup> gl_share_group_;
212 #if defined(OS_ANDROID)
213 scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
216 // Only used with explicit scheduling and the gpu thread is the same as
217 // the client thread.
218 scoped_ptr<base::SequenceChecker> sequence_checker_;
220 base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
221 base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
223 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
228 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_