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