Update To 11.40.268.0
[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/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/EXTsRGB.h"
36 #include "core/html/canvas/OESElementIndexUint.h"
37 #include "core/html/canvas/OESStandardDerivatives.h"
38 #include "core/html/canvas/OESTextureFloat.h"
39 #include "core/html/canvas/OESTextureFloatLinear.h"
40 #include "core/html/canvas/OESTextureHalfFloat.h"
41 #include "core/html/canvas/OESTextureHalfFloatLinear.h"
42 #include "core/html/canvas/OESVertexArrayObject.h"
43 #include "core/html/canvas/WebGLCompressedTextureATC.h"
44 #include "core/html/canvas/WebGLCompressedTextureETC1.h"
45 #include "core/html/canvas/WebGLCompressedTexturePVRTC.h"
46 #include "core/html/canvas/WebGLCompressedTextureS3TC.h"
47 #include "core/html/canvas/WebGLContextAttributes.h"
48 #include "core/html/canvas/WebGLContextEvent.h"
49 #include "core/html/canvas/WebGLDebugRendererInfo.h"
50 #include "core/html/canvas/WebGLDebugShaders.h"
51 #include "core/html/canvas/WebGLDepthTexture.h"
52 #include "core/html/canvas/WebGLDrawBuffers.h"
53 #include "core/html/canvas/WebGLLoseContext.h"
54 #include "core/loader/FrameLoader.h"
55 #include "core/loader/FrameLoaderClient.h"
56 #include "core/frame/Settings.h"
57 #include "core/rendering/RenderBox.h"
58 #include "platform/CheckedInt.h"
59 #include "platform/graphics/gpu/DrawingBuffer.h"
60 #include "public/platform/Platform.h"
61
62 namespace blink {
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     RefPtrWillBeRawPtr<WebGLContextAttributes> defaultAttrs = nullptr;
83     if (!attrs) {
84         defaultAttrs = WebGLContextAttributes::create();
85         attrs = defaultAttrs.get();
86     }
87     blink::WebGraphicsContext3D::Attributes attributes = attrs->attributes(document.topDocument().url().string(), settings, 1);
88     blink::WebGLInfo glInfo;
89     OwnPtr<blink::WebGraphicsContext3D> context = adoptPtr(blink::Platform::current()->createOffscreenGraphicsContext3D(attributes, 0, &glInfo));
90     if (!context) {
91         String statusMessage("Could not create a WebGL context for VendorInfo = ");
92         statusMessage.append(glInfo.vendorInfo);
93         statusMessage.append(", RendererInfo = ");
94         statusMessage.append(glInfo.rendererInfo);
95         statusMessage.append(", DriverInfo = ");
96         statusMessage.append(glInfo.driverVersion);
97         statusMessage.append(".");
98         canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, statusMessage));
99         return nullptr;
100     }
101
102     OwnPtr<Extensions3DUtil> extensionsUtil = Extensions3DUtil::create(context.get());
103     if (!extensionsUtil)
104         return nullptr;
105     if (extensionsUtil->supportsExtension("GL_EXT_debug_marker"))
106         context->pushGroupMarkerEXT("WebGLRenderingContext");
107
108     OwnPtrWillBeRawPtr<WebGLRenderingContext> renderingContext = adoptPtrWillBeNoop(new WebGLRenderingContext(canvas, context.release(), attrs));
109     renderingContext->registerContextExtensions();
110     renderingContext->suspendIfNeeded();
111
112     if (!renderingContext->drawingBuffer()) {
113         canvas->dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Could not create a WebGL context."));
114         return nullptr;
115     }
116
117     return renderingContext.release();
118 }
119
120 WebGLRenderingContext::WebGLRenderingContext(HTMLCanvasElement* passedCanvas, PassOwnPtr<blink::WebGraphicsContext3D> context, WebGLContextAttributes* requestedAttributes)
121     : WebGLRenderingContextBase(passedCanvas, context, requestedAttributes)
122 {
123 }
124
125 WebGLRenderingContext::~WebGLRenderingContext()
126 {
127 }
128
129 void WebGLRenderingContext::registerContextExtensions()
130 {
131     // Register extensions.
132     static const char* const bothPrefixes[] = { "", "WEBKIT_", 0, };
133
134     registerExtension<ANGLEInstancedArrays>(m_angleInstancedArrays);
135     registerExtension<EXTBlendMinMax>(m_extBlendMinMax);
136     registerExtension<EXTFragDepth>(m_extFragDepth);
137     registerExtension<EXTShaderTextureLOD>(m_extShaderTextureLOD);
138     registerExtension<EXTsRGB>(m_extsRGB);
139     registerExtension<EXTTextureFilterAnisotropic>(m_extTextureFilterAnisotropic, ApprovedExtension, bothPrefixes);
140     registerExtension<OESElementIndexUint>(m_oesElementIndexUint);
141     registerExtension<OESStandardDerivatives>(m_oesStandardDerivatives);
142     registerExtension<OESTextureFloat>(m_oesTextureFloat);
143     registerExtension<OESTextureFloatLinear>(m_oesTextureFloatLinear);
144     registerExtension<OESTextureHalfFloat>(m_oesTextureHalfFloat);
145     registerExtension<OESTextureHalfFloatLinear>(m_oesTextureHalfFloatLinear);
146     registerExtension<OESVertexArrayObject>(m_oesVertexArrayObject);
147     registerExtension<WebGLCompressedTextureATC>(m_webglCompressedTextureATC, ApprovedExtension, bothPrefixes);
148     registerExtension<WebGLCompressedTextureETC1>(m_webglCompressedTextureETC1);
149     registerExtension<WebGLCompressedTexturePVRTC>(m_webglCompressedTexturePVRTC, ApprovedExtension, bothPrefixes);
150     registerExtension<WebGLCompressedTextureS3TC>(m_webglCompressedTextureS3TC, ApprovedExtension, bothPrefixes);
151     registerExtension<WebGLDebugRendererInfo>(m_webglDebugRendererInfo);
152     registerExtension<WebGLDebugShaders>(m_webglDebugShaders);
153     registerExtension<WebGLDepthTexture>(m_webglDepthTexture, ApprovedExtension, bothPrefixes);
154     registerExtension<WebGLDrawBuffers>(m_webglDrawBuffers);
155     registerExtension<WebGLLoseContext>(m_webglLoseContext, ApprovedExtension, bothPrefixes);
156 }
157
158 void WebGLRenderingContext::trace(Visitor* visitor)
159 {
160     visitor->trace(m_angleInstancedArrays);
161     visitor->trace(m_extBlendMinMax);
162     visitor->trace(m_extFragDepth);
163     visitor->trace(m_extShaderTextureLOD);
164     visitor->trace(m_extsRGB);
165     visitor->trace(m_extTextureFilterAnisotropic);
166     visitor->trace(m_oesTextureFloat);
167     visitor->trace(m_oesTextureFloatLinear);
168     visitor->trace(m_oesTextureHalfFloat);
169     visitor->trace(m_oesTextureHalfFloatLinear);
170     visitor->trace(m_oesStandardDerivatives);
171     visitor->trace(m_oesVertexArrayObject);
172     visitor->trace(m_oesElementIndexUint);
173     visitor->trace(m_webglLoseContext);
174     visitor->trace(m_webglDebugRendererInfo);
175     visitor->trace(m_webglDebugShaders);
176     visitor->trace(m_webglDrawBuffers);
177     visitor->trace(m_webglCompressedTextureATC);
178     visitor->trace(m_webglCompressedTextureETC1);
179     visitor->trace(m_webglCompressedTexturePVRTC);
180     visitor->trace(m_webglCompressedTextureS3TC);
181     visitor->trace(m_webglDepthTexture);
182     WebGLRenderingContextBase::trace(visitor);
183 }
184
185 } // namespace blink