Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/egl/main/eglglobals.c
4560 views
1
/**************************************************************************
2
*
3
* Copyright 2008 VMware, Inc.
4
* Copyright 2009-2010 Chia-I Wu <[email protected]>
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
41
#include "util/macros.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 = 2,
57
.AtExitCalls = {
58
/* default AtExitCalls, called in reverse order */
59
_eglFiniDevice, /* always called last */
60
_eglFiniDisplay,
61
},
62
63
#if USE_LIBGLVND
64
.ClientOnlyExtensionString =
65
#else
66
.ClientExtensionString =
67
#endif
68
"EGL_EXT_client_extensions"
69
" EGL_EXT_device_base"
70
" EGL_EXT_device_enumeration"
71
" EGL_EXT_device_query"
72
" EGL_EXT_platform_base"
73
" EGL_KHR_client_get_all_proc_addresses"
74
" EGL_KHR_debug"
75
76
#if USE_LIBGLVND
77
,
78
.PlatformExtensionString =
79
#else
80
" "
81
#endif
82
83
"EGL_EXT_platform_device"
84
#ifdef HAVE_WAYLAND_PLATFORM
85
" EGL_EXT_platform_wayland"
86
" EGL_KHR_platform_wayland"
87
#endif
88
#ifdef HAVE_X11_PLATFORM
89
" EGL_EXT_platform_x11"
90
" EGL_KHR_platform_x11"
91
#endif
92
#ifdef HAVE_XCB_PLATFORM
93
" EGL_MESA_platform_xcb"
94
#endif
95
#ifdef HAVE_DRM_PLATFORM
96
" EGL_MESA_platform_gbm"
97
" EGL_KHR_platform_gbm"
98
#endif
99
" EGL_MESA_platform_surfaceless"
100
"",
101
102
.debugCallback = NULL,
103
.debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
104
};
105
106
107
static void
108
_eglAtExit(void)
109
{
110
EGLint i;
111
for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
112
_eglGlobal.AtExitCalls[i]();
113
}
114
115
116
void
117
_eglAddAtExitCall(void (*func)(void))
118
{
119
if (func) {
120
static EGLBoolean registered = EGL_FALSE;
121
122
mtx_lock(_eglGlobal.Mutex);
123
124
if (!registered) {
125
atexit(_eglAtExit);
126
registered = EGL_TRUE;
127
}
128
129
assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
130
_eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
131
132
mtx_unlock(_eglGlobal.Mutex);
133
}
134
}
135
136
EGLBoolean
137
_eglPointerIsDereferencable(void *p)
138
{
139
uintptr_t addr = (uintptr_t) p;
140
const long page_size = getpagesize();
141
#ifdef HAVE_MINCORE
142
unsigned char valid = 0;
143
144
if (p == NULL)
145
return EGL_FALSE;
146
147
/* align addr to page_size */
148
addr &= ~(page_size - 1);
149
150
/* mincore expects &valid to be unsigned char* on Linux but char* on BSD:
151
* we cast pointers to void, to fix type mismatch warnings in all systems
152
*/
153
if (mincore((void *) addr, page_size, (void*)&valid) < 0) {
154
return EGL_FALSE;
155
}
156
157
/* mincore() returns 0 on success, and -1 on failure. The last parameter
158
* is a vector of bytes with one entry for each page queried. mincore
159
* returns page residency information in the first bit of each byte in the
160
* vector.
161
*
162
* Residency doesn't actually matter when determining whether a pointer is
163
* dereferenceable, so the output vector can be ignored. What matters is
164
* whether mincore succeeds. See:
165
*
166
* http://man7.org/linux/man-pages/man2/mincore.2.html
167
*/
168
return EGL_TRUE;
169
#else
170
// Without mincore(), we just assume that the first page is unmapped.
171
return addr >= page_size;
172
#endif
173
}
174
175