Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * XGL |
| 3 | * |
| 4 | * Copyright (C) 2014 LunarG, Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
Chia-I Wu | 701f3f6 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 23 | * |
| 24 | * Authors: |
| 25 | * Chia-I Wu <olv@lunarg.com> |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 26 | * Jon Ashburn <jon@lunarg.com> |
Chia-I Wu | 701f3f6 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 27 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 28 | * Ian Elliott <ian@lunarg.com> |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 29 | */ |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 30 | #define _GNU_SOURCE |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <stdbool.h> |
| 35 | #include <string.h> |
| 36 | |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 37 | #include <sys/types.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 38 | #if defined(WIN32) |
| 39 | #include "dirent_on_windows.h" |
| 40 | #else // WIN32 |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 41 | #include <dirent.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 42 | #endif // WIN32 |
| 43 | #include "loader_platform.h" |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 44 | #include "table_ops.h" |
Chia-I Wu | 1930060 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 45 | #include "loader.h" |
Courtney Goeltzenleuchter | 64ca923 | 2015-02-10 18:40:14 -0700 | [diff] [blame] | 46 | #include "xglIcd.h" |
Ian Elliott | 655cad7 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 47 | // The following is #included again to catch certain OS-specific functions |
| 48 | // being used: |
| 49 | #include "loader_platform.h" |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 50 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 51 | struct loader_instance { |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 52 | struct loader_icd *icds; |
| 53 | struct loader_instance *next; |
| 54 | }; |
| 55 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 56 | struct loader_layers { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 57 | loader_platform_dl_handle lib_handle; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 58 | char name[256]; |
| 59 | }; |
| 60 | |
| 61 | struct layer_name_pair { |
| 62 | char *layer_name; |
| 63 | const char *lib_name; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 64 | }; |
| 65 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 66 | struct loader_icd { |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 67 | const struct loader_scanned_icds *scanned_icds; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 68 | |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 69 | XGL_LAYER_DISPATCH_TABLE *loader_dispatch; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 70 | uint32_t layer_count[XGL_MAX_PHYSICAL_GPUS]; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 71 | struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES]; |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 72 | XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS]; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 73 | uint32_t gpu_count; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 74 | XGL_BASE_LAYER_OBJECT *gpus; |
Jon Ashburn | df7d584 | 2014-10-16 15:48:50 -0600 | [diff] [blame] | 75 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 76 | struct loader_icd *next; |
| 77 | }; |
| 78 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 79 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 80 | struct loader_msg_callback { |
| 81 | XGL_DBG_MSG_CALLBACK_FUNCTION func; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 82 | void *data; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 83 | |
| 84 | struct loader_msg_callback *next; |
| 85 | }; |
| 86 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 87 | struct loader_scanned_icds { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 88 | loader_platform_dl_handle handle; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 89 | xglGetProcAddrType GetProcAddr; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 90 | xglCreateInstanceType CreateInstance; |
| 91 | xglDestroyInstanceType DestroyInstance; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 92 | xglEnumerateGpusType EnumerateGpus; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 93 | XGL_INSTANCE instance; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 94 | struct loader_scanned_icds *next; |
| 95 | }; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 96 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 97 | // Note: Since the following is a static structure, all members are initialized |
| 98 | // to zero. |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 99 | static struct { |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 100 | struct loader_instance *instances; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 101 | bool icds_scanned; |
| 102 | struct loader_scanned_icds *scanned_icd_list; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 103 | bool layer_scanned; |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 104 | char *layer_dirs; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 105 | unsigned int scanned_layer_count; |
| 106 | char *scanned_layer_names[MAX_LAYER_LIBRARIES]; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 107 | struct loader_msg_callback *msg_callbacks; |
| 108 | |
| 109 | bool debug_echo_enable; |
| 110 | bool break_on_error; |
| 111 | bool break_on_warning; |
| 112 | } loader; |
| 113 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 114 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 115 | #if defined(WIN32) |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 116 | char *loader_get_registry_string(const HKEY hive, |
| 117 | const LPCTSTR sub_key, |
| 118 | const char *value) |
| 119 | { |
| 120 | DWORD access_flags = KEY_QUERY_VALUE; |
| 121 | DWORD value_type; |
| 122 | HKEY key; |
| 123 | LONG rtn_value; |
| 124 | char *rtn_str = NULL; |
| 125 | size_t rtn_len = 0; |
| 126 | size_t allocated_len = 0; |
| 127 | |
| 128 | rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key); |
| 129 | if (rtn_value != ERROR_SUCCESS) { |
| 130 | // We didn't find the key. Try the 32-bit hive (where we've seen the |
| 131 | // key end up on some people's systems): |
| 132 | access_flags |= KEY_WOW64_32KEY; |
| 133 | rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key); |
| 134 | if (rtn_value != ERROR_SUCCESS) { |
| 135 | // We still couldn't find the key, so give up: |
| 136 | return NULL; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | rtn_value = RegQueryValueEx(key, value, NULL, &value_type, |
| 141 | (PVOID) rtn_str, &rtn_len); |
| 142 | if (rtn_value == ERROR_SUCCESS) { |
| 143 | // If we get to here, we found the key, and need to allocate memory |
| 144 | // large enough for rtn_str, and query again: |
| 145 | allocated_len = rtn_len + 4; |
| 146 | rtn_str = malloc(allocated_len); |
| 147 | rtn_value = RegQueryValueEx(key, value, NULL, &value_type, |
| 148 | (PVOID) rtn_str, &rtn_len); |
| 149 | if (rtn_value == ERROR_SUCCESS) { |
| 150 | // We added 4 extra bytes to rtn_str, so that we can ensure that |
| 151 | // the string is NULL-terminated (albeit, in a brute-force manner): |
| 152 | rtn_str[allocated_len-1] = '\0'; |
| 153 | } else { |
| 154 | // This should never occur, but in case it does, clean up: |
| 155 | free(rtn_str); |
| 156 | rtn_str = NULL; |
| 157 | } |
| 158 | } // else - shouldn't happen, but if it does, return rtn_str, which is NULL |
| 159 | |
| 160 | // Close the registry key that was opened: |
| 161 | RegCloseKey(key); |
| 162 | |
| 163 | return rtn_str; |
| 164 | } |
| 165 | |
| 166 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 167 | // For ICD developers, look in the registry, and look for an environment |
| 168 | // variable for a path(s) where to find the ICD(s): |
| 169 | static char *loader_get_registry_and_env(const char *env_var, |
| 170 | const char *registry_value) |
| 171 | { |
| 172 | char *env_str = getenv(env_var); |
| 173 | size_t env_len = (env_str == NULL) ? 0 : strlen(env_str); |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 174 | char *registry_str = NULL; |
| 175 | DWORD registry_len = 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 176 | char *rtn_str = NULL; |
| 177 | size_t rtn_len; |
| 178 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 179 | registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE, |
| 180 | "Software\\XGL", |
| 181 | registry_value); |
Ian Elliott | 2de26bc | 2015-04-03 13:13:01 -0600 | [diff] [blame] | 182 | registry_len = (registry_str) ? strlen(registry_str) : 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 183 | |
| 184 | rtn_len = env_len + registry_len + 1; |
| 185 | if (rtn_len <= 2) { |
| 186 | // We found neither the desired registry value, nor the environment |
| 187 | // variable; return NULL: |
| 188 | return NULL; |
| 189 | } else { |
| 190 | // We found something, and so we need to allocate memory for the string |
| 191 | // to return: |
| 192 | rtn_str = malloc(rtn_len); |
| 193 | } |
| 194 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 195 | if (registry_len == 0) { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 196 | // We didn't find the desired registry value, and so we must have found |
| 197 | // only the environment variable: |
| 198 | _snprintf(rtn_str, rtn_len, "%s", env_str); |
| 199 | } else if (env_str != NULL) { |
| 200 | // We found both the desired registry value and the environment |
| 201 | // variable, so concatenate them both: |
| 202 | _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str); |
| 203 | } else { |
| 204 | // We must have only found the desired registry value: |
| 205 | _snprintf(rtn_str, rtn_len, "%s", registry_str); |
| 206 | } |
| 207 | |
Ian Elliott | 2de26bc | 2015-04-03 13:13:01 -0600 | [diff] [blame] | 208 | if (registry_str) { |
| 209 | free(registry_str); |
| 210 | } |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 211 | |
| 212 | return(rtn_str); |
| 213 | } |
| 214 | #endif // WIN32 |
| 215 | |
| 216 | |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 217 | static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func, |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 218 | void *data) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 219 | { |
| 220 | struct loader_msg_callback *cb; |
| 221 | |
| 222 | cb = malloc(sizeof(*cb)); |
| 223 | if (!cb) |
| 224 | return XGL_ERROR_OUT_OF_MEMORY; |
| 225 | |
| 226 | cb->func = func; |
| 227 | cb->data = data; |
| 228 | |
| 229 | cb->next = loader.msg_callbacks; |
| 230 | loader.msg_callbacks = cb; |
| 231 | |
| 232 | return XGL_SUCCESS; |
| 233 | } |
| 234 | |
| 235 | static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func) |
| 236 | { |
| 237 | struct loader_msg_callback *cb = loader.msg_callbacks; |
| 238 | |
| 239 | /* |
| 240 | * Find the first match (last registered). |
| 241 | * |
| 242 | * XXX What if the same callback function is registered more than once? |
| 243 | */ |
| 244 | while (cb) { |
| 245 | if (cb->func == func) { |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | cb = cb->next; |
| 250 | } |
| 251 | |
| 252 | if (!cb) |
| 253 | return XGL_ERROR_INVALID_POINTER; |
| 254 | |
| 255 | free(cb); |
| 256 | |
| 257 | return XGL_SUCCESS; |
| 258 | } |
| 259 | |
| 260 | static void loader_msg_callback_clear(void) |
| 261 | { |
| 262 | struct loader_msg_callback *cb = loader.msg_callbacks; |
| 263 | |
| 264 | while (cb) { |
| 265 | struct loader_msg_callback *next = cb->next; |
| 266 | free(cb); |
| 267 | cb = next; |
| 268 | } |
| 269 | |
| 270 | loader.msg_callbacks = NULL; |
| 271 | } |
| 272 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 273 | static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code, |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 274 | const char *format, ...) |
| 275 | { |
| 276 | const struct loader_msg_callback *cb = loader.msg_callbacks; |
| 277 | char msg[256]; |
| 278 | va_list ap; |
| 279 | int ret; |
| 280 | |
| 281 | va_start(ap, format); |
| 282 | ret = vsnprintf(msg, sizeof(msg), format, ap); |
Ian Elliott | 4204584 | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 283 | if ((ret >= (int) sizeof(msg)) || ret < 0) { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 284 | msg[sizeof(msg) - 1] = '\0'; |
| 285 | } |
| 286 | va_end(ap); |
| 287 | |
| 288 | if (loader.debug_echo_enable || !cb) { |
| 289 | fputs(msg, stderr); |
| 290 | fputc('\n', stderr); |
| 291 | } |
| 292 | |
| 293 | while (cb) { |
| 294 | cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, |
Chia-I Wu | f1a5a74 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 295 | msg_code, msg, cb->data); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 296 | cb = cb->next; |
| 297 | } |
| 298 | |
| 299 | switch (msg_type) { |
| 300 | case XGL_DBG_MSG_ERROR: |
| 301 | if (loader.break_on_error) { |
| 302 | exit(1); |
| 303 | } |
| 304 | /* fall through */ |
| 305 | case XGL_DBG_MSG_WARNING: |
| 306 | if (loader.break_on_warning) { |
| 307 | exit(1); |
| 308 | } |
| 309 | break; |
| 310 | default: |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static void |
| 316 | loader_icd_destroy(struct loader_icd *icd) |
| 317 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 318 | loader_platform_close_library(icd->scanned_icds->handle); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 319 | free(icd); |
| 320 | } |
| 321 | |
| 322 | static struct loader_icd * |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 323 | loader_icd_create(const struct loader_scanned_icds *scanned) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 324 | { |
| 325 | struct loader_icd *icd; |
| 326 | |
| 327 | icd = malloc(sizeof(*icd)); |
| 328 | if (!icd) |
| 329 | return NULL; |
| 330 | |
Courtney Goeltzenleuchter | 55001bb | 2014-10-28 10:29:27 -0600 | [diff] [blame] | 331 | memset(icd, 0, sizeof(*icd)); |
| 332 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 333 | icd->scanned_icds = scanned; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 334 | |
| 335 | return icd; |
| 336 | } |
| 337 | |
| 338 | static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd) |
| 339 | { |
| 340 | const struct loader_msg_callback *cb = loader.msg_callbacks; |
| 341 | XGL_RESULT res; |
| 342 | |
| 343 | while (cb) { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 344 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 345 | res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data); |
| 346 | if (res != XGL_SUCCESS) { |
| 347 | break; |
| 348 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 349 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 350 | cb = cb->next; |
| 351 | } |
| 352 | |
| 353 | /* roll back on errors */ |
| 354 | if (cb) { |
| 355 | const struct loader_msg_callback *tmp = loader.msg_callbacks; |
| 356 | |
| 357 | while (tmp != cb) { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 358 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 359 | (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func); |
| 360 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 361 | tmp = tmp->next; |
| 362 | } |
| 363 | |
| 364 | return res; |
| 365 | } |
| 366 | |
| 367 | return XGL_SUCCESS; |
| 368 | } |
| 369 | |
| 370 | static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd) |
| 371 | { |
| 372 | #define SETB(icd, opt, val) do { \ |
| 373 | if (val) { \ |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 374 | for (uint32_t i = 0; i < icd->gpu_count; i++) { \ |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 375 | const XGL_RESULT res = \ |
| 376 | (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \ |
| 377 | if (res != XGL_SUCCESS) \ |
| 378 | return res; \ |
| 379 | } \ |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 380 | } \ |
| 381 | } while (0) |
| 382 | SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable); |
| 383 | SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error); |
| 384 | SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning); |
| 385 | #undef SETB |
| 386 | |
| 387 | return XGL_SUCCESS; |
| 388 | } |
| 389 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 390 | static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst, |
| 391 | const struct loader_scanned_icds *scanned) |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 392 | { |
| 393 | struct loader_icd *icd; |
| 394 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 395 | icd = loader_icd_create(scanned); |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 396 | if (!icd) |
| 397 | return NULL; |
| 398 | |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 399 | /* prepend to the list */ |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 400 | icd->next = ptr_inst->icds; |
| 401 | ptr_inst->icds = icd; |
Chia-I Wu | 13a61a5 | 2014-08-04 11:18:20 +0800 | [diff] [blame] | 402 | |
| 403 | return icd; |
| 404 | } |
| 405 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 406 | static void loader_scanned_icd_add(const char *filename) |
| 407 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 408 | loader_platform_dl_handle handle; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 409 | void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 410 | struct loader_scanned_icds *new_node; |
| 411 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 412 | // Used to call: dlopen(filename, RTLD_LAZY); |
| 413 | handle = loader_platform_open_library(filename); |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 414 | if (!handle) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 415 | loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename)); |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 416 | return; |
| 417 | } |
| 418 | |
| 419 | #define LOOKUP(func_ptr, func) do { \ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 420 | func_ptr = (xgl ##func## Type) loader_platform_get_proc_address(handle, "xgl" #func); \ |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 421 | if (!func_ptr) { \ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 422 | loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("xgl" #func)); \ |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 423 | return; \ |
| 424 | } \ |
| 425 | } while (0) |
| 426 | |
| 427 | LOOKUP(fp_gpa, GetProcAddr); |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 428 | LOOKUP(fp_create_inst, CreateInstance); |
| 429 | LOOKUP(fp_destroy_inst, DestroyInstance); |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 430 | LOOKUP(fp_enumerate, EnumerateGpus); |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 431 | #undef LOOKUP |
| 432 | |
| 433 | new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds)); |
| 434 | if (!new_node) { |
| 435 | loader_log(XGL_DBG_MSG_WARNING, 0, "Out of memory can't add icd"); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | new_node->handle = handle; |
| 440 | new_node->GetProcAddr = fp_gpa; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 441 | new_node->CreateInstance = fp_create_inst; |
| 442 | new_node->DestroyInstance = fp_destroy_inst; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 443 | new_node->EnumerateGpus = fp_enumerate; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 444 | new_node->next = loader.scanned_icd_list; |
| 445 | loader.scanned_icd_list = new_node; |
| 446 | } |
| 447 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 448 | |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 449 | /** |
| 450 | * Try to \c loader_icd_scan XGL driver(s). |
| 451 | * |
| 452 | * This function scans the default system path or path |
| 453 | * specified by the \c LIBXGL_DRIVERS_PATH environment variable in |
| 454 | * order to find loadable XGL ICDs with the name of libXGL_*. |
| 455 | * |
| 456 | * \returns |
| 457 | * void; but side effect is to set loader_icd_scanned to true |
| 458 | */ |
| 459 | static void loader_icd_scan(void) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 460 | { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 461 | const char *p, *next; |
| 462 | char *libPaths = NULL; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 463 | DIR *sysdir; |
| 464 | struct dirent *dent; |
| 465 | char icd_library[1024]; |
Jon Ashburn | 5cda59c | 2014-10-03 16:31:35 -0600 | [diff] [blame] | 466 | char path[1024]; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 467 | uint32_t len; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 468 | #if defined(WIN32) |
| 469 | bool must_free_libPaths; |
| 470 | libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV, |
| 471 | DRIVER_PATH_REGISTRY_VALUE); |
| 472 | if (libPaths != NULL) { |
| 473 | must_free_libPaths = true; |
| 474 | } else { |
| 475 | must_free_libPaths = false; |
| 476 | libPaths = DEFAULT_XGL_DRIVERS_PATH; |
| 477 | } |
| 478 | #else // WIN32 |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 479 | if (geteuid() == getuid()) { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 480 | /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */ |
| 481 | libPaths = getenv(DRIVER_PATH_ENV); |
| 482 | } |
| 483 | if (libPaths == NULL) { |
| 484 | libPaths = DEFAULT_XGL_DRIVERS_PATH; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 485 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 486 | #endif // WIN32 |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 487 | |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 488 | for (p = libPaths; *p; p = next) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 489 | next = strchr(p, PATH_SEPERATOR); |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 490 | if (next == NULL) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 491 | len = (uint32_t) strlen(p); |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 492 | next = p + len; |
| 493 | } |
| 494 | else { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 495 | len = (uint32_t) (next - p); |
Jon Ashburn | 5cda59c | 2014-10-03 16:31:35 -0600 | [diff] [blame] | 496 | sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p); |
| 497 | p = path; |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 498 | next++; |
| 499 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 500 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 501 | // TODO/TBD: Do we want to do this on Windows, or just let Windows take |
| 502 | // care of its own search path (which it apparently has)? |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 503 | sysdir = opendir(p); |
| 504 | if (sysdir) { |
| 505 | dent = readdir(sysdir); |
| 506 | while (dent) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 507 | /* Look for ICDs starting with XGL_DRIVER_LIBRARY_PREFIX and |
| 508 | * ending with XGL_LIBRARY_SUFFIX |
| 509 | */ |
| 510 | if (!strncmp(dent->d_name, |
| 511 | XGL_DRIVER_LIBRARY_PREFIX, |
| 512 | XGL_DRIVER_LIBRARY_PREFIX_LEN)) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 513 | uint32_t nlen = (uint32_t) strlen(dent->d_name); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 514 | const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN; |
| 515 | if ((nlen > XGL_LIBRARY_SUFFIX_LEN) && |
| 516 | !strncmp(suf, |
| 517 | XGL_LIBRARY_SUFFIX, |
| 518 | XGL_LIBRARY_SUFFIX_LEN)) { |
| 519 | snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name); |
| 520 | loader_scanned_icd_add(icd_library); |
| 521 | } |
| 522 | } |
Courtney Goeltzenleuchter | 4af9bd1 | 2014-08-01 14:18:11 -0600 | [diff] [blame] | 523 | |
| 524 | dent = readdir(sysdir); |
| 525 | } |
| 526 | closedir(sysdir); |
| 527 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 528 | } |
| 529 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 530 | #if defined(WIN32) |
| 531 | // Free any allocated memory: |
| 532 | if (must_free_libPaths) { |
| 533 | free(libPaths); |
| 534 | } |
| 535 | #endif // WIN32 |
| 536 | |
| 537 | // Note that we've scanned for ICDs: |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 538 | loader.icds_scanned = true; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 539 | } |
| 540 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 541 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 542 | static void layer_lib_scan(void) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 543 | { |
| 544 | const char *p, *next; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 545 | char *libPaths = NULL; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 546 | DIR *curdir; |
| 547 | struct dirent *dent; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 548 | size_t len, i; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 549 | char temp_str[1024]; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 550 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 551 | #if defined(WIN32) |
| 552 | bool must_free_libPaths; |
| 553 | libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV, |
| 554 | LAYERS_PATH_REGISTRY_VALUE); |
| 555 | if (libPaths != NULL) { |
| 556 | must_free_libPaths = true; |
| 557 | } else { |
| 558 | must_free_libPaths = false; |
| 559 | libPaths = DEFAULT_XGL_LAYERS_PATH; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 560 | } |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 561 | #else // WIN32 |
| 562 | if (geteuid() == getuid()) { |
| 563 | /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */ |
Courtney Goeltzenleuchter | 66b72f9 | 2015-02-18 20:03:02 -0700 | [diff] [blame] | 564 | libPaths = getenv(LAYERS_PATH_ENV); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 565 | } |
| 566 | if (libPaths == NULL) { |
| 567 | libPaths = DEFAULT_XGL_LAYERS_PATH; |
| 568 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 569 | #endif // WIN32 |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 570 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 571 | if (libPaths == NULL) { |
| 572 | // Have no paths to search: |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 573 | return; |
| 574 | } |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 575 | len = strlen(libPaths); |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 576 | loader.layer_dirs = malloc(len+1); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 577 | if (loader.layer_dirs == NULL) { |
| 578 | free(libPaths); |
Courtney Goeltzenleuchter | a66265b | 2014-12-02 18:12:51 -0700 | [diff] [blame] | 579 | return; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 580 | } |
| 581 | // Alloc passed, so we know there is enough space to hold the string, don't |
| 582 | // need strncpy |
| 583 | strcpy(loader.layer_dirs, libPaths); |
| 584 | #if defined(WIN32) |
| 585 | // Free any allocated memory: |
| 586 | if (must_free_libPaths) { |
| 587 | free(libPaths); |
| 588 | must_free_libPaths = false; |
| 589 | } |
| 590 | #endif // WIN32 |
Courtney Goeltzenleuchter | 57985ce | 2014-12-01 09:29:42 -0700 | [diff] [blame] | 591 | libPaths = loader.layer_dirs; |
| 592 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 593 | /* cleanup any previously scanned libraries */ |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 594 | for (i = 0; i < loader.scanned_layer_count; i++) { |
| 595 | if (loader.scanned_layer_names[i] != NULL) |
| 596 | free(loader.scanned_layer_names[i]); |
| 597 | loader.scanned_layer_names[i] = NULL; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 598 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 599 | loader.scanned_layer_count = 0; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 600 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 601 | for (p = libPaths; *p; p = next) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 602 | next = strchr(p, PATH_SEPERATOR); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 603 | if (next == NULL) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 604 | len = (uint32_t) strlen(p); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 605 | next = p + len; |
| 606 | } |
| 607 | else { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 608 | len = (uint32_t) (next - p); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 609 | *(char *) next = '\0'; |
| 610 | next++; |
| 611 | } |
| 612 | |
| 613 | curdir = opendir(p); |
| 614 | if (curdir) { |
| 615 | dent = readdir(curdir); |
| 616 | while (dent) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 617 | /* Look for layers starting with XGL_LAYER_LIBRARY_PREFIX and |
| 618 | * ending with XGL_LIBRARY_SUFFIX |
| 619 | */ |
| 620 | if (!strncmp(dent->d_name, |
| 621 | XGL_LAYER_LIBRARY_PREFIX, |
| 622 | XGL_LAYER_LIBRARY_PREFIX_LEN)) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 623 | uint32_t nlen = (uint32_t) strlen(dent->d_name); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 624 | const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN; |
| 625 | if ((nlen > XGL_LIBRARY_SUFFIX_LEN) && |
| 626 | !strncmp(suf, |
| 627 | XGL_LIBRARY_SUFFIX, |
| 628 | XGL_LIBRARY_SUFFIX_LEN)) { |
| 629 | loader_platform_dl_handle handle; |
| 630 | snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name); |
| 631 | // Used to call: dlopen(temp_str, RTLD_LAZY) |
| 632 | if ((handle = loader_platform_open_library(temp_str)) == NULL) { |
| 633 | dent = readdir(curdir); |
| 634 | continue; |
| 635 | } |
| 636 | if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) { |
| 637 | loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str); |
| 638 | break; |
| 639 | } |
| 640 | if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) { |
| 641 | loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str); |
| 642 | break; |
| 643 | } |
| 644 | strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str); |
| 645 | loader.scanned_layer_count++; |
| 646 | loader_platform_close_library(handle); |
| 647 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | dent = readdir(curdir); |
| 651 | } |
| 652 | closedir(curdir); |
| 653 | } |
| 654 | } |
| 655 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 656 | loader.layer_scanned = true; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 657 | } |
| 658 | |
Mark Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame] | 659 | static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 660 | { |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 661 | loader_initialize_dispatch_table(tab, fpGPA, gpu); |
| 662 | |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 663 | if (tab->EnumerateLayers == NULL) |
| 664 | tab->EnumerateLayers = xglEnumerateLayers; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 665 | } |
| 666 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 667 | static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index) |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 668 | { |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 669 | for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) { |
| 670 | for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) { |
| 671 | for (uint32_t i = 0; i < icd->gpu_count; i++) |
| 672 | if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == |
| 673 | gpu->baseObject) { |
| 674 | *gpu_index = i; |
| 675 | return icd; |
| 676 | } |
| 677 | } |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 678 | } |
| 679 | return NULL; |
| 680 | } |
| 681 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 682 | static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index) |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 683 | { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 684 | if (icd->layer_count[gpu_index]) |
| 685 | return true; |
| 686 | else |
| 687 | return false; |
Jon Ashburn | 876b1ac | 2014-10-17 15:09:07 -0600 | [diff] [blame] | 688 | } |
| 689 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 690 | static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair * pLayerNames, uint32_t count) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 691 | { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 692 | if (!icd) |
| 693 | return; |
Jon Ashburn | df7d584 | 2014-10-16 15:48:50 -0600 | [diff] [blame] | 694 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 695 | struct loader_layers *obj; |
| 696 | bool foundLib; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 697 | for (uint32_t i = 0; i < count; i++) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 698 | foundLib = false; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 699 | for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 700 | if (icd->layer_libs[gpu_index][j].lib_handle && !strcmp(icd->layer_libs[gpu_index][j].name, (char *) pLayerNames[i].layer_name)) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 701 | foundLib = true; |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | if (!foundLib) { |
| 706 | obj = &(icd->layer_libs[gpu_index][i]); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 707 | strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1); |
| 708 | obj->name[sizeof(obj->name) - 1] = '\0'; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 709 | // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND) |
| 710 | if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) { |
| 711 | loader_log(XGL_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name)); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 712 | continue; |
| 713 | } else { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 714 | loader_log(XGL_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s", pLayerNames[i].layer_name, pLayerNames[i].lib_name); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 715 | } |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 716 | free(pLayerNames[i].layer_name); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 717 | icd->layer_count[gpu_index]++; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 718 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 719 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 720 | } |
| 721 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 722 | static bool find_layer_name(struct loader_icd *icd, uint32_t gpu_index, const char * layer_name, const char **lib_name) |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 723 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 724 | loader_platform_dl_handle handle; |
Mark Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame] | 725 | xglEnumerateLayersType fpEnumerateLayers; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 726 | char layer_buf[16][256]; |
| 727 | char * layers[16]; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 728 | |
| 729 | for (int i = 0; i < 16; i++) |
| 730 | layers[i] = &layer_buf[i][0]; |
| 731 | |
| 732 | for (unsigned int j = 0; j < loader.scanned_layer_count; j++) { |
| 733 | *lib_name = loader.scanned_layer_names[j]; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 734 | // Used to call: dlopen(*lib_name, RTLD_LAZY) |
| 735 | if ((handle = loader_platform_open_library(*lib_name)) == NULL) |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 736 | continue; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 737 | if ((fpEnumerateLayers = (xglEnumerateLayersType) loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 738 | char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name)); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 739 | //use default layer name |
| 740 | snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), |
Courtney Goeltzenleuchter | 8d095ff | 2015-04-03 14:53:08 -0600 | [diff] [blame^] | 741 | XGL_LAYER_LIBRARY_PREFIX "%s" XGL_LIBRARY_SUFFIX, |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 742 | layer_name); |
| 743 | loader_platform_close_library(handle); |
| 744 | if (!strcmp(*lib_name, lib_str)) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 745 | free(lib_str); |
| 746 | return true; |
| 747 | } |
| 748 | else { |
| 749 | free(lib_str); |
| 750 | continue; |
| 751 | } |
| 752 | } |
| 753 | else { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 754 | size_t cnt; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 755 | fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (char *) icd->gpus + gpu_index); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 756 | for (unsigned int i = 0; i < cnt; i++) { |
| 757 | if (!strcmp((char *) layers[i], layer_name)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 758 | loader_platform_close_library(handle); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 759 | return true; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 764 | loader_platform_close_library(handle); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | return false; |
| 768 | } |
| 769 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 770 | static uint32_t loader_get_layer_env(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair *pLayerNames) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 771 | { |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 772 | char *layerEnv; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 773 | uint32_t len, count = 0; |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 774 | char *p, *pOrig, *next, *name; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 775 | |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 776 | #if defined(WIN32) |
| 777 | layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV, |
| 778 | LAYER_NAMES_REGISTRY_VALUE); |
| 779 | #else // WIN32 |
| 780 | layerEnv = getenv(LAYER_NAMES_ENV); |
| 781 | #endif // WIN32 |
| 782 | if (layerEnv == NULL) { |
Jon Ashburn | 3fb5544 | 2014-10-23 10:29:09 -0600 | [diff] [blame] | 783 | return 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 784 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 785 | p = malloc(strlen(layerEnv) + 1); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 786 | if (p == NULL) { |
| 787 | #if defined(WIN32) |
| 788 | free(layerEnv); |
| 789 | #endif // WIN32 |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 790 | return 0; |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 791 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 792 | strcpy(p, layerEnv); |
Ian Elliott | 4470a30 | 2015-02-17 10:33:47 -0700 | [diff] [blame] | 793 | #if defined(WIN32) |
| 794 | free(layerEnv); |
| 795 | #endif // WIN32 |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 796 | pOrig = p; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 797 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 798 | while (p && *p && count < MAX_LAYER_LIBRARIES) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 799 | const char *lib_name = NULL; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 800 | next = strchr(p, PATH_SEPERATOR); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 801 | if (next == NULL) { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 802 | len = (uint32_t) strlen(p); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 803 | next = p + len; |
| 804 | } |
| 805 | else { |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 806 | len = (uint32_t) (next - p); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 807 | *(char *) next = '\0'; |
| 808 | next++; |
| 809 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 810 | name = basename(p); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 811 | if (!find_layer_name(icd, gpu_index, name, &lib_name)) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 812 | p = next; |
| 813 | continue; |
| 814 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 815 | |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 816 | len = (uint32_t) strlen(name); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 817 | pLayerNames[count].layer_name = malloc(len + 1); |
| 818 | if (!pLayerNames[count].layer_name) { |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 819 | free(pOrig); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 820 | return count; |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 821 | } |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 822 | strncpy((char *) pLayerNames[count].layer_name, name, len); |
| 823 | pLayerNames[count].layer_name[len] = '\0'; |
| 824 | pLayerNames[count].lib_name = lib_name; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 825 | count++; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 826 | p = next; |
| 827 | |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 828 | }; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 829 | |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 830 | free(pOrig); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 831 | return count; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 832 | } |
| 833 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 834 | static uint32_t loader_get_layer_libs(struct loader_icd *icd, uint32_t gpu_index, const XGL_DEVICE_CREATE_INFO* pCreateInfo, struct layer_name_pair **ppLayerNames) |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 835 | { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 836 | static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES]; |
Courtney Goeltzenleuchter | 89ba928 | 2015-02-17 14:21:21 -0700 | [diff] [blame] | 837 | int env_layer_count = 0; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 838 | |
| 839 | *ppLayerNames = &layerNames[0]; |
Courtney Goeltzenleuchter | 89ba928 | 2015-02-17 14:21:21 -0700 | [diff] [blame] | 840 | /* Load any layers specified in the environment first */ |
| 841 | env_layer_count = loader_get_layer_env(icd, gpu_index, layerNames); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 842 | |
Chia-I Wu | b0a2cc9 | 2015-01-28 14:00:47 +0800 | [diff] [blame] | 843 | const XGL_LAYER_CREATE_INFO *pCi = |
| 844 | (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 845 | |
| 846 | while (pCi) { |
| 847 | if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) { |
| 848 | const char *name; |
Jon Ashburn | c0e8601 | 2015-02-18 11:29:58 -0700 | [diff] [blame] | 849 | uint32_t len, j = 0; |
Courtney Goeltzenleuchter | 89ba928 | 2015-02-17 14:21:21 -0700 | [diff] [blame] | 850 | for (uint32_t i = env_layer_count; i < (env_layer_count + pCi->layerCount); i++) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 851 | const char * lib_name = NULL; |
Jon Ashburn | c0e8601 | 2015-02-18 11:29:58 -0700 | [diff] [blame] | 852 | name = *(pCi->ppActiveLayerNames + j); |
| 853 | if (!find_layer_name(icd, gpu_index, name, &lib_name)) { |
| 854 | return i; |
| 855 | } |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 856 | len = (uint32_t) strlen(name); |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 857 | layerNames[i].layer_name = malloc(len + 1); |
| 858 | if (!layerNames[i].layer_name) |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 859 | return i; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 860 | strncpy((char *) layerNames[i].layer_name, name, len); |
| 861 | layerNames[i].layer_name[len] = '\0'; |
| 862 | layerNames[i].lib_name = lib_name; |
Jon Ashburn | c0e8601 | 2015-02-18 11:29:58 -0700 | [diff] [blame] | 863 | j++; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 864 | } |
Jon Ashburn | c0e8601 | 2015-02-18 11:29:58 -0700 | [diff] [blame] | 865 | return pCi->layerCount + env_layer_count; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 866 | } |
| 867 | pCi = pCi->pNext; |
| 868 | } |
Jon Ashburn | c0e8601 | 2015-02-18 11:29:58 -0700 | [diff] [blame] | 869 | return env_layer_count; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 870 | } |
| 871 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 872 | static void loader_deactivate_layer(const struct loader_instance *instance) |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 873 | { |
| 874 | struct loader_icd *icd; |
| 875 | struct loader_layers *libs; |
| 876 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 877 | for (icd = instance->icds; icd; icd = icd->next) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 878 | if (icd->gpus) |
| 879 | free(icd->gpus); |
| 880 | icd->gpus = NULL; |
| 881 | if (icd->loader_dispatch) |
| 882 | free(icd->loader_dispatch); |
| 883 | icd->loader_dispatch = NULL; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 884 | for (uint32_t j = 0; j < icd->gpu_count; j++) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 885 | if (icd->layer_count[j] > 0) { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 886 | for (uint32_t i = 0; i < icd->layer_count[j]; i++) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 887 | libs = &(icd->layer_libs[j][i]); |
| 888 | if (libs->lib_handle) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 889 | loader_platform_close_library(libs->lib_handle); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 890 | libs->lib_handle = NULL; |
| 891 | } |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 892 | if (icd->wrappedGpus[j]) |
| 893 | free(icd->wrappedGpus[j]); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 894 | } |
| 895 | icd->layer_count[j] = 0; |
| 896 | } |
| 897 | icd->gpu_count = 0; |
| 898 | } |
| 899 | } |
| 900 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 901 | extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo) |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 902 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 903 | uint32_t gpu_index; |
| 904 | uint32_t count; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 905 | struct layer_name_pair *pLayerNames; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 906 | struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 907 | |
| 908 | if (!icd) |
| 909 | return 0; |
| 910 | assert(gpu_index < XGL_MAX_PHYSICAL_GPUS); |
| 911 | |
| 912 | /* activate any layer libraries */ |
| 913 | if (!loader_layers_activated(icd, gpu_index)) { |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 914 | XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu; |
| 915 | XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject; |
Mark Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame] | 916 | xglGetProcAddrType nextGPA = xglGetProcAddr; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 917 | |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 918 | count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 919 | if (!count) |
| 920 | return 0; |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 921 | loader_init_layer_libs(icd, gpu_index, pLayerNames, count); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 922 | |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 923 | icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]); |
| 924 | if (! icd->wrappedGpus[gpu_index]) |
| 925 | loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer"); |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 926 | for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) { |
Jon Ashburn | d09bd10 | 2014-10-22 21:15:26 -0600 | [diff] [blame] | 927 | nextGpuObj = (icd->wrappedGpus[gpu_index] + i); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 928 | nextGpuObj->pGPA = nextGPA; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 929 | nextGpuObj->baseObject = baseObj; |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 930 | nextGpuObj->nextObject = gpuObj; |
| 931 | gpuObj = nextGpuObj; |
| 932 | |
Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 933 | char funcStr[256]; |
| 934 | snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 935 | if ((nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL) |
| 936 | nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr"); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 937 | if (!nextGPA) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 938 | loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to find xglGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 939 | continue; |
| 940 | } |
| 941 | |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 942 | if (i == 0) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 943 | loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj); |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 944 | //Insert the new wrapped objects into the list with loader object at head |
| 945 | ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj; |
| 946 | ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA; |
| 947 | gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1; |
| 948 | gpuObj->nextObject = baseObj; |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 949 | gpuObj->pGPA = icd->scanned_icds->GetProcAddr; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 950 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 951 | |
| 952 | } |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 953 | } |
| 954 | else { |
| 955 | //make sure requested Layers matches currently activated Layers |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 956 | count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames); |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 957 | for (uint32_t i = 0; i < count; i++) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 958 | if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) { |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 959 | loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested"); |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | if (count != icd->layer_count[gpu_index]) { |
Jon Ashburn | b835805 | 2014-11-18 09:06:04 -0700 | [diff] [blame] | 964 | loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested"); |
Jon Ashburn | 6b4d70c | 2014-10-22 18:13:16 -0600 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | return icd->layer_count[gpu_index]; |
| 968 | } |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 969 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 970 | LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance( |
| 971 | const XGL_APPLICATION_INFO* pAppInfo, |
| 972 | const XGL_ALLOC_CALLBACKS* pAllocCb, |
| 973 | XGL_INSTANCE* pInstance) |
| 974 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 975 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd); |
| 976 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer); |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 977 | struct loader_instance *ptr_instance = NULL; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 978 | struct loader_scanned_icds *scanned_icds; |
| 979 | struct loader_icd *icd; |
Ian Elliott | 55dbc5b | 2015-02-19 15:49:03 -0700 | [diff] [blame] | 980 | XGL_RESULT res = XGL_ERROR_INITIALIZATION_FAILED; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 981 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 982 | /* Scan/discover all ICD libraries in a single-threaded manner */ |
| 983 | loader_platform_thread_once(&once_icd, loader_icd_scan); |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 984 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 985 | /* get layer libraries in a single-threaded manner */ |
| 986 | loader_platform_thread_once(&once_layer, layer_lib_scan); |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 987 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 988 | ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance)); |
| 989 | if (ptr_instance == NULL) { |
| 990 | return XGL_ERROR_OUT_OF_MEMORY; |
| 991 | } |
| 992 | memset(ptr_instance, 0, sizeof(struct loader_instance)); |
| 993 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 994 | ptr_instance->next = loader.instances; |
| 995 | loader.instances = ptr_instance; |
| 996 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 997 | scanned_icds = loader.scanned_icd_list; |
| 998 | while (scanned_icds) { |
| 999 | icd = loader_icd_add(ptr_instance, scanned_icds); |
| 1000 | if (icd) { |
| 1001 | res = scanned_icds->CreateInstance(pAppInfo, pAllocCb, |
| 1002 | &(scanned_icds->instance)); |
| 1003 | if (res != XGL_SUCCESS) |
| 1004 | { |
| 1005 | ptr_instance->icds = ptr_instance->icds->next; |
| 1006 | loader_icd_destroy(icd); |
| 1007 | scanned_icds->instance = NULL; |
| 1008 | loader_log(XGL_DBG_MSG_WARNING, 0, |
| 1009 | "ICD ignored: failed to CreateInstance on device"); |
| 1010 | } |
| 1011 | } |
| 1012 | scanned_icds = scanned_icds->next; |
| 1013 | } |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1014 | |
Ian Elliott | eb45076 | 2015-02-05 15:19:15 -0700 | [diff] [blame] | 1015 | if (ptr_instance->icds == NULL) { |
| 1016 | return XGL_ERROR_INCOMPATIBLE_DRIVER; |
| 1017 | } |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1018 | |
| 1019 | *pInstance = (XGL_INSTANCE) ptr_instance; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1020 | return XGL_SUCCESS; |
| 1021 | } |
| 1022 | |
| 1023 | LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance( |
| 1024 | XGL_INSTANCE instance) |
| 1025 | { |
| 1026 | struct loader_instance *ptr_instance = (struct loader_instance *) instance; |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1027 | struct loader_scanned_icds *scanned_icds; |
| 1028 | XGL_RESULT res; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1029 | |
| 1030 | // Remove this instance from the list of instances: |
| 1031 | struct loader_instance *prev = NULL; |
| 1032 | struct loader_instance *next = loader.instances; |
| 1033 | while (next != NULL) { |
| 1034 | if (next == ptr_instance) { |
| 1035 | // Remove this instance from the list: |
| 1036 | if (prev) |
| 1037 | prev->next = next->next; |
Jon Ashburn | c5c4960 | 2015-02-03 09:26:59 -0700 | [diff] [blame] | 1038 | else |
| 1039 | loader.instances = next->next; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | prev = next; |
| 1043 | next = next->next; |
| 1044 | } |
| 1045 | if (next == NULL) { |
| 1046 | // This must be an invalid instance handle or empty list |
| 1047 | return XGL_ERROR_INVALID_HANDLE; |
| 1048 | } |
| 1049 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1050 | // cleanup any prior layer initializations |
| 1051 | loader_deactivate_layer(ptr_instance); |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1052 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1053 | scanned_icds = loader.scanned_icd_list; |
| 1054 | while (scanned_icds) { |
| 1055 | if (scanned_icds->instance) |
| 1056 | res = scanned_icds->DestroyInstance(scanned_icds->instance); |
| 1057 | if (res != XGL_SUCCESS) |
| 1058 | loader_log(XGL_DBG_MSG_WARNING, 0, |
| 1059 | "ICD ignored: failed to DestroyInstance on device"); |
| 1060 | scanned_icds->instance = NULL; |
| 1061 | scanned_icds = scanned_icds->next; |
| 1062 | } |
| 1063 | |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1064 | free(ptr_instance); |
| 1065 | |
| 1066 | return XGL_SUCCESS; |
| 1067 | } |
| 1068 | |
| 1069 | LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus( |
| 1070 | |
| 1071 | XGL_INSTANCE instance, |
| 1072 | uint32_t maxGpus, |
| 1073 | uint32_t* pGpuCount, |
| 1074 | XGL_PHYSICAL_GPU* pGpus) |
| 1075 | { |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1076 | struct loader_instance *ptr_instance = (struct loader_instance *) instance; |
| 1077 | struct loader_icd *icd; |
Jon Ashburn | 8d66a05 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 1078 | uint32_t count = 0; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1079 | XGL_RESULT res; |
| 1080 | |
| 1081 | //in spirit of XGL don't error check on the instance parameter |
| 1082 | icd = ptr_instance->icds; |
| 1083 | while (icd) { |
| 1084 | XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS]; |
| 1085 | XGL_BASE_LAYER_OBJECT * wrapped_gpus; |
| 1086 | xglGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr; |
Jon Ashburn | 8d66a05 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 1087 | uint32_t n, max = maxGpus - count; |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1088 | |
| 1089 | if (max > XGL_MAX_PHYSICAL_GPUS) { |
| 1090 | max = XGL_MAX_PHYSICAL_GPUS; |
| 1091 | } |
| 1092 | |
Jon Ashburn | 4688839 | 2015-01-29 15:45:51 -0700 | [diff] [blame] | 1093 | res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance, |
| 1094 | max, &n, |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1095 | gpus); |
| 1096 | if (res == XGL_SUCCESS && n) { |
| 1097 | wrapped_gpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * |
| 1098 | sizeof(XGL_BASE_LAYER_OBJECT)); |
| 1099 | icd->gpus = wrapped_gpus; |
| 1100 | icd->gpu_count = n; |
| 1101 | icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * |
| 1102 | sizeof(XGL_LAYER_DISPATCH_TABLE)); |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1103 | for (unsigned int i = 0; i < n; i++) { |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1104 | (wrapped_gpus + i)->baseObject = gpus[i]; |
| 1105 | (wrapped_gpus + i)->pGPA = get_proc_addr; |
| 1106 | (wrapped_gpus + i)->nextObject = gpus[i]; |
| 1107 | memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus)); |
| 1108 | loader_init_dispatch_table(icd->loader_dispatch + i, |
| 1109 | get_proc_addr, gpus[i]); |
Courtney Goeltzenleuchter | 64ca923 | 2015-02-10 18:40:14 -0700 | [diff] [blame] | 1110 | |
| 1111 | /* Verify ICD compatibility */ |
| 1112 | if (!valid_loader_magic_value(gpus[i])) { |
| 1113 | loader_log(XGL_DBG_MSG_WARNING, 0, |
| 1114 | "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n"); |
| 1115 | assert(0); |
| 1116 | } |
| 1117 | |
Jon Ashburn | 4c392fb | 2015-01-28 19:57:09 -0700 | [diff] [blame] | 1118 | const XGL_LAYER_DISPATCH_TABLE **disp; |
| 1119 | disp = (const XGL_LAYER_DISPATCH_TABLE **) gpus[i]; |
| 1120 | *disp = icd->loader_dispatch + i; |
| 1121 | } |
| 1122 | |
| 1123 | if (loader_icd_set_global_options(icd) != XGL_SUCCESS || |
| 1124 | loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) { |
| 1125 | loader_log(XGL_DBG_MSG_WARNING, 0, |
| 1126 | "ICD ignored: failed to migrate settings"); |
| 1127 | loader_icd_destroy(icd); |
| 1128 | } |
| 1129 | count += n; |
| 1130 | |
| 1131 | if (count >= maxGpus) { |
| 1132 | break; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | icd = icd->next; |
| 1137 | } |
| 1138 | |
| 1139 | /* we have nothing to log anymore */ |
| 1140 | loader_msg_callback_clear(); |
| 1141 | |
| 1142 | *pGpuCount = count; |
| 1143 | |
| 1144 | return (count > 0) ? XGL_SUCCESS : res; |
Jon Ashburn | 1beab2d | 2015-01-26 14:51:40 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName) |
| 1148 | { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1149 | if (gpu == NULL) { |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1150 | return NULL; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1151 | } |
Jon Ashburn | 891537f | 2014-10-22 12:42:13 -0600 | [diff] [blame] | 1152 | XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1153 | XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject; |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 1154 | void *addr; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1155 | |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1156 | if (disp_table == NULL) |
| 1157 | return NULL; |
| 1158 | |
Chia-I Wu | f46b81a | 2015-01-04 11:12:47 +0800 | [diff] [blame] | 1159 | addr = loader_lookup_dispatch_table(disp_table, pName); |
| 1160 | if (addr) |
| 1161 | return addr; |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1162 | else { |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1163 | if (disp_table->GetProcAddr == NULL) |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1164 | return NULL; |
Jon Ashburn | f261001 | 2014-10-24 15:48:55 -0600 | [diff] [blame] | 1165 | return disp_table->GetProcAddr(gpuw->nextObject, pName); |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1169 | LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved) |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1170 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1171 | uint32_t gpu_index; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1172 | size_t count = 0; |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1173 | char *lib_name; |
| 1174 | struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1175 | loader_platform_dl_handle handle; |
Mark Lobodzinski | 391bb6d | 2015-01-09 15:12:03 -0600 | [diff] [blame] | 1176 | xglEnumerateLayersType fpEnumerateLayers; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1177 | char layer_buf[16][256]; |
| 1178 | char * layers[16]; |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1179 | |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1180 | if (pOutLayerCount == NULL || pOutLayers == NULL) |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1181 | return XGL_ERROR_INVALID_POINTER; |
| 1182 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 1183 | if (!icd) |
| 1184 | return XGL_ERROR_UNAVAILABLE; |
| 1185 | |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1186 | for (int i = 0; i < 16; i++) |
| 1187 | layers[i] = &layer_buf[i][0]; |
| 1188 | |
| 1189 | for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) { |
| 1190 | lib_name = loader.scanned_layer_names[j]; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1191 | // Used to call: dlopen(*lib_name, RTLD_LAZY) |
| 1192 | if ((handle = loader_platform_open_library(lib_name)) == NULL) |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1193 | continue; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1194 | if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) { |
| 1195 | //use default layer name based on library name XGL_LAYER_LIBRARY_PREFIX<name>.XGL_LIBRARY_SUFFIX |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1196 | char *pEnd, *cpyStr; |
Ian Elliott | 4204584 | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 1197 | size_t siz; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1198 | loader_platform_close_library(handle); |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1199 | lib_name = basename(lib_name); |
| 1200 | pEnd = strrchr(lib_name, '.'); |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1201 | siz = (int) (pEnd - lib_name - strlen(XGL_LAYER_LIBRARY_PREFIX) + 1); |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1202 | if (pEnd == NULL || siz <= 0) |
| 1203 | continue; |
| 1204 | cpyStr = malloc(siz); |
| 1205 | if (cpyStr == NULL) { |
| 1206 | free(cpyStr); |
| 1207 | continue; |
| 1208 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1209 | strncpy(cpyStr, lib_name + strlen(XGL_LAYER_LIBRARY_PREFIX), siz); |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1210 | cpyStr[siz - 1] = '\0'; |
| 1211 | if (siz > maxStringSize) |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1212 | siz = (int) maxStringSize; |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1213 | strncpy((char *) (pOutLayers[count]), cpyStr, siz); |
| 1214 | pOutLayers[count][siz - 1] = '\0'; |
| 1215 | count++; |
| 1216 | free(cpyStr); |
| 1217 | } |
| 1218 | else { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1219 | size_t cnt; |
| 1220 | uint32_t n; |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1221 | XGL_RESULT res; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1222 | n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1223 | res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index); |
| 1224 | loader_platform_close_library(handle); |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1225 | if (res != XGL_SUCCESS) |
| 1226 | continue; |
| 1227 | if (cnt + count > maxLayerCount) |
| 1228 | cnt = maxLayerCount - count; |
Ian Elliott | 1962880 | 2015-02-04 12:06:46 -0700 | [diff] [blame] | 1229 | for (uint32_t i = (uint32_t) count; i < cnt + count; i++) { |
Jon Ashburn | 1323eb7 | 2014-12-02 13:03:09 -0700 | [diff] [blame] | 1230 | strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n); |
| 1231 | if (n > 0) |
| 1232 | pOutLayers[i - count][n - 1] = '\0'; |
| 1233 | } |
| 1234 | count += cnt; |
| 1235 | } |
| 1236 | } |
| 1237 | |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1238 | *pOutLayerCount = count; |
| 1239 | |
Jon Ashburn | f7bcf9b | 2014-10-15 15:30:23 -0600 | [diff] [blame] | 1240 | return XGL_SUCCESS; |
| 1241 | } |
| 1242 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1243 | LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1244 | { |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1245 | const struct loader_icd *icd; |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1246 | struct loader_instance *inst; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1247 | XGL_RESULT res; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1248 | uint32_t gpu_idx; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1249 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 1250 | if (!loader.icds_scanned) { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1251 | return loader_msg_callback_add(pfnMsgCallback, pUserData); |
| 1252 | } |
| 1253 | |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1254 | for (inst = loader.instances; inst; inst = inst->next) { |
| 1255 | for (icd = inst->icds; icd; icd = icd->next) { |
| 1256 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
| 1257 | res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback( |
| 1258 | pfnMsgCallback, pUserData); |
| 1259 | if (res != XGL_SUCCESS) { |
| 1260 | gpu_idx = i; |
| 1261 | break; |
| 1262 | } |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1263 | } |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1264 | if (res != XGL_SUCCESS) |
| 1265 | break; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1266 | } |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1267 | if (res != XGL_SUCCESS) |
| 1268 | break; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /* roll back on errors */ |
| 1272 | if (icd) { |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1273 | for (struct loader_instance *tmp_inst = loader.instances; |
| 1274 | tmp_inst != inst; tmp_inst = tmp_inst->next) { |
| 1275 | for (const struct loader_icd * tmp = tmp_inst->icds; tmp != icd; |
| 1276 | tmp = tmp->next) { |
| 1277 | for (uint32_t i = 0; i < icd->gpu_count; i++) |
| 1278 | (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback); |
| 1279 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1280 | } |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1281 | /* and gpus on current icd */ |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1282 | for (uint32_t i = 0; i < gpu_idx; i++) |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1283 | (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback); |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1284 | |
| 1285 | return res; |
| 1286 | } |
| 1287 | |
| 1288 | return XGL_SUCCESS; |
| 1289 | } |
| 1290 | |
Chia-I Wu | 1930060 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 1291 | LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1292 | { |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1293 | XGL_RESULT res = XGL_SUCCESS; |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1294 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 1295 | if (!loader.icds_scanned) { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1296 | return loader_msg_callback_remove(pfnMsgCallback); |
| 1297 | } |
| 1298 | |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1299 | for (struct loader_instance *inst = loader.instances; inst; |
| 1300 | inst = inst->next) { |
| 1301 | for (const struct loader_icd * icd = inst->icds; icd; |
| 1302 | icd = icd->next) { |
| 1303 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
| 1304 | XGL_RESULT r; |
| 1305 | r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback); |
| 1306 | if (r != XGL_SUCCESS) { |
| 1307 | res = r; |
| 1308 | } |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1309 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1310 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1311 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1312 | return res; |
| 1313 | } |
| 1314 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1315 | LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData) |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1316 | { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1317 | XGL_RESULT res = XGL_SUCCESS; |
| 1318 | |
Jon Ashburn | 46d1f58 | 2015-01-28 11:01:35 -0700 | [diff] [blame] | 1319 | if (!loader.icds_scanned) { |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1320 | if (dataSize == 0) |
| 1321 | return XGL_ERROR_INVALID_VALUE; |
| 1322 | |
| 1323 | switch (dbgOption) { |
| 1324 | case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE: |
| 1325 | loader.debug_echo_enable = *((const bool *) pData); |
| 1326 | break; |
| 1327 | case XGL_DBG_OPTION_BREAK_ON_ERROR: |
| 1328 | loader.break_on_error = *((const bool *) pData); |
| 1329 | break; |
| 1330 | case XGL_DBG_OPTION_BREAK_ON_WARNING: |
| 1331 | loader.break_on_warning = *((const bool *) pData); |
| 1332 | break; |
| 1333 | default: |
| 1334 | res = XGL_ERROR_INVALID_VALUE; |
| 1335 | break; |
| 1336 | } |
| 1337 | |
| 1338 | return res; |
| 1339 | } |
| 1340 | |
Jon Ashburn | 98bd454 | 2015-01-29 16:44:24 -0700 | [diff] [blame] | 1341 | for (struct loader_instance *inst = loader.instances; inst; |
| 1342 | inst = inst->next) { |
| 1343 | for (const struct loader_icd * icd = inst->icds; icd; |
| 1344 | icd = icd->next) { |
| 1345 | for (uint32_t i = 0; i < icd->gpu_count; i++) { |
| 1346 | XGL_RESULT r; |
| 1347 | r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, |
| 1348 | dataSize, pData); |
| 1349 | /* unfortunately we cannot roll back */ |
| 1350 | if (r != XGL_SUCCESS) { |
| 1351 | res = r; |
| 1352 | } |
Jon Ashburn | 01e2d66 | 2014-11-14 09:52:42 -0700 | [diff] [blame] | 1353 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1354 | } |
Chia-I Wu | 5f72d0f | 2014-08-01 11:21:23 +0800 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | return res; |
| 1358 | } |