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

Side by Side Diff: third_party/WebKit/Source/modules/webgl/WebGLRenderingContextBase.cpp

Issue 2594843002: Implementing promise-based commit for driving OffscreenCanvas animations (Closed)
Patch Set: test fix Created 3 years, 12 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 ImageBitmap* WebGLRenderingContextBase::transferToImageBitmapBase( 705 ImageBitmap* WebGLRenderingContextBase::transferToImageBitmapBase(
706 ScriptState* scriptState) { 706 ScriptState* scriptState) {
707 UseCounter::Feature feature = 707 UseCounter::Feature feature =
708 UseCounter::OffscreenCanvasTransferToImageBitmapWebGL; 708 UseCounter::OffscreenCanvasTransferToImageBitmapWebGL;
709 UseCounter::count(scriptState->getExecutionContext(), feature); 709 UseCounter::count(scriptState->getExecutionContext(), feature);
710 if (!drawingBuffer()) 710 if (!drawingBuffer())
711 return nullptr; 711 return nullptr;
712 return ImageBitmap::create(drawingBuffer()->transferToStaticBitmapImage()); 712 return ImageBitmap::create(drawingBuffer()->transferToStaticBitmapImage());
713 } 713 }
714 714
715 void WebGLRenderingContextBase::commit(ScriptState* scriptState, 715 ScriptPromise WebGLRenderingContextBase::commit(
716 ExceptionState& exceptionState) { 716 ScriptState* scriptState,
717 ExceptionState& exceptionState) {
717 UseCounter::Feature feature = UseCounter::OffscreenCanvasCommitWebGL; 718 UseCounter::Feature feature = UseCounter::OffscreenCanvasCommitWebGL;
718 UseCounter::count(scriptState->getExecutionContext(), feature); 719 UseCounter::count(scriptState->getExecutionContext(), feature);
719 if (!getOffscreenCanvas()) { 720 if (!offscreenCanvas()) {
720 exceptionState.throwDOMException(InvalidStateError, 721 exceptionState.throwDOMException(InvalidStateError,
721 "Commit() was called on a rendering " 722 "Commit() was called on a rendering "
722 "context that was not created from an " 723 "context that was not created from an "
723 "OffscreenCanvas."); 724 "OffscreenCanvas.");
724 return; 725 return exceptionState.reject(scriptState);
725 } 726 }
726 // no HTMLCanvas associated, thrown InvalidStateError 727 // no HTMLCanvas associated, thrown InvalidStateError
727 if (!getOffscreenCanvas()->hasPlaceholderCanvas()) { 728 if (!offscreenCanvas()->hasPlaceholderCanvas()) {
728 exceptionState.throwDOMException(InvalidStateError, 729 exceptionState.throwDOMException(InvalidStateError,
729 "Commit() was called on a context whose " 730 "Commit() was called on a context whose "
730 "OffscreenCanvas is not associated with a " 731 "OffscreenCanvas is not associated with a "
731 "canvas element."); 732 "canvas element.");
732 return; 733 return exceptionState.reject(scriptState);
733 } 734 }
734 if (!drawingBuffer()) 735 if (!drawingBuffer()) {
735 return; 736 exceptionState.throwDOMException(InvalidStateError,
736 double commitStartTime = WTF::monotonicallyIncreasingTime(); 737 "WebGL context has no drawing buffer.");
738 return exceptionState.reject(scriptState);
739 }
737 // TODO(crbug.com/646864): Make commit() work correctly with 740 // TODO(crbug.com/646864): Make commit() work correctly with
738 // { preserveDrawingBuffer : true }. 741 // { preserveDrawingBuffer : true }.
739 getOffscreenCanvas()->getOrCreateFrameDispatcher()->dispatchFrame( 742 return offscreenCanvas()->commit(
740 std::move(drawingBuffer()->transferToStaticBitmapImage()), 743 std::move(drawingBuffer()->transferToStaticBitmapImage()),
741 commitStartTime, 744 drawingBuffer()->contextProvider()->isSoftwareRendering(), scriptState);
742 drawingBuffer()->contextProvider()->isSoftwareRendering());
743 } 745 }
744 746
745 PassRefPtr<Image> WebGLRenderingContextBase::getImage( 747 PassRefPtr<Image> WebGLRenderingContextBase::getImage(
746 AccelerationHint hint, 748 AccelerationHint hint,
747 SnapshotReason reason) const { 749 SnapshotReason reason) const {
748 if (!drawingBuffer()) 750 if (!drawingBuffer())
749 return nullptr; 751 return nullptr;
750 752
751 drawingBuffer()->resolveAndBindForReadAndDraw(); 753 drawingBuffer()->resolveAndBindForReadAndDraw();
752 IntSize size = clampedCanvasSize(); 754 IntSize size = clampedCanvasSize();
(...skipping 20 matching lines...) Expand all
773 // images. See crbug.com/657531. 775 // images. See crbug.com/657531.
774 ImageData* imageData = nullptr; 776 ImageData* imageData = nullptr;
775 // TODO(ccameron): WebGL should produce sRGB images. 777 // TODO(ccameron): WebGL should produce sRGB images.
776 // https://crbug.com/672299 778 // https://crbug.com/672299
777 if (this->drawingBuffer()) { 779 if (this->drawingBuffer()) {
778 sk_sp<SkImage> snapshot = 780 sk_sp<SkImage> snapshot =
779 this->drawingBuffer() 781 this->drawingBuffer()
780 ->transferToStaticBitmapImage() 782 ->transferToStaticBitmapImage()
781 ->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()); 783 ->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget());
782 if (snapshot) { 784 if (snapshot) {
783 imageData = ImageData::create(this->getOffscreenCanvas()->size()); 785 imageData = ImageData::create(this->offscreenCanvas()->size());
784 SkImageInfo imageInfo = SkImageInfo::Make( 786 SkImageInfo imageInfo = SkImageInfo::Make(
785 this->drawingBufferWidth(), this->drawingBufferHeight(), 787 this->drawingBufferWidth(), this->drawingBufferHeight(),
786 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType); 788 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);
787 snapshot->readPixels(imageInfo, imageData->data()->data(), 789 snapshot->readPixels(imageInfo, imageData->data()->data(),
788 imageInfo.minRowBytes(), 0, 0); 790 imageInfo.minRowBytes(), 0, 0);
789 } 791 }
790 } 792 }
791 return imageData; 793 return imageData;
792 } 794 }
793 795
(...skipping 6583 matching lines...) Expand 10 before | Expand all | Expand 10 after
7377 synthesizeGLError(GL_INVALID_FRAMEBUFFER_OPERATION, functionName, reason); 7379 synthesizeGLError(GL_INVALID_FRAMEBUFFER_OPERATION, functionName, reason);
7378 return false; 7380 return false;
7379 } 7381 }
7380 7382
7381 return true; 7383 return true;
7382 } 7384 }
7383 7385
7384 void WebGLRenderingContextBase::dispatchContextLostEvent(TimerBase*) { 7386 void WebGLRenderingContextBase::dispatchContextLostEvent(TimerBase*) {
7385 WebGLContextEvent* event = WebGLContextEvent::create( 7387 WebGLContextEvent* event = WebGLContextEvent::create(
7386 EventTypeNames::webglcontextlost, false, true, ""); 7388 EventTypeNames::webglcontextlost, false, true, "");
7387 if (getOffscreenCanvas()) 7389 if (offscreenCanvas())
7388 getOffscreenCanvas()->dispatchEvent(event); 7390 offscreenCanvas()->dispatchEvent(event);
7389 else 7391 else
7390 canvas()->dispatchEvent(event); 7392 canvas()->dispatchEvent(event);
7391 m_restoreAllowed = event->defaultPrevented(); 7393 m_restoreAllowed = event->defaultPrevented();
7392 if (m_restoreAllowed && !m_isHidden) { 7394 if (m_restoreAllowed && !m_isHidden) {
7393 if (m_autoRecoveryMethod == Auto) 7395 if (m_autoRecoveryMethod == Auto)
7394 m_restoreTimer.startOneShot(0, BLINK_FROM_HERE); 7396 m_restoreTimer.startOneShot(0, BLINK_FROM_HERE);
7395 } 7397 }
7396 } 7398 }
7397 7399
7398 void WebGLRenderingContextBase::maybeRestoreContext(TimerBase*) { 7400 void WebGLRenderingContextBase::maybeRestoreContext(TimerBase*) {
(...skipping 25 matching lines...) Expand all
7424 // enough. 7426 // enough.
7425 if (drawingBuffer()) { 7427 if (drawingBuffer()) {
7426 m_drawingBuffer->beginDestruction(); 7428 m_drawingBuffer->beginDestruction();
7427 m_drawingBuffer.clear(); 7429 m_drawingBuffer.clear();
7428 } 7430 }
7429 7431
7430 Platform::ContextAttributes attributes = 7432 Platform::ContextAttributes attributes =
7431 toPlatformContextAttributes(creationAttributes(), version()); 7433 toPlatformContextAttributes(creationAttributes(), version());
7432 Platform::GraphicsInfo glInfo; 7434 Platform::GraphicsInfo glInfo;
7433 std::unique_ptr<WebGraphicsContext3DProvider> contextProvider; 7435 std::unique_ptr<WebGraphicsContext3DProvider> contextProvider;
7434 const auto& url = canvas() 7436 const auto& url = canvas() ? canvas()->document().topDocument().url()
7435 ? canvas()->document().topDocument().url() 7437 : offscreenCanvas()->getExecutionContext()->url();
7436 : getOffscreenCanvas()->getExecutionContext()->url();
7437 if (isMainThread()) { 7438 if (isMainThread()) {
7438 contextProvider = WTF::wrapUnique( 7439 contextProvider = WTF::wrapUnique(
7439 Platform::current()->createOffscreenGraphicsContext3DProvider( 7440 Platform::current()->createOffscreenGraphicsContext3DProvider(
7440 attributes, url, 0, &glInfo)); 7441 attributes, url, 0, &glInfo));
7441 } else { 7442 } else {
7442 contextProvider = 7443 contextProvider =
7443 createContextProviderOnWorkerThread(attributes, &glInfo, url); 7444 createContextProviderOnWorkerThread(attributes, &glInfo, url);
7444 } 7445 }
7445 RefPtr<DrawingBuffer> buffer; 7446 RefPtr<DrawingBuffer> buffer;
7446 if (contextProvider && contextProvider->bindToCurrentThread()) { 7447 if (contextProvider && contextProvider->bindToCurrentThread()) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
7481 removeFromEvictedList(this); 7482 removeFromEvictedList(this);
7482 7483
7483 setupFlags(); 7484 setupFlags();
7484 initializeNewContext(); 7485 initializeNewContext();
7485 markContextChanged(CanvasContextChanged); 7486 markContextChanged(CanvasContextChanged);
7486 WebGLContextEvent* event = WebGLContextEvent::create( 7487 WebGLContextEvent* event = WebGLContextEvent::create(
7487 EventTypeNames::webglcontextrestored, false, true, ""); 7488 EventTypeNames::webglcontextrestored, false, true, "");
7488 if (canvas()) 7489 if (canvas())
7489 canvas()->dispatchEvent(event); 7490 canvas()->dispatchEvent(event);
7490 else 7491 else
7491 getOffscreenCanvas()->dispatchEvent(event); 7492 offscreenCanvas()->dispatchEvent(event);
7492 } 7493 }
7493 7494
7494 String WebGLRenderingContextBase::ensureNotNull(const String& text) const { 7495 String WebGLRenderingContextBase::ensureNotNull(const String& text) const {
7495 if (text.isNull()) 7496 if (text.isNull())
7496 return WTF::emptyString(); 7497 return WTF::emptyString();
7497 return text; 7498 return text;
7498 } 7499 }
7499 7500
7500 WebGLRenderingContextBase::LRUImageBufferCache::LRUImageBufferCache( 7501 WebGLRenderingContextBase::LRUImageBufferCache::LRUImageBufferCache(
7501 int capacity) 7502 int capacity)
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
7607 else 7608 else
7608 contextGL()->Disable(capability); 7609 contextGL()->Disable(capability);
7609 } 7610 }
7610 7611
7611 IntSize WebGLRenderingContextBase::clampedCanvasSize() const { 7612 IntSize WebGLRenderingContextBase::clampedCanvasSize() const {
7612 int width, height; 7613 int width, height;
7613 if (canvas()) { 7614 if (canvas()) {
7614 width = canvas()->width(); 7615 width = canvas()->width();
7615 height = canvas()->height(); 7616 height = canvas()->height();
7616 } else { 7617 } else {
7617 width = getOffscreenCanvas()->width(); 7618 width = offscreenCanvas()->width();
7618 height = getOffscreenCanvas()->height(); 7619 height = offscreenCanvas()->height();
7619 } 7620 }
7620 return IntSize(clamp(width, 1, m_maxViewportDims[0]), 7621 return IntSize(clamp(width, 1, m_maxViewportDims[0]),
7621 clamp(height, 1, m_maxViewportDims[1])); 7622 clamp(height, 1, m_maxViewportDims[1]));
7622 } 7623 }
7623 7624
7624 GLint WebGLRenderingContextBase::maxDrawBuffers() { 7625 GLint WebGLRenderingContextBase::maxDrawBuffers() {
7625 if (isContextLost() || 7626 if (isContextLost() ||
7626 !(extensionEnabled(WebGLDrawBuffersName) || isWebGL2OrHigher())) 7627 !(extensionEnabled(WebGLDrawBuffersName) || isWebGL2OrHigher()))
7627 return 0; 7628 return 0;
7628 if (!m_maxDrawBuffers) 7629 if (!m_maxDrawBuffers)
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
7770 void WebGLRenderingContextBase::restoreUnpackParameters() { 7771 void WebGLRenderingContextBase::restoreUnpackParameters() {
7771 if (m_unpackAlignment != 1) 7772 if (m_unpackAlignment != 1)
7772 contextGL()->PixelStorei(GL_UNPACK_ALIGNMENT, m_unpackAlignment); 7773 contextGL()->PixelStorei(GL_UNPACK_ALIGNMENT, m_unpackAlignment);
7773 } 7774 }
7774 7775
7775 void WebGLRenderingContextBase::getHTMLOrOffscreenCanvas( 7776 void WebGLRenderingContextBase::getHTMLOrOffscreenCanvas(
7776 HTMLCanvasElementOrOffscreenCanvas& result) const { 7777 HTMLCanvasElementOrOffscreenCanvas& result) const {
7777 if (canvas()) 7778 if (canvas())
7778 result.setHTMLCanvasElement(canvas()); 7779 result.setHTMLCanvasElement(canvas());
7779 else 7780 else
7780 result.setOffscreenCanvas(getOffscreenCanvas()); 7781 result.setOffscreenCanvas(offscreenCanvas());
7781 } 7782 }
7782 7783
7783 } // namespace blink 7784 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698