X11 and Ozone: Try to check for display type when creating framebuf

We're making a new config in the Chromium project for Linux builds -
USE_X11 && USE_OZONE. The idea of that is to ease migration
from non-Ozone/X11 to Ozone. It will be possible to choose between
Ozone and non-Ozone path on runtime by supplying
the --enable-features=UseOzonePlatform flag.

However, SwiftShader doesn't support such a build and always uses
FrameBufferOzone. This is not correct when the browser is
run without the Ozone feature enabled aka using old
non-Ozone/X11 path. Basically, it results in crashes
during swap buffers call.

The very naive solution is to try to test the display. If we
can cast it to XDisplay and its valid, use X11 framebuffer.
Otherwise, use Ozone. Though, I'm not 100% sure about the safiness
of this check. Thus, we must be careful about that.

This is one of the last patches that blocks enabling both
use_x11 && use_ozone in Chromium.

Bug: chromium:1119303
Change-Id: I969798abd4d0bbacaffb7704161eac38e77d9311
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/47868
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Maksim Sisov <msisov@igalia.com>
diff --git a/src/Main/BUILD.gn b/src/Main/BUILD.gn
index 22da32f..0658d66 100644
--- a/src/Main/BUILD.gn
+++ b/src/Main/BUILD.gn
@@ -48,14 +48,10 @@
   ]
 
   if (use_ozone && !is_win) {
-    sources += [ "FrameBufferOzone.cpp" ]
-  } else if (is_linux) {
-    if (use_x11) {
-      sources += [
-        "FrameBufferX11.cpp",
-        "libX11.cpp",
-      ]
-    }
+    sources += [
+      "FrameBufferOzone.cpp",
+      "FrameBufferFactoryOzone.cpp",
+    ]
   } else if (is_mac) {
     sources += [ "FrameBufferOSX.mm" ]
   } else if (is_win) {
@@ -66,6 +62,13 @@
     ]
   }
 
+  if (use_x11) {
+    sources += [
+      "FrameBufferX11.cpp",
+      "libX11.cpp",
+    ]
+  }
+
   if (is_win) {
     libs = [ "dxguid.lib" ]  # For FrameBufferDD
   }
diff --git a/src/Main/FrameBufferFactoryOzone.cpp b/src/Main/FrameBufferFactoryOzone.cpp
new file mode 100644
index 0000000..ef01817
--- /dev/null
+++ b/src/Main/FrameBufferFactoryOzone.cpp
@@ -0,0 +1,33 @@
+// Copyright 2020 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Main/FrameBuffer.hpp"
+
+#include "Main/FrameBufferOzone.hpp"
+
+#if defined(USE_X11)
+#include "libX11.hpp"
+#include "Main/FrameBufferX11.hpp"
+#endif
+
+NO_SANITIZE_FUNCTION sw::FrameBuffer *createFrameBuffer(void *display, intptr_t window, int width, int height)
+{
+#if defined(USE_X11)
+	if(reinterpret_cast<::Display *>(display))
+	{
+		return new sw::FrameBufferX11((::Display *)display, window, width, height);
+	}
+#endif
+	return new sw::FrameBufferOzone((intptr_t)display, window, width, height);
+}
diff --git a/src/Main/FrameBufferOzone.cpp b/src/Main/FrameBufferOzone.cpp
index 95e0729..db00adf 100644
--- a/src/Main/FrameBufferOzone.cpp
+++ b/src/Main/FrameBufferOzone.cpp
@@ -47,8 +47,3 @@
 		copy(source);
 	}
 }
-
-NO_SANITIZE_FUNCTION sw::FrameBuffer *createFrameBuffer(void* display, intptr_t window, int width, int height)
-{
-	return new sw::FrameBufferOzone((intptr_t)display, window, width, height);
-}
diff --git a/src/Main/FrameBufferX11.cpp b/src/Main/FrameBufferX11.cpp
index b3ae3b4..1b76bf2 100644
--- a/src/Main/FrameBufferX11.cpp
+++ b/src/Main/FrameBufferX11.cpp
@@ -186,7 +186,11 @@
 	}
 }
 
+// Chromium can be built with USE_X11 && USE_OZONE. In case of pure USE_X11 builds, we can use this path.
+// In another case, the decision what framebuffer to create will be done by the FrameBufferFactoryOzone.
+#if !defined(USE_OZONE)
 NO_SANITIZE_FUNCTION sw::FrameBuffer *createFrameBuffer(void *display, Window window, int width, int height)
 {
 	return new sw::FrameBufferX11((::Display*)display, window, width, height);
 }
+#endif