Chromium Code Reviews| Index: Source/core/html/canvas/WebGL2RenderingContext.cpp |
| diff --git a/Source/core/html/canvas/WebGL2RenderingContext.cpp b/Source/core/html/canvas/WebGL2RenderingContext.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..716b81cd7acb062267346f34a4825f0fba6b0cd2 |
| --- /dev/null |
| +++ b/Source/core/html/canvas/WebGL2RenderingContext.cpp |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "core/html/canvas/WebGL2RenderingContext.h" |
| + |
| +#include "core/frame/LocalFrame.h" |
| +#include "core/frame/Settings.h" |
| +#include "core/html/canvas/ContextAttributeHelpers.h" |
| +#include "core/html/canvas/WebGLContextAttributes.h" |
| +#include "core/html/canvas/WebGLContextEvent.h" |
| +#include "core/html/canvas/WebGLDebugRendererInfo.h" |
| +#include "core/html/canvas/WebGLDebugShaders.h" |
| +#include "core/loader/FrameLoader.h" |
| +#include "core/loader/FrameLoaderClient.h" |
| +#include "platform/graphics/gpu/DrawingBuffer.h" |
| +#include "public/platform/Platform.h" |
| + |
| +namespace blink { |
| + |
| +static bool shouldFailContextCreationForTesting = false; |
| + |
| +PassOwnPtr<WebGL2RenderingContext> WebGL2RenderingContext::create(HTMLCanvasElement* canvas, const CanvasContextCreationAttributes& attrs) |
| +{ |
| + if (!RuntimeEnabledFeatures::unsafeES3APIsEnabled()) { |
| + canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Creation of WebGL2 contexts disabled.")); |
| + return nullptr; |
| + } |
| + |
| + Document& document = canvas->document(); |
| + LocalFrame* frame = document.frame(); |
| + if (!frame) { |
| + canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Web page was not allowed to create a WebGL2 context.")); |
| + return nullptr; |
| + } |
| + Settings* settings = frame->settings(); |
| + |
| + // The FrameLoaderClient might block creation of a new WebGL context despite the page settings; in |
| + // particular, if WebGL contexts were lost one or more times via the GL_ARB_robustness extension. |
| + if (!frame->loader().client()->allowWebGL(settings && settings->webGLEnabled())) { |
| + canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Web page was not allowed to create a WebGL2 context.")); |
| + return nullptr; |
| + } |
| + |
| + WebGLContextAttributes attributes = toWebGLContextAttributes(attrs); |
| + blink::WebGraphicsContext3D::Attributes wgc3dAttributes = toWebGraphicsContext3DAttributes(attributes, document.topDocument().url().string(), settings, 2); |
| + blink::WebGLInfo glInfo; |
| + OwnPtr<blink::WebGraphicsContext3D> context = adoptPtr(blink::Platform::current()->createOffscreenGraphicsContext3D(wgc3dAttributes, 0, &glInfo)); |
| + if (!context || shouldFailContextCreationForTesting) { |
| + shouldFailContextCreationForTesting = false; |
|
Ken Russell (switch to Gerrit)
2015/02/03 01:23:24
Please refactor this boilerplate error handling co
|
| + String statusMessage; |
| + if (!glInfo.contextInfoCollectionFailure.isEmpty()) { |
| + statusMessage.append("Could not create a WebGL2 context. "); |
| + statusMessage.append(glInfo.contextInfoCollectionFailure); |
| + } else { |
| + statusMessage.append("Could not create a WebGL2 context"); |
| + if (!glInfo.vendorInfo.isEmpty()) { |
| + statusMessage.append(" VendorInfo = "); |
| + statusMessage.append(glInfo.vendorInfo); |
| + } else { |
| + statusMessage.append(" VendorInfo = Not Available"); |
| + } |
| + if (!glInfo.rendererInfo.isEmpty()) { |
| + statusMessage.append(", RendererInfo = "); |
| + statusMessage.append(glInfo.rendererInfo); |
| + } else { |
| + statusMessage.append(", RendererInfo = Not Available"); |
| + } |
| + if (!glInfo.driverVersion.isEmpty()) { |
| + statusMessage.append(", DriverInfo = "); |
| + statusMessage.append(glInfo.driverVersion); |
| + } else { |
| + statusMessage.append(", DriverInfo = Not Available"); |
| + } |
| + statusMessage.append("."); |
| + } |
| + canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, statusMessage)); |
| + return nullptr; |
| + } |
| + |
| + OwnPtr<Extensions3DUtil> extensionsUtil = Extensions3DUtil::create(context.get()); |
| + if (!extensionsUtil) |
| + return nullptr; |
| + if (extensionsUtil->supportsExtension("GL_EXT_debug_marker")) { |
| + String contextLabel(String::format("WebGL2RenderingContext-%p", context.get())); |
| + context->pushGroupMarkerEXT(contextLabel.ascii().data()); |
| + } |
| + |
| + OwnPtrWillBeRawPtr<WebGL2RenderingContext> renderingContext = adoptPtrWillBeNoop(new WebGL2RenderingContext(canvas, context.release(), attributes)); |
| + renderingContext->registerContextExtensions(); |
| + renderingContext->suspendIfNeeded(); |
| + |
| + if (!renderingContext->drawingBuffer()) { |
| + canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Could not create a WebGL2 context.")); |
| + return nullptr; |
| + } |
| + |
| + return renderingContext.release(); |
| +} |
| + |
| +WebGL2RenderingContext::WebGL2RenderingContext(HTMLCanvasElement* passedCanvas, PassOwnPtr<blink::WebGraphicsContext3D> context, const WebGLContextAttributes& requestedAttributes) |
| + : WebGL2RenderingContextBase(passedCanvas, context, requestedAttributes) |
| +{ |
| +} |
| + |
| +WebGL2RenderingContext::~WebGL2RenderingContext() |
| +{ |
| + |
| +} |
| + |
| +void WebGL2RenderingContext::registerContextExtensions() |
| +{ |
| + // Register privileged extensions. |
| + registerExtension<WebGLDebugRendererInfo>(m_webglDebugRendererInfo); |
| + registerExtension<WebGLDebugShaders>(m_webglDebugShaders); |
| +} |
| + |
| +void WebGL2RenderingContext::trace(Visitor* visitor) |
| +{ |
| + visitor->trace(m_webglDebugRendererInfo); |
| + visitor->trace(m_webglDebugShaders); |
| + WebGL2RenderingContextBase::trace(visitor); |
| +} |
| + |
| +void WebGL2RenderingContext::forceNextWebGLContextCreationToFail() |
| +{ |
| + shouldFailContextCreationForTesting = true; |
| +} |
| + |
| +} // namespace blink |