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