2395126363f109a96b8e3deccb04ee413c9a5655
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / canvas / WebGLRenderingContext.cpp
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/EXTFragDepth.h"
32 #include "core/html/canvas/EXTShaderTextureLOD.h"
33 #include "core/html/canvas/EXTTextureFilterAnisotropic.h"
34 #include "core/html/canvas/OESElementIndexUint.h"
35 #include "core/html/canvas/OESStandardDerivatives.h"
36 #include "core/html/canvas/OESTextureFloat.h"
37 #include "core/html/canvas/OESTextureFloatLinear.h"
38 #include "core/html/canvas/OESTextureHalfFloat.h"
39 #include "core/html/canvas/OESTextureHalfFloatLinear.h"
40 #include "core/html/canvas/OESVertexArrayObject.h"
41 #include "core/html/canvas/WebGLCompressedTextureATC.h"
42 #include "core/html/canvas/WebGLCompressedTextureETC1.h"
43 #include "core/html/canvas/WebGLCompressedTexturePVRTC.h"
44 #include "core/html/canvas/WebGLCompressedTextureS3TC.h"
45 #include "core/html/canvas/WebGLContextAttributes.h"
46 #include "core/html/canvas/WebGLContextEvent.h"
47 #include "core/html/canvas/WebGLDebugRendererInfo.h"
48 #include "core/html/canvas/WebGLDebugShaders.h"
49 #include "core/html/canvas/WebGLDepthTexture.h"
50 #include "core/html/canvas/WebGLDrawBuffers.h"
51 #include "core/html/canvas/WebGLLoseContext.h"
52 #include "core/loader/FrameLoader.h"
53 #include "core/loader/FrameLoaderClient.h"
54 #include "core/frame/Settings.h"
55 #include "core/rendering/RenderBox.h"
56 #include "platform/CheckedInt.h"
57 #include "platform/NotImplemented.h"
58 #include "platform/graphics/gpu/DrawingBuffer.h"
59 #include "public/platform/Platform.h"
60
61 namespace WebCore {
62
63 PassOwnPtrWillBeRawPtr<WebGLRenderingContext> WebGLRenderingContext::create(HTMLCanvasElement* canvas, WebGLContextAttributes* attrs)
64 {
65     Document& document = canvas->document();
66     LocalFrame* frame = document.frame();
67     if (!frame) {
68         canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Web page was not allowed to create a WebGL context."));
69         return nullptr;
70     }
71     Settings* settings = frame->settings();
72
73     // The FrameLoaderClient might block creation of a new WebGL context despite the page settings; in
74     // particular, if WebGL contexts were lost one or more times via the GL_ARB_robustness extension.
75     if (!frame->loader().client()->allowWebGL(settings && settings->webGLEnabled())) {
76         canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Web page was not allowed to create a WebGL context."));
77         return nullptr;
78     }
79
80     // The only situation that attrs is null is through Document::getCSSCanvasContext().
81     RefPtr<WebGLContextAttributes> defaultAttrs;
82     if (!attrs) {
83         defaultAttrs = WebGLContextAttributes::create();
84         attrs = defaultAttrs.get();
85     }
86     blink::WebGraphicsContext3D::Attributes attributes = attrs->attributes(document.topDocument().url().string(), settings);
87     OwnPtr<blink::WebGraphicsContext3D> context = adoptPtr(blink::Platform::current()->createOffscreenGraphicsContext3D(attributes, 0));
88     if (!context || !context->makeContextCurrent()) {
89         canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Could not create a WebGL context."));
90         return nullptr;
91     }
92
93     OwnPtr<Extensions3DUtil> extensionsUtil = Extensions3DUtil::create(context.get());
94     if (!extensionsUtil)
95         return nullptr;
96     if (extensionsUtil->supportsExtension("GL_EXT_debug_marker"))
97         context->pushGroupMarkerEXT("WebGLRenderingContext");
98
99     OwnPtrWillBeRawPtr<WebGLRenderingContext> renderingContext = adoptPtrWillBeNoop(new WebGLRenderingContext(canvas, context.release(), attrs));
100     renderingContext->registerContextExtensions();
101     renderingContext->suspendIfNeeded();
102
103     if (!renderingContext->m_drawingBuffer) {
104         canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Could not create a WebGL context."));
105         return nullptr;
106     }
107
108     return renderingContext.release();
109 }
110
111 WebGLRenderingContext::WebGLRenderingContext(HTMLCanvasElement* passedCanvas, PassOwnPtr<blink::WebGraphicsContext3D> context, WebGLContextAttributes* requestedAttributes)
112     : WebGLRenderingContextBase(passedCanvas, context, requestedAttributes)
113 {
114     ScriptWrappable::init(this);
115 }
116
117 WebGLRenderingContext::~WebGLRenderingContext()
118 {
119
120 }
121
122 void WebGLRenderingContext::registerContextExtensions()
123 {
124     // Register extensions.
125     static const char* const webkitPrefix[] = { "WEBKIT_", 0, };
126     static const char* const bothPrefixes[] = { "", "WEBKIT_", 0, };
127
128     registerExtension<ANGLEInstancedArrays>(m_angleInstancedArrays);
129     registerExtension<EXTTextureFilterAnisotropic>(m_extTextureFilterAnisotropic, ApprovedExtension, bothPrefixes);
130     registerExtension<OESElementIndexUint>(m_oesElementIndexUint);
131     registerExtension<OESStandardDerivatives>(m_oesStandardDerivatives);
132     registerExtension<OESTextureFloat>(m_oesTextureFloat);
133     registerExtension<OESTextureFloatLinear>(m_oesTextureFloatLinear);
134     registerExtension<OESTextureHalfFloat>(m_oesTextureHalfFloat);
135     registerExtension<OESTextureHalfFloatLinear>(m_oesTextureHalfFloatLinear);
136     registerExtension<OESVertexArrayObject>(m_oesVertexArrayObject);
137     registerExtension<WebGLCompressedTextureATC>(m_webglCompressedTextureATC, EnabledDraftExtension, webkitPrefix);
138     registerExtension<WebGLCompressedTexturePVRTC>(m_webglCompressedTexturePVRTC, EnabledDraftExtension, webkitPrefix);
139     registerExtension<WebGLCompressedTextureS3TC>(m_webglCompressedTextureS3TC, ApprovedExtension, bothPrefixes);
140     registerExtension<WebGLDepthTexture>(m_webglDepthTexture, ApprovedExtension, bothPrefixes);
141     registerExtension<WebGLDrawBuffers>(m_webglDrawBuffers);
142     registerExtension<WebGLLoseContext>(m_webglLoseContext, ApprovedExtension, bothPrefixes);
143
144     // Register draft extensions.
145     registerExtension<EXTFragDepth>(m_extFragDepth, DraftExtension);
146     registerExtension<EXTShaderTextureLOD>(m_extShaderTextureLOD, DraftExtension);
147     registerExtension<WebGLCompressedTextureETC1>(m_webglCompressedTextureETC1, DraftExtension);
148
149     // Register privileged extensions.
150     registerExtension<WebGLDebugRendererInfo>(m_webglDebugRendererInfo, WebGLDebugRendererInfoExtension);
151     registerExtension<WebGLDebugShaders>(m_webglDebugShaders, PrivilegedExtension);
152 }
153
154 } // namespace WebCore