egl: fold _eglError() + return EGL_FALSE
[mesa.git] / src / egl / main / egldisplay.c
index 2a1f02738df88e0c29e60a2cb4d6c320185d65da..7aaab3c2c94fc8e9e403c4ad22b83e2e1dc328db 100644 (file)
@@ -36,6 +36,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "c11/threads.h"
+#include "util/u_atomic.h"
 
 #include "eglcontext.h"
 #include "eglcurrent.h"
@@ -180,24 +181,31 @@ _eglNativePlatformDetectNativeDisplay(void *nativeDisplay)
 _EGLPlatformType
 _eglGetNativePlatform(void *nativeDisplay)
 {
-   static _EGLPlatformType native_platform;
-   char *detection_method;
+   static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
+   _EGLPlatformType detected_platform = native_platform;
 
-   native_platform = _eglGetNativePlatformFromEnv();
-   detection_method = "environment overwrite";
+   if (detected_platform == _EGL_INVALID_PLATFORM) {
+      const char *detection_method;
 
-   if (native_platform == _EGL_INVALID_PLATFORM) {
-      native_platform = _eglNativePlatformDetectNativeDisplay(nativeDisplay);
-      detection_method = "autodetected";
-   }
+      detected_platform = _eglGetNativePlatformFromEnv();
+      detection_method = "environment overwrite";
 
-   if (native_platform == _EGL_INVALID_PLATFORM) {
-      native_platform = _EGL_NATIVE_PLATFORM;
-      detection_method = "build-time configuration";
-   }
+      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[native_platform].name, detection_method);
+      _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;
 }
@@ -487,12 +495,10 @@ _eglParseX11DisplayAttribList(_EGLDisplay *display, const EGLint *attrib_list)
       /* EGL_EXT_platform_x11 recognizes exactly one attribute,
        * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
        */
-      if (attrib == EGL_PLATFORM_X11_SCREEN_EXT) {
-         display->Options.Platform = (void *)(uintptr_t)value;
-      } else {
-         _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
-         return EGL_FALSE;
-      }
+      if (attrib != EGL_PLATFORM_X11_SCREEN_EXT)
+         return _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
+
+      display->Options.Platform = (void *)(uintptr_t)value;
    }
 
    return EGL_TRUE;