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/command_buffer/client/gl_in_process_context.h"
11 #include <GLES2/gl2.h>
12 #ifndef GL_GLEXT_PROTOTYPES
13 #define GL_GLEXT_PROTOTYPES 1
15 #include <GLES2/gl2ext.h>
16 #include <GLES2/gl2extchromium.h>
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 "ui/gfx/size.h"
30 #include "ui/gl/gl_image.h"
32 #if defined(OS_ANDROID)
33 #include "ui/gl/android/surface_texture.h"
40 const int32 kCommandBufferSize = 1024 * 1024;
41 // TODO(kbr): make the transfer buffer size configurable via context
42 // creation attributes.
43 const size_t kStartTransferBufferSize = 4 * 1024 * 1024;
44 const size_t kMinTransferBufferSize = 1 * 256 * 1024;
45 const size_t kMaxTransferBufferSize = 16 * 1024 * 1024;
47 class GLInProcessContextImpl
48 : public GLInProcessContext,
49 public base::SupportsWeakPtr<GLInProcessContextImpl> {
51 explicit GLInProcessContextImpl();
52 virtual ~GLInProcessContextImpl();
55 scoped_refptr<gfx::GLSurface> surface,
57 bool use_global_share_group,
58 GLInProcessContext* share_context,
59 gfx::AcceleratedWidget window,
60 const gfx::Size& size,
61 const GLInProcessContextAttribs& attribs,
62 gfx::GpuPreference gpu_preference,
63 const scoped_refptr<InProcessCommandBuffer::Service>& service);
65 // GLInProcessContext implementation:
66 virtual void SetContextLostCallback(const base::Closure& callback) OVERRIDE;
67 virtual gles2::GLES2Implementation* GetImplementation() OVERRIDE;
69 #if defined(OS_ANDROID)
70 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
71 uint32 stream_id) OVERRIDE;
77 void OnSignalSyncPoint(const base::Closure& callback);
79 scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_;
80 scoped_ptr<TransferBuffer> transfer_buffer_;
81 scoped_ptr<gles2::GLES2Implementation> gles2_implementation_;
82 scoped_ptr<InProcessCommandBuffer> command_buffer_;
85 base::Closure context_lost_callback_;
87 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl);
90 base::LazyInstance<base::Lock> g_all_shared_contexts_lock =
91 LAZY_INSTANCE_INITIALIZER;
92 base::LazyInstance<std::set<GLInProcessContextImpl*> > g_all_shared_contexts =
93 LAZY_INSTANCE_INITIALIZER;
95 GLInProcessContextImpl::GLInProcessContextImpl()
96 : context_lost_(false) {}
98 GLInProcessContextImpl::~GLInProcessContextImpl() {
100 base::AutoLock lock(g_all_shared_contexts_lock.Get());
101 g_all_shared_contexts.Get().erase(this);
106 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() {
107 return gles2_implementation_.get();
110 void GLInProcessContextImpl::SetContextLostCallback(
111 const base::Closure& callback) {
112 context_lost_callback_ = callback;
115 void GLInProcessContextImpl::OnContextLost() {
116 context_lost_ = true;
117 if (!context_lost_callback_.is_null()) {
118 context_lost_callback_.Run();
122 bool GLInProcessContextImpl::Initialize(
123 scoped_refptr<gfx::GLSurface> surface,
125 bool use_global_share_group,
126 GLInProcessContext* share_context,
127 gfx::AcceleratedWidget window,
128 const gfx::Size& size,
129 const GLInProcessContextAttribs& attribs,
130 gfx::GpuPreference gpu_preference,
131 const scoped_refptr<InProcessCommandBuffer::Service>& service) {
132 DCHECK(!use_global_share_group || !share_context);
133 DCHECK(size.width() >= 0 && size.height() >= 0);
135 // Changes to these values should also be copied to
136 // gpu/command_buffer/client/gl_in_process_context.cc and to
137 // content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h
138 const int32 ALPHA_SIZE = 0x3021;
139 const int32 BLUE_SIZE = 0x3022;
140 const int32 GREEN_SIZE = 0x3023;
141 const int32 RED_SIZE = 0x3024;
142 const int32 DEPTH_SIZE = 0x3025;
143 const int32 STENCIL_SIZE = 0x3026;
144 const int32 SAMPLES = 0x3031;
145 const int32 SAMPLE_BUFFERS = 0x3032;
146 const int32 NONE = 0x3038;
148 // Chromium-specific attributes
149 const int32 FAIL_IF_MAJOR_PERF_CAVEAT = 0x10002;
150 const int32 LOSE_CONTEXT_WHEN_OUT_OF_MEMORY = 0x10003;
152 std::vector<int32> attrib_vector;
153 if (attribs.alpha_size >= 0) {
154 attrib_vector.push_back(ALPHA_SIZE);
155 attrib_vector.push_back(attribs.alpha_size);
157 if (attribs.blue_size >= 0) {
158 attrib_vector.push_back(BLUE_SIZE);
159 attrib_vector.push_back(attribs.blue_size);
161 if (attribs.green_size >= 0) {
162 attrib_vector.push_back(GREEN_SIZE);
163 attrib_vector.push_back(attribs.green_size);
165 if (attribs.red_size >= 0) {
166 attrib_vector.push_back(RED_SIZE);
167 attrib_vector.push_back(attribs.red_size);
169 if (attribs.depth_size >= 0) {
170 attrib_vector.push_back(DEPTH_SIZE);
171 attrib_vector.push_back(attribs.depth_size);
173 if (attribs.stencil_size >= 0) {
174 attrib_vector.push_back(STENCIL_SIZE);
175 attrib_vector.push_back(attribs.stencil_size);
177 if (attribs.samples >= 0) {
178 attrib_vector.push_back(SAMPLES);
179 attrib_vector.push_back(attribs.samples);
181 if (attribs.sample_buffers >= 0) {
182 attrib_vector.push_back(SAMPLE_BUFFERS);
183 attrib_vector.push_back(attribs.sample_buffers);
185 if (attribs.fail_if_major_perf_caveat > 0) {
186 attrib_vector.push_back(FAIL_IF_MAJOR_PERF_CAVEAT);
187 attrib_vector.push_back(attribs.fail_if_major_perf_caveat);
189 if (attribs.lose_context_when_out_of_memory > 0) {
190 attrib_vector.push_back(LOSE_CONTEXT_WHEN_OUT_OF_MEMORY);
191 attrib_vector.push_back(attribs.lose_context_when_out_of_memory);
193 attrib_vector.push_back(NONE);
195 base::Closure wrapped_callback =
196 base::Bind(&GLInProcessContextImpl::OnContextLost, AsWeakPtr());
197 command_buffer_.reset(new InProcessCommandBuffer(service));
199 scoped_ptr<base::AutoLock> scoped_shared_context_lock;
200 scoped_refptr<gles2::ShareGroup> share_group;
201 InProcessCommandBuffer* share_command_buffer = NULL;
202 if (use_global_share_group) {
203 scoped_shared_context_lock.reset(
204 new base::AutoLock(g_all_shared_contexts_lock.Get()));
205 for (std::set<GLInProcessContextImpl*>::const_iterator it =
206 g_all_shared_contexts.Get().begin();
207 it != g_all_shared_contexts.Get().end();
209 const GLInProcessContextImpl* context = *it;
210 if (!context->context_lost_) {
211 share_group = context->gles2_implementation_->share_group();
212 share_command_buffer = context->command_buffer_.get();
214 DCHECK(share_command_buffer);
218 } else if (share_context) {
219 GLInProcessContextImpl* impl =
220 static_cast<GLInProcessContextImpl*>(share_context);
221 share_group = impl->gles2_implementation_->share_group();
222 share_command_buffer = impl->command_buffer_.get();
224 DCHECK(share_command_buffer);
227 if (!command_buffer_->Initialize(surface,
234 share_command_buffer)) {
235 LOG(ERROR) << "Failed to initialize InProcessCommmandBuffer";
239 // Create the GLES2 helper, which writes the command buffer protocol.
240 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get()));
241 if (!gles2_helper_->Initialize(kCommandBufferSize)) {
242 LOG(ERROR) << "Failed to initialize GLES2CmdHelper";
247 // Create a transfer buffer.
248 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get()));
250 bool bind_generates_resources = false;
252 // Create the object exposing the OpenGL API.
253 gles2_implementation_.reset(new gles2::GLES2Implementation(
256 transfer_buffer_.get(),
257 bind_generates_resources,
258 attribs.lose_context_when_out_of_memory > 0,
259 command_buffer_.get()));
261 if (use_global_share_group) {
262 g_all_shared_contexts.Get().insert(this);
263 scoped_shared_context_lock.reset();
266 if (!gles2_implementation_->Initialize(
267 kStartTransferBufferSize,
268 kMinTransferBufferSize,
269 kMaxTransferBufferSize,
270 gles2::GLES2Implementation::kNoLimit)) {
277 void GLInProcessContextImpl::Destroy() {
278 if (gles2_implementation_) {
279 // First flush the context to ensure that any pending frees of resources
280 // are completed. Otherwise, if this context is part of a share group,
281 // those resources might leak. Also, any remaining side effects of commands
282 // issued on this context might not be visible to other contexts in the
284 gles2_implementation_->Flush();
286 gles2_implementation_.reset();
289 transfer_buffer_.reset();
290 gles2_helper_.reset();
291 command_buffer_.reset();
294 #if defined(OS_ANDROID)
295 scoped_refptr<gfx::SurfaceTexture>
296 GLInProcessContextImpl::GetSurfaceTexture(uint32 stream_id) {
297 return command_buffer_->GetSurfaceTexture(stream_id);
301 } // anonymous namespace
303 GLInProcessContextAttribs::GLInProcessContextAttribs()
312 fail_if_major_perf_caveat(-1),
313 lose_context_when_out_of_memory(-1) {}
316 GLInProcessContext* GLInProcessContext::CreateContext(
318 gfx::AcceleratedWidget window,
319 const gfx::Size& size,
320 bool share_resources,
321 const GLInProcessContextAttribs& attribs,
322 gfx::GpuPreference gpu_preference) {
323 scoped_ptr<GLInProcessContextImpl> context(
324 new GLInProcessContextImpl());
325 if (!context->Initialize(
334 scoped_refptr<InProcessCommandBuffer::Service>()))
337 return context.release();
340 GLInProcessContext* GLInProcessContext::Create(
341 scoped_refptr<gpu::InProcessCommandBuffer::Service> service,
342 scoped_refptr<gfx::GLSurface> surface,
344 gfx::AcceleratedWidget window,
345 const gfx::Size& size,
346 GLInProcessContext* share_context,
347 bool use_global_share_group,
348 const GLInProcessContextAttribs& attribs,
349 gfx::GpuPreference gpu_preference) {
350 DCHECK(!use_global_share_group || !share_context);
352 DCHECK_EQ(surface->IsOffscreen(), is_offscreen);
353 DCHECK(surface->GetSize() == size);
354 DCHECK_EQ(gfx::kNullAcceleratedWidget, window);
357 scoped_ptr<GLInProcessContextImpl> context(new GLInProcessContextImpl());
358 if (!context->Initialize(surface,
360 use_global_share_group,
362 gfx::kNullAcceleratedWidget,
369 return context.release();