Upstream version 11.40.277.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 <map>
9 #include <vector>
10
11 #include "base/atomic_sequence_num.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/synchronization/lock.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "gpu/command_buffer/client/gpu_control.h"
21 #include "gpu/command_buffer/common/command_buffer.h"
22 #include "gpu/gpu_export.h"
23 #include "ui/gfx/gpu_memory_buffer.h"
24 #include "ui/gfx/native_widget_types.h"
25 #include "ui/gl/gl_surface.h"
26 #include "ui/gl/gpu_preference.h"
27
28 namespace base {
29 class SequenceChecker;
30 }
31
32 namespace gfx {
33 class GLContext;
34 class GLShareGroup;
35 class GLSurface;
36 class Size;
37 }
38
39 #if defined(OS_ANDROID)
40 namespace gfx {
41 class SurfaceTexture;
42 }
43 namespace gpu {
44 class StreamTextureManagerInProcess;
45 }
46 #endif
47
48 namespace gpu {
49
50 namespace gles2 {
51 class GLES2Decoder;
52 class MailboxManager;
53 class ShaderTranslatorCache;
54 }
55
56 class CommandBufferServiceBase;
57 class GpuMemoryBufferManager;
58 class GpuScheduler;
59 class ImageFactory;
60 class TransferBufferManagerInterface;
61
62 // This class provides a thread-safe interface to the global GPU service (for
63 // example GPU thread) when being run in single process mode.
64 // However, the behavior for accessing one context (i.e. one instance of this
65 // class) from different client threads is undefined.
66 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
67                                           public GpuControl {
68  public:
69   class Service;
70   explicit InProcessCommandBuffer(const scoped_refptr<Service>& service);
71   ~InProcessCommandBuffer() override;
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                   GpuMemoryBufferManager* gpu_memory_buffer_manager,
85                   ImageFactory* image_factory);
86   void Destroy();
87
88   // CommandBuffer implementation:
89   bool Initialize() override;
90   State GetLastState() override;
91   int32 GetLastToken() override;
92   void Flush(int32 put_offset) override;
93   void WaitForTokenInRange(int32 start, int32 end) override;
94   void WaitForGetOffsetInRange(int32 start, int32 end) override;
95   void SetGetBuffer(int32 shm_id) override;
96   scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
97                                                   int32* id) override;
98   void DestroyTransferBuffer(int32 id) override;
99   gpu::error::Error GetLastError() override;
100
101   // GpuControl implementation:
102   gpu::Capabilities GetCapabilities() override;
103   int32 CreateImage(ClientBuffer buffer,
104                     size_t width,
105                     size_t height,
106                     unsigned internalformat) override;
107   void DestroyImage(int32 id) override;
108   int32 CreateGpuMemoryBufferImage(size_t width,
109                                    size_t height,
110                                    unsigned internalformat,
111                                    unsigned usage) override;
112   uint32 InsertSyncPoint() override;
113   uint32 InsertFutureSyncPoint() override;
114   void RetireSyncPoint(uint32 sync_point) override;
115   void SignalSyncPoint(uint32 sync_point,
116                        const base::Closure& callback) override;
117   void SignalQuery(uint32 query_id, const base::Closure& callback) override;
118   void SetSurfaceVisible(bool visible) override;
119   uint32 CreateStreamTexture(uint32 texture_id) override;
120
121   // The serializer interface to the GPU service (i.e. thread).
122   class Service {
123    public:
124     Service();
125     virtual ~Service();
126
127     virtual void AddRef() const = 0;
128     virtual void Release() const = 0;
129
130     // Queues a task to run as soon as possible.
131     virtual void ScheduleTask(const base::Closure& task) = 0;
132
133     // Schedules |callback| to run at an appropriate time for performing idle
134     // work.
135     virtual void ScheduleIdleWork(const base::Closure& task) = 0;
136
137     virtual bool UseVirtualizedGLContexts() = 0;
138     virtual scoped_refptr<gles2::ShaderTranslatorCache>
139         shader_translator_cache() = 0;
140     scoped_refptr<gles2::MailboxManager> mailbox_manager();
141
142    private:
143     scoped_refptr<gles2::MailboxManager> mailbox_manager_;
144   };
145
146 #if defined(OS_ANDROID)
147   scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
148       uint32 stream_id);
149 #endif
150
151  private:
152   struct InitializeOnGpuThreadParams {
153     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;  // Ouptut.
159     InProcessCommandBuffer* context_group;
160     ImageFactory* image_factory;
161
162     InitializeOnGpuThreadParams(bool is_offscreen,
163                                 gfx::AcceleratedWidget window,
164                                 const gfx::Size& size,
165                                 const std::vector<int32>& attribs,
166                                 gfx::GpuPreference gpu_preference,
167                                 gpu::Capabilities* capabilities,
168                                 InProcessCommandBuffer* share_group,
169                                 ImageFactory* image_factory)
170         : is_offscreen(is_offscreen),
171           window(window),
172           size(size),
173           attribs(attribs),
174           gpu_preference(gpu_preference),
175           capabilities(capabilities),
176           context_group(share_group),
177           image_factory(image_factory) {}
178   };
179
180   bool InitializeOnGpuThread(const InitializeOnGpuThreadParams& params);
181   bool DestroyOnGpuThread();
182   void FlushOnGpuThread(int32 put_offset);
183   void ScheduleIdleWorkOnGpuThread();
184   uint32 CreateStreamTextureOnGpuThread(uint32 client_texture_id);
185   bool MakeCurrent();
186   base::Closure WrapCallback(const base::Closure& callback);
187   State GetStateFast();
188   void QueueTask(const base::Closure& task) { service_->ScheduleTask(task); }
189   void CheckSequencedThread();
190   void RetireSyncPointOnGpuThread(uint32 sync_point);
191   void SignalSyncPointOnGpuThread(uint32 sync_point,
192                                   const base::Closure& callback);
193   bool WaitSyncPointOnGpuThread(uint32 sync_point);
194   void SignalQueryOnGpuThread(unsigned query_id, const base::Closure& callback);
195   void DestroyTransferBufferOnGpuThread(int32 id);
196   void CreateImageOnGpuThread(int32 id,
197                               const gfx::GpuMemoryBufferHandle& handle,
198                               const gfx::Size& size,
199                               gfx::GpuMemoryBuffer::Format format,
200                               uint32 internalformat);
201   void DestroyImageOnGpuThread(int32 id);
202
203   // Callbacks:
204   void OnContextLost();
205   void OnResizeView(gfx::Size size, float scale_factor);
206   bool GetBufferChanged(int32 transfer_buffer_id);
207   void PumpCommands();
208   void PerformIdleWork();
209
210   static scoped_refptr<Service> GetDefaultService();
211
212   // Members accessed on the gpu thread (possibly with the exception of
213   // creation):
214   bool context_lost_;
215   scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
216   scoped_ptr<GpuScheduler> gpu_scheduler_;
217   scoped_ptr<gles2::GLES2Decoder> decoder_;
218   scoped_refptr<gfx::GLContext> context_;
219   scoped_refptr<gfx::GLSurface> surface_;
220   base::Closure context_lost_callback_;
221   bool idle_work_pending_;  // Used to throttle PerformIdleWork.
222   ImageFactory* image_factory_;
223
224   // Members accessed on the client thread:
225   State last_state_;
226   int32 last_put_offset_;
227   gpu::Capabilities capabilities_;
228   GpuMemoryBufferManager* gpu_memory_buffer_manager_;
229   base::AtomicSequenceNumber next_image_id_;
230
231   // Accessed on both threads:
232   scoped_ptr<CommandBufferServiceBase> command_buffer_;
233   base::Lock command_buffer_lock_;
234   base::WaitableEvent flush_event_;
235   scoped_refptr<Service> service_;
236   State state_after_last_flush_;
237   base::Lock state_after_last_flush_lock_;
238   scoped_refptr<gfx::GLShareGroup> gl_share_group_;
239
240 #if defined(OS_ANDROID)
241   scoped_ptr<StreamTextureManagerInProcess> stream_texture_manager_;
242 #endif
243
244   // Only used with explicit scheduling and the gpu thread is the same as
245   // the client thread.
246   scoped_ptr<base::SequenceChecker> sequence_checker_;
247
248   base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
249   base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
250
251   DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
252 };
253
254 }  // namespace gpu
255
256 #endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_