Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / in_process_command_buffer.h
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 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)
114       OVERRIDE;
115   virtual void Echo(const base::Closure& callback) OVERRIDE;
116   virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
117
118   // The serializer interface to the GPU service (i.e. thread).
119   class Service {
120    public:
121     Service();
122     virtual ~Service();
123
124     virtual void AddRef() const = 0;
125     virtual void Release() const = 0;
126
127     // Queues a task to run as soon as possible.
128     virtual void ScheduleTask(const base::Closure& task) = 0;
129
130     // Schedules |callback| to run at an appropriate time for performing idle
131     // work.
132     virtual void ScheduleIdleWork(const base::Closure& task) = 0;
133
134     virtual bool UseVirtualizedGLContexts() = 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
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) {}
166   };
167
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
178   // Callbacks:
179   void OnContextLost();
180   void OnResizeView(gfx::Size size, float scale_factor);
181   bool GetBufferChanged(int32 transfer_buffer_id);
182   void PumpCommands();
183   void ScheduleMoreIdleWork();
184
185   static scoped_refptr<Service> GetDefaultService();
186
187   // Members accessed on the gpu thread (possibly with the exception of
188   // creation):
189   bool context_lost_;
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_;
196
197   // Members accessed on the client thread:
198   State last_state_;
199   int32 last_put_offset_;
200   gpu::Capabilities capabilities_;
201
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_;
211
212 #if defined(OS_ANDROID)
213   scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
214 #endif
215
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_;
219
220   base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
221   base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
222
223   DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
224 };
225
226 }  // namespace gpu
227
228 #endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_