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 "gpu/command_buffer/service/in_process_command_buffer.h"
30 #include "ui/gfx/size.h"
31 #include "ui/gl/gl_image.h"
33 #if defined(OS_ANDROID)
34 #include "ui/gl/android/surface_texture.h"
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;
48 class GLInProcessContextImpl
49 : public GLInProcessContext,
50 public base::SupportsWeakPtr<GLInProcessContextImpl> {
52 explicit GLInProcessContextImpl();
53 virtual ~GLInProcessContextImpl();
55 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
58 gfx::AcceleratedWidget window,
59 const gfx::Size& size,
60 const GLInProcessContextAttribs& attribs,
61 gfx::GpuPreference gpu_preference);
63 // GLInProcessContext implementation:
64 virtual void SetContextLostCallback(const base::Closure& callback) OVERRIDE;
65 virtual gles2::GLES2Implementation* GetImplementation() OVERRIDE;
67 #if defined(OS_ANDROID)
68 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
69 uint32 stream_id) OVERRIDE;
75 void OnSignalSyncPoint(const base::Closure& callback);
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_;
82 unsigned int share_group_id_;
84 base::Closure context_lost_callback_;
86 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl);
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;
94 size_t SharedContextCount() {
95 base::AutoLock lock(g_all_shared_contexts_lock.Get());
96 return g_all_shared_contexts.Get().size();
99 GLInProcessContextImpl::GLInProcessContextImpl()
100 : share_group_id_(0), context_lost_(false) {}
102 GLInProcessContextImpl::~GLInProcessContextImpl() {
104 base::AutoLock lock(g_all_shared_contexts_lock.Get());
105 g_all_shared_contexts.Get().erase(this);
110 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() {
111 return gles2_implementation_.get();
114 void GLInProcessContextImpl::SetContextLostCallback(
115 const base::Closure& callback) {
116 context_lost_callback_ = callback;
119 void GLInProcessContextImpl::OnContextLost() {
120 context_lost_ = true;
121 if (!context_lost_callback_.is_null()) {
122 context_lost_callback_.Run();
126 bool GLInProcessContextImpl::Initialize(
127 scoped_refptr<gfx::GLSurface> surface,
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);
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;
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);
151 if (attribs.blue_size >= 0) {
152 attrib_vector.push_back(BLUE_SIZE);
153 attrib_vector.push_back(attribs.blue_size);
155 if (attribs.green_size >= 0) {
156 attrib_vector.push_back(GREEN_SIZE);
157 attrib_vector.push_back(attribs.green_size);
159 if (attribs.red_size >= 0) {
160 attrib_vector.push_back(RED_SIZE);
161 attrib_vector.push_back(attribs.red_size);
163 if (attribs.depth_size >= 0) {
164 attrib_vector.push_back(DEPTH_SIZE);
165 attrib_vector.push_back(attribs.depth_size);
167 if (attribs.stencil_size >= 0) {
168 attrib_vector.push_back(STENCIL_SIZE);
169 attrib_vector.push_back(attribs.stencil_size);
171 if (attribs.samples >= 0) {
172 attrib_vector.push_back(SAMPLES);
173 attrib_vector.push_back(attribs.samples);
175 if (attribs.sample_buffers >= 0) {
176 attrib_vector.push_back(SAMPLE_BUFFERS);
177 attrib_vector.push_back(attribs.sample_buffers);
179 attrib_vector.push_back(NONE);
181 base::Closure wrapped_callback =
182 base::Bind(&GLInProcessContextImpl::OnContextLost, AsWeakPtr());
183 command_buffer_.reset(new InProcessCommandBuffer());
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();
194 const GLInProcessContextImpl* context = *it;
195 if (!context->context_lost_) {
196 share_group = context->gles2_implementation_->share_group();
198 share_group_id_ = context->share_group_id_;
201 share_group_id_ = std::max(share_group_id_, context->share_group_id_);
203 if (!share_group && !++share_group_id_)
206 if (!command_buffer_->Initialize(surface,
215 LOG(INFO) << "Failed to initialize InProcessCommmandBuffer";
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";
227 // Create a transfer buffer.
228 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get()));
230 // Create the object exposing the OpenGL API.
231 gles2_implementation_.reset(new gles2::GLES2Implementation(
234 transfer_buffer_.get(),
236 command_buffer_.get()));
238 if (share_resources) {
239 g_all_shared_contexts.Get().insert(this);
240 scoped_shared_context_lock.reset();
243 if (!gles2_implementation_->Initialize(
244 kStartTransferBufferSize,
245 kMinTransferBufferSize,
246 kMaxTransferBufferSize,
247 gles2::GLES2Implementation::kNoLimit)) {
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
261 gles2_implementation_->Flush();
263 gles2_implementation_.reset();
266 transfer_buffer_.reset();
267 gles2_helper_.reset();
268 command_buffer_.reset();
271 #if defined(OS_ANDROID)
272 scoped_refptr<gfx::SurfaceTexture>
273 GLInProcessContextImpl::GetSurfaceTexture(uint32 stream_id) {
274 return command_buffer_->GetSurfaceTexture(stream_id);
278 } // anonymous namespace
280 GLInProcessContextAttribs::GLInProcessContextAttribs()
288 sample_buffers(-1) {}
291 GLInProcessContext* GLInProcessContext::CreateContext(
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(
310 return context.release();
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(
323 surface->IsOffscreen(),
325 gfx::kNullAcceleratedWidget,
331 return context.release();