WebGL not supported on WKWebView on UIKit for Mac
https://bugs.webkit.org/show_bug.cgi?id=199785
<rdar://problem/52911449>

Reviewed by Antoine Quint.

Source/WebCore:

UIKit for Mac was not creating a CGLPixelFormatObj because
it wasn't using the code hidden in PLATFORM(MAC). Instead
we should be guarding for USE(OPENGL).

There are still some inconsistencies: <rdar://53062794>

Test: webgl/smell-test.html

* platform/graphics/cocoa/GraphicsContext3DCocoa.mm:
(WebCore::GraphicsContext3D::GraphicsContext3D):
(WebCore::GraphicsContext3D::allowOfflineRenderers const): We have to return
true for this, since we don't have access to the Window Server.

Source/WTF:

MacCatalyst has Apple Graphics Control, although
this area is very messy, see <rdar://53062794>.

* wtf/Platform.h:

LayoutTests:

Even though we don't yet run tests on UIKit for Mac, we
should have the most simple "is WebGL working?" ref test.

* webgl/smell-test-expected.html: Added.
* webgl/smell-test.html: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@247422 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 0ac5b32..8f55d72 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,17 @@
+2019-07-14  Dean Jackson  <dino@apple.com>
+
+        WebGL not supported on WKWebView on UIKit for Mac
+        https://bugs.webkit.org/show_bug.cgi?id=199785
+        <rdar://problem/52911449>
+
+        Reviewed by Antoine Quint.
+
+        Even though we don't yet run tests on UIKit for Mac, we
+        should have the most simple "is WebGL working?" ref test.
+
+        * webgl/smell-test-expected.html: Added.
+        * webgl/smell-test.html: Added.
+
 2019-07-13  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [Text autosizing] [iPadOS] Further adjust our heuristics to determine text autosizing candidates
diff --git a/LayoutTests/webgl/smell-test-expected.html b/LayoutTests/webgl/smell-test-expected.html
new file mode 100644
index 0000000..4ac079a
--- /dev/null
+++ b/LayoutTests/webgl/smell-test-expected.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<style>
+canvas {
+    width: 200px;
+    height: 200px;
+}
+</style>
+<canvas></canvas>
+<script>
+function paint() {
+    const canvas = document.querySelector("canvas");
+    canvas.width = 200;
+    canvas.height = 200;
+    const ctx = canvas.getContext("2d");
+    ctx.fillStyle = "rgb(0, 255, 0)";
+    ctx.fillRect(0, 0, 200, 200);
+}
+window.addEventListener("load", paint, false);
+</script>
diff --git a/LayoutTests/webgl/smell-test.html b/LayoutTests/webgl/smell-test.html
new file mode 100644
index 0000000..04f38e7
--- /dev/null
+++ b/LayoutTests/webgl/smell-test.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<style>
+canvas {
+    width: 200px;
+    height: 200px;
+}
+</style>
+<canvas></canvas>
+<script>
+function paint() {
+    const canvas = document.querySelector("canvas");
+    canvas.width = 200;
+    canvas.height = 200;
+    const gl = canvas.getContext("webgl");
+    gl.clearColor(0, 1, 0, 1);
+    gl.clear(gl.COLOR_BUFFER_BIT);
+}
+window.addEventListener("load", paint, false);
+</script>
diff --git a/Source/WTF/ChangeLog b/Source/WTF/ChangeLog
index 0151ba9..88ed0b9 100644
--- a/Source/WTF/ChangeLog
+++ b/Source/WTF/ChangeLog
@@ -1,3 +1,16 @@
+2019-07-14  Dean Jackson  <dino@apple.com>
+
+        WebGL not supported on WKWebView on UIKit for Mac
+        https://bugs.webkit.org/show_bug.cgi?id=199785
+        <rdar://problem/52911449>
+
+        Reviewed by Antoine Quint.
+
+        MacCatalyst has Apple Graphics Control, although
+        this area is very messy, see <rdar://53062794>.
+
+        * wtf/Platform.h:
+
 2019-07-11  Myles C. Maxfield  <mmaxfield@apple.com>
 
         New York font erroneously gets synthetic bold
