1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
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:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
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.
28 **************************************************************************/
35 #include "c11/threads.h"
37 #include "eglglobals.h"
38 #include "egldevice.h"
39 #include "egldisplay.h"
40 #include "egldriver.h"
43 #include "util/macros.h"
51 static mtx_t _eglGlobalMutex
= _MTX_INITIALIZER_NP
;
53 struct _egl_global _eglGlobal
=
55 .Mutex
= &_eglGlobalMutex
,
57 .DeviceList
= &_eglSoftwareDevice
,
60 /* default AtExitCalls, called in reverse order */
61 _eglFiniDevice
, /* always called last */
66 .ClientOnlyExtensionString
=
67 "EGL_EXT_client_extensions"
68 " EGL_EXT_device_base"
69 " EGL_EXT_device_enumeration"
70 " EGL_EXT_device_query"
71 " EGL_EXT_platform_base"
72 " EGL_KHR_client_get_all_proc_addresses"
75 .PlatformExtensionString
=
76 #ifdef HAVE_WAYLAND_PLATFORM
77 " EGL_EXT_platform_wayland"
79 #ifdef HAVE_X11_PLATFORM
80 " EGL_EXT_platform_x11"
82 #ifdef HAVE_DRM_PLATFORM
83 " EGL_MESA_platform_gbm"
85 #ifdef HAVE_SURFACELESS_PLATFORM
86 " EGL_MESA_platform_surfaceless"
88 " EGL_EXT_platform_device"
91 .ClientExtensionString
= NULL
,
93 .debugCallback
= NULL
,
94 .debugTypesEnabled
= _EGL_DEBUG_BIT_CRITICAL
| _EGL_DEBUG_BIT_ERROR
,
102 for (i
= _eglGlobal
.NumAtExitCalls
- 1; i
>= 0; i
--)
103 _eglGlobal
.AtExitCalls
[i
]();
108 _eglAddAtExitCall(void (*func
)(void))
111 static EGLBoolean registered
= EGL_FALSE
;
113 mtx_lock(_eglGlobal
.Mutex
);
117 registered
= EGL_TRUE
;
120 assert(_eglGlobal
.NumAtExitCalls
< ARRAY_SIZE(_eglGlobal
.AtExitCalls
));
121 _eglGlobal
.AtExitCalls
[_eglGlobal
.NumAtExitCalls
++] = func
;
123 mtx_unlock(_eglGlobal
.Mutex
);
128 _eglGetClientExtensionString(void)
132 mtx_lock(_eglGlobal
.Mutex
);
134 if (_eglGlobal
.ClientExtensionString
== NULL
) {
135 size_t clientLen
= strlen(_eglGlobal
.ClientOnlyExtensionString
);
136 size_t platformLen
= strlen(_eglGlobal
.PlatformExtensionString
);
138 _eglGlobal
.ClientExtensionString
= (char *) malloc(clientLen
+ platformLen
+ 1);
139 if (_eglGlobal
.ClientExtensionString
!= NULL
) {
140 char *ptr
= _eglGlobal
.ClientExtensionString
;
142 memcpy(ptr
, _eglGlobal
.ClientOnlyExtensionString
, clientLen
);
145 if (platformLen
> 0) {
146 // Note that if PlatformExtensionString is not empty, then it will
147 // already have a leading space.
148 assert(_eglGlobal
.PlatformExtensionString
[0] == ' ');
149 memcpy(ptr
, _eglGlobal
.PlatformExtensionString
, platformLen
);
155 ret
= _eglGlobal
.ClientExtensionString
;
157 mtx_unlock(_eglGlobal
.Mutex
);
162 _eglPointerIsDereferencable(void *p
)
165 uintptr_t addr
= (uintptr_t) p
;
166 unsigned char valid
= 0;
167 const long page_size
= getpagesize();
172 /* align addr to page_size */
173 addr
&= ~(page_size
- 1);
175 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
176 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
180 /* mincore() returns 0 on success, and -1 on failure. The last parameter
181 * is a vector of bytes with one entry for each page queried. mincore
182 * returns page residency information in the first bit of each byte in the
185 * Residency doesn't actually matter when determining whether a pointer is
186 * dereferenceable, so the output vector can be ignored. What matters is
187 * whether mincore succeeds. See:
189 * http://man7.org/linux/man-pages/man2/mincore.2.html