Disabling WebGL2 should mean no context is created
https://bugs.webkit.org/show_bug.cgi?id=157352
<rdar://problem/26096346>

Reviewed by Eric Carlson.

Source/WebCore:

If WebGL2 is disabled by the runtime flag, we should
not create a context.

Test: fast/canvas/webgl/webgl2-runtime-flag.html

* html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::create):
* testing/Internals.cpp: Added some helpers to toggle the runtime setting.
(WebCore::Internals::webGL2Enabled):
(WebCore::Internals::setWebGL2Enabled):
* testing/Internals.h:
* testing/Internals.idl:

LayoutTests:

Check that toggling the runtime flag enables/disables WebGL 2.0.

* fast/canvas/webgl/webgl2-runtime-flag-expected.txt: Added.
* fast/canvas/webgl/webgl2-runtime-flag.html: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@200435 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index f31c055..2bc4e52 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,16 @@
+2016-05-04  Dean Jackson  <dino@apple.com>
+
+        Disabling WebGL2 should mean no context is created
+        https://bugs.webkit.org/show_bug.cgi?id=157352
+        <rdar://problem/26096346>
+
+        Reviewed by Eric Carlson.
+
+        Check that toggling the runtime flag enables/disables WebGL 2.0.
+
+        * fast/canvas/webgl/webgl2-runtime-flag-expected.txt: Added.
+        * fast/canvas/webgl/webgl2-runtime-flag.html: Added.
+
 2016-05-04  Chris Dumez  <cdumez@apple.com>
 
         Media elements should not be paused right away when removed from the document
diff --git a/LayoutTests/fast/canvas/webgl/webgl2-runtime-flag-expected.txt b/LayoutTests/fast/canvas/webgl/webgl2-runtime-flag-expected.txt
new file mode 100644
index 0000000..79bacac
--- /dev/null
+++ b/LayoutTests/fast/canvas/webgl/webgl2-runtime-flag-expected.txt
@@ -0,0 +1,2 @@
+PASS: WebGL 2.0 was available when enabled.
+PASS: WebGL 2.0 was not available when disabled.
diff --git a/LayoutTests/fast/canvas/webgl/webgl2-runtime-flag.html b/LayoutTests/fast/canvas/webgl/webgl2-runtime-flag.html
new file mode 100644
index 0000000..1b94576
--- /dev/null
+++ b/LayoutTests/fast/canvas/webgl/webgl2-runtime-flag.html
@@ -0,0 +1,46 @@
+<script>
+
+if (window.testRunner)
+    window.testRunner.dumpAsText();
+
+function getContext(name) {
+    var c = document.createElement("canvas");
+    return c.getContext(name);
+}
+
+function run() {
+    var output;
+
+    output = document.getElementById("output1");
+
+    if (!window.testRunner) {
+        output.textContent = "This test only runs inside DRT/WKTR.";
+        return;
+    }
+
+    var originalSetting = window.internals.webGL2Enabled();
+
+    window.internals.setWebGL2Enabled(true);
+    var ctx = getContext("webgl2");
+    if (ctx)
+        output.textContent = "PASS: WebGL 2.0 was available when enabled.";
+    else
+        output.textContent = "FAIL: WebGL 2.0 was not available when enabled.";
+
+    output = document.getElementById("output2");
+
+    window.internals.setWebGL2Enabled(false);
+    var ctx = getContext("webgl2");
+    if (ctx)
+        output.textContent = "FAIL: WebGL 2.0 was available when disabled.";
+    else
+        output.textContent = "PASS: WebGL 2.0 was not available when disabled.";
+
+    window.internals.setWebGL2Enabled(originalSetting);
+}
+
+window.addEventListener("load", run, false);
+
+</script>
+<div id="output1"></div>
+<div id="output2"></div>
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 639ff6a..eeb5e2a 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,24 @@
+2016-05-04  Dean Jackson  <dino@apple.com>
+
+        Disabling WebGL2 should mean no context is created
+        https://bugs.webkit.org/show_bug.cgi?id=157352
+        <rdar://problem/26096346>
+
+        Reviewed by Eric Carlson.
+
+        If WebGL2 is disabled by the runtime flag, we should
+        not create a context.
+
+        Test: fast/canvas/webgl/webgl2-runtime-flag.html
+
+        * html/canvas/WebGLRenderingContextBase.cpp:
+        (WebCore::WebGLRenderingContextBase::create):
+        * testing/Internals.cpp: Added some helpers to toggle the runtime setting.
+        (WebCore::Internals::webGL2Enabled):
+        (WebCore::Internals::setWebGL2Enabled):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
 2016-05-04  Anders Carlsson  <andersca@apple.com>
 
         Add an override point for drawing named images in ThemeCocoa
