1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/gpu/client/gl_helper.h"
#include <queue>
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "ui/gfx/size.h"
#include "ui/gl/gl_bindings.h"
using WebKit::WebGLId;
using WebKit::WebGraphicsContext3D;
namespace {
const char kGLHelperThreadName[] = "GLHelperThread";
class GLHelperThread : public base::Thread {
public:
GLHelperThread() : base::Thread(kGLHelperThreadName) {
Start();
}
virtual ~GLHelperThread() {}
DISALLOW_COPY_AND_ASSIGN(GLHelperThread);
};
base::LazyInstance<GLHelperThread> g_gl_helper_thread =
LAZY_INSTANCE_INITIALIZER;
class ScopedWebGLId {
public:
typedef void (WebGraphicsContext3D::*DeleteFunc)(WebGLId);
ScopedWebGLId(WebGraphicsContext3D* context,
WebGLId id,
DeleteFunc delete_func)
: context_(context),
id_(id),
delete_func_(delete_func) {
}
operator WebGLId() const {
return id_;
}
WebGLId id() const { return id_; }
WebGLId Detach() {
WebGLId id = id_;
id_ = 0;
return id;
}
virtual ~ScopedWebGLId() {
if (id_ != 0)
(context_->*delete_func_)(id_);
}
private:
WebGraphicsContext3D* context_;
WebGLId id_;
DeleteFunc delete_func_;
DISALLOW_COPY_AND_ASSIGN(ScopedWebGLId);
};
class ScopedBuffer : public ScopedWebGLId {
public:
ScopedBuffer(WebGraphicsContext3D* context,
WebGLId id)
: ScopedWebGLId(context,
id,
&WebGraphicsContext3D::deleteBuffer) {}
};
class ScopedFramebuffer : public ScopedWebGLId {
public:
ScopedFramebuffer(WebGraphicsContext3D* context,
WebGLId id)
: ScopedWebGLId(context,
id,
&WebGraphicsContext3D::deleteFramebuffer) {}
};
class ScopedRenderbuffer : public ScopedWebGLId {
public:
ScopedRenderbuffer(WebGraphicsContext3D* context,
WebGLId id)
: ScopedWebGLId(context,
id,
&WebGraphicsContext3D::deleteRenderbuffer) {}
};
class ScopedProgram : public ScopedWebGLId {
public:
ScopedProgram(WebGraphicsContext3D* context,
WebGLId id)
: ScopedWebGLId(context,
id,
&WebGraphicsContext3D::deleteProgram) {}
};
class ScopedShader : public ScopedWebGLId {
public:
ScopedShader(WebGraphicsContext3D* context,
WebGLId id)
: ScopedWebGLId(context,
id,
&WebGraphicsContext3D::deleteShader) {}
};
class ScopedTexture : public ScopedWebGLId {
public:
ScopedTexture(WebGraphicsContext3D* context,
WebGLId id)
: ScopedWebGLId(context,
id,
&WebGraphicsContext3D::deleteTexture) {}
};
template <WebKit::WGC3Denum target>
class ScopedBinder {
public:
typedef void (WebGraphicsContext3D::*BindFunc)(WebKit::WGC3Denum,
WebGLId);
ScopedBinder(WebGraphicsContext3D* context,
WebGLId id,
BindFunc bind_func)
: context_(context),
id_(id),
bind_func_(bind_func) {
(context_->*bind_func_)(target, id_);
}
virtual ~ScopedBinder() {
if (id_ != 0)
(context_->*bind_func_)(target, 0);
}
private:
WebGraphicsContext3D* context_;
WebGLId id_;
BindFunc bind_func_;
DISALLOW_COPY_AND_ASSIGN(ScopedBinder);
};
template <WebKit::WGC3Denum target>
class ScopedBufferBinder : ScopedBinder<target> {
public:
ScopedBufferBinder(WebGraphicsContext3D* context,
WebGLId id)
: ScopedBinder<target>(
context,
id,
&WebGraphicsContext3D::bindBuffer) {}
};
template <WebKit::WGC3Denum target>
class ScopedFramebufferBinder : ScopedBinder<target> {
public:
ScopedFramebufferBinder(WebGraphicsContext3D* context,
WebGLId id)
: ScopedBinder<target>(
context,
id,
&WebGraphicsContext3D::bindFramebuffer) {}
};
template <WebKit::WGC3Denum target>
class ScopedRenderbufferBinder : ScopedBinder<target> {
public:
ScopedRenderbufferBinder(WebGraphicsContext3D* context,
WebGLId id)
: ScopedBinder<target>(
context,
id,
&WebGraphicsContext3D::bindRenderbuffer) {}
};
template <WebKit::WGC3Denum target>
class ScopedTextureBinder : ScopedBinder<target> {
public:
ScopedTextureBinder(WebGraphicsContext3D* context,
WebGLId id)
: ScopedBinder<target>(
context,
id,
&WebGraphicsContext3D::bindTexture) {}
};
class ScopedFlush {
public:
ScopedFlush(WebGraphicsContext3D* context)
: context_(context) {
}
virtual ~ScopedFlush() {
context_->flush();
}
private:
WebGraphicsContext3D* context_;
DISALLOW_COPY_AND_ASSIGN(ScopedFlush);
};
void DeleteContext(WebGraphicsContext3D* context) {
delete context;
}
void SignalWaitableEvent(base::WaitableEvent* event) {
event->Signal();
}
} // namespace
namespace content {
// Implements GLHelper::CopyTextureTo and encapsulates the data needed for it.
class GLHelper::CopyTextureToImpl {
public:
CopyTextureToImpl(WebGraphicsContext3D* context,
WebGraphicsContext3D* context_for_thread,
GLHelper* helper)
: context_(context),
context_for_thread_(context_for_thread),
helper_(helper),
flush_(context),
program_(context, context->createProgram()),
vertex_attributes_buffer_(context_, context_->createBuffer()) {
InitBuffer();
InitProgram();
}
~CopyTextureToImpl() {
CancelRequests();
DeleteContextForThread();
}
void InitBuffer();
void InitProgram();
void CopyTextureTo(WebGLId src_texture,
const gfx::Size& src_size,
const gfx::Size& dst_size,
unsigned char* out,
const base::Callback<void(bool)>& callback);
private:
// A single request to CopyTextureTo.
// Thread-safety notes: the main thread creates instances of this class. The
// main thread can cancel the request, before it's handled by the helper
// thread, by resetting the texture and pixels fields. Alternatively, the
// thread marks that it handles the request by resetting the pixels field
// (meaning it guarantees that the callback with be called).
// In either case, the callback must be called exactly once, and the texture
// must be deleted by the main thread context.
struct Request : public base::RefCounted<Request> {
Request(CopyTextureToImpl* impl,
WebGLId texture_,
const gfx::Size& size_,
unsigned char* pixels_,
const base::Callback<void(bool)>& callback_)
: copy_texture_impl(impl),
size(size_),
callback(callback_),
lock(),
texture(texture_),
pixels(pixels_) {
}
// These members are only accessed on the main thread.
GLHelper::CopyTextureToImpl* copy_texture_impl;
gfx::Size size;
base::Callback<void(bool)> callback;
// Locks access to below members, which can be accessed on any thread.
base::Lock lock;
WebGLId texture;
unsigned char* pixels;
private:
friend class base::RefCounted<Request>;
~Request() {}
};
// Returns the id of a framebuffer that
WebGLId ScaleTexture(WebGLId src_texture,
const gfx::Size& src_size,
const gfx::Size& dst_size);
// Deletes the context for GLHelperThread.
void DeleteContextForThread();
static void ReadBackFramebuffer(scoped_refptr<Request> request,
WebGraphicsContext3D* context,
scoped_refptr<base::TaskRunner> reply_loop);
static void ReadBackFramebufferComplete(scoped_refptr<Request> request,
bool result);
void FinishRequest(scoped_refptr<Request> request);
void CancelRequests();
// Interleaved array of 2-dimentional vertex positions (x, y) and
// 2-dimentional texture coordinates (s, t).
static const WebKit::WGC3Dfloat kVertexAttributes[];
// Shader sources used for GLHelper::CopyTextureTo
static const WebKit::WGC3Dchar kCopyVertexShader[];
static const WebKit::WGC3Dchar kCopyFragmentShader[];
WebGraphicsContext3D* context_;
WebGraphicsContext3D* context_for_thread_;
GLHelper* helper_;
// A scoped flush that will ensure all resource deletions are flushed when
// this object is destroyed. Must be declared before other Scoped* fields.
ScopedFlush flush_;
// A program for copying a source texture into a destination texture.
ScopedProgram program_;
// The buffer that holds the vertices and the texture coordinates data for
// drawing a quad.
ScopedBuffer vertex_attributes_buffer_;
// The location of the position in the program.
WebKit::WGC3Dint position_location_;
// The location of the texture coordinate in the program.
WebKit::WGC3Dint texcoord_location_;
// The location of the source texture in the program.
WebKit::WGC3Dint texture_location_;
std::queue<scoped_refptr<Request> > request_queue_;
};
const WebKit::WGC3Dfloat GLHelper::CopyTextureToImpl::kVertexAttributes[] = {
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};
const WebKit::WGC3Dchar GLHelper::CopyTextureToImpl::kCopyVertexShader[] =
"attribute vec2 a_position;"
"attribute vec2 a_texcoord;"
"varying vec2 v_texcoord;"
"void main() {"
" gl_Position = vec4(a_position, 0.0, 1.0);"
" v_texcoord = a_texcoord;"
"}";
const WebKit::WGC3Dchar GLHelper::CopyTextureToImpl::kCopyFragmentShader[] =
"precision mediump float;"
"varying vec2 v_texcoord;"
"uniform sampler2D s_texture;"
"void main() {"
" gl_FragColor = texture2D(s_texture, v_texcoord);"
"}";
void GLHelper::CopyTextureToImpl::InitBuffer() {
ScopedBufferBinder<GL_ARRAY_BUFFER> buffer_binder(
context_, vertex_attributes_buffer_);
context_->bufferData(GL_ARRAY_BUFFER,
sizeof(kVertexAttributes),
kVertexAttributes,
GL_STATIC_DRAW);
}
void GLHelper::CopyTextureToImpl::InitProgram() {
// Shaders to map the source texture to |dst_texture_|.
ScopedShader vertex_shader(context_, helper_->CompileShaderFromSource(
kCopyVertexShader, GL_VERTEX_SHADER));
DCHECK_NE(0U, vertex_shader.id());
context_->attachShader(program_, vertex_shader);
ScopedShader fragment_shader(context_, helper_->CompileShaderFromSource(
kCopyFragmentShader, GL_FRAGMENT_SHADER));
DCHECK_NE(0U, fragment_shader.id());
context_->attachShader(program_, fragment_shader);
context_->linkProgram(program_);
WebKit::WGC3Dint link_status = 0;
context_->getProgramiv(program_, GL_LINK_STATUS, &link_status);
if (!link_status) {
LOG(ERROR) << std::string(context_->getProgramInfoLog(program_).utf8());
return;
}
position_location_ = context_->getAttribLocation(program_, "a_position");
texcoord_location_ = context_->getAttribLocation(program_, "a_texcoord");
texture_location_ = context_->getUniformLocation(program_, "s_texture");
}
WebGLId GLHelper::CopyTextureToImpl::ScaleTexture(
WebGLId src_texture,
const gfx::Size& src_size,
const gfx::Size& dst_size) {
WebGLId dst_texture = context_->createTexture();
{
ScopedFramebuffer dst_framebuffer(context_, context_->createFramebuffer());
ScopedFramebufferBinder<GL_DRAW_FRAMEBUFFER> framebuffer_binder(
context_, dst_framebuffer);
{
ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(
context_, dst_texture);
context_->texImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
dst_size.width(),
dst_size.height(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
NULL);
context_->framebufferTexture2D(GL_DRAW_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
dst_texture,
0);
}
ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(context_, src_texture);
ScopedBufferBinder<GL_ARRAY_BUFFER> buffer_binder(
context_, vertex_attributes_buffer_);
context_->viewport(0, 0, dst_size.width(), dst_size.height());
context_->useProgram(program_);
WebKit::WGC3Dintptr offset = 0;
context_->vertexAttribPointer(position_location_,
2,
GL_FLOAT,
GL_FALSE,
4 * sizeof(WebKit::WGC3Dfloat),
offset);
context_->enableVertexAttribArray(position_location_);
offset += 2 * sizeof(WebKit::WGC3Dfloat);
context_->vertexAttribPointer(texcoord_location_,
2,
GL_FLOAT,
GL_FALSE,
4 * sizeof(WebKit::WGC3Dfloat),
offset);
context_->enableVertexAttribArray(texcoord_location_);
context_->uniform1i(texture_location_, 0);
// Conduct texture mapping by drawing a quad composed of two triangles.
context_->drawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
return dst_texture;
}
void GLHelper::CopyTextureToImpl::DeleteContextForThread() {
if (!context_for_thread_)
return;
g_gl_helper_thread.Pointer()->message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&DeleteContext,
context_for_thread_));
context_for_thread_ = NULL;
}
void GLHelper::CopyTextureToImpl::CopyTextureTo(
WebGLId src_texture,
const gfx::Size& src_size,
const gfx::Size& dst_size,
unsigned char* out,
const base::Callback<void(bool)>& callback) {
if (!context_for_thread_) {
callback.Run(false);
return;
}
WebGLId texture = ScaleTexture(src_texture, src_size, dst_size);
context_->flush();
scoped_refptr<Request> request =
new Request(this, texture, dst_size, out, callback);
request_queue_.push(request);
g_gl_helper_thread.Pointer()->message_loop_proxy()->PostTask(FROM_HERE,
base::Bind(&ReadBackFramebuffer,
request,
context_for_thread_,
base::MessageLoopProxy::current()));
}
void GLHelper::CopyTextureToImpl::ReadBackFramebuffer(
scoped_refptr<Request> request,
WebGraphicsContext3D* context,
scoped_refptr<base::TaskRunner> reply_loop) {
DCHECK(context);
if (!context->makeContextCurrent() || context->isContextLost()) {
base::AutoLock auto_lock(request->lock);
if (request->pixels) {
// Only report failure if the request wasn't canceled (otherwise the
// failure has already been reported).
request->pixels = NULL;
reply_loop->PostTask(
FROM_HERE, base::Bind(ReadBackFramebufferComplete, request, false));
}
return;
}
ScopedFlush flush(context);
ScopedFramebuffer dst_framebuffer(context, context->createFramebuffer());
unsigned char* pixels = NULL;
gfx::Size size;
{
// Note: We don't want to keep the lock while doing the readBack (since we
// don't want to block the UI thread). We rely on the fact that once the
// texture is bound to a FBO (that isn't current), deleting the texture is
// delayed until the FBO is deleted. We ensure ordering by flushing while
// the lock is held. Either the main thread cancelled before we get the
// lock, and we'll exit early, or we ensure that the texture is bound to the
// framebuffer before the main thread has a chance to delete it.
base::AutoLock auto_lock(request->lock);
if (!request->texture || !request->pixels)
return;
pixels = request->pixels;
request->pixels = NULL;
size = request->size;
{
ScopedFlush flush(context);
ScopedFramebufferBinder<GL_DRAW_FRAMEBUFFER> framebuffer_binder(
context, dst_framebuffer);
ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(
context, request->texture);
context->framebufferTexture2D(GL_DRAW_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
request->texture,
0);
}
}
bool result = context->readBackFramebuffer(
pixels,
4 * size.GetArea(),
dst_framebuffer.id(),
size.width(),
size.height());
reply_loop->PostTask(
FROM_HERE, base::Bind(ReadBackFramebufferComplete, request, result));
}
void GLHelper::CopyTextureToImpl::ReadBackFramebufferComplete(
scoped_refptr<Request> request,
bool result) {
request->callback.Run(result);
if (request->copy_texture_impl)
request->copy_texture_impl->FinishRequest(request);
}
void GLHelper::CopyTextureToImpl::FinishRequest(
scoped_refptr<Request> request) {
CHECK(request_queue_.front() == request);
request_queue_.pop();
base::AutoLock auto_lock(request->lock);
if (request->texture != 0) {
context_->deleteTexture(request->texture);
request->texture = 0;
context_->flush();
}
}
void GLHelper::CopyTextureToImpl::CancelRequests() {
while (!request_queue_.empty()) {
scoped_refptr<Request> request = request_queue_.front();
request_queue_.pop();
request->copy_texture_impl = NULL;
bool cancelled = false;
{
base::AutoLock auto_lock(request->lock);
if (request->texture != 0) {
context_->deleteTexture(request->texture);
request->texture = 0;
}
if (request->pixels != NULL) {
request->pixels = NULL;
cancelled = true;
}
}
if (cancelled)
request->callback.Run(false);
}
}
base::subtle::Atomic32 GLHelper::count_ = 0;
GLHelper::GLHelper(WebGraphicsContext3D* context,
WebGraphicsContext3D* context_for_thread)
: context_(context),
context_for_thread_(context_for_thread) {
base::subtle::NoBarrier_AtomicIncrement(&count_, 1);
}
GLHelper::~GLHelper() {
DCHECK_NE(MessageLoop::current(),
g_gl_helper_thread.Pointer()->message_loop());
base::subtle::Atomic32 decremented_count =
base::subtle::NoBarrier_AtomicIncrement(&count_, -1);
if (decremented_count == 0) {
// When this is the last instance, we synchronize with the pending
// operations on GLHelperThread. Otherwise on shutdown we may kill the GPU
// process infrastructure (BrowserGpuChannelHostFactory) before they have
// a chance to complete, likely leading to a crash.
base::WaitableEvent event(false, false);
g_gl_helper_thread.Pointer()->message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&SignalWaitableEvent,
&event));
// http://crbug.com/125415
base::ThreadRestrictions::ScopedAllowWait allow_wait;
event.Wait();
}
}
WebGraphicsContext3D* GLHelper::context() const {
return context_;
}
void GLHelper::CopyTextureTo(WebGLId src_texture,
const gfx::Size& src_size,
const gfx::Size& dst_size,
unsigned char* out,
const base::Callback<void(bool)>& callback) {
// Lazily initialize |copy_texture_to_impl_|
if (!copy_texture_to_impl_.get())
copy_texture_to_impl_.reset(new CopyTextureToImpl(context_,
context_for_thread_,
this));
copy_texture_to_impl_->CopyTextureTo(src_texture,
src_size,
dst_size,
out,
callback);
}
WebGLId GLHelper::CompileShaderFromSource(
const WebKit::WGC3Dchar* source,
WebKit::WGC3Denum type) {
ScopedShader shader(context_, context_->createShader(type));
context_->shaderSource(shader, source);
context_->compileShader(shader);
WebKit::WGC3Dint compile_status = 0;
context_->getShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (!compile_status) {
LOG(ERROR) << std::string(context_->getShaderInfoLog(shader).utf8());
return 0;
}
return shader.Detach();
}
} // namespace content
|