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

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

Issue 1914233006: Implement offscreenCanvas.getContext('webgl') on a worker thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: runTerminationGC() calls releaseStaticPersistentNodes() Created 4 years, 7 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 #include "modules/webgl/OESTextureHalfFloatLinear.h" 59 #include "modules/webgl/OESTextureHalfFloatLinear.h"
60 #include "modules/webgl/OESVertexArrayObject.h" 60 #include "modules/webgl/OESVertexArrayObject.h"
61 #include "modules/webgl/WebGLActiveInfo.h" 61 #include "modules/webgl/WebGLActiveInfo.h"
62 #include "modules/webgl/WebGLBuffer.h" 62 #include "modules/webgl/WebGLBuffer.h"
63 #include "modules/webgl/WebGLCompressedTextureASTC.h" 63 #include "modules/webgl/WebGLCompressedTextureASTC.h"
64 #include "modules/webgl/WebGLCompressedTextureATC.h" 64 #include "modules/webgl/WebGLCompressedTextureATC.h"
65 #include "modules/webgl/WebGLCompressedTextureETC1.h" 65 #include "modules/webgl/WebGLCompressedTextureETC1.h"
66 #include "modules/webgl/WebGLCompressedTexturePVRTC.h" 66 #include "modules/webgl/WebGLCompressedTexturePVRTC.h"
67 #include "modules/webgl/WebGLCompressedTextureS3TC.h" 67 #include "modules/webgl/WebGLCompressedTextureS3TC.h"
68 #include "modules/webgl/WebGLContextAttributeHelpers.h" 68 #include "modules/webgl/WebGLContextAttributeHelpers.h"
69 #include "modules/webgl/WebGLContextAttributes.h"
70 #include "modules/webgl/WebGLContextEvent.h" 69 #include "modules/webgl/WebGLContextEvent.h"
71 #include "modules/webgl/WebGLContextGroup.h" 70 #include "modules/webgl/WebGLContextGroup.h"
72 #include "modules/webgl/WebGLDebugRendererInfo.h" 71 #include "modules/webgl/WebGLDebugRendererInfo.h"
73 #include "modules/webgl/WebGLDebugShaders.h" 72 #include "modules/webgl/WebGLDebugShaders.h"
74 #include "modules/webgl/WebGLDepthTexture.h" 73 #include "modules/webgl/WebGLDepthTexture.h"
75 #include "modules/webgl/WebGLDrawBuffers.h" 74 #include "modules/webgl/WebGLDrawBuffers.h"
76 #include "modules/webgl/WebGLFramebuffer.h" 75 #include "modules/webgl/WebGLFramebuffer.h"
77 #include "modules/webgl/WebGLLoseContext.h" 76 #include "modules/webgl/WebGLLoseContext.h"
78 #include "modules/webgl/WebGLProgram.h" 77 #include "modules/webgl/WebGLProgram.h"
79 #include "modules/webgl/WebGLRenderbuffer.h" 78 #include "modules/webgl/WebGLRenderbuffer.h"
80 #include "modules/webgl/WebGLShader.h" 79 #include "modules/webgl/WebGLShader.h"
81 #include "modules/webgl/WebGLShaderPrecisionFormat.h" 80 #include "modules/webgl/WebGLShaderPrecisionFormat.h"
82 #include "modules/webgl/WebGLTexture.h"
83 #include "modules/webgl/WebGLUniformLocation.h" 81 #include "modules/webgl/WebGLUniformLocation.h"
84 #include "modules/webgl/WebGLVertexArrayObject.h" 82 #include "modules/webgl/WebGLVertexArrayObject.h"
85 #include "modules/webgl/WebGLVertexArrayObjectOES.h" 83 #include "modules/webgl/WebGLVertexArrayObjectOES.h"
86 #include "platform/CheckedInt.h" 84 #include "platform/CheckedInt.h"
87 #include "platform/RuntimeEnabledFeatures.h" 85 #include "platform/RuntimeEnabledFeatures.h"
86 #include "platform/ThreadSafeFunctional.h"
87 #include "platform/WaitableEvent.h"
88 #include "platform/geometry/IntSize.h" 88 #include "platform/geometry/IntSize.h"
89 #include "platform/graphics/GraphicsContext.h" 89 #include "platform/graphics/GraphicsContext.h"
90 #include "platform/graphics/UnacceleratedImageBufferSurface.h" 90 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
91 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h" 91 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h"
92 #include "platform/graphics/gpu/DrawingBuffer.h"
93 #include "public/platform/Platform.h" 92 #include "public/platform/Platform.h"
94 #include "public/platform/WebGraphicsContext3DProvider.h"
95 #include "public/platform/functional/WebFunction.h" 93 #include "public/platform/functional/WebFunction.h"
96 #include "wtf/Functional.h" 94 #include "wtf/Functional.h"
97 #include "wtf/PassOwnPtr.h" 95 #include "wtf/PassOwnPtr.h"
98 #include "wtf/text/StringBuilder.h" 96 #include "wtf/text/StringBuilder.h"
99 #include "wtf/text/StringUTF8Adaptor.h" 97 #include "wtf/text/StringUTF8Adaptor.h"
100 #include "wtf/typed_arrays/ArrayBufferContents.h" 98 #include "wtf/typed_arrays/ArrayBufferContents.h"
101 99
102 #include <memory> 100 #include <memory>
103 101
104 namespace blink { 102 namespace blink {
105 103
106 namespace { 104 namespace {
107 105
108 const double secondsBetweenRestoreAttempts = 1.0; 106 const double secondsBetweenRestoreAttempts = 1.0;
109 const int maxGLErrorsAllowedToConsole = 256; 107 const int maxGLErrorsAllowedToConsole = 256;
110 const unsigned maxGLActiveContexts = 16; 108 const unsigned maxGLActiveContexts = 16;
111 109
112 using WebGLRenderingContextBaseSet = PersistentHeapHashSet<WeakMember<WebGLRende ringContextBase>>; 110 using WebGLRenderingContextBaseSet = PersistentHeapHashSet<WeakMember<WebGLRende ringContextBase>>;
113 WebGLRenderingContextBaseSet& activeContexts() 111 WebGLRenderingContextBaseSet& activeContexts()
114 { 112 {
115 DEFINE_STATIC_LOCAL(WebGLRenderingContextBaseSet, activeContexts, ()); 113 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<WebGLRenderingContextBaseSet> , activeContexts, new ThreadSpecific<WebGLRenderingContextBaseSet>());
116 return activeContexts; 114 if (!activeContexts.isSet())
115 activeContexts->registerAsStaticReference();
116 return *activeContexts;
117 } 117 }
118 118
119 using WebGLRenderingContextBaseMap = PersistentHeapHashMap<WeakMember<WebGLRende ringContextBase>, int>; 119 using WebGLRenderingContextBaseMap = PersistentHeapHashMap<WeakMember<WebGLRende ringContextBase>, int>;
120 WebGLRenderingContextBaseMap& forciblyEvictedContexts() 120 WebGLRenderingContextBaseMap& forciblyEvictedContexts()
121 { 121 {
122 DEFINE_STATIC_LOCAL(WebGLRenderingContextBaseMap, forciblyEvictedContexts, ( )); 122 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<WebGLRenderingContextBaseMap> , forciblyEvictedContexts, new ThreadSpecific<WebGLRenderingContextBaseMap>());
123 return forciblyEvictedContexts; 123 if (!forciblyEvictedContexts.isSet())
124 forciblyEvictedContexts->registerAsStaticReference();
125 return *forciblyEvictedContexts;
124 } 126 }
125 127
126 } // namespace 128 } // namespace
127 129
128 ScopedRGBEmulationColorMask::ScopedRGBEmulationColorMask(gpu::gles2::GLES2Interf ace* contextGL, GLboolean* colorMask, DrawingBuffer* drawingBuffer) 130 ScopedRGBEmulationColorMask::ScopedRGBEmulationColorMask(gpu::gles2::GLES2Interf ace* contextGL, GLboolean* colorMask, DrawingBuffer* drawingBuffer)
129 : m_contextGL(contextGL) 131 : m_contextGL(contextGL)
130 , m_requiresEmulation(drawingBuffer->requiresAlphaChannelToBePreserved()) 132 , m_requiresEmulation(drawingBuffer->requiresAlphaChannelToBePreserved())
131 { 133 {
132 if (m_requiresEmulation) { 134 if (m_requiresEmulation) {
133 memcpy(m_colorMask, colorMask, 4 * sizeof(GLboolean)); 135 memcpy(m_colorMask, colorMask, 4 * sizeof(GLboolean));
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 formatWebGLStatusString("Sandboxed", info.sandboxed ? "yes" : "no", statusMe ssage); 515 formatWebGLStatusString("Sandboxed", info.sandboxed ? "yes" : "no", statusMe ssage);
514 formatWebGLStatusString("Optimus", info.optimus ? "yes" : "no", statusMessag e); 516 formatWebGLStatusString("Optimus", info.optimus ? "yes" : "no", statusMessag e);
515 formatWebGLStatusString("AMD switchable", info.amdSwitchable ? "yes" : "no", statusMessage); 517 formatWebGLStatusString("AMD switchable", info.amdSwitchable ? "yes" : "no", statusMessage);
516 formatWebGLStatusString("Reset notification strategy", String::format("0x%04 x", info.resetNotificationStrategy).utf8().data(), statusMessage); 518 formatWebGLStatusString("Reset notification strategy", String::format("0x%04 x", info.resetNotificationStrategy).utf8().data(), statusMessage);
517 formatWebGLStatusString("GPU process crash count", String::number(info.proce ssCrashCount).utf8().data(), statusMessage); 519 formatWebGLStatusString("GPU process crash count", String::number(info.proce ssCrashCount).utf8().data(), statusMessage);
518 formatWebGLStatusString("ErrorMessage", info.errorMessage.utf8().data(), sta tusMessage); 520 formatWebGLStatusString("ErrorMessage", info.errorMessage.utf8().data(), sta tusMessage);
519 statusMessage.append("."); 521 statusMessage.append(".");
520 return statusMessage; 522 return statusMessage;
521 } 523 }
522 524
523 static PassOwnPtr<WebGraphicsContext3DProvider> createWebGraphicsContext3DProvid erInternal(HTMLCanvasElement* canvas, ScriptState* scriptState, WebGLContextAttr ibutes attributes, unsigned webGLVersion) 525 void WebGLRenderingContextBase::createWebGraphicsContext3DProviderOnMainThread(W ebGLRenderingContextBase::createWebGraphicsContext3DProviderUtils* createUtils, WaitableEvent* waitableEvent)
526 {
527 ASSERT(isMainThread());
528 Platform::GraphicsInfo glInfo = createUtils->glInfo();
529 OwnPtr<WebGraphicsContext3DProvider> provider = adoptPtr(Platform::current() ->createOffscreenGraphicsContext3DProvider(
530 createUtils->contextAttributes(), createUtils->scriptState()->getExecuti onContext()->url(), 0, &glInfo, Platform::DoNotBindToCurrentThread));
531 createUtils->setContextProvider(provider.release());
532 waitableEvent->signal();
533 }
534
535 PassOwnPtr<WebGraphicsContext3DProvider> WebGLRenderingContextBase::createWebGra phicsContext3DProviderInternal(HTMLCanvasElement* canvas, ScriptState* scriptSta te, WebGLContextAttributes attributes, unsigned webGLVersion)
524 { 536 {
525 Platform::ContextAttributes contextAttributes = toPlatformContextAttributes( attributes, webGLVersion); 537 Platform::ContextAttributes contextAttributes = toPlatformContextAttributes( attributes, webGLVersion);
526 Platform::GraphicsInfo glInfo; 538 Platform::GraphicsInfo glInfo;
527 OwnPtr<WebGraphicsContext3DProvider> contextProvider; 539 OwnPtr<WebGraphicsContext3DProvider> contextProvider;
528 if (canvas) { 540 if (canvas) {
529 contextProvider = adoptPtr(Platform::current()->createOffscreenGraphicsC ontext3DProvider( 541 contextProvider = adoptPtr(Platform::current()->createOffscreenGraphicsC ontext3DProvider(
530 contextAttributes, canvas->document().topDocument().url(), 0, &glInf o)); 542 contextAttributes, canvas->document().topDocument().url(), 0, &glInf o, Platform::BindToCurrentThread));
531 } else { 543 } else {
532 contextProvider = adoptPtr(Platform::current()->createOffscreenGraphicsC ontext3DProvider( 544 if (isMainThread()) {
533 contextAttributes, scriptState->getExecutionContext()->url(), 0, &gl Info)); 545 contextProvider = adoptPtr(Platform::current()->createOffscreenGraph icsContext3DProvider(
546 contextAttributes, scriptState->getExecutionContext()->url(), 0, &glInfo, Platform::BindToCurrentThread));
547 } else {
548 WaitableEvent waitableEvent;
549 WebTaskRunner* taskRunner = Platform::current()->mainThread()->getWe bTaskRunner();
550 OwnPtr<WebGLRenderingContextBase::createWebGraphicsContext3DProvider Utils> createUtils = adoptPtr(new WebGLRenderingContextBase::createWebGraphicsCo ntext3DProviderUtils(contextAttributes, glInfo, scriptState));
551 taskRunner->postTask(BLINK_FROM_HERE, threadSafeBind(&createWebGraph icsContext3DProviderOnMainThread, AllowCrossThreadAccess(createUtils.get()), All owCrossThreadAccess(&waitableEvent)));
552 waitableEvent.wait();
553 contextProvider = createUtils->releaseContextProvider();
554 if (!contextProvider->BindToCurrentThread())
555 return nullptr;
556 }
534 } 557 }
535 if (!contextProvider || shouldFailContextCreationForTesting) { 558 if (!contextProvider || shouldFailContextCreationForTesting) {
536 shouldFailContextCreationForTesting = false; 559 shouldFailContextCreationForTesting = false;
537 if (canvas) 560 if (canvas)
538 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webg lcontextcreationerror, false, true, extractWebGLContextCreationError(glInfo))); 561 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webg lcontextcreationerror, false, true, extractWebGLContextCreationError(glInfo)));
539 return nullptr; 562 return nullptr;
540 } 563 }
541 gpu::gles2::GLES2Interface* gl = contextProvider->contextGL(); 564 gpu::gles2::GLES2Interface* gl = contextProvider->contextGL();
542 if (!String(gl->GetString(GL_EXTENSIONS)).contains("GL_OES_packed_depth_sten cil")) { 565 if (!String(gl->GetString(GL_EXTENSIONS)).contains("GL_OES_packed_depth_sten cil")) {
543 if (canvas) 566 if (canvas)
544 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webg lcontextcreationerror, false, true, "OES_packed_depth_stencil support is require d.")); 567 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webg lcontextcreationerror, false, true, "OES_packed_depth_stencil support is require d."));
545 return nullptr; 568 return nullptr;
546 } 569 }
547
548 return contextProvider.release(); 570 return contextProvider.release();
549 } 571 }
550 572
551 PassOwnPtr<WebGraphicsContext3DProvider> WebGLRenderingContextBase::createWebGra phicsContext3DProvider(HTMLCanvasElement* canvas, WebGLContextAttributes attribu tes, unsigned webGLVersion) 573 PassOwnPtr<WebGraphicsContext3DProvider> WebGLRenderingContextBase::createWebGra phicsContext3DProvider(HTMLCanvasElement* canvas, WebGLContextAttributes attribu tes, unsigned webGLVersion)
552 { 574 {
553 Document& document = canvas->document(); 575 Document& document = canvas->document();
554 LocalFrame* frame = document.frame(); 576 LocalFrame* frame = document.frame();
555 if (!frame) { 577 if (!frame) {
556 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcon textcreationerror, false, true, "Web page was not allowed to create a WebGL cont ext.")); 578 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcon textcreationerror, false, true, "Web page was not allowed to create a WebGL cont ext."));
557 return nullptr; 579 return nullptr;
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 tracker->loseExtension(true); 1099 tracker->loseExtension(true);
1078 } 1100 }
1079 m_extensions.clear(); 1101 m_extensions.clear();
1080 1102
1081 // Context must be removed from the group prior to the destruction of the 1103 // Context must be removed from the group prior to the destruction of the
1082 // GL context, otherwise shared objects may not be properly deleted. 1104 // GL context, otherwise shared objects may not be properly deleted.
1083 m_contextGroup->removeContext(this); 1105 m_contextGroup->removeContext(this);
1084 1106
1085 destroyContext(); 1107 destroyContext();
1086 1108
1109 if (activeContexts().contains(this))
sof 2016/05/06 16:06:17 This shouldn't be needed -- if this context object
xidachen 2016/05/06 16:54:43 I was actually going to ask for your suggestions.
Ken Russell (switch to Gerrit) 2016/05/06 17:56:46 Interesting. What's the order of processing when c
sof 2016/05/06 18:43:34 The only reason I can think of why that might happ
sof 2016/05/06 21:37:23 I now understand what's missing here, https://code
sof 2016/05/07 17:09:49 If the CL is rebased >= r392263, the fix will be i
1110 activeContexts().remove(this);
1111 if (forciblyEvictedContexts().contains(this))
1112 forciblyEvictedContexts().remove(this);
1113
1087 willDestroyContext(this); 1114 willDestroyContext(this);
1088 } 1115 }
1089 1116
1090 void WebGLRenderingContextBase::destroyContext() 1117 void WebGLRenderingContextBase::destroyContext()
1091 { 1118 {
1092 if (!drawingBuffer()) 1119 if (!drawingBuffer())
1093 return; 1120 return;
1094 1121
1095 m_extensionsUtil.clear(); 1122 m_extensionsUtil.clear();
1096 1123
(...skipping 4864 matching lines...) Expand 10 before | Expand all | Expand 10 after
5961 5988
5962 // If the context was lost due to RealLostContext, we need to destroy the ol d DrawingBuffer before creating new DrawingBuffer to ensure resource budget enou gh. 5989 // If the context was lost due to RealLostContext, we need to destroy the ol d DrawingBuffer before creating new DrawingBuffer to ensure resource budget enou gh.
5963 if (drawingBuffer()) { 5990 if (drawingBuffer()) {
5964 m_drawingBuffer->beginDestruction(); 5991 m_drawingBuffer->beginDestruction();
5965 m_drawingBuffer.clear(); 5992 m_drawingBuffer.clear();
5966 } 5993 }
5967 5994
5968 Platform::ContextAttributes attributes = toPlatformContextAttributes(m_reque stedAttributes, version()); 5995 Platform::ContextAttributes attributes = toPlatformContextAttributes(m_reque stedAttributes, version());
5969 Platform::GraphicsInfo glInfo; 5996 Platform::GraphicsInfo glInfo;
5970 OwnPtr<WebGraphicsContext3DProvider> contextProvider = adoptPtr(Platform::cu rrent()->createOffscreenGraphicsContext3DProvider( 5997 OwnPtr<WebGraphicsContext3DProvider> contextProvider = adoptPtr(Platform::cu rrent()->createOffscreenGraphicsContext3DProvider(
5971 attributes, canvas()->document().topDocument().url(), 0, &glInfo)); 5998 attributes, canvas()->document().topDocument().url(), 0, &glInfo, Platfo rm::BindToCurrentThread));
5972 RefPtr<DrawingBuffer> buffer; 5999 RefPtr<DrawingBuffer> buffer;
5973 if (contextProvider) { 6000 if (contextProvider) {
5974 // Construct a new drawing buffer with the new GL context. 6001 // Construct a new drawing buffer with the new GL context.
5975 buffer = createDrawingBuffer(contextProvider.release()); 6002 buffer = createDrawingBuffer(contextProvider.release());
5976 // If DrawingBuffer::create() fails to allocate a fbo, |drawingBuffer| i s set to null. 6003 // If DrawingBuffer::create() fails to allocate a fbo, |drawingBuffer| i s set to null.
5977 } 6004 }
5978 if (!buffer) { 6005 if (!buffer) {
5979 if (m_contextLostMode == RealLostContext) { 6006 if (m_contextLostMode == RealLostContext) {
5980 m_restoreTimer.startOneShot(secondsBetweenRestoreAttempts, BLINK_FRO M_HERE); 6007 m_restoreTimer.startOneShot(secondsBetweenRestoreAttempts, BLINK_FRO M_HERE);
5981 } else { 6008 } else {
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
6306 contextGL()->PixelStorei(GL_UNPACK_ALIGNMENT, 1); 6333 contextGL()->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
6307 } 6334 }
6308 6335
6309 void WebGLRenderingContextBase::restoreUnpackParameters() 6336 void WebGLRenderingContextBase::restoreUnpackParameters()
6310 { 6337 {
6311 if (m_unpackAlignment != 1) 6338 if (m_unpackAlignment != 1)
6312 contextGL()->PixelStorei(GL_UNPACK_ALIGNMENT, m_unpackAlignment); 6339 contextGL()->PixelStorei(GL_UNPACK_ALIGNMENT, m_unpackAlignment);
6313 } 6340 }
6314 6341
6315 } // namespace blink 6342 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698