blob: 34df4a5e9cb96dc527a93eab07a16cfd414924b4 [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"
Ian Elliott655cad72015-02-12 17:08:34 -070046// The following is #included again to catch certain OS-specific functions
47// being used:
48#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080049
Jon Ashburn1beab2d2015-01-26 14:51:40 -070050struct loader_instance {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070051 struct loader_icd *icds;
52 struct loader_instance *next;
53};
54
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060055struct loader_layers {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070056 loader_platform_dl_handle lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070057 char name[256];
58};
59
60struct layer_name_pair {
61 char *layer_name;
62 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060063};
64
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080065struct loader_icd {
Jon Ashburn46d1f582015-01-28 11:01:35 -070066 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080067
Jon Ashburn876b1ac2014-10-17 15:09:07 -060068 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060069 uint32_t layer_count[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060070 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnd09bd102014-10-22 21:15:26 -060071 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060072 uint32_t gpu_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060073 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060074
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080075 struct loader_icd *next;
76};
77
Jon Ashburnd38bfb12014-10-14 19:15:22 -060078
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080079struct loader_msg_callback {
80 XGL_DBG_MSG_CALLBACK_FUNCTION func;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060081 void *data;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080082
83 struct loader_msg_callback *next;
84};
85
Jon Ashburn46d1f582015-01-28 11:01:35 -070086struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070087 loader_platform_dl_handle handle;
Jon Ashburn46d1f582015-01-28 11:01:35 -070088 xglGetProcAddrType GetProcAddr;
Jon Ashburn46888392015-01-29 15:45:51 -070089 xglCreateInstanceType CreateInstance;
90 xglDestroyInstanceType DestroyInstance;
Jon Ashburn4c392fb2015-01-28 19:57:09 -070091 xglEnumerateGpusType EnumerateGpus;
Jon Ashburn46888392015-01-29 15:45:51 -070092 XGL_INSTANCE instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070093 struct loader_scanned_icds *next;
94};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060095
Jon Ashburn1beab2d2015-01-26 14:51:40 -070096// Note: Since the following is a static structure, all members are initialized
97// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080098static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070099 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700100 bool icds_scanned;
101 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600102 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700103 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600104 unsigned int scanned_layer_count;
105 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800106 struct loader_msg_callback *msg_callbacks;
107
108 bool debug_echo_enable;
109 bool break_on_error;
110 bool break_on_warning;
111} loader;
112
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700113
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800114static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600115 void *data)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800116{
117 struct loader_msg_callback *cb;
118
119 cb = malloc(sizeof(*cb));
120 if (!cb)
121 return XGL_ERROR_OUT_OF_MEMORY;
122
123 cb->func = func;
124 cb->data = data;
125
126 cb->next = loader.msg_callbacks;
127 loader.msg_callbacks = cb;
128
129 return XGL_SUCCESS;
130}
131
132static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
133{
134 struct loader_msg_callback *cb = loader.msg_callbacks;
135
136 /*
137 * Find the first match (last registered).
138 *
139 * XXX What if the same callback function is registered more than once?
140 */
141 while (cb) {
142 if (cb->func == func) {
143 break;
144 }
145
146 cb = cb->next;
147 }
148
149 if (!cb)
150 return XGL_ERROR_INVALID_POINTER;
151
152 free(cb);
153
154 return XGL_SUCCESS;
155}
156
157static void loader_msg_callback_clear(void)
158{
159 struct loader_msg_callback *cb = loader.msg_callbacks;
160
161 while (cb) {
162 struct loader_msg_callback *next = cb->next;
163 free(cb);
164 cb = next;
165 }
166
167 loader.msg_callbacks = NULL;
168}
169
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600170static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800171 const char *format, ...)
172{
173 const struct loader_msg_callback *cb = loader.msg_callbacks;
174 char msg[256];
175 va_list ap;
176 int ret;
177
178 va_start(ap, format);
179 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700180 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800181 msg[sizeof(msg) - 1] = '\0';
182 }
183 va_end(ap);
184
185 if (loader.debug_echo_enable || !cb) {
186 fputs(msg, stderr);
187 fputc('\n', stderr);
188 }
189
190 while (cb) {
191 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800192 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800193 cb = cb->next;
194 }
195
196 switch (msg_type) {
197 case XGL_DBG_MSG_ERROR:
198 if (loader.break_on_error) {
199 exit(1);
200 }
201 /* fall through */
202 case XGL_DBG_MSG_WARNING:
203 if (loader.break_on_warning) {
204 exit(1);
205 }
206 break;
207 default:
208 break;
209 }
210}
211
212static void
213loader_icd_destroy(struct loader_icd *icd)
214{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700215 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800216 free(icd);
217}
218
219static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700220loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800221{
222 struct loader_icd *icd;
223
224 icd = malloc(sizeof(*icd));
225 if (!icd)
226 return NULL;
227
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600228 memset(icd, 0, sizeof(*icd));
229
Jon Ashburn46d1f582015-01-28 11:01:35 -0700230 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800231
232 return icd;
233}
234
235static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
236{
237 const struct loader_msg_callback *cb = loader.msg_callbacks;
238 XGL_RESULT res;
239
240 while (cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600241 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700242 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
243 if (res != XGL_SUCCESS) {
244 break;
245 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800246 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800247 cb = cb->next;
248 }
249
250 /* roll back on errors */
251 if (cb) {
252 const struct loader_msg_callback *tmp = loader.msg_callbacks;
253
254 while (tmp != cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600255 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700256 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
257 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800258 tmp = tmp->next;
259 }
260
261 return res;
262 }
263
264 return XGL_SUCCESS;
265}
266
267static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
268{
269#define SETB(icd, opt, val) do { \
270 if (val) { \
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600271 for (uint32_t i = 0; i < icd->gpu_count; i++) { \
Jon Ashburn01e2d662014-11-14 09:52:42 -0700272 const XGL_RESULT res = \
273 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
274 if (res != XGL_SUCCESS) \
275 return res; \
276 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800277 } \
278} while (0)
279 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
280 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
281 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
282#undef SETB
283
284return XGL_SUCCESS;
285}
286
Jon Ashburn46888392015-01-29 15:45:51 -0700287static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
288 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800289{
290 struct loader_icd *icd;
291
Jon Ashburn46d1f582015-01-28 11:01:35 -0700292 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800293 if (!icd)
294 return NULL;
295
Chia-I Wu13a61a52014-08-04 11:18:20 +0800296 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700297 icd->next = ptr_inst->icds;
298 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800299
300 return icd;
301}
302
Jon Ashburn46d1f582015-01-28 11:01:35 -0700303static void loader_scanned_icd_add(const char *filename)
304{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700305 loader_platform_dl_handle handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700306 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700307 struct loader_scanned_icds *new_node;
308
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700309 // Used to call: dlopen(filename, RTLD_LAZY);
310 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700311 if (!handle) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700312 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700313 return;
314 }
315
316#define LOOKUP(func_ptr, func) do { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700317 func_ptr = (xgl ##func## Type) loader_platform_get_proc_address(handle, "xgl" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700318 if (!func_ptr) { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700319 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("xgl" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700320 return; \
321 } \
322} while (0)
323
324 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700325 LOOKUP(fp_create_inst, CreateInstance);
326 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700327 LOOKUP(fp_enumerate, EnumerateGpus);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700328#undef LOOKUP
329
330 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
331 if (!new_node) {
332 loader_log(XGL_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
333 return;
334 }
335
336 new_node->handle = handle;
337 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700338 new_node->CreateInstance = fp_create_inst;
339 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700340 new_node->EnumerateGpus = fp_enumerate;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700341 new_node->next = loader.scanned_icd_list;
342 loader.scanned_icd_list = new_node;
343}
344
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700345
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600346/**
347 * Try to \c loader_icd_scan XGL driver(s).
348 *
349 * This function scans the default system path or path
350 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
351 * order to find loadable XGL ICDs with the name of libXGL_*.
352 *
353 * \returns
354 * void; but side effect is to set loader_icd_scanned to true
355 */
356static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800357{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600358 const char *libPaths, *p, *next;
359 DIR *sysdir;
360 struct dirent *dent;
361 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600362 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700363 uint32_t len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800364
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600365 libPaths = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700366#if !defined(WIN32)
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600367 if (geteuid() == getuid()) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700368 /* Don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
369#endif // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600370 libPaths = getenv("LIBXGL_DRIVERS_PATH");
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700371#if !defined(WIN32)
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600372 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700373#endif // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600374 if (libPaths == NULL)
375 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800376
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600377 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700378 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600379 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700380 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600381 next = p + len;
382 }
383 else {
Ian Elliott19628802015-02-04 12:06:46 -0700384 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600385 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
386 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600387 next++;
388 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800389
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700390 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
391 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600392 sysdir = opendir(p);
393 if (sysdir) {
394 dent = readdir(sysdir);
395 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700396 /* Look for ICDs starting with XGL_DRIVER_LIBRARY_PREFIX and
397 * ending with XGL_LIBRARY_SUFFIX
398 */
399 if (!strncmp(dent->d_name,
400 XGL_DRIVER_LIBRARY_PREFIX,
401 XGL_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700402 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700403 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
404 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
405 !strncmp(suf,
406 XGL_LIBRARY_SUFFIX,
407 XGL_LIBRARY_SUFFIX_LEN)) {
408 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
409 loader_scanned_icd_add(icd_library);
410 }
411 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600412
413 dent = readdir(sysdir);
414 }
415 closedir(sysdir);
416 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800417 }
418
Jon Ashburn46d1f582015-01-28 11:01:35 -0700419 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800420}
421
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600422
Jon Ashburn46888392015-01-29 15:45:51 -0700423static void layer_lib_scan_path(const char * libInPaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600424{
425 const char *p, *next;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700426 char *libPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600427 DIR *curdir;
428 struct dirent *dent;
Ian Elliott19628802015-02-04 12:06:46 -0700429 uint32_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600430 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600431
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700432 len = 0;
433 loader.layer_dirs = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600434 if (libInPaths){
Ian Elliott19628802015-02-04 12:06:46 -0700435 len = (uint32_t) strlen(libInPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700436 p = libInPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600437 }
438 else {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700439#if !defined(WIN32)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700440 if (geteuid() == getuid()) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700441#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700442 p = getenv("LIBXGL_LAYERS_PATH");
443 if (p != NULL)
Ian Elliott19628802015-02-04 12:06:46 -0700444 len = (uint32_t) strlen(p);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700445#if !defined(WIN32)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700446 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700447#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600448 }
449
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700450 if (len == 0) {
Ian Elliott19628802015-02-04 12:06:46 -0700451 len = (uint32_t) strlen(DEFAULT_XGL_LAYERS_PATH);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700452 p = DEFAULT_XGL_LAYERS_PATH;
453 }
454
455 if (len == 0) {
456 // Have no paths to search
457 return;
458 }
459 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700460 if (loader.layer_dirs == NULL)
461 return;
462
463 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
464 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700465 libPaths = loader.layer_dirs;
466
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600467 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600468 for (i = 0; i < loader.scanned_layer_count; i++) {
469 if (loader.scanned_layer_names[i] != NULL)
470 free(loader.scanned_layer_names[i]);
471 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600472 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600473 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600474
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600475 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700476 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600477 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700478 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600479 next = p + len;
480 }
481 else {
Ian Elliott19628802015-02-04 12:06:46 -0700482 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600483 *(char *) next = '\0';
484 next++;
485 }
486
487 curdir = opendir(p);
488 if (curdir) {
489 dent = readdir(curdir);
490 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700491 /* Look for layers starting with XGL_LAYER_LIBRARY_PREFIX and
492 * ending with XGL_LIBRARY_SUFFIX
493 */
494 if (!strncmp(dent->d_name,
495 XGL_LAYER_LIBRARY_PREFIX,
496 XGL_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700497 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700498 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
499 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
500 !strncmp(suf,
501 XGL_LIBRARY_SUFFIX,
502 XGL_LIBRARY_SUFFIX_LEN)) {
503 loader_platform_dl_handle handle;
504 snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
505 // Used to call: dlopen(temp_str, RTLD_LAZY)
506 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
507 dent = readdir(curdir);
508 continue;
509 }
510 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
511 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
512 break;
513 }
514 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
515 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
516 break;
517 }
518 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
519 loader.scanned_layer_count++;
520 loader_platform_close_library(handle);
521 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600522 }
523
524 dent = readdir(curdir);
525 }
526 closedir(curdir);
527 }
528 }
529
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600530 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600531}
532
Ian Elliott19628802015-02-04 12:06:46 -0700533static void layer_lib_scan(void)
Jon Ashburn46888392015-01-29 15:45:51 -0700534{
535 layer_lib_scan_path(NULL);
536}
537
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600538static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600539{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800540 loader_initialize_dispatch_table(tab, fpGPA, gpu);
541
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600542 if (tab->EnumerateLayers == NULL)
543 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600544}
545
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600546static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600547{
Jon Ashburn98bd4542015-01-29 16:44:24 -0700548 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
549 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
550 for (uint32_t i = 0; i < icd->gpu_count; i++)
551 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
552 gpu->baseObject) {
553 *gpu_index = i;
554 return icd;
555 }
556 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600557 }
558 return NULL;
559}
560
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600561static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600562{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600563 if (icd->layer_count[gpu_index])
564 return true;
565 else
566 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600567}
568
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600569static 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 -0600570{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600571 if (!icd)
572 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600573
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600574 struct loader_layers *obj;
575 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600576 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600577 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600578 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700579 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 -0600580 foundLib = true;
581 break;
582 }
583 }
584 if (!foundLib) {
585 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700586 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
587 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700588 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
589 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
590 loader_log(XGL_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600591 continue;
592 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700593 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 -0600594 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700595 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600596 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600597 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600598 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600599}
600
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600601static 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 -0700602{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700603 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600604 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600605 char layer_buf[16][256];
606 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700607
608 for (int i = 0; i < 16; i++)
609 layers[i] = &layer_buf[i][0];
610
611 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
612 *lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700613 // Used to call: dlopen(*lib_name, RTLD_LAZY)
614 if ((handle = loader_platform_open_library(*lib_name)) == NULL)
Jon Ashburnb8358052014-11-18 09:06:04 -0700615 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700616 if ((fpEnumerateLayers = (xglEnumerateLayersType) loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700617 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700618 //use default layer name
619 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name),
620 XGL_DRIVER_LIBRARY_PREFIX "%s" XGL_LIBRARY_SUFFIX,
621 layer_name);
622 loader_platform_close_library(handle);
623 if (!strcmp(*lib_name, lib_str)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700624 free(lib_str);
625 return true;
626 }
627 else {
628 free(lib_str);
629 continue;
630 }
631 }
632 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600633 size_t cnt;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700634 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (char *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700635 for (unsigned int i = 0; i < cnt; i++) {
636 if (!strcmp((char *) layers[i], layer_name)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700637 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700638 return true;
639 }
640 }
641 }
642
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700643 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700644 }
645
646 return false;
647}
648
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600649static 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 -0600650{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600651 const char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600652 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600653 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600654
Jon Ashburnb8358052014-11-18 09:06:04 -0700655 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn3fb55442014-10-23 10:29:09 -0600656 if (!layerEnv)
657 return 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600658 p = malloc(strlen(layerEnv) + 1);
659 if (!p)
660 return 0;
661 strcpy(p, layerEnv);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600662 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600663
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600664 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700665 const char *lib_name = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700666 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600667 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700668 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600669 next = p + len;
670 }
671 else {
Ian Elliott19628802015-02-04 12:06:46 -0700672 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600673 *(char *) next = '\0';
674 next++;
675 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600676 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700677 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600678 p = next;
679 continue;
680 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600681
Ian Elliott19628802015-02-04 12:06:46 -0700682 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700683 pLayerNames[count].layer_name = malloc(len + 1);
684 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600685 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600686 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600687 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700688 strncpy((char *) pLayerNames[count].layer_name, name, len);
689 pLayerNames[count].layer_name[len] = '\0';
690 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600691 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600692 p = next;
693
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600694 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600695
Jon Ashburnd09bd102014-10-22 21:15:26 -0600696 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600697 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600698}
699
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600700static 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 -0600701{
Jon Ashburnb8358052014-11-18 09:06:04 -0700702 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600703
704 *ppLayerNames = &layerNames[0];
705 if (!pCreateInfo) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700706 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600707 }
708
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800709 const XGL_LAYER_CREATE_INFO *pCi =
710 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600711
712 while (pCi) {
713 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
714 const char *name;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600715 uint32_t len;
716 for (uint32_t i = 0; i < pCi->layerCount; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700717 const char * lib_name = NULL;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800718 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnb8358052014-11-18 09:06:04 -0700719 if (!find_layer_name(icd, gpu_index, name, &lib_name))
720 return loader_get_layer_env(icd, gpu_index, layerNames);
Ian Elliott19628802015-02-04 12:06:46 -0700721 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700722 layerNames[i].layer_name = malloc(len + 1);
723 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600724 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700725 strncpy((char *) layerNames[i].layer_name, name, len);
726 layerNames[i].layer_name[len] = '\0';
727 layerNames[i].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600728 }
Courtney Goeltzenleuchter61e10b62015-01-07 10:17:44 -0700729 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600730 }
731 pCi = pCi->pNext;
732 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700733 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600734}
735
Jon Ashburn46d1f582015-01-28 11:01:35 -0700736static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600737{
738 struct loader_icd *icd;
739 struct loader_layers *libs;
740
Jon Ashburn46d1f582015-01-28 11:01:35 -0700741 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600742 if (icd->gpus)
743 free(icd->gpus);
744 icd->gpus = NULL;
745 if (icd->loader_dispatch)
746 free(icd->loader_dispatch);
747 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600748 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600749 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600750 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600751 libs = &(icd->layer_libs[j][i]);
752 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700753 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600754 libs->lib_handle = NULL;
755 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600756 if (icd->wrappedGpus[j])
757 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600758 }
759 icd->layer_count[j] = 0;
760 }
761 icd->gpu_count = 0;
762 }
763}
764
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600765extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600766{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600767 uint32_t gpu_index;
768 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700769 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600770 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600771
772 if (!icd)
773 return 0;
774 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
775
776 /* activate any layer libraries */
777 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600778 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
779 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600780 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600781
Jon Ashburnb8358052014-11-18 09:06:04 -0700782 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600783 if (!count)
784 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700785 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600786
Jon Ashburnd09bd102014-10-22 21:15:26 -0600787 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
788 if (! icd->wrappedGpus[gpu_index])
789 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600790 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600791 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600792 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600793 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600794 nextGpuObj->nextObject = gpuObj;
795 gpuObj = nextGpuObj;
796
Jon Ashburn79113cc2014-12-01 14:22:40 -0700797 char funcStr[256];
798 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700799 if ((nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
800 nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600801 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700802 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 -0600803 continue;
804 }
805
Jon Ashburnf2610012014-10-24 15:48:55 -0600806 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600807 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600808 //Insert the new wrapped objects into the list with loader object at head
809 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
810 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
811 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
812 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700813 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600814 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600815
816 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600817 }
818 else {
819 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700820 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600821 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700822 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600823 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
824 break;
825 }
826 }
827 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700828 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600829 }
830 }
831 return icd->layer_count[gpu_index];
832}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600833
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700834LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
835 const XGL_APPLICATION_INFO* pAppInfo,
836 const XGL_ALLOC_CALLBACKS* pAllocCb,
837 XGL_INSTANCE* pInstance)
838{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700839 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
840 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700841 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700842 struct loader_scanned_icds *scanned_icds;
843 struct loader_icd *icd;
844 XGL_RESULT res;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600845
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700846 /* Scan/discover all ICD libraries in a single-threaded manner */
847 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700848
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700849 /* get layer libraries in a single-threaded manner */
850 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700851
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700852 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
853 if (ptr_instance == NULL) {
854 return XGL_ERROR_OUT_OF_MEMORY;
855 }
856 memset(ptr_instance, 0, sizeof(struct loader_instance));
857
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700858 ptr_instance->next = loader.instances;
859 loader.instances = ptr_instance;
860
Jon Ashburn46888392015-01-29 15:45:51 -0700861 scanned_icds = loader.scanned_icd_list;
862 while (scanned_icds) {
863 icd = loader_icd_add(ptr_instance, scanned_icds);
864 if (icd) {
865 res = scanned_icds->CreateInstance(pAppInfo, pAllocCb,
866 &(scanned_icds->instance));
867 if (res != XGL_SUCCESS)
868 {
869 ptr_instance->icds = ptr_instance->icds->next;
870 loader_icd_destroy(icd);
871 scanned_icds->instance = NULL;
872 loader_log(XGL_DBG_MSG_WARNING, 0,
873 "ICD ignored: failed to CreateInstance on device");
874 }
875 }
876 scanned_icds = scanned_icds->next;
877 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700878
Ian Elliotteb450762015-02-05 15:19:15 -0700879 if (ptr_instance->icds == NULL) {
880 return XGL_ERROR_INCOMPATIBLE_DRIVER;
881 }
Jon Ashburn46888392015-01-29 15:45:51 -0700882
883 *pInstance = (XGL_INSTANCE) ptr_instance;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700884 return XGL_SUCCESS;
885}
886
887LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
888 XGL_INSTANCE instance)
889{
890 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -0700891 struct loader_scanned_icds *scanned_icds;
892 XGL_RESULT res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700893
894 // Remove this instance from the list of instances:
895 struct loader_instance *prev = NULL;
896 struct loader_instance *next = loader.instances;
897 while (next != NULL) {
898 if (next == ptr_instance) {
899 // Remove this instance from the list:
900 if (prev)
901 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -0700902 else
903 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700904 break;
905 }
906 prev = next;
907 next = next->next;
908 }
909 if (next == NULL) {
910 // This must be an invalid instance handle or empty list
911 return XGL_ERROR_INVALID_HANDLE;
912 }
913
Jon Ashburn46888392015-01-29 15:45:51 -0700914 // cleanup any prior layer initializations
915 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700916
Jon Ashburn46888392015-01-29 15:45:51 -0700917 scanned_icds = loader.scanned_icd_list;
918 while (scanned_icds) {
919 if (scanned_icds->instance)
920 res = scanned_icds->DestroyInstance(scanned_icds->instance);
921 if (res != XGL_SUCCESS)
922 loader_log(XGL_DBG_MSG_WARNING, 0,
923 "ICD ignored: failed to DestroyInstance on device");
924 scanned_icds->instance = NULL;
925 scanned_icds = scanned_icds->next;
926 }
927
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700928 free(ptr_instance);
929
930 return XGL_SUCCESS;
931}
932
933LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
934
935 XGL_INSTANCE instance,
936 uint32_t maxGpus,
937 uint32_t* pGpuCount,
938 XGL_PHYSICAL_GPU* pGpus)
939{
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700940 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
941 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700942 uint32_t count = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700943 XGL_RESULT res;
944
945 //in spirit of XGL don't error check on the instance parameter
946 icd = ptr_instance->icds;
947 while (icd) {
948 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
949 XGL_BASE_LAYER_OBJECT * wrapped_gpus;
950 xglGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700951 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700952
953 if (max > XGL_MAX_PHYSICAL_GPUS) {
954 max = XGL_MAX_PHYSICAL_GPUS;
955 }
956
Jon Ashburn46888392015-01-29 15:45:51 -0700957 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
958 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700959 gpus);
960 if (res == XGL_SUCCESS && n) {
961 wrapped_gpus = (XGL_BASE_LAYER_OBJECT*) malloc(n *
962 sizeof(XGL_BASE_LAYER_OBJECT));
963 icd->gpus = wrapped_gpus;
964 icd->gpu_count = n;
965 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n *
966 sizeof(XGL_LAYER_DISPATCH_TABLE));
Ian Elliott19628802015-02-04 12:06:46 -0700967 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700968 (wrapped_gpus + i)->baseObject = gpus[i];
969 (wrapped_gpus + i)->pGPA = get_proc_addr;
970 (wrapped_gpus + i)->nextObject = gpus[i];
971 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
972 loader_init_dispatch_table(icd->loader_dispatch + i,
973 get_proc_addr, gpus[i]);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -0700974
975 /* Verify ICD compatibility */
976 if (!valid_loader_magic_value(gpus[i])) {
977 loader_log(XGL_DBG_MSG_WARNING, 0,
978 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
979 assert(0);
980 }
981
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700982 const XGL_LAYER_DISPATCH_TABLE **disp;
983 disp = (const XGL_LAYER_DISPATCH_TABLE **) gpus[i];
984 *disp = icd->loader_dispatch + i;
985 }
986
987 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
988 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
989 loader_log(XGL_DBG_MSG_WARNING, 0,
990 "ICD ignored: failed to migrate settings");
991 loader_icd_destroy(icd);
992 }
993 count += n;
994
995 if (count >= maxGpus) {
996 break;
997 }
998 }
999
1000 icd = icd->next;
1001 }
1002
1003 /* we have nothing to log anymore */
1004 loader_msg_callback_clear();
1005
1006 *pGpuCount = count;
1007
1008 return (count > 0) ? XGL_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001009}
1010
1011LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
1012{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001013 if (gpu == NULL) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001014 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001015 }
Jon Ashburn891537f2014-10-22 12:42:13 -06001016 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -06001017 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001018 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001019
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001020 if (disp_table == NULL)
1021 return NULL;
1022
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001023 addr = loader_lookup_dispatch_table(disp_table, pName);
1024 if (addr)
1025 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001026 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001027 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001028 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -06001029 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001030 }
1031}
1032
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001033LOADER_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 -06001034{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001035 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001036 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001037 char *lib_name;
1038 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001039 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -06001040 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001041 char layer_buf[16][256];
1042 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001043
Jon Ashburn1323eb72014-12-02 13:03:09 -07001044 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001045 return XGL_ERROR_INVALID_POINTER;
1046
Jon Ashburn46d1f582015-01-28 11:01:35 -07001047 if (!icd)
1048 return XGL_ERROR_UNAVAILABLE;
1049
Jon Ashburn1323eb72014-12-02 13:03:09 -07001050 for (int i = 0; i < 16; i++)
1051 layers[i] = &layer_buf[i][0];
1052
1053 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1054 lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001055 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1056 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001057 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001058 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
1059 //use default layer name based on library name XGL_LAYER_LIBRARY_PREFIX<name>.XGL_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001060 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001061 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001062 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001063 lib_name = basename(lib_name);
1064 pEnd = strrchr(lib_name, '.');
Ian Elliott19628802015-02-04 12:06:46 -07001065 siz = (int) (pEnd - lib_name - strlen(XGL_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001066 if (pEnd == NULL || siz <= 0)
1067 continue;
1068 cpyStr = malloc(siz);
1069 if (cpyStr == NULL) {
1070 free(cpyStr);
1071 continue;
1072 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001073 strncpy(cpyStr, lib_name + strlen(XGL_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001074 cpyStr[siz - 1] = '\0';
1075 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001076 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001077 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1078 pOutLayers[count][siz - 1] = '\0';
1079 count++;
1080 free(cpyStr);
1081 }
1082 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001083 size_t cnt;
1084 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001085 XGL_RESULT res;
Ian Elliott19628802015-02-04 12:06:46 -07001086 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001087 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1088 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001089 if (res != XGL_SUCCESS)
1090 continue;
1091 if (cnt + count > maxLayerCount)
1092 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001093 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001094 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1095 if (n > 0)
1096 pOutLayers[i - count][n - 1] = '\0';
1097 }
1098 count += cnt;
1099 }
1100 }
1101
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001102 *pOutLayerCount = count;
1103
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001104 return XGL_SUCCESS;
1105}
1106
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001107LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001108{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001109 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001110 struct loader_instance *inst;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001111 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001112 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001113
Jon Ashburn46d1f582015-01-28 11:01:35 -07001114 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001115 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1116 }
1117
Jon Ashburn98bd4542015-01-29 16:44:24 -07001118 for (inst = loader.instances; inst; inst = inst->next) {
1119 for (icd = inst->icds; icd; icd = icd->next) {
1120 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1121 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(
1122 pfnMsgCallback, pUserData);
1123 if (res != XGL_SUCCESS) {
1124 gpu_idx = i;
1125 break;
1126 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001127 }
Jon Ashburn98bd4542015-01-29 16:44:24 -07001128 if (res != XGL_SUCCESS)
1129 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001130 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001131 if (res != XGL_SUCCESS)
1132 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001133 }
1134
1135 /* roll back on errors */
1136 if (icd) {
Jon Ashburn98bd4542015-01-29 16:44:24 -07001137 for (struct loader_instance *tmp_inst = loader.instances;
1138 tmp_inst != inst; tmp_inst = tmp_inst->next) {
1139 for (const struct loader_icd * tmp = tmp_inst->icds; tmp != icd;
1140 tmp = tmp->next) {
1141 for (uint32_t i = 0; i < icd->gpu_count; i++)
1142 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1143 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001144 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001145 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001146 for (uint32_t i = 0; i < gpu_idx; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001147 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001148
1149 return res;
1150 }
1151
1152 return XGL_SUCCESS;
1153}
1154
Chia-I Wu19300602014-08-04 08:03:57 +08001155LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001156{
Jon Ashburn98bd4542015-01-29 16:44:24 -07001157 XGL_RESULT res = XGL_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001158
Jon Ashburn46d1f582015-01-28 11:01:35 -07001159 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001160 return loader_msg_callback_remove(pfnMsgCallback);
1161 }
1162
Jon Ashburn98bd4542015-01-29 16:44:24 -07001163 for (struct loader_instance *inst = loader.instances; inst;
1164 inst = inst->next) {
1165 for (const struct loader_icd * icd = inst->icds; icd;
1166 icd = icd->next) {
1167 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1168 XGL_RESULT r;
1169 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1170 if (r != XGL_SUCCESS) {
1171 res = r;
1172 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001173 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001174 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001175 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001176 return res;
1177}
1178
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001179LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001180{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001181 XGL_RESULT res = XGL_SUCCESS;
1182
Jon Ashburn46d1f582015-01-28 11:01:35 -07001183 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001184 if (dataSize == 0)
1185 return XGL_ERROR_INVALID_VALUE;
1186
1187 switch (dbgOption) {
1188 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1189 loader.debug_echo_enable = *((const bool *) pData);
1190 break;
1191 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1192 loader.break_on_error = *((const bool *) pData);
1193 break;
1194 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1195 loader.break_on_warning = *((const bool *) pData);
1196 break;
1197 default:
1198 res = XGL_ERROR_INVALID_VALUE;
1199 break;
1200 }
1201
1202 return res;
1203 }
1204
Jon Ashburn98bd4542015-01-29 16:44:24 -07001205 for (struct loader_instance *inst = loader.instances; inst;
1206 inst = inst->next) {
1207 for (const struct loader_icd * icd = inst->icds; icd;
1208 icd = icd->next) {
1209 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1210 XGL_RESULT r;
1211 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption,
1212 dataSize, pData);
1213 /* unfortunately we cannot roll back */
1214 if (r != XGL_SUCCESS) {
1215 res = r;
1216 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001217 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001218 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001219 }
1220
1221 return res;
1222}