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