• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
7 
8 #include <map>
9 #include <vector>
10 
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"
26 
27 namespace base {
28 class SequenceChecker;
29 }
30 
31 namespace gfx {
32 class GLContext;
33 class GLShareGroup;
34 class GLSurface;
35 class Size;
36 }
37 
38 #if defined(OS_ANDROID)
39 namespace gfx {
40 class SurfaceTexture;
41 }
42 namespace gpu {
43 class StreamTextureManagerInProcess;
44 }
45 #endif
46 
47 namespace gpu {
48 
49 namespace gles2 {
50 class GLES2Decoder;
51 class ShaderTranslatorCache;
52 }
53 
54 class CommandBufferServiceBase;
55 class GpuControlService;
56 class GpuMemoryBufferFactory;
57 class GpuScheduler;
58 class TransferBufferManagerInterface;
59 
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();
70 
71   static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory* factory);
72 
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();
85 
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;
98 
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;
115 
116   // The serializer interface to the GPU service (i.e. thread).
117   class Service {
118    public:
119     Service();
120     virtual ~Service();
121 
122     virtual void AddRef() const = 0;
123     virtual void Release() const = 0;
124 
125     // Queues a task to run as soon as possible.
126     virtual void ScheduleTask(const base::Closure& task) = 0;
127 
128     // Schedules |callback| to run at an appropriate time for performing idle
129     // work.
130     virtual void ScheduleIdleWork(const base::Closure& task) = 0;
131 
132     virtual bool UseVirtualizedGLContexts() = 0;
133     virtual scoped_refptr<gles2::ShaderTranslatorCache>
134         shader_translator_cache() = 0;
135   };
136 
137 #if defined(OS_ANDROID)
138   scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
139       uint32 stream_id);
140 #endif
141 
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;
151 
InitializeOnGpuThreadParamsInitializeOnGpuThreadParams152     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) {}
166   };
167 
168   bool InitializeOnGpuThread(const InitializeOnGpuThreadParams& params);
169   bool DestroyOnGpuThread();
170   void FlushOnGpuThread(int32 put_offset);
171   void ScheduleIdleWorkOnGpuThread();
172   uint32 CreateStreamTextureOnGpuThread(uint32 client_texture_id);
173   bool MakeCurrent();
174   base::Closure WrapCallback(const base::Closure& callback);
175   State GetStateFast();
QueueTask(const base::Closure & task)176   void QueueTask(const base::Closure& task) { service_->ScheduleTask(task); }
177   void CheckSequencedThread();
178   void RetireSyncPointOnGpuThread(uint32 sync_point);
179   void SignalSyncPointOnGpuThread(uint32 sync_point,
180                                   const base::Closure& callback);
181   void DestroyTransferBufferOnGputhread(int32 id);
182 
183   // Callbacks:
184   void OnContextLost();
185   void OnResizeView(gfx::Size size, float scale_factor);
186   bool GetBufferChanged(int32 transfer_buffer_id);
187   void PumpCommands();
188   void PerformIdleWork();
189 
190   static scoped_refptr<Service> GetDefaultService();
191 
192   // Members accessed on the gpu thread (possibly with the exception of
193   // creation):
194   bool context_lost_;
195   scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
196   scoped_ptr<GpuScheduler> gpu_scheduler_;
197   scoped_ptr<gles2::GLES2Decoder> decoder_;
198   scoped_refptr<gfx::GLContext> context_;
199   scoped_refptr<gfx::GLSurface> surface_;
200   base::Closure context_lost_callback_;
201   bool idle_work_pending_;  // Used to throttle PerformIdleWork.
202 
203   // Members accessed on the client thread:
204   State last_state_;
205   int32 last_put_offset_;
206   gpu::Capabilities capabilities_;
207   typedef std::map<int32, linked_ptr<gfx::GpuMemoryBuffer> > GpuMemoryBufferMap;
208   GpuMemoryBufferMap gpu_memory_buffers_;
209 
210   // Accessed on both threads:
211   scoped_ptr<CommandBufferServiceBase> command_buffer_;
212   base::Lock command_buffer_lock_;
213   base::WaitableEvent flush_event_;
214   scoped_refptr<Service> service_;
215   State state_after_last_flush_;
216   base::Lock state_after_last_flush_lock_;
217   scoped_ptr<GpuControlService> gpu_control_;
218   scoped_refptr<gfx::GLShareGroup> gl_share_group_;
219 
220 #if defined(OS_ANDROID)
221   scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
222 #endif
223 
224   // Only used with explicit scheduling and the gpu thread is the same as
225   // the client thread.
226   scoped_ptr<base::SequenceChecker> sequence_checker_;
227 
228   base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
229   base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
230 
231   DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
232 };
233 
234 }  // namespace gpu
235 
236 #endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
237