OLD | NEW |
---|---|
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 |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "cc/proxy.h" | 24 #include "cc/proxy.h" |
25 #include "cc/render_pass.h" | 25 #include "cc/render_pass.h" |
26 #include "cc/render_surface_filters.h" | 26 #include "cc/render_surface_filters.h" |
27 #include "cc/scoped_resource.h" | 27 #include "cc/scoped_resource.h" |
28 #include "cc/single_thread_proxy.h" | 28 #include "cc/single_thread_proxy.h" |
29 #include "cc/stream_video_draw_quad.h" | 29 #include "cc/stream_video_draw_quad.h" |
30 #include "cc/texture_draw_quad.h" | 30 #include "cc/texture_draw_quad.h" |
31 #include "cc/video_layer_impl.h" | 31 #include "cc/video_layer_impl.h" |
32 #include "gpu/GLES2/gl2extchromium.h" | 32 #include "gpu/GLES2/gl2extchromium.h" |
33 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h" | 33 #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" | 34 #include "third_party/khronos/GLES2/gl2.h" |
36 #include "third_party/khronos/GLES2/gl2ext.h" | 35 #include "third_party/khronos/GLES2/gl2ext.h" |
37 #include "third_party/skia/include/core/SkBitmap.h" | 36 #include "third_party/skia/include/core/SkBitmap.h" |
38 #include "third_party/skia/include/core/SkColor.h" | 37 #include "third_party/skia/include/core/SkColor.h" |
39 #include "third_party/skia/include/gpu/GrContext.h" | 38 #include "third_party/skia/include/gpu/GrContext.h" |
40 #include "third_party/skia/include/gpu/GrTexture.h" | 39 #include "third_party/skia/include/gpu/GrTexture.h" |
41 #include "third_party/skia/include/gpu/SkGpuDevice.h" | 40 #include "third_party/skia/include/gpu/SkGpuDevice.h" |
42 #include "third_party/skia/include/gpu/SkGrTexturePixelRef.h" | 41 #include "third_party/skia/include/gpu/SkGrTexturePixelRef.h" |
43 #include "ui/gfx/quad_f.h" | 42 #include "ui/gfx/quad_f.h" |
44 #include "ui/gfx/rect_conversions.h" | 43 #include "ui/gfx/rect_conversions.h" |
44 #include "ui/gl/context_provider.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 Loading... | |
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 Loading... | |
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 ui::ContextProvider* offscreenContexts = renderer->resourceProvider()->offsc reenContextProvider(); |
397 GrContext* filterGrContext = getFilterGrContext(hasImplThread); | 382 if (!offscreenContexts || !offscreenContexts->Context3d() || !offscreenConte xts->GrContext()) |
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 // Flush the compositor context to ensure that textures there are available |
405 SkBitmap source = RenderSurfaceFilters::apply(filters, lock.textureId(), sou rceTexture->size(), filterContext, filterGrContext); | 388 // in the shared context. Do this after locking/creating the compositor |
389 // texture. | |
390 renderer->resourceProvider()->flush(); | |
391 | |
392 // Make sure ganesh uses the correct GL context. | |
piman
2013/02/21 22:49:48
nit: s/ganesh/skia/
danakj
2013/02/22 01:56:31
Done.
| |
393 offscreenContexts->Context3d()->makeContextCurrent(); | |
394 | |
395 SkBitmap source = RenderSurfaceFilters::apply(filters, lock.textureId(), sou rceTextureResource->size(), offscreenContexts->GrContext()); | |
396 | |
397 // Flush ganesh context so that all the rendered stuff appears on the | |
398 // texture. | |
399 offscreenContexts->GrContext()->flush(); | |
400 | |
401 // Flush the GL context so rendering results from this context are | |
402 // visible in the compositor's context. | |
403 offscreenContexts->Context3d()->flush(); | |
piman
2013/02/21 22:49:48
Should we also make the compositor context current
danakj
2013/02/22 01:56:31
I copied this code from resource update controller
| |
406 return source; | 404 return source; |
407 } | 405 } |
408 | 406 |
409 static SkBitmap applyImageFilter(GLRenderer* renderer, SkImageFilter* filter, Sc opedResource* sourceTexture, bool hasImplThread) | 407 static SkBitmap applyImageFilter(GLRenderer* renderer, SkImageFilter* filter, Sc opedResource* sourceTextureResource) |
410 { | 408 { |
411 if (!filter) | 409 if (!filter) |
412 return SkBitmap(); | 410 return SkBitmap(); |
413 | 411 |
414 WebGraphicsContext3D* context3d = getFilterContext(hasImplThread); | 412 ui::ContextProvider* offscreenContexts = renderer->resourceProvider()->offsc reenContextProvider(); |
415 GrContext* grContext = getFilterGrContext(hasImplThread); | 413 if (!offscreenContexts || !offscreenContexts->Context3d() || !offscreenConte xts->GrContext()) |
416 | |
417 if (!context3d || !grContext) | |
418 return SkBitmap(); | 414 return SkBitmap(); |
419 | 415 |
420 renderer->context()->flush(); | 416 ResourceProvider::ScopedWriteLockGL lock(renderer->resourceProvider(), sourc eTextureResource->id()); |
421 | 417 |
422 ResourceProvider::ScopedWriteLockGL lock(renderer->resourceProvider(), sourc eTexture->id()); | 418 // Flush the compositor context to ensure that textures there are available |
419 // in the shared context. Do this after locking/creating the compositor | |
420 // texture. | |
421 renderer->resourceProvider()->flush(); | |
422 | |
423 // Make sure ganesh uses the correct GL context. | |
424 offscreenContexts->Context3d()->makeContextCurrent(); | |
423 | 425 |
424 // Wrap the source texture in a Ganesh platform texture. | 426 // Wrap the source texture in a Ganesh platform texture. |
425 GrBackendTextureDesc backendTextureDescription; | 427 GrBackendTextureDesc backendTextureDescription; |
426 backendTextureDescription.fWidth = sourceTexture->size().width(); | 428 backendTextureDescription.fWidth = sourceTextureResource->size().width(); |
427 backendTextureDescription.fHeight = sourceTexture->size().height(); | 429 backendTextureDescription.fHeight = sourceTextureResource->size().height(); |
428 backendTextureDescription.fConfig = kSkia8888_GrPixelConfig; | 430 backendTextureDescription.fConfig = kSkia8888_GrPixelConfig; |
429 backendTextureDescription.fTextureHandle = lock.textureId(); | 431 backendTextureDescription.fTextureHandle = lock.textureId(); |
430 backendTextureDescription.fOrigin = kTopLeft_GrSurfaceOrigin; | 432 backendTextureDescription.fOrigin = kTopLeft_GrSurfaceOrigin; |
431 skia::RefPtr<GrTexture> texture = skia::AdoptRef(grContext->wrapBackendTextu re(backendTextureDescription)); | 433 skia::RefPtr<GrTexture> texture = skia::AdoptRef(offscreenContexts->GrContex t()->wrapBackendTexture(backendTextureDescription)); |
432 | 434 |
433 // Place the platform texture inside an SkBitmap. | 435 // Place the platform texture inside an SkBitmap. |
434 SkBitmap source; | 436 SkBitmap source; |
435 source.setConfig(SkBitmap::kARGB_8888_Config, sourceTexture->size().width(), sourceTexture->size().height()); | 437 source.setConfig(SkBitmap::kARGB_8888_Config, sourceTextureResource->size(). width(), sourceTextureResource->size().height()); |
436 skia::RefPtr<SkGrPixelRef> pixelRef = skia::AdoptRef(new SkGrPixelRef(textur e.get())); | 438 skia::RefPtr<SkGrPixelRef> pixelRef = skia::AdoptRef(new SkGrPixelRef(textur e.get())); |
437 source.setPixelRef(pixelRef.get()); | 439 source.setPixelRef(pixelRef.get()); |
438 | 440 |
439 // Create a scratch texture for backing store. | 441 // Create a scratch texture for backing store. |
440 GrTextureDesc desc; | 442 GrTextureDesc desc; |
441 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; | 443 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
442 desc.fSampleCnt = 0; | 444 desc.fSampleCnt = 0; |
443 desc.fWidth = source.width(); | 445 desc.fWidth = source.width(); |
444 desc.fHeight = source.height(); | 446 desc.fHeight = source.height(); |
445 desc.fConfig = kSkia8888_GrPixelConfig; | 447 desc.fConfig = kSkia8888_GrPixelConfig; |
446 desc.fOrigin = kTopLeft_GrSurfaceOrigin; | 448 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
447 GrAutoScratchTexture scratchTexture(grContext, desc, GrContext::kExact_Scrat chTexMatch); | 449 GrAutoScratchTexture scratchTexture(offscreenContexts->GrContext(), desc, Gr Context::kExact_ScratchTexMatch); |
448 skia::RefPtr<GrTexture> backingStore = skia::AdoptRef(scratchTexture.detach( )); | 450 skia::RefPtr<GrTexture> backingStore = skia::AdoptRef(scratchTexture.detach( )); |
449 | 451 |
450 // Create a device and canvas using that backing store. | 452 // Create a device and canvas using that backing store. |
451 SkGpuDevice device(grContext, backingStore.get()); | 453 SkGpuDevice device(offscreenContexts->GrContext(), backingStore.get()); |
452 SkCanvas canvas(&device); | 454 SkCanvas canvas(&device); |
453 | 455 |
454 // Draw the source bitmap through the filter to the canvas. | 456 // Draw the source bitmap through the filter to the canvas. |
455 SkPaint paint; | 457 SkPaint paint; |
456 paint.setImageFilter(filter); | 458 paint.setImageFilter(filter); |
457 canvas.clear(0x0); | 459 canvas.clear(0x0); |
458 canvas.drawSprite(source, 0, 0, &paint); | 460 canvas.drawSprite(source, 0, 0, &paint); |
459 canvas.flush(); | 461 |
460 context3d->flush(); | 462 // Flush ganesh context so that all the rendered stuff appears on the |
463 // texture. | |
464 offscreenContexts->GrContext()->flush(); | |
465 | |
466 // Flush the GL context so rendering results from this context are | |
467 // visible in the compositor's context. | |
468 offscreenContexts->Context3d()->flush(); | |
469 | |
461 return device.accessBitmap(false); | 470 return device.accessBitmap(false); |
462 } | 471 } |
463 | 472 |
464 scoped_ptr<ScopedResource> GLRenderer::drawBackgroundFilters( | 473 scoped_ptr<ScopedResource> GLRenderer::drawBackgroundFilters( |
465 DrawingFrame& frame, const RenderPassDrawQuad* quad, | 474 DrawingFrame& frame, const RenderPassDrawQuad* quad, |
466 const gfx::Transform& contentsDeviceTransform, | 475 const gfx::Transform& contentsDeviceTransform, |
467 const gfx::Transform& contentsDeviceTransformInverse) | 476 const gfx::Transform& contentsDeviceTransformInverse) |
468 { | 477 { |
469 // This method draws a background filter, which applies a filter to any pixe ls behind the quad and seen through its background. | 478 // 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: | 479 // The algorithm works as follows: |
(...skipping 27 matching lines...) Expand all Loading... | |
498 int top, right, bottom, left; | 507 int top, right, bottom, left; |
499 filters.getOutsets(top, right, bottom, left); | 508 filters.getOutsets(top, right, bottom, left); |
500 deviceRect.Inset(-left, -top, -right, -bottom); | 509 deviceRect.Inset(-left, -top, -right, -bottom); |
501 | 510 |
502 deviceRect.Intersect(frame.currentRenderPass->output_rect); | 511 deviceRect.Intersect(frame.currentRenderPass->output_rect); |
503 | 512 |
504 scoped_ptr<ScopedResource> deviceBackgroundTexture = ScopedResource::create( m_resourceProvider); | 513 scoped_ptr<ScopedResource> deviceBackgroundTexture = ScopedResource::create( m_resourceProvider); |
505 if (!getFramebufferTexture(deviceBackgroundTexture.get(), deviceRect)) | 514 if (!getFramebufferTexture(deviceBackgroundTexture.get(), deviceRect)) |
506 return scoped_ptr<ScopedResource>(); | 515 return scoped_ptr<ScopedResource>(); |
507 | 516 |
508 SkBitmap filteredDeviceBackground = applyFilters(this, filters, deviceBackgr oundTexture.get(), m_client->hasImplThread()); | 517 SkBitmap filteredDeviceBackground = applyFilters(this, filters, deviceBackgr oundTexture.get()); |
509 if (!filteredDeviceBackground.getTexture()) | 518 if (!filteredDeviceBackground.getTexture()) |
510 return scoped_ptr<ScopedResource>(); | 519 return scoped_ptr<ScopedResource>(); |
511 | 520 |
512 GrTexture* texture = reinterpret_cast<GrTexture*>(filteredDeviceBackground.g etTexture()); | 521 GrTexture* texture = reinterpret_cast<GrTexture*>(filteredDeviceBackground.g etTexture()); |
513 int filteredDeviceBackgroundTextureId = texture->getTextureHandle(); | 522 int filteredDeviceBackgroundTextureId = texture->getTextureHandle(); |
514 | 523 |
515 scoped_ptr<ScopedResource> backgroundTexture = ScopedResource::create(m_reso urceProvider); | 524 scoped_ptr<ScopedResource> backgroundTexture = ScopedResource::create(m_reso urceProvider); |
516 if (!backgroundTexture->Allocate(quad->rect.size(), GL_RGBA, ResourceProvide r::TextureUsageFramebuffer)) | 525 if (!backgroundTexture->Allocate(quad->rect.size(), GL_RGBA, ResourceProvide r::TextureUsageFramebuffer)) |
517 return scoped_ptr<ScopedResource>(); | 526 return scoped_ptr<ScopedResource>(); |
518 | 527 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
551 if (!contentsDeviceTransform.GetInverse(&contentsDeviceTransformInverse)) | 560 if (!contentsDeviceTransform.GetInverse(&contentsDeviceTransformInverse)) |
552 return; | 561 return; |
553 | 562 |
554 scoped_ptr<ScopedResource> backgroundTexture = drawBackgroundFilters( | 563 scoped_ptr<ScopedResource> backgroundTexture = drawBackgroundFilters( |
555 frame, quad, contentsDeviceTransform, contentsDeviceTransformInverse); | 564 frame, quad, contentsDeviceTransform, contentsDeviceTransformInverse); |
556 | 565 |
557 // FIXME: Cache this value so that we don't have to do it for both the surfa ce and its replica. | 566 // 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. | 567 // Apply filters to the contents texture. |
559 SkBitmap filterBitmap; | 568 SkBitmap filterBitmap; |
560 if (quad->filter) { | 569 if (quad->filter) { |
561 filterBitmap = applyImageFilter(this, quad->filter.get(), contentsTextur e, m_client->hasImplThread()); | 570 filterBitmap = applyImageFilter(this, quad->filter.get(), contentsTextur e); |
562 } else { | 571 } else { |
563 filterBitmap = applyFilters(this, quad->filters, contentsTexture, m_clie nt->hasImplThread()); | 572 filterBitmap = applyFilters(this, quad->filters, contentsTexture); |
564 } | 573 } |
565 | 574 |
566 // Draw the background texture if there is one. | 575 // Draw the background texture if there is one. |
567 if (backgroundTexture) { | 576 if (backgroundTexture) { |
568 DCHECK(backgroundTexture->size() == quad->rect.size()); | 577 DCHECK(backgroundTexture->size() == quad->rect.size()); |
569 ResourceProvider::ScopedReadLockGL lock(m_resourceProvider, backgroundTe xture->id()); | 578 ResourceProvider::ScopedReadLockGL lock(m_resourceProvider, backgroundTe xture->id()); |
570 copyTextureToFramebuffer(frame, lock.textureId(), quad->rect, quad->quad Transform()); | 579 copyTextureToFramebuffer(frame, lock.textureId(), quad->rect, quad->quad Transform()); |
571 } | 580 } |
572 | 581 |
573 bool clipped = false; | 582 bool clipped = false; |
(...skipping 1258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1832 | 1841 |
1833 releaseRenderPassTextures(); | 1842 releaseRenderPassTextures(); |
1834 } | 1843 } |
1835 | 1844 |
1836 bool GLRenderer::isContextLost() | 1845 bool GLRenderer::isContextLost() |
1837 { | 1846 { |
1838 return (m_context->getGraphicsResetStatusARB() != GL_NO_ERROR); | 1847 return (m_context->getGraphicsResetStatusARB() != GL_NO_ERROR); |
1839 } | 1848 } |
1840 | 1849 |
1841 } // namespace cc | 1850 } // namespace cc |
OLD | NEW |