blob: 6dd4013f1f7f9e0c733e24b7cdcaa19a70539e5e [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>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070037#if defined(WIN32)
38#include "dirent_on_windows.h"
39#else // WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080040#include <dirent.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070041#endif // WIN32
42#include "loader_platform.h"
Chia-I Wuf46b81a2015-01-04 11:12:47 +080043#include "table_ops.h"
Chia-I Wu19300602014-08-04 08:03:57 +080044#include "loader.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080045
Jon Ashburn1beab2d2015-01-26 14:51:40 -070046struct loader_instance {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070047 struct loader_icd *icds;
48 struct loader_instance *next;
49};
50
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060051struct loader_layers {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070052 loader_platform_dl_handle lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070053 char name[256];
54};
55
56struct layer_name_pair {
57 char *layer_name;
58 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060059};
60
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080061struct loader_icd {
Jon Ashburn46d1f582015-01-28 11:01:35 -070062 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080063
Jon Ashburn876b1ac2014-10-17 15:09:07 -060064 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060065 uint32_t layer_count[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060066 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnd09bd102014-10-22 21:15:26 -060067 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060068 uint32_t gpu_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060069 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060070
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080071 struct loader_icd *next;
72};
73
Jon Ashburnd38bfb12014-10-14 19:15:22 -060074
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080075struct loader_msg_callback {
76 XGL_DBG_MSG_CALLBACK_FUNCTION func;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060077 void *data;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080078
79 struct loader_msg_callback *next;
80};
81
Jon Ashburn46d1f582015-01-28 11:01:35 -070082struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070083 loader_platform_dl_handle handle;
Jon Ashburn46d1f582015-01-28 11:01:35 -070084 xglGetProcAddrType GetProcAddr;
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
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700109
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800110static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600111 void *data)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800112{
113 struct loader_msg_callback *cb;
114
115 cb = malloc(sizeof(*cb));
116 if (!cb)
117 return XGL_ERROR_OUT_OF_MEMORY;
118
119 cb->func = func;
120 cb->data = data;
121
122 cb->next = loader.msg_callbacks;
123 loader.msg_callbacks = cb;
124
125 return XGL_SUCCESS;
126}
127
128static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
129{
130 struct loader_msg_callback *cb = loader.msg_callbacks;
131
132 /*
133 * Find the first match (last registered).
134 *
135 * XXX What if the same callback function is registered more than once?
136 */
137 while (cb) {
138 if (cb->func == func) {
139 break;
140 }
141
142 cb = cb->next;
143 }
144
145 if (!cb)
146 return XGL_ERROR_INVALID_POINTER;
147
148 free(cb);
149
150 return XGL_SUCCESS;
151}
152
153static void loader_msg_callback_clear(void)
154{
155 struct loader_msg_callback *cb = loader.msg_callbacks;
156
157 while (cb) {
158 struct loader_msg_callback *next = cb->next;
159 free(cb);
160 cb = next;
161 }
162
163 loader.msg_callbacks = NULL;
164}
165
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600166static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800167 const char *format, ...)
168{
169 const struct loader_msg_callback *cb = loader.msg_callbacks;
170 char msg[256];
171 va_list ap;
172 int ret;
173
174 va_start(ap, format);
175 ret = vsnprintf(msg, sizeof(msg), format, ap);
176 if (ret >= sizeof(msg) || ret < 0) {
177 msg[sizeof(msg) - 1] = '\0';
178 }
179 va_end(ap);
180
181 if (loader.debug_echo_enable || !cb) {
182 fputs(msg, stderr);
183 fputc('\n', stderr);
184 }
185
186 while (cb) {
187 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800188 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800189 cb = cb->next;
190 }
191
192 switch (msg_type) {
193 case XGL_DBG_MSG_ERROR:
194 if (loader.break_on_error) {
195 exit(1);
196 }
197 /* fall through */
198 case XGL_DBG_MSG_WARNING:
199 if (loader.break_on_warning) {
200 exit(1);
201 }
202 break;
203 default:
204 break;
205 }
206}
207
208static void
209loader_icd_destroy(struct loader_icd *icd)
210{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700211 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800212 free(icd);
213}
214
215static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700216loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800217{
218 struct loader_icd *icd;
219
220 icd = malloc(sizeof(*icd));
221 if (!icd)
222 return NULL;
223
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600224 memset(icd, 0, sizeof(*icd));
225
Jon Ashburn46d1f582015-01-28 11:01:35 -0700226 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800227
228 return icd;
229}
230
231static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
232{
233 const struct loader_msg_callback *cb = loader.msg_callbacks;
234 XGL_RESULT res;
235
236 while (cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600237 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700238 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
239 if (res != XGL_SUCCESS) {
240 break;
241 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800243 cb = cb->next;
244 }
245
246 /* roll back on errors */
247 if (cb) {
248 const struct loader_msg_callback *tmp = loader.msg_callbacks;
249
250 while (tmp != cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600251 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700252 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
253 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800254 tmp = tmp->next;
255 }
256
257 return res;
258 }
259
260 return XGL_SUCCESS;
261}
262
263static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
264{
265#define SETB(icd, opt, val) do { \
266 if (val) { \
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600267 for (uint32_t i = 0; i < icd->gpu_count; i++) { \
Jon Ashburn01e2d662014-11-14 09:52:42 -0700268 const XGL_RESULT res = \
269 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
270 if (res != XGL_SUCCESS) \
271 return res; \
272 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800273 } \
274} while (0)
275 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
276 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
277 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
278#undef SETB
279
280return XGL_SUCCESS;
281}
282
Jon Ashburn46888392015-01-29 15:45:51 -0700283static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
284 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800285{
286 struct loader_icd *icd;
287
Jon Ashburn46d1f582015-01-28 11:01:35 -0700288 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800289 if (!icd)
290 return NULL;
291
Chia-I Wu13a61a52014-08-04 11:18:20 +0800292 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700293 icd->next = ptr_inst->icds;
294 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800295
296 return icd;
297}
298
Jon Ashburn46d1f582015-01-28 11:01:35 -0700299static void loader_scanned_icd_add(const char *filename)
300{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700301 loader_platform_dl_handle handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700302 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700303 struct loader_scanned_icds *new_node;
304
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700305 // Used to call: dlopen(filename, RTLD_LAZY);
306 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700307 if (!handle) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700308 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700309 return;
310 }
311
312#define LOOKUP(func_ptr, func) do { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700313 func_ptr = (xgl ##func## Type) loader_platform_get_proc_address(handle, "xgl" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700314 if (!func_ptr) { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700315 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("xgl" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700316 return; \
317 } \
318} while (0)
319
320 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700321 LOOKUP(fp_create_inst, CreateInstance);
322 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700323 LOOKUP(fp_enumerate, EnumerateGpus);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700324#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->next = loader.scanned_icd_list;
338 loader.scanned_icd_list = new_node;
339}
340
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700341
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600342/**
343 * Try to \c loader_icd_scan XGL driver(s).
344 *
345 * This function scans the default system path or path
346 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
347 * order to find loadable XGL ICDs with the name of libXGL_*.
348 *
349 * \returns
350 * void; but side effect is to set loader_icd_scanned to true
351 */
352static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800353{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600354 const char *libPaths, *p, *next;
355 DIR *sysdir;
356 struct dirent *dent;
357 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600358 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700359 uint32_t len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800360
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600361 libPaths = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700362#if !defined(WIN32)
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600363 if (geteuid() == getuid()) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700364 /* Don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
365#endif // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600366 libPaths = getenv("LIBXGL_DRIVERS_PATH");
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700367#if !defined(WIN32)
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600368 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700369#endif // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600370 if (libPaths == NULL)
371 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800372
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600373 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700374 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600375 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700376 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600377 next = p + len;
378 }
379 else {
Ian Elliott19628802015-02-04 12:06:46 -0700380 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600381 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
382 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600383 next++;
384 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800385
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700386 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
387 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600388 sysdir = opendir(p);
389 if (sysdir) {
390 dent = readdir(sysdir);
391 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700392 /* Look for ICDs starting with XGL_DRIVER_LIBRARY_PREFIX and
393 * ending with XGL_LIBRARY_SUFFIX
394 */
395 if (!strncmp(dent->d_name,
396 XGL_DRIVER_LIBRARY_PREFIX,
397 XGL_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700398 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700399 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
400 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
401 !strncmp(suf,
402 XGL_LIBRARY_SUFFIX,
403 XGL_LIBRARY_SUFFIX_LEN)) {
404 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
405 loader_scanned_icd_add(icd_library);
406 }
407 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600408
409 dent = readdir(sysdir);
410 }
411 closedir(sysdir);
412 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800413 }
414
Jon Ashburn46d1f582015-01-28 11:01:35 -0700415 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800416}
417
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600418
Jon Ashburn46888392015-01-29 15:45:51 -0700419static void layer_lib_scan_path(const char * libInPaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600420{
421 const char *p, *next;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700422 char *libPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600423 DIR *curdir;
424 struct dirent *dent;
Ian Elliott19628802015-02-04 12:06:46 -0700425 uint32_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600426 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600427
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700428 len = 0;
429 loader.layer_dirs = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600430 if (libInPaths){
Ian Elliott19628802015-02-04 12:06:46 -0700431 len = (uint32_t) strlen(libInPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700432 p = libInPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600433 }
434 else {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700435#if !defined(WIN32)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700436 if (geteuid() == getuid()) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700437#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700438 p = getenv("LIBXGL_LAYERS_PATH");
439 if (p != NULL)
Ian Elliott19628802015-02-04 12:06:46 -0700440 len = (uint32_t) strlen(p);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700441#if !defined(WIN32)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700442 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700443#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600444 }
445
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700446 if (len == 0) {
Ian Elliott19628802015-02-04 12:06:46 -0700447 len = (uint32_t) strlen(DEFAULT_XGL_LAYERS_PATH);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700448 p = DEFAULT_XGL_LAYERS_PATH;
449 }
450
451 if (len == 0) {
452 // Have no paths to search
453 return;
454 }
455 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700456 if (loader.layer_dirs == NULL)
457 return;
458
459 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
460 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700461 libPaths = loader.layer_dirs;
462
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600463 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600464 for (i = 0; i < loader.scanned_layer_count; i++) {
465 if (loader.scanned_layer_names[i] != NULL)
466 free(loader.scanned_layer_names[i]);
467 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600468 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600469 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600470
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600471 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700472 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600473 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700474 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600475 next = p + len;
476 }
477 else {
Ian Elliott19628802015-02-04 12:06:46 -0700478 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600479 *(char *) next = '\0';
480 next++;
481 }
482
483 curdir = opendir(p);
484 if (curdir) {
485 dent = readdir(curdir);
486 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700487 /* Look for layers starting with XGL_LAYER_LIBRARY_PREFIX and
488 * ending with XGL_LIBRARY_SUFFIX
489 */
490 if (!strncmp(dent->d_name,
491 XGL_LAYER_LIBRARY_PREFIX,
492 XGL_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700493 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700494 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
495 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
496 !strncmp(suf,
497 XGL_LIBRARY_SUFFIX,
498 XGL_LIBRARY_SUFFIX_LEN)) {
499 loader_platform_dl_handle handle;
500 snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
501 // Used to call: dlopen(temp_str, RTLD_LAZY)
502 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
503 dent = readdir(curdir);
504 continue;
505 }
506 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
507 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
508 break;
509 }
510 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
511 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
512 break;
513 }
514 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
515 loader.scanned_layer_count++;
516 loader_platform_close_library(handle);
517 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600518 }
519
520 dent = readdir(curdir);
521 }
522 closedir(curdir);
523 }
524 }
525
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600526 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600527}
528
Ian Elliott19628802015-02-04 12:06:46 -0700529static void layer_lib_scan(void)
Jon Ashburn46888392015-01-29 15:45:51 -0700530{
531 layer_lib_scan_path(NULL);
532}
533
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600534static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600535{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800536 loader_initialize_dispatch_table(tab, fpGPA, gpu);
537
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600538 if (tab->EnumerateLayers == NULL)
539 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600540}
541
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600542static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600543{
Jon Ashburn98bd4542015-01-29 16:44:24 -0700544 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
545 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
546 for (uint32_t i = 0; i < icd->gpu_count; i++)
547 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
548 gpu->baseObject) {
549 *gpu_index = i;
550 return icd;
551 }
552 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600553 }
554 return NULL;
555}
556
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600557static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600558{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600559 if (icd->layer_count[gpu_index])
560 return true;
561 else
562 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600563}
564
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600565static 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 -0600566{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600567 if (!icd)
568 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600569
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600570 struct loader_layers *obj;
571 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600572 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600573 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600574 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700575 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 -0600576 foundLib = true;
577 break;
578 }
579 }
580 if (!foundLib) {
581 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700582 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
583 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700584 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
585 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
586 loader_log(XGL_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600587 continue;
588 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700589 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 -0600590 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700591 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600592 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600593 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600594 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600595}
596
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600597static 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 -0700598{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700599 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600600 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600601 char layer_buf[16][256];
602 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700603
604 for (int i = 0; i < 16; i++)
605 layers[i] = &layer_buf[i][0];
606
607 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
608 *lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700609 // Used to call: dlopen(*lib_name, RTLD_LAZY)
610 if ((handle = loader_platform_open_library(*lib_name)) == NULL)
Jon Ashburnb8358052014-11-18 09:06:04 -0700611 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700612 if ((fpEnumerateLayers = (xglEnumerateLayersType) loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700613 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700614 //use default layer name
615 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name),
616 XGL_DRIVER_LIBRARY_PREFIX "%s" XGL_LIBRARY_SUFFIX,
617 layer_name);
618 loader_platform_close_library(handle);
619 if (!strcmp(*lib_name, lib_str)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700620 free(lib_str);
621 return true;
622 }
623 else {
624 free(lib_str);
625 continue;
626 }
627 }
628 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600629 size_t cnt;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700630 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (char *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700631 for (unsigned int i = 0; i < cnt; i++) {
632 if (!strcmp((char *) layers[i], layer_name)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700633 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700634 return true;
635 }
636 }
637 }
638
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700639 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700640 }
641
642 return false;
643}
644
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600645static 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 -0600646{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600647 const char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600648 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600649 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600650
Jon Ashburnb8358052014-11-18 09:06:04 -0700651 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn3fb55442014-10-23 10:29:09 -0600652 if (!layerEnv)
653 return 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600654 p = malloc(strlen(layerEnv) + 1);
655 if (!p)
656 return 0;
657 strcpy(p, layerEnv);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600658 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600659
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600660 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700661 const char *lib_name = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700662 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600663 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700664 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600665 next = p + len;
666 }
667 else {
Ian Elliott19628802015-02-04 12:06:46 -0700668 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600669 *(char *) next = '\0';
670 next++;
671 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600672 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700673 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600674 p = next;
675 continue;
676 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600677
Ian Elliott19628802015-02-04 12:06:46 -0700678 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700679 pLayerNames[count].layer_name = malloc(len + 1);
680 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600681 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600682 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600683 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700684 strncpy((char *) pLayerNames[count].layer_name, name, len);
685 pLayerNames[count].layer_name[len] = '\0';
686 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600687 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600688 p = next;
689
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600690 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600691
Jon Ashburnd09bd102014-10-22 21:15:26 -0600692 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600693 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600694}
695
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600696static 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 -0600697{
Jon Ashburnb8358052014-11-18 09:06:04 -0700698 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600699
700 *ppLayerNames = &layerNames[0];
701 if (!pCreateInfo) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700702 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600703 }
704
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800705 const XGL_LAYER_CREATE_INFO *pCi =
706 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600707
708 while (pCi) {
709 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
710 const char *name;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600711 uint32_t len;
712 for (uint32_t i = 0; i < pCi->layerCount; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700713 const char * lib_name = NULL;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800714 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnb8358052014-11-18 09:06:04 -0700715 if (!find_layer_name(icd, gpu_index, name, &lib_name))
716 return loader_get_layer_env(icd, gpu_index, layerNames);
Ian Elliott19628802015-02-04 12:06:46 -0700717 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700718 layerNames[i].layer_name = malloc(len + 1);
719 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600720 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700721 strncpy((char *) layerNames[i].layer_name, name, len);
722 layerNames[i].layer_name[len] = '\0';
723 layerNames[i].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600724 }
Courtney Goeltzenleuchter61e10b62015-01-07 10:17:44 -0700725 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600726 }
727 pCi = pCi->pNext;
728 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700729 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600730}
731
Jon Ashburn46d1f582015-01-28 11:01:35 -0700732static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600733{
734 struct loader_icd *icd;
735 struct loader_layers *libs;
736
Jon Ashburn46d1f582015-01-28 11:01:35 -0700737 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600738 if (icd->gpus)
739 free(icd->gpus);
740 icd->gpus = NULL;
741 if (icd->loader_dispatch)
742 free(icd->loader_dispatch);
743 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600744 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600745 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600746 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600747 libs = &(icd->layer_libs[j][i]);
748 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700749 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600750 libs->lib_handle = NULL;
751 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600752 if (icd->wrappedGpus[j])
753 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600754 }
755 icd->layer_count[j] = 0;
756 }
757 icd->gpu_count = 0;
758 }
759}
760
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600761extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600762{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600763 uint32_t gpu_index;
764 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700765 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600766 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600767
768 if (!icd)
769 return 0;
770 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
771
772 /* activate any layer libraries */
773 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600774 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
775 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600776 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600777
Jon Ashburnb8358052014-11-18 09:06:04 -0700778 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600779 if (!count)
780 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700781 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600782
Jon Ashburnd09bd102014-10-22 21:15:26 -0600783 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
784 if (! icd->wrappedGpus[gpu_index])
785 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600786 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600787 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600788 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600789 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600790 nextGpuObj->nextObject = gpuObj;
791 gpuObj = nextGpuObj;
792
Jon Ashburn79113cc2014-12-01 14:22:40 -0700793 char funcStr[256];
794 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700795 if ((nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
796 nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600797 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700798 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 -0600799 continue;
800 }
801
Jon Ashburnf2610012014-10-24 15:48:55 -0600802 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600803 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600804 //Insert the new wrapped objects into the list with loader object at head
805 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
806 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
807 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
808 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700809 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600810 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600811
812 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600813 }
814 else {
815 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700816 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600817 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700818 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600819 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
820 break;
821 }
822 }
823 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700824 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600825 }
826 }
827 return icd->layer_count[gpu_index];
828}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600829
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700830LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
831 const XGL_APPLICATION_INFO* pAppInfo,
832 const XGL_ALLOC_CALLBACKS* pAllocCb,
833 XGL_INSTANCE* pInstance)
834{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700835 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
836 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700837 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700838 struct loader_scanned_icds *scanned_icds;
839 struct loader_icd *icd;
840 XGL_RESULT res;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600841
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700842 /* Scan/discover all ICD libraries in a single-threaded manner */
843 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700844
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700845 /* get layer libraries in a single-threaded manner */
846 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700847
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700848 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
849 if (ptr_instance == NULL) {
850 return XGL_ERROR_OUT_OF_MEMORY;
851 }
852 memset(ptr_instance, 0, sizeof(struct loader_instance));
853
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700854 ptr_instance->next = loader.instances;
855 loader.instances = ptr_instance;
856
Jon Ashburn46888392015-01-29 15:45:51 -0700857 scanned_icds = loader.scanned_icd_list;
858 while (scanned_icds) {
859 icd = loader_icd_add(ptr_instance, scanned_icds);
860 if (icd) {
861 res = scanned_icds->CreateInstance(pAppInfo, pAllocCb,
862 &(scanned_icds->instance));
863 if (res != XGL_SUCCESS)
864 {
865 ptr_instance->icds = ptr_instance->icds->next;
866 loader_icd_destroy(icd);
867 scanned_icds->instance = NULL;
868 loader_log(XGL_DBG_MSG_WARNING, 0,
869 "ICD ignored: failed to CreateInstance on device");
870 }
871 }
872 scanned_icds = scanned_icds->next;
873 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700874
Ian Elliotteb450762015-02-05 15:19:15 -0700875 if (ptr_instance->icds == NULL) {
876 return XGL_ERROR_INCOMPATIBLE_DRIVER;
877 }
Jon Ashburn46888392015-01-29 15:45:51 -0700878
879 *pInstance = (XGL_INSTANCE) ptr_instance;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700880 return XGL_SUCCESS;
881}
882
883LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
884 XGL_INSTANCE instance)
885{
886 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -0700887 struct loader_scanned_icds *scanned_icds;
888 XGL_RESULT res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700889
890 // Remove this instance from the list of instances:
891 struct loader_instance *prev = NULL;
892 struct loader_instance *next = loader.instances;
893 while (next != NULL) {
894 if (next == ptr_instance) {
895 // Remove this instance from the list:
896 if (prev)
897 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -0700898 else
899 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700900 break;
901 }
902 prev = next;
903 next = next->next;
904 }
905 if (next == NULL) {
906 // This must be an invalid instance handle or empty list
907 return XGL_ERROR_INVALID_HANDLE;
908 }
909
Jon Ashburn46888392015-01-29 15:45:51 -0700910 // cleanup any prior layer initializations
911 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700912
Jon Ashburn46888392015-01-29 15:45:51 -0700913 scanned_icds = loader.scanned_icd_list;
914 while (scanned_icds) {
915 if (scanned_icds->instance)
916 res = scanned_icds->DestroyInstance(scanned_icds->instance);
917 if (res != XGL_SUCCESS)
918 loader_log(XGL_DBG_MSG_WARNING, 0,
919 "ICD ignored: failed to DestroyInstance on device");
920 scanned_icds->instance = NULL;
921 scanned_icds = scanned_icds->next;
922 }
923
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700924 free(ptr_instance);
925
926 return XGL_SUCCESS;
927}
928
929LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
930
931 XGL_INSTANCE instance,
932 uint32_t maxGpus,
933 uint32_t* pGpuCount,
934 XGL_PHYSICAL_GPU* pGpus)
935{
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700936 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
937 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700938 uint32_t count = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700939 XGL_RESULT res;
940
941 //in spirit of XGL don't error check on the instance parameter
942 icd = ptr_instance->icds;
943 while (icd) {
944 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
945 XGL_BASE_LAYER_OBJECT * wrapped_gpus;
946 xglGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700947 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700948
949 if (max > XGL_MAX_PHYSICAL_GPUS) {
950 max = XGL_MAX_PHYSICAL_GPUS;
951 }
952
Jon Ashburn46888392015-01-29 15:45:51 -0700953 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
954 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700955 gpus);
956 if (res == XGL_SUCCESS && n) {
957 wrapped_gpus = (XGL_BASE_LAYER_OBJECT*) malloc(n *
958 sizeof(XGL_BASE_LAYER_OBJECT));
959 icd->gpus = wrapped_gpus;
960 icd->gpu_count = n;
961 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n *
962 sizeof(XGL_LAYER_DISPATCH_TABLE));
Ian Elliott19628802015-02-04 12:06:46 -0700963 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700964 (wrapped_gpus + i)->baseObject = gpus[i];
965 (wrapped_gpus + i)->pGPA = get_proc_addr;
966 (wrapped_gpus + i)->nextObject = gpus[i];
967 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
968 loader_init_dispatch_table(icd->loader_dispatch + i,
969 get_proc_addr, gpus[i]);
970 const XGL_LAYER_DISPATCH_TABLE **disp;
971 disp = (const XGL_LAYER_DISPATCH_TABLE **) gpus[i];
972 *disp = icd->loader_dispatch + i;
973 }
974
975 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
976 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
977 loader_log(XGL_DBG_MSG_WARNING, 0,
978 "ICD ignored: failed to migrate settings");
979 loader_icd_destroy(icd);
980 }
981 count += n;
982
983 if (count >= maxGpus) {
984 break;
985 }
986 }
987
988 icd = icd->next;
989 }
990
991 /* we have nothing to log anymore */
992 loader_msg_callback_clear();
993
994 *pGpuCount = count;
995
996 return (count > 0) ? XGL_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700997}
998
999LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
1000{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001001 if (gpu == NULL) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001002 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001003 }
Jon Ashburn891537f2014-10-22 12:42:13 -06001004 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -06001005 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001006 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001007
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001008 if (disp_table == NULL)
1009 return NULL;
1010
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001011 addr = loader_lookup_dispatch_table(disp_table, pName);
1012 if (addr)
1013 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001014 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001015 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001016 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -06001017 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001018 }
1019}
1020
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001021LOADER_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 -06001022{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001023 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001024 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001025 char *lib_name;
1026 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001027 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -06001028 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001029 char layer_buf[16][256];
1030 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001031
Jon Ashburn1323eb72014-12-02 13:03:09 -07001032 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001033 return XGL_ERROR_INVALID_POINTER;
1034
Jon Ashburn46d1f582015-01-28 11:01:35 -07001035 if (!icd)
1036 return XGL_ERROR_UNAVAILABLE;
1037
Jon Ashburn1323eb72014-12-02 13:03:09 -07001038 for (int i = 0; i < 16; i++)
1039 layers[i] = &layer_buf[i][0];
1040
1041 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1042 lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001043 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1044 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001045 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001046 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
1047 //use default layer name based on library name XGL_LAYER_LIBRARY_PREFIX<name>.XGL_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001048 char *pEnd, *cpyStr;
1049 int siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001050 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001051 lib_name = basename(lib_name);
1052 pEnd = strrchr(lib_name, '.');
Ian Elliott19628802015-02-04 12:06:46 -07001053 siz = (int) (pEnd - lib_name - strlen(XGL_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001054 if (pEnd == NULL || siz <= 0)
1055 continue;
1056 cpyStr = malloc(siz);
1057 if (cpyStr == NULL) {
1058 free(cpyStr);
1059 continue;
1060 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001061 strncpy(cpyStr, lib_name + strlen(XGL_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001062 cpyStr[siz - 1] = '\0';
1063 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001064 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001065 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1066 pOutLayers[count][siz - 1] = '\0';
1067 count++;
1068 free(cpyStr);
1069 }
1070 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001071 size_t cnt;
1072 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001073 XGL_RESULT res;
Ian Elliott19628802015-02-04 12:06:46 -07001074 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001075 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1076 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001077 if (res != XGL_SUCCESS)
1078 continue;
1079 if (cnt + count > maxLayerCount)
1080 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001081 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001082 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1083 if (n > 0)
1084 pOutLayers[i - count][n - 1] = '\0';
1085 }
1086 count += cnt;
1087 }
1088 }
1089
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001090 *pOutLayerCount = count;
1091
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001092 return XGL_SUCCESS;
1093}
1094
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001095LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001096{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001097 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001098 struct loader_instance *inst;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001099 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001100 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001101
Jon Ashburn46d1f582015-01-28 11:01:35 -07001102 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001103 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1104 }
1105
Jon Ashburn98bd4542015-01-29 16:44:24 -07001106 for (inst = loader.instances; inst; inst = inst->next) {
1107 for (icd = inst->icds; icd; icd = icd->next) {
1108 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1109 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(
1110 pfnMsgCallback, pUserData);
1111 if (res != XGL_SUCCESS) {
1112 gpu_idx = i;
1113 break;
1114 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001115 }
Jon Ashburn98bd4542015-01-29 16:44:24 -07001116 if (res != XGL_SUCCESS)
1117 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001118 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001119 if (res != XGL_SUCCESS)
1120 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001121 }
1122
1123 /* roll back on errors */
1124 if (icd) {
Jon Ashburn98bd4542015-01-29 16:44:24 -07001125 for (struct loader_instance *tmp_inst = loader.instances;
1126 tmp_inst != inst; tmp_inst = tmp_inst->next) {
1127 for (const struct loader_icd * tmp = tmp_inst->icds; tmp != icd;
1128 tmp = tmp->next) {
1129 for (uint32_t i = 0; i < icd->gpu_count; i++)
1130 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1131 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001132 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001133 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001134 for (uint32_t i = 0; i < gpu_idx; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001135 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001136
1137 return res;
1138 }
1139
1140 return XGL_SUCCESS;
1141}
1142
Chia-I Wu19300602014-08-04 08:03:57 +08001143LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001144{
Jon Ashburn98bd4542015-01-29 16:44:24 -07001145 XGL_RESULT res = XGL_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001146
Jon Ashburn46d1f582015-01-28 11:01:35 -07001147 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001148 return loader_msg_callback_remove(pfnMsgCallback);
1149 }
1150
Jon Ashburn98bd4542015-01-29 16:44:24 -07001151 for (struct loader_instance *inst = loader.instances; inst;
1152 inst = inst->next) {
1153 for (const struct loader_icd * icd = inst->icds; icd;
1154 icd = icd->next) {
1155 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1156 XGL_RESULT r;
1157 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1158 if (r != XGL_SUCCESS) {
1159 res = r;
1160 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001161 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001162 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001163 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001164 return res;
1165}
1166
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001167LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001168{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001169 XGL_RESULT res = XGL_SUCCESS;
1170
Jon Ashburn46d1f582015-01-28 11:01:35 -07001171 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001172 if (dataSize == 0)
1173 return XGL_ERROR_INVALID_VALUE;
1174
1175 switch (dbgOption) {
1176 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1177 loader.debug_echo_enable = *((const bool *) pData);
1178 break;
1179 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1180 loader.break_on_error = *((const bool *) pData);
1181 break;
1182 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1183 loader.break_on_warning = *((const bool *) pData);
1184 break;
1185 default:
1186 res = XGL_ERROR_INVALID_VALUE;
1187 break;
1188 }
1189
1190 return res;
1191 }
1192
Jon Ashburn98bd4542015-01-29 16:44:24 -07001193 for (struct loader_instance *inst = loader.instances; inst;
1194 inst = inst->next) {
1195 for (const struct loader_icd * icd = inst->icds; icd;
1196 icd = icd->next) {
1197 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1198 XGL_RESULT r;
1199 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption,
1200 dataSize, pData);
1201 /* unfortunately we cannot roll back */
1202 if (r != XGL_SUCCESS) {
1203 res = r;
1204 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001205 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001206 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001207 }
1208
1209 return res;
1210}