Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: cc/gl_renderer.cc

Issue 12212007: cc: Route offscreen context creation for compositor to the browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove the GrContextProvider::ScopedContexts guard classes Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2010 The Chromium Authors. All rights reserved. 1 // Copyright 2010 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 "cc/gl_renderer.h" 5 #include "cc/gl_renderer.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/debug/trace_event.h" 11 #include "base/debug/trace_event.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/string_split.h" 13 #include "base/string_split.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "cc/compositor_frame.h" 16 #include "cc/compositor_frame.h"
17 #include "cc/compositor_frame_metadata.h" 17 #include "cc/compositor_frame_metadata.h"
18 #include "cc/damage_tracker.h" 18 #include "cc/damage_tracker.h"
19 #include "cc/geometry_binding.h" 19 #include "cc/geometry_binding.h"
20 #include "cc/gl_frame_data.h" 20 #include "cc/gl_frame_data.h"
21 #include "cc/grcontext_provider.h"
21 #include "cc/layer_quad.h" 22 #include "cc/layer_quad.h"
22 #include "cc/math_util.h" 23 #include "cc/math_util.h"
23 #include "cc/priority_calculator.h" 24 #include "cc/priority_calculator.h"
24 #include "cc/proxy.h" 25 #include "cc/proxy.h"
25 #include "cc/render_pass.h" 26 #include "cc/render_pass.h"
26 #include "cc/render_surface_filters.h" 27 #include "cc/render_surface_filters.h"
27 #include "cc/scoped_resource.h" 28 #include "cc/scoped_resource.h"
28 #include "cc/single_thread_proxy.h" 29 #include "cc/single_thread_proxy.h"
29 #include "cc/stream_video_draw_quad.h" 30 #include "cc/stream_video_draw_quad.h"
30 #include "cc/texture_draw_quad.h" 31 #include "cc/texture_draw_quad.h"
31 #include "cc/video_layer_impl.h" 32 #include "cc/video_layer_impl.h"
32 #include "gpu/GLES2/gl2extchromium.h" 33 #include "gpu/GLES2/gl2extchromium.h"
33 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h" 34 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h"
34 #include "third_party/WebKit/Source/Platform/chromium/public/WebSharedGraphicsCo ntext3D.h"
35 #include "third_party/khronos/GLES2/gl2.h" 35 #include "third_party/khronos/GLES2/gl2.h"
36 #include "third_party/khronos/GLES2/gl2ext.h" 36 #include "third_party/khronos/GLES2/gl2ext.h"
37 #include "third_party/skia/include/core/SkBitmap.h" 37 #include "third_party/skia/include/core/SkBitmap.h"
38 #include "third_party/skia/include/core/SkColor.h" 38 #include "third_party/skia/include/core/SkColor.h"
39 #include "third_party/skia/include/gpu/GrContext.h" 39 #include "third_party/skia/include/gpu/GrContext.h"
40 #include "third_party/skia/include/gpu/GrTexture.h" 40 #include "third_party/skia/include/gpu/GrTexture.h"
41 #include "third_party/skia/include/gpu/SkGpuDevice.h" 41 #include "third_party/skia/include/gpu/SkGpuDevice.h"
42 #include "third_party/skia/include/gpu/SkGrTexturePixelRef.h" 42 #include "third_party/skia/include/gpu/SkGrTexturePixelRef.h"
43 #include "ui/gfx/quad_f.h" 43 #include "ui/gfx/quad_f.h"
44 #include "ui/gfx/rect_conversions.h" 44 #include "ui/gfx/rect_conversions.h"
45 45
46 using WebKit::WebGraphicsContext3D; 46 using WebKit::WebGraphicsContext3D;
47 using WebKit::WebGraphicsMemoryAllocation; 47 using WebKit::WebGraphicsMemoryAllocation;
48 using WebKit::WebSharedGraphicsContext3D;
49 48
50 namespace cc { 49 namespace cc {
51 50
52 namespace { 51 namespace {
53 52
54 // TODO(epenner): This should probably be moved to output surface. 53 // TODO(epenner): This should probably be moved to output surface.
55 // 54 //
56 // This implements a simple fence based on client side swaps. 55 // This implements a simple fence based on client side swaps.
57 // This is to isolate the ResourceProvider from 'frames' which 56 // This is to isolate the ResourceProvider from 'frames' which
58 // it shouldn't need to care about, while still allowing us to 57 // it shouldn't need to care about, while still allowing us to
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 m_capabilities.maxTextureSize = m_resourceProvider->maxTextureSize(); 147 m_capabilities.maxTextureSize = m_resourceProvider->maxTextureSize();
149 m_capabilities.bestTextureFormat = m_resourceProvider->bestTextureFormat(); 148 m_capabilities.bestTextureFormat = m_resourceProvider->bestTextureFormat();
150 149
151 // The updater can access textures while the GLRenderer is using them. 150 // The updater can access textures while the GLRenderer is using them.
152 m_capabilities.allowPartialTextureUpdates = true; 151 m_capabilities.allowPartialTextureUpdates = true;
153 152
154 // Check for texture fast paths. Currently we always use MO8 textures, 153 // Check for texture fast paths. Currently we always use MO8 textures,
155 // so we only need to avoid POT textures if we have an NPOT fast-path. 154 // so we only need to avoid POT textures if we have an NPOT fast-path.
156 m_capabilities.avoidPow2Textures = extensions.count("GL_CHROMIUM_fast_NPOT_M O8_textures"); 155 m_capabilities.avoidPow2Textures = extensions.count("GL_CHROMIUM_fast_NPOT_M O8_textures");
157 156
157 m_capabilities.usingOffscreenContext3d = true;
158
158 m_isUsingBindUniform = extensions.count("GL_CHROMIUM_bind_uniform_location") ; 159 m_isUsingBindUniform = extensions.count("GL_CHROMIUM_bind_uniform_location") ;
159 160
160 // Make sure scissoring starts as disabled. 161 // Make sure scissoring starts as disabled.
161 GLC(m_context, m_context->disable(GL_SCISSOR_TEST)); 162 GLC(m_context, m_context->disable(GL_SCISSOR_TEST));
162 DCHECK(!m_isScissorEnabled); 163 DCHECK(!m_isScissorEnabled);
163 164
164 if (!initializeSharedObjects()) 165 if (!initializeSharedObjects())
165 return false; 166 return false;
166 167
167 // Make sure the viewport and context gets initialized, even if it is to zer o. 168 // Make sure the viewport and context gets initialized, even if it is to zer o.
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 float alpha = SkColorGetA(color) / 255.0; 366 float alpha = SkColorGetA(color) / 255.0;
366 367
367 GLC(context(), context()->uniform4f(program->fragmentShader().colorLocation( ), (SkColorGetR(color) / 255.0) * alpha, (SkColorGetG(color) / 255.0) * alpha, ( SkColorGetB(color) / 255.0) * alpha, alpha)); 368 GLC(context(), context()->uniform4f(program->fragmentShader().colorLocation( ), (SkColorGetR(color) / 255.0) * alpha, (SkColorGetG(color) / 255.0) * alpha, ( SkColorGetB(color) / 255.0) * alpha, alpha));
368 369
369 GLC(context(), context()->lineWidth(quad->width)); 370 GLC(context(), context()->lineWidth(quad->width));
370 371
371 // The indices for the line are stored in the same array as the triangle ind ices. 372 // The indices for the line are stored in the same array as the triangle ind ices.
372 GLC(context(), context()->drawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_SHORT, 0 )); 373 GLC(context(), context()->drawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_SHORT, 0 ));
373 } 374 }
374 375
375 static WebGraphicsContext3D* getFilterContext(bool hasImplThread) 376 static inline SkBitmap applyFilters(GLRenderer* renderer, const WebKit::WebFilte rOperations& filters, ScopedResource* sourceTextureResource)
376 {
377 if (hasImplThread)
378 return WebSharedGraphicsContext3D::compositorThreadContext();
379 else
380 return WebSharedGraphicsContext3D::mainThreadContext();
381 }
382
383 static GrContext* getFilterGrContext(bool hasImplThread)
384 {
385 if (hasImplThread)
386 return WebSharedGraphicsContext3D::compositorThreadGrContext();
387 else
388 return WebSharedGraphicsContext3D::mainThreadGrContext();
389 }
390
391 static inline SkBitmap applyFilters(GLRenderer* renderer, const WebKit::WebFilte rOperations& filters, ScopedResource* sourceTexture, bool hasImplThread)
392 { 377 {
393 if (filters.isEmpty()) 378 if (filters.isEmpty())
394 return SkBitmap(); 379 return SkBitmap();
395 380
396 WebGraphicsContext3D* filterContext = getFilterContext(hasImplThread); 381 GrContextProvider* offscreenContexts = renderer->resourceProvider()->offscre enContextProvider();
397 GrContext* filterGrContext = getFilterGrContext(hasImplThread); 382 if (!offscreenContexts->has_contexts())
398
399 if (!filterContext || !filterGrContext)
400 return SkBitmap(); 383 return SkBitmap();
401 384
402 renderer->context()->flush(); 385 ResourceProvider::ScopedWriteLockGL lock(renderer->resourceProvider(), sourc eTextureResource->id());
403 386
404 ResourceProvider::ScopedWriteLockGL lock(renderer->resourceProvider(), sourc eTexture->id()); 387 // Do this after locking/creating the compositor texture.
405 SkBitmap source = RenderSurfaceFilters::apply(filters, lock.textureId(), sou rceTexture->size(), filterContext, filterGrContext); 388 offscreenContexts->BeginUsingContexts();
389
390 SkBitmap source = RenderSurfaceFilters::apply(filters, lock.textureId(), sou rceTextureResource->size(), offscreenContexts->gr_context());
391
392 offscreenContexts->FinishUsingContexts();
406 return source; 393 return source;
407 } 394 }
408 395
409 static SkBitmap applyImageFilter(GLRenderer* renderer, SkImageFilter* filter, Sc opedResource* sourceTexture, bool hasImplThread) 396 static SkBitmap applyImageFilter(GLRenderer* renderer, SkImageFilter* filter, Sc opedResource* sourceTextureResource)
410 { 397 {
411 if (!filter) 398 if (!filter)
412 return SkBitmap(); 399 return SkBitmap();
413 400
414 WebGraphicsContext3D* context3d = getFilterContext(hasImplThread); 401 GrContextProvider* offscreenContexts = renderer->resourceProvider()->offscre enContextProvider();
415 GrContext* grContext = getFilterGrContext(hasImplThread); 402 if (!offscreenContexts->has_contexts())
416
417 if (!context3d || !grContext)
418 return SkBitmap(); 403 return SkBitmap();
419 404
420 renderer->context()->flush(); 405 ResourceProvider::ScopedWriteLockGL lock(renderer->resourceProvider(), sourc eTextureResource->id());
421 406
422 ResourceProvider::ScopedWriteLockGL lock(renderer->resourceProvider(), sourc eTexture->id()); 407 // Do this after locking/creating the compositor texture.
408 offscreenContexts->BeginUsingContexts();
Stephen White 2013/02/13 15:26:06 This is still somewhat mysterious to someone who d
423 409
424 // Wrap the source texture in a Ganesh platform texture. 410 // Wrap the source texture in a Ganesh platform texture.
425 GrBackendTextureDesc backendTextureDescription; 411 GrBackendTextureDesc backendTextureDescription;
426 backendTextureDescription.fWidth = sourceTexture->size().width(); 412 backendTextureDescription.fWidth = sourceTextureResource->size().width();
427 backendTextureDescription.fHeight = sourceTexture->size().height(); 413 backendTextureDescription.fHeight = sourceTextureResource->size().height();
428 backendTextureDescription.fConfig = kSkia8888_GrPixelConfig; 414 backendTextureDescription.fConfig = kSkia8888_GrPixelConfig;
429 backendTextureDescription.fTextureHandle = lock.textureId(); 415 backendTextureDescription.fTextureHandle = lock.textureId();
430 backendTextureDescription.fOrigin = kTopLeft_GrSurfaceOrigin; 416 backendTextureDescription.fOrigin = kTopLeft_GrSurfaceOrigin;
431 skia::RefPtr<GrTexture> texture = skia::AdoptRef(grContext->wrapBackendTextu re(backendTextureDescription)); 417 skia::RefPtr<GrTexture> texture = skia::AdoptRef(offscreenContexts->gr_conte xt()->wrapBackendTexture(backendTextureDescription));
432 418
433 // Place the platform texture inside an SkBitmap. 419 // Place the platform texture inside an SkBitmap.
434 SkBitmap source; 420 SkBitmap source;
435 source.setConfig(SkBitmap::kARGB_8888_Config, sourceTexture->size().width(), sourceTexture->size().height()); 421 source.setConfig(SkBitmap::kARGB_8888_Config, sourceTextureResource->size(). width(), sourceTextureResource->size().height());
436 skia::RefPtr<SkGrPixelRef> pixelRef = skia::AdoptRef(new SkGrPixelRef(textur e.get())); 422 skia::RefPtr<SkGrPixelRef> pixelRef = skia::AdoptRef(new SkGrPixelRef(textur e.get()));
437 source.setPixelRef(pixelRef.get()); 423 source.setPixelRef(pixelRef.get());
438 424
439 // Create a scratch texture for backing store. 425 // Create a scratch texture for backing store.
440 GrTextureDesc desc; 426 GrTextureDesc desc;
441 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; 427 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
442 desc.fSampleCnt = 0; 428 desc.fSampleCnt = 0;
443 desc.fWidth = source.width(); 429 desc.fWidth = source.width();
444 desc.fHeight = source.height(); 430 desc.fHeight = source.height();
445 desc.fConfig = kSkia8888_GrPixelConfig; 431 desc.fConfig = kSkia8888_GrPixelConfig;
446 desc.fOrigin = kTopLeft_GrSurfaceOrigin; 432 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
447 GrAutoScratchTexture scratchTexture(grContext, desc, GrContext::kExact_Scrat chTexMatch); 433 GrAutoScratchTexture scratchTexture(offscreenContexts->gr_context(), desc, G rContext::kExact_ScratchTexMatch);
448 skia::RefPtr<GrTexture> backingStore = skia::AdoptRef(scratchTexture.detach( )); 434 skia::RefPtr<GrTexture> backingStore = skia::AdoptRef(scratchTexture.detach( ));
449 435
450 // Create a device and canvas using that backing store. 436 // Create a device and canvas using that backing store.
451 SkGpuDevice device(grContext, backingStore.get()); 437 SkGpuDevice device(offscreenContexts->gr_context(), backingStore.get());
452 SkCanvas canvas(&device); 438 SkCanvas canvas(&device);
453 439
454 // Draw the source bitmap through the filter to the canvas. 440 // Draw the source bitmap through the filter to the canvas.
455 SkPaint paint; 441 SkPaint paint;
456 paint.setImageFilter(filter); 442 paint.setImageFilter(filter);
457 canvas.clear(0x0); 443 canvas.clear(0x0);
458 canvas.drawSprite(source, 0, 0, &paint); 444 canvas.drawSprite(source, 0, 0, &paint);
459 canvas.flush(); 445 offscreenContexts->FinishUsingContexts();
460 context3d->flush();
461 return device.accessBitmap(false); 446 return device.accessBitmap(false);
462 } 447 }
463 448
464 scoped_ptr<ScopedResource> GLRenderer::drawBackgroundFilters( 449 scoped_ptr<ScopedResource> GLRenderer::drawBackgroundFilters(
465 DrawingFrame& frame, const RenderPassDrawQuad* quad, 450 DrawingFrame& frame, const RenderPassDrawQuad* quad,
466 const gfx::Transform& contentsDeviceTransform, 451 const gfx::Transform& contentsDeviceTransform,
467 const gfx::Transform& contentsDeviceTransformInverse) 452 const gfx::Transform& contentsDeviceTransformInverse)
468 { 453 {
469 // This method draws a background filter, which applies a filter to any pixe ls behind the quad and seen through its background. 454 // This method draws a background filter, which applies a filter to any pixe ls behind the quad and seen through its background.
470 // The algorithm works as follows: 455 // The algorithm works as follows:
(...skipping 27 matching lines...) Expand all
498 int top, right, bottom, left; 483 int top, right, bottom, left;
499 filters.getOutsets(top, right, bottom, left); 484 filters.getOutsets(top, right, bottom, left);
500 deviceRect.Inset(-left, -top, -right, -bottom); 485 deviceRect.Inset(-left, -top, -right, -bottom);
501 486
502 deviceRect.Intersect(frame.currentRenderPass->output_rect); 487 deviceRect.Intersect(frame.currentRenderPass->output_rect);
503 488
504 scoped_ptr<ScopedResource> deviceBackgroundTexture = ScopedResource::create( m_resourceProvider); 489 scoped_ptr<ScopedResource> deviceBackgroundTexture = ScopedResource::create( m_resourceProvider);
505 if (!getFramebufferTexture(deviceBackgroundTexture.get(), deviceRect)) 490 if (!getFramebufferTexture(deviceBackgroundTexture.get(), deviceRect))
506 return scoped_ptr<ScopedResource>(); 491 return scoped_ptr<ScopedResource>();
507 492
508 SkBitmap filteredDeviceBackground = applyFilters(this, filters, deviceBackgr oundTexture.get(), m_client->hasImplThread()); 493 SkBitmap filteredDeviceBackground = applyFilters(this, filters, deviceBackgr oundTexture.get());
509 if (!filteredDeviceBackground.getTexture()) 494 if (!filteredDeviceBackground.getTexture())
510 return scoped_ptr<ScopedResource>(); 495 return scoped_ptr<ScopedResource>();
511 496
512 GrTexture* texture = reinterpret_cast<GrTexture*>(filteredDeviceBackground.g etTexture()); 497 GrTexture* texture = reinterpret_cast<GrTexture*>(filteredDeviceBackground.g etTexture());
513 int filteredDeviceBackgroundTextureId = texture->getTextureHandle(); 498 int filteredDeviceBackgroundTextureId = texture->getTextureHandle();
514 499
515 scoped_ptr<ScopedResource> backgroundTexture = ScopedResource::create(m_reso urceProvider); 500 scoped_ptr<ScopedResource> backgroundTexture = ScopedResource::create(m_reso urceProvider);
516 if (!backgroundTexture->Allocate(quad->rect.size(), GL_RGBA, ResourceProvide r::TextureUsageFramebuffer)) 501 if (!backgroundTexture->Allocate(quad->rect.size(), GL_RGBA, ResourceProvide r::TextureUsageFramebuffer))
517 return scoped_ptr<ScopedResource>(); 502 return scoped_ptr<ScopedResource>();
518 503
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 if (!contentsDeviceTransform.GetInverse(&contentsDeviceTransformInverse)) 536 if (!contentsDeviceTransform.GetInverse(&contentsDeviceTransformInverse))
552 return; 537 return;
553 538
554 scoped_ptr<ScopedResource> backgroundTexture = drawBackgroundFilters( 539 scoped_ptr<ScopedResource> backgroundTexture = drawBackgroundFilters(
555 frame, quad, contentsDeviceTransform, contentsDeviceTransformInverse); 540 frame, quad, contentsDeviceTransform, contentsDeviceTransformInverse);
556 541
557 // FIXME: Cache this value so that we don't have to do it for both the surfa ce and its replica. 542 // FIXME: Cache this value so that we don't have to do it for both the surfa ce and its replica.
558 // Apply filters to the contents texture. 543 // Apply filters to the contents texture.
559 SkBitmap filterBitmap; 544 SkBitmap filterBitmap;
560 if (quad->filter) { 545 if (quad->filter) {
561 filterBitmap = applyImageFilter(this, quad->filter.get(), contentsTextur e, m_client->hasImplThread()); 546 filterBitmap = applyImageFilter(this, quad->filter.get(), contentsTextur e);
562 } else { 547 } else {
563 filterBitmap = applyFilters(this, quad->filters, contentsTexture, m_clie nt->hasImplThread()); 548 filterBitmap = applyFilters(this, quad->filters, contentsTexture);
564 } 549 }
565 550
566 // Draw the background texture if there is one. 551 // Draw the background texture if there is one.
567 if (backgroundTexture) { 552 if (backgroundTexture) {
568 DCHECK(backgroundTexture->size() == quad->rect.size()); 553 DCHECK(backgroundTexture->size() == quad->rect.size());
569 ResourceProvider::ScopedReadLockGL lock(m_resourceProvider, backgroundTe xture->id()); 554 ResourceProvider::ScopedReadLockGL lock(m_resourceProvider, backgroundTe xture->id());
570 copyTextureToFramebuffer(frame, lock.textureId(), quad->rect, quad->quad Transform()); 555 copyTextureToFramebuffer(frame, lock.textureId(), quad->rect, quad->quad Transform());
571 } 556 }
572 557
573 bool clipped = false; 558 bool clipped = false;
(...skipping 1244 matching lines...) Expand 10 before | Expand all | Expand 10 after
1818 1803
1819 releaseRenderPassTextures(); 1804 releaseRenderPassTextures();
1820 } 1805 }
1821 1806
1822 bool GLRenderer::isContextLost() 1807 bool GLRenderer::isContextLost()
1823 { 1808 {
1824 return (m_context->getGraphicsResetStatusARB() != GL_NO_ERROR); 1809 return (m_context->getGraphicsResetStatusARB() != GL_NO_ERROR);
1825 } 1810 }
1826 1811
1827 } // namespace cc 1812 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698