egl: always compile surfaceless
[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
42 #include "util/macros.h"
43
44 #ifdef HAVE_MINCORE
45 #include <unistd.h>
46 #include <sys/mman.h>
47 #endif
48
49
50 static mtx_t _eglGlobalMutex = _MTX_INITIALIZER_NP;
51
52 struct _egl_global _eglGlobal =
53 {
54 .Mutex = &_eglGlobalMutex,
55 .DisplayList = NULL,
56 .DeviceList = &_eglSoftwareDevice,
57 .NumAtExitCalls = 3,
58 .AtExitCalls = {
59 /* default AtExitCalls, called in reverse order */
60 _eglFiniDevice, /* always called last */
61 _eglUnloadDrivers,
62 _eglFiniDisplay,
63 },
64
65 #if USE_LIBGLVND
66 .ClientOnlyExtensionString =
67 #else
68 .ClientExtensionString =
69 #endif
70 "EGL_EXT_client_extensions"
71 " EGL_EXT_device_base"
72 " EGL_EXT_device_enumeration"
73 " EGL_EXT_device_query"
74 " EGL_EXT_platform_base"
75 " EGL_KHR_client_get_all_proc_addresses"
76 " EGL_KHR_debug"
77
78 #if USE_LIBGLVND
79 ,
80 .PlatformExtensionString =
81 #else
82 " "
83 #endif
84
85 "EGL_EXT_platform_device"
86 #ifdef HAVE_WAYLAND_PLATFORM
87 " EGL_EXT_platform_wayland"
88 " EGL_KHR_platform_wayland"
89 #endif
90 #ifdef HAVE_X11_PLATFORM
91 " EGL_EXT_platform_x11"
92 " EGL_KHR_platform_x11"
93 #endif
94 #ifdef HAVE_DRM_PLATFORM
95 " EGL_MESA_platform_gbm"
96 " EGL_KHR_platform_gbm"
97 #endif
98 " EGL_MESA_platform_surfaceless"
99 "",
100
101 .debugCallback = NULL,
102 .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
103 };
104
105
106 static void
107 _eglAtExit(void)
108 {
109 EGLint i;
110 for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
111 _eglGlobal.AtExitCalls[i]();
112 }
113
114
115 void
116 _eglAddAtExitCall(void (*func)(void))
117 {
118 if (func) {
119 static EGLBoolean registered = EGL_FALSE;
120
121 mtx_lock(_eglGlobal.Mutex);
122
123 if (!registered) {
124 atexit(_eglAtExit);
125 registered = EGL_TRUE;
126 }
127
128 assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
129 _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
130
131 mtx_unlock(_eglGlobal.Mutex);
132 }
133 }
134
135 EGLBoolean
136 _eglPointerIsDereferencable(void *p)
137 {
138 uintptr_t addr = (uintptr_t) p;
139 const long page_size = getpagesize();
140 #ifdef HAVE_MINCORE
141 unsigned char valid = 0;
142
143 if (p == NULL)
144 return EGL_FALSE;
145
146 /* align addr to page_size */
147 addr &= ~(page_size - 1);
148
149 if (mincore((void *) addr, page_size, &valid) < 0) {
150 return EGL_FALSE;
151 }
152
153 /* mincore() returns 0 on success, and -1 on failure. The last parameter
154 * is a vector of bytes with one entry for each page queried. mincore
155 * returns page residency information in the first bit of each byte in the
156 * vector.
157 *
158 * Residency doesn't actually matter when determining whether a pointer is
159 * dereferenceable, so the output vector can be ignored. What matters is
160 * whether mincore succeeds. See:
161 *
162 * http://man7.org/linux/man-pages/man2/mincore.2.html
163 */
164 return EGL_TRUE;
165 #else
166 // Without mincore(), we just assume that the first page is unmapped.
167 return addr >= page_size;
168 #endif
169 }