blob: 7f5309f75b98ca557ccfed9171e85f47d25661b5 [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>
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 Ashburnd38bfb12014-10-14 19:15:22 -060041#include <assert.h>
Chia-I Wuf46b81a2015-01-04 11:12:47 +080042#include "table_ops.h"
Chia-I Wu19300602014-08-04 08:03:57 +080043#include "loader.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080044
Jon Ashburn1beab2d2015-01-26 14:51:40 -070045struct loader_instance {
46 const XGL_APPLICATION_INFO *app_info;
47 const XGL_ALLOC_CALLBACKS *alloc_cb;
48
49 struct loader_icd *icds;
50 struct loader_instance *next;
51};
52
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060053struct loader_layers {
54 void *lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070055 char name[256];
56};
57
58struct layer_name_pair {
59 char *layer_name;
60 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060061};
62
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080063struct loader_icd {
64 void *handle;
65
Jon Ashburn876b1ac2014-10-17 15:09:07 -060066 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060067 uint32_t layer_count[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060068 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnd09bd102014-10-22 21:15:26 -060069 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060070 uint32_t gpu_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060071 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060072
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -060073 xglGetProcAddrType GetProcAddr;
74 xglInitAndEnumerateGpusType InitAndEnumerateGpus;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080075
76 struct loader_icd *next;
77};
78
Jon Ashburnd38bfb12014-10-14 19:15:22 -060079
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080080struct loader_msg_callback {
81 XGL_DBG_MSG_CALLBACK_FUNCTION func;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060082 void *data;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080083
84 struct loader_msg_callback *next;
85};
86
Jon Ashburnd38bfb12014-10-14 19:15:22 -060087
Jon Ashburn1beab2d2015-01-26 14:51:40 -070088// Note: Since the following is a static structure, all members are initialized
89// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080090static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070091 struct loader_instance *instances;
92
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080093 bool scanned;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060094 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -070095 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060096 unsigned int scanned_layer_count;
97 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080098 struct loader_msg_callback *msg_callbacks;
99
100 bool debug_echo_enable;
101 bool break_on_error;
102 bool break_on_warning;
103} loader;
104
105static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600106 void *data)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800107{
108 struct loader_msg_callback *cb;
109
110 cb = malloc(sizeof(*cb));
111 if (!cb)
112 return XGL_ERROR_OUT_OF_MEMORY;
113
114 cb->func = func;
115 cb->data = data;
116
117 cb->next = loader.msg_callbacks;
118 loader.msg_callbacks = cb;
119
120 return XGL_SUCCESS;
121}
122
123static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
124{
125 struct loader_msg_callback *cb = loader.msg_callbacks;
126
127 /*
128 * Find the first match (last registered).
129 *
130 * XXX What if the same callback function is registered more than once?
131 */
132 while (cb) {
133 if (cb->func == func) {
134 break;
135 }
136
137 cb = cb->next;
138 }
139
140 if (!cb)
141 return XGL_ERROR_INVALID_POINTER;
142
143 free(cb);
144
145 return XGL_SUCCESS;
146}
147
148static void loader_msg_callback_clear(void)
149{
150 struct loader_msg_callback *cb = loader.msg_callbacks;
151
152 while (cb) {
153 struct loader_msg_callback *next = cb->next;
154 free(cb);
155 cb = next;
156 }
157
158 loader.msg_callbacks = NULL;
159}
160
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600161static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800162 const char *format, ...)
163{
164 const struct loader_msg_callback *cb = loader.msg_callbacks;
165 char msg[256];
166 va_list ap;
167 int ret;
168
169 va_start(ap, format);
170 ret = vsnprintf(msg, sizeof(msg), format, ap);
171 if (ret >= sizeof(msg) || ret < 0) {
172 msg[sizeof(msg) - 1] = '\0';
173 }
174 va_end(ap);
175
176 if (loader.debug_echo_enable || !cb) {
177 fputs(msg, stderr);
178 fputc('\n', stderr);
179 }
180
181 while (cb) {
182 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800183 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800184 cb = cb->next;
185 }
186
187 switch (msg_type) {
188 case XGL_DBG_MSG_ERROR:
189 if (loader.break_on_error) {
190 exit(1);
191 }
192 /* fall through */
193 case XGL_DBG_MSG_WARNING:
194 if (loader.break_on_warning) {
195 exit(1);
196 }
197 break;
198 default:
199 break;
200 }
201}
202
203static void
204loader_icd_destroy(struct loader_icd *icd)
205{
206 dlclose(icd->handle);
207 free(icd);
208}
209
210static struct loader_icd *
211loader_icd_create(const char *filename)
212{
213 struct loader_icd *icd;
214
215 icd = malloc(sizeof(*icd));
216 if (!icd)
217 return NULL;
218
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600219 memset(icd, 0, sizeof(*icd));
220
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800221 icd->handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
222 if (!icd->handle) {
223 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror());
224 free(icd);
225 return NULL;
226 }
227
228#define LOOKUP(icd, func) do { \
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600229 icd->func = (xgl ##func## Type) dlsym(icd->handle, "xgl" #func); \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800230 if (!icd->func) { \
231 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror()); \
232 loader_icd_destroy(icd); \
233 return NULL; \
234 } \
235} while (0)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600236 LOOKUP(icd, GetProcAddr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800237 LOOKUP(icd, InitAndEnumerateGpus);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800238#undef LOOKUP
239
240 return icd;
241}
242
243static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
244{
245 const struct loader_msg_callback *cb = loader.msg_callbacks;
246 XGL_RESULT res;
247
248 while (cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600249 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700250 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
251 if (res != XGL_SUCCESS) {
252 break;
253 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800254 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800255 cb = cb->next;
256 }
257
258 /* roll back on errors */
259 if (cb) {
260 const struct loader_msg_callback *tmp = loader.msg_callbacks;
261
262 while (tmp != cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600263 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700264 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
265 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800266 tmp = tmp->next;
267 }
268
269 return res;
270 }
271
272 return XGL_SUCCESS;
273}
274
275static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
276{
277#define SETB(icd, opt, val) do { \
278 if (val) { \
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600279 for (uint32_t i = 0; i < icd->gpu_count; i++) { \
Jon Ashburn01e2d662014-11-14 09:52:42 -0700280 const XGL_RESULT res = \
281 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
282 if (res != XGL_SUCCESS) \
283 return res; \
284 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800285 } \
286} while (0)
287 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
288 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
289 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
290#undef SETB
291
292return XGL_SUCCESS;
293}
294
Chia-I Wu13a61a52014-08-04 11:18:20 +0800295static struct loader_icd *loader_icd_add(const char *filename)
296{
297 struct loader_icd *icd;
298
299 icd = loader_icd_create(filename);
300 if (!icd)
301 return NULL;
302
Chia-I Wu13a61a52014-08-04 11:18:20 +0800303 /* prepend to the list */
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700304 icd->next = loader.instances->icds;
305 loader.instances->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800306
307 return icd;
308}
309
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600310#ifndef DEFAULT_XGL_DRIVERS_PATH
311// TODO: Is this a good default location?
312// Need to search for both 32bit and 64bit ICDs
313#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
314#endif
315
316/**
317 * Try to \c loader_icd_scan XGL driver(s).
318 *
319 * This function scans the default system path or path
320 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
321 * order to find loadable XGL ICDs with the name of libXGL_*.
322 *
323 * \returns
324 * void; but side effect is to set loader_icd_scanned to true
325 */
326static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800327{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600328 const char *libPaths, *p, *next;
329 DIR *sysdir;
330 struct dirent *dent;
331 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600332 char path[1024];
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600333 int len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800334
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600335 libPaths = NULL;
336 if (geteuid() == getuid()) {
337 /* don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
338 libPaths = getenv("LIBXGL_DRIVERS_PATH");
339 }
340 if (libPaths == NULL)
341 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800342
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600343 for (p = libPaths; *p; p = next) {
344 next = strchr(p, ':');
345 if (next == NULL) {
346 len = strlen(p);
347 next = p + len;
348 }
349 else {
350 len = next - p;
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600351 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
352 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600353 next++;
354 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800355
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600356 sysdir = opendir(p);
357 if (sysdir) {
358 dent = readdir(sysdir);
359 while (dent) {
360 /* look for ICDs starting with "libXGL_" */
361 if (!strncmp(dent->d_name, "libXGL_", 7)) {
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600362 snprintf(icd_library, 1024, "%s/%s",p,dent->d_name);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800363 loader_icd_add(icd_library);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600364 }
365
366 dent = readdir(sysdir);
367 }
368 closedir(sysdir);
369 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800370 }
371
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800372
373 loader.scanned = true;
374}
375
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600376#ifndef DEFAULT_XGL_LAYERS_PATH
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700377// TODO: Are these good default locations?
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600378#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
379#endif
380
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700381static void layer_lib_scan(const char * libInPaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600382{
383 const char *p, *next;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700384 char *libPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600385 DIR *curdir;
386 struct dirent *dent;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600387 int len, i;
388 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600389
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700390 len = 0;
391 loader.layer_dirs = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600392 if (libInPaths){
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700393 len = strlen(libInPaths);
394 p = libInPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600395 }
396 else {
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700397 if (geteuid() == getuid()) {
398 p = getenv("LIBXGL_LAYERS_PATH");
399 if (p != NULL)
400 len = strlen(p);
401 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600402 }
403
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700404 if (len == 0) {
405 len = strlen(DEFAULT_XGL_LAYERS_PATH);
406 p = DEFAULT_XGL_LAYERS_PATH;
407 }
408
409 if (len == 0) {
410 // Have no paths to search
411 return;
412 }
413 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700414 if (loader.layer_dirs == NULL)
415 return;
416
417 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
418 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700419 libPaths = loader.layer_dirs;
420
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600421 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600422 for (i = 0; i < loader.scanned_layer_count; i++) {
423 if (loader.scanned_layer_names[i] != NULL)
424 free(loader.scanned_layer_names[i]);
425 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600426 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600427 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600428
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600429 for (p = libPaths; *p; p = next) {
430 next = strchr(p, ':');
431 if (next == NULL) {
432 len = strlen(p);
433 next = p + len;
434 }
435 else {
436 len = next - p;
437 *(char *) next = '\0';
438 next++;
439 }
440
441 curdir = opendir(p);
442 if (curdir) {
443 dent = readdir(curdir);
444 while (dent) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600445 /* look for wrappers starting with "libXGLlayer" */
446 if (!strncmp(dent->d_name, "libXGLLayer", strlen("libXGLLayer"))) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600447 void * handle;
448 snprintf(temp_str, sizeof(temp_str), "%s/%s",p,dent->d_name);
Jon Ashburn2ae02262015-01-16 08:46:38 -0700449 if ((handle = dlopen(temp_str, RTLD_LAZY)) == NULL) {
450 dent = readdir(curdir);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600451 continue;
Jon Ashburn2ae02262015-01-16 08:46:38 -0700452 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600453 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
454 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
455 break;
456 }
457 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
458 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
459 break;
460 }
461 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
462 loader.scanned_layer_count++;
463 dlclose(handle);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600464 }
465
466 dent = readdir(curdir);
467 }
468 closedir(curdir);
469 }
470 }
471
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600472 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600473}
474
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600475static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600476{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800477 loader_initialize_dispatch_table(tab, fpGPA, gpu);
478
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600479 if (tab->EnumerateLayers == NULL)
480 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600481}
482
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600483static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600484{
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700485 for (struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600486 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600487 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600488 *gpu_index = i;
489 return icd;
490 }
491 }
492 return NULL;
493}
494
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600495static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600496{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600497 if (icd->layer_count[gpu_index])
498 return true;
499 else
500 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600501}
502
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600503static 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 -0600504{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600505 if (!icd)
506 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600507
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600508 struct loader_layers *obj;
509 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600510 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600511 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600512 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700513 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 -0600514 foundLib = true;
515 break;
516 }
517 }
518 if (!foundLib) {
519 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700520 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
521 obj->name[sizeof(obj->name) - 1] = '\0';
522 if ((obj->lib_handle = dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
523 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to open layer library %s got error %d", pLayerNames[i].lib_name, dlerror());
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600524 continue;
525 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700526 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 -0600527 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700528 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600529 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600530 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600531 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600532}
533
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600534static 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 -0700535{
536 void *handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600537 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600538 char layer_buf[16][256];
539 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700540
541 for (int i = 0; i < 16; i++)
542 layers[i] = &layer_buf[i][0];
543
544 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
545 *lib_name = loader.scanned_layer_names[j];
546 if ((handle = dlopen(*lib_name, RTLD_LAZY)) == NULL)
547 continue;
548 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
549 //use default layer name based on library name libXGLLayer<name>.so
550 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
551 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), "libXGLLayer%s.so", layer_name);
552 dlclose(handle);
553 if (!strcmp(basename(*lib_name), lib_str)) {
554 free(lib_str);
555 return true;
556 }
557 else {
558 free(lib_str);
559 continue;
560 }
561 }
562 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600563 size_t cnt;
564 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (void *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700565 for (unsigned int i = 0; i < cnt; i++) {
566 if (!strcmp((char *) layers[i], layer_name)) {
567 dlclose(handle);
568 return true;
569 }
570 }
571 }
572
573 dlclose(handle);
574 }
575
576 return false;
577}
578
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600579static 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 -0600580{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600581 const char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600582 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600583 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600584
Jon Ashburnb8358052014-11-18 09:06:04 -0700585 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn3fb55442014-10-23 10:29:09 -0600586 if (!layerEnv)
587 return 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600588 p = malloc(strlen(layerEnv) + 1);
589 if (!p)
590 return 0;
591 strcpy(p, layerEnv);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600592 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600593
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600594 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700595 const char *lib_name = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600596 next = strchr(p, ':');
597 if (next == NULL) {
598 len = strlen(p);
599 next = p + len;
600 }
601 else {
602 len = next - p;
603 *(char *) next = '\0';
604 next++;
605 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600606 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700607 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600608 p = next;
609 continue;
610 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600611
Jon Ashburnb8358052014-11-18 09:06:04 -0700612 len = strlen(name);
613 pLayerNames[count].layer_name = malloc(len + 1);
614 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600615 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600616 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600617 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700618 strncpy((char *) pLayerNames[count].layer_name, name, len);
619 pLayerNames[count].layer_name[len] = '\0';
620 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600621 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600622 p = next;
623
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600624 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600625
Jon Ashburnd09bd102014-10-22 21:15:26 -0600626 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600627 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600628}
629
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600630static 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 -0600631{
Jon Ashburnb8358052014-11-18 09:06:04 -0700632 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600633
634 *ppLayerNames = &layerNames[0];
635 if (!pCreateInfo) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700636 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600637 }
638
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800639 const XGL_LAYER_CREATE_INFO *pCi =
640 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600641
642 while (pCi) {
643 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
644 const char *name;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600645 uint32_t len;
646 for (uint32_t i = 0; i < pCi->layerCount; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700647 const char * lib_name = NULL;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800648 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnb8358052014-11-18 09:06:04 -0700649 if (!find_layer_name(icd, gpu_index, name, &lib_name))
650 return loader_get_layer_env(icd, gpu_index, layerNames);
651 len = strlen(name);
652 layerNames[i].layer_name = malloc(len + 1);
653 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600654 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700655 strncpy((char *) layerNames[i].layer_name, name, len);
656 layerNames[i].layer_name[len] = '\0';
657 layerNames[i].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600658 }
Courtney Goeltzenleuchter61e10b62015-01-07 10:17:44 -0700659 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600660 }
661 pCi = pCi->pNext;
662 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700663 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600664}
665
666static void loader_deactivate_layer()
667{
668 struct loader_icd *icd;
669 struct loader_layers *libs;
670
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700671 for (icd = loader.instances->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600672 if (icd->gpus)
673 free(icd->gpus);
674 icd->gpus = NULL;
675 if (icd->loader_dispatch)
676 free(icd->loader_dispatch);
677 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600678 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600679 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600680 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600681 libs = &(icd->layer_libs[j][i]);
682 if (libs->lib_handle)
683 dlclose(libs->lib_handle);
684 libs->lib_handle = NULL;
685 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600686 if (icd->wrappedGpus[j])
687 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600688 }
689 icd->layer_count[j] = 0;
690 }
691 icd->gpu_count = 0;
692 }
693}
694
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600695extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600696{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600697 uint32_t gpu_index;
698 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700699 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600700 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600701
702 if (!icd)
703 return 0;
704 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
705
706 /* activate any layer libraries */
707 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600708 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
709 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600710 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600711
Jon Ashburnb8358052014-11-18 09:06:04 -0700712 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600713 if (!count)
714 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700715 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600716
Jon Ashburnd09bd102014-10-22 21:15:26 -0600717 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
718 if (! icd->wrappedGpus[gpu_index])
719 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600720 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600721 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600722 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600723 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600724 nextGpuObj->nextObject = gpuObj;
725 gpuObj = nextGpuObj;
726
Jon Ashburn79113cc2014-12-01 14:22:40 -0700727 char funcStr[256];
728 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
729 if ((nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
730 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600731 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700732 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 -0600733 continue;
734 }
735
Jon Ashburnf2610012014-10-24 15:48:55 -0600736 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600737 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600738 //Insert the new wrapped objects into the list with loader object at head
739 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
740 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
741 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
742 gpuObj->nextObject = baseObj;
743 gpuObj->pGPA = icd->GetProcAddr;
744 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600745
746 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600747 }
748 else {
749 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700750 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600751 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700752 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600753 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
754 break;
755 }
756 }
757 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700758 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600759 }
760 }
761 return icd->layer_count[gpu_index];
762}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600763
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700764LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
765 const XGL_APPLICATION_INFO* pAppInfo,
766 const XGL_ALLOC_CALLBACKS* pAllocCb,
767 XGL_INSTANCE* pInstance)
768{
769 struct loader_instance *ptr_instance = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600770
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700771 //TODO does this XGL_INSTANCE really have to be a dispatchable object??
772 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
773 if (ptr_instance == NULL) {
774 return XGL_ERROR_OUT_OF_MEMORY;
775 }
776 memset(ptr_instance, 0, sizeof(struct loader_instance));
777
778 ptr_instance->app_info = pAppInfo;
779 ptr_instance->alloc_cb = pAllocCb;
780 ptr_instance->next = loader.instances;
781 loader.instances = ptr_instance;
782
783 *pInstance = (XGL_INSTANCE) ptr_instance;
784 // FIXME/TODO/TBD: WHAT ELSE NEEDS TO BE DONE?
785
786 return XGL_SUCCESS;
787}
788
789LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
790 XGL_INSTANCE instance)
791{
792 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
793
794 // Remove this instance from the list of instances:
795 struct loader_instance *prev = NULL;
796 struct loader_instance *next = loader.instances;
797 while (next != NULL) {
798 if (next == ptr_instance) {
799 // Remove this instance from the list:
800 if (prev)
801 prev->next = next->next;
802 break;
803 }
804 prev = next;
805 next = next->next;
806 }
807 if (next == NULL) {
808 // This must be an invalid instance handle or empty list
809 return XGL_ERROR_INVALID_HANDLE;
810 }
811
812 // FIXME/TODO/TBD: WHAT ELSE NEEDS TO BE DONE HERE???
813
814 // Now, remove the instance:
815 free(ptr_instance);
816
817 return XGL_SUCCESS;
818}
819
820LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
821
822 XGL_INSTANCE instance,
823 uint32_t maxGpus,
824 uint32_t* pGpuCount,
825 XGL_PHYSICAL_GPU* pGpus)
826{
827 // FIXME/TODO/TBD: WHAT ELSE NEEDS TO BE DONE HERE???
828 return XGL_SUCCESS;
829}
830
831LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
832{
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600833 if (gpu == NULL)
834 return NULL;
Jon Ashburn891537f2014-10-22 12:42:13 -0600835 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -0600836 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800837 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600838
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600839 if (disp_table == NULL)
840 return NULL;
841
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800842 addr = loader_lookup_dispatch_table(disp_table, pName);
843 if (addr)
844 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600845 else {
Jon Ashburnf2610012014-10-24 15:48:55 -0600846 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600847 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -0600848 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600849 }
850}
851
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600852LOADER_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO* pAppInfo, const XGL_ALLOC_CALLBACKS* pAllocCb, uint32_t maxGpus, uint32_t* pGpuCount, XGL_PHYSICAL_GPU* pGpus)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800853{
854 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600855 struct loader_icd *icd;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600856 uint32_t count = 0;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800857 XGL_RESULT res;
858
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700859 // for now only one instance
860 if (loader.instances == NULL) {
861 loader.instances = malloc(sizeof(struct loader_instance));
862 if (loader.instances == NULL)
863 return XGL_ERROR_UNAVAILABLE;
864 memset(loader.instances, 0, sizeof(struct loader_instance));
865 }
866
867
Jon Ashburndf7d5842014-10-16 15:48:50 -0600868 // cleanup any prior layer initializations
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600869 loader_deactivate_layer();
Jon Ashburndf7d5842014-10-16 15:48:50 -0600870
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800871 pthread_once(&once, loader_icd_scan);
872
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700873 if (!loader.instances->icds)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800874 return XGL_ERROR_UNAVAILABLE;
875
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700876 icd = loader.instances->icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800877 while (icd) {
878 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600879 XGL_BASE_LAYER_OBJECT * wrappedGpus;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600880 xglGetProcAddrType getProcAddr = icd->GetProcAddr;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600881 uint32_t n, max = maxGpus - count;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800882
883 if (max > XGL_MAX_PHYSICAL_GPUS) {
884 max = XGL_MAX_PHYSICAL_GPUS;
885 }
886
887 res = icd->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu7f44d352014-08-06 12:17:04 +0800888 if (res == XGL_SUCCESS && n) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600889 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600890 icd->gpus = wrappedGpus;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600891 icd->gpu_count = n;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600892 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600893 for (int i = 0; i < n; i++) {
894 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburndf7d5842014-10-16 15:48:50 -0600895 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600896 (wrappedGpus + i)->nextObject = gpus[i];
897 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn891537f2014-10-22 12:42:13 -0600898 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600899 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburndf7d5842014-10-16 15:48:50 -0600900 *disp = icd->loader_dispatch + i;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600901 }
902
Jon Ashburn27a24af2014-12-18 17:00:52 -0700903 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
904 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
905 loader_log(XGL_DBG_MSG_WARNING, 0,
906 "ICD ignored: failed to migrate settings");
907 loader_icd_destroy(icd);
908 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800909 count += n;
910
911 if (count >= maxGpus) {
912 break;
913 }
914 }
915
916 icd = icd->next;
917 }
918
Jon Ashburn27a24af2014-12-18 17:00:52 -0700919 /* we have nothing to log anymore */
920 loader_msg_callback_clear();
921
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600922 /* get layer libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600923 if (!loader.layer_scanned)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700924 layer_lib_scan(NULL);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600925
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800926 *pGpuCount = count;
927
928 return (count > 0) ? XGL_SUCCESS : res;
929}
930
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600931LOADER_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 -0600932{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600933 uint32_t gpu_index;
934 uint32_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -0700935 char *lib_name;
936 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
937 void *handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600938 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600939 char layer_buf[16][256];
940 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600941
Jon Ashburn1323eb72014-12-02 13:03:09 -0700942 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600943 return XGL_ERROR_INVALID_POINTER;
944
Jon Ashburn1323eb72014-12-02 13:03:09 -0700945 for (int i = 0; i < 16; i++)
946 layers[i] = &layer_buf[i][0];
947
948 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
949 lib_name = loader.scanned_layer_names[j];
950 if ((handle = dlopen(lib_name, RTLD_LAZY)) == NULL)
951 continue;
952 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
953 //use default layer name based on library name libXGLLayer<name>.so
954 char *pEnd, *cpyStr;
955 int siz;
956 dlclose(handle);
957 lib_name = basename(lib_name);
958 pEnd = strrchr(lib_name, '.');
959 siz = pEnd - lib_name - strlen("libXGLLayer") + 1;
960 if (pEnd == NULL || siz <= 0)
961 continue;
962 cpyStr = malloc(siz);
963 if (cpyStr == NULL) {
964 free(cpyStr);
965 continue;
966 }
967 strncpy(cpyStr, lib_name + strlen("libXGLLayer"), siz);
968 cpyStr[siz - 1] = '\0';
969 if (siz > maxStringSize)
970 siz = maxStringSize;
971 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
972 pOutLayers[count][siz - 1] = '\0';
973 count++;
974 free(cpyStr);
975 }
976 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600977 size_t cnt;
978 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -0700979 XGL_RESULT res;
980 n = (maxStringSize < 256) ? maxStringSize : 256;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600981 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (void *) icd->gpus + gpu_index);
Jon Ashburn1323eb72014-12-02 13:03:09 -0700982 dlclose(handle);
983 if (res != XGL_SUCCESS)
984 continue;
985 if (cnt + count > maxLayerCount)
986 cnt = maxLayerCount - count;
987 for (unsigned int i = count; i < cnt + count; i++) {
988 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
989 if (n > 0)
990 pOutLayers[i - count][n - 1] = '\0';
991 }
992 count += cnt;
993 }
994 }
995
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600996 *pOutLayerCount = count;
997
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600998 return XGL_SUCCESS;
999}
1000
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001001LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001002{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001003 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001004 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001005 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001006
1007 if (!loader.scanned) {
1008 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1009 }
1010
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001011 for (icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001012 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001013 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
1014 if (res != XGL_SUCCESS) {
1015 gpu_idx = i;
1016 break;
1017 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001018 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001019 if (res != XGL_SUCCESS)
1020 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001021 }
1022
1023 /* roll back on errors */
1024 if (icd) {
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001025 for (const struct loader_icd * tmp = loader.instances->icds; tmp != icd; tmp = tmp->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001026 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001027 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001028 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001029 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001030 for (uint32_t i = 0; i < gpu_idx; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001031 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001032
1033 return res;
1034 }
1035
1036 return XGL_SUCCESS;
1037}
1038
Chia-I Wu19300602014-08-04 08:03:57 +08001039LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001040{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001041 XGL_RESULT res = XGL_SUCCESS;
1042
1043 if (!loader.scanned) {
1044 return loader_msg_callback_remove(pfnMsgCallback);
1045 }
1046
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001047 for (const struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001048 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001049 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1050 if (r != XGL_SUCCESS) {
1051 res = r;
1052 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001053 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001054 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001055 return res;
1056}
1057
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001058LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001059{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001060 XGL_RESULT res = XGL_SUCCESS;
1061
1062 if (!loader.scanned) {
1063 if (dataSize == 0)
1064 return XGL_ERROR_INVALID_VALUE;
1065
1066 switch (dbgOption) {
1067 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1068 loader.debug_echo_enable = *((const bool *) pData);
1069 break;
1070 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1071 loader.break_on_error = *((const bool *) pData);
1072 break;
1073 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1074 loader.break_on_warning = *((const bool *) pData);
1075 break;
1076 default:
1077 res = XGL_ERROR_INVALID_VALUE;
1078 break;
1079 }
1080
1081 return res;
1082 }
1083
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001084 for (const struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001085 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001086 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
1087 /* unfortunately we cannot roll back */
1088 if (r != XGL_SUCCESS) {
1089 res = r;
1090 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001091 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001092 }
1093
1094 return res;
1095}