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