blob: 343fb142c3593acdd82aedebe1bf38eef43d1e40 [file] [log] [blame]
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001/*
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 Wu701f3f62014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Jon Ashburn01e2d662014-11-14 09:52:42 -070026 * Jon Ashburn <jon@lunarg.com>
Chia-I Wu701f3f62014-09-02 08:32:09 +080027 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080028 */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060029#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <stdarg.h>
33#include <stdbool.h>
34#include <string.h>
35
Chia-I Wu13a61a52014-08-04 11:18:20 +080036#include <sys/types.h>
37#include <dirent.h>
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -060038#include <unistd.h>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080039#include <dlfcn.h>
40#include <pthread.h>
Jon Ashburnd38bfb12014-10-14 19:15:22 -060041#include <assert.h>
Chia-I Wuf46b81a2015-01-04 11:12:47 +080042#include "table_ops.h"
Chia-I Wu19300602014-08-04 08:03:57 +080043#include "loader.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080044
Jon Ashburn1beab2d2015-01-26 14:51:40 -070045struct loader_instance {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070046 struct loader_icd *icds;
47 struct loader_instance *next;
48};
49
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060050struct loader_layers {
51 void *lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070052 char name[256];
53};
54
55struct layer_name_pair {
56 char *layer_name;
57 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060058};
59
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080060struct loader_icd {
Jon Ashburn46d1f582015-01-28 11:01:35 -070061 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080062
Jon Ashburn876b1ac2014-10-17 15:09:07 -060063 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060064 uint32_t layer_count[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060065 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnd09bd102014-10-22 21:15:26 -060066 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060067 uint32_t gpu_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060068 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060069
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080070 struct loader_icd *next;
71};
72
Jon Ashburnd38bfb12014-10-14 19:15:22 -060073
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080074struct loader_msg_callback {
75 XGL_DBG_MSG_CALLBACK_FUNCTION func;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060076 void *data;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080077
78 struct loader_msg_callback *next;
79};
80
Jon Ashburn46d1f582015-01-28 11:01:35 -070081struct loader_scanned_icds {
82 void *handle;
83 xglGetProcAddrType GetProcAddr;
84 xglInitAndEnumerateGpusType InitAndEnumerateGpus;
Jon Ashburn46888392015-01-29 15:45:51 -070085 xglCreateInstanceType CreateInstance;
86 xglDestroyInstanceType DestroyInstance;
Jon Ashburn4c392fb2015-01-28 19:57:09 -070087 xglEnumerateGpusType EnumerateGpus;
Jon Ashburn46888392015-01-29 15:45:51 -070088 XGL_INSTANCE instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070089 struct loader_scanned_icds *next;
90};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060091
Jon Ashburn1beab2d2015-01-26 14:51:40 -070092// Note: Since the following is a static structure, all members are initialized
93// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080094static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070095 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -070096 bool icds_scanned;
97 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060098 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -070099 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600100 unsigned int scanned_layer_count;
101 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800102 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
109static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600110 void *data)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800111{
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
127static 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
152static 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 Lobodzinski17caf572015-01-29 08:55:56 -0600165static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800166 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 Wuf1a5a742014-12-27 15:16:07 +0800187 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800188 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
207static void
208loader_icd_destroy(struct loader_icd *icd)
209{
Jon Ashburn46d1f582015-01-28 11:01:35 -0700210 dlclose(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800211 free(icd);
212}
213
214static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700215loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800216{
217 struct loader_icd *icd;
218
219 icd = malloc(sizeof(*icd));
220 if (!icd)
221 return NULL;
222
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600223 memset(icd, 0, sizeof(*icd));
224
Jon Ashburn46d1f582015-01-28 11:01:35 -0700225 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800226
227 return icd;
228}
229
230static 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 Lobodzinski17caf572015-01-29 08:55:56 -0600236 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700237 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
238 if (res != XGL_SUCCESS) {
239 break;
240 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800241 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242 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 Lobodzinski17caf572015-01-29 08:55:56 -0600250 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700251 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
252 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800253 tmp = tmp->next;
254 }
255
256 return res;
257 }
258
259 return XGL_SUCCESS;
260}
261
262static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
263{
264#define SETB(icd, opt, val) do { \
265 if (val) { \
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600266 for (uint32_t i = 0; i < icd->gpu_count; i++) { \
Jon Ashburn01e2d662014-11-14 09:52:42 -0700267 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 Wu5f72d0f2014-08-01 11:21:23 +0800272 } \
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
279return XGL_SUCCESS;
280}
281
Jon Ashburn46888392015-01-29 15:45:51 -0700282static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
283 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800284{
285 struct loader_icd *icd;
286
Jon Ashburn46d1f582015-01-28 11:01:35 -0700287 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800288 if (!icd)
289 return NULL;
290
Chia-I Wu13a61a52014-08-04 11:18:20 +0800291 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700292 icd->next = ptr_inst->icds;
293 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800294
295 return icd;
296}
297
Jon Ashburn46d1f582015-01-28 11:01:35 -0700298static void loader_scanned_icd_add(const char *filename)
299{
300 void *handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700301 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700302 void *fp_init;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700303 struct loader_scanned_icds *new_node;
304
305 handle = dlopen(filename, RTLD_LAZY);
306 if (!handle) {
307 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror());
308 return;
309 }
310
311#define LOOKUP(func_ptr, func) do { \
312 func_ptr = (xgl ##func## Type) dlsym(handle, "xgl" #func); \
313 if (!func_ptr) { \
314 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror()); \
315 return; \
316 } \
317} while (0)
318
319 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700320 LOOKUP(fp_create_inst, CreateInstance);
321 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700322 LOOKUP(fp_enumerate, EnumerateGpus);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700323 LOOKUP(fp_init, InitAndEnumerateGpus);
324#undef LOOKUP
325
326 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
327 if (!new_node) {
328 loader_log(XGL_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
329 return;
330 }
331
332 new_node->handle = handle;
333 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700334 new_node->CreateInstance = fp_create_inst;
335 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700336 new_node->EnumerateGpus = fp_enumerate;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700337 new_node->InitAndEnumerateGpus = fp_init;
338 new_node->next = loader.scanned_icd_list;
339 loader.scanned_icd_list = new_node;
340}
341
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600342#ifndef DEFAULT_XGL_DRIVERS_PATH
343// TODO: Is this a good default location?
344// Need to search for both 32bit and 64bit ICDs
345#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
346#endif
347
348/**
349 * Try to \c loader_icd_scan XGL driver(s).
350 *
351 * This function scans the default system path or path
352 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
353 * order to find loadable XGL ICDs with the name of libXGL_*.
354 *
355 * \returns
356 * void; but side effect is to set loader_icd_scanned to true
357 */
358static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800359{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600360 const char *libPaths, *p, *next;
361 DIR *sysdir;
362 struct dirent *dent;
363 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600364 char path[1024];
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600365 int len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800366
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600367 libPaths = NULL;
368 if (geteuid() == getuid()) {
369 /* don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
370 libPaths = getenv("LIBXGL_DRIVERS_PATH");
371 }
372 if (libPaths == NULL)
373 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800374
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600375 for (p = libPaths; *p; p = next) {
376 next = strchr(p, ':');
377 if (next == NULL) {
378 len = strlen(p);
379 next = p + len;
380 }
381 else {
382 len = next - p;
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600383 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
384 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600385 next++;
386 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800387
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600388 sysdir = opendir(p);
389 if (sysdir) {
390 dent = readdir(sysdir);
391 while (dent) {
392 /* look for ICDs starting with "libXGL_" */
393 if (!strncmp(dent->d_name, "libXGL_", 7)) {
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600394 snprintf(icd_library, 1024, "%s/%s",p,dent->d_name);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700395
396 loader_scanned_icd_add(icd_library);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600397 }
398
399 dent = readdir(sysdir);
400 }
401 closedir(sysdir);
402 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800403 }
404
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800405
Jon Ashburn46d1f582015-01-28 11:01:35 -0700406 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800407}
408
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600409#ifndef DEFAULT_XGL_LAYERS_PATH
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700410// TODO: Are these good default locations?
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600411#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
412#endif
413
Jon Ashburn46888392015-01-29 15:45:51 -0700414static void layer_lib_scan_path(const char * libInPaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600415{
416 const char *p, *next;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700417 char *libPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600418 DIR *curdir;
419 struct dirent *dent;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600420 int len, i;
421 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600422
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700423 len = 0;
424 loader.layer_dirs = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600425 if (libInPaths){
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700426 len = strlen(libInPaths);
427 p = libInPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600428 }
429 else {
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700430 if (geteuid() == getuid()) {
431 p = getenv("LIBXGL_LAYERS_PATH");
432 if (p != NULL)
433 len = strlen(p);
434 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600435 }
436
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700437 if (len == 0) {
438 len = strlen(DEFAULT_XGL_LAYERS_PATH);
439 p = DEFAULT_XGL_LAYERS_PATH;
440 }
441
442 if (len == 0) {
443 // Have no paths to search
444 return;
445 }
446 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700447 if (loader.layer_dirs == NULL)
448 return;
449
450 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
451 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700452 libPaths = loader.layer_dirs;
453
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600454 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600455 for (i = 0; i < loader.scanned_layer_count; i++) {
456 if (loader.scanned_layer_names[i] != NULL)
457 free(loader.scanned_layer_names[i]);
458 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600459 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600460 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600461
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600462 for (p = libPaths; *p; p = next) {
463 next = strchr(p, ':');
464 if (next == NULL) {
465 len = strlen(p);
466 next = p + len;
467 }
468 else {
469 len = next - p;
470 *(char *) next = '\0';
471 next++;
472 }
473
474 curdir = opendir(p);
475 if (curdir) {
476 dent = readdir(curdir);
477 while (dent) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600478 /* look for wrappers starting with "libXGLlayer" */
479 if (!strncmp(dent->d_name, "libXGLLayer", strlen("libXGLLayer"))) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600480 void * handle;
481 snprintf(temp_str, sizeof(temp_str), "%s/%s",p,dent->d_name);
Jon Ashburn2ae02262015-01-16 08:46:38 -0700482 if ((handle = dlopen(temp_str, RTLD_LAZY)) == NULL) {
483 dent = readdir(curdir);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600484 continue;
Jon Ashburn2ae02262015-01-16 08:46:38 -0700485 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600486 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
487 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
488 break;
489 }
490 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
491 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
492 break;
493 }
494 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
495 loader.scanned_layer_count++;
496 dlclose(handle);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600497 }
498
499 dent = readdir(curdir);
500 }
501 closedir(curdir);
502 }
503 }
504
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600505 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600506}
507
Jon Ashburn46888392015-01-29 15:45:51 -0700508static void layer_lib_scan()
509{
510 layer_lib_scan_path(NULL);
511}
512
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600513static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600514{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800515 loader_initialize_dispatch_table(tab, fpGPA, gpu);
516
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600517 if (tab->EnumerateLayers == NULL)
518 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600519}
520
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600521static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600522{
Jon Ashburn46d1f582015-01-28 11:01:35 -0700523 if (!loader.instances)
524 return NULL;
525
526 //TODO go through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700527 for (struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600528 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600529 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600530 *gpu_index = i;
531 return icd;
532 }
533 }
534 return NULL;
535}
536
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600537static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600538{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600539 if (icd->layer_count[gpu_index])
540 return true;
541 else
542 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600543}
544
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600545static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair * pLayerNames, uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600546{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600547 if (!icd)
548 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600549
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600550 struct loader_layers *obj;
551 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600552 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600553 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600554 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700555 if (icd->layer_libs[gpu_index][j].lib_handle && !strcmp(icd->layer_libs[gpu_index][j].name, (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600556 foundLib = true;
557 break;
558 }
559 }
560 if (!foundLib) {
561 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700562 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
563 obj->name[sizeof(obj->name) - 1] = '\0';
564 if ((obj->lib_handle = dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
565 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to open layer library %s got error %d", pLayerNames[i].lib_name, dlerror());
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600566 continue;
567 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700568 loader_log(XGL_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s", pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600569 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700570 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600571 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600572 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600573 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600574}
575
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600576static bool find_layer_name(struct loader_icd *icd, uint32_t gpu_index, const char * layer_name, const char **lib_name)
Jon Ashburnb8358052014-11-18 09:06:04 -0700577{
578 void *handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600579 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600580 char layer_buf[16][256];
581 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700582
583 for (int i = 0; i < 16; i++)
584 layers[i] = &layer_buf[i][0];
585
586 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
587 *lib_name = loader.scanned_layer_names[j];
588 if ((handle = dlopen(*lib_name, RTLD_LAZY)) == NULL)
589 continue;
590 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
591 //use default layer name based on library name libXGLLayer<name>.so
592 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
593 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), "libXGLLayer%s.so", layer_name);
594 dlclose(handle);
595 if (!strcmp(basename(*lib_name), lib_str)) {
596 free(lib_str);
597 return true;
598 }
599 else {
600 free(lib_str);
601 continue;
602 }
603 }
604 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600605 size_t cnt;
606 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (void *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700607 for (unsigned int i = 0; i < cnt; i++) {
608 if (!strcmp((char *) layers[i], layer_name)) {
609 dlclose(handle);
610 return true;
611 }
612 }
613 }
614
615 dlclose(handle);
616 }
617
618 return false;
619}
620
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600621static uint32_t loader_get_layer_env(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair *pLayerNames)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600622{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600623 const char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600624 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600625 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600626
Jon Ashburnb8358052014-11-18 09:06:04 -0700627 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn3fb55442014-10-23 10:29:09 -0600628 if (!layerEnv)
629 return 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600630 p = malloc(strlen(layerEnv) + 1);
631 if (!p)
632 return 0;
633 strcpy(p, layerEnv);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600634 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600635
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600636 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700637 const char *lib_name = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600638 next = strchr(p, ':');
639 if (next == NULL) {
640 len = strlen(p);
641 next = p + len;
642 }
643 else {
644 len = next - p;
645 *(char *) next = '\0';
646 next++;
647 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600648 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700649 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600650 p = next;
651 continue;
652 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600653
Jon Ashburnb8358052014-11-18 09:06:04 -0700654 len = strlen(name);
655 pLayerNames[count].layer_name = malloc(len + 1);
656 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600657 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600658 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600659 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700660 strncpy((char *) pLayerNames[count].layer_name, name, len);
661 pLayerNames[count].layer_name[len] = '\0';
662 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600663 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600664 p = next;
665
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600666 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600667
Jon Ashburnd09bd102014-10-22 21:15:26 -0600668 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600669 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600670}
671
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600672static 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 Ashburn6b4d70c2014-10-22 18:13:16 -0600673{
Jon Ashburnb8358052014-11-18 09:06:04 -0700674 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600675
676 *ppLayerNames = &layerNames[0];
677 if (!pCreateInfo) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700678 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600679 }
680
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800681 const XGL_LAYER_CREATE_INFO *pCi =
682 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600683
684 while (pCi) {
685 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
686 const char *name;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600687 uint32_t len;
688 for (uint32_t i = 0; i < pCi->layerCount; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700689 const char * lib_name = NULL;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800690 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnb8358052014-11-18 09:06:04 -0700691 if (!find_layer_name(icd, gpu_index, name, &lib_name))
692 return loader_get_layer_env(icd, gpu_index, layerNames);
693 len = strlen(name);
694 layerNames[i].layer_name = malloc(len + 1);
695 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600696 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700697 strncpy((char *) layerNames[i].layer_name, name, len);
698 layerNames[i].layer_name[len] = '\0';
699 layerNames[i].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600700 }
Courtney Goeltzenleuchter61e10b62015-01-07 10:17:44 -0700701 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600702 }
703 pCi = pCi->pNext;
704 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700705 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600706}
707
Jon Ashburn46d1f582015-01-28 11:01:35 -0700708static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600709{
710 struct loader_icd *icd;
711 struct loader_layers *libs;
712
Jon Ashburn46d1f582015-01-28 11:01:35 -0700713 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600714 if (icd->gpus)
715 free(icd->gpus);
716 icd->gpus = NULL;
717 if (icd->loader_dispatch)
718 free(icd->loader_dispatch);
719 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600720 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600721 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600722 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600723 libs = &(icd->layer_libs[j][i]);
724 if (libs->lib_handle)
725 dlclose(libs->lib_handle);
726 libs->lib_handle = NULL;
727 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600728 if (icd->wrappedGpus[j])
729 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600730 }
731 icd->layer_count[j] = 0;
732 }
733 icd->gpu_count = 0;
734 }
735}
736
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600737extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600738{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600739 uint32_t gpu_index;
740 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700741 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600742 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600743
744 if (!icd)
745 return 0;
746 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
747
748 /* activate any layer libraries */
749 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600750 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
751 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600752 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600753
Jon Ashburnb8358052014-11-18 09:06:04 -0700754 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600755 if (!count)
756 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700757 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600758
Jon Ashburnd09bd102014-10-22 21:15:26 -0600759 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
760 if (! icd->wrappedGpus[gpu_index])
761 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600762 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600763 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600764 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600765 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600766 nextGpuObj->nextObject = gpuObj;
767 gpuObj = nextGpuObj;
768
Jon Ashburn79113cc2014-12-01 14:22:40 -0700769 char funcStr[256];
770 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
771 if ((nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
772 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600773 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700774 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to find xglGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600775 continue;
776 }
777
Jon Ashburnf2610012014-10-24 15:48:55 -0600778 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600779 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600780 //Insert the new wrapped objects into the list with loader object at head
781 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
782 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
783 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
784 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700785 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600786 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600787
788 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600789 }
790 else {
791 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700792 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600793 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700794 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600795 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
796 break;
797 }
798 }
799 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700800 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600801 }
802 }
803 return icd->layer_count[gpu_index];
804}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600805
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700806LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
807 const XGL_APPLICATION_INFO* pAppInfo,
808 const XGL_ALLOC_CALLBACKS* pAllocCb,
809 XGL_INSTANCE* pInstance)
810{
Jon Ashburn46888392015-01-29 15:45:51 -0700811 static pthread_once_t once_icd = PTHREAD_ONCE_INIT;
812 static pthread_once_t once_layer = PTHREAD_ONCE_INIT;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700813 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700814 struct loader_scanned_icds *scanned_icds;
815 struct loader_icd *icd;
816 XGL_RESULT res;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600817
Jon Ashburn46888392015-01-29 15:45:51 -0700818 pthread_once(&once_icd, loader_icd_scan);
819
820 /* get layer libraries */
821 pthread_once(&once_layer, layer_lib_scan);
822
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700823 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
824 if (ptr_instance == NULL) {
825 return XGL_ERROR_OUT_OF_MEMORY;
826 }
827 memset(ptr_instance, 0, sizeof(struct loader_instance));
828
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700829 ptr_instance->next = loader.instances;
830 loader.instances = ptr_instance;
831
Jon Ashburn46888392015-01-29 15:45:51 -0700832 scanned_icds = loader.scanned_icd_list;
833 while (scanned_icds) {
834 icd = loader_icd_add(ptr_instance, scanned_icds);
835 if (icd) {
836 res = scanned_icds->CreateInstance(pAppInfo, pAllocCb,
837 &(scanned_icds->instance));
838 if (res != XGL_SUCCESS)
839 {
840 ptr_instance->icds = ptr_instance->icds->next;
841 loader_icd_destroy(icd);
842 scanned_icds->instance = NULL;
843 loader_log(XGL_DBG_MSG_WARNING, 0,
844 "ICD ignored: failed to CreateInstance on device");
845 }
846 }
847 scanned_icds = scanned_icds->next;
848 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700849
Jon Ashburn46888392015-01-29 15:45:51 -0700850 if (ptr_instance->icds == NULL)
851 return res;
852
853 *pInstance = (XGL_INSTANCE) ptr_instance;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700854 return XGL_SUCCESS;
855}
856
857LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
858 XGL_INSTANCE instance)
859{
860 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -0700861 struct loader_scanned_icds *scanned_icds;
862 XGL_RESULT res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700863
864 // Remove this instance from the list of instances:
865 struct loader_instance *prev = NULL;
866 struct loader_instance *next = loader.instances;
867 while (next != NULL) {
868 if (next == ptr_instance) {
869 // Remove this instance from the list:
870 if (prev)
871 prev->next = next->next;
872 break;
873 }
874 prev = next;
875 next = next->next;
876 }
877 if (next == NULL) {
878 // This must be an invalid instance handle or empty list
879 return XGL_ERROR_INVALID_HANDLE;
880 }
881
Jon Ashburn46888392015-01-29 15:45:51 -0700882 // cleanup any prior layer initializations
883 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700884
Jon Ashburn46888392015-01-29 15:45:51 -0700885 scanned_icds = loader.scanned_icd_list;
886 while (scanned_icds) {
887 if (scanned_icds->instance)
888 res = scanned_icds->DestroyInstance(scanned_icds->instance);
889 if (res != XGL_SUCCESS)
890 loader_log(XGL_DBG_MSG_WARNING, 0,
891 "ICD ignored: failed to DestroyInstance on device");
892 scanned_icds->instance = NULL;
893 scanned_icds = scanned_icds->next;
894 }
895
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700896 free(ptr_instance);
897
898 return XGL_SUCCESS;
899}
900
901LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
902
903 XGL_INSTANCE instance,
904 uint32_t maxGpus,
905 uint32_t* pGpuCount,
906 XGL_PHYSICAL_GPU* pGpus)
907{
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700908 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
909 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700910 uint32_t count = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700911 XGL_RESULT res;
912
913 //in spirit of XGL don't error check on the instance parameter
914 icd = ptr_instance->icds;
915 while (icd) {
916 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
917 XGL_BASE_LAYER_OBJECT * wrapped_gpus;
918 xglGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700919 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700920
921 if (max > XGL_MAX_PHYSICAL_GPUS) {
922 max = XGL_MAX_PHYSICAL_GPUS;
923 }
924
Jon Ashburn46888392015-01-29 15:45:51 -0700925 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
926 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700927 gpus);
928 if (res == XGL_SUCCESS && n) {
929 wrapped_gpus = (XGL_BASE_LAYER_OBJECT*) malloc(n *
930 sizeof(XGL_BASE_LAYER_OBJECT));
931 icd->gpus = wrapped_gpus;
932 icd->gpu_count = n;
933 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n *
934 sizeof(XGL_LAYER_DISPATCH_TABLE));
935 for (int i = 0; i < n; i++) {
936 (wrapped_gpus + i)->baseObject = gpus[i];
937 (wrapped_gpus + i)->pGPA = get_proc_addr;
938 (wrapped_gpus + i)->nextObject = gpus[i];
939 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
940 loader_init_dispatch_table(icd->loader_dispatch + i,
941 get_proc_addr, gpus[i]);
942 const XGL_LAYER_DISPATCH_TABLE **disp;
943 disp = (const XGL_LAYER_DISPATCH_TABLE **) gpus[i];
944 *disp = icd->loader_dispatch + i;
945 }
946
947 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
948 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
949 loader_log(XGL_DBG_MSG_WARNING, 0,
950 "ICD ignored: failed to migrate settings");
951 loader_icd_destroy(icd);
952 }
953 count += n;
954
955 if (count >= maxGpus) {
956 break;
957 }
958 }
959
960 icd = icd->next;
961 }
962
963 /* we have nothing to log anymore */
964 loader_msg_callback_clear();
965
966 *pGpuCount = count;
967
968 return (count > 0) ? XGL_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700969}
970
971LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
972{
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600973 if (gpu == NULL)
974 return NULL;
Jon Ashburn891537f2014-10-22 12:42:13 -0600975 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -0600976 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800977 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600978
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600979 if (disp_table == NULL)
980 return NULL;
981
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800982 addr = loader_lookup_dispatch_table(disp_table, pName);
983 if (addr)
984 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600985 else {
Jon Ashburnf2610012014-10-24 15:48:55 -0600986 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600987 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -0600988 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600989 }
990}
991
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600992LOADER_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 Wu5f72d0f2014-08-01 11:21:23 +0800993{
994 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600995 struct loader_icd *icd;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600996 uint32_t count = 0;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800997 XGL_RESULT res;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700998 struct loader_scanned_icds *scanned_icds;
999
1000 pthread_once(&once, loader_icd_scan);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001001
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001002 // for now only one instance
1003 if (loader.instances == NULL) {
1004 loader.instances = malloc(sizeof(struct loader_instance));
1005 if (loader.instances == NULL)
1006 return XGL_ERROR_UNAVAILABLE;
1007 memset(loader.instances, 0, sizeof(struct loader_instance));
Jon Ashburn46d1f582015-01-28 11:01:35 -07001008
1009 scanned_icds = loader.scanned_icd_list;
1010 while (scanned_icds) {
Jon Ashburn46888392015-01-29 15:45:51 -07001011 loader_icd_add(loader.instances, scanned_icds);
Jon Ashburn46d1f582015-01-28 11:01:35 -07001012 scanned_icds = scanned_icds->next;
1013 }
1014
1015 if (loader.instances->icds == NULL)
1016 return XGL_ERROR_UNAVAILABLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001017 }
Jon Ashburn46d1f582015-01-28 11:01:35 -07001018
Jon Ashburndf7d5842014-10-16 15:48:50 -06001019 // cleanup any prior layer initializations
Jon Ashburn46d1f582015-01-28 11:01:35 -07001020 loader_deactivate_layer(loader.instances);
Jon Ashburndf7d5842014-10-16 15:48:50 -06001021
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001022
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001023 icd = loader.instances->icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001024 while (icd) {
1025 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001026 XGL_BASE_LAYER_OBJECT * wrappedGpus;
Jon Ashburn46d1f582015-01-28 11:01:35 -07001027 xglGetProcAddrType getProcAddr = icd->scanned_icds->GetProcAddr;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001028 uint32_t n, max = maxGpus - count;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001029
1030 if (max > XGL_MAX_PHYSICAL_GPUS) {
1031 max = XGL_MAX_PHYSICAL_GPUS;
1032 }
1033
Jon Ashburn46d1f582015-01-28 11:01:35 -07001034 res = icd->scanned_icds->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu7f44d352014-08-06 12:17:04 +08001035 if (res == XGL_SUCCESS && n) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001036 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001037 icd->gpus = wrappedGpus;
Jon Ashburn876b1ac2014-10-17 15:09:07 -06001038 icd->gpu_count = n;
Jon Ashburndf7d5842014-10-16 15:48:50 -06001039 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001040 for (int i = 0; i < n; i++) {
1041 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburndf7d5842014-10-16 15:48:50 -06001042 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001043 (wrappedGpus + i)->nextObject = gpus[i];
1044 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn891537f2014-10-22 12:42:13 -06001045 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001046 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburndf7d5842014-10-16 15:48:50 -06001047 *disp = icd->loader_dispatch + i;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001048 }
1049
Jon Ashburn27a24af2014-12-18 17:00:52 -07001050 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
1051 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
1052 loader_log(XGL_DBG_MSG_WARNING, 0,
1053 "ICD ignored: failed to migrate settings");
1054 loader_icd_destroy(icd);
1055 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001056 count += n;
1057
1058 if (count >= maxGpus) {
1059 break;
1060 }
1061 }
1062
1063 icd = icd->next;
1064 }
1065
Jon Ashburn27a24af2014-12-18 17:00:52 -07001066 /* we have nothing to log anymore */
1067 loader_msg_callback_clear();
1068
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001069 /* get layer libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -06001070 if (!loader.layer_scanned)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -07001071 layer_lib_scan(NULL);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001072
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001073 *pGpuCount = count;
1074
1075 return (count > 0) ? XGL_SUCCESS : res;
1076}
1077
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001078LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001079{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001080 uint32_t gpu_index;
1081 uint32_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001082 char *lib_name;
1083 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
1084 void *handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -06001085 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001086 char layer_buf[16][256];
1087 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001088
Jon Ashburn1323eb72014-12-02 13:03:09 -07001089 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001090 return XGL_ERROR_INVALID_POINTER;
1091
Jon Ashburn46d1f582015-01-28 11:01:35 -07001092 if (!icd)
1093 return XGL_ERROR_UNAVAILABLE;
1094
Jon Ashburn1323eb72014-12-02 13:03:09 -07001095 for (int i = 0; i < 16; i++)
1096 layers[i] = &layer_buf[i][0];
1097
1098 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1099 lib_name = loader.scanned_layer_names[j];
1100 if ((handle = dlopen(lib_name, RTLD_LAZY)) == NULL)
1101 continue;
1102 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
1103 //use default layer name based on library name libXGLLayer<name>.so
1104 char *pEnd, *cpyStr;
1105 int siz;
1106 dlclose(handle);
1107 lib_name = basename(lib_name);
1108 pEnd = strrchr(lib_name, '.');
1109 siz = pEnd - lib_name - strlen("libXGLLayer") + 1;
1110 if (pEnd == NULL || siz <= 0)
1111 continue;
1112 cpyStr = malloc(siz);
1113 if (cpyStr == NULL) {
1114 free(cpyStr);
1115 continue;
1116 }
1117 strncpy(cpyStr, lib_name + strlen("libXGLLayer"), siz);
1118 cpyStr[siz - 1] = '\0';
1119 if (siz > maxStringSize)
1120 siz = maxStringSize;
1121 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1122 pOutLayers[count][siz - 1] = '\0';
1123 count++;
1124 free(cpyStr);
1125 }
1126 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001127 size_t cnt;
1128 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001129 XGL_RESULT res;
1130 n = (maxStringSize < 256) ? maxStringSize : 256;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001131 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (void *) icd->gpus + gpu_index);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001132 dlclose(handle);
1133 if (res != XGL_SUCCESS)
1134 continue;
1135 if (cnt + count > maxLayerCount)
1136 cnt = maxLayerCount - count;
1137 for (unsigned int i = count; i < cnt + count; i++) {
1138 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1139 if (n > 0)
1140 pOutLayers[i - count][n - 1] = '\0';
1141 }
1142 count += cnt;
1143 }
1144 }
1145
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001146 *pOutLayerCount = count;
1147
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001148 return XGL_SUCCESS;
1149}
1150
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001151LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001152{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001153 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001154 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001155 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001156
Jon Ashburn46d1f582015-01-28 11:01:35 -07001157 //TODO fix for uncreated instances
1158 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001159 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1160 }
1161
Jon Ashburn46d1f582015-01-28 11:01:35 -07001162 //TODO go through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001163 for (icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001164 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001165 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
1166 if (res != XGL_SUCCESS) {
1167 gpu_idx = i;
1168 break;
1169 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001170 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001171 if (res != XGL_SUCCESS)
1172 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001173 }
1174
1175 /* roll back on errors */
1176 if (icd) {
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001177 for (const struct loader_icd * tmp = loader.instances->icds; tmp != icd; tmp = tmp->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001178 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001179 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001180 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001181 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001182 for (uint32_t i = 0; i < gpu_idx; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001183 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001184
1185 return res;
1186 }
1187
1188 return XGL_SUCCESS;
1189}
1190
Chia-I Wu19300602014-08-04 08:03:57 +08001191LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001192{
Jon Ashburn46d1f582015-01-28 11:01:35 -07001193 XGL_RESULT res = XGL_SUCCESS;\
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001194
Jon Ashburn46d1f582015-01-28 11:01:35 -07001195 //TODO fix for uncreated instances
1196 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001197 return loader_msg_callback_remove(pfnMsgCallback);
1198 }
1199
Jon Ashburn46d1f582015-01-28 11:01:35 -07001200 //TODO loop through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001201 for (const struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001202 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001203 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1204 if (r != XGL_SUCCESS) {
1205 res = r;
1206 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001207 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001208 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001209 return res;
1210}
1211
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001212LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001213{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001214 XGL_RESULT res = XGL_SUCCESS;
1215
Jon Ashburn46d1f582015-01-28 11:01:35 -07001216 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001217 if (dataSize == 0)
1218 return XGL_ERROR_INVALID_VALUE;
1219
1220 switch (dbgOption) {
1221 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1222 loader.debug_echo_enable = *((const bool *) pData);
1223 break;
1224 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1225 loader.break_on_error = *((const bool *) pData);
1226 break;
1227 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1228 loader.break_on_warning = *((const bool *) pData);
1229 break;
1230 default:
1231 res = XGL_ERROR_INVALID_VALUE;
1232 break;
1233 }
1234
1235 return res;
1236 }
1237
Jon Ashburn46d1f582015-01-28 11:01:35 -07001238 //TODO loop through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001239 for (const struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001240 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001241 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
1242 /* unfortunately we cannot roll back */
1243 if (r != XGL_SUCCESS) {
1244 res = r;
1245 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001246 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001247 }
1248
1249 return res;
1250}