-#include <stdio.h>
+/**************************************************************************
+ *
+ * Copyright 2008 VMware, Inc.
+ * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
+ * Copyright 2010-2011 LunarG, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "c11/threads.h"
+
#include "eglglobals.h"
+#include "egldevice.h"
+#include "egldisplay.h"
+#include "egldriver.h"
-struct _egl_global _eglGlobal =
-{
- EGL_FALSE
-};
+#include "util/macros.h"
-/**
- * Init the fields in the _eglGlobal struct
- * May be safely called more than once.
- */
-void
-_eglInitGlobals(void)
+#ifdef HAVE_MINCORE
+#include <unistd.h>
+#include <sys/mman.h>
+#endif
+
+
+static mtx_t _eglGlobalMutex = _MTX_INITIALIZER_NP;
+
+struct _egl_global _eglGlobal =
{
- if (!_eglGlobal.Initialized) {
- _eglGlobal.Displays = _eglNewHashTable();
- _eglGlobal.Surfaces = _eglNewHashTable();
- _eglGlobal.FreeScreenHandle = 1;
- _eglGlobal.Initialized = EGL_TRUE;
+ .Mutex = &_eglGlobalMutex,
+ .DisplayList = NULL,
+ .DeviceList = &_eglSoftwareDevice,
+ .NumAtExitCalls = 3,
+ .AtExitCalls = {
+ /* default AtExitCalls, called in reverse order */
+ _eglFiniDevice, /* always called last */
+ _eglUnloadDrivers,
+ _eglFiniDisplay,
+ },
+
+ .ClientOnlyExtensionString =
+ "EGL_EXT_client_extensions"
+ " EGL_EXT_device_base"
+ " EGL_EXT_device_enumeration"
+ " EGL_EXT_device_query"
+ " EGL_EXT_platform_base"
+ " EGL_KHR_client_get_all_proc_addresses"
+ " EGL_KHR_debug",
+
+ .PlatformExtensionString =
+#ifdef HAVE_WAYLAND_PLATFORM
+ " EGL_EXT_platform_wayland"
+#endif
+#ifdef HAVE_X11_PLATFORM
+ " EGL_EXT_platform_x11"
+#endif
+#ifdef HAVE_DRM_PLATFORM
+ " EGL_MESA_platform_gbm"
+#endif
+#ifdef HAVE_SURFACELESS_PLATFORM
+ " EGL_MESA_platform_surfaceless"
+#endif
+ " EGL_EXT_platform_device"
+ "",
+
+ .ClientExtensionString = NULL,
+
+ .debugCallback = NULL,
+ .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
+};
- _eglGlobal.ClientAPIsMask = 0x0;
- /* XXX temporary */
- _eglGlobal.ThreadInfo = _eglNewThreadInfo();
- }
+static void
+_eglAtExit(void)
+{
+ EGLint i;
+ for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
+ _eglGlobal.AtExitCalls[i]();
}
-/**
- * Should call this via an atexit handler.
- */
void
-_eglDestroyGlobals(void)
+_eglAddAtExitCall(void (*func)(void))
{
- /* XXX TODO walk over table entries, deleting each */
- _eglDeleteHashTable(_eglGlobal.Displays);
- _eglDeleteHashTable(_eglGlobal.Surfaces);
-}
+ if (func) {
+ static EGLBoolean registered = EGL_FALSE;
+ mtx_lock(_eglGlobal.Mutex);
-/**
- * Allocate and init a new _EGLThreadInfo object.
- */
-_EGLThreadInfo *
-_eglNewThreadInfo(void)
-{
- _EGLThreadInfo *t = (_EGLThreadInfo *) calloc(1, sizeof(_EGLThreadInfo));
- if (t) {
- t->CurrentContext = EGL_NO_CONTEXT;
- t->LastError = EGL_SUCCESS;
- t->CurrentAPI = EGL_OPENGL_ES_API; /* default, per EGL spec */
+ if (!registered) {
+ atexit(_eglAtExit);
+ registered = EGL_TRUE;
+ }
+
+ assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
+ _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
+
+ mtx_unlock(_eglGlobal.Mutex);
}
- return t;
}
-
-/**
- * Delete/free a _EGLThreadInfo object.
- */
-void
-_eglDeleteThreadData(_EGLThreadInfo *t)
+const char *
+_eglGetClientExtensionString(void)
{
- free(t);
-}
+ const char *ret;
+ mtx_lock(_eglGlobal.Mutex);
+ if (_eglGlobal.ClientExtensionString == NULL) {
+ size_t clientLen = strlen(_eglGlobal.ClientOnlyExtensionString);
+ size_t platformLen = strlen(_eglGlobal.PlatformExtensionString);
-/**
- * Return pointer to calling thread's _EGLThreadInfo object.
- * Create a new one if needed.
- * Should never return NULL.
- */
-_EGLThreadInfo *
-_eglGetCurrentThread(void)
-{
- _eglInitGlobals();
+ _eglGlobal.ClientExtensionString = (char *) malloc(clientLen + platformLen + 1);
+ if (_eglGlobal.ClientExtensionString != NULL) {
+ char *ptr = _eglGlobal.ClientExtensionString;
- /* XXX temporary */
- return _eglGlobal.ThreadInfo;
-}
+ memcpy(ptr, _eglGlobal.ClientOnlyExtensionString, clientLen);
+ ptr += clientLen;
+ if (platformLen > 0) {
+ // Note that if PlatformExtensionString is not empty, then it will
+ // already have a leading space.
+ assert(_eglGlobal.PlatformExtensionString[0] == ' ');
+ memcpy(ptr, _eglGlobal.PlatformExtensionString, platformLen);
+ ptr += platformLen;
+ }
+ *ptr = '\0';
+ }
+ }
+ ret = _eglGlobal.ClientExtensionString;
-/**
- * Record EGL error code.
- */
-void
-_eglError(EGLint errCode, const char *msg)
+ mtx_unlock(_eglGlobal.Mutex);
+ return ret;
+}
+
+EGLBoolean
+_eglPointerIsDereferencable(void *p)
{
- _EGLThreadInfo *t = _eglGetCurrentThread();
- const char *s;
-
- if (t->LastError == EGL_SUCCESS) {
- t->LastError = errCode;
-
- switch (errCode) {
- case EGL_BAD_ACCESS:
- s = "EGL_BAD_ACCESS";
- break;
- case EGL_BAD_ALLOC:
- s = "EGL_BAD_ALLOC";
- break;
- case EGL_BAD_ATTRIBUTE:
- s = "EGL_BAD_ATTRIBUTE";
- break;
- case EGL_BAD_CONFIG:
- s = "EGL_BAD_CONFIG";
- break;
- case EGL_BAD_CONTEXT:
- s = "EGL_BAD_CONTEXT";
- break;
- case EGL_BAD_CURRENT_SURFACE:
- s = "EGL_BAD_CURRENT_SURFACE";
- break;
- case EGL_BAD_DISPLAY:
- s = "EGL_BAD_DISPLAY";
- break;
- case EGL_BAD_MATCH:
- s = "EGL_BAD_MATCH";
- break;
- case EGL_BAD_NATIVE_PIXMAP:
- s = "EGL_BAD_NATIVE_PIXMAP";
- break;
- case EGL_BAD_NATIVE_WINDOW:
- s = "EGL_BAD_NATIVE_WINDOW";
- break;
- case EGL_BAD_PARAMETER:
- s = "EGL_BAD_PARAMETER";
- break;
- case EGL_BAD_SURFACE:
- s = "EGL_BAD_SURFACE";
- break;
- case EGL_BAD_SCREEN_MESA:
- s = "EGL_BAD_SCREEN_MESA";
- break;
- case EGL_BAD_MODE_MESA:
- s = "EGL_BAD_MODE_MESA";
- break;
- default:
- s = "other";
- }
- /* XXX temporary */
- fprintf(stderr, "EGL user error 0x%x (%s) in %s\n", errCode, s, msg);
+ uintptr_t addr = (uintptr_t) p;
+ const long page_size = getpagesize();
+#ifdef HAVE_MINCORE
+ unsigned char valid = 0;
+
+ if (p == NULL)
+ return EGL_FALSE;
+
+ /* align addr to page_size */
+ addr &= ~(page_size - 1);
+
+ if (mincore((void *) addr, page_size, &valid) < 0) {
+ return EGL_FALSE;
}
+
+ /* mincore() returns 0 on success, and -1 on failure. The last parameter
+ * is a vector of bytes with one entry for each page queried. mincore
+ * returns page residency information in the first bit of each byte in the
+ * vector.
+ *
+ * Residency doesn't actually matter when determining whether a pointer is
+ * dereferenceable, so the output vector can be ignored. What matters is
+ * whether mincore succeeds. See:
+ *
+ * http://man7.org/linux/man-pages/man2/mincore.2.html
+ */
+ return EGL_TRUE;
+#else
+ // Without mincore(), we just assume that the first page is unmapped.
+ return addr >= page_size;
+#endif
}