OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/client/gl_helper.h" | 5 #include "content/common/gpu/client/gl_helper.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/debug/trace_event.h" |
10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
15 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
| 19 #include "base/time.h" |
18 #include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h" | 20 #include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h" |
19 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | 21 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
20 #include "third_party/skia/include/core/SkRegion.h" | 22 #include "third_party/skia/include/core/SkRegion.h" |
21 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
22 #include "ui/gfx/size.h" | 24 #include "ui/gfx/size.h" |
23 #include "ui/gl/gl_bindings.h" | 25 #include "ui/gl/gl_bindings.h" |
24 | 26 |
25 using WebKit::WebGLId; | 27 using WebKit::WebGLId; |
26 using WebKit::WebGraphicsContext3D; | 28 using WebKit::WebGraphicsContext3D; |
27 | 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 const char kGLHelperThreadName[] = "GLHelperThread"; | |
31 | |
32 class GLHelperThread : public base::Thread { | |
33 public: | |
34 GLHelperThread() : base::Thread(kGLHelperThreadName) { | |
35 Start(); | |
36 } | |
37 virtual ~GLHelperThread() { | |
38 Stop(); | |
39 } | |
40 | |
41 private: | |
42 DISALLOW_COPY_AND_ASSIGN(GLHelperThread); | |
43 }; | |
44 | |
45 base::LazyInstance<GLHelperThread> g_gl_helper_thread = | |
46 LAZY_INSTANCE_INITIALIZER; | |
47 | |
48 class ScopedWebGLId { | 32 class ScopedWebGLId { |
49 public: | 33 public: |
50 typedef void (WebGraphicsContext3D::*DeleteFunc)(WebGLId); | 34 typedef void (WebGraphicsContext3D::*DeleteFunc)(WebGLId); |
51 ScopedWebGLId(WebGraphicsContext3D* context, | 35 ScopedWebGLId(WebGraphicsContext3D* context, |
52 WebGLId id, | 36 WebGLId id, |
53 DeleteFunc delete_func) | 37 DeleteFunc delete_func) |
54 : context_(context), | 38 : context_(context), |
55 id_(id), | 39 id_(id), |
56 delete_func_(delete_func) { | 40 delete_func_(delete_func) { |
57 } | 41 } |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 void SignalWaitableEvent(base::WaitableEvent* event) { | 213 void SignalWaitableEvent(base::WaitableEvent* event) { |
230 event->Signal(); | 214 event->Signal(); |
231 } | 215 } |
232 | 216 |
233 } // namespace | 217 } // namespace |
234 | 218 |
235 namespace content { | 219 namespace content { |
236 | 220 |
237 // Implements GLHelper::CropScaleReadbackAndCleanTexture and encapsulates the | 221 // Implements GLHelper::CropScaleReadbackAndCleanTexture and encapsulates the |
238 // data needed for it. | 222 // data needed for it. |
239 class GLHelper::CopyTextureToImpl { | 223 class GLHelper::CopyTextureToImpl : |
| 224 public base::SupportsWeakPtr<GLHelper::CopyTextureToImpl> { |
240 public: | 225 public: |
241 CopyTextureToImpl(WebGraphicsContext3D* context, | 226 CopyTextureToImpl(WebGraphicsContext3D* context, |
242 WebGraphicsContext3D* context_for_thread, | |
243 GLHelper* helper) | 227 GLHelper* helper) |
244 : context_(context), | 228 : context_(context), |
245 context_for_thread_(context_for_thread), | |
246 helper_(helper), | 229 helper_(helper), |
247 flush_(context), | 230 flush_(context), |
248 program_(context, context->createProgram()), | 231 program_(context, context->createProgram()), |
249 vertex_attributes_buffer_(context_, context_->createBuffer()), | 232 vertex_attributes_buffer_(context_, context_->createBuffer()), |
250 flipped_vertex_attributes_buffer_(context_, context_->createBuffer()) { | 233 flipped_vertex_attributes_buffer_(context_, context_->createBuffer()) { |
251 InitBuffer(); | 234 InitBuffer(); |
252 InitProgram(); | 235 InitProgram(); |
253 } | 236 } |
254 ~CopyTextureToImpl() { | 237 ~CopyTextureToImpl() { |
255 CancelRequests(); | 238 CancelRequests(); |
256 DeleteContextForThread(); | |
257 } | 239 } |
258 | 240 |
259 void InitBuffer(); | 241 void InitBuffer(); |
260 void InitProgram(); | 242 void InitProgram(); |
261 | 243 |
262 void CropScaleReadbackAndCleanTexture( | 244 void CropScaleReadbackAndCleanTexture( |
263 WebGLId src_texture, | 245 WebGLId src_texture, |
264 const gfx::Size& src_size, | 246 const gfx::Size& src_size, |
265 const gfx::Rect& src_subrect, | 247 const gfx::Rect& src_subrect, |
266 const gfx::Size& dst_size, | 248 const gfx::Size& dst_size, |
267 unsigned char* out, | 249 unsigned char* out, |
268 const base::Callback<void(bool)>& callback); | 250 const base::Callback<void(bool)>& callback); |
269 | 251 |
270 void ReadbackTextureSync(WebGLId texture, | 252 void ReadbackTextureSync(WebGLId texture, |
271 const gfx::Rect& src_rect, | 253 const gfx::Rect& src_rect, |
272 unsigned char* out); | 254 unsigned char* out); |
273 | 255 |
274 WebKit::WebGLId CopyAndScaleTexture(WebGLId texture, | 256 WebKit::WebGLId CopyAndScaleTexture(WebGLId texture, |
275 const gfx::Size& src_size, | 257 const gfx::Size& src_size, |
276 const gfx::Size& dst_size, | 258 const gfx::Size& dst_size, |
277 bool vertically_flip_texture); | 259 bool vertically_flip_texture); |
278 | 260 |
279 private: | 261 private: |
280 // A single request to CropScaleReadbackAndCleanTexture. | 262 // A single request to CropScaleReadbackAndCleanTexture. |
281 // Thread-safety notes: the main thread creates instances of this class. The | 263 // The main thread can cancel the request, before it's handled by the helper |
282 // main thread can cancel the request, before it's handled by the helper | |
283 // thread, by resetting the texture and pixels fields. Alternatively, the | 264 // thread, by resetting the texture and pixels fields. Alternatively, the |
284 // thread marks that it handles the request by resetting the pixels field | 265 // thread marks that it handles the request by resetting the pixels field |
285 // (meaning it guarantees that the callback with be called). | 266 // (meaning it guarantees that the callback with be called). |
286 // In either case, the callback must be called exactly once, and the texture | 267 // In either case, the callback must be called exactly once, and the texture |
287 // must be deleted by the main thread context. | 268 // must be deleted by the main thread context. |
288 struct Request : public base::RefCountedThreadSafe<Request> { | 269 struct Request { |
289 Request(CopyTextureToImpl* impl, | 270 Request(WebGLId texture_, |
290 WebGLId texture_, | |
291 const gfx::Size& size_, | 271 const gfx::Size& size_, |
292 unsigned char* pixels_, | 272 unsigned char* pixels_, |
293 const base::Callback<void(bool)>& callback_) | 273 const base::Callback<void(bool)>& callback_) |
294 : copy_texture_impl(impl), | 274 : size(size_), |
295 size(size_), | 275 callback(callback_), |
296 callback(callback_), | 276 texture(texture_), |
297 lock(), | 277 pixels(pixels_), |
298 texture(texture_), | 278 buffer(0), |
299 pixels(pixels_) { | 279 query(0) { |
300 } | 280 } |
301 | 281 |
302 // These members are only accessed on the main thread. | |
303 GLHelper::CopyTextureToImpl* copy_texture_impl; | |
304 gfx::Size size; | 282 gfx::Size size; |
305 base::Callback<void(bool)> callback; | 283 base::Callback<void(bool)> callback; |
306 | 284 |
307 // Locks access to below members, which can be accessed on any thread. | |
308 base::Lock lock; | |
309 WebGLId texture; | 285 WebGLId texture; |
310 unsigned char* pixels; | 286 unsigned char* pixels; |
311 | 287 GLuint buffer; |
312 private: | 288 GLuint query; |
313 friend class base::RefCountedThreadSafe<Request>; | |
314 ~Request() {} | |
315 }; | 289 }; |
316 | 290 |
317 // Copies the block of pixels specified with |src_subrect| from |src_texture|, | 291 // Copies the block of pixels specified with |src_subrect| from |src_texture|, |
318 // scales it to |dst_size|, writes it into a texture, and returns its ID. | 292 // scales it to |dst_size|, writes it into a texture, and returns its ID. |
319 // |src_size| is the size of |src_texture|. | 293 // |src_size| is the size of |src_texture|. |
320 WebGLId ScaleTexture(WebGLId src_texture, | 294 WebGLId ScaleTexture(WebGLId src_texture, |
321 const gfx::Size& src_size, | 295 const gfx::Size& src_size, |
322 const gfx::Rect& src_subrect, | 296 const gfx::Rect& src_subrect, |
323 const gfx::Size& dst_size, | 297 const gfx::Size& dst_size, |
324 bool vertically_flip_texture); | 298 bool vertically_flip_texture); |
325 | 299 |
326 // Deletes the context for GLHelperThread. | 300 void ScheduleCheckReadbackDone(); |
327 void DeleteContextForThread(); | 301 void CheckReadbackDone(); |
328 static void ReadBackFramebuffer(scoped_refptr<Request> request, | 302 void FinishRequest(Request* request, bool result); |
329 WebGraphicsContext3D* context, | |
330 scoped_refptr<base::TaskRunner> reply_loop); | |
331 static void ReadBackFramebufferComplete(scoped_refptr<Request> request, | |
332 bool result); | |
333 void FinishRequest(scoped_refptr<Request> request); | |
334 void CancelRequests(); | 303 void CancelRequests(); |
335 | 304 |
336 // Interleaved array of 2-dimentional vertex positions (x, y) and | 305 // Interleaved array of 2-dimentional vertex positions (x, y) and |
337 // 2-dimentional texture coordinates (s, t). | 306 // 2-dimentional texture coordinates (s, t). |
338 static const WebKit::WGC3Dfloat kVertexAttributes[]; | 307 static const WebKit::WGC3Dfloat kVertexAttributes[]; |
339 // Interleaved array of 2-dimensional vertex positions (x, y) and | 308 // Interleaved array of 2-dimensional vertex positions (x, y) and |
340 // 2 dimensional texture coordinates (s, t). | 309 // 2 dimensional texture coordinates (s, t). |
341 static const WebKit::WGC3Dfloat kFlippedVertexAttributes[]; | 310 static const WebKit::WGC3Dfloat kFlippedVertexAttributes[]; |
342 // Shader sources used for GLHelper::CropScaleReadbackAndCleanTexture and | 311 // Shader sources used for GLHelper::CropScaleReadbackAndCleanTexture and |
343 // GLHelper::ReadbackTextureSync | 312 // GLHelper::ReadbackTextureSync |
344 static const WebKit::WGC3Dchar kCopyVertexShader[]; | 313 static const WebKit::WGC3Dchar kCopyVertexShader[]; |
345 static const WebKit::WGC3Dchar kCopyFragmentShader[]; | 314 static const WebKit::WGC3Dchar kCopyFragmentShader[]; |
346 | 315 |
347 WebGraphicsContext3D* context_; | 316 WebGraphicsContext3D* context_; |
348 WebGraphicsContext3D* context_for_thread_; | |
349 GLHelper* helper_; | 317 GLHelper* helper_; |
350 | 318 |
351 // A scoped flush that will ensure all resource deletions are flushed when | 319 // A scoped flush that will ensure all resource deletions are flushed when |
352 // this object is destroyed. Must be declared before other Scoped* fields. | 320 // this object is destroyed. Must be declared before other Scoped* fields. |
353 ScopedFlush flush_; | 321 ScopedFlush flush_; |
354 // A program for copying a source texture into a destination texture. | 322 // A program for copying a source texture into a destination texture. |
355 ScopedProgram program_; | 323 ScopedProgram program_; |
356 // The buffer that holds the vertices and the texture coordinates data for | 324 // The buffer that holds the vertices and the texture coordinates data for |
357 // drawing a quad. | 325 // drawing a quad. |
358 ScopedBuffer vertex_attributes_buffer_; | 326 ScopedBuffer vertex_attributes_buffer_; |
359 ScopedBuffer flipped_vertex_attributes_buffer_; | 327 ScopedBuffer flipped_vertex_attributes_buffer_; |
360 | 328 |
361 // The location of the position in the program. | 329 // The location of the position in the program. |
362 WebKit::WGC3Dint position_location_; | 330 WebKit::WGC3Dint position_location_; |
363 // The location of the texture coordinate in the program. | 331 // The location of the texture coordinate in the program. |
364 WebKit::WGC3Dint texcoord_location_; | 332 WebKit::WGC3Dint texcoord_location_; |
365 // The location of the source texture in the program. | 333 // The location of the source texture in the program. |
366 WebKit::WGC3Dint texture_location_; | 334 WebKit::WGC3Dint texture_location_; |
367 // The location of the texture coordinate of the sub-rectangle in the program. | 335 // The location of the texture coordinate of the sub-rectangle in the program. |
368 WebKit::WGC3Dint src_subrect_location_; | 336 WebKit::WGC3Dint src_subrect_location_; |
369 std::queue<scoped_refptr<Request> > request_queue_; | 337 std::queue<Request*> request_queue_; |
370 }; | 338 }; |
371 | 339 |
372 const WebKit::WGC3Dfloat GLHelper::CopyTextureToImpl::kVertexAttributes[] = { | 340 const WebKit::WGC3Dfloat GLHelper::CopyTextureToImpl::kVertexAttributes[] = { |
373 -1.0f, -1.0f, 0.0f, 0.0f, | 341 -1.0f, -1.0f, 0.0f, 0.0f, |
374 1.0f, -1.0f, 1.0f, 0.0f, | 342 1.0f, -1.0f, 1.0f, 0.0f, |
375 -1.0f, 1.0f, 0.0f, 1.0f, | 343 -1.0f, 1.0f, 0.0f, 1.0f, |
376 1.0f, 1.0f, 1.0f, 1.0f, | 344 1.0f, 1.0f, 1.0f, 1.0f, |
377 }; | 345 }; |
378 | 346 |
379 const WebKit::WGC3Dfloat | 347 const WebKit::WGC3Dfloat |
380 GLHelper::CopyTextureToImpl::kFlippedVertexAttributes[] = { | 348 GLHelper::CopyTextureToImpl::kFlippedVertexAttributes[] = { |
381 -1.0f, -1.0f, 0.0f, 1.0f, | 349 -1.0f, -1.0f, 0.0f, 1.0f, |
382 1.0f, -1.0f, 1.0f, 1.0f, | 350 1.0f, -1.0f, 1.0f, 1.0f, |
383 -1.0f, 1.0f, 0.0f, 0.0f, | 351 -1.0f, 1.0f, 0.0f, 0.0f, |
384 1.0f, 1.0f, 1.0f, 0.0f, | 352 1.0f, 1.0f, 1.0f, 0.0f, |
385 }; | 353 }; |
386 | 354 |
387 const WebKit::WGC3Dchar GLHelper::CopyTextureToImpl::kCopyVertexShader[] = | 355 const WebKit::WGC3Dchar GLHelper::CopyTextureToImpl::kCopyVertexShader[] = |
388 "attribute vec2 a_position;" | 356 "attribute vec2 a_position;" |
389 "attribute vec2 a_texcoord;" | 357 "attribute vec2 a_texcoord;" |
390 "varying vec2 v_texcoord;" | 358 "varying vec2 v_texcoord;" |
391 "uniform vec4 src_subrect;" | 359 "uniform vec4 src_subrect;" |
392 "void main() {" | 360 "void main() {" |
393 " gl_Position = vec4(a_position, 0.0, 1.0);" | 361 " gl_Position = vec4(a_position, 0.0, 1.0);" |
394 " v_texcoord = src_subrect.xy + a_texcoord * src_subrect.zw;" | 362 " v_texcoord = src_subrect.xy + a_texcoord * src_subrect.zw;" |
395 "}"; | 363 "}"; |
396 | 364 |
| 365 |
| 366 #if (SK_R32_SHIFT == 16) && !SK_B32_SHIFT |
397 const WebKit::WGC3Dchar GLHelper::CopyTextureToImpl::kCopyFragmentShader[] = | 367 const WebKit::WGC3Dchar GLHelper::CopyTextureToImpl::kCopyFragmentShader[] = |
398 "precision mediump float;" | 368 "precision mediump float;" |
399 "varying vec2 v_texcoord;" | 369 "varying vec2 v_texcoord;" |
| 370 "uniform sampler2D s_texture;" |
| 371 "void main() {" |
| 372 " gl_FragColor = texture2D(s_texture, v_texcoord).bgra;" |
| 373 "}"; |
| 374 #else |
| 375 const WebKit::WGC3Dchar GLHelper::CopyTextureToImpl::kCopyFragmentShader[] = |
| 376 "precision mediump float;" |
| 377 "varying vec2 v_texcoord;" |
400 "uniform sampler2D s_texture;" | 378 "uniform sampler2D s_texture;" |
401 "void main() {" | 379 "void main() {" |
402 " gl_FragColor = texture2D(s_texture, v_texcoord);" | 380 " gl_FragColor = texture2D(s_texture, v_texcoord);" |
403 "}"; | 381 "}"; |
| 382 #endif |
| 383 |
404 | 384 |
405 void GLHelper::CopyTextureToImpl::InitBuffer() { | 385 void GLHelper::CopyTextureToImpl::InitBuffer() { |
406 ScopedBufferBinder<GL_ARRAY_BUFFER> buffer_binder( | 386 ScopedBufferBinder<GL_ARRAY_BUFFER> buffer_binder( |
407 context_, vertex_attributes_buffer_); | 387 context_, vertex_attributes_buffer_); |
408 context_->bufferData(GL_ARRAY_BUFFER, | 388 context_->bufferData(GL_ARRAY_BUFFER, |
409 sizeof(kVertexAttributes), | 389 sizeof(kVertexAttributes), |
410 kVertexAttributes, | 390 kVertexAttributes, |
411 GL_STATIC_DRAW); | 391 GL_STATIC_DRAW); |
412 ScopedBufferBinder<GL_ARRAY_BUFFER> flipped_buffer_binder( | 392 ScopedBufferBinder<GL_ARRAY_BUFFER> flipped_buffer_binder( |
413 context_, flipped_vertex_attributes_buffer_); | 393 context_, flipped_vertex_attributes_buffer_); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 }; | 490 }; |
511 | 491 |
512 context_->uniform4fv(src_subrect_location_, 1, src_subrect_texcoord); | 492 context_->uniform4fv(src_subrect_location_, 1, src_subrect_texcoord); |
513 | 493 |
514 // Conduct texture mapping by drawing a quad composed of two triangles. | 494 // Conduct texture mapping by drawing a quad composed of two triangles. |
515 context_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); | 495 context_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); |
516 } | 496 } |
517 return dst_texture; | 497 return dst_texture; |
518 } | 498 } |
519 | 499 |
520 void GLHelper::CopyTextureToImpl::DeleteContextForThread() { | |
521 if (!context_for_thread_) | |
522 return; | |
523 | |
524 g_gl_helper_thread.Pointer()->message_loop_proxy()->PostTask( | |
525 FROM_HERE, | |
526 base::Bind(&DeleteContext, | |
527 context_for_thread_)); | |
528 context_for_thread_ = NULL; | |
529 } | |
530 | |
531 void GLHelper::CopyTextureToImpl::CropScaleReadbackAndCleanTexture( | 500 void GLHelper::CopyTextureToImpl::CropScaleReadbackAndCleanTexture( |
532 WebGLId src_texture, | 501 WebGLId src_texture, |
533 const gfx::Size& src_size, | 502 const gfx::Size& src_size, |
534 const gfx::Rect& src_subrect, | 503 const gfx::Rect& src_subrect, |
535 const gfx::Size& dst_size, | 504 const gfx::Size& dst_size, |
536 unsigned char* out, | 505 unsigned char* out, |
537 const base::Callback<void(bool)>& callback) { | 506 const base::Callback<void(bool)>& callback) { |
538 if (!context_for_thread_) { | |
539 callback.Run(false); | |
540 return; | |
541 } | |
542 | |
543 WebGLId texture = ScaleTexture(src_texture, | 507 WebGLId texture = ScaleTexture(src_texture, |
544 src_size, | 508 src_size, |
545 src_subrect, | 509 src_subrect, |
546 dst_size, | 510 dst_size, |
547 false); | 511 #ifdef USE_SKIA |
| 512 true |
| 513 #else |
| 514 false |
| 515 #endif |
| 516 ); |
548 context_->flush(); | 517 context_->flush(); |
549 scoped_refptr<Request> request = | 518 Request *request = new Request(texture, dst_size, out, callback); |
550 new Request(this, texture, dst_size, out, callback); | |
551 request_queue_.push(request); | 519 request_queue_.push(request); |
| 520 if (request_queue_.size() == 1) { |
| 521 ScheduleCheckReadbackDone(); |
| 522 } |
552 | 523 |
553 g_gl_helper_thread.Pointer()->message_loop_proxy()->PostTask(FROM_HERE, | 524 ScopedFlush flush(context_); |
554 base::Bind(&ReadBackFramebuffer, | 525 ScopedFramebuffer dst_framebuffer(context_, context_->createFramebuffer()); |
555 request, | 526 gfx::Size size; |
556 context_for_thread_, | 527 size = request->size; |
557 base::MessageLoopProxy::current())); | 528 |
| 529 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder( |
| 530 context_, dst_framebuffer); |
| 531 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder( |
| 532 context_, request->texture); |
| 533 context_->framebufferTexture2D(GL_FRAMEBUFFER, |
| 534 GL_COLOR_ATTACHMENT0, |
| 535 GL_TEXTURE_2D, |
| 536 request->texture, |
| 537 0); |
| 538 request->buffer = context_->createBuffer(); |
| 539 context_->bindBuffer(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, |
| 540 request->buffer); |
| 541 context_->bufferData(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, |
| 542 4 * size.GetArea(), |
| 543 NULL, |
| 544 GL_STREAM_READ); |
| 545 |
| 546 request->query = context_->createQueryEXT(); |
| 547 context_->beginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM, request->query); |
| 548 context_->readPixels(0, 0, size.width(), size.height(), |
| 549 GL_RGBA, GL_UNSIGNED_BYTE, 0); |
| 550 context_->endQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM); |
| 551 context_->bindBuffer(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, 0); |
| 552 |
558 } | 553 } |
559 | 554 |
560 void GLHelper::CopyTextureToImpl::ReadbackTextureSync(WebGLId texture, | 555 void GLHelper::CopyTextureToImpl::ReadbackTextureSync(WebGLId texture, |
561 const gfx::Rect& src_rect, | 556 const gfx::Rect& src_rect, |
562 unsigned char* out) { | 557 unsigned char* out) { |
563 ScopedFramebuffer dst_framebuffer(context_, context_->createFramebuffer()); | 558 ScopedFramebuffer dst_framebuffer(context_, context_->createFramebuffer()); |
564 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder( | 559 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder( |
565 context_, dst_framebuffer); | 560 context_, dst_framebuffer); |
566 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(context_, texture); | 561 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(context_, texture); |
567 context_->framebufferTexture2D(GL_FRAMEBUFFER, | 562 context_->framebufferTexture2D(GL_FRAMEBUFFER, |
(...skipping 15 matching lines...) Expand all Loading... |
583 const gfx::Size& src_size, | 578 const gfx::Size& src_size, |
584 const gfx::Size& dst_size, | 579 const gfx::Size& dst_size, |
585 bool vertically_flip_texture) { | 580 bool vertically_flip_texture) { |
586 return ScaleTexture(src_texture, | 581 return ScaleTexture(src_texture, |
587 src_size, | 582 src_size, |
588 gfx::Rect(src_size), | 583 gfx::Rect(src_size), |
589 dst_size, | 584 dst_size, |
590 vertically_flip_texture); | 585 vertically_flip_texture); |
591 } | 586 } |
592 | 587 |
593 void GLHelper::CopyTextureToImpl::ReadBackFramebuffer( | 588 |
594 scoped_refptr<Request> request, | 589 void GLHelper::CopyTextureToImpl::ScheduleCheckReadbackDone() { |
595 WebGraphicsContext3D* context, | 590 DCHECK(!request_queue_.empty()); |
596 scoped_refptr<base::TaskRunner> reply_loop) { | 591 base::MessageLoopProxy::current()->PostDelayedTask( |
597 DCHECK(context); | 592 FROM_HERE, |
598 if (!context->makeContextCurrent() || context->isContextLost()) { | 593 base::Bind(&CopyTextureToImpl::CheckReadbackDone, AsWeakPtr()), |
599 base::AutoLock auto_lock(request->lock); | 594 base::TimeDelta::FromMilliseconds(2)); |
600 if (request->pixels) { | |
601 // Only report failure if the request wasn't canceled (otherwise the | |
602 // failure has already been reported). | |
603 request->pixels = NULL; | |
604 reply_loop->PostTask( | |
605 FROM_HERE, base::Bind(ReadBackFramebufferComplete, request, false)); | |
606 } | |
607 return; | |
608 } | |
609 ScopedFlush flush(context); | |
610 ScopedFramebuffer dst_framebuffer(context, context->createFramebuffer()); | |
611 unsigned char* pixels = NULL; | |
612 gfx::Size size; | |
613 { | |
614 // Note: We don't want to keep the lock while doing the readBack (since we | |
615 // don't want to block the UI thread). We rely on the fact that once the | |
616 // texture is bound to a FBO (that isn't current), deleting the texture is | |
617 // delayed until the FBO is deleted. We ensure ordering by flushing while | |
618 // the lock is held. Either the main thread cancelled before we get the | |
619 // lock, and we'll exit early, or we ensure that the texture is bound to the | |
620 // framebuffer before the main thread has a chance to delete it. | |
621 base::AutoLock auto_lock(request->lock); | |
622 if (!request->texture || !request->pixels) | |
623 return; | |
624 pixels = request->pixels; | |
625 request->pixels = NULL; | |
626 size = request->size; | |
627 { | |
628 ScopedFlush flush(context); | |
629 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder( | |
630 context, dst_framebuffer); | |
631 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder( | |
632 context, request->texture); | |
633 context->framebufferTexture2D(GL_FRAMEBUFFER, | |
634 GL_COLOR_ATTACHMENT0, | |
635 GL_TEXTURE_2D, | |
636 request->texture, | |
637 0); | |
638 } | |
639 } | |
640 bool result = context->readBackFramebuffer( | |
641 pixels, | |
642 4 * size.GetArea(), | |
643 dst_framebuffer.id(), | |
644 size.width(), | |
645 size.height()); | |
646 reply_loop->PostTask( | |
647 FROM_HERE, base::Bind(ReadBackFramebufferComplete, request, result)); | |
648 } | 595 } |
649 | 596 |
650 void GLHelper::CopyTextureToImpl::ReadBackFramebufferComplete( | 597 void GLHelper::CopyTextureToImpl::CheckReadbackDone() { |
651 scoped_refptr<Request> request, | 598 TRACE_EVENT0("mirror", |
652 bool result) { | 599 "GLHelper::CopyTextureToImpl::CheckReadBackFramebufferComplete"); |
653 request->callback.Run(result); | 600 while (!request_queue_.empty()) { |
654 if (request->copy_texture_impl) | 601 Request* request = request_queue_.front(); |
655 request->copy_texture_impl->FinishRequest(request); | 602 |
| 603 bool result = false; |
| 604 if (request->buffer != 0) { |
| 605 unsigned int done = 1; |
| 606 context_->getQueryObjectuivEXT(request->query, |
| 607 GL_QUERY_RESULT_AVAILABLE_EXT, &done); |
| 608 if (!done) { |
| 609 ScheduleCheckReadbackDone(); |
| 610 return; |
| 611 } |
| 612 context_->bindBuffer(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, |
| 613 request->buffer); |
| 614 void* data = context_->mapBufferCHROMIUM( |
| 615 GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, GL_READ_ONLY); |
| 616 |
| 617 if (data) { |
| 618 result = true; |
| 619 memcpy(request->pixels, data, request->size.GetArea() * 4); |
| 620 context_->unmapBufferCHROMIUM(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM); |
| 621 } |
| 622 context_->bindBuffer(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, 0); |
| 623 } |
| 624 |
| 625 FinishRequest(request, result); |
| 626 } |
656 } | 627 } |
657 | 628 |
658 void GLHelper::CopyTextureToImpl::FinishRequest( | 629 void GLHelper::CopyTextureToImpl::FinishRequest(Request* request, bool result) { |
659 scoped_refptr<Request> request) { | 630 DCHECK(request_queue_.front() == request); |
660 CHECK(request_queue_.front() == request); | |
661 request_queue_.pop(); | 631 request_queue_.pop(); |
662 base::AutoLock auto_lock(request->lock); | 632 request->callback.Run(result); |
663 if (request->texture != 0) { | 633 if (request->texture != 0) { |
664 context_->deleteTexture(request->texture); | 634 context_->deleteTexture(request->texture); |
665 request->texture = 0; | 635 request->texture = 0; |
666 context_->flush(); | 636 context_->flush(); |
667 } | 637 } |
| 638 if (request->buffer != 0) { |
| 639 context_->deleteBuffer(request->buffer); |
| 640 request->buffer = 0; |
| 641 context_->flush(); |
| 642 } |
| 643 if (request->query != 0) { |
| 644 context_->deleteQueryEXT(request->query); |
| 645 request->query = 0; |
| 646 context_->flush(); |
| 647 } |
| 648 delete request; |
668 } | 649 } |
669 | 650 |
670 void GLHelper::CopyTextureToImpl::CancelRequests() { | 651 void GLHelper::CopyTextureToImpl::CancelRequests() { |
671 while (!request_queue_.empty()) { | 652 while (!request_queue_.empty()) { |
672 scoped_refptr<Request> request = request_queue_.front(); | 653 Request* request = request_queue_.front(); |
673 request_queue_.pop(); | 654 FinishRequest(request, false); |
674 request->copy_texture_impl = NULL; | |
675 bool cancelled = false; | |
676 { | |
677 base::AutoLock auto_lock(request->lock); | |
678 if (request->texture != 0) { | |
679 context_->deleteTexture(request->texture); | |
680 request->texture = 0; | |
681 } | |
682 if (request->pixels != NULL) { | |
683 request->pixels = NULL; | |
684 cancelled = true; | |
685 } | |
686 } | |
687 if (cancelled) | |
688 request->callback.Run(false); | |
689 } | 655 } |
690 } | 656 } |
691 | 657 |
692 base::subtle::Atomic32 GLHelper::count_ = 0; | 658 GLHelper::GLHelper(WebGraphicsContext3D* context) |
693 | 659 : context_(context) { |
694 GLHelper::GLHelper(WebGraphicsContext3D* context, | |
695 WebGraphicsContext3D* context_for_thread) | |
696 : context_(context), | |
697 context_for_thread_(context_for_thread) { | |
698 base::subtle::NoBarrier_AtomicIncrement(&count_, 1); | |
699 } | 660 } |
700 | 661 |
701 GLHelper::~GLHelper() { | 662 GLHelper::~GLHelper() { |
702 DCHECK_NE(MessageLoop::current(), | |
703 g_gl_helper_thread.Pointer()->message_loop()); | |
704 base::subtle::Atomic32 decremented_count = | |
705 base::subtle::NoBarrier_AtomicIncrement(&count_, -1); | |
706 if (decremented_count == 0) { | |
707 // When this is the last instance, we synchronize with the pending | |
708 // operations on GLHelperThread. Otherwise on shutdown we may kill the GPU | |
709 // process infrastructure (BrowserGpuChannelHostFactory) before they have | |
710 // a chance to complete, likely leading to a crash. | |
711 base::WaitableEvent event(false, false); | |
712 g_gl_helper_thread.Pointer()->message_loop_proxy()->PostTask( | |
713 FROM_HERE, | |
714 base::Bind(&SignalWaitableEvent, | |
715 &event)); | |
716 // http://crbug.com/125415 | |
717 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
718 event.Wait(); | |
719 } | |
720 } | 663 } |
721 | 664 |
722 WebGraphicsContext3D* GLHelper::context() const { | 665 WebGraphicsContext3D* GLHelper::context() const { |
723 return context_; | 666 return context_; |
724 } | 667 } |
725 | 668 |
726 void GLHelper::CropScaleReadbackAndCleanTexture( | 669 void GLHelper::CropScaleReadbackAndCleanTexture( |
727 WebGLId src_texture, | 670 WebGLId src_texture, |
728 const gfx::Size& src_size, | 671 const gfx::Size& src_size, |
729 const gfx::Rect& src_subrect, | 672 const gfx::Rect& src_subrect, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 if (!compile_status) { | 722 if (!compile_status) { |
780 LOG(ERROR) << std::string(context_->getShaderInfoLog(shader).utf8()); | 723 LOG(ERROR) << std::string(context_->getShaderInfoLog(shader).utf8()); |
781 return 0; | 724 return 0; |
782 } | 725 } |
783 return shader.Detach(); | 726 return shader.Detach(); |
784 } | 727 } |
785 | 728 |
786 void GLHelper::InitCopyTextToImpl() { | 729 void GLHelper::InitCopyTextToImpl() { |
787 // Lazily initialize |copy_texture_to_impl_| | 730 // Lazily initialize |copy_texture_to_impl_| |
788 if (!copy_texture_to_impl_.get()) | 731 if (!copy_texture_to_impl_.get()) |
789 copy_texture_to_impl_.reset(new CopyTextureToImpl(context_, | 732 copy_texture_to_impl_.reset(new CopyTextureToImpl(context_, this)); |
790 context_for_thread_, | |
791 this)); | |
792 } | 733 } |
793 | 734 |
794 void GLHelper::CopySubBufferDamage(WebKit::WebGLId texture, | 735 void GLHelper::CopySubBufferDamage(WebKit::WebGLId texture, |
795 WebKit::WebGLId previous_texture, | 736 WebKit::WebGLId previous_texture, |
796 const SkRegion& new_damage, | 737 const SkRegion& new_damage, |
797 const SkRegion& old_damage) { | 738 const SkRegion& old_damage) { |
798 SkRegion region(old_damage); | 739 SkRegion region(old_damage); |
799 if (region.op(new_damage, SkRegion::kDifference_Op)) { | 740 if (region.op(new_damage, SkRegion::kDifference_Op)) { |
800 ScopedFramebuffer dst_framebuffer(context_, context_->createFramebuffer()); | 741 ScopedFramebuffer dst_framebuffer(context_, context_->createFramebuffer()); |
801 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder( | 742 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder( |
802 context_, dst_framebuffer); | 743 context_, dst_framebuffer); |
803 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(context_, texture); | 744 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(context_, texture); |
804 context_->framebufferTexture2D(GL_FRAMEBUFFER, | 745 context_->framebufferTexture2D(GL_FRAMEBUFFER, |
805 GL_COLOR_ATTACHMENT0, | 746 GL_COLOR_ATTACHMENT0, |
806 GL_TEXTURE_2D, | 747 GL_TEXTURE_2D, |
807 previous_texture, | 748 previous_texture, |
808 0); | 749 0); |
809 for (SkRegion::Iterator it(region); !it.done(); it.next()) { | 750 for (SkRegion::Iterator it(region); !it.done(); it.next()) { |
810 const SkIRect& rect = it.rect(); | 751 const SkIRect& rect = it.rect(); |
811 context_->copyTexSubImage2D(GL_TEXTURE_2D, 0, | 752 context_->copyTexSubImage2D(GL_TEXTURE_2D, 0, |
812 rect.x(), rect.y(), | 753 rect.x(), rect.y(), |
813 rect.x(), rect.y(), | 754 rect.x(), rect.y(), |
814 rect.width(), rect.height()); | 755 rect.width(), rect.height()); |
815 } | 756 } |
816 context_->flush(); | 757 context_->flush(); |
817 } | 758 } |
818 } | 759 } |
819 | 760 |
820 } // namespace content | 761 } // namespace content |
OLD | NEW |