1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "core/html/canvas/WebGLRenderingContext.h" 28 29 #include "core/frame/LocalFrame.h" 30 #include "core/html/canvas/ANGLEInstancedArrays.h" 31 #include "core/html/canvas/EXTBlendMinMax.h" 32 #include "core/html/canvas/EXTFragDepth.h" 33 #include "core/html/canvas/EXTShaderTextureLOD.h" 34 #include "core/html/canvas/EXTTextureFilterAnisotropic.h" 35 #include "core/html/canvas/OESElementIndexUint.h" 36 #include "core/html/canvas/OESStandardDerivatives.h" 37 #include "core/html/canvas/OESTextureFloat.h" 38 #include "core/html/canvas/OESTextureFloatLinear.h" 39 #include "core/html/canvas/OESTextureHalfFloat.h" 40 #include "core/html/canvas/OESTextureHalfFloatLinear.h" 41 #include "core/html/canvas/OESVertexArrayObject.h" 42 #include "core/html/canvas/WebGLCompressedTextureATC.h" 43 #include "core/html/canvas/WebGLCompressedTextureETC1.h" 44 #include "core/html/canvas/WebGLCompressedTexturePVRTC.h" 45 #include "core/html/canvas/WebGLCompressedTextureS3TC.h" 46 #include "core/html/canvas/WebGLContextAttributes.h" 47 #include "core/html/canvas/WebGLContextEvent.h" 48 #include "core/html/canvas/WebGLDebugRendererInfo.h" 49 #include "core/html/canvas/WebGLDebugShaders.h" 50 #include "core/html/canvas/WebGLDepthTexture.h" 51 #include "core/html/canvas/WebGLDrawBuffers.h" 52 #include "core/html/canvas/WebGLLoseContext.h" 53 #include "core/loader/FrameLoader.h" 54 #include "core/loader/FrameLoaderClient.h" 55 #include "core/frame/Settings.h" 56 #include "core/rendering/RenderBox.h" 57 #include "platform/CheckedInt.h" 58 #include "platform/NotImplemented.h" 59 #include "platform/graphics/gpu/DrawingBuffer.h" 60 #include "public/platform/Platform.h" 61 62 namespace WebCore { 63 64 PassOwnPtrWillBeRawPtr<WebGLRenderingContext> WebGLRenderingContext::create(HTMLCanvasElement* canvas, WebGLContextAttributes* attrs) 65 { 66 Document& document = canvas->document(); 67 LocalFrame* frame = document.frame(); 68 if (!frame) { 69 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Web page was not allowed to create a WebGL context.")); 70 return nullptr; 71 } 72 Settings* settings = frame->settings(); 73 74 // The FrameLoaderClient might block creation of a new WebGL context despite the page settings; in 75 // particular, if WebGL contexts were lost one or more times via the GL_ARB_robustness extension. 76 if (!frame->loader().client()->allowWebGL(settings && settings->webGLEnabled())) { 77 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Web page was not allowed to create a WebGL context.")); 78 return nullptr; 79 } 80 81 // The only situation that attrs is null is through Document::getCSSCanvasContext(). 82 RefPtr<WebGLContextAttributes> defaultAttrs; 83 if (!attrs) { 84 defaultAttrs = WebGLContextAttributes::create(); 85 attrs = defaultAttrs.get(); 86 } 87 blink::WebGraphicsContext3D::Attributes attributes = attrs->attributes(document.topDocument().url().string(), settings); 88 OwnPtr<blink::WebGraphicsContext3D> context = adoptPtr(blink::Platform::current()->createOffscreenGraphicsContext3D(attributes, 0)); 89 if (!context || !context->makeContextCurrent()) { 90 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Could not create a WebGL context.")); 91 return nullptr; 92 } 93 94 OwnPtr<Extensions3DUtil> extensionsUtil = Extensions3DUtil::create(context.get()); 95 if (!extensionsUtil) 96 return nullptr; 97 if (extensionsUtil->supportsExtension("GL_EXT_debug_marker")) 98 context->pushGroupMarkerEXT("WebGLRenderingContext"); 99 100 OwnPtrWillBeRawPtr<WebGLRenderingContext> renderingContext = adoptPtrWillBeNoop(new WebGLRenderingContext(canvas, context.release(), attrs)); 101 renderingContext->registerContextExtensions(); 102 renderingContext->suspendIfNeeded(); 103 104 if (!renderingContext->m_drawingBuffer) { 105 canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Could not create a WebGL context.")); 106 return nullptr; 107 } 108 109 return renderingContext.release(); 110 } 111 112 WebGLRenderingContext::WebGLRenderingContext(HTMLCanvasElement* passedCanvas, PassOwnPtr<blink::WebGraphicsContext3D> context, WebGLContextAttributes* requestedAttributes) 113 : WebGLRenderingContextBase(passedCanvas, context, requestedAttributes) 114 { 115 ScriptWrappable::init(this); 116 } 117 118 WebGLRenderingContext::~WebGLRenderingContext() 119 { 120 121 } 122 123 void WebGLRenderingContext::registerContextExtensions() 124 { 125 // Register extensions. 126 static const char* const webkitPrefix[] = { "WEBKIT_", 0, }; 127 static const char* const bothPrefixes[] = { "", "WEBKIT_", 0, }; 128 129 registerExtension<ANGLEInstancedArrays>(m_angleInstancedArrays); 130 registerExtension<EXTTextureFilterAnisotropic>(m_extTextureFilterAnisotropic, ApprovedExtension, bothPrefixes); 131 registerExtension<OESElementIndexUint>(m_oesElementIndexUint); 132 registerExtension<OESStandardDerivatives>(m_oesStandardDerivatives); 133 registerExtension<OESTextureFloat>(m_oesTextureFloat); 134 registerExtension<OESTextureFloatLinear>(m_oesTextureFloatLinear); 135 registerExtension<OESTextureHalfFloat>(m_oesTextureHalfFloat); 136 registerExtension<OESTextureHalfFloatLinear>(m_oesTextureHalfFloatLinear); 137 registerExtension<OESVertexArrayObject>(m_oesVertexArrayObject); 138 registerExtension<WebGLCompressedTextureATC>(m_webglCompressedTextureATC, EnabledDraftExtension, webkitPrefix); 139 registerExtension<WebGLCompressedTexturePVRTC>(m_webglCompressedTexturePVRTC, EnabledDraftExtension, webkitPrefix); 140 registerExtension<WebGLCompressedTextureS3TC>(m_webglCompressedTextureS3TC, ApprovedExtension, bothPrefixes); 141 registerExtension<WebGLDebugRendererInfo>(m_webglDebugRendererInfo); 142 registerExtension<WebGLDebugShaders>(m_webglDebugShaders); 143 registerExtension<WebGLDepthTexture>(m_webglDepthTexture, ApprovedExtension, bothPrefixes); 144 registerExtension<WebGLDrawBuffers>(m_webglDrawBuffers); 145 registerExtension<WebGLLoseContext>(m_webglLoseContext, ApprovedExtension, bothPrefixes); 146 147 // Register draft extensions. 148 registerExtension<EXTBlendMinMax>(m_extBlendMinMax, DraftExtension); 149 registerExtension<EXTFragDepth>(m_extFragDepth, DraftExtension); 150 registerExtension<EXTShaderTextureLOD>(m_extShaderTextureLOD, DraftExtension); 151 registerExtension<WebGLCompressedTextureETC1>(m_webglCompressedTextureETC1, DraftExtension); 152 } 153 154 } // namespace WebCore 155