Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / webkit / common / gpu / webgraphicscontext3d_in_process_command_buffer_impl.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 "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
6
7 #include <GLES2/gl2.h>
8 #ifndef GL_GLEXT_PROTOTYPES
9 #define GL_GLEXT_PROTOTYPES 1
10 #endif
11 #include <GLES2/gl2ext.h>
12 #include <GLES2/gl2extchromium.h>
13
14 #include <string>
15
16 #include "base/atomicops.h"
17 #include "base/bind.h"
18 #include "base/bind_helpers.h"
19 #include "base/callback.h"
20 #include "base/logging.h"
21 #include "gpu/command_buffer/client/gles2_implementation.h"
22 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
23 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
24 #include "ui/gfx/size.h"
25 #include "ui/gl/gl_implementation.h"
26
27 using gpu::gles2::GLES2Implementation;
28 using gpu::GLInProcessContext;
29
30 namespace webkit {
31 namespace gpu {
32
33 // static
34 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>
35 WebGraphicsContext3DInProcessCommandBufferImpl::CreateViewContext(
36     const blink::WebGraphicsContext3D::Attributes& attributes,
37     bool lose_context_when_out_of_memory,
38     gfx::AcceleratedWidget window) {
39   DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone);
40   bool is_offscreen = false;
41   return make_scoped_ptr(new WebGraphicsContext3DInProcessCommandBufferImpl(
42       scoped_ptr< ::gpu::GLInProcessContext>(),
43       attributes,
44       lose_context_when_out_of_memory,
45       is_offscreen,
46       window));
47 }
48
49 // static
50 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>
51 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
52     const blink::WebGraphicsContext3D::Attributes& attributes,
53     bool lose_context_when_out_of_memory) {
54   bool is_offscreen = true;
55   return make_scoped_ptr(new WebGraphicsContext3DInProcessCommandBufferImpl(
56       scoped_ptr< ::gpu::GLInProcessContext>(),
57       attributes,
58       lose_context_when_out_of_memory,
59       is_offscreen,
60       gfx::kNullAcceleratedWidget));
61 }
62
63 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>
64 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
65     scoped_ptr< ::gpu::GLInProcessContext> context,
66     const blink::WebGraphicsContext3D::Attributes& attributes) {
67   bool lose_context_when_out_of_memory = false;  // Not used.
68   bool is_offscreen = true;                      // Not used.
69   return make_scoped_ptr(new WebGraphicsContext3DInProcessCommandBufferImpl(
70       context.Pass(),
71       attributes,
72       lose_context_when_out_of_memory,
73       is_offscreen,
74       gfx::kNullAcceleratedWidget /* window. Not used. */));
75 }
76
77 WebGraphicsContext3DInProcessCommandBufferImpl::
78     WebGraphicsContext3DInProcessCommandBufferImpl(
79         scoped_ptr< ::gpu::GLInProcessContext> context,
80         const blink::WebGraphicsContext3D::Attributes& attributes,
81         bool lose_context_when_out_of_memory,
82         bool is_offscreen,
83         gfx::AcceleratedWidget window)
84     : share_resources_(attributes.shareResources),
85       webgl_context_(attributes.webGL),
86       is_offscreen_(is_offscreen),
87       window_(window),
88       context_(context.Pass()) {
89   ConvertAttributes(attributes, &attribs_);
90   attribs_.lose_context_when_out_of_memory = lose_context_when_out_of_memory;
91 }
92
93 WebGraphicsContext3DInProcessCommandBufferImpl::
94     ~WebGraphicsContext3DInProcessCommandBufferImpl() {
95 }
96
97 size_t WebGraphicsContext3DInProcessCommandBufferImpl::GetMappedMemoryLimit() {
98   return context_->GetMappedMemoryLimit();
99 }
100
101 bool WebGraphicsContext3DInProcessCommandBufferImpl::MaybeInitializeGL() {
102   if (initialized_)
103     return true;
104
105   if (initialize_failed_)
106     return false;
107
108   if (!context_) {
109     // TODO(kbr): More work will be needed in this implementation to
110     // properly support GPU switching. Like in the out-of-process
111     // command buffer implementation, all previously created contexts
112     // will need to be lost either when the first context requesting the
113     // discrete GPU is created, or the last one is destroyed.
114     gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
115     context_.reset(GLInProcessContext::Create(
116         NULL, /* service */
117         NULL, /* surface */
118         is_offscreen_,
119         window_,
120         gfx::Size(1, 1),
121         NULL, /* share_context */
122         share_resources_,
123         attribs_,
124         gpu_preference,
125         ::gpu::GLInProcessContextSharedMemoryLimits()));
126   }
127
128   if (context_) {
129     base::Closure context_lost_callback = base::Bind(
130         &WebGraphicsContext3DInProcessCommandBufferImpl::OnContextLost,
131         base::Unretained(this));
132     context_->SetContextLostCallback(context_lost_callback);
133   } else {
134     initialize_failed_ = true;
135     return false;
136   }
137
138   real_gl_ = context_->GetImplementation();
139   setGLInterface(real_gl_);
140
141   if (real_gl_ && webgl_context_)
142     real_gl_->EnableFeatureCHROMIUM("webgl_enable_glsl_webgl_validation");
143
144   initialized_ = true;
145   return true;
146 }
147
148 bool
149 WebGraphicsContext3DInProcessCommandBufferImpl::InitializeOnCurrentThread() {
150   if (!MaybeInitializeGL())
151     return false;
152   return context_ && !isContextLost();
153 }
154
155 bool WebGraphicsContext3DInProcessCommandBufferImpl::isContextLost() {
156   return context_lost_reason_ != GL_NO_ERROR;
157 }
158
159 WGC3Denum WebGraphicsContext3DInProcessCommandBufferImpl::
160     getGraphicsResetStatusARB() {
161   return context_lost_reason_;
162 }
163
164 ::gpu::ContextSupport*
165 WebGraphicsContext3DInProcessCommandBufferImpl::GetContextSupport() {
166   return real_gl_;
167 }
168
169 void WebGraphicsContext3DInProcessCommandBufferImpl::OnContextLost() {
170   // TODO(kbr): improve the precision here.
171   context_lost_reason_ = GL_UNKNOWN_CONTEXT_RESET_ARB;
172   if (context_lost_callback_) {
173     context_lost_callback_->onContextLost();
174   }
175 }
176
177 }  // namespace gpu
178 }  // namespace webkit