Merge branch 'lp-offset-twoside'
[mesa.git] / src / egl / main / egldisplay.c
index d666bdabe02a2093a8e4ba63cc669d016033e3d0..565e44d2d23c8fc98d292aaa77983772042d2bdb 100644 (file)
 #include "egllog.h"
 
 
+/**
+ * Return the native platform by parsing EGL_PLATFORM.
+ */
+static _EGLPlatformType
+_eglGetNativePlatformFromEnv(void)
+{
+   /* map --with-egl-platforms names to platform types */
+   static const struct {
+      _EGLPlatformType platform;
+      const char *name;
+   } egl_platforms[_EGL_NUM_PLATFORMS] = {
+      { _EGL_PLATFORM_WINDOWS, "gdi" },
+      { _EGL_PLATFORM_X11, "x11" },
+      { _EGL_PLATFORM_DRM, "drm" },
+      { _EGL_PLATFORM_FBDEV, "fbdev" }
+   };
+   _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;
+}
+
+
+/**
+ * Return the native platform.  It is the platform of the EGL native types.
+ */
+_EGLPlatformType
+_eglGetNativePlatform(void)
+{
+   static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
+
+   if (native_platform == _EGL_INVALID_PLATFORM) {
+      native_platform = _eglGetNativePlatformFromEnv();
+      if (native_platform == _EGL_INVALID_PLATFORM)
+         native_platform = _EGL_NATIVE_PLATFORM;
+   }
+
+   return native_platform;
+}
+
+
 /**
  * Finish display management.
  */
@@ -123,15 +179,9 @@ _eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *display)
 void
 _eglCleanupDisplay(_EGLDisplay *disp)
 {
-   EGLint i;
-
    if (disp->Configs) {
-      for (i = 0; i < disp->NumConfigs; i++)
-         free(disp->Configs[i]);
-      free(disp->Configs);
+      _eglDestroyArray(disp->Configs, free);
       disp->Configs = NULL;
-      disp->NumConfigs = 0;
-      disp->MaxConfigs = 0;
    }
 
    /* XXX incomplete */
@@ -183,17 +233,53 @@ _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy)
 
 
 /**
- * Link a resource to a display.
+ * Initialize a display resource.
  */
 void
-_eglLinkResource(_EGLResource *res, _EGLResourceType type, _EGLDisplay *dpy)
+_eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *dpy)
 {
-   assert(!res->Display || res->Display == dpy);
-
+   memset(res, 0, size);
    res->Display = dpy;
+   res->RefCount = 1;
+}
+
+
+/**
+ * Increment reference count for the resource.
+ */
+void
+_eglGetResource(_EGLResource *res)
+{
+   assert(res && res->RefCount > 0);
+   /* hopefully a resource is always manipulated with its display locked */
+   res->RefCount++;
+}
+
+
+/**
+ * Decrement reference count for the resource.
+ */
+EGLBoolean
+_eglPutResource(_EGLResource *res)
+{
+   assert(res && res->RefCount > 0);
+   res->RefCount--;
+   return (!res->RefCount);
+}
+
+
+/**
+ * Link a resource to its display.
+ */
+void
+_eglLinkResource(_EGLResource *res, _EGLResourceType type)
+{
+   assert(res->Display);
+
    res->IsLinked = EGL_TRUE;
-   res->Next = dpy->ResourceLists[type];
-   dpy->ResourceLists[type] = res;
+   res->Next = res->Display->ResourceLists[type];
+   res->Display->ResourceLists[type] = res;
+   _eglGetResource(res);
 }
 
 
@@ -220,6 +306,9 @@ _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
    }
 
    res->Next = NULL;
-   /* do not reset res->Display */
    res->IsLinked = EGL_FALSE;
+   _eglPutResource(res);
+
+   /* We always unlink before destroy.  The driver still owns a reference */
+   assert(res->RefCount);
 }