[GTK] White pages in AC mode: Cannot get default EGL display: EGL_BAD_PARAMETER
https://bugs.webkit.org/show_bug.cgi?id=202362

Reviewed by Carlos Alberto Lopez Perez.

Source/WebCore:

The problem is that PlatformDisplayLibWPE::initialize() is failing to initialize the EGL display for some
reason. We need to understand why, but we should also handle the case of failing to initialize the EGL display
and simply disable accelerated compositing mode to avoid white pages and crashes in websites using WebGL. This
patch doesn't actually fix the bug, it just handles the EGL display initialization failure.

* platform/graphics/PlatformDisplay.cpp:
(WebCore::PlatformDisplay::~PlatformDisplay): Set s_sharedDisplayForCompositing to nullptr when the shared
display for compositing is destroyed.
* platform/graphics/libwpe/PlatformDisplayLibWPE.cpp:
(WebCore::PlatformDisplayLibWPE::initialize): Return false when EGL display initialization fails.
* platform/graphics/libwpe/PlatformDisplayLibWPE.h:

Source/WebKit:

* WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.cpp:
(WebKit::DrawingAreaCoordinatedGraphics::updatePreferences): Disable accelerated compositing mode when we failed
to reate the shared display for compositing.
* WebProcess/glib/WebProcessGLib.cpp:
(WebKit::WebProcess::platformInitializeWebProcess): Destroy the wpe display when initialization fails.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@251122 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 8812361..898baa7 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,22 @@
+2019-10-14  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK] White pages in AC mode: Cannot get default EGL display: EGL_BAD_PARAMETER
+        https://bugs.webkit.org/show_bug.cgi?id=202362
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        The problem is that PlatformDisplayLibWPE::initialize() is failing to initialize the EGL display for some
+        reason. We need to understand why, but we should also handle the case of failing to initialize the EGL display
+        and simply disable accelerated compositing mode to avoid white pages and crashes in websites using WebGL. This
+        patch doesn't actually fix the bug, it just handles the EGL display initialization failure.
+
+        * platform/graphics/PlatformDisplay.cpp:
+        (WebCore::PlatformDisplay::~PlatformDisplay): Set s_sharedDisplayForCompositing to nullptr when the shared
+        display for compositing is destroyed.
+        * platform/graphics/libwpe/PlatformDisplayLibWPE.cpp:
+        (WebCore::PlatformDisplayLibWPE::initialize): Return false when EGL display initialization fails.
+        * platform/graphics/libwpe/PlatformDisplayLibWPE.h:
+
 2019-10-14  Chris Dumez  <cdumez@apple.com>
 
         [WK2] Have WebBackForwardCache class coordinate page caching in all WebProcesses
diff --git a/Source/WebCore/platform/graphics/PlatformDisplay.cpp b/Source/WebCore/platform/graphics/PlatformDisplay.cpp
index 028be1a..f978dbe 100644
--- a/Source/WebCore/platform/graphics/PlatformDisplay.cpp
+++ b/Source/WebCore/platform/graphics/PlatformDisplay.cpp
@@ -150,6 +150,8 @@
 #if USE(EGL)
     ASSERT(m_eglDisplay == EGL_NO_DISPLAY);
 #endif
+    if (s_sharedDisplayForCompositing == this)
+        s_sharedDisplayForCompositing = nullptr;
 }
 
 #if USE(EGL) || USE(GLX)
diff --git a/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.cpp b/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.cpp
index ab10b30..e6962a7 100644
--- a/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.cpp
+++ b/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.cpp
@@ -66,17 +66,18 @@
     wpe_renderer_backend_egl_destroy(m_backend);
 }
 
-void PlatformDisplayLibWPE::initialize(int hostFd)
+bool PlatformDisplayLibWPE::initialize(int hostFd)
 {
     m_backend = wpe_renderer_backend_egl_create(hostFd);
 
     m_eglDisplay = eglGetDisplay(wpe_renderer_backend_egl_get_native_display(m_backend));
     if (m_eglDisplay == EGL_NO_DISPLAY) {
         WTFLogAlways("PlatformDisplayLibWPE: could not create the EGL display: %s.", GLContextEGL::lastErrorString());
-        return;
+        return false;
     }
 
     PlatformDisplay::initializeEGLDisplay();
+    return m_eglDisplay != EGL_NO_DISPLAY;
 }
 
 } // namespace WebCore
diff --git a/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.h b/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.h
index bde26e6..a382445 100644
--- a/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.h
+++ b/Source/WebCore/platform/graphics/libwpe/PlatformDisplayLibWPE.h
@@ -39,7 +39,7 @@
 
     virtual ~PlatformDisplayLibWPE();
 
-    void initialize(int);
+    bool initialize(int);
 
     struct wpe_renderer_backend_egl* backend() const { return m_backend; }
 
diff --git a/Source/WebKit/ChangeLog b/Source/WebKit/ChangeLog
index 2997b0a..13228d3 100644
--- a/Source/WebKit/ChangeLog
+++ b/Source/WebKit/ChangeLog
@@ -1,3 +1,16 @@
+2019-10-14  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GTK] White pages in AC mode: Cannot get default EGL display: EGL_BAD_PARAMETER
+        https://bugs.webkit.org/show_bug.cgi?id=202362
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        * WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.cpp:
+        (WebKit::DrawingAreaCoordinatedGraphics::updatePreferences): Disable accelerated compositing mode when we failed
+        to reate the shared display for compositing.
+        * WebProcess/glib/WebProcessGLib.cpp:
+        (WebKit::WebProcess::platformInitializeWebProcess): Destroy the wpe display when initialization fails.
+
 2019-10-14  Chris Dumez  <cdumez@apple.com>
 
         [WK2] Have WebBackForwardCache class coordinate page caching in all WebProcesses
diff --git a/Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.cpp b/Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.cpp
index dc4157f..39a26b0 100644
--- a/Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.cpp
+++ b/Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.cpp
@@ -235,6 +235,13 @@
 void DrawingAreaCoordinatedGraphics::updatePreferences(const WebPreferencesStore& store)
 {
     Settings& settings = m_webPage.corePage()->settings();
+#if PLATFORM(WAYLAND) && USE(WPE_RENDERER)
+    if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::Wayland
+        && &PlatformDisplay::sharedDisplayForCompositing() == &PlatformDisplay::sharedDisplay()) {
+        // We failed to create the shared display for compositing, disable accelerated compositing.
+        settings.setAcceleratedCompositingEnabled(false);
+    }
+#endif
     settings.setForceCompositingMode(store.getBoolValueForKey(WebPreferencesKey::forceCompositingModeKey()));
     // Fixed position elements need to be composited and create stacking contexts
     // in order to be scrolled by the ScrollingCoordinator.
diff --git a/Source/WebKit/WebProcess/glib/WebProcessGLib.cpp b/Source/WebKit/WebProcess/glib/WebProcessGLib.cpp
index cec7716..9b00374 100644
--- a/Source/WebKit/WebProcess/glib/WebProcessGLib.cpp
+++ b/Source/WebKit/WebProcess/glib/WebProcessGLib.cpp
@@ -74,7 +74,8 @@
             if (hostClientFileDescriptor != -1) {
                 wpe_loader_init(parameters.implementationLibraryName.data());
                 m_wpeDisplay = WebCore::PlatformDisplayLibWPE::create();
-                m_wpeDisplay->initialize(hostClientFileDescriptor);
+                if (!m_wpeDisplay->initialize(hostClientFileDescriptor))
+                    m_wpeDisplay = nullptr;
             }
         }
 #else