egl: enable EGL_EXT_device_{base,enumeration,query}
[mesa.git] / src / egl / main / eglglobals.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35 #include "c11/threads.h"
36
37 #include "eglglobals.h"
38 #include "egldevice.h"
39 #include "egldisplay.h"
40 #include "egldriver.h"
41 #include "egllog.h"
42
43 #ifdef HAVE_MINCORE
44 #include <unistd.h>
45 #include <sys/mman.h>
46 #endif
47
48
49 static mtx_t _eglGlobalMutex = _MTX_INITIALIZER_NP;
50
51 struct _egl_global _eglGlobal =
52 {
53 .Mutex = &_eglGlobalMutex,
54 .DisplayList = NULL,
55 .DeviceList = &_eglSoftwareDevice,
56 .NumAtExitCalls = 3,
57 .AtExitCalls = {
58 /* default AtExitCalls, called in reverse order */
59 _eglFiniDevice, /* always called last */
60 _eglUnloadDrivers,
61 _eglFiniDisplay,
62 },
63
64 .ClientOnlyExtensionString =
65 "EGL_EXT_client_extensions"
66 " EGL_EXT_device_base"
67 " EGL_EXT_device_enumeration"
68 " EGL_EXT_device_query"
69 " EGL_EXT_platform_base"
70 " EGL_KHR_client_get_all_proc_addresses"
71 " EGL_KHR_debug",
72
73 .PlatformExtensionString =
74 #ifdef HAVE_WAYLAND_PLATFORM
75 " EGL_EXT_platform_wayland"
76 #endif
77 #ifdef HAVE_X11_PLATFORM
78 " EGL_EXT_platform_x11"
79 #endif
80 #ifdef HAVE_DRM_PLATFORM
81 " EGL_MESA_platform_gbm"
82 #endif
83 #ifdef HAVE_SURFACELESS_PLATFORM
84 " EGL_MESA_platform_surfaceless"
85 #endif
86 "",
87
88 .ClientExtensionString = NULL,
89
90 .debugCallback = NULL,
91 .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
92 };
93
94
95 static void
96 _eglAtExit(void)
97 {
98 EGLint i;
99 for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
100 _eglGlobal.AtExitCalls[i]();
101 }
102
103
104 void
105 _eglAddAtExitCall(void (*func)(void))
106 {
107 if (func) {
108 static EGLBoolean registered = EGL_FALSE;
109
110 mtx_lock(_eglGlobal.Mutex);
111
112 if (!registered) {
113 atexit(_eglAtExit);
114 registered = EGL_TRUE;
115 }
116
117 assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
118 _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
119
120 mtx_unlock(_eglGlobal.Mutex);
121 }
122 }
123
124 const char *
125 _eglGetClientExtensionString(void)
126 {
127 const char *ret;
128
129 mtx_lock(_eglGlobal.Mutex);
130
131 if (_eglGlobal.ClientExtensionString == NULL) {
132 size_t clientLen = strlen(_eglGlobal.ClientOnlyExtensionString);
133 size_t platformLen = strlen(_eglGlobal.PlatformExtensionString);
134
135 _eglGlobal.ClientExtensionString = (char *) malloc(clientLen + platformLen + 1);
136 if (_eglGlobal.ClientExtensionString != NULL) {
137 char *ptr = _eglGlobal.ClientExtensionString;
138
139 memcpy(ptr, _eglGlobal.ClientOnlyExtensionString, clientLen);
140 ptr += clientLen;
141
142 if (platformLen > 0) {
143 // Note that if PlatformExtensionString is not empty, then it will
144 // already have a leading space.
145 assert(_eglGlobal.PlatformExtensionString[0] == ' ');
146 memcpy(ptr, _eglGlobal.PlatformExtensionString, platformLen);
147 ptr += platformLen;
148 }
149 *ptr = '\0';
150 }
151 }
152 ret = _eglGlobal.ClientExtensionString;
153
154 mtx_unlock(_eglGlobal.Mutex);
155 return ret;
156 }
157
158 EGLBoolean
159 _eglPointerIsDereferencable(void *p)
160 {
161 #ifdef HAVE_MINCORE
162 uintptr_t addr = (uintptr_t) p;
163 unsigned char valid = 0;
164 const long page_size = getpagesize();
165
166 if (p == NULL)
167 return EGL_FALSE;
168
169 /* align addr to page_size */
170 addr &= ~(page_size - 1);
171
172 if (mincore((void *) addr, page_size, &valid) < 0) {
173 _eglLog(_EGL_DEBUG, "mincore failed: %m");
174 return EGL_FALSE;
175 }
176
177 /* mincore() returns 0 on success, and -1 on failure. The last parameter
178 * is a vector of bytes with one entry for each page queried. mincore
179 * returns page residency information in the first bit of each byte in the
180 * vector.
181 *
182 * Residency doesn't actually matter when determining whether a pointer is
183 * dereferenceable, so the output vector can be ignored. What matters is
184 * whether mincore succeeds. See:
185 *
186 * http://man7.org/linux/man-pages/man2/mincore.2.html
187 */
188 return EGL_TRUE;
189 #else
190 return p != NULL;
191 #endif
192 }