Performance histograms for extension content verification
[chromium-blink-merge.git] / gpu / command_buffer / service / in_process_command_buffer.h
blobf8986d81ab09e765d2c747024dc4831d05f9b3eb
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_
8 #include <map>
9 #include <vector>
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"
27 namespace base {
28 class SequenceChecker;
31 namespace gfx {
32 class GLContext;
33 class GLShareGroup;
34 class GLSurface;
35 class Size;
38 #if defined(OS_ANDROID)
39 namespace gfx {
40 class SurfaceTexture;
42 namespace gpu {
43 class StreamTextureManagerInProcess;
45 #endif
47 namespace gpu {
49 namespace gles2 {
50 class GLES2Decoder;
51 class ShaderTranslatorCache;
54 class CommandBufferServiceBase;
55 class GpuControlService;
56 class GpuMemoryBufferFactory;
57 class GpuScheduler;
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,
65 public GpuControl {
66 public:
67 class Service;
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
75 // a new GLSurface.
76 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
77 bool is_offscreen,
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);
84 void Destroy();
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,
95 int32* id) OVERRIDE;
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,
102 size_t height,
103 unsigned internalformat,
104 unsigned usage,
105 int32* id) OVERRIDE;
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).
117 class Service {
118 public:
119 Service();
120 virtual ~Service();
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
129 // work.
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(
139 uint32 stream_id);
140 #endif
142 private:
143 struct InitializeOnGpuThreadParams {
144 bool is_offscreen;
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),
160 window(window),
161 size(size),
162 attribs(attribs),
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);
172 bool MakeCurrent();
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);
182 // Callbacks:
183 void OnContextLost();
184 void OnResizeView(gfx::Size size, float scale_factor);
185 bool GetBufferChanged(int32 transfer_buffer_id);
186 void PumpCommands();
187 void ScheduleMoreIdleWork();
189 static scoped_refptr<Service> GetDefaultService();
191 // Members accessed on the gpu thread (possibly with the exception of
192 // creation):
193 bool context_lost_;
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:
202 State last_state_;
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_;
220 #endif
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);
232 } // namespace gpu
234 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_