• 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 <vector>
9 
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"
24 
25 namespace base {
26 class SequenceChecker;
27 }
28 
29 namespace gfx {
30 class GLContext;
31 class GLShareGroup;
32 class GLSurface;
33 class Size;
34 }
35 
36 #if defined(OS_ANDROID)
37 namespace gfx {
38 class SurfaceTexture;
39 }
40 namespace gpu {
41 class StreamTextureManagerInProcess;
42 }
43 #endif
44 
45 namespace gpu {
46 
47 namespace gles2 {
48 class GLES2Decoder;
49 }
50 
51 class GpuMemoryBufferFactory;
52 class GpuScheduler;
53 class TransferBufferManagerInterface;
54 
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,
60                                           public GpuControl {
61  public:
62   class Service;
63   explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
64   virtual ~InProcessCommandBuffer();
65 
66   static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory* factory);
67 
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
70   // a new GLSurface.
71   bool Initialize(scoped_refptr<gfx::GLSurface> surface,
72                   bool is_offscreen,
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);
79   void Destroy();
80 
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;
98 
99   // GpuControl implementation:
100   virtual gpu::Capabilities GetCapabilities() OVERRIDE;
101   virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
102       size_t width,
103       size_t height,
104       unsigned internalformat,
105       int32* id) OVERRIDE;
106   virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
107   virtual bool GenerateMailboxNames(unsigned num,
108                                     std::vector<gpu::Mailbox>* names) OVERRIDE;
109   virtual uint32 InsertSyncPoint() OVERRIDE;
110   virtual void SignalSyncPoint(uint32 sync_point,
111                                const base::Closure& callback) OVERRIDE;
112   virtual void SignalQuery(uint32 query,
113                            const base::Closure& callback) OVERRIDE;
114   virtual void SetSurfaceVisible(bool visible) OVERRIDE;
115   virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
116       OVERRIDE;
117   virtual void Echo(const base::Closure& callback) OVERRIDE;
118 
119   // The serializer interface to the GPU service (i.e. thread).
120   class Service {
121    public:
122     Service();
123     virtual ~Service();
124 
125     virtual void AddRef() const = 0;
126     virtual void Release() const = 0;
127 
128     // Queues a task to run as soon as possible.
129     virtual void ScheduleTask(const base::Closure& task) = 0;
130 
131     // Schedules |callback| to run at an appropriate time for performing idle
132     // work.
133     virtual void ScheduleIdleWork(const base::Closure& task) = 0;
134 
135     virtual bool UseVirtualizedGLContexts() = 0;
136   };
137 
138 #if defined(OS_ANDROID)
139   scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
140       uint32 stream_id);
141 #endif
142 
143  private:
144   struct InitializeOnGpuThreadParams {
145     bool is_offscreen;
146     gfx::AcceleratedWidget window;
147     const gfx::Size& size;
148     const std::vector<int32>& attribs;
149     gfx::GpuPreference gpu_preference;
150     gpu::Capabilities* capabilities;  // Ouptut.
151     InProcessCommandBuffer* context_group;
152 
InitializeOnGpuThreadParamsInitializeOnGpuThreadParams153     InitializeOnGpuThreadParams(bool is_offscreen,
154                                 gfx::AcceleratedWidget window,
155                                 const gfx::Size& size,
156                                 const std::vector<int32>& attribs,
157                                 gfx::GpuPreference gpu_preference,
158                                 gpu::Capabilities* capabilities,
159                                 InProcessCommandBuffer* share_group)
160         : is_offscreen(is_offscreen),
161           window(window),
162           size(size),
163           attribs(attribs),
164           gpu_preference(gpu_preference),
165           capabilities(capabilities),
166           context_group(share_group) {}
167   };
168 
169   bool InitializeOnGpuThread(const InitializeOnGpuThreadParams& params);
170   bool DestroyOnGpuThread();
171   void FlushOnGpuThread(int32 put_offset);
172   bool MakeCurrent();
173   base::Closure WrapCallback(const base::Closure& callback);
174   State GetStateFast();
QueueTask(const base::Closure & task)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 
181   // Callbacks:
182   void OnContextLost();
183   void OnResizeView(gfx::Size size, float scale_factor);
184   bool GetBufferChanged(int32 transfer_buffer_id);
185   void PumpCommands();
186   void ScheduleMoreIdleWork();
187 
188   static scoped_refptr<Service> GetDefaultService();
189 
190   // Members accessed on the gpu thread (possibly with the exception of
191   // creation):
192   bool context_lost_;
193   scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
194   scoped_ptr<GpuScheduler> gpu_scheduler_;
195   scoped_ptr<gles2::GLES2Decoder> decoder_;
196   scoped_refptr<gfx::GLContext> context_;
197   scoped_refptr<gfx::GLSurface> surface_;
198   base::Closure context_lost_callback_;
199 
200   // Members accessed on the client thread:
201   State last_state_;
202   int32 last_put_offset_;
203   gpu::Capabilities capabilities_;
204 
205   // Accessed on both threads:
206   scoped_ptr<CommandBuffer> command_buffer_;
207   base::Lock command_buffer_lock_;
208   base::WaitableEvent flush_event_;
209   scoped_refptr<Service> service_;
210   State state_after_last_flush_;
211   base::Lock state_after_last_flush_lock_;
212   scoped_ptr<GpuControl> gpu_control_;
213   scoped_refptr<gfx::GLShareGroup> gl_share_group_;
214 
215 #if defined(OS_ANDROID)
216   scoped_refptr<StreamTextureManagerInProcess> stream_texture_manager_;
217 #endif
218 
219   // Only used with explicit scheduling and the gpu thread is the same as
220   // the client thread.
221   scoped_ptr<base::SequenceChecker> sequence_checker_;
222 
223   base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
224   base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
225 
226   DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
227 };
228 
229 }  // namespace gpu
230 
231 #endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
232