diff --git a/Source/WTF/wtf/Platform.h b/Source/WTF/wtf/Platform.h
index 57b59e1..5237af2 100644
--- a/Source/WTF/wtf/Platform.h
+++ b/Source/WTF/wtf/Platform.h
@@ -1217,8 +1217,11 @@
 #define USE_REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR 1
 #endif
 
-#if PLATFORM(MAC)
+#if PLATFORM(MAC) || PLATFORM(MACCATALYST)
 #define HAVE_APPLE_GRAPHICS_CONTROL 1
+#endif
+
+#if PLATFORM(MAC)
 #define USE_COREAUDIO 1
 #endif
 
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 7ac2fc0..130c7f6 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,24 @@
+2019-07-14  Dean Jackson  <dino@apple.com>
+
+        WebGL not supported on WKWebView on UIKit for Mac
+        https://bugs.webkit.org/show_bug.cgi?id=199785
+        <rdar://problem/52911449>
+
+        Reviewed by Antoine Quint.
+
+        UIKit for Mac was not creating a CGLPixelFormatObj because
+        it wasn't using the code hidden in PLATFORM(MAC). Instead
+        we should be guarding for USE(OPENGL).
+
+        There are still some inconsistencies: <rdar://53062794>
+
+        Test: webgl/smell-test.html
+
+        * platform/graphics/cocoa/GraphicsContext3DCocoa.mm:
+        (WebCore::GraphicsContext3D::GraphicsContext3D):
+        (WebCore::GraphicsContext3D::allowOfflineRenderers const): We have to return
+        true for this, since we don't have access to the Window Server.
+
 2019-07-13  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [Text autosizing] [iPadOS] Further adjust our heuristics to determine text autosizing candidates
diff --git a/Source/WebCore/platform/graphics/cocoa/GraphicsContext3DCocoa.mm b/Source/WebCore/platform/graphics/cocoa/GraphicsContext3DCocoa.mm
index e3e287d..0757fa6 100644
--- a/Source/WebCore/platform/graphics/cocoa/GraphicsContext3DCocoa.mm
+++ b/Source/WebCore/platform/graphics/cocoa/GraphicsContext3DCocoa.mm
@@ -180,7 +180,7 @@
     return context;
 }
 
-#if PLATFORM(MAC) && USE(OPENGL)
+#if PLATFORM(MAC) && USE(OPENGL) // FIXME: This probably should be just USE(OPENGL) - see <rdar://53062794>.
 
 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
 static void setGPUByRegistryID(PlatformGraphicsContext3D contextObj, CGLPixelFormatObj pixelFormatObj, IORegistryGPUID preferredGPUID)
