blob: c14ea9b90fb01635b1c74d00a071a12d02ed930d [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 "/"
48#ifndef DEFAULT_XGL_DRIVERS_PATH
49// TODO: Is this a good default location?
50// Need to search for both 32bit and 64bit ICDs
51#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
52#define XGL_DRIVER_LIBRARY_PREFIX "libXGL_"
53#define XGL_DRIVER_LIBRARY_PREFIX_LEN 7
54#define XGL_LAYER_LIBRARY_PREFIX "libXGLLayer"
55#define XGL_LAYER_LIBRARY_PREFIX_LEN 11
56#define XGL_LIBRARY_SUFFIX ".so"
57#define XGL_LIBRARY_SUFFIX_LEN 3
58#endif // DEFAULT_XGL_DRIVERS_PATH
59#ifndef DEFAULT_XGL_LAYERS_PATH
60// TODO: Are these good default locations?
61#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
62#endif
63
Tobin Ehlisbf0146e2015-02-11 14:24:02 -070064#define PRINTF_SIZE_T_SPECIFIER "%zu"
65
Ian Elliott81ac44c2015-01-13 17:52:38 -070066// Dynamic Loading:
67typedef void * loader_platform_dl_handle;
68static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath)
69{
70 // NOTE: The prior (Linux only) loader code always used RTLD_LAZY. In one
71 // place, it used RTLD_DEEPBIND. It probably doesn't hurt to always use
72 // RTLD_DEEPBIND, and so that is what is being done.
73 return dlopen(libPath, RTLD_LAZY | RTLD_DEEPBIND | RTLD_LOCAL);
74}
75static inline char * loader_platform_open_library_error(const char* libPath)
76{
77 return dlerror();
78}
79static inline void loader_platform_close_library(loader_platform_dl_handle library)
80{
81 dlclose(library);
82}
83static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library,
84 const char *name)
85{
86 assert(library);
87 assert(name);
88 return dlsym(library, name);
89}
90static inline char * loader_platform_get_proc_address_error(const char *name)
91{
92 return dlerror();
93}
94
95// Threads:
96typedef pthread_t loader_platform_thread;
97#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
98 pthread_once_t var = PTHREAD_ONCE_INIT;
99static inline void loader_platform_thread_once(void *ctl, void (* func) (void))
100{
101 assert(func != NULL);
102 assert(ctl != NULL);
103 pthread_once((pthread_once_t *) ctl, func);
104}
105
106// Thread IDs:
107typedef pthread_t loader_platform_thread_id;
108static inline loader_platform_thread_id loader_platform_get_thread_id()
109{
110 return pthread_self();
111}
112
113// Thread mutex:
114typedef pthread_mutex_t loader_platform_thread_mutex;
115static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
116{
117 pthread_mutex_init(pMutex, NULL);
118}
119static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
120{
121 pthread_mutex_lock(pMutex);
122}
123static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
124{
125 pthread_mutex_unlock(pMutex);
126}
127static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
128{
129 pthread_mutex_destroy(pMutex);
130}
131
132
133#elif defined(_WIN32) // defined(__linux__)
134/* Windows-specific common code: */
135
136// Headers:
137#include <windows.h>
138#include <assert.h>
139#ifdef __cplusplus
140#include <iostream>
141#include <string>
142using namespace std;
143#endif // __cplusplus
144
Ian Elliott665c5632015-02-04 11:22:39 -0700145// XGL Library Filenames, Paths, etc.:
146#define PATH_SEPERATOR ';'
147#define DIRECTORY_SYMBOL "\\"
148#ifndef DEFAULT_XGL_DRIVERS_PATH
149// TODO: Is this a good default location?
150// Need to search for both 32bit and 64bit ICDs
151#define DEFAULT_XGL_DRIVERS_PATH "C:\\Windows\\System32"
152// TODO/TBD: Is this an appropriate prefix for Windows?
153#define XGL_DRIVER_LIBRARY_PREFIX "XGL_"
154#define XGL_DRIVER_LIBRARY_PREFIX_LEN 4
155// TODO/TBD: Is this an appropriate suffix for Windows?
156#define XGL_LAYER_LIBRARY_PREFIX "XGLLayer"
157#define XGL_LAYER_LIBRARY_PREFIX_LEN 8
158#define XGL_LIBRARY_SUFFIX ".dll"
159#define XGL_LIBRARY_SUFFIX_LEN 4
160#endif // DEFAULT_XGL_DRIVERS_PATH
161#ifndef DEFAULT_XGL_LAYERS_PATH
162// TODO: Is this a good default location?
163#define DEFAULT_XGL_LAYERS_PATH "C:\\Windows\\System32"
164#endif // DEFAULT_XGL_LAYERS_PATH
165
Ian Elliott81ac44c2015-01-13 17:52:38 -0700166// C99:
167// Microsoft didn't implement C99 in Visual Studio; but started adding it with
168// VS2013. However, VS2013 still didn't have snprintf(). The following is a
Ian Elliott64f74a82015-02-04 12:06:46 -0700169// work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the
170// "CMakeLists.txt" file).
Ian Elliott81ac44c2015-01-13 17:52:38 -0700171#define snprintf _snprintf
Tobin Ehlisbf0146e2015-02-11 14:24:02 -0700172#define PRINTF_SIZE_T_SPECIFIER "%Iu"
Ian Elliott81ac44c2015-01-13 17:52:38 -0700173// Microsoft also doesn't have basename(). Paths are different on Windows, and
174// so this is just a temporary solution in order to get us compiling, so that we
175// can test some scenarios, and develop the correct solution for Windows.
176 // TODO: Develop a better, permanent solution for Windows, to replace this
177 // temporary code:
178static char *basename(char *pathname)
179{
180 char *current, *next;
181
Ian Elliott81ac44c2015-01-13 17:52:38 -0700182// TODO/TBD: Do we need to deal with the Windows's ":" character?
183
Ian Elliott665c5632015-02-04 11:22:39 -0700184#define DIRECTORY_SYMBOL_CHAR '\\'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700185 for (current = pathname; *current != '\0'; current = next) {
Ian Elliott665c5632015-02-04 11:22:39 -0700186 next = strchr(current, DIRECTORY_SYMBOL_CHAR);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700187 if (next == NULL) {
Ian Elliott665c5632015-02-04 11:22:39 -0700188 // No more DIRECTORY_SYMBOL_CHAR's so return p:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700189 return current;
190 } else {
Ian Elliott665c5632015-02-04 11:22:39 -0700191 // Point one character past the DIRECTORY_SYMBOL_CHAR:
Ian Elliott81ac44c2015-01-13 17:52:38 -0700192 next++;
193 }
194 }
Ian Elliott64f74a82015-02-04 12:06:46 -0700195 // We shouldn't get to here, but this makes the compiler happy:
196 return current;
Ian Elliott81ac44c2015-01-13 17:52:38 -0700197}
198
199// Dynamic Loading:
200typedef HMODULE loader_platform_dl_handle;
201static loader_platform_dl_handle loader_platform_open_library(const char* libPath)
202{
203 return LoadLibrary(libPath);
204}
205static char * loader_platform_open_library_error(const char* libPath)
206{
207 static char errorMsg[120];
208 snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath);
209 return errorMsg;
210}
211static void loader_platform_close_library(loader_platform_dl_handle library)
212{
213 FreeLibrary(library);
214}
215static void * loader_platform_get_proc_address(loader_platform_dl_handle library,
216 const char *name)
217{
218 assert(library);
219 assert(name);
220 return GetProcAddress(library, name);
221}
222static char * loader_platform_get_proc_address_error(const char *name)
223{
224 static char errorMsg[120];
225 snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name);
226 return errorMsg;
227}
228
229// Threads:
230typedef HANDLE loader_platform_thread;
231#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \
232 INIT_ONCE var = INIT_ONCE_STATIC_INIT;
233static void loader_platform_thread_once(void *ctl, void (* func) (void))
234{
235 assert(func != NULL);
236 assert(ctl != NULL);
237 InitOnceExecuteOnce((PINIT_ONCE) ctl, (PINIT_ONCE_FN) func, NULL, NULL);
238}
239
240// Thread IDs:
241typedef DWORD loader_platform_thread_id;
242static loader_platform_thread_id loader_platform_get_thread_id()
243{
244 return GetCurrentThreadId();
245}
246
247// Thread mutex:
248typedef CRITICAL_SECTION loader_platform_thread_mutex;
249static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex)
250{
251 InitializeCriticalSection(pMutex);
252}
253static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex)
254{
255 EnterCriticalSection(pMutex);
256}
257static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex)
258{
259 LeaveCriticalSection(pMutex);
260}
261static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex)
262{
263 DeleteCriticalSection(pMutex);
264}
265
266#else // defined(_WIN32)
267
268#error The "loader_platform.h" file must be modified for this OS.
269
270// NOTE: In order to support another OS, an #elif needs to be added (above the
271// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
272// contents of this file must be created.
273
274// NOTE: Other OS-specific changes are also needed for this OS. Search for
275// files with "WIN32" in it, as a quick way to find files that must be changed.
276
277#endif // defined(_WIN32)
278
279#endif /* LOADER_PLATFORM_H */