blob: 38d53328067270f1ceab50cb11129fab99987c4a [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 {
Jon Ashburn46d1f582015-01-28 11:01:35 -070064 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080065
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
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080073 struct loader_icd *next;
74};
75
Jon Ashburnd38bfb12014-10-14 19:15:22 -060076
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080077struct loader_msg_callback {
78 XGL_DBG_MSG_CALLBACK_FUNCTION func;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060079 void *data;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080080
81 struct loader_msg_callback *next;
82};
83
Jon Ashburn46d1f582015-01-28 11:01:35 -070084struct loader_scanned_icds {
85 void *handle;
86 xglGetProcAddrType GetProcAddr;
87 xglInitAndEnumerateGpusType InitAndEnumerateGpus;
88
89 struct loader_scanned_icds *next;
90};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060091
Jon Ashburn1beab2d2015-01-26 14:51:40 -070092// Note: Since the following is a static structure, all members are initialized
93// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080094static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070095 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -070096 bool icds_scanned;
97 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060098 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -070099 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600100 unsigned int scanned_layer_count;
101 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800102 struct loader_msg_callback *msg_callbacks;
103
104 bool debug_echo_enable;
105 bool break_on_error;
106 bool break_on_warning;
107} loader;
108
109static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600110 void *data)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800111{
112 struct loader_msg_callback *cb;
113
114 cb = malloc(sizeof(*cb));
115 if (!cb)
116 return XGL_ERROR_OUT_OF_MEMORY;
117
118 cb->func = func;
119 cb->data = data;
120
121 cb->next = loader.msg_callbacks;
122 loader.msg_callbacks = cb;
123
124 return XGL_SUCCESS;
125}
126
127static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
128{
129 struct loader_msg_callback *cb = loader.msg_callbacks;
130
131 /*
132 * Find the first match (last registered).
133 *
134 * XXX What if the same callback function is registered more than once?
135 */
136 while (cb) {
137 if (cb->func == func) {
138 break;
139 }
140
141 cb = cb->next;
142 }
143
144 if (!cb)
145 return XGL_ERROR_INVALID_POINTER;
146
147 free(cb);
148
149 return XGL_SUCCESS;
150}
151
152static void loader_msg_callback_clear(void)
153{
154 struct loader_msg_callback *cb = loader.msg_callbacks;
155
156 while (cb) {
157 struct loader_msg_callback *next = cb->next;
158 free(cb);
159 cb = next;
160 }
161
162 loader.msg_callbacks = NULL;
163}
164
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600165static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800166 const char *format, ...)
167{
168 const struct loader_msg_callback *cb = loader.msg_callbacks;
169 char msg[256];
170 va_list ap;
171 int ret;
172
173 va_start(ap, format);
174 ret = vsnprintf(msg, sizeof(msg), format, ap);
175 if (ret >= sizeof(msg) || ret < 0) {
176 msg[sizeof(msg) - 1] = '\0';
177 }
178 va_end(ap);
179
180 if (loader.debug_echo_enable || !cb) {
181 fputs(msg, stderr);
182 fputc('\n', stderr);
183 }
184
185 while (cb) {
186 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800187 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800188 cb = cb->next;
189 }
190
191 switch (msg_type) {
192 case XGL_DBG_MSG_ERROR:
193 if (loader.break_on_error) {
194 exit(1);
195 }
196 /* fall through */
197 case XGL_DBG_MSG_WARNING:
198 if (loader.break_on_warning) {
199 exit(1);
200 }
201 break;
202 default:
203 break;
204 }
205}
206
207static void
208loader_icd_destroy(struct loader_icd *icd)
209{
Jon Ashburn46d1f582015-01-28 11:01:35 -0700210 dlclose(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800211 free(icd);
212}
213
214static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700215loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800216{
217 struct loader_icd *icd;
218
219 icd = malloc(sizeof(*icd));
220 if (!icd)
221 return NULL;
222
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600223 memset(icd, 0, sizeof(*icd));
224
Jon Ashburn46d1f582015-01-28 11:01:35 -0700225 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800226
227 return icd;
228}
229
230static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
231{
232 const struct loader_msg_callback *cb = loader.msg_callbacks;
233 XGL_RESULT res;
234
235 while (cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600236 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700237 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
238 if (res != XGL_SUCCESS) {
239 break;
240 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800241 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242 cb = cb->next;
243 }
244
245 /* roll back on errors */
246 if (cb) {
247 const struct loader_msg_callback *tmp = loader.msg_callbacks;
248
249 while (tmp != cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600250 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700251 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
252 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800253 tmp = tmp->next;
254 }
255
256 return res;
257 }
258
259 return XGL_SUCCESS;
260}
261
262static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
263{
264#define SETB(icd, opt, val) do { \
265 if (val) { \
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600266 for (uint32_t i = 0; i < icd->gpu_count; i++) { \
Jon Ashburn01e2d662014-11-14 09:52:42 -0700267 const XGL_RESULT res = \
268 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
269 if (res != XGL_SUCCESS) \
270 return res; \
271 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800272 } \
273} while (0)
274 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
275 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
276 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
277#undef SETB
278
279return XGL_SUCCESS;
280}
281
Jon Ashburn46d1f582015-01-28 11:01:35 -0700282static struct loader_icd *loader_icd_add(const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800283{
284 struct loader_icd *icd;
285
Jon Ashburn46d1f582015-01-28 11:01:35 -0700286 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800287 if (!icd)
288 return NULL;
289
Chia-I Wu13a61a52014-08-04 11:18:20 +0800290 /* prepend to the list */
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700291 icd->next = loader.instances->icds;
292 loader.instances->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800293
294 return icd;
295}
296
Jon Ashburn46d1f582015-01-28 11:01:35 -0700297static void loader_scanned_icd_add(const char *filename)
298{
299 void *handle;
300 void *fp_gpa, *fp_init;
301 struct loader_scanned_icds *new_node;
302
303 handle = dlopen(filename, RTLD_LAZY);
304 if (!handle) {
305 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror());
306 return;
307 }
308
309#define LOOKUP(func_ptr, func) do { \
310 func_ptr = (xgl ##func## Type) dlsym(handle, "xgl" #func); \
311 if (!func_ptr) { \
312 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror()); \
313 return; \
314 } \
315} while (0)
316
317 LOOKUP(fp_gpa, GetProcAddr);
318 LOOKUP(fp_init, InitAndEnumerateGpus);
319#undef LOOKUP
320
321 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
322 if (!new_node) {
323 loader_log(XGL_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
324 return;
325 }
326
327 new_node->handle = handle;
328 new_node->GetProcAddr = fp_gpa;
329 new_node->InitAndEnumerateGpus = fp_init;
330 new_node->next = loader.scanned_icd_list;
331 loader.scanned_icd_list = new_node;
332}
333
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600334#ifndef DEFAULT_XGL_DRIVERS_PATH
335// TODO: Is this a good default location?
336// Need to search for both 32bit and 64bit ICDs
337#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
338#endif
339
340/**
341 * Try to \c loader_icd_scan XGL driver(s).
342 *
343 * This function scans the default system path or path
344 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
345 * order to find loadable XGL ICDs with the name of libXGL_*.
346 *
347 * \returns
348 * void; but side effect is to set loader_icd_scanned to true
349 */
350static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800351{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600352 const char *libPaths, *p, *next;
353 DIR *sysdir;
354 struct dirent *dent;
355 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600356 char path[1024];
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600357 int len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800358
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600359 libPaths = NULL;
360 if (geteuid() == getuid()) {
361 /* don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
362 libPaths = getenv("LIBXGL_DRIVERS_PATH");
363 }
364 if (libPaths == NULL)
365 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800366
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600367 for (p = libPaths; *p; p = next) {
368 next = strchr(p, ':');
369 if (next == NULL) {
370 len = strlen(p);
371 next = p + len;
372 }
373 else {
374 len = next - p;
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600375 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
376 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600377 next++;
378 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800379
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600380 sysdir = opendir(p);
381 if (sysdir) {
382 dent = readdir(sysdir);
383 while (dent) {
384 /* look for ICDs starting with "libXGL_" */
385 if (!strncmp(dent->d_name, "libXGL_", 7)) {
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600386 snprintf(icd_library, 1024, "%s/%s",p,dent->d_name);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700387
388 loader_scanned_icd_add(icd_library);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600389 }
390
391 dent = readdir(sysdir);
392 }
393 closedir(sysdir);
394 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800395 }
396
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800397
Jon Ashburn46d1f582015-01-28 11:01:35 -0700398 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800399}
400
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600401#ifndef DEFAULT_XGL_LAYERS_PATH
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700402// TODO: Are these good default locations?
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600403#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
404#endif
405
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700406static void layer_lib_scan(const char * libInPaths)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600407{
408 const char *p, *next;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700409 char *libPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600410 DIR *curdir;
411 struct dirent *dent;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600412 int len, i;
413 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600414
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700415 len = 0;
416 loader.layer_dirs = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600417 if (libInPaths){
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700418 len = strlen(libInPaths);
419 p = libInPaths;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600420 }
421 else {
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700422 if (geteuid() == getuid()) {
423 p = getenv("LIBXGL_LAYERS_PATH");
424 if (p != NULL)
425 len = strlen(p);
426 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600427 }
428
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700429 if (len == 0) {
430 len = strlen(DEFAULT_XGL_LAYERS_PATH);
431 p = DEFAULT_XGL_LAYERS_PATH;
432 }
433
434 if (len == 0) {
435 // Have no paths to search
436 return;
437 }
438 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700439 if (loader.layer_dirs == NULL)
440 return;
441
442 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
443 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700444 libPaths = loader.layer_dirs;
445
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600446 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600447 for (i = 0; i < loader.scanned_layer_count; i++) {
448 if (loader.scanned_layer_names[i] != NULL)
449 free(loader.scanned_layer_names[i]);
450 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600451 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600452 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600453
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600454 for (p = libPaths; *p; p = next) {
455 next = strchr(p, ':');
456 if (next == NULL) {
457 len = strlen(p);
458 next = p + len;
459 }
460 else {
461 len = next - p;
462 *(char *) next = '\0';
463 next++;
464 }
465
466 curdir = opendir(p);
467 if (curdir) {
468 dent = readdir(curdir);
469 while (dent) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600470 /* look for wrappers starting with "libXGLlayer" */
471 if (!strncmp(dent->d_name, "libXGLLayer", strlen("libXGLLayer"))) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600472 void * handle;
473 snprintf(temp_str, sizeof(temp_str), "%s/%s",p,dent->d_name);
Jon Ashburn2ae02262015-01-16 08:46:38 -0700474 if ((handle = dlopen(temp_str, RTLD_LAZY)) == NULL) {
475 dent = readdir(curdir);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600476 continue;
Jon Ashburn2ae02262015-01-16 08:46:38 -0700477 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600478 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
479 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
480 break;
481 }
482 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
483 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
484 break;
485 }
486 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
487 loader.scanned_layer_count++;
488 dlclose(handle);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600489 }
490
491 dent = readdir(curdir);
492 }
493 closedir(curdir);
494 }
495 }
496
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600497 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600498}
499
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600500static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600501{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800502 loader_initialize_dispatch_table(tab, fpGPA, gpu);
503
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600504 if (tab->EnumerateLayers == NULL)
505 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600506}
507
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600508static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600509{
Jon Ashburn46d1f582015-01-28 11:01:35 -0700510 if (!loader.instances)
511 return NULL;
512
513 //TODO go through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700514 for (struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600515 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600516 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600517 *gpu_index = i;
518 return icd;
519 }
520 }
521 return NULL;
522}
523
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600524static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600525{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600526 if (icd->layer_count[gpu_index])
527 return true;
528 else
529 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600530}
531
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600532static 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 -0600533{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600534 if (!icd)
535 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600536
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600537 struct loader_layers *obj;
538 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600539 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600540 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600541 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700542 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 -0600543 foundLib = true;
544 break;
545 }
546 }
547 if (!foundLib) {
548 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700549 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
550 obj->name[sizeof(obj->name) - 1] = '\0';
551 if ((obj->lib_handle = dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
552 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 -0600553 continue;
554 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700555 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 -0600556 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700557 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600558 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600559 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600560 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600561}
562
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600563static 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 -0700564{
565 void *handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600566 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600567 char layer_buf[16][256];
568 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700569
570 for (int i = 0; i < 16; i++)
571 layers[i] = &layer_buf[i][0];
572
573 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
574 *lib_name = loader.scanned_layer_names[j];
575 if ((handle = dlopen(*lib_name, RTLD_LAZY)) == NULL)
576 continue;
577 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
578 //use default layer name based on library name libXGLLayer<name>.so
579 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
580 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), "libXGLLayer%s.so", layer_name);
581 dlclose(handle);
582 if (!strcmp(basename(*lib_name), lib_str)) {
583 free(lib_str);
584 return true;
585 }
586 else {
587 free(lib_str);
588 continue;
589 }
590 }
591 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600592 size_t cnt;
593 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (void *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700594 for (unsigned int i = 0; i < cnt; i++) {
595 if (!strcmp((char *) layers[i], layer_name)) {
596 dlclose(handle);
597 return true;
598 }
599 }
600 }
601
602 dlclose(handle);
603 }
604
605 return false;
606}
607
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600608static 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 -0600609{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600610 const char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600611 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600612 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600613
Jon Ashburnb8358052014-11-18 09:06:04 -0700614 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn3fb55442014-10-23 10:29:09 -0600615 if (!layerEnv)
616 return 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600617 p = malloc(strlen(layerEnv) + 1);
618 if (!p)
619 return 0;
620 strcpy(p, layerEnv);
Jon Ashburnd09bd102014-10-22 21:15:26 -0600621 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600622
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600623 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700624 const char *lib_name = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600625 next = strchr(p, ':');
626 if (next == NULL) {
627 len = strlen(p);
628 next = p + len;
629 }
630 else {
631 len = next - p;
632 *(char *) next = '\0';
633 next++;
634 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600635 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700636 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600637 p = next;
638 continue;
639 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600640
Jon Ashburnb8358052014-11-18 09:06:04 -0700641 len = strlen(name);
642 pLayerNames[count].layer_name = malloc(len + 1);
643 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600644 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600645 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600646 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700647 strncpy((char *) pLayerNames[count].layer_name, name, len);
648 pLayerNames[count].layer_name[len] = '\0';
649 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600650 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600651 p = next;
652
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600653 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600654
Jon Ashburnd09bd102014-10-22 21:15:26 -0600655 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600656 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600657}
658
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600659static 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 -0600660{
Jon Ashburnb8358052014-11-18 09:06:04 -0700661 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600662
663 *ppLayerNames = &layerNames[0];
664 if (!pCreateInfo) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700665 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600666 }
667
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800668 const XGL_LAYER_CREATE_INFO *pCi =
669 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600670
671 while (pCi) {
672 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
673 const char *name;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600674 uint32_t len;
675 for (uint32_t i = 0; i < pCi->layerCount; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700676 const char * lib_name = NULL;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800677 name = *(pCi->ppActiveLayerNames + i);
Jon Ashburnb8358052014-11-18 09:06:04 -0700678 if (!find_layer_name(icd, gpu_index, name, &lib_name))
679 return loader_get_layer_env(icd, gpu_index, layerNames);
680 len = strlen(name);
681 layerNames[i].layer_name = malloc(len + 1);
682 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600683 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700684 strncpy((char *) layerNames[i].layer_name, name, len);
685 layerNames[i].layer_name[len] = '\0';
686 layerNames[i].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600687 }
Courtney Goeltzenleuchter61e10b62015-01-07 10:17:44 -0700688 return pCi->layerCount + loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600689 }
690 pCi = pCi->pNext;
691 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700692 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600693}
694
Jon Ashburn46d1f582015-01-28 11:01:35 -0700695static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600696{
697 struct loader_icd *icd;
698 struct loader_layers *libs;
699
Jon Ashburn46d1f582015-01-28 11:01:35 -0700700 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600701 if (icd->gpus)
702 free(icd->gpus);
703 icd->gpus = NULL;
704 if (icd->loader_dispatch)
705 free(icd->loader_dispatch);
706 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600707 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600708 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600709 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600710 libs = &(icd->layer_libs[j][i]);
711 if (libs->lib_handle)
712 dlclose(libs->lib_handle);
713 libs->lib_handle = NULL;
714 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600715 if (icd->wrappedGpus[j])
716 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600717 }
718 icd->layer_count[j] = 0;
719 }
720 icd->gpu_count = 0;
721 }
722}
723
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600724extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600725{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600726 uint32_t gpu_index;
727 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700728 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600729 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600730
731 if (!icd)
732 return 0;
733 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
734
735 /* activate any layer libraries */
736 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600737 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
738 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600739 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600740
Jon Ashburnb8358052014-11-18 09:06:04 -0700741 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600742 if (!count)
743 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700744 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600745
Jon Ashburnd09bd102014-10-22 21:15:26 -0600746 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
747 if (! icd->wrappedGpus[gpu_index])
748 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600749 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600750 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600751 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600752 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600753 nextGpuObj->nextObject = gpuObj;
754 gpuObj = nextGpuObj;
755
Jon Ashburn79113cc2014-12-01 14:22:40 -0700756 char funcStr[256];
757 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
758 if ((nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
759 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600760 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700761 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 -0600762 continue;
763 }
764
Jon Ashburnf2610012014-10-24 15:48:55 -0600765 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600766 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600767 //Insert the new wrapped objects into the list with loader object at head
768 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
769 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
770 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
771 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700772 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600773 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600774
775 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600776 }
777 else {
778 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700779 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600780 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700781 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600782 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
783 break;
784 }
785 }
786 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700787 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600788 }
789 }
790 return icd->layer_count[gpu_index];
791}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600792
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700793LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
794 const XGL_APPLICATION_INFO* pAppInfo,
795 const XGL_ALLOC_CALLBACKS* pAllocCb,
796 XGL_INSTANCE* pInstance)
797{
798 struct loader_instance *ptr_instance = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600799
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700800 //TODO does this XGL_INSTANCE really have to be a dispatchable object??
801 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
802 if (ptr_instance == NULL) {
803 return XGL_ERROR_OUT_OF_MEMORY;
804 }
805 memset(ptr_instance, 0, sizeof(struct loader_instance));
806
807 ptr_instance->app_info = pAppInfo;
808 ptr_instance->alloc_cb = pAllocCb;
809 ptr_instance->next = loader.instances;
810 loader.instances = ptr_instance;
811
812 *pInstance = (XGL_INSTANCE) ptr_instance;
813 // FIXME/TODO/TBD: WHAT ELSE NEEDS TO BE DONE?
814
815 return XGL_SUCCESS;
816}
817
818LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
819 XGL_INSTANCE instance)
820{
821 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
822
823 // Remove this instance from the list of instances:
824 struct loader_instance *prev = NULL;
825 struct loader_instance *next = loader.instances;
826 while (next != NULL) {
827 if (next == ptr_instance) {
828 // Remove this instance from the list:
829 if (prev)
830 prev->next = next->next;
831 break;
832 }
833 prev = next;
834 next = next->next;
835 }
836 if (next == NULL) {
837 // This must be an invalid instance handle or empty list
838 return XGL_ERROR_INVALID_HANDLE;
839 }
840
841 // FIXME/TODO/TBD: WHAT ELSE NEEDS TO BE DONE HERE???
842
843 // Now, remove the instance:
844 free(ptr_instance);
845
846 return XGL_SUCCESS;
847}
848
849LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
850
851 XGL_INSTANCE instance,
852 uint32_t maxGpus,
853 uint32_t* pGpuCount,
854 XGL_PHYSICAL_GPU* pGpus)
855{
856 // FIXME/TODO/TBD: WHAT ELSE NEEDS TO BE DONE HERE???
857 return XGL_SUCCESS;
858}
859
860LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
861{
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600862 if (gpu == NULL)
863 return NULL;
Jon Ashburn891537f2014-10-22 12:42:13 -0600864 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -0600865 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800866 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600867
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600868 if (disp_table == NULL)
869 return NULL;
870
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800871 addr = loader_lookup_dispatch_table(disp_table, pName);
872 if (addr)
873 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600874 else {
Jon Ashburnf2610012014-10-24 15:48:55 -0600875 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600876 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -0600877 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600878 }
879}
880
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600881LOADER_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 +0800882{
883 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600884 struct loader_icd *icd;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600885 uint32_t count = 0;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800886 XGL_RESULT res;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700887 struct loader_scanned_icds *scanned_icds;
888
889 pthread_once(&once, loader_icd_scan);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800890
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700891 // for now only one instance
892 if (loader.instances == NULL) {
893 loader.instances = malloc(sizeof(struct loader_instance));
894 if (loader.instances == NULL)
895 return XGL_ERROR_UNAVAILABLE;
896 memset(loader.instances, 0, sizeof(struct loader_instance));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700897
898 scanned_icds = loader.scanned_icd_list;
899 while (scanned_icds) {
900 loader_icd_add(scanned_icds);
901 scanned_icds = scanned_icds->next;
902 }
903
904 if (loader.instances->icds == NULL)
905 return XGL_ERROR_UNAVAILABLE;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700906 }
Jon Ashburn46d1f582015-01-28 11:01:35 -0700907
Jon Ashburndf7d5842014-10-16 15:48:50 -0600908 // cleanup any prior layer initializations
Jon Ashburn46d1f582015-01-28 11:01:35 -0700909 loader_deactivate_layer(loader.instances);
Jon Ashburndf7d5842014-10-16 15:48:50 -0600910
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800911
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700912 icd = loader.instances->icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800913 while (icd) {
914 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600915 XGL_BASE_LAYER_OBJECT * wrappedGpus;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700916 xglGetProcAddrType getProcAddr = icd->scanned_icds->GetProcAddr;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600917 uint32_t n, max = maxGpus - count;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800918
919 if (max > XGL_MAX_PHYSICAL_GPUS) {
920 max = XGL_MAX_PHYSICAL_GPUS;
921 }
922
Jon Ashburn46d1f582015-01-28 11:01:35 -0700923 res = icd->scanned_icds->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu7f44d352014-08-06 12:17:04 +0800924 if (res == XGL_SUCCESS && n) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600925 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600926 icd->gpus = wrappedGpus;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600927 icd->gpu_count = n;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600928 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600929 for (int i = 0; i < n; i++) {
930 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburndf7d5842014-10-16 15:48:50 -0600931 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600932 (wrappedGpus + i)->nextObject = gpus[i];
933 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn891537f2014-10-22 12:42:13 -0600934 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600935 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburndf7d5842014-10-16 15:48:50 -0600936 *disp = icd->loader_dispatch + i;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600937 }
938
Jon Ashburn27a24af2014-12-18 17:00:52 -0700939 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
940 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
941 loader_log(XGL_DBG_MSG_WARNING, 0,
942 "ICD ignored: failed to migrate settings");
943 loader_icd_destroy(icd);
944 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800945 count += n;
946
947 if (count >= maxGpus) {
948 break;
949 }
950 }
951
952 icd = icd->next;
953 }
954
Jon Ashburn27a24af2014-12-18 17:00:52 -0700955 /* we have nothing to log anymore */
956 loader_msg_callback_clear();
957
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600958 /* get layer libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600959 if (!loader.layer_scanned)
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700960 layer_lib_scan(NULL);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600961
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800962 *pGpuCount = count;
963
964 return (count > 0) ? XGL_SUCCESS : res;
965}
966
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600967LOADER_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 -0600968{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600969 uint32_t gpu_index;
970 uint32_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -0700971 char *lib_name;
972 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
973 void *handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600974 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600975 char layer_buf[16][256];
976 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600977
Jon Ashburn1323eb72014-12-02 13:03:09 -0700978 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600979 return XGL_ERROR_INVALID_POINTER;
980
Jon Ashburn46d1f582015-01-28 11:01:35 -0700981 if (!icd)
982 return XGL_ERROR_UNAVAILABLE;
983
Jon Ashburn1323eb72014-12-02 13:03:09 -0700984 for (int i = 0; i < 16; i++)
985 layers[i] = &layer_buf[i][0];
986
987 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
988 lib_name = loader.scanned_layer_names[j];
989 if ((handle = dlopen(lib_name, RTLD_LAZY)) == NULL)
990 continue;
991 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
992 //use default layer name based on library name libXGLLayer<name>.so
993 char *pEnd, *cpyStr;
994 int siz;
995 dlclose(handle);
996 lib_name = basename(lib_name);
997 pEnd = strrchr(lib_name, '.');
998 siz = pEnd - lib_name - strlen("libXGLLayer") + 1;
999 if (pEnd == NULL || siz <= 0)
1000 continue;
1001 cpyStr = malloc(siz);
1002 if (cpyStr == NULL) {
1003 free(cpyStr);
1004 continue;
1005 }
1006 strncpy(cpyStr, lib_name + strlen("libXGLLayer"), siz);
1007 cpyStr[siz - 1] = '\0';
1008 if (siz > maxStringSize)
1009 siz = maxStringSize;
1010 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1011 pOutLayers[count][siz - 1] = '\0';
1012 count++;
1013 free(cpyStr);
1014 }
1015 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001016 size_t cnt;
1017 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001018 XGL_RESULT res;
1019 n = (maxStringSize < 256) ? maxStringSize : 256;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001020 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (void *) icd->gpus + gpu_index);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001021 dlclose(handle);
1022 if (res != XGL_SUCCESS)
1023 continue;
1024 if (cnt + count > maxLayerCount)
1025 cnt = maxLayerCount - count;
1026 for (unsigned int i = count; i < cnt + count; i++) {
1027 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1028 if (n > 0)
1029 pOutLayers[i - count][n - 1] = '\0';
1030 }
1031 count += cnt;
1032 }
1033 }
1034
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001035 *pOutLayerCount = count;
1036
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001037 return XGL_SUCCESS;
1038}
1039
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001040LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001041{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001042 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001043 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001044 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001045
Jon Ashburn46d1f582015-01-28 11:01:35 -07001046 //TODO fix for uncreated instances
1047 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001048 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1049 }
1050
Jon Ashburn46d1f582015-01-28 11:01:35 -07001051 //TODO go through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001052 for (icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001053 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001054 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
1055 if (res != XGL_SUCCESS) {
1056 gpu_idx = i;
1057 break;
1058 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001059 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001060 if (res != XGL_SUCCESS)
1061 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001062 }
1063
1064 /* roll back on errors */
1065 if (icd) {
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001066 for (const struct loader_icd * tmp = loader.instances->icds; tmp != icd; tmp = tmp->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001067 for (uint32_t i = 0; i < icd->gpu_count; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001068 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001069 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001070 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001071 for (uint32_t i = 0; i < gpu_idx; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001072 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001073
1074 return res;
1075 }
1076
1077 return XGL_SUCCESS;
1078}
1079
Chia-I Wu19300602014-08-04 08:03:57 +08001080LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001081{
Jon Ashburn46d1f582015-01-28 11:01:35 -07001082 XGL_RESULT res = XGL_SUCCESS;\
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001083
Jon Ashburn46d1f582015-01-28 11:01:35 -07001084 //TODO fix for uncreated instances
1085 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001086 return loader_msg_callback_remove(pfnMsgCallback);
1087 }
1088
Jon Ashburn46d1f582015-01-28 11:01:35 -07001089 //TODO loop through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001090 for (const struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001091 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001092 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1093 if (r != XGL_SUCCESS) {
1094 res = r;
1095 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001096 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001097 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001098 return res;
1099}
1100
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001101LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001102{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001103 XGL_RESULT res = XGL_SUCCESS;
1104
Jon Ashburn46d1f582015-01-28 11:01:35 -07001105 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001106 if (dataSize == 0)
1107 return XGL_ERROR_INVALID_VALUE;
1108
1109 switch (dbgOption) {
1110 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1111 loader.debug_echo_enable = *((const bool *) pData);
1112 break;
1113 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1114 loader.break_on_error = *((const bool *) pData);
1115 break;
1116 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1117 loader.break_on_warning = *((const bool *) pData);
1118 break;
1119 default:
1120 res = XGL_ERROR_INVALID_VALUE;
1121 break;
1122 }
1123
1124 return res;
1125 }
1126
Jon Ashburn46d1f582015-01-28 11:01:35 -07001127 //TODO loop through all instances
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001128 for (const struct loader_icd * icd = loader.instances->icds; icd; icd = icd->next) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001129 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -07001130 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
1131 /* unfortunately we cannot roll back */
1132 if (r != XGL_SUCCESS) {
1133 res = r;
1134 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001135 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001136 }
1137
1138 return res;
1139}