Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 45 | // 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 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 64 | // C99: |
| 65 | #define STATIC_INLINE static inline |
| 66 | |
| 67 | // Dynamic Loading: |
| 68 | typedef void * loader_platform_dl_handle; |
| 69 | static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 70 | { |
| 71 | // NOTE: The prior (Linux only) loader code always used RTLD_LAZY. In one |
| 72 | // place, it used RTLD_DEEPBIND. It probably doesn't hurt to always use |
| 73 | // RTLD_DEEPBIND, and so that is what is being done. |
| 74 | return dlopen(libPath, RTLD_LAZY | RTLD_DEEPBIND | RTLD_LOCAL); |
| 75 | } |
| 76 | static inline char * loader_platform_open_library_error(const char* libPath) |
| 77 | { |
| 78 | return dlerror(); |
| 79 | } |
| 80 | static inline void loader_platform_close_library(loader_platform_dl_handle library) |
| 81 | { |
| 82 | dlclose(library); |
| 83 | } |
| 84 | static 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 | } |
| 91 | static inline char * loader_platform_get_proc_address_error(const char *name) |
| 92 | { |
| 93 | return dlerror(); |
| 94 | } |
| 95 | |
| 96 | // Threads: |
| 97 | typedef pthread_t loader_platform_thread; |
| 98 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 99 | pthread_once_t var = PTHREAD_ONCE_INIT; |
| 100 | static 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: |
| 108 | typedef pthread_t loader_platform_thread_id; |
| 109 | static inline loader_platform_thread_id loader_platform_get_thread_id() |
| 110 | { |
| 111 | return pthread_self(); |
| 112 | } |
| 113 | |
| 114 | // Thread mutex: |
| 115 | typedef pthread_mutex_t loader_platform_thread_mutex; |
| 116 | static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 117 | { |
| 118 | pthread_mutex_init(pMutex, NULL); |
| 119 | } |
| 120 | static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 121 | { |
| 122 | pthread_mutex_lock(pMutex); |
| 123 | } |
| 124 | static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 125 | { |
| 126 | pthread_mutex_unlock(pMutex); |
| 127 | } |
| 128 | static 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> |
| 143 | using namespace std; |
| 144 | #endif // __cplusplus |
| 145 | |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 146 | // XGL Library Filenames, Paths, etc.: |
| 147 | #define PATH_SEPERATOR ';' |
| 148 | #define DIRECTORY_SYMBOL "\\" |
| 149 | #ifndef DEFAULT_XGL_DRIVERS_PATH |
| 150 | // TODO: Is this a good default location? |
| 151 | // Need to search for both 32bit and 64bit ICDs |
| 152 | #define DEFAULT_XGL_DRIVERS_PATH "C:\\Windows\\System32" |
| 153 | // TODO/TBD: Is this an appropriate prefix for Windows? |
| 154 | #define XGL_DRIVER_LIBRARY_PREFIX "XGL_" |
| 155 | #define XGL_DRIVER_LIBRARY_PREFIX_LEN 4 |
| 156 | // TODO/TBD: Is this an appropriate suffix for Windows? |
| 157 | #define XGL_LAYER_LIBRARY_PREFIX "XGLLayer" |
| 158 | #define XGL_LAYER_LIBRARY_PREFIX_LEN 8 |
| 159 | #define XGL_LIBRARY_SUFFIX ".dll" |
| 160 | #define XGL_LIBRARY_SUFFIX_LEN 4 |
| 161 | #endif // DEFAULT_XGL_DRIVERS_PATH |
| 162 | #ifndef DEFAULT_XGL_LAYERS_PATH |
| 163 | // TODO: Is this a good default location? |
| 164 | #define DEFAULT_XGL_LAYERS_PATH "C:\\Windows\\System32" |
| 165 | #endif // DEFAULT_XGL_LAYERS_PATH |
| 166 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 167 | // C99: |
| 168 | // Microsoft didn't implement C99 in Visual Studio; but started adding it with |
| 169 | // VS2013. However, VS2013 still didn't have snprintf(). The following is a |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame^] | 170 | // work-around (Note: The _CRT_SECURE_NO_WARNINGS macro must be set in the |
| 171 | // "CMakeLists.txt" file). |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 172 | #define snprintf _snprintf |
| 173 | #define STATIC_INLINE static |
| 174 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 175 | // so this is just a temporary solution in order to get us compiling, so that we |
| 176 | // can test some scenarios, and develop the correct solution for Windows. |
| 177 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 178 | // temporary code: |
| 179 | static char *basename(char *pathname) |
| 180 | { |
| 181 | char *current, *next; |
| 182 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 183 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 184 | |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 185 | #define DIRECTORY_SYMBOL_CHAR '\\' |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 186 | for (current = pathname; *current != '\0'; current = next) { |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 187 | next = strchr(current, DIRECTORY_SYMBOL_CHAR); |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 188 | if (next == NULL) { |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 189 | // No more DIRECTORY_SYMBOL_CHAR's so return p: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 190 | return current; |
| 191 | } else { |
Ian Elliott | 665c563 | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 192 | // Point one character past the DIRECTORY_SYMBOL_CHAR: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 193 | next++; |
| 194 | } |
| 195 | } |
Ian Elliott | 64f74a8 | 2015-02-04 12:06:46 -0700 | [diff] [blame^] | 196 | // We shouldn't get to here, but this makes the compiler happy: |
| 197 | return current; |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Dynamic Loading: |
| 201 | typedef HMODULE loader_platform_dl_handle; |
| 202 | static loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 203 | { |
| 204 | return LoadLibrary(libPath); |
| 205 | } |
| 206 | static char * loader_platform_open_library_error(const char* libPath) |
| 207 | { |
| 208 | static char errorMsg[120]; |
| 209 | snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath); |
| 210 | return errorMsg; |
| 211 | } |
| 212 | static void loader_platform_close_library(loader_platform_dl_handle library) |
| 213 | { |
| 214 | FreeLibrary(library); |
| 215 | } |
| 216 | static void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 217 | const char *name) |
| 218 | { |
| 219 | assert(library); |
| 220 | assert(name); |
| 221 | return GetProcAddress(library, name); |
| 222 | } |
| 223 | static char * loader_platform_get_proc_address_error(const char *name) |
| 224 | { |
| 225 | static char errorMsg[120]; |
| 226 | snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name); |
| 227 | return errorMsg; |
| 228 | } |
| 229 | |
| 230 | // Threads: |
| 231 | typedef HANDLE loader_platform_thread; |
| 232 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 233 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
| 234 | static void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 235 | { |
| 236 | assert(func != NULL); |
| 237 | assert(ctl != NULL); |
| 238 | InitOnceExecuteOnce((PINIT_ONCE) ctl, (PINIT_ONCE_FN) func, NULL, NULL); |
| 239 | } |
| 240 | |
| 241 | // Thread IDs: |
| 242 | typedef DWORD loader_platform_thread_id; |
| 243 | static loader_platform_thread_id loader_platform_get_thread_id() |
| 244 | { |
| 245 | return GetCurrentThreadId(); |
| 246 | } |
| 247 | |
| 248 | // Thread mutex: |
| 249 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
| 250 | static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 251 | { |
| 252 | InitializeCriticalSection(pMutex); |
| 253 | } |
| 254 | static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 255 | { |
| 256 | EnterCriticalSection(pMutex); |
| 257 | } |
| 258 | static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 259 | { |
| 260 | LeaveCriticalSection(pMutex); |
| 261 | } |
| 262 | static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 263 | { |
| 264 | DeleteCriticalSection(pMutex); |
| 265 | } |
| 266 | |
| 267 | #else // defined(_WIN32) |
| 268 | |
| 269 | #error The "loader_platform.h" file must be modified for this OS. |
| 270 | |
| 271 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 272 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 273 | // contents of this file must be created. |
| 274 | |
| 275 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 276 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 277 | |
| 278 | #endif // defined(_WIN32) |
| 279 | |
| 280 | #endif /* LOADER_PLATFORM_H */ |