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