Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / blink / webgraphicscontext3d_in_process_command_buffer_impl.cc
blob4f83fa396b6eefc6132ba96dab5cc0ed380e004e
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.
5 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
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>
14 #include <string>
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/geometry/size.h"
25 #include "ui/gl/gl_implementation.h"
27 using blink::WGC3Denum;
28 using gpu::gles2::GLES2Implementation;
29 using gpu::GLInProcessContext;
31 namespace gpu_blink {
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));
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));
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. */));
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 is_offscreen_(is_offscreen),
86 window_(window),
87 context_(context.Pass()) {
88 ConvertAttributes(attributes, &attribs_);
89 attribs_.lose_context_when_out_of_memory = lose_context_when_out_of_memory;
92 WebGraphicsContext3DInProcessCommandBufferImpl::
93 ~WebGraphicsContext3DInProcessCommandBufferImpl() {
96 size_t WebGraphicsContext3DInProcessCommandBufferImpl::GetMappedMemoryLimit() {
97 return context_->GetMappedMemoryLimit();
100 bool WebGraphicsContext3DInProcessCommandBufferImpl::MaybeInitializeGL() {
101 if (initialized_)
102 return true;
104 if (initialize_failed_)
105 return false;
107 if (!context_) {
108 // TODO(kbr): More work will be needed in this implementation to
109 // properly support GPU switching. Like in the out-of-process
110 // command buffer implementation, all previously created contexts
111 // will need to be lost either when the first context requesting the
112 // discrete GPU is created, or the last one is destroyed.
113 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
114 context_.reset(GLInProcessContext::Create(
115 NULL, /* service */
116 NULL, /* surface */
117 is_offscreen_,
118 window_,
119 gfx::Size(1, 1),
120 NULL, /* share_context */
121 share_resources_,
122 attribs_,
123 gpu_preference,
124 ::gpu::GLInProcessContextSharedMemoryLimits(),
125 nullptr,
126 nullptr));
129 if (context_) {
130 base::Closure context_lost_callback = base::Bind(
131 &WebGraphicsContext3DInProcessCommandBufferImpl::OnContextLost,
132 base::Unretained(this));
133 context_->SetContextLostCallback(context_lost_callback);
134 } else {
135 initialize_failed_ = true;
136 return false;
139 real_gl_ = context_->GetImplementation();
140 setGLInterface(real_gl_);
142 initialized_ = true;
143 return true;
146 bool
147 WebGraphicsContext3DInProcessCommandBufferImpl::InitializeOnCurrentThread() {
148 if (!MaybeInitializeGL())
149 return false;
150 return context_ && !isContextLost();
153 void WebGraphicsContext3DInProcessCommandBufferImpl::SetLock(base::Lock* lock) {
154 context_->SetLock(lock);
157 ::gpu::ContextSupport*
158 WebGraphicsContext3DInProcessCommandBufferImpl::GetContextSupport() {
159 return real_gl_;
162 void WebGraphicsContext3DInProcessCommandBufferImpl::OnContextLost() {
163 if (context_lost_callback_) {
164 context_lost_callback_->onContextLost();
168 } // namespace gpu_blink