diff --git a/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp b/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp
index 697cc82..a4af07d 100644
--- a/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp
+++ b/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp
@@ -62,6 +62,7 @@
 #include "OESVertexArrayObject.h"
 #include "Page.h"
 #include "RenderBox.h"
+#include "RuntimeEnabledFeatures.h"
 #include "Settings.h"
 #include "WebGL2RenderingContext.h"
 #include "WebGLActiveInfo.h"
@@ -348,7 +349,10 @@
 
 std::unique_ptr<WebGLRenderingContextBase> WebGLRenderingContextBase::create(HTMLCanvasElement* canvas, WebGLContextAttributes* attrs, const String& type)
 {
-#if !ENABLE(WEBGL2)
+#if ENABLE(WEBGL2)
+    if (type == "webgl2" && !RuntimeEnabledFeatures::sharedFeatures().webGL2Enabled())
+        return nullptr;
+#else
     UNUSED_PARAM(type);
 #endif
 
diff --git a/Source/WebCore/testing/Internals.cpp b/Source/WebCore/testing/Internals.cpp
index 56fda69..a4ce7e0 100644
--- a/Source/WebCore/testing/Internals.cpp
+++ b/Source/WebCore/testing/Internals.cpp
@@ -3316,6 +3316,18 @@
 }
 #endif
 
+#if ENABLE(WEBGL2)
+bool Internals::webGL2Enabled() const
+{
+    return RuntimeEnabledFeatures::sharedFeatures().webGL2Enabled();
+}
+
+void Internals::setWebGL2Enabled(bool enable)
+{
+    RuntimeEnabledFeatures::sharedFeatures().setWebGL2Enabled(enable);
+}
+#endif
+
 void Internals::setResourceTimingSupport(bool enable)
 {
     RuntimeEnabledFeatures::sharedFeatures().setResourceTimingEnabled(enable);
diff --git a/Source/WebCore/testing/Internals.h b/Source/WebCore/testing/Internals.h
index 11f8c88..5f2abb3 100644
--- a/Source/WebCore/testing/Internals.h
+++ b/Source/WebCore/testing/Internals.h
@@ -478,6 +478,11 @@
     void setCSSGridLayoutEnabled(bool);
 #endif
 
+#if ENABLE(WEBGL2)
+    bool webGL2Enabled() const;
+    void setWebGL2Enabled(bool);
+#endif
+
 private:
     explicit Internals(Document&);
     Document* contextDocument() const;
diff --git a/Source/WebCore/testing/Internals.idl b/Source/WebCore/testing/Internals.idl
index b34897c..4dc4031 100644
--- a/Source/WebCore/testing/Internals.idl
+++ b/Source/WebCore/testing/Internals.idl
@@ -453,5 +453,10 @@
     void setCSSGridLayoutEnabled(boolean enable);
 #endif
 
+#if defined(ENABLE_WEBGL2) && ENABLE_WEBGL2
+    boolean webGL2Enabled();
+    void setWebGL2Enabled(boolean enable);
+#endif
+
     void setResourceTimingSupport(boolean scalable);
 };