@@ -265,11 +265,8 @@
     : m_attrs(attrs)
     , m_private(std::make_unique<GraphicsContext3DPrivate>(this))
 {
-#if USE(ANGLE)
-    // In the ANGLE backend, the only shader compiler instantiated is
-    // the one ANGLE uses internally.
-#else
-#if PLATFORM(IOS_FAMILY)
+#if !USE(ANGLE)
+#if USE(OPENGL_ES)
     if (m_attrs.isWebGL2)
         m_compiler = ANGLEWebKitBridge(SH_ESSL_OUTPUT, SH_WEBGL2_SPEC);
     else
@@ -277,8 +274,8 @@
 #else
     if (m_attrs.isWebGL2)
         m_compiler = ANGLEWebKitBridge(SH_GLSL_410_CORE_OUTPUT, SH_WEBGL2_SPEC);
-#endif // PLATFORM(IOS_FAMILY)
-#endif // USE(ANGLE)
+#endif // USE(OPENGL_ES)
+#endif // !USE(ANGLE)
 
 #if USE(OPENGL_ES)
     UNUSED_PARAM(hostWindow);
@@ -296,6 +293,12 @@
     CGLPixelFormatObj pixelFormatObj = 0;
     GLint numPixelFormats = 0;
     
+#if HAVE(APPLE_GRAPHICS_CONTROL)
+    m_powerPreferenceUsedForCreation = (hasLowAndHighPowerGPUs() && attrs.powerPreference == GraphicsContext3DPowerPreference::HighPerformance) ? GraphicsContext3DPowerPreference::HighPerformance : GraphicsContext3DPowerPreference::Default;
+#else
+    m_powerPreferenceUsedForCreation = GraphicsContext3DPowerPreference::Default;
+#endif
+
     // If we're configured to demand the software renderer, we'll
     // do so. We attempt to create contexts in this order:
     //
@@ -308,31 +311,30 @@
     //
     // 4) closest to 32 bit RGBA/16 bit depth/software renderer
     //
-    // If none of that works, we simply fail and set m_contextObj to 0.
+    // If none of that works, we fail and leave m_contextObj as nullptr.
 
     bool useMultisampling = m_attrs.antialias;
 
-#if HAVE(APPLE_GRAPHICS_CONTROL)
-    m_powerPreferenceUsedForCreation = (hasLowAndHighPowerGPUs() && attrs.powerPreference == GraphicsContext3DPowerPreference::HighPerformance) ? GraphicsContext3DPowerPreference::HighPerformance : GraphicsContext3DPowerPreference::Default;
-#else
-    m_powerPreferenceUsedForCreation = GraphicsContext3DPowerPreference::Default;
-#endif
-
     setPixelFormat(attribs, 32, 32, !attrs.forceSoftwareRenderer, true, false, useMultisampling, attrs.isWebGL2, allowOfflineRenderers());
     CGLChoosePixelFormat(attribs.data(), &pixelFormatObj, &numPixelFormats);
 
     if (!numPixelFormats) {
-        setPixelFormat(attribs, 32, 32, !attrs.forceSoftwareRenderer, false, false, useMultisampling, attrs.isWebGL2, allowOfflineRenderers());
+        setPixelFormat(attribs, 32, 32, !attrs.forceSoftwareRenderer, true, false, useMultisampling, attrs.isWebGL2, allowOfflineRenderers());
         CGLChoosePixelFormat(attribs.data(), &pixelFormatObj, &numPixelFormats);
 
         if (!numPixelFormats) {
-            setPixelFormat(attribs, 32, 16, !attrs.forceSoftwareRenderer, false, false, useMultisampling, attrs.isWebGL2, allowOfflineRenderers());
+            setPixelFormat(attribs, 32, 32, !attrs.forceSoftwareRenderer, false, false, useMultisampling, attrs.isWebGL2, allowOfflineRenderers());
             CGLChoosePixelFormat(attribs.data(), &pixelFormatObj, &numPixelFormats);
 
-            if (!attrs.forceSoftwareRenderer && !numPixelFormats) {
-                setPixelFormat(attribs, 32, 16, false, false, true, false, attrs.isWebGL2, allowOfflineRenderers());
+            if (!numPixelFormats) {
+                setPixelFormat(attribs, 32, 16, !attrs.forceSoftwareRenderer, false, false, useMultisampling, attrs.isWebGL2, allowOfflineRenderers());
                 CGLChoosePixelFormat(attribs.data(), &pixelFormatObj, &numPixelFormats);
-                useMultisampling = false;
+
+                if (!attrs.forceSoftwareRenderer && !numPixelFormats) {
+                    setPixelFormat(attribs, 32, 16, false, false, true, false, attrs.isWebGL2, allowOfflineRenderers());
+                    CGLChoosePixelFormat(attribs.data(), &pixelFormatObj, &numPixelFormats);
+                    useMultisampling = false;
+                }
             }
         }
     }
@@ -344,7 +346,7 @@
     GLint abortOnBlacklist = 0;
     CGLSetParameter(m_contextObj, kCGLCPAbortOnGPURestartStatusBlacklisted, &abortOnBlacklist);
     
-#if PLATFORM(MAC)
+#if PLATFORM(MAC) // FIXME: This probably should be USE(OPENGL) - see <rdar://53062794>.
 
 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
     auto gpuID = (hostWindow && hostWindow->displayID()) ? gpuIDForDisplay(hostWindow->displayID()) : primaryGPUID();
@@ -814,6 +816,10 @@
     // m_displayMask will not be set in this case.
     if (primaryOpenGLDisplayMask())
         return true;
+#elif PLATFORM(MACCATALYST)
+    // FIXME: <rdar://53062794> We're very inconsistent about WEBPROCESS_WINDOWSERVER_BLOCKING
+    // and MAC/MACCATALYST and OPENGL/OPENGLES.
+    return true;
 #endif
         
 #if HAVE(APPLE_GRAPHICS_CONTROL)