- add sources.
[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 GLSurface;
32 class Size;
33 }
34
35 #if defined(OS_ANDROID)
36 namespace gfx {
37 class SurfaceTexture;
38 }
39 namespace gpu {
40 class StreamTextureManagerInProcess;
41 }
42 #endif
43
44 namespace gpu {
45
46 namespace gles2 {
47 class GLES2Decoder;
48 }
49
50 class GpuMemoryBufferFactory;
51 class GpuScheduler;
52 class TransferBufferManagerInterface;
53
54 // This class provides a thread-safe interface to the global GPU service (for
55 // example GPU thread) when being run in single process mode.
56 // However, the behavior for accessing one context (i.e. one instance of this
57 // class) from different client threads is undefined.
58 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
59                                           public GpuControl {
60  public:
61   InProcessCommandBuffer();
62   virtual ~InProcessCommandBuffer();
63
64   // Used to override the GPU thread with explicit scheduling.
65   // (By default an internal GPU thread will be spawned to handle all GL work
66   // and the two functions are unused.)
67   // The callback will be called from different client threads. After the
68   // callback is issued, the client is expected to eventually call
69   // ProcessGpuWorkOnCurrentThread(). The latter cannot be called from different
70   // threads.
71   // The callback needs to be set before any context is created.
72   static void SetScheduleCallback(const base::Closure& callback);
73   static void ProcessGpuWorkOnCurrentThread();
74
75   static void EnableVirtualizedContext();
76   static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory* factory);
77
78   // If |surface| is not NULL, use it directly; in this case, the command
79   // buffer gpu thread must be the same as the client thread. Otherwise create
80   // a new GLSurface.
81   bool Initialize(scoped_refptr<gfx::GLSurface> surface,
82                   bool is_offscreen,
83                   bool share_resources,
84                   gfx::AcceleratedWidget window,
85                   const gfx::Size& size,
86                   const std::vector<int32>& attribs,
87                   gfx::GpuPreference gpu_preference,
88                   const base::Closure& context_lost_callback,
89                   unsigned int share_group_id);
90   void Destroy();
91
92   // CommandBuffer implementation:
93   virtual bool Initialize() OVERRIDE;
94   virtual State GetState() OVERRIDE;
95   virtual State GetLastState() OVERRIDE;
96   virtual int32 GetLastToken() OVERRIDE;
97   virtual void Flush(int32 put_offset) OVERRIDE;
98   virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
99   virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
100   virtual void SetGetOffset(int32 get_offset) OVERRIDE;
101   virtual gpu::Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
102   virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
103   virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
104   virtual void SetToken(int32 token) OVERRIDE;
105   virtual void SetParseError(gpu::error::Error error) OVERRIDE;
106   virtual void SetContextLostReason(
107       gpu::error::ContextLostReason reason) OVERRIDE;
108   virtual gpu::error::Error GetLastError() OVERRIDE;
109
110   // GpuControl implementation:
111   virtual bool SupportsGpuMemoryBuffer() OVERRIDE;
112   virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
113       size_t width,
114       size_t height,
115       unsigned internalformat,
116       int32* id) OVERRIDE;
117   virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
118   virtual bool GenerateMailboxNames(unsigned num,
119                                     std::vector<gpu::Mailbox>* names) OVERRIDE;
120   virtual uint32 InsertSyncPoint() OVERRIDE;
121   virtual void SignalSyncPoint(uint32 sync_point,
122                                const base::Closure& callback) OVERRIDE;
123   virtual void SignalQuery(uint32 query,
124                            const base::Closure& callback) OVERRIDE;
125   virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
126       OVERRIDE;
127
128   // The serializer interface to the GPU service (i.e. thread).
129   class SchedulerClient {
130    public:
131      virtual ~SchedulerClient() {}
132
133      // Queues a task to run as soon as possible.
134      virtual void QueueTask(const base::Closure& task) = 0;
135
136      // Schedules |callback| to run at an appropriate time for performing idle
137      // work.
138      virtual void ScheduleIdleWork(const base::Closure& task) = 0;
139   };
140
141 #if defined(OS_ANDROID)
142   scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
143       uint32 stream_id);
144 #endif
145
146  private:
147   bool InitializeOnGpuThread(bool is_offscreen,
148                              gfx::AcceleratedWidget window,
149                              const gfx::Size& size,
150                              const std::vector<int32>& attribs,
151                              gfx::GpuPreference gpu_preference);
152   bool DestroyOnGpuThread();
153   void FlushOnGpuThread(int32 put_offset);
154   bool MakeCurrent();
155   bool IsContextLost();
156   base::Closure WrapCallback(const base::Closure& callback);
157   State GetStateFast();
158   void QueueTask(const base::Closure& task) { queue_->QueueTask(task); }
159   void CheckSequencedThread();
160
161   // Callbacks:
162   void OnContextLost();
163   void OnResizeView(gfx::Size size, float scale_factor);
164   bool GetBufferChanged(int32 transfer_buffer_id);
165   void PumpCommands();
166   void ScheduleMoreIdleWork();
167
168   // Members accessed on the gpu thread (possibly with the exception of
169   // creation):
170   bool context_lost_;
171   bool share_resources_;
172   scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
173   scoped_ptr<GpuScheduler> gpu_scheduler_;
174   scoped_ptr<gles2::GLES2Decoder> decoder_;
175   scoped_refptr<gfx::GLContext> context_;
176   scoped_refptr<gfx::GLSurface> surface_;
177   base::Closure context_lost_callback_;
178   unsigned int share_group_id_;
179
180   // Members accessed on the client thread:
181   State last_state_;
182   int32 last_put_offset_;
183   bool supports_gpu_memory_buffer_;
184
185   // Accessed on both threads:
186   scoped_ptr<CommandBuffer> command_buffer_;
187   base::Lock command_buffer_lock_;
188   base::WaitableEvent flush_event_;
189   scoped_ptr<SchedulerClient> queue_;
190   State state_after_last_flush_;
191   base::Lock state_after_last_flush_lock_;
192   scoped_ptr<GpuControl> gpu_control_;
193
194 #if defined(OS_ANDROID)
195   scoped_refptr<StreamTextureManagerInProcess> stream_texture_manager_;
196 #endif
197
198   // Only used with explicit scheduling and the gpu thread is the same as
199   // the client thread.
200   scoped_ptr<base::SequenceChecker> sequence_checker_;
201
202   base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
203   base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
204
205   DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
206 };
207
208 }  // namespace gpu
209
210 #endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_