blob: 4cc7c1a08a03f8918a1244cc02f31251af2d6c53 [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"
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -070045#include "xglIcd.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080046
Jon Ashburn1beab2d2015-01-26 14:51:40 -070047struct loader_instance {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070048 struct loader_icd *icds;
49 struct loader_instance *next;
50};
51
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060052struct loader_layers {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070053 loader_platform_dl_handle lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070054 char name[256];
55};
56
57struct layer_name_pair {
58 char *layer_name;
59 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060060};
61
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080062struct loader_icd {
Jon Ashburn46d1f582015-01-28 11:01:35 -070063 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080064
Jon Ashburn876b1ac2014-10-17 15:09:07 -060065 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060066 uint32_t layer_count[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060067 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnd09bd102014-10-22 21:15:26 -060068 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060069 uint32_t gpu_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060070 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060071
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080072 struct loader_icd *next;
73};
74
Jon Ashburnd38bfb12014-10-14 19:15:22 -060075
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080076struct loader_msg_callback {
77 XGL_DBG_MSG_CALLBACK_FUNCTION func;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060078 void *data;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080079
80 struct loader_msg_callback *next;
81};
82
Jon Ashburn46d1f582015-01-28 11:01:35 -070083struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070084 loader_platform_dl_handle handle;
Jon Ashburn46d1f582015-01-28 11:01:35 -070085 xglGetProcAddrType GetProcAddr;
Jon Ashburn46888392015-01-29 15:45:51 -070086 xglCreateInstanceType CreateInstance;
87 xglDestroyInstanceType DestroyInstance;
Jon Ashburn4c392fb2015-01-28 19:57:09 -070088 xglEnumerateGpusType EnumerateGpus;
Jon Ashburn46888392015-01-29 15:45:51 -070089 XGL_INSTANCE instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070090 struct loader_scanned_icds *next;
91};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060092
Jon Ashburn1beab2d2015-01-26 14:51:40 -070093// Note: Since the following is a static structure, all members are initialized
94// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080095static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070096 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -070097 bool icds_scanned;
98 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060099 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700100 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600101 unsigned int scanned_layer_count;
102 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800103 struct loader_msg_callback *msg_callbacks;
104
105 bool debug_echo_enable;
106 bool break_on_error;
107 bool break_on_warning;
108} loader;
109
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700110
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800111static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600112 void *data)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800113{
114 struct loader_msg_callback *cb;
115
116 cb = malloc(sizeof(*cb));
117 if (!cb)
118 return XGL_ERROR_OUT_OF_MEMORY;
119
120 cb->func = func;
121 cb->data = data;
122
123 cb->next = loader.msg_callbacks;
124 loader.msg_callbacks = cb;
125
126 return XGL_SUCCESS;
127}
128
129static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
130{
131 struct loader_msg_callback *cb = loader.msg_callbacks;
132
133 /*
134 * Find the first match (last registered).
135 *
136 * XXX What if the same callback function is registered more than once?
137 */
138 while (cb) {
139 if (cb->func == func) {
140 break;
141 }
142
143 cb = cb->next;
144 }
145
146 if (!cb)
147 return XGL_ERROR_INVALID_POINTER;
148
149 free(cb);
150
151 return XGL_SUCCESS;
152}
153
154static void loader_msg_callback_clear(void)
155{
156 struct loader_msg_callback *cb = loader.msg_callbacks;
157
158 while (cb) {
159 struct loader_msg_callback *next = cb->next;
160 free(cb);
161 cb = next;
162 }
163
164 loader.msg_callbacks = NULL;
165}
166
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600167static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800168 const char *format, ...)
169{
170 const struct loader_msg_callback *cb = loader.msg_callbacks;
171 char msg[256];
172 va_list ap;
173 int ret;
174
175 va_start(ap, format);
176 ret = vsnprintf(msg, sizeof(msg), format, ap);
177 if (ret >= sizeof(msg) || ret < 0) {
178 msg[sizeof(msg) - 1] = '\0';
179 }
180 va_end(ap);
181
182 if (loader.debug_echo_enable || !cb) {
183 fputs(msg, stderr);
184 fputc('\n', stderr);
185 }
186
187 while (cb) {
188 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800189 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800190 cb = cb->next;
191 }
192
193 switch (msg_type) {
194 case XGL_DBG_MSG_ERROR:
195 if (loader.break_on_error) {
196 exit(1);
197 }
198 /* fall through */
199 case XGL_DBG_MSG_WARNING:
200 if (loader.break_on_warning) {
201 exit(1);
202 }
203 break;
204 default:
205 break;
206 }
207}
208
209static void
210loader_icd_destroy(struct loader_icd *icd)
211{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700212 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800213 free(icd);
214}
215
216static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700217loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800218{
219 struct loader_icd *icd;
220
221 icd = malloc(sizeof(*icd));
222 if (!icd)
223 return NULL;
224
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600225 memset(icd, 0, sizeof(*icd));
226
Jon Ashburn46d1f582015-01-28 11:01:35 -0700227 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800228
229 return icd;
230}
231
232static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
233{
234 const struct loader_msg_callback *cb = loader.msg_callbacks;
235 XGL_RESULT res;
236
237 while (cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600238 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700239 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
240 if (res != XGL_SUCCESS) {
241 break;
242 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800243 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800244 cb = cb->next;
245 }
246
247 /* roll back on errors */
248 if (cb) {
249 const struct loader_msg_callback *tmp = loader.msg_callbacks;
250
251 while (tmp != cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600252 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700253 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
254 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800255 tmp = tmp->next;
256 }
257
258 return res;
259 }
260
261 return XGL_SUCCESS;
262}
263
264static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
265{
266#define SETB(icd, opt, val) do { \
267 if (val) { \
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600268 for (uint32_t i = 0; i < icd->gpu_count; i++) { \
Jon Ashburn01e2d662014-11-14 09:52:42 -0700269 const XGL_RESULT res = \
270 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
271 if (res != XGL_SUCCESS) \
272 return res; \
273 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800274 } \
275} while (0)
276 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
277 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
278 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
279#undef SETB
280
281return XGL_SUCCESS;
282}
283
Jon Ashburn46888392015-01-29 15:45:51 -0700284static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
285 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800286{
287 struct loader_icd *icd;
288
Jon Ashburn46d1f582015-01-28 11:01:35 -0700289 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800290 if (!icd)
291 return NULL;
292
Chia-I Wu13a61a52014-08-04 11:18:20 +0800293 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700294 icd->next = ptr_inst->icds;
295 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800296
297 return icd;
298}
299
Jon Ashburn46d1f582015-01-28 11:01:35 -0700300static void loader_scanned_icd_add(const char *filename)
301{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700302 loader_platform_dl_handle handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700303 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700304 struct loader_scanned_icds *new_node;
305
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700306 // Used to call: dlopen(filename, RTLD_LAZY);
307 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700308 if (!handle) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700309 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700310 return;
311 }
312
313#define LOOKUP(func_ptr, func) do { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700314 func_ptr = (xgl ##func## Type) loader_platform_get_proc_address(handle, "xgl" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700315 if (!func_ptr) { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700316 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("xgl" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700317 return; \
318 } \
319} while (0)
320
321 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700322 LOOKUP(fp_create_inst, CreateInstance);
323 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700324 LOOKUP(fp_enumerate, EnumerateGpus);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700325#undef LOOKUP
326
327 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
328 if (!new_node) {
329 loader_log(XGL_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
330 return;
331 }
332
333 new_node->handle = handle;
334 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700335 new_node->CreateInstance = fp_create_inst;
336 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700337 new_node->EnumerateGpus = fp_enumerate;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700338 new_node->next = loader.scanned_icd_list;
339 loader.scanned_icd_list = new_node;
340}
341
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700342
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600343/**
344 * Try to \c loader_icd_scan XGL driver(s).
345 *
346 * This function scans the default system path or path
347 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
348 * order to find loadable XGL ICDs with the name of libXGL_*.
349 *
350 * \returns
351 * void; but side effect is to set loader_icd_scanned to true
352 */
353static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800354{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600355 const char *libPaths, *p, *next;
356 DIR *sysdir;
357 struct dirent *dent;
358 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600359 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700360 uint32_t len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800361
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600362 libPaths = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700363#if !defined(WIN32)
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600364 if (geteuid() == getuid()) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700365 /* Don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
366#endif // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600367 libPaths = getenv("LIBXGL_DRIVERS_PATH");
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700368#if !defined(WIN32)
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600369 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700370#endif // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600371 if (libPaths == NULL)
372 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800373
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600374 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700375 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600376 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700377 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600378 next = p + len;
379 }
380 else {
Ian Elliott19628802015-02-04 12:06:46 -0700381 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600382 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
383 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600384 next++;
385 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800386
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700387 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
388 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600389 sysdir = opendir(p);
390 if (sysdir) {
391 dent = readdir(sysdir);
392 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700393 /* Look for ICDs starting with XGL_DRIVER_LIBRARY_PREFIX and
394 * ending with XGL_LIBRARY_SUFFIX
395 */
396 if (!strncmp(dent->d_name,
397 XGL_DRIVER_LIBRARY_PREFIX,
398 XGL_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700399 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700400 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
401 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
402 !strncmp(suf,
403 XGL_LIBRARY_SUFFIX,
404 XGL_LIBRARY_SUFFIX_LEN)) {
405 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
406 loader_scanned_icd_add(icd_library);
407 }
408 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600409
410 dent = readdir(sysdir);
411 }
412 closedir(sysdir);
413 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800414 }
415
Jon Ashburn46d1f582015-01-28 11:01:35 -0700416 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800417}
418
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600419
Jon Ashburn46888392015-01-29 15:45:51 -0700420static void layer_lib_scan_path(const char * libInPaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600421{
422 const char *p, *next;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700423 char *libPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600424 DIR *curdir;
425 struct dirent *dent;
Ian Elliott19628802015-02-04 12:06:46 -0700426 uint32_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600427 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600428
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700429 len = 0;
430 loader.layer_dirs = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600431 if (libInPaths){
Ian Elliott19628802015-02-04 12:06:46 -0700432 len = (uint32_t) strlen(libInPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700433 p = libInPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600434 }
435 else {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700436#if !defined(WIN32)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700437 if (geteuid() == getuid()) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700438#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700439 p = getenv("LIBXGL_LAYERS_PATH");
440 if (p != NULL)
Ian Elliott19628802015-02-04 12:06:46 -0700441 len = (uint32_t) strlen(p);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700442#if !defined(WIN32)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700443 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700444#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600445 }
446
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700447 if (len == 0) {
Ian Elliott19628802015-02-04 12:06:46 -0700448 len = (uint32_t) strlen(DEFAULT_XGL_LAYERS_PATH);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700449 p = DEFAULT_XGL_LAYERS_PATH;
450 }
451
452 if (len == 0) {
453 // Have no paths to search
454 return;
455 }
456 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700457 if (loader.layer_dirs == NULL)
458 return;
459
460 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
461 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700462 libPaths = loader.layer_dirs;
463
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600464 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600465 for (i = 0; i < loader.scanned_layer_count; i++) {
466 if (loader.scanned_layer_names[i] != NULL)
467 free(loader.scanned_layer_names[i]);
468 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600469 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600470 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600471
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600472 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700473 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600474 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700475 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600476 next = p + len;
477 }
478 else {
Ian Elliott19628802015-02-04 12:06:46 -0700479 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600480 *(char *) next = '\0';
481 next++;
482 }
483
484 curdir = opendir(p);
485 if (curdir) {
486 dent = readdir(curdir);
487 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700488 /* Look for layers starting with XGL_LAYER_LIBRARY_PREFIX and
489 * ending with XGL_LIBRARY_SUFFIX
490 */
491 if (!strncmp(dent->d_name,
492 XGL_LAYER_LIBRARY_PREFIX,
493 XGL_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700494 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700495 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
496 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
497 !strncmp(suf,
498 XGL_LIBRARY_SUFFIX,
499 XGL_LIBRARY_SUFFIX_LEN)) {
500 loader_platform_dl_handle handle;
501 snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
502 // Used to call: dlopen(temp_str, RTLD_LAZY)
503 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
504 dent = readdir(curdir);
505 continue;
506 }
507 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
508 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
509 break;
510 }
511 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
512 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
513 break;
514 }
515 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
516 loader.scanned_layer_count++;
517 loader_platform_close_library(handle);
518 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600519 }
520
521 dent = readdir(curdir);
522 }
523 closedir(curdir);
524 }
525 }
526
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600527 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600528}
529
Ian Elliott19628802015-02-04 12:06:46 -0700530static void layer_lib_scan(void)
Jon Ashburn46888392015-01-29 15:45:51 -0700531{
532 layer_lib_scan_path(NULL);
533}
534
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600535static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600536{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800537 loader_initialize_dispatch_table(tab, fpGPA, gpu);
538
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600539 if (tab->EnumerateLayers == NULL)
540 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600541}
542
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600543static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600544{
Jon Ashburn98bd4542015-01-29 16:44:24 -0700545 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
546 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
547 for (uint32_t i = 0; i < icd->gpu_count; i++)
548 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
549 gpu->baseObject) {
550 *gpu_index = i;
551 return icd;
552 }
553 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600554 }
555 return NULL;
556}
557
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600558static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600559{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600560 if (icd->layer_count[gpu_index])
561 return true;
562 else
563 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600564}
565
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600566static 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 -0600567{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600568 if (!icd)
569 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600570
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600571 struct loader_layers *obj;
572 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600573 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600574 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600575 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700576 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 -0600577 foundLib = true;
578 break;
579 }
580 }
581 if (!foundLib) {
582 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700583 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
584 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700585 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
586 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
587 loader_log(XGL_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600588 continue;
589 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700590 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 -0600591 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700592 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600593 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600594 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600595 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600596}
597
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600598static 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 -0700599{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700600 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600601 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600602 char layer_buf[16][256];
603 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700604
605 for (int i = 0; i < 16; i++)
606 layers[i] = &layer_buf[i][0];
607
608 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
609 *lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700610 // Used to call: dlopen(*lib_name, RTLD_LAZY)
611 if ((handle = loader_platform_open_library(*lib_name)) == NULL)
Jon Ashburnb8358052014-11-18 09:06:04 -0700612 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700613 if ((fpEnumerateLayers = (xglEnumerateLayersType) loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700614 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700615 //use default layer name
616 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name),
617 XGL_DRIVER_LIBRARY_PREFIX "%s" XGL_LIBRARY_SUFFIX,
618 layer_name);
619 loader_platform_close_library(handle);
620 if (!strcmp(*lib_name, lib_str)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700621 free(lib_str);
622 return true;
623 }
624 else {
625 free(lib_str);
626 continue;
627 }
628 }
629 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600630 size_t cnt;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700631 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (char *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700632 for (unsigned int i = 0; i < cnt; i++) {
633 if (!strcmp((char *) layers[i], layer_name)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700634 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700635 return true;
636 }
637 }
638 }
639
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700640 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700641 }
642
643 return false;
644}
645
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600646static 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 -0600647{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600648 const char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600649 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600650 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600651
Jon Ashburnb8358052014-11-18 09:06:04 -0700652 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn3fb55442014-10-23 10:29:09 -0600653 if (!layerEnv)
654 return 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600655 p = malloc(strlen(layerEnv) + 1);
656 if (!p)
657 return 0;
658 strcpy(p, layerEnv);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600659 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600660
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600661 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700662 const char *lib_name = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700663 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600664 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700665 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600666 next = p + len;
667 }
668 else {
Ian Elliott19628802015-02-04 12:06:46 -0700669 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600670 *(char *) next = '\0';
671 next++;
672 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600673 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700674 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600675 p = next;
676 continue;
677 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600678
Ian Elliott19628802015-02-04 12:06:46 -0700679 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700680 pLayerNames[count].layer_name = malloc(len + 1);
681 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600682 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600683 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600684 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700685 strncpy((char *) pLayerNames[count].layer_name, name, len);
686 pLayerNames[count].layer_name[len] = '\0';
687 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600688 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600689 p = next;
690
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600691 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600692
Jon Ashburnd09bd102014-10-22 21:15:26 -0600693 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600694 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600695}
696
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600697static 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 -0600698{
Jon Ashburnb8358052014-11-18 09:06:04 -0700699 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600700
701 *ppLayerNames = &layerNames[0];
702 if (!pCreateInfo) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700703 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600704 }
705
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800706 const XGL_LAYER_CREATE_INFO *pCi =
707 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600708
709 while (pCi) {
710 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
711 const char *name;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600712 uint32_t len;
713 for (uint32_t i = 0; i < pCi->layerCount; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700714 const char * lib_name = NULL;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800715 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnb8358052014-11-18 09:06:04 -0700716 if (!find_layer_name(icd, gpu_index, name, &lib_name))
717 return loader_get_layer_env(icd, gpu_index, layerNames);
Ian Elliott19628802015-02-04 12:06:46 -0700718 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700719 layerNames[i].layer_name = malloc(len + 1);
720 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600721 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700722 strncpy((char *) layerNames[i].layer_name, name, len);
723 layerNames[i].layer_name[len] = '\0';
724 layerNames[i].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600725 }
Courtney Goeltzenleuchter61e10b62015-01-07 10:17:44 -0700726 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600727 }
728 pCi = pCi->pNext;
729 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700730 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600731}
732
Jon Ashburn46d1f582015-01-28 11:01:35 -0700733static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600734{
735 struct loader_icd *icd;
736 struct loader_layers *libs;
737
Jon Ashburn46d1f582015-01-28 11:01:35 -0700738 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600739 if (icd->gpus)
740 free(icd->gpus);
741 icd->gpus = NULL;
742 if (icd->loader_dispatch)
743 free(icd->loader_dispatch);
744 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600745 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600746 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600747 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600748 libs = &(icd->layer_libs[j][i]);
749 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700750 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600751 libs->lib_handle = NULL;
752 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600753 if (icd->wrappedGpus[j])
754 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600755 }
756 icd->layer_count[j] = 0;
757 }
758 icd->gpu_count = 0;
759 }
760}
761
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600762extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600763{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600764 uint32_t gpu_index;
765 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700766 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600767 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600768
769 if (!icd)
770 return 0;
771 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
772
773 /* activate any layer libraries */
774 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600775 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
776 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600777 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600778
Jon Ashburnb8358052014-11-18 09:06:04 -0700779 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600780 if (!count)
781 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700782 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600783
Jon Ashburnd09bd102014-10-22 21:15:26 -0600784 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
785 if (! icd->wrappedGpus[gpu_index])
786 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600787 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600788 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600789 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600790 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600791 nextGpuObj->nextObject = gpuObj;
792 gpuObj = nextGpuObj;
793
Jon Ashburn79113cc2014-12-01 14:22:40 -0700794 char funcStr[256];
795 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700796 if ((nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
797 nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600798 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700799 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 -0600800 continue;
801 }
802
Jon Ashburnf2610012014-10-24 15:48:55 -0600803 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600804 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600805 //Insert the new wrapped objects into the list with loader object at head
806 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
807 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
808 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
809 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700810 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600811 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600812
813 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600814 }
815 else {
816 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700817 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600818 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700819 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600820 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
821 break;
822 }
823 }
824 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700825 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600826 }
827 }
828 return icd->layer_count[gpu_index];
829}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600830
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700831LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
832 const XGL_APPLICATION_INFO* pAppInfo,
833 const XGL_ALLOC_CALLBACKS* pAllocCb,
834 XGL_INSTANCE* pInstance)
835{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700836 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
837 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700838 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700839 struct loader_scanned_icds *scanned_icds;
840 struct loader_icd *icd;
841 XGL_RESULT res;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600842
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700843 /* Scan/discover all ICD libraries in a single-threaded manner */
844 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700845
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700846 /* get layer libraries in a single-threaded manner */
847 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700848
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700849 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
850 if (ptr_instance == NULL) {
851 return XGL_ERROR_OUT_OF_MEMORY;
852 }
853 memset(ptr_instance, 0, sizeof(struct loader_instance));
854
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700855 ptr_instance->next = loader.instances;
856 loader.instances = ptr_instance;
857
Jon Ashburn46888392015-01-29 15:45:51 -0700858 scanned_icds = loader.scanned_icd_list;
859 while (scanned_icds) {
860 icd = loader_icd_add(ptr_instance, scanned_icds);
861 if (icd) {
862 res = scanned_icds->CreateInstance(pAppInfo, pAllocCb,
863 &(scanned_icds->instance));
864 if (res != XGL_SUCCESS)
865 {
866 ptr_instance->icds = ptr_instance->icds->next;
867 loader_icd_destroy(icd);
868 scanned_icds->instance = NULL;
869 loader_log(XGL_DBG_MSG_WARNING, 0,
870 "ICD ignored: failed to CreateInstance on device");
871 }
872 }
873 scanned_icds = scanned_icds->next;
874 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700875
Ian Elliotteb450762015-02-05 15:19:15 -0700876 if (ptr_instance->icds == NULL) {
877 return XGL_ERROR_INCOMPATIBLE_DRIVER;
878 }
Jon Ashburn46888392015-01-29 15:45:51 -0700879
880 *pInstance = (XGL_INSTANCE) ptr_instance;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700881 return XGL_SUCCESS;
882}
883
884LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
885 XGL_INSTANCE instance)
886{
887 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -0700888 struct loader_scanned_icds *scanned_icds;
889 XGL_RESULT res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700890
891 // Remove this instance from the list of instances:
892 struct loader_instance *prev = NULL;
893 struct loader_instance *next = loader.instances;
894 while (next != NULL) {
895 if (next == ptr_instance) {
896 // Remove this instance from the list:
897 if (prev)
898 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -0700899 else
900 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700901 break;
902 }
903 prev = next;
904 next = next->next;
905 }
906 if (next == NULL) {
907 // This must be an invalid instance handle or empty list
908 return XGL_ERROR_INVALID_HANDLE;
909 }
910
Jon Ashburn46888392015-01-29 15:45:51 -0700911 // cleanup any prior layer initializations
912 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700913
Jon Ashburn46888392015-01-29 15:45:51 -0700914 scanned_icds = loader.scanned_icd_list;
915 while (scanned_icds) {
916 if (scanned_icds->instance)
917 res = scanned_icds->DestroyInstance(scanned_icds->instance);
918 if (res != XGL_SUCCESS)
919 loader_log(XGL_DBG_MSG_WARNING, 0,
920 "ICD ignored: failed to DestroyInstance on device");
921 scanned_icds->instance = NULL;
922 scanned_icds = scanned_icds->next;
923 }
924
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700925 free(ptr_instance);
926
927 return XGL_SUCCESS;
928}
929
930LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
931
932 XGL_INSTANCE instance,
933 uint32_t maxGpus,
934 uint32_t* pGpuCount,
935 XGL_PHYSICAL_GPU* pGpus)
936{
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700937 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
938 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700939 uint32_t count = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700940 XGL_RESULT res;
941
942 //in spirit of XGL don't error check on the instance parameter
943 icd = ptr_instance->icds;
944 while (icd) {
945 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
946 XGL_BASE_LAYER_OBJECT * wrapped_gpus;
947 xglGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700948 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700949
950 if (max > XGL_MAX_PHYSICAL_GPUS) {
951 max = XGL_MAX_PHYSICAL_GPUS;
952 }
953
Jon Ashburn46888392015-01-29 15:45:51 -0700954 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
955 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700956 gpus);
957 if (res == XGL_SUCCESS && n) {
958 wrapped_gpus = (XGL_BASE_LAYER_OBJECT*) malloc(n *
959 sizeof(XGL_BASE_LAYER_OBJECT));
960 icd->gpus = wrapped_gpus;
961 icd->gpu_count = n;
962 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n *
963 sizeof(XGL_LAYER_DISPATCH_TABLE));
Ian Elliott19628802015-02-04 12:06:46 -0700964 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700965 (wrapped_gpus + i)->baseObject = gpus[i];
966 (wrapped_gpus + i)->pGPA = get_proc_addr;
967 (wrapped_gpus + i)->nextObject = gpus[i];
968 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
969 loader_init_dispatch_table(icd->loader_dispatch + i,
970 get_proc_addr, gpus[i]);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -0700971
972 /* Verify ICD compatibility */
973 if (!valid_loader_magic_value(gpus[i])) {
974 loader_log(XGL_DBG_MSG_WARNING, 0,
975 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
976 assert(0);
977 }
978
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700979 const XGL_LAYER_DISPATCH_TABLE **disp;
980 disp = (const XGL_LAYER_DISPATCH_TABLE **) gpus[i];
981 *disp = icd->loader_dispatch + i;
982 }
983
984 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
985 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
986 loader_log(XGL_DBG_MSG_WARNING, 0,
987 "ICD ignored: failed to migrate settings");
988 loader_icd_destroy(icd);
989 }
990 count += n;
991
992 if (count >= maxGpus) {
993 break;
994 }
995 }
996
997 icd = icd->next;
998 }
999
1000 /* we have nothing to log anymore */
1001 loader_msg_callback_clear();
1002
1003 *pGpuCount = count;
1004
1005 return (count > 0) ? XGL_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001006}
1007
1008LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
1009{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001010 if (gpu == NULL) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001011 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001012 }
Jon Ashburn891537f2014-10-22 12:42:13 -06001013 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -06001014 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001015 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001016
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001017 if (disp_table == NULL)
1018 return NULL;
1019
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001020 addr = loader_lookup_dispatch_table(disp_table, pName);
1021 if (addr)
1022 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001023 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001024 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001025 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -06001026 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001027 }
1028}
1029
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001030LOADER_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 -06001031{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001032 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001033 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001034 char *lib_name;
1035 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001036 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -06001037 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001038 char layer_buf[16][256];
1039 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001040
Jon Ashburn1323eb72014-12-02 13:03:09 -07001041 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001042 return XGL_ERROR_INVALID_POINTER;
1043
Jon Ashburn46d1f582015-01-28 11:01:35 -07001044 if (!icd)
1045 return XGL_ERROR_UNAVAILABLE;
1046
Jon Ashburn1323eb72014-12-02 13:03:09 -07001047 for (int i = 0; i < 16; i++)
1048 layers[i] = &layer_buf[i][0];
1049
1050 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1051 lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001052 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1053 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001054 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001055 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
1056 //use default layer name based on library name XGL_LAYER_LIBRARY_PREFIX<name>.XGL_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001057 char *pEnd, *cpyStr;
1058 int siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001059 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001060 lib_name = basename(lib_name);
1061 pEnd = strrchr(lib_name, '.');
Ian Elliott19628802015-02-04 12:06:46 -07001062 siz = (int) (pEnd - lib_name - strlen(XGL_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001063 if (pEnd == NULL || siz <= 0)
1064 continue;
1065 cpyStr = malloc(siz);
1066 if (cpyStr == NULL) {
1067 free(cpyStr);
1068 continue;
1069 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001070 strncpy(cpyStr, lib_name + strlen(XGL_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001071 cpyStr[siz - 1] = '\0';
1072 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001073 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001074 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1075 pOutLayers[count][siz - 1] = '\0';
1076 count++;
1077 free(cpyStr);
1078 }
1079 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001080 size_t cnt;
1081 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001082 XGL_RESULT res;
Ian Elliott19628802015-02-04 12:06:46 -07001083 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001084 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1085 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001086 if (res != XGL_SUCCESS)
1087 continue;
1088 if (cnt + count > maxLayerCount)
1089 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001090 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001091 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1092 if (n > 0)
1093 pOutLayers[i - count][n - 1] = '\0';
1094 }
1095 count += cnt;
1096 }
1097 }
1098
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001099 *pOutLayerCount = count;
1100
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001101 return XGL_SUCCESS;
1102}
1103
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001104LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001105{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001106 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001107 struct loader_instance *inst;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001108 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001109 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001110
Jon Ashburn46d1f582015-01-28 11:01:35 -07001111 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001112 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1113 }
1114
Jon Ashburn98bd4542015-01-29 16:44:24 -07001115 for (inst = loader.instances; inst; inst = inst->next) {
1116 for (icd = inst->icds; icd; icd = icd->next) {
1117 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1118 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(
1119 pfnMsgCallback, pUserData);
1120 if (res != XGL_SUCCESS) {
1121 gpu_idx = i;
1122 break;
1123 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001124 }
Jon Ashburn98bd4542015-01-29 16:44:24 -07001125 if (res != XGL_SUCCESS)
1126 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001127 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001128 if (res != XGL_SUCCESS)
1129 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001130 }
1131
1132 /* roll back on errors */
1133 if (icd) {
Jon Ashburn98bd4542015-01-29 16:44:24 -07001134 for (struct loader_instance *tmp_inst = loader.instances;
1135 tmp_inst != inst; tmp_inst = tmp_inst->next) {
1136 for (const struct loader_icd * tmp = tmp_inst->icds; tmp != icd;
1137 tmp = tmp->next) {
1138 for (uint32_t i = 0; i < icd->gpu_count; i++)
1139 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1140 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001141 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001142 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001143 for (uint32_t i = 0; i < gpu_idx; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001144 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001145
1146 return res;
1147 }
1148
1149 return XGL_SUCCESS;
1150}
1151
Chia-I Wu19300602014-08-04 08:03:57 +08001152LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001153{
Jon Ashburn98bd4542015-01-29 16:44:24 -07001154 XGL_RESULT res = XGL_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001155
Jon Ashburn46d1f582015-01-28 11:01:35 -07001156 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001157 return loader_msg_callback_remove(pfnMsgCallback);
1158 }
1159
Jon Ashburn98bd4542015-01-29 16:44:24 -07001160 for (struct loader_instance *inst = loader.instances; inst;
1161 inst = inst->next) {
1162 for (const struct loader_icd * icd = inst->icds; icd;
1163 icd = icd->next) {
1164 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1165 XGL_RESULT r;
1166 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1167 if (r != XGL_SUCCESS) {
1168 res = r;
1169 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001170 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001171 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001172 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001173 return res;
1174}
1175
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001176LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001177{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001178 XGL_RESULT res = XGL_SUCCESS;
1179
Jon Ashburn46d1f582015-01-28 11:01:35 -07001180 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001181 if (dataSize == 0)
1182 return XGL_ERROR_INVALID_VALUE;
1183
1184 switch (dbgOption) {
1185 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1186 loader.debug_echo_enable = *((const bool *) pData);
1187 break;
1188 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1189 loader.break_on_error = *((const bool *) pData);
1190 break;
1191 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1192 loader.break_on_warning = *((const bool *) pData);
1193 break;
1194 default:
1195 res = XGL_ERROR_INVALID_VALUE;
1196 break;
1197 }
1198
1199 return res;
1200 }
1201
Jon Ashburn98bd4542015-01-29 16:44:24 -07001202 for (struct loader_instance *inst = loader.instances; inst;
1203 inst = inst->next) {
1204 for (const struct loader_icd * icd = inst->icds; icd;
1205 icd = icd->next) {
1206 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1207 XGL_RESULT r;
1208 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption,
1209 dataSize, pData);
1210 /* unfortunately we cannot roll back */
1211 if (r != XGL_SUCCESS) {
1212 res = r;
1213 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001214 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001215 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001216 }
1217
1218 return res;
1219}