blob: b4f711afe1dcf061c463b6e7cf0f5bf2c2e78c7d [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:
Piers Daniell188f8202015-02-24 13:58:47 -0700138#include <WinSock2.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700139#include <windows.h>
140#include <assert.h>
141#ifdef __cplusplus
142#include <iostream>
143#include <string>
144using namespace std;
145#endif // __cplusplus
146
Ian Elliott457810e2015-02-04 11:22:39 -0700147// XGL Library Filenames, Paths, etc.:
148#define PATH_SEPERATOR ';'
149#define DIRECTORY_SYMBOL "\\"
Ian Elliott4470a302015-02-17 10:33:47 -0700150#define DRIVER_PATH_REGISTRY_VALUE "XGL_DRIVERS_PATH"
151#define LAYERS_PATH_REGISTRY_VALUE "XGL_LAYERS_PATH"
152#define LAYER_NAMES_REGISTRY_VALUE "XGL_LAYER_NAMES"
153#define DRIVER_PATH_ENV "XGL_DRIVERS_PATH"
154#define LAYERS_PATH_ENV "XGL_LAYERS_PATH"
155#define LAYER_NAMES_ENV "XGL_LAYER_NAMES"
Ian Elliott457810e2015-02-04 11:22:39 -0700156#ifndef DEFAULT_XGL_DRIVERS_PATH
157// TODO: Is this a good default location?
158// Need to search for both 32bit and 64bit ICDs
159#define DEFAULT_XGL_DRIVERS_PATH "C:\\Windows\\System32"
160// TODO/TBD: Is this an appropriate prefix for Windows?
161#define XGL_DRIVER_LIBRARY_PREFIX "XGL_"
162#define XGL_DRIVER_LIBRARY_PREFIX_LEN 4
163// TODO/TBD: Is this an appropriate suffix for Windows?
164#define XGL_LAYER_LIBRARY_PREFIX "XGLLayer"
165#define XGL_LAYER_LIBRARY_PREFIX_LEN 8
166#define XGL_LIBRARY_SUFFIX ".dll"
167#define XGL_LIBRARY_SUFFIX_LEN 4
168#endif // DEFAULT_XGL_DRIVERS_PATH
169#ifndef DEFAULT_XGL_LAYERS_PATH
170// TODO: Is this a good default location?
171#define DEFAULT_XGL_LAYERS_PATH "C:\\Windows\\System32"
172#endif // DEFAULT_XGL_LAYERS_PATH
173
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700174// C99:
175// Microsoft didn't implement C99 in Visual Studio; but started adding it with
176// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott19628802015-02-04 12:06:46 -0700177// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
178// "CMakeLists.txt" file).
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700179#define snprintf _snprintf
Tobin Ehlisb0497852015-02-11 14:24:02 -0700180#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700181// Microsoft also doesn't have basename(). Paths are different on Windows, and
182// so this is just a temporary solution in order to get us compiling, so that we
183// can test some scenarios, and develop the correct solution for Windows.
184 // TODO: Develop a better, permanent solution for Windows, to replace this
185 // temporary code:
186static char *basename(char *pathname)
187{
188 char *current, *next;
189
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700190// TODO/TBD: Do we need to deal with the Windows's ":" character?
191
Ian Elliott457810e2015-02-04 11:22:39 -0700192#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700193 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott457810e2015-02-04 11:22:39 -0700194 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700195 if (next == NULL) {
Ian Elliott457810e2015-02-04 11:22:39 -0700196 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700197 return current;
198 } else {
Ian Elliott457810e2015-02-04 11:22:39 -0700199 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700200 next++;
201 }
202 }
Ian Elliott19628802015-02-04 12:06:46 -0700203 // We shouldn't get to here, but this makes the compiler happy:
204 return current;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700205}
206
207// Dynamic Loading:
208typedef HMODULE loader_platform_dl_handle;
209static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
210{
211 return LoadLibrary(libPath);
212}
213static char * loader_platform_open_library_error(const char* libPath)
214{
215 static char errorMsg[120];
216 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
217 return errorMsg;
218}
219static void loader_platform_close_library(loader_platform_dl_handle library)
220{
221 FreeLibrary(library);
222}
223static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
224 const char *name)
225{
226 assert(library);
227 assert(name);
228 return GetProcAddress(library, name);
229}
230static char * loader_platform_get_proc_address_error(const char *name)
231{
232 static char errorMsg[120];
233 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
234 return errorMsg;
235}
236
237// Threads:
238typedef HANDLE loader_platform_thread;
239#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
240 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
Piers Daniell4da523a2015-02-23 16:23:13 -0700241static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
242{
243 void (*func)(void) = (void (*)(void))Parameter;
244 func();
245 return TRUE;
246}
247
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700248static void loader_platform_thread_once(void *ctl, void (* func) (void))
249{
250 assert(func != NULL);
251 assert(ctl != NULL);
Piers Daniell4da523a2015-02-23 16:23:13 -0700252 InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700253}
254
255// Thread IDs:
256typedef DWORD loader_platform_thread_id;
257static loader_platform_thread_id loader_platform_get_thread_id()
258{
259 return GetCurrentThreadId();
260}
261
262// Thread mutex:
263typedef CRITICAL_SECTION loader_platform_thread_mutex;
264static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
265{
266 InitializeCriticalSection(pMutex);
267}
268static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
269{
270 EnterCriticalSection(pMutex);
271}
272static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
273{
274 LeaveCriticalSection(pMutex);
275}
276static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
277{
278 DeleteCriticalSection(pMutex);
279}
280
281#else // defined(_WIN32)
282
283#error The "loader_platform.h" file must be modified for this OS.
284
285// NOTE: In order to support another OS, an #elif needs to be added (above the
286// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
287// contents of this file must be created.
288
289// NOTE: Other OS-specific changes are also needed for this OS. Search for
290// files with "WIN32" in it, as a quick way to find files that must be changed.
291
292#endif // defined(_WIN32)
293
Ian Elliott655cad72015-02-12 17:08:34 -0700294#else /* LOADER_PLATFORM_H */
295#ifndef LOADER_PLATFORM_H_TEMP
296#define LOADER_PLATFORM_H_TEMP
297
298// NOTE: The following are hopefully-temporary macros to ensure that people
299// don't forget to use the loader_platform_*() functions above:
300
301#if defined(__linux__)
302/* Linux-specific common code: */
303
304// Dynamic Loading:
305#define dlopen PLEASE USE THE loader_platform_open_library() FUNCTION
306#define dlerror PLEASE DO NOT USE THE dlerror() FUNCTION DIRECTLY
307#define dlclose PLEASE USE THE loader_platform_close_library() FUNCTION
308#define dlsym PLEASE USE THE loader_platform_get_proc_address() FUNCTION
309
310// Threads:
311#define pthread_once PLEASE USE THE loader_platform_thread_once() FUNCTION
312#define pthread_self PLEASE USE THE loader_platform_get_thread_id() FUNCTION
313
314// Thread mutex:
315#define pthread_mutex_init PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
316#define pthread_mutex_lock PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
317#define pthread_mutex_unlock PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
318#define pthread_mutex_destroy PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
319
320
321#elif defined(_WIN32) // defined(__linux__)
322/* Windows-specific common code: */
323
324// Dynamic Loading:
325//#define LoadLibrary PLEASE USE THE loader_platform_open_library() FUNCTION
326#define FreeLibrary PLEASE USE THE loader_platform_close_library() FUNCTION
327#define GetProcAddress PLEASE USE THE loader_platform_get_proc_address() FUNCTION
328
329// Threads:
330#define InitOnceExecuteOnce PLEASE USE THE loader_platform_thread_once() FUNCTION
331#define GetCurrentThreadId PLEASE USE THE loader_platform_get_thread_id() FUNCTION
332
333// Thread mutex:
334#define InitializeCriticalSection PLEASE USE THE loader_platform_thread_create_mutex() FUNCTION
335#define EnterCriticalSection PLEASE USE THE loader_platform_thread_lock_mutex() FUNCTION
336#define LeaveCriticalSection PLEASE USE THE loader_platform_thread_unlock_mutex() FUNCTION
337#define DeleteCriticalSection PLEASE USE THE loader_platform_thread_delete_mutex() FUNCTION
338
339
340#endif // defined(_WIN32)
341#endif /* LOADER_PLATFORM_H_TEMP */
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700342#endif /* LOADER_PLATFORM_H */