blob: 2d878bce5cd1056a5a06f8bcc1dbc00692afe16c [file] [log] [blame]
Ian Elliott81ac44c2015-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 Elliott665c5632015-02-04 11:22:39 -070045// XGL Library Filenames, Paths, etc.:
46#define PATH_SEPERATOR ':'
47#define DIRECTORY_SYMBOL "/"
Ian Elliott225188f2015-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 Elliott665c5632015-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 Elliottecb60ec2015-02-12 16:44:56 -070067// C99:
Tobin Ehlisbf0146e2015-02-11 14:24:02 -070068#define PRINTF_SIZE_T_SPECIFIER "%zu"
69
Ian Elliott81ac44c2015-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 Wu7397c432015-02-18 14:39:54 -070074 return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL);
Ian Elliott81ac44c2015-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 Elliott665c5632015-02-04 11:22:39 -0700146// XGL Library Filenames, Paths, etc.:
147#define PATH_SEPERATOR ';'
148#define DIRECTORY_SYMBOL "\\"
Ian Elliott225188f2015-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 Elliott665c5632015-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 Elliott81ac44c2015-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 Elliott64f74a82015-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 Elliott81ac44c2015-01-13 17:52:38 -0700178#define snprintf _snprintf
Tobin Ehlisbf0146e2015-02-11 14:24:02 -0700179#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott81ac44c2015-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 Elliott81ac44c2015-01-13 17:52:38 -0700189// TODO/TBD: Do we need to deal with the Windows's ":" character?
190
Ian Elliott665c5632015-02-04 11:22:39 -0700191#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700192 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott665c5632015-02-04 11:22:39 -0700193 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700194 if (next == NULL) {
Ian Elliott665c5632015-02-04 11:22:39 -0700195 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700196 return current;
197 } else {
Ian Elliott665c5632015-02-04 11:22:39 -0700198 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700199 next++;
200 }
201 }
Ian Elliott64f74a82015-02-04 12:06:46 -0700202 // We shouldn't get to here, but this makes the compiler happy:
203 return current;
Ian Elliott81ac44c2015-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;
Piers Daniell886be472015-02-23 16:23:13 -0700240static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
241{
242 void (*func)(void) = (void (*)(void))Parameter;
243 func();
244 return TRUE;
245}
246
Ian Elliott81ac44c2015-01-13 17:52:38 -0700247static void loader_platform_thread_once(void *ctl, void (* func) (void))
248{
249 assert(func != NULL);
250 assert(ctl != NULL);
Piers Daniell886be472015-02-23 16:23:13 -0700251 InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700252}
253
254// Thread IDs:
255typedef DWORD loader_platform_thread_id;
256static loader_platform_thread_id loader_platform_get_thread_id()
257{
258 return GetCurrentThreadId();
259}
260
261// Thread mutex:
262typedef CRITICAL_SECTION loader_platform_thread_mutex;
263static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
264{
265 InitializeCriticalSection(pMutex);
266}
267static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
268{
269 EnterCriticalSection(pMutex);
270}
271static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
272{
273 LeaveCriticalSection(pMutex);
274}
275static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
276{
277 DeleteCriticalSection(pMutex);
278}
279
280#else // defined(_WIN32)
281
282#error The "loader_platform.h" file must be modified for this OS.
283
284// NOTE: In order to support another OS, an #elif needs to be added (above the
285// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
286// contents of this file must be created.
287
288// NOTE: Other OS-specific changes are also needed for this OS. Search for
289// files with "WIN32" in it, as a quick way to find files that must be changed.
290
291#endif // defined(_WIN32)
292
Ian Elliott20f06872015-02-12 17:08:34 -0700293#else /* LOADER_PLATFORM_H */
294#ifndef LOADER_PLATFORM_H_TEMP
295#define LOADER_PLATFORM_H_TEMP
296
297// NOTE: The following are hopefully-temporary macros to ensure that people
298// don't forget to use the loader_platform_*() functions above:
299
300#if defined(__linux__)
301/* Linux-specific common code: */
302
303// Dynamic Loading:
304#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
305#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
306#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
307#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
308
309// Threads:
310#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
311#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
312
313// Thread mutex:
314#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
315#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
316#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
317#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
318
319
320#elif defined(_WIN32) // defined(__linux__)
321/* Windows-specific common code: */
322
323// Dynamic Loading:
324//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
325#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
326#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
327
328// Threads:
329#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
330#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
331
332// Thread mutex:
333#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
334#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
335#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
336#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
337
338
339#endif // defined(_WIN32)
340#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott81ac44c2015-01-13 17:52:38 -0700341#endif /* LOADER_PLATFORM_H */