blob: d3fe4d7375a428cf37c57d6b41b9bed97a5e917b [file] [log] [blame]
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001/*
2 * XGL
3 *
4 * Copyright (C) 2015 LunarG, Inc.
5 * Copyright 2014 Valve Software
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 "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Ian Elliott <ian@lunarg.com>
28 */
29
30#ifndef LOADER_PLATFORM_H
31#define LOADER_PLATFORM_H
32
33#if defined(__linux__)
34/* Linux-specific common code: */
35
36// Headers:
37//#define _GNU_SOURCE 1
38// TBD: Are the contents of the following file used?
39#include <unistd.h>
40// Note: The following file is for dynamic loading:
41#include <dlfcn.h>
42#include <pthread.h>
43#include <assert.h>
44
Ian Elliott457810e2015-02-04 11:22:39 -070045// XGL Library Filenames, Paths, etc.:
46#define PATH_SEPERATOR ':'
47#define DIRECTORY_SYMBOL "/"
Ian Elliott4470a302015-02-17 10:33:47 -070048#define DRIVER_PATH_ENV "LIBXGL_DRIVERS_PATH"
49#define LAYERS_PATH_ENV "LIBXGL_LAYERS_PATH"
50#define LAYER_NAMES_ENV "LIBXGL_LAYER_NAMES"
Ian Elliott457810e2015-02-04 11:22:39 -070051#ifndef DEFAULT_XGL_DRIVERS_PATH
52// TODO: Is this a good default location?
53// Need to search for both 32bit and 64bit ICDs
54#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
55#define XGL_DRIVER_LIBRARY_PREFIX "libXGL_"
56#define XGL_DRIVER_LIBRARY_PREFIX_LEN 7
57#define XGL_LAYER_LIBRARY_PREFIX "libXGLLayer"
58#define XGL_LAYER_LIBRARY_PREFIX_LEN 11
59#define XGL_LIBRARY_SUFFIX ".so"
60#define XGL_LIBRARY_SUFFIX_LEN 3
61#endif // DEFAULT_XGL_DRIVERS_PATH
62#ifndef DEFAULT_XGL_LAYERS_PATH
63// TODO: Are these good default locations?
64#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
65#endif
66
Ian Elliotte5369462015-02-12 16:44:56 -070067// C99:
Tobin Ehlisb0497852015-02-11 14:24:02 -070068#define PRINTF_SIZE_T_SPECIFIER "%zu"
69
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070070// Dynamic Loading:
71typedef void * loader_platform_dl_handle;
72static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath)
73{
Chia-I Wu6a218342015-02-18 14:39:54 -070074 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070075}
76static inline char * loader_platform_open_library_error(const char* libPath)
77{
78 return dlerror();
79}
80static inline void loader_platform_close_library(loader_platform_dl_handle library)
81{
82 dlclose(library);
83}
84static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library,
85 const char *name)
86{
87 assert(library);
88 assert(name);
89 return dlsym(library, name);
90}
91static inline char * loader_platform_get_proc_address_error(const char *name)
92{
93 return dlerror();
94}
95
96// Threads:
97typedef pthread_t loader_platform_thread;
98#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
99 pthread_once_t var = PTHREAD_ONCE_INIT;
100static inline void loader_platform_thread_once(void *ctl, void (* func) (void))
101{
102 assert(func != NULL);
103 assert(ctl != NULL);
104 pthread_once((pthread_once_t *) ctl, func);
105}
106
107// Thread IDs:
108typedef pthread_t loader_platform_thread_id;
109static inline loader_platform_thread_id loader_platform_get_thread_id()
110{
111 return pthread_self();
112}
113
114// Thread mutex:
115typedef pthread_mutex_t loader_platform_thread_mutex;
116static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
117{
118 pthread_mutex_init(pMutex, NULL);
119}
120static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
121{
122 pthread_mutex_lock(pMutex);
123}
124static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
125{
126 pthread_mutex_unlock(pMutex);
127}
128static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
129{
130 pthread_mutex_destroy(pMutex);
131}
132
133
134#elif defined(_WIN32) // defined(__linux__)
135/* Windows-specific common code: */
136
137// Headers:
138#include <windows.h>
139#include <assert.h>
140#ifdef __cplusplus
141#include <iostream>
142#include <string>
143using namespace std;
144#endif // __cplusplus
145
Ian Elliott457810e2015-02-04 11:22:39 -0700146// XGL Library Filenames, Paths, etc.:
147#define PATH_SEPERATOR ';'
148#define DIRECTORY_SYMBOL "\\"
Ian Elliott4470a302015-02-17 10:33:47 -0700149#define DRIVER_PATH_REGISTRY_VALUE "XGL_DRIVERS_PATH"
150#define LAYERS_PATH_REGISTRY_VALUE "XGL_LAYERS_PATH"
151#define LAYER_NAMES_REGISTRY_VALUE "XGL_LAYER_NAMES"
152#define DRIVER_PATH_ENV "XGL_DRIVERS_PATH"
153#define LAYERS_PATH_ENV "XGL_LAYERS_PATH"
154#define LAYER_NAMES_ENV "XGL_LAYER_NAMES"
Ian Elliott457810e2015-02-04 11:22:39 -0700155#ifndef DEFAULT_XGL_DRIVERS_PATH
156// TODO: Is this a good default location?
157// Need to search for both 32bit and 64bit ICDs
158#define DEFAULT_XGL_DRIVERS_PATH "C:\\Windows\\System32"
159// TODO/TBD: Is this an appropriate prefix for Windows?
160#define XGL_DRIVER_LIBRARY_PREFIX "XGL_"
161#define XGL_DRIVER_LIBRARY_PREFIX_LEN 4
162// TODO/TBD: Is this an appropriate suffix for Windows?
163#define XGL_LAYER_LIBRARY_PREFIX "XGLLayer"
164#define XGL_LAYER_LIBRARY_PREFIX_LEN 8
165#define XGL_LIBRARY_SUFFIX ".dll"
166#define XGL_LIBRARY_SUFFIX_LEN 4
167#endif // DEFAULT_XGL_DRIVERS_PATH
168#ifndef DEFAULT_XGL_LAYERS_PATH
169// TODO: Is this a good default location?
170#define DEFAULT_XGL_LAYERS_PATH "C:\\Windows\\System32"
171#endif // DEFAULT_XGL_LAYERS_PATH
172
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700173// C99:
174// Microsoft didn't implement C99 in Visual Studio; but started adding it with
175// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott19628802015-02-04 12:06:46 -0700176// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
177// "CMakeLists.txt" file).
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700178#define snprintf _snprintf
Tobin Ehlisb0497852015-02-11 14:24:02 -0700179#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700180// Microsoft also doesn't have basename(). Paths are different on Windows, and
181// so this is just a temporary solution in order to get us compiling, so that we
182// can test some scenarios, and develop the correct solution for Windows.
183 // TODO: Develop a better, permanent solution for Windows, to replace this
184 // temporary code:
185static char *basename(char *pathname)
186{
187 char *current, *next;
188
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700189// TODO/TBD: Do we need to deal with the Windows's ":" character?
190
Ian Elliott457810e2015-02-04 11:22:39 -0700191#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700192 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott457810e2015-02-04 11:22:39 -0700193 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700194 if (next == NULL) {
Ian Elliott457810e2015-02-04 11:22:39 -0700195 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700196 return current;
197 } else {
Ian Elliott457810e2015-02-04 11:22:39 -0700198 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700199 next++;
200 }
201 }
Ian Elliott19628802015-02-04 12:06:46 -0700202 // We shouldn't get to here, but this makes the compiler happy:
203 return current;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700204}
205
206// Dynamic Loading:
207typedef HMODULE loader_platform_dl_handle;
208static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
209{
210 return LoadLibrary(libPath);
211}
212static char * loader_platform_open_library_error(const char* libPath)
213{
214 static char errorMsg[120];
215 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
216 return errorMsg;
217}
218static void loader_platform_close_library(loader_platform_dl_handle library)
219{
220 FreeLibrary(library);
221}
222static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
223 const char *name)
224{
225 assert(library);
226 assert(name);
227 return GetProcAddress(library, name);
228}
229static char * loader_platform_get_proc_address_error(const char *name)
230{
231 static char errorMsg[120];
232 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
233 return errorMsg;
234}
235
236// Threads:
237typedef HANDLE loader_platform_thread;
238#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
239 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
240static void loader_platform_thread_once(void *ctl, void (* func) (void))
241{
242 assert(func != NULL);
243 assert(ctl != NULL);
244 InitOnceExecuteOnce((PINIT_ONCE) ctl, (PINIT_ONCE_FN) func, NULL, NULL);
245}
246
247// Thread IDs:
248typedef DWORD loader_platform_thread_id;
249static loader_platform_thread_id loader_platform_get_thread_id()
250{
251 return GetCurrentThreadId();
252}
253
254// Thread mutex:
255typedef CRITICAL_SECTION loader_platform_thread_mutex;
256static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
257{
258 InitializeCriticalSection(pMutex);
259}
260static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
261{
262 EnterCriticalSection(pMutex);
263}
264static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
265{
266 LeaveCriticalSection(pMutex);
267}
268static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
269{
270 DeleteCriticalSection(pMutex);
271}
272
273#else // defined(_WIN32)
274
275#error The "loader_platform.h" file must be modified for this OS.
276
277// NOTE: In order to support another OS, an #elif needs to be added (above the
278// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
279// contents of this file must be created.
280
281// NOTE: Other OS-specific changes are also needed for this OS. Search for
282// files with "WIN32" in it, as a quick way to find files that must be changed.
283
284#endif // defined(_WIN32)
285
Ian Elliott655cad72015-02-12 17:08:34 -0700286#else /* LOADER_PLATFORM_H */
287#ifndef LOADER_PLATFORM_H_TEMP
288#define LOADER_PLATFORM_H_TEMP
289
290// NOTE: The following are hopefully-temporary macros to ensure that people
291// don't forget to use the loader_platform_*() functions above:
292
293#if defined(__linux__)
294/* Linux-specific common code: */
295
296// Dynamic Loading:
297#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
298#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
299#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
300#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
301
302// Threads:
303#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
304#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
305
306// Thread mutex:
307#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
308#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
309#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
310#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
311
312
313#elif defined(_WIN32) // defined(__linux__)
314/* Windows-specific common code: */
315
316// Dynamic Loading:
317//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
318#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
319#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
320
321// Threads:
322#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
323#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
324
325// Thread mutex:
326#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
327#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
328#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
329#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
330
331
332#endif // defined(_WIN32)
333#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700334#endif /* LOADER_PLATFORM_H */