• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "egldisplay.h"
39 #include "egldriver.h"
40 #include "egllog.h"
41 
42 #ifdef HAVE_MINCORE
43 #include <unistd.h>
44 #include <sys/mman.h>
45 #endif
46 
47 
48 static mtx_t _eglGlobalMutex = _MTX_INITIALIZER_NP;
49 
50 struct _egl_global _eglGlobal =
51 {
52    .Mutex = &_eglGlobalMutex,
53    .DisplayList = NULL,
54    .NumAtExitCalls = 2,
55    .AtExitCalls = {
56       /* default AtExitCalls, called in reverse order */
57       _eglUnloadDrivers, /* always called last */
58       _eglFiniDisplay
59    },
60 
61    .ClientOnlyExtensionString =
62    "EGL_EXT_client_extensions"
63    " EGL_EXT_platform_base"
64    " EGL_KHR_client_get_all_proc_addresses"
65    " EGL_KHR_debug",
66 
67    .PlatformExtensionString =
68 #ifdef HAVE_WAYLAND_PLATFORM
69    " EGL_EXT_platform_wayland"
70 #endif
71 #ifdef HAVE_X11_PLATFORM
72    " EGL_EXT_platform_x11"
73 #endif
74 #ifdef HAVE_DRM_PLATFORM
75    " EGL_MESA_platform_gbm"
76 #endif
77 #ifdef HAVE_SURFACELESS_PLATFORM
78    " EGL_MESA_platform_surfaceless"
79 #endif
80    "",
81 
82    .ClientExtensionString = NULL,
83 
84    .debugCallback = NULL,
85    .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
86 };
87 
88 
89 static void
_eglAtExit(void)90 _eglAtExit(void)
91 {
92    EGLint i;
93    for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
94       _eglGlobal.AtExitCalls[i]();
95 }
96 
97 
98 void
_eglAddAtExitCall(void (* func)(void))99 _eglAddAtExitCall(void (*func)(void))
100 {
101    if (func) {
102       static EGLBoolean registered = EGL_FALSE;
103 
104       mtx_lock(_eglGlobal.Mutex);
105 
106       if (!registered) {
107          atexit(_eglAtExit);
108          registered = EGL_TRUE;
109       }
110 
111       assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
112       _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
113 
114       mtx_unlock(_eglGlobal.Mutex);
115    }
116 }
117 
118 const char *
_eglGetClientExtensionString(void)119 _eglGetClientExtensionString(void)
120 {
121    const char *ret;
122 
123    mtx_lock(_eglGlobal.Mutex);
124 
125    if (_eglGlobal.ClientExtensionString == NULL) {
126       size_t clientLen = strlen(_eglGlobal.ClientOnlyExtensionString);
127       size_t platformLen = strlen(_eglGlobal.PlatformExtensionString);
128 
129       _eglGlobal.ClientExtensionString = (char *) malloc(clientLen + platformLen + 1);
130       if (_eglGlobal.ClientExtensionString != NULL) {
131          char *ptr = _eglGlobal.ClientExtensionString;
132 
133          memcpy(ptr, _eglGlobal.ClientOnlyExtensionString, clientLen);
134          ptr += clientLen;
135 
136          if (platformLen > 0) {
137             // Note that if PlatformExtensionString is not empty, then it will
138             // already have a leading space.
139             assert(_eglGlobal.PlatformExtensionString[0] == ' ');
140             memcpy(ptr, _eglGlobal.PlatformExtensionString, platformLen);
141             ptr += platformLen;
142          }
143          *ptr = '\0';
144       }
145    }
146    ret = _eglGlobal.ClientExtensionString;
147 
148    mtx_unlock(_eglGlobal.Mutex);
149    return ret;
150 }
151 
152 EGLBoolean
_eglPointerIsDereferencable(void * p)153 _eglPointerIsDereferencable(void *p)
154 {
155 #ifdef HAVE_MINCORE
156    uintptr_t addr = (uintptr_t) p;
157    unsigned char valid = 0;
158    const long page_size = getpagesize();
159 
160    if (p == NULL)
161       return EGL_FALSE;
162 
163    /* align addr to page_size */
164    addr &= ~(page_size - 1);
165 
166    if (mincore((void *) addr, page_size, &valid) < 0) {
167       _eglLog(_EGL_DEBUG, "mincore failed: %m");
168       return EGL_FALSE;
169    }
170 
171    /* mincore() returns 0 on success, and -1 on failure.  The last parameter
172     * is a vector of bytes with one entry for each page queried.  mincore
173     * returns page residency information in the first bit of each byte in the
174     * vector.
175     *
176     * Residency doesn't actually matter when determining whether a pointer is
177     * dereferenceable, so the output vector can be ignored.  What matters is
178     * whether mincore succeeds. See:
179     *
180     *   http://man7.org/linux/man-pages/man2/mincore.2.html
181     */
182    return EGL_TRUE;
183 #else
184    return p != NULL;
185 #endif
186 }
187