+#include "eglimage.h"
+#include "eglsync.h"
+
+/* Includes for _eglNativePlatformDetectNativeDisplay */
+#ifdef HAVE_WAYLAND_PLATFORM
+#include <wayland-client.h>
+#endif
+#ifdef HAVE_DRM_PLATFORM
+#include <gbm.h>
+#endif
+
+
+/**
+ * Map build-system platform names to platform types.
+ */
+static const struct {
+ _EGLPlatformType platform;
+ const char *name;
+} egl_platforms[_EGL_NUM_PLATFORMS] = {
+ { _EGL_PLATFORM_X11, "x11" },
+ { _EGL_PLATFORM_WAYLAND, "wayland" },
+ { _EGL_PLATFORM_DRM, "drm" },
+ { _EGL_PLATFORM_ANDROID, "android" },
+ { _EGL_PLATFORM_HAIKU, "haiku" },
+ { _EGL_PLATFORM_SURFACELESS, "surfaceless" },
+};
+
+
+/**
+ * Return the native platform by parsing EGL_PLATFORM.
+ */
+static _EGLPlatformType
+_eglGetNativePlatformFromEnv(void)
+{
+ _EGLPlatformType plat = _EGL_INVALID_PLATFORM;
+ const char *plat_name;
+ EGLint i;
+
+ plat_name = getenv("EGL_PLATFORM");
+ /* try deprecated env variable */
+ if (!plat_name || !plat_name[0])
+ plat_name = getenv("EGL_DISPLAY");
+ if (!plat_name || !plat_name[0])
+ return _EGL_INVALID_PLATFORM;
+
+ for (i = 0; i < _EGL_NUM_PLATFORMS; i++) {
+ if (strcmp(egl_platforms[i].name, plat_name) == 0) {
+ plat = egl_platforms[i].platform;
+ break;
+ }
+ }
+
+ return plat;
+}
+
+
+/**
+ * Try detecting native platform with the help of native display characteristcs.
+ */
+static _EGLPlatformType
+_eglNativePlatformDetectNativeDisplay(void *nativeDisplay)
+{
+ if (nativeDisplay == EGL_DEFAULT_DISPLAY)
+ return _EGL_INVALID_PLATFORM;
+
+ if (_eglPointerIsDereferencable(nativeDisplay)) {
+ void *first_pointer = *(void **) nativeDisplay;
+
+ (void) first_pointer; /* silence unused var warning */
+
+#ifdef HAVE_WAYLAND_PLATFORM
+ /* wl_display is a wl_proxy, which is a wl_object.
+ * wl_object's first element points to the interfacetype. */
+ if (first_pointer == &wl_display_interface)
+ return _EGL_PLATFORM_WAYLAND;
+#endif
+
+#ifdef HAVE_DRM_PLATFORM
+ /* gbm has a pointer to its constructor as first element. */
+ if (first_pointer == gbm_create_device)
+ return _EGL_PLATFORM_DRM;
+#endif
+
+#ifdef HAVE_X11_PLATFORM
+ /* If not matched to any other platform, fallback to x11. */
+ return _EGL_PLATFORM_X11;
+#endif
+
+#ifdef HAVE_HAIKU_PLATFORM
+ return _EGL_PLATFORM_HAIKU;
+#endif
+ }
+
+ return _EGL_INVALID_PLATFORM;
+}
+
+
+/**
+ * Return the native platform. It is the platform of the EGL native types.
+ */
+_EGLPlatformType
+_eglGetNativePlatform(void *nativeDisplay)
+{
+ static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
+ _EGLPlatformType detected_platform = native_platform;
+
+ if (detected_platform == _EGL_INVALID_PLATFORM) {
+ const char *detection_method;
+
+ detected_platform = _eglGetNativePlatformFromEnv();
+ detection_method = "environment overwrite";
+
+ if (detected_platform == _EGL_INVALID_PLATFORM) {
+ detected_platform = _eglNativePlatformDetectNativeDisplay(nativeDisplay);
+ detection_method = "autodetected";
+ }
+
+ if (detected_platform == _EGL_INVALID_PLATFORM) {
+ detected_platform = _EGL_NATIVE_PLATFORM;
+ detection_method = "build-time configuration";
+ }
+
+ _eglLog(_EGL_DEBUG, "Native platform type: %s (%s)",
+ egl_platforms[detected_platform].name, detection_method);
+
+ p_atomic_cmpxchg(&native_platform, _EGL_INVALID_PLATFORM,
+ detected_platform);
+ }
+
+ return native_platform;
+}