blob: 8a0b3eb9f59b28cca92171bc33889f2f236dad65 [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 Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Jon Ashburn406a0fe2014-11-14 09:52:42 -070026 * Jon Ashburn <jon@lunarg.com>
Chia-I Wu44e42362014-09-02 08:32:09 +080027 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080028 */
Jon Ashburn183dfd02014-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 Wu894a1172014-08-04 11:18:20 +080036#include <sys/types.h>
37#include <dirent.h>
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -060038#include <unistd.h>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080039#include <dlfcn.h>
40#include <pthread.h>
Jon Ashburnd43f9b62014-10-14 19:15:22 -060041#include <assert.h>
Chia-I Wu38e5a2c2015-01-04 11:12:47 +080042#include "table_ops.h"
Chia-I Wu468e3c32014-08-04 08:03:57 +080043#include "loader.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080044
Jon Ashburn183dfd02014-10-22 18:13:16 -060045struct loader_layers {
46 void *lib_handle;
Jon Ashburnead95c52014-11-18 09:06:04 -070047 char name[256];
48};
49
50struct layer_name_pair {
51 char *layer_name;
52 const char *lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -060053};
54
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080055struct loader_icd {
56 void *handle;
57
Jon Ashburnb55278a2014-10-17 15:09:07 -060058 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Jon Ashburn183dfd02014-10-22 18:13:16 -060059 XGL_UINT layer_count[XGL_MAX_PHYSICAL_GPUS];
60 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnb4d00532014-10-22 21:15:26 -060061 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnb55278a2014-10-17 15:09:07 -060062 XGL_UINT gpu_count;
Jon Ashburn183dfd02014-10-22 18:13:16 -060063 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburn815bddd2014-10-16 15:48:50 -060064
Mark Lobodzinski953a1692015-01-09 15:12:03 -060065 xglGetProcAddrType GetProcAddr;
66 xglInitAndEnumerateGpusType InitAndEnumerateGpus;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080067
68 struct loader_icd *next;
69};
70
Jon Ashburnd43f9b62014-10-14 19:15:22 -060071
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080072struct loader_msg_callback {
73 XGL_DBG_MSG_CALLBACK_FUNCTION func;
74 XGL_VOID *data;
75
76 struct loader_msg_callback *next;
77};
78
Jon Ashburnd43f9b62014-10-14 19:15:22 -060079
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080080static struct {
81 bool scanned;
82 struct loader_icd *icds;
Jon Ashburn183dfd02014-10-22 18:13:16 -060083 bool layer_scanned;
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -070084 char *layer_dirs;
Jon Ashburn183dfd02014-10-22 18:13:16 -060085 unsigned int scanned_layer_count;
86 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080087 struct loader_msg_callback *msg_callbacks;
88
89 bool debug_echo_enable;
90 bool break_on_error;
91 bool break_on_warning;
92} loader;
93
94static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
95 XGL_VOID *data)
96{
97 struct loader_msg_callback *cb;
98
99 cb = malloc(sizeof(*cb));
100 if (!cb)
101 return XGL_ERROR_OUT_OF_MEMORY;
102
103 cb->func = func;
104 cb->data = data;
105
106 cb->next = loader.msg_callbacks;
107 loader.msg_callbacks = cb;
108
109 return XGL_SUCCESS;
110}
111
112static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
113{
114 struct loader_msg_callback *cb = loader.msg_callbacks;
115
116 /*
117 * Find the first match (last registered).
118 *
119 * XXX What if the same callback function is registered more than once?
120 */
121 while (cb) {
122 if (cb->func == func) {
123 break;
124 }
125
126 cb = cb->next;
127 }
128
129 if (!cb)
130 return XGL_ERROR_INVALID_POINTER;
131
132 free(cb);
133
134 return XGL_SUCCESS;
135}
136
137static void loader_msg_callback_clear(void)
138{
139 struct loader_msg_callback *cb = loader.msg_callbacks;
140
141 while (cb) {
142 struct loader_msg_callback *next = cb->next;
143 free(cb);
144 cb = next;
145 }
146
147 loader.msg_callbacks = NULL;
148}
149
150static void loader_log(XGL_DBG_MSG_TYPE msg_type, XGL_INT msg_code,
151 const char *format, ...)
152{
153 const struct loader_msg_callback *cb = loader.msg_callbacks;
154 char msg[256];
155 va_list ap;
156 int ret;
157
158 va_start(ap, format);
159 ret = vsnprintf(msg, sizeof(msg), format, ap);
160 if (ret >= sizeof(msg) || ret < 0) {
161 msg[sizeof(msg) - 1] = '\0';
162 }
163 va_end(ap);
164
165 if (loader.debug_echo_enable || !cb) {
166 fputs(msg, stderr);
167 fputc('\n', stderr);
168 }
169
170 while (cb) {
171 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800172 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800173 cb = cb->next;
174 }
175
176 switch (msg_type) {
177 case XGL_DBG_MSG_ERROR:
178 if (loader.break_on_error) {
179 exit(1);
180 }
181 /* fall through */
182 case XGL_DBG_MSG_WARNING:
183 if (loader.break_on_warning) {
184 exit(1);
185 }
186 break;
187 default:
188 break;
189 }
190}
191
192static void
193loader_icd_destroy(struct loader_icd *icd)
194{
195 dlclose(icd->handle);
196 free(icd);
197}
198
199static struct loader_icd *
200loader_icd_create(const char *filename)
201{
202 struct loader_icd *icd;
203
204 icd = malloc(sizeof(*icd));
205 if (!icd)
206 return NULL;
207
Courtney Goeltzenleuchter6f928162014-10-28 10:29:27 -0600208 memset(icd, 0, sizeof(*icd));
209
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800210 icd->handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
211 if (!icd->handle) {
212 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror());
213 free(icd);
214 return NULL;
215 }
216
217#define LOOKUP(icd, func) do { \
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600218 icd->func = (xgl ##func## Type) dlsym(icd->handle, "xgl" #func); \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800219 if (!icd->func) { \
220 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror()); \
221 loader_icd_destroy(icd); \
222 return NULL; \
223 } \
224} while (0)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600225 LOOKUP(icd, GetProcAddr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800226 LOOKUP(icd, InitAndEnumerateGpus);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800227#undef LOOKUP
228
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) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700238 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
239 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) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700252 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
253 (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) { \
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700268 for (XGL_UINT i = 0; i < icd->gpu_count; i++) { \
269 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
Chia-I Wu894a1172014-08-04 11:18:20 +0800284static struct loader_icd *loader_icd_add(const char *filename)
285{
286 struct loader_icd *icd;
287
288 icd = loader_icd_create(filename);
289 if (!icd)
290 return NULL;
291
Chia-I Wu894a1172014-08-04 11:18:20 +0800292 /* prepend to the list */
293 icd->next = loader.icds;
294 loader.icds = icd;
295
296 return icd;
297}
298
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600299#ifndef DEFAULT_XGL_DRIVERS_PATH
300// TODO: Is this a good default location?
301// Need to search for both 32bit and 64bit ICDs
302#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
303#endif
304
305/**
306 * Try to \c loader_icd_scan XGL driver(s).
307 *
308 * This function scans the default system path or path
309 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
310 * order to find loadable XGL ICDs with the name of libXGL_*.
311 *
312 * \returns
313 * void; but side effect is to set loader_icd_scanned to true
314 */
315static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800316{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600317 const char *libPaths, *p, *next;
318 DIR *sysdir;
319 struct dirent *dent;
320 char icd_library[1024];
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600321 char path[1024];
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600322 int len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800323
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600324 libPaths = NULL;
325 if (geteuid() == getuid()) {
326 /* don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
327 libPaths = getenv("LIBXGL_DRIVERS_PATH");
328 }
329 if (libPaths == NULL)
330 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800331
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600332 for (p = libPaths; *p; p = next) {
333 next = strchr(p, ':');
334 if (next == NULL) {
335 len = strlen(p);
336 next = p + len;
337 }
338 else {
339 len = next - p;
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600340 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
341 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600342 next++;
343 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800344
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600345 sysdir = opendir(p);
346 if (sysdir) {
347 dent = readdir(sysdir);
348 while (dent) {
349 /* look for ICDs starting with "libXGL_" */
350 if (!strncmp(dent->d_name, "libXGL_", 7)) {
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600351 snprintf(icd_library, 1024, "%s/%s",p,dent->d_name);
Chia-I Wu894a1172014-08-04 11:18:20 +0800352 loader_icd_add(icd_library);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600353 }
354
355 dent = readdir(sysdir);
356 }
357 closedir(sysdir);
358 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800359 }
360
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800361
362 loader.scanned = true;
363}
364
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600365#ifndef DEFAULT_XGL_LAYERS_PATH
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700366// TODO: Are these good default locations?
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600367#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
368#endif
369
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700370static void layer_lib_scan(const char * libInPaths)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600371{
372 const char *p, *next;
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700373 char *libPaths;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600374 DIR *curdir;
375 struct dirent *dent;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600376 int len, i;
377 char temp_str[1024];
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600378
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700379 len = 0;
380 loader.layer_dirs = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600381 if (libInPaths){
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700382 len = strlen(libInPaths);
383 p = libInPaths;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600384 }
385 else {
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700386 if (geteuid() == getuid()) {
387 p = getenv("LIBXGL_LAYERS_PATH");
388 if (p != NULL)
389 len = strlen(p);
390 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600391 }
392
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700393 if (len == 0) {
394 len = strlen(DEFAULT_XGL_LAYERS_PATH);
395 p = DEFAULT_XGL_LAYERS_PATH;
396 }
397
398 if (len == 0) {
399 // Have no paths to search
400 return;
401 }
402 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchter688c74b2014-12-02 18:12:51 -0700403 if (loader.layer_dirs == NULL)
404 return;
405
406 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
407 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700408 libPaths = loader.layer_dirs;
409
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600410 /* cleanup any previously scanned libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -0600411 for (i = 0; i < loader.scanned_layer_count; i++) {
412 if (loader.scanned_layer_names[i] != NULL)
413 free(loader.scanned_layer_names[i]);
414 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600415 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600416 loader.scanned_layer_count = 0;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600417
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600418 for (p = libPaths; *p; p = next) {
419 next = strchr(p, ':');
420 if (next == NULL) {
421 len = strlen(p);
422 next = p + len;
423 }
424 else {
425 len = next - p;
426 *(char *) next = '\0';
427 next++;
428 }
429
430 curdir = opendir(p);
431 if (curdir) {
432 dent = readdir(curdir);
433 while (dent) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600434 /* look for wrappers starting with "libXGLlayer" */
435 if (!strncmp(dent->d_name, "libXGLLayer", strlen("libXGLLayer"))) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600436 void * handle;
437 snprintf(temp_str, sizeof(temp_str), "%s/%s",p,dent->d_name);
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800438 if ((handle = dlopen(temp_str, RTLD_LAZY)) == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600439 continue;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600440 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
441 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
442 break;
443 }
444 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
445 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
446 break;
447 }
448 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
449 loader.scanned_layer_count++;
450 dlclose(handle);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600451 }
452
453 dent = readdir(curdir);
454 }
455 closedir(curdir);
456 }
457 }
458
Jon Ashburn183dfd02014-10-22 18:13:16 -0600459 loader.layer_scanned = true;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600460}
461
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600462static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600463{
Chia-I Wu38e5a2c2015-01-04 11:12:47 +0800464 loader_initialize_dispatch_table(tab, fpGPA, gpu);
465
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600466 if (tab->EnumerateLayers == NULL)
467 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600468}
469
Jon Ashburnb55278a2014-10-17 15:09:07 -0600470static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, XGL_UINT *gpu_index)
471{
472 for (struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
473 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600474 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburnb55278a2014-10-17 15:09:07 -0600475 *gpu_index = i;
476 return icd;
477 }
478 }
479 return NULL;
480}
481
Jon Ashburn183dfd02014-10-22 18:13:16 -0600482static bool loader_layers_activated(const struct loader_icd *icd, const XGL_UINT gpu_index)
Jon Ashburnb55278a2014-10-17 15:09:07 -0600483{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600484 if (icd->layer_count[gpu_index])
485 return true;
486 else
487 return false;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600488}
489
Jon Ashburnead95c52014-11-18 09:06:04 -0700490static void loader_init_layer_libs(struct loader_icd *icd, XGL_UINT gpu_index, struct layer_name_pair * pLayerNames, XGL_UINT count)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600491{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600492 if (!icd)
493 return;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600494
Jon Ashburn183dfd02014-10-22 18:13:16 -0600495 struct loader_layers *obj;
496 bool foundLib;
497 for (XGL_UINT i = 0; i < count; i++) {
498 foundLib = false;
499 for (XGL_UINT j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700500 if (icd->layer_libs[gpu_index][j].lib_handle && !strcmp(icd->layer_libs[gpu_index][j].name, (char *) pLayerNames[i].layer_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600501 foundLib = true;
502 break;
503 }
504 }
505 if (!foundLib) {
506 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnead95c52014-11-18 09:06:04 -0700507 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
508 obj->name[sizeof(obj->name) - 1] = '\0';
509 if ((obj->lib_handle = dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
510 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to open layer library %s got error %d", pLayerNames[i].lib_name, dlerror());
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600511 continue;
512 } else {
Jon Ashburnead95c52014-11-18 09:06:04 -0700513 loader_log(XGL_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s", pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600514 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700515 free(pLayerNames[i].layer_name);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600516 icd->layer_count[gpu_index]++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600517 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600518 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600519}
520
Jon Ashburnead95c52014-11-18 09:06:04 -0700521static bool find_layer_name(struct loader_icd *icd, XGL_UINT gpu_index, const char * layer_name, const char **lib_name)
522{
523 void *handle;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600524 xglEnumerateLayersType fpEnumerateLayers;
Jon Ashburnead95c52014-11-18 09:06:04 -0700525 XGL_CHAR layer_buf[16][256];
526 XGL_CHAR * layers[16];
527
528 for (int i = 0; i < 16; i++)
529 layers[i] = &layer_buf[i][0];
530
531 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
532 *lib_name = loader.scanned_layer_names[j];
533 if ((handle = dlopen(*lib_name, RTLD_LAZY)) == NULL)
534 continue;
535 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
536 //use default layer name based on library name libXGLLayer<name>.so
537 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
538 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), "libXGLLayer%s.so", layer_name);
539 dlclose(handle);
540 if (!strcmp(basename(*lib_name), lib_str)) {
541 free(lib_str);
542 return true;
543 }
544 else {
545 free(lib_str);
546 continue;
547 }
548 }
549 else {
550 XGL_SIZE cnt;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600551 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (XGL_VOID *) icd->gpus + gpu_index);
Jon Ashburnead95c52014-11-18 09:06:04 -0700552 for (unsigned int i = 0; i < cnt; i++) {
553 if (!strcmp((char *) layers[i], layer_name)) {
554 dlclose(handle);
555 return true;
556 }
557 }
558 }
559
560 dlclose(handle);
561 }
562
563 return false;
564}
565
566static XGL_UINT loader_get_layer_env(struct loader_icd *icd, XGL_UINT gpu_index, struct layer_name_pair *pLayerNames)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600567{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600568 const char *layerEnv;
569 XGL_UINT len, count = 0;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600570 char *p, *pOrig, *next, *name;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600571
Jon Ashburnead95c52014-11-18 09:06:04 -0700572 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn4fbcb032014-10-23 10:29:09 -0600573 if (!layerEnv)
574 return 0;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600575 p = malloc(strlen(layerEnv) + 1);
576 if (!p)
577 return 0;
578 strcpy(p, layerEnv);
Jon Ashburnb4d00532014-10-22 21:15:26 -0600579 pOrig = p;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600580
Jon Ashburn183dfd02014-10-22 18:13:16 -0600581 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700582 const char *lib_name = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600583 next = strchr(p, ':');
584 if (next == NULL) {
585 len = strlen(p);
586 next = p + len;
587 }
588 else {
589 len = next - p;
590 *(char *) next = '\0';
591 next++;
592 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600593 name = basename(p);
Jon Ashburnead95c52014-11-18 09:06:04 -0700594 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600595 p = next;
596 continue;
597 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600598
Jon Ashburnead95c52014-11-18 09:06:04 -0700599 len = strlen(name);
600 pLayerNames[count].layer_name = malloc(len + 1);
601 if (!pLayerNames[count].layer_name) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600602 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600603 return count;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600604 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700605 strncpy((char *) pLayerNames[count].layer_name, name, len);
606 pLayerNames[count].layer_name[len] = '\0';
607 pLayerNames[count].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600608 count++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600609 p = next;
610
Jon Ashburn183dfd02014-10-22 18:13:16 -0600611 };
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600612
Jon Ashburnb4d00532014-10-22 21:15:26 -0600613 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600614 return count;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600615}
616
Jon Ashburnead95c52014-11-18 09:06:04 -0700617static XGL_UINT loader_get_layer_libs(struct loader_icd *icd, XGL_UINT gpu_index, const XGL_DEVICE_CREATE_INFO* pCreateInfo, struct layer_name_pair **ppLayerNames)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600618{
Jon Ashburnead95c52014-11-18 09:06:04 -0700619 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn183dfd02014-10-22 18:13:16 -0600620
621 *ppLayerNames = &layerNames[0];
622 if (!pCreateInfo) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700623 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600624 }
625
626 XGL_LAYER_CREATE_INFO *pCi = (XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
627
628 while (pCi) {
629 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
630 const char *name;
631 XGL_UINT len;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600632 for (XGL_UINT i = 0; i < pCi->layerCount; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700633 const char * lib_name = NULL;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800634 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnead95c52014-11-18 09:06:04 -0700635 if (!find_layer_name(icd, gpu_index, name, &lib_name))
636 return loader_get_layer_env(icd, gpu_index, layerNames);
637 len = strlen(name);
638 layerNames[i].layer_name = malloc(len + 1);
639 if (!layerNames[i].layer_name)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600640 return i;
Jon Ashburnead95c52014-11-18 09:06:04 -0700641 strncpy((char *) layerNames[i].layer_name, name, len);
642 layerNames[i].layer_name[len] = '\0';
643 layerNames[i].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600644 }
Courtney Goeltzenleuchter68af55e2015-01-07 10:17:44 -0700645 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600646 }
647 pCi = pCi->pNext;
648 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700649 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600650}
651
652static void loader_deactivate_layer()
653{
654 struct loader_icd *icd;
655 struct loader_layers *libs;
656
657 for (icd = loader.icds; icd; icd = icd->next) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600658 if (icd->gpus)
659 free(icd->gpus);
660 icd->gpus = NULL;
661 if (icd->loader_dispatch)
662 free(icd->loader_dispatch);
663 icd->loader_dispatch = NULL;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600664 for (XGL_UINT j = 0; j < icd->gpu_count; j++) {
665 if (icd->layer_count[j] > 0) {
666 for (XGL_UINT i = 0; i < icd->layer_count[j]; i++) {
667 libs = &(icd->layer_libs[j][i]);
668 if (libs->lib_handle)
669 dlclose(libs->lib_handle);
670 libs->lib_handle = NULL;
671 }
Jon Ashburnb4d00532014-10-22 21:15:26 -0600672 if (icd->wrappedGpus[j])
673 free(icd->wrappedGpus[j]);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600674 }
675 icd->layer_count[j] = 0;
676 }
677 icd->gpu_count = 0;
678 }
679}
680
Jon Ashburn10053332014-11-17 10:17:37 -0700681extern XGL_UINT loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600682{
683 XGL_UINT gpu_index;
684 XGL_UINT count;
Jon Ashburnead95c52014-11-18 09:06:04 -0700685 struct layer_name_pair *pLayerNames;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600686 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600687
688 if (!icd)
689 return 0;
690 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
691
692 /* activate any layer libraries */
693 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600694 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
695 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600696 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600697
Jon Ashburnead95c52014-11-18 09:06:04 -0700698 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600699 if (!count)
700 return 0;
Jon Ashburnead95c52014-11-18 09:06:04 -0700701 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600702
Jon Ashburnb4d00532014-10-22 21:15:26 -0600703 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
704 if (! icd->wrappedGpus[gpu_index])
705 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600706 for (XGL_INT i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600707 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600708 nextGpuObj->pGPA = nextGPA;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600709 nextGpuObj->baseObject = baseObj;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600710 nextGpuObj->nextObject = gpuObj;
711 gpuObj = nextGpuObj;
712
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700713 char funcStr[256];
714 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
715 if ((nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
716 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600717 if (!nextGPA) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700718 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to find xglGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600719 continue;
720 }
721
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600722 if (i == 0) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600723 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600724 //Insert the new wrapped objects into the list with loader object at head
725 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
726 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
727 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
728 gpuObj->nextObject = baseObj;
729 gpuObj->pGPA = icd->GetProcAddr;
730 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600731
732 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600733 }
734 else {
735 //make sure requested Layers matches currently activated Layers
Jon Ashburnead95c52014-11-18 09:06:04 -0700736 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600737 for (XGL_UINT i = 0; i < count; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700738 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600739 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
740 break;
741 }
742 }
743 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700744 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600745 }
746 }
747 return icd->layer_count[gpu_index];
748}
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600749
Jon Ashburn21734942014-10-17 15:31:22 -0600750LOADER_EXPORT XGL_VOID * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR * pName) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600751
752 if (gpu == NULL)
753 return NULL;
Jon Ashburn8eecd422014-10-22 12:42:13 -0600754 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600755 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wu38e5a2c2015-01-04 11:12:47 +0800756 void *addr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600757
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600758 if (disp_table == NULL)
759 return NULL;
760
Chia-I Wu38e5a2c2015-01-04 11:12:47 +0800761 addr = loader_lookup_dispatch_table(disp_table, pName);
762 if (addr)
763 return addr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600764 else {
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600765 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600766 return NULL;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600767 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600768 }
769}
770
Chia-I Wu468e3c32014-08-04 08:03:57 +0800771LOADER_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO* pAppInfo, const XGL_ALLOC_CALLBACKS* pAllocCb, XGL_UINT maxGpus, XGL_UINT* pGpuCount, XGL_PHYSICAL_GPU* pGpus)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800772{
773 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600774 struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800775 XGL_UINT count = 0;
776 XGL_RESULT res;
777
Jon Ashburn815bddd2014-10-16 15:48:50 -0600778 // cleanup any prior layer initializations
Jon Ashburnb55278a2014-10-17 15:09:07 -0600779 loader_deactivate_layer();
Jon Ashburn815bddd2014-10-16 15:48:50 -0600780
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800781 pthread_once(&once, loader_icd_scan);
782
783 if (!loader.icds)
784 return XGL_ERROR_UNAVAILABLE;
785
786 icd = loader.icds;
787 while (icd) {
788 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600789 XGL_BASE_LAYER_OBJECT * wrappedGpus;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600790 xglGetProcAddrType getProcAddr = icd->GetProcAddr;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800791 XGL_UINT n, max = maxGpus - count;
792
793 if (max > XGL_MAX_PHYSICAL_GPUS) {
794 max = XGL_MAX_PHYSICAL_GPUS;
795 }
796
797 res = icd->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu74916ed2014-08-06 12:17:04 +0800798 if (res == XGL_SUCCESS && n) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600799 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn183dfd02014-10-22 18:13:16 -0600800 icd->gpus = wrappedGpus;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600801 icd->gpu_count = n;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600802 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600803 for (int i = 0; i < n; i++) {
804 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -0600805 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600806 (wrappedGpus + i)->nextObject = gpus[i];
807 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn8eecd422014-10-22 12:42:13 -0600808 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600809 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -0600810 *disp = icd->loader_dispatch + i;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600811 }
812
Jon Ashburn99e996a2014-12-18 17:00:52 -0700813 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
814 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
815 loader_log(XGL_DBG_MSG_WARNING, 0,
816 "ICD ignored: failed to migrate settings");
817 loader_icd_destroy(icd);
818 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800819 count += n;
820
821 if (count >= maxGpus) {
822 break;
823 }
824 }
825
826 icd = icd->next;
827 }
828
Jon Ashburn99e996a2014-12-18 17:00:52 -0700829 /* we have nothing to log anymore */
830 loader_msg_callback_clear();
831
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600832 /* get layer libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -0600833 if (!loader.layer_scanned)
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700834 layer_lib_scan(NULL);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600835
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800836 *pGpuCount = count;
837
838 return (count > 0) ? XGL_SUCCESS : res;
839}
840
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600841LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, XGL_SIZE maxLayerCount, XGL_SIZE maxStringSize, XGL_SIZE* pOutLayerCount, XGL_CHAR* const* pOutLayers, XGL_VOID* pReserved)
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600842{
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700843 XGL_UINT gpu_index;
844 XGL_UINT count = 0;
845 char *lib_name;
846 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
847 void *handle;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600848 xglEnumerateLayersType fpEnumerateLayers;
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700849 XGL_CHAR layer_buf[16][256];
850 XGL_CHAR * layers[16];
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600851
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700852 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600853 return XGL_ERROR_INVALID_POINTER;
854
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700855 for (int i = 0; i < 16; i++)
856 layers[i] = &layer_buf[i][0];
857
858 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
859 lib_name = loader.scanned_layer_names[j];
860 if ((handle = dlopen(lib_name, RTLD_LAZY)) == NULL)
861 continue;
862 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
863 //use default layer name based on library name libXGLLayer<name>.so
864 char *pEnd, *cpyStr;
865 int siz;
866 dlclose(handle);
867 lib_name = basename(lib_name);
868 pEnd = strrchr(lib_name, '.');
869 siz = pEnd - lib_name - strlen("libXGLLayer") + 1;
870 if (pEnd == NULL || siz <= 0)
871 continue;
872 cpyStr = malloc(siz);
873 if (cpyStr == NULL) {
874 free(cpyStr);
875 continue;
876 }
877 strncpy(cpyStr, lib_name + strlen("libXGLLayer"), siz);
878 cpyStr[siz - 1] = '\0';
879 if (siz > maxStringSize)
880 siz = maxStringSize;
881 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
882 pOutLayers[count][siz - 1] = '\0';
883 count++;
884 free(cpyStr);
885 }
886 else {
887 XGL_SIZE cnt;
888 XGL_UINT n;
889 XGL_RESULT res;
890 n = (maxStringSize < 256) ? maxStringSize : 256;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600891 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (XGL_VOID *) icd->gpus + gpu_index);
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700892 dlclose(handle);
893 if (res != XGL_SUCCESS)
894 continue;
895 if (cnt + count > maxLayerCount)
896 cnt = maxLayerCount - count;
897 for (unsigned int i = count; i < cnt + count; i++) {
898 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
899 if (n > 0)
900 pOutLayers[i - count][n - 1] = '\0';
901 }
902 count += cnt;
903 }
904 }
905
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600906 *pOutLayerCount = count;
907
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600908 return XGL_SUCCESS;
909}
910
Chia-I Wu468e3c32014-08-04 08:03:57 +0800911LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800912{
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700913 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800914 XGL_RESULT res;
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700915 XGL_UINT gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800916
917 if (!loader.scanned) {
918 return loader_msg_callback_add(pfnMsgCallback, pUserData);
919 }
920
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700921 for (icd = loader.icds; icd; icd = icd->next) {
922 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
923 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
924 if (res != XGL_SUCCESS) {
925 gpu_idx = i;
926 break;
927 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800928 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700929 if (res != XGL_SUCCESS)
930 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800931 }
932
933 /* roll back on errors */
934 if (icd) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700935 for (const struct loader_icd * tmp = loader.icds; tmp != icd; tmp = tmp->next) {
936 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
937 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800938 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700939 /* and gpus on current icd */
940 for (XGL_UINT i = 0; i < gpu_idx; i++)
941 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800942
943 return res;
944 }
945
946 return XGL_SUCCESS;
947}
948
Chia-I Wu468e3c32014-08-04 08:03:57 +0800949LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800950{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800951 XGL_RESULT res = XGL_SUCCESS;
952
953 if (!loader.scanned) {
954 return loader_msg_callback_remove(pfnMsgCallback);
955 }
956
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700957 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
958 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
959 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
960 if (r != XGL_SUCCESS) {
961 res = r;
962 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800963 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800964 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800965 return res;
966}
967
Chia-I Wu468e3c32014-08-04 08:03:57 +0800968LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, XGL_SIZE dataSize, const XGL_VOID* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800969{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800970 XGL_RESULT res = XGL_SUCCESS;
971
972 if (!loader.scanned) {
973 if (dataSize == 0)
974 return XGL_ERROR_INVALID_VALUE;
975
976 switch (dbgOption) {
977 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
978 loader.debug_echo_enable = *((const bool *) pData);
979 break;
980 case XGL_DBG_OPTION_BREAK_ON_ERROR:
981 loader.break_on_error = *((const bool *) pData);
982 break;
983 case XGL_DBG_OPTION_BREAK_ON_WARNING:
984 loader.break_on_warning = *((const bool *) pData);
985 break;
986 default:
987 res = XGL_ERROR_INVALID_VALUE;
988 break;
989 }
990
991 return res;
992 }
993
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700994 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
995 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
996 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
997 /* unfortunately we cannot roll back */
998 if (r != XGL_SUCCESS) {
999 res = r;
1000 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001001 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001002 }
1003
1004 return res;
1005}