blob: 548a9aa3a671b01f51618c3793479475d9c3dd96 [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);
Jon Ashburn42688c32015-01-16 08:46:38 -0700438 if ((handle = dlopen(temp_str, RTLD_LAZY)) == NULL) {
439 dent = readdir(curdir);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600440 continue;
Jon Ashburn42688c32015-01-16 08:46:38 -0700441 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600442 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
443 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
444 break;
445 }
446 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
447 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
448 break;
449 }
450 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
451 loader.scanned_layer_count++;
452 dlclose(handle);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600453 }
454
455 dent = readdir(curdir);
456 }
457 closedir(curdir);
458 }
459 }
460
Jon Ashburn183dfd02014-10-22 18:13:16 -0600461 loader.layer_scanned = true;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600462}
463
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600464static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600465{
Chia-I Wu38e5a2c2015-01-04 11:12:47 +0800466 loader_initialize_dispatch_table(tab, fpGPA, gpu);
467
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600468 if (tab->EnumerateLayers == NULL)
469 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600470}
471
Jon Ashburnb55278a2014-10-17 15:09:07 -0600472static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, XGL_UINT *gpu_index)
473{
474 for (struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
475 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600476 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburnb55278a2014-10-17 15:09:07 -0600477 *gpu_index = i;
478 return icd;
479 }
480 }
481 return NULL;
482}
483
Jon Ashburn183dfd02014-10-22 18:13:16 -0600484static bool loader_layers_activated(const struct loader_icd *icd, const XGL_UINT gpu_index)
Jon Ashburnb55278a2014-10-17 15:09:07 -0600485{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600486 if (icd->layer_count[gpu_index])
487 return true;
488 else
489 return false;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600490}
491
Jon Ashburnead95c52014-11-18 09:06:04 -0700492static 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 -0600493{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600494 if (!icd)
495 return;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600496
Jon Ashburn183dfd02014-10-22 18:13:16 -0600497 struct loader_layers *obj;
498 bool foundLib;
499 for (XGL_UINT i = 0; i < count; i++) {
500 foundLib = false;
501 for (XGL_UINT j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700502 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 -0600503 foundLib = true;
504 break;
505 }
506 }
507 if (!foundLib) {
508 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnead95c52014-11-18 09:06:04 -0700509 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
510 obj->name[sizeof(obj->name) - 1] = '\0';
511 if ((obj->lib_handle = dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
512 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 -0600513 continue;
514 } else {
Jon Ashburnead95c52014-11-18 09:06:04 -0700515 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 -0600516 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700517 free(pLayerNames[i].layer_name);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600518 icd->layer_count[gpu_index]++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600519 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600520 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600521}
522
Jon Ashburnead95c52014-11-18 09:06:04 -0700523static bool find_layer_name(struct loader_icd *icd, XGL_UINT gpu_index, const char * layer_name, const char **lib_name)
524{
525 void *handle;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600526 xglEnumerateLayersType fpEnumerateLayers;
Jon Ashburnead95c52014-11-18 09:06:04 -0700527 XGL_CHAR layer_buf[16][256];
528 XGL_CHAR * layers[16];
529
530 for (int i = 0; i < 16; i++)
531 layers[i] = &layer_buf[i][0];
532
533 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
534 *lib_name = loader.scanned_layer_names[j];
535 if ((handle = dlopen(*lib_name, RTLD_LAZY)) == NULL)
536 continue;
537 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
538 //use default layer name based on library name libXGLLayer<name>.so
539 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
540 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), "libXGLLayer%s.so", layer_name);
541 dlclose(handle);
542 if (!strcmp(basename(*lib_name), lib_str)) {
543 free(lib_str);
544 return true;
545 }
546 else {
547 free(lib_str);
548 continue;
549 }
550 }
551 else {
552 XGL_SIZE cnt;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600553 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (XGL_VOID *) icd->gpus + gpu_index);
Jon Ashburnead95c52014-11-18 09:06:04 -0700554 for (unsigned int i = 0; i < cnt; i++) {
555 if (!strcmp((char *) layers[i], layer_name)) {
556 dlclose(handle);
557 return true;
558 }
559 }
560 }
561
562 dlclose(handle);
563 }
564
565 return false;
566}
567
568static 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 -0600569{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600570 const char *layerEnv;
571 XGL_UINT len, count = 0;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600572 char *p, *pOrig, *next, *name;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600573
Jon Ashburnead95c52014-11-18 09:06:04 -0700574 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn4fbcb032014-10-23 10:29:09 -0600575 if (!layerEnv)
576 return 0;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600577 p = malloc(strlen(layerEnv) + 1);
578 if (!p)
579 return 0;
580 strcpy(p, layerEnv);
Jon Ashburnb4d00532014-10-22 21:15:26 -0600581 pOrig = p;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600582
Jon Ashburn183dfd02014-10-22 18:13:16 -0600583 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700584 const char *lib_name = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600585 next = strchr(p, ':');
586 if (next == NULL) {
587 len = strlen(p);
588 next = p + len;
589 }
590 else {
591 len = next - p;
592 *(char *) next = '\0';
593 next++;
594 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600595 name = basename(p);
Jon Ashburnead95c52014-11-18 09:06:04 -0700596 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600597 p = next;
598 continue;
599 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600600
Jon Ashburnead95c52014-11-18 09:06:04 -0700601 len = strlen(name);
602 pLayerNames[count].layer_name = malloc(len + 1);
603 if (!pLayerNames[count].layer_name) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600604 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600605 return count;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600606 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700607 strncpy((char *) pLayerNames[count].layer_name, name, len);
608 pLayerNames[count].layer_name[len] = '\0';
609 pLayerNames[count].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600610 count++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600611 p = next;
612
Jon Ashburn183dfd02014-10-22 18:13:16 -0600613 };
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600614
Jon Ashburnb4d00532014-10-22 21:15:26 -0600615 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600616 return count;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600617}
618
Jon Ashburnead95c52014-11-18 09:06:04 -0700619static 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 -0600620{
Jon Ashburnead95c52014-11-18 09:06:04 -0700621 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn183dfd02014-10-22 18:13:16 -0600622
623 *ppLayerNames = &layerNames[0];
624 if (!pCreateInfo) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700625 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600626 }
627
628 XGL_LAYER_CREATE_INFO *pCi = (XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
629
630 while (pCi) {
631 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
632 const char *name;
633 XGL_UINT len;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600634 for (XGL_UINT i = 0; i < pCi->layerCount; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700635 const char * lib_name = NULL;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800636 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnead95c52014-11-18 09:06:04 -0700637 if (!find_layer_name(icd, gpu_index, name, &lib_name))
638 return loader_get_layer_env(icd, gpu_index, layerNames);
639 len = strlen(name);
640 layerNames[i].layer_name = malloc(len + 1);
641 if (!layerNames[i].layer_name)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600642 return i;
Jon Ashburnead95c52014-11-18 09:06:04 -0700643 strncpy((char *) layerNames[i].layer_name, name, len);
644 layerNames[i].layer_name[len] = '\0';
645 layerNames[i].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600646 }
Courtney Goeltzenleuchter68af55e2015-01-07 10:17:44 -0700647 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600648 }
649 pCi = pCi->pNext;
650 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700651 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600652}
653
654static void loader_deactivate_layer()
655{
656 struct loader_icd *icd;
657 struct loader_layers *libs;
658
659 for (icd = loader.icds; icd; icd = icd->next) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600660 if (icd->gpus)
661 free(icd->gpus);
662 icd->gpus = NULL;
663 if (icd->loader_dispatch)
664 free(icd->loader_dispatch);
665 icd->loader_dispatch = NULL;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600666 for (XGL_UINT j = 0; j < icd->gpu_count; j++) {
667 if (icd->layer_count[j] > 0) {
668 for (XGL_UINT i = 0; i < icd->layer_count[j]; i++) {
669 libs = &(icd->layer_libs[j][i]);
670 if (libs->lib_handle)
671 dlclose(libs->lib_handle);
672 libs->lib_handle = NULL;
673 }
Jon Ashburnb4d00532014-10-22 21:15:26 -0600674 if (icd->wrappedGpus[j])
675 free(icd->wrappedGpus[j]);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600676 }
677 icd->layer_count[j] = 0;
678 }
679 icd->gpu_count = 0;
680 }
681}
682
Jon Ashburn10053332014-11-17 10:17:37 -0700683extern XGL_UINT loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600684{
685 XGL_UINT gpu_index;
686 XGL_UINT count;
Jon Ashburnead95c52014-11-18 09:06:04 -0700687 struct layer_name_pair *pLayerNames;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600688 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600689
690 if (!icd)
691 return 0;
692 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
693
694 /* activate any layer libraries */
695 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600696 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
697 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600698 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600699
Jon Ashburnead95c52014-11-18 09:06:04 -0700700 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600701 if (!count)
702 return 0;
Jon Ashburnead95c52014-11-18 09:06:04 -0700703 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600704
Jon Ashburnb4d00532014-10-22 21:15:26 -0600705 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
706 if (! icd->wrappedGpus[gpu_index])
707 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600708 for (XGL_INT i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600709 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600710 nextGpuObj->pGPA = nextGPA;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600711 nextGpuObj->baseObject = baseObj;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600712 nextGpuObj->nextObject = gpuObj;
713 gpuObj = nextGpuObj;
714
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700715 char funcStr[256];
716 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
717 if ((nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
718 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600719 if (!nextGPA) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700720 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 -0600721 continue;
722 }
723
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600724 if (i == 0) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600725 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600726 //Insert the new wrapped objects into the list with loader object at head
727 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
728 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
729 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
730 gpuObj->nextObject = baseObj;
731 gpuObj->pGPA = icd->GetProcAddr;
732 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600733
734 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600735 }
736 else {
737 //make sure requested Layers matches currently activated Layers
Jon Ashburnead95c52014-11-18 09:06:04 -0700738 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600739 for (XGL_UINT i = 0; i < count; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700740 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600741 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
742 break;
743 }
744 }
745 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700746 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600747 }
748 }
749 return icd->layer_count[gpu_index];
750}
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600751
Jon Ashburn21734942014-10-17 15:31:22 -0600752LOADER_EXPORT XGL_VOID * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR * pName) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600753
754 if (gpu == NULL)
755 return NULL;
Jon Ashburn8eecd422014-10-22 12:42:13 -0600756 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600757 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wu38e5a2c2015-01-04 11:12:47 +0800758 void *addr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600759
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600760 if (disp_table == NULL)
761 return NULL;
762
Chia-I Wu38e5a2c2015-01-04 11:12:47 +0800763 addr = loader_lookup_dispatch_table(disp_table, pName);
764 if (addr)
765 return addr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600766 else {
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600767 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600768 return NULL;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600769 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600770 }
771}
772
Chia-I Wu468e3c32014-08-04 08:03:57 +0800773LOADER_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 +0800774{
775 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600776 struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800777 XGL_UINT count = 0;
778 XGL_RESULT res;
779
Jon Ashburn815bddd2014-10-16 15:48:50 -0600780 // cleanup any prior layer initializations
Jon Ashburnb55278a2014-10-17 15:09:07 -0600781 loader_deactivate_layer();
Jon Ashburn815bddd2014-10-16 15:48:50 -0600782
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800783 pthread_once(&once, loader_icd_scan);
784
785 if (!loader.icds)
786 return XGL_ERROR_UNAVAILABLE;
787
788 icd = loader.icds;
789 while (icd) {
790 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600791 XGL_BASE_LAYER_OBJECT * wrappedGpus;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600792 xglGetProcAddrType getProcAddr = icd->GetProcAddr;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800793 XGL_UINT n, max = maxGpus - count;
794
795 if (max > XGL_MAX_PHYSICAL_GPUS) {
796 max = XGL_MAX_PHYSICAL_GPUS;
797 }
798
799 res = icd->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu74916ed2014-08-06 12:17:04 +0800800 if (res == XGL_SUCCESS && n) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600801 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn183dfd02014-10-22 18:13:16 -0600802 icd->gpus = wrappedGpus;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600803 icd->gpu_count = n;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600804 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600805 for (int i = 0; i < n; i++) {
806 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -0600807 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600808 (wrappedGpus + i)->nextObject = gpus[i];
809 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn8eecd422014-10-22 12:42:13 -0600810 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600811 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -0600812 *disp = icd->loader_dispatch + i;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600813 }
814
Jon Ashburn99e996a2014-12-18 17:00:52 -0700815 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
816 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
817 loader_log(XGL_DBG_MSG_WARNING, 0,
818 "ICD ignored: failed to migrate settings");
819 loader_icd_destroy(icd);
820 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800821 count += n;
822
823 if (count >= maxGpus) {
824 break;
825 }
826 }
827
828 icd = icd->next;
829 }
830
Jon Ashburn99e996a2014-12-18 17:00:52 -0700831 /* we have nothing to log anymore */
832 loader_msg_callback_clear();
833
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600834 /* get layer libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -0600835 if (!loader.layer_scanned)
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700836 layer_lib_scan(NULL);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600837
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800838 *pGpuCount = count;
839
840 return (count > 0) ? XGL_SUCCESS : res;
841}
842
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600843LOADER_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 -0600844{
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700845 XGL_UINT gpu_index;
846 XGL_UINT count = 0;
847 char *lib_name;
848 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
849 void *handle;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600850 xglEnumerateLayersType fpEnumerateLayers;
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700851 XGL_CHAR layer_buf[16][256];
852 XGL_CHAR * layers[16];
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600853
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700854 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600855 return XGL_ERROR_INVALID_POINTER;
856
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700857 for (int i = 0; i < 16; i++)
858 layers[i] = &layer_buf[i][0];
859
860 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
861 lib_name = loader.scanned_layer_names[j];
862 if ((handle = dlopen(lib_name, RTLD_LAZY)) == NULL)
863 continue;
864 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
865 //use default layer name based on library name libXGLLayer<name>.so
866 char *pEnd, *cpyStr;
867 int siz;
868 dlclose(handle);
869 lib_name = basename(lib_name);
870 pEnd = strrchr(lib_name, '.');
871 siz = pEnd - lib_name - strlen("libXGLLayer") + 1;
872 if (pEnd == NULL || siz <= 0)
873 continue;
874 cpyStr = malloc(siz);
875 if (cpyStr == NULL) {
876 free(cpyStr);
877 continue;
878 }
879 strncpy(cpyStr, lib_name + strlen("libXGLLayer"), siz);
880 cpyStr[siz - 1] = '\0';
881 if (siz > maxStringSize)
882 siz = maxStringSize;
883 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
884 pOutLayers[count][siz - 1] = '\0';
885 count++;
886 free(cpyStr);
887 }
888 else {
889 XGL_SIZE cnt;
890 XGL_UINT n;
891 XGL_RESULT res;
892 n = (maxStringSize < 256) ? maxStringSize : 256;
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600893 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (XGL_VOID *) icd->gpus + gpu_index);
Jon Ashburnb9a54ac2014-12-02 13:03:09 -0700894 dlclose(handle);
895 if (res != XGL_SUCCESS)
896 continue;
897 if (cnt + count > maxLayerCount)
898 cnt = maxLayerCount - count;
899 for (unsigned int i = count; i < cnt + count; i++) {
900 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
901 if (n > 0)
902 pOutLayers[i - count][n - 1] = '\0';
903 }
904 count += cnt;
905 }
906 }
907
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600908 *pOutLayerCount = count;
909
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600910 return XGL_SUCCESS;
911}
912
Chia-I Wu468e3c32014-08-04 08:03:57 +0800913LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800914{
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700915 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800916 XGL_RESULT res;
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700917 XGL_UINT gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800918
919 if (!loader.scanned) {
920 return loader_msg_callback_add(pfnMsgCallback, pUserData);
921 }
922
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700923 for (icd = loader.icds; icd; icd = icd->next) {
924 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
925 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
926 if (res != XGL_SUCCESS) {
927 gpu_idx = i;
928 break;
929 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800930 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700931 if (res != XGL_SUCCESS)
932 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800933 }
934
935 /* roll back on errors */
936 if (icd) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700937 for (const struct loader_icd * tmp = loader.icds; tmp != icd; tmp = tmp->next) {
938 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
939 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800940 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700941 /* and gpus on current icd */
942 for (XGL_UINT i = 0; i < gpu_idx; i++)
943 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800944
945 return res;
946 }
947
948 return XGL_SUCCESS;
949}
950
Chia-I Wu468e3c32014-08-04 08:03:57 +0800951LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800952{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800953 XGL_RESULT res = XGL_SUCCESS;
954
955 if (!loader.scanned) {
956 return loader_msg_callback_remove(pfnMsgCallback);
957 }
958
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700959 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
960 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
961 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
962 if (r != XGL_SUCCESS) {
963 res = r;
964 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800965 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800966 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800967 return res;
968}
969
Chia-I Wu468e3c32014-08-04 08:03:57 +0800970LOADER_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 +0800971{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800972 XGL_RESULT res = XGL_SUCCESS;
973
974 if (!loader.scanned) {
975 if (dataSize == 0)
976 return XGL_ERROR_INVALID_VALUE;
977
978 switch (dbgOption) {
979 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
980 loader.debug_echo_enable = *((const bool *) pData);
981 break;
982 case XGL_DBG_OPTION_BREAK_ON_ERROR:
983 loader.break_on_error = *((const bool *) pData);
984 break;
985 case XGL_DBG_OPTION_BREAK_ON_WARNING:
986 loader.break_on_warning = *((const bool *) pData);
987 break;
988 default:
989 res = XGL_ERROR_INVALID_VALUE;
990 break;
991 }
992
993 return res;
994 }
995
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700996 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
997 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
998 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
999 /* unfortunately we cannot roll back */
1000 if (r != XGL_SUCCESS) {
1001 res = r;
1002 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001003 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001004 }
1005
1006 return res;
1007}