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