- add sources.
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / client / gl_in_process_context.cc
1 // Copyright (c) 2012 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 #include "gpu/command_buffer/client/gl_in_process_context.h"
6
7 #include <set>
8 #include <utility>
9 #include <vector>
10
11 #include <GLES2/gl2.h>
12 #ifndef GL_GLEXT_PROTOTYPES
13 #define GL_GLEXT_PROTOTYPES 1
14 #endif
15 #include <GLES2/gl2ext.h>
16 #include <GLES2/gl2extchromium.h>
17
18 #include "base/bind.h"
19 #include "base/bind_helpers.h"
20 #include "base/lazy_instance.h"
21 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/memory/weak_ptr.h"
24 #include "base/message_loop/message_loop.h"
25 #include "gpu/command_buffer/client/gles2_implementation.h"
26 #include "gpu/command_buffer/client/transfer_buffer.h"
27 #include "gpu/command_buffer/common/command_buffer.h"
28 #include "gpu/command_buffer/common/constants.h"
29 #include "gpu/command_buffer/service/in_process_command_buffer.h"
30 #include "ui/gfx/size.h"
31 #include "ui/gl/gl_image.h"
32
33 #if defined(OS_ANDROID)
34 #include "ui/gl/android/surface_texture.h"
35 #endif
36
37 namespace gpu {
38
39 namespace {
40
41 const int32 kCommandBufferSize = 1024 * 1024;
42 // TODO(kbr): make the transfer buffer size configurable via context
43 // creation attributes.
44 const size_t kStartTransferBufferSize = 4 * 1024 * 1024;
45 const size_t kMinTransferBufferSize = 1 * 256 * 1024;
46 const size_t kMaxTransferBufferSize = 16 * 1024 * 1024;
47
48 class GLInProcessContextImpl
49     : public GLInProcessContext,
50       public base::SupportsWeakPtr<GLInProcessContextImpl> {
51  public:
52   explicit GLInProcessContextImpl();
53   virtual ~GLInProcessContextImpl();
54
55   bool Initialize(scoped_refptr<gfx::GLSurface> surface,
56                   bool is_offscreen,
57                   bool share_resources,
58                   gfx::AcceleratedWidget window,
59                   const gfx::Size& size,
60                   const GLInProcessContextAttribs& attribs,
61                   gfx::GpuPreference gpu_preference);
62
63   // GLInProcessContext implementation:
64   virtual void SetContextLostCallback(const base::Closure& callback) OVERRIDE;
65   virtual gles2::GLES2Implementation* GetImplementation() OVERRIDE;
66
67 #if defined(OS_ANDROID)
68   virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
69       uint32 stream_id) OVERRIDE;
70 #endif
71
72  private:
73   void Destroy();
74   void OnContextLost();
75   void OnSignalSyncPoint(const base::Closure& callback);
76
77   scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_;
78   scoped_ptr<TransferBuffer> transfer_buffer_;
79   scoped_ptr<gles2::GLES2Implementation> gles2_implementation_;
80   scoped_ptr<InProcessCommandBuffer> command_buffer_;
81
82   unsigned int share_group_id_;
83   bool context_lost_;
84   base::Closure context_lost_callback_;
85
86   DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl);
87 };
88
89 base::LazyInstance<base::Lock> g_all_shared_contexts_lock =
90     LAZY_INSTANCE_INITIALIZER;
91 base::LazyInstance<std::set<GLInProcessContextImpl*> > g_all_shared_contexts =
92     LAZY_INSTANCE_INITIALIZER;
93
94 size_t SharedContextCount() {
95   base::AutoLock lock(g_all_shared_contexts_lock.Get());
96   return g_all_shared_contexts.Get().size();
97 }
98
99 GLInProcessContextImpl::GLInProcessContextImpl()
100     : share_group_id_(0), context_lost_(false) {}
101
102 GLInProcessContextImpl::~GLInProcessContextImpl() {
103   {
104     base::AutoLock lock(g_all_shared_contexts_lock.Get());
105     g_all_shared_contexts.Get().erase(this);
106   }
107   Destroy();
108 }
109
110 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() {
111   return gles2_implementation_.get();
112 }
113
114 void GLInProcessContextImpl::SetContextLostCallback(
115     const base::Closure& callback) {
116   context_lost_callback_ = callback;
117 }
118
119 void GLInProcessContextImpl::OnContextLost() {
120   context_lost_ = true;
121   if (!context_lost_callback_.is_null()) {
122     context_lost_callback_.Run();
123   }
124 }
125
126 bool GLInProcessContextImpl::Initialize(
127     scoped_refptr<gfx::GLSurface> surface,
128     bool is_offscreen,
129     bool share_resources,
130     gfx::AcceleratedWidget window,
131     const gfx::Size& size,
132     const GLInProcessContextAttribs& attribs,
133     gfx::GpuPreference gpu_preference) {
134   DCHECK(size.width() >= 0 && size.height() >= 0);
135
136   const int32 ALPHA_SIZE     = 0x3021;
137   const int32 BLUE_SIZE      = 0x3022;
138   const int32 GREEN_SIZE     = 0x3023;
139   const int32 RED_SIZE       = 0x3024;
140   const int32 DEPTH_SIZE     = 0x3025;
141   const int32 STENCIL_SIZE   = 0x3026;
142   const int32 SAMPLES        = 0x3031;
143   const int32 SAMPLE_BUFFERS = 0x3032;
144   const int32 NONE           = 0x3038;
145
146   std::vector<int32> attrib_vector;
147   if (attribs.alpha_size >= 0) {
148     attrib_vector.push_back(ALPHA_SIZE);
149     attrib_vector.push_back(attribs.alpha_size);
150   }
151   if (attribs.blue_size >= 0) {
152     attrib_vector.push_back(BLUE_SIZE);
153     attrib_vector.push_back(attribs.blue_size);
154   }
155   if (attribs.green_size >= 0) {
156     attrib_vector.push_back(GREEN_SIZE);
157     attrib_vector.push_back(attribs.green_size);
158   }
159   if (attribs.red_size >= 0) {
160     attrib_vector.push_back(RED_SIZE);
161     attrib_vector.push_back(attribs.red_size);
162   }
163   if (attribs.depth_size >= 0) {
164     attrib_vector.push_back(DEPTH_SIZE);
165     attrib_vector.push_back(attribs.depth_size);
166   }
167   if (attribs.stencil_size >= 0) {
168     attrib_vector.push_back(STENCIL_SIZE);
169     attrib_vector.push_back(attribs.stencil_size);
170   }
171   if (attribs.samples >= 0) {
172     attrib_vector.push_back(SAMPLES);
173     attrib_vector.push_back(attribs.samples);
174   }
175   if (attribs.sample_buffers >= 0) {
176     attrib_vector.push_back(SAMPLE_BUFFERS);
177     attrib_vector.push_back(attribs.sample_buffers);
178   }
179   attrib_vector.push_back(NONE);
180
181   base::Closure wrapped_callback =
182       base::Bind(&GLInProcessContextImpl::OnContextLost, AsWeakPtr());
183   command_buffer_.reset(new InProcessCommandBuffer());
184
185   scoped_ptr<base::AutoLock> scoped_shared_context_lock;
186   scoped_refptr<gles2::ShareGroup> share_group;
187   if (share_resources) {
188     scoped_shared_context_lock.reset(
189         new base::AutoLock(g_all_shared_contexts_lock.Get()));
190     for (std::set<GLInProcessContextImpl*>::const_iterator it =
191              g_all_shared_contexts.Get().begin();
192          it != g_all_shared_contexts.Get().end();
193          it++) {
194       const GLInProcessContextImpl* context = *it;
195       if (!context->context_lost_) {
196         share_group = context->gles2_implementation_->share_group();
197         DCHECK(share_group);
198         share_group_id_ = context->share_group_id_;
199         break;
200       }
201       share_group_id_ = std::max(share_group_id_, context->share_group_id_);
202     }
203     if (!share_group && !++share_group_id_)
204         ++share_group_id_;
205   }
206   if (!command_buffer_->Initialize(surface,
207                                    is_offscreen,
208                                    share_resources,
209                                    window,
210                                    size,
211                                    attrib_vector,
212                                    gpu_preference,
213                                    wrapped_callback,
214                                    share_group_id_)) {
215     LOG(INFO) << "Failed to initialize InProcessCommmandBuffer";
216     return false;
217   }
218
219   // Create the GLES2 helper, which writes the command buffer protocol.
220   gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get()));
221   if (!gles2_helper_->Initialize(kCommandBufferSize)) {
222     LOG(INFO) << "Failed to initialize GLES2CmdHelper";
223     Destroy();
224     return false;
225   }
226
227   // Create a transfer buffer.
228   transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get()));
229
230   // Create the object exposing the OpenGL API.
231   gles2_implementation_.reset(new gles2::GLES2Implementation(
232       gles2_helper_.get(),
233       share_group,
234       transfer_buffer_.get(),
235       false,
236       command_buffer_.get()));
237
238   if (share_resources) {
239     g_all_shared_contexts.Get().insert(this);
240     scoped_shared_context_lock.reset();
241   }
242
243   if (!gles2_implementation_->Initialize(
244       kStartTransferBufferSize,
245       kMinTransferBufferSize,
246       kMaxTransferBufferSize,
247       gles2::GLES2Implementation::kNoLimit)) {
248     return false;
249   }
250
251   return true;
252 }
253
254 void GLInProcessContextImpl::Destroy() {
255   if (gles2_implementation_) {
256     // First flush the context to ensure that any pending frees of resources
257     // are completed. Otherwise, if this context is part of a share group,
258     // those resources might leak. Also, any remaining side effects of commands
259     // issued on this context might not be visible to other contexts in the
260     // share group.
261     gles2_implementation_->Flush();
262
263     gles2_implementation_.reset();
264   }
265
266   transfer_buffer_.reset();
267   gles2_helper_.reset();
268   command_buffer_.reset();
269 }
270
271 #if defined(OS_ANDROID)
272 scoped_refptr<gfx::SurfaceTexture>
273 GLInProcessContextImpl::GetSurfaceTexture(uint32 stream_id) {
274   return command_buffer_->GetSurfaceTexture(stream_id);
275 }
276 #endif
277
278 }  // anonymous namespace
279
280 GLInProcessContextAttribs::GLInProcessContextAttribs()
281     : alpha_size(-1),
282       blue_size(-1),
283       green_size(-1),
284       red_size(-1),
285       depth_size(-1),
286       stencil_size(-1),
287       samples(-1),
288       sample_buffers(-1) {}
289
290 // static
291 GLInProcessContext* GLInProcessContext::CreateContext(
292     bool is_offscreen,
293     gfx::AcceleratedWidget window,
294     const gfx::Size& size,
295     bool share_resources,
296     const GLInProcessContextAttribs& attribs,
297     gfx::GpuPreference gpu_preference) {
298   scoped_ptr<GLInProcessContextImpl> context(
299       new GLInProcessContextImpl());
300   if (!context->Initialize(
301       NULL /* surface */,
302       is_offscreen,
303       share_resources,
304       window,
305       size,
306       attribs,
307       gpu_preference))
308     return NULL;
309
310   return context.release();
311 }
312
313 // static
314 GLInProcessContext* GLInProcessContext::CreateWithSurface(
315     scoped_refptr<gfx::GLSurface> surface,
316     bool share_resources,
317     const GLInProcessContextAttribs& attribs,
318     gfx::GpuPreference gpu_preference) {
319   scoped_ptr<GLInProcessContextImpl> context(
320       new GLInProcessContextImpl());
321   if (!context->Initialize(
322       surface,
323       surface->IsOffscreen(),
324       share_resources,
325       gfx::kNullAcceleratedWidget,
326       surface->GetSize(),
327       attribs,
328       gpu_preference))
329     return NULL;
330
331   return context.release();
332 }
333
334 }  // namespace gpu