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 "ui/gfx/size.h"
30 #include "ui/gl/gl_image.h"
31
32 #if defined(OS_ANDROID)
33 #include "ui/gl/android/surface_texture.h"
34 #endif
35
36 namespace gpu {
37
38 namespace {
39
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 = 6 * 1024 * 1024;
46
47 class GLInProcessContextImpl
48 : public GLInProcessContext,
49 public base::SupportsWeakPtr<GLInProcessContextImpl> {
50 public:
51 explicit GLInProcessContextImpl();
52 virtual ~GLInProcessContextImpl();
53
54 bool Initialize(
55 scoped_refptr<gfx::GLSurface> surface,
56 bool is_offscreen,
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);
64
65 // GLInProcessContext implementation:
66 virtual void SetContextLostCallback(const base::Closure& callback) OVERRIDE;
67 virtual gles2::GLES2Implementation* GetImplementation() OVERRIDE;
68
69 #if defined(OS_ANDROID)
70 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
71 uint32 stream_id) OVERRIDE;
72 #endif
73
74 private:
75 void Destroy();
76 void OnContextLost();
77 void OnSignalSyncPoint(const base::Closure& callback);
78
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_;
83
84 bool context_lost_;
85 base::Closure context_lost_callback_;
86
87 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl);
88 };
89
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;
94
GLInProcessContextImpl()95 GLInProcessContextImpl::GLInProcessContextImpl()
96 : context_lost_(false) {}
97
~GLInProcessContextImpl()98 GLInProcessContextImpl::~GLInProcessContextImpl() {
99 {
100 base::AutoLock lock(g_all_shared_contexts_lock.Get());
101 g_all_shared_contexts.Get().erase(this);
102 }
103 Destroy();
104 }
105
GetImplementation()106 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() {
107 return gles2_implementation_.get();
108 }
109
SetContextLostCallback(const base::Closure & callback)110 void GLInProcessContextImpl::SetContextLostCallback(
111 const base::Closure& callback) {
112 context_lost_callback_ = callback;
113 }
114
OnContextLost()115 void GLInProcessContextImpl::OnContextLost() {
116 context_lost_ = true;
117 if (!context_lost_callback_.is_null()) {
118 context_lost_callback_.Run();
119 }
120 }
121
Initialize(scoped_refptr<gfx::GLSurface> surface,bool is_offscreen,bool use_global_share_group,GLInProcessContext * share_context,gfx::AcceleratedWidget window,const gfx::Size & size,const GLInProcessContextAttribs & attribs,gfx::GpuPreference gpu_preference,const scoped_refptr<InProcessCommandBuffer::Service> & service)122 bool GLInProcessContextImpl::Initialize(
123 scoped_refptr<gfx::GLSurface> surface,
124 bool is_offscreen,
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);
134
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;
147
148 // Chromium-specific attributes
149 const int32 FAIL_IF_MAJOR_PERF_CAVEAT = 0x10002;
150 const int32 LOSE_CONTEXT_WHEN_OUT_OF_MEMORY = 0x10003;
151
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);
156 }
157 if (attribs.blue_size >= 0) {
158 attrib_vector.push_back(BLUE_SIZE);
159 attrib_vector.push_back(attribs.blue_size);
160 }
161 if (attribs.green_size >= 0) {
162 attrib_vector.push_back(GREEN_SIZE);
163 attrib_vector.push_back(attribs.green_size);
164 }
165 if (attribs.red_size >= 0) {
166 attrib_vector.push_back(RED_SIZE);
167 attrib_vector.push_back(attribs.red_size);
168 }
169 if (attribs.depth_size >= 0) {
170 attrib_vector.push_back(DEPTH_SIZE);
171 attrib_vector.push_back(attribs.depth_size);
172 }
173 if (attribs.stencil_size >= 0) {
174 attrib_vector.push_back(STENCIL_SIZE);
175 attrib_vector.push_back(attribs.stencil_size);
176 }
177 if (attribs.samples >= 0) {
178 attrib_vector.push_back(SAMPLES);
179 attrib_vector.push_back(attribs.samples);
180 }
181 if (attribs.sample_buffers >= 0) {
182 attrib_vector.push_back(SAMPLE_BUFFERS);
183 attrib_vector.push_back(attribs.sample_buffers);
184 }
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);
188 }
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);
192 }
193 attrib_vector.push_back(NONE);
194
195 base::Closure wrapped_callback =
196 base::Bind(&GLInProcessContextImpl::OnContextLost, AsWeakPtr());
197 command_buffer_.reset(new InProcessCommandBuffer(service));
198
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();
208 it++) {
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();
213 DCHECK(share_group);
214 DCHECK(share_command_buffer);
215 break;
216 }
217 }
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();
223 DCHECK(share_group);
224 DCHECK(share_command_buffer);
225 }
226
227 if (!command_buffer_->Initialize(surface,
228 is_offscreen,
229 window,
230 size,
231 attrib_vector,
232 gpu_preference,
233 wrapped_callback,
234 share_command_buffer)) {
235 LOG(ERROR) << "Failed to initialize InProcessCommmandBuffer";
236 return false;
237 }
238
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";
243 Destroy();
244 return false;
245 }
246
247 // Create a transfer buffer.
248 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get()));
249
250 bool bind_generates_resources = false;
251
252 // Create the object exposing the OpenGL API.
253 gles2_implementation_.reset(new gles2::GLES2Implementation(
254 gles2_helper_.get(),
255 share_group,
256 transfer_buffer_.get(),
257 bind_generates_resources,
258 attribs.lose_context_when_out_of_memory > 0,
259 command_buffer_.get()));
260
261 if (use_global_share_group) {
262 g_all_shared_contexts.Get().insert(this);
263 scoped_shared_context_lock.reset();
264 }
265
266 if (!gles2_implementation_->Initialize(
267 kStartTransferBufferSize,
268 kMinTransferBufferSize,
269 kMaxTransferBufferSize,
270 gles2::GLES2Implementation::kNoLimit)) {
271 return false;
272 }
273
274 return true;
275 }
276
Destroy()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
283 // share group.
284 gles2_implementation_->Flush();
285
286 gles2_implementation_.reset();
287 }
288
289 transfer_buffer_.reset();
290 gles2_helper_.reset();
291 command_buffer_.reset();
292 }
293
294 #if defined(OS_ANDROID)
295 scoped_refptr<gfx::SurfaceTexture>
GetSurfaceTexture(uint32 stream_id)296 GLInProcessContextImpl::GetSurfaceTexture(uint32 stream_id) {
297 return command_buffer_->GetSurfaceTexture(stream_id);
298 }
299 #endif
300
301 } // anonymous namespace
302
GLInProcessContextAttribs()303 GLInProcessContextAttribs::GLInProcessContextAttribs()
304 : alpha_size(-1),
305 blue_size(-1),
306 green_size(-1),
307 red_size(-1),
308 depth_size(-1),
309 stencil_size(-1),
310 samples(-1),
311 sample_buffers(-1),
312 fail_if_major_perf_caveat(-1),
313 lose_context_when_out_of_memory(-1) {}
314
Create(scoped_refptr<gpu::InProcessCommandBuffer::Service> service,scoped_refptr<gfx::GLSurface> surface,bool is_offscreen,gfx::AcceleratedWidget window,const gfx::Size & size,GLInProcessContext * share_context,bool use_global_share_group,const GLInProcessContextAttribs & attribs,gfx::GpuPreference gpu_preference)315 GLInProcessContext* GLInProcessContext::Create(
316 scoped_refptr<gpu::InProcessCommandBuffer::Service> service,
317 scoped_refptr<gfx::GLSurface> surface,
318 bool is_offscreen,
319 gfx::AcceleratedWidget window,
320 const gfx::Size& size,
321 GLInProcessContext* share_context,
322 bool use_global_share_group,
323 const GLInProcessContextAttribs& attribs,
324 gfx::GpuPreference gpu_preference) {
325 DCHECK(!use_global_share_group || !share_context);
326 if (surface.get()) {
327 DCHECK_EQ(surface->IsOffscreen(), is_offscreen);
328 DCHECK(surface->GetSize() == size);
329 DCHECK_EQ(gfx::kNullAcceleratedWidget, window);
330 }
331
332 scoped_ptr<GLInProcessContextImpl> context(new GLInProcessContextImpl());
333 if (!context->Initialize(surface,
334 is_offscreen,
335 use_global_share_group,
336 share_context,
337 window,
338 size,
339 attribs,
340 gpu_preference,
341 service))
342 return NULL;
343
344 return context.release();
345 }
346
347 } // namespace gpu
348