blob: 7229cf0a74d2837a9e46ffb33459ed1bc6af9f70 [file] [log] [blame]
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Jon Ashburn406a0fe2014-11-14 09:52:42 -070026 * Jon Ashburn <jon@lunarg.com>
Chia-I Wu44e42362014-09-02 08:32:09 +080027 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080028 */
Jon Ashburn183dfd02014-10-22 18:13:16 -060029#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <stdarg.h>
33#include <stdbool.h>
34#include <string.h>
35
Chia-I Wu894a1172014-08-04 11:18:20 +080036#include <sys/types.h>
37#include <dirent.h>
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -060038#include <unistd.h>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080039#include <dlfcn.h>
40#include <pthread.h>
Jon Ashburnd43f9b62014-10-14 19:15:22 -060041#include <assert.h>
Chia-I Wu468e3c32014-08-04 08:03:57 +080042#include "loader.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080043
Jon Ashburn183dfd02014-10-22 18:13:16 -060044struct loader_layers {
45 void *lib_handle;
Jon Ashburnead95c52014-11-18 09:06:04 -070046 char name[256];
47};
48
49struct layer_name_pair {
50 char *layer_name;
51 const char *lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -060052};
53
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080054struct loader_icd {
55 void *handle;
56
Jon Ashburnb55278a2014-10-17 15:09:07 -060057 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Jon Ashburn183dfd02014-10-22 18:13:16 -060058 XGL_UINT layer_count[XGL_MAX_PHYSICAL_GPUS];
59 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnb4d00532014-10-22 21:15:26 -060060 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnb55278a2014-10-17 15:09:07 -060061 XGL_UINT gpu_count;
Jon Ashburn183dfd02014-10-22 18:13:16 -060062 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburn815bddd2014-10-16 15:48:50 -060063
Jon Ashburnd43f9b62014-10-14 19:15:22 -060064 GetProcAddrType GetProcAddr;
65 InitAndEnumerateGpusType InitAndEnumerateGpus;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080066
67 struct loader_icd *next;
68};
69
Jon Ashburnd43f9b62014-10-14 19:15:22 -060070
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080071struct loader_msg_callback {
72 XGL_DBG_MSG_CALLBACK_FUNCTION func;
73 XGL_VOID *data;
74
75 struct loader_msg_callback *next;
76};
77
Jon Ashburnd43f9b62014-10-14 19:15:22 -060078
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080079static struct {
80 bool scanned;
81 struct loader_icd *icds;
Jon Ashburn183dfd02014-10-22 18:13:16 -060082 bool layer_scanned;
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -070083 char *layer_dirs;
Jon Ashburn183dfd02014-10-22 18:13:16 -060084 unsigned int scanned_layer_count;
85 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080086 struct loader_msg_callback *msg_callbacks;
87
88 bool debug_echo_enable;
89 bool break_on_error;
90 bool break_on_warning;
91} loader;
92
93static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
94 XGL_VOID *data)
95{
96 struct loader_msg_callback *cb;
97
98 cb = malloc(sizeof(*cb));
99 if (!cb)
100 return XGL_ERROR_OUT_OF_MEMORY;
101
102 cb->func = func;
103 cb->data = data;
104
105 cb->next = loader.msg_callbacks;
106 loader.msg_callbacks = cb;
107
108 return XGL_SUCCESS;
109}
110
111static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
112{
113 struct loader_msg_callback *cb = loader.msg_callbacks;
114
115 /*
116 * Find the first match (last registered).
117 *
118 * XXX What if the same callback function is registered more than once?
119 */
120 while (cb) {
121 if (cb->func == func) {
122 break;
123 }
124
125 cb = cb->next;
126 }
127
128 if (!cb)
129 return XGL_ERROR_INVALID_POINTER;
130
131 free(cb);
132
133 return XGL_SUCCESS;
134}
135
136static void loader_msg_callback_clear(void)
137{
138 struct loader_msg_callback *cb = loader.msg_callbacks;
139
140 while (cb) {
141 struct loader_msg_callback *next = cb->next;
142 free(cb);
143 cb = next;
144 }
145
146 loader.msg_callbacks = NULL;
147}
148
149static void loader_log(XGL_DBG_MSG_TYPE msg_type, XGL_INT msg_code,
150 const char *format, ...)
151{
152 const struct loader_msg_callback *cb = loader.msg_callbacks;
153 char msg[256];
154 va_list ap;
155 int ret;
156
157 va_start(ap, format);
158 ret = vsnprintf(msg, sizeof(msg), format, ap);
159 if (ret >= sizeof(msg) || ret < 0) {
160 msg[sizeof(msg) - 1] = '\0';
161 }
162 va_end(ap);
163
164 if (loader.debug_echo_enable || !cb) {
165 fputs(msg, stderr);
166 fputc('\n', stderr);
167 }
168
169 while (cb) {
170 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
171 msg_code, (const XGL_CHAR *) msg, cb->data);
172 cb = cb->next;
173 }
174
175 switch (msg_type) {
176 case XGL_DBG_MSG_ERROR:
177 if (loader.break_on_error) {
178 exit(1);
179 }
180 /* fall through */
181 case XGL_DBG_MSG_WARNING:
182 if (loader.break_on_warning) {
183 exit(1);
184 }
185 break;
186 default:
187 break;
188 }
189}
190
191static void
192loader_icd_destroy(struct loader_icd *icd)
193{
194 dlclose(icd->handle);
195 free(icd);
196}
197
198static struct loader_icd *
199loader_icd_create(const char *filename)
200{
201 struct loader_icd *icd;
202
203 icd = malloc(sizeof(*icd));
204 if (!icd)
205 return NULL;
206
Courtney Goeltzenleuchter6f928162014-10-28 10:29:27 -0600207 memset(icd, 0, sizeof(*icd));
208
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800209 icd->handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
210 if (!icd->handle) {
211 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror());
212 free(icd);
213 return NULL;
214 }
215
216#define LOOKUP(icd, func) do { \
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600217 icd->func = (func## Type) dlsym(icd->handle, "xgl" #func); \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800218 if (!icd->func) { \
219 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror()); \
220 loader_icd_destroy(icd); \
221 return NULL; \
222 } \
223} while (0)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600224 LOOKUP(icd, GetProcAddr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800225 LOOKUP(icd, InitAndEnumerateGpus);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800226#undef LOOKUP
227
228 return icd;
229}
230
231static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
232{
233 const struct loader_msg_callback *cb = loader.msg_callbacks;
234 XGL_RESULT res;
235
236 while (cb) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700237 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
238 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
239 if (res != XGL_SUCCESS) {
240 break;
241 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800243 cb = cb->next;
244 }
245
246 /* roll back on errors */
247 if (cb) {
248 const struct loader_msg_callback *tmp = loader.msg_callbacks;
249
250 while (tmp != cb) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700251 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
252 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
253 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800254 tmp = tmp->next;
255 }
256
257 return res;
258 }
259
260 return XGL_SUCCESS;
261}
262
263static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
264{
265#define SETB(icd, opt, val) do { \
266 if (val) { \
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700267 for (XGL_UINT i = 0; i < icd->gpu_count; i++) { \
268 const XGL_RESULT res = \
269 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
270 if (res != XGL_SUCCESS) \
271 return res; \
272 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800273 } \
274} while (0)
275 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
276 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
277 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
278#undef SETB
279
280return XGL_SUCCESS;
281}
282
Chia-I Wu894a1172014-08-04 11:18:20 +0800283static struct loader_icd *loader_icd_add(const char *filename)
284{
285 struct loader_icd *icd;
286
287 icd = loader_icd_create(filename);
288 if (!icd)
289 return NULL;
290
291 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
292 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
293 loader_log(XGL_DBG_MSG_WARNING, 0,
294 "%s ignored: failed to migrate settings", filename);
295 loader_icd_destroy(icd);
296 }
297
298 /* prepend to the list */
299 icd->next = loader.icds;
300 loader.icds = icd;
301
302 return icd;
303}
304
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600305#ifndef DEFAULT_XGL_DRIVERS_PATH
306// TODO: Is this a good default location?
307// Need to search for both 32bit and 64bit ICDs
308#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
309#endif
310
311/**
312 * Try to \c loader_icd_scan XGL driver(s).
313 *
314 * This function scans the default system path or path
315 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
316 * order to find loadable XGL ICDs with the name of libXGL_*.
317 *
318 * \returns
319 * void; but side effect is to set loader_icd_scanned to true
320 */
321static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800322{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600323 const char *libPaths, *p, *next;
324 DIR *sysdir;
325 struct dirent *dent;
326 char icd_library[1024];
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600327 char path[1024];
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600328 int len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800329
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600330 libPaths = NULL;
331 if (geteuid() == getuid()) {
332 /* don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
333 libPaths = getenv("LIBXGL_DRIVERS_PATH");
334 }
335 if (libPaths == NULL)
336 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800337
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600338 for (p = libPaths; *p; p = next) {
339 next = strchr(p, ':');
340 if (next == NULL) {
341 len = strlen(p);
342 next = p + len;
343 }
344 else {
345 len = next - p;
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600346 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
347 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600348 next++;
349 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800350
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600351 sysdir = opendir(p);
352 if (sysdir) {
353 dent = readdir(sysdir);
354 while (dent) {
355 /* look for ICDs starting with "libXGL_" */
356 if (!strncmp(dent->d_name, "libXGL_", 7)) {
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600357 snprintf(icd_library, 1024, "%s/%s",p,dent->d_name);
Chia-I Wu894a1172014-08-04 11:18:20 +0800358 loader_icd_add(icd_library);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600359 }
360
361 dent = readdir(sysdir);
362 }
363 closedir(sysdir);
364 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800365 }
366
367 /* we have nothing to log anymore */
368 loader_msg_callback_clear();
369
370 loader.scanned = true;
371}
372
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600373#ifndef DEFAULT_XGL_LAYERS_PATH
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700374// TODO: Are these good default locations?
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600375#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
376#endif
377
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700378static void layer_lib_scan(const char * libInPaths)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600379{
380 const char *p, *next;
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700381 char *libPaths;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600382 DIR *curdir;
383 struct dirent *dent;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600384 int len, i;
385 char temp_str[1024];
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600386
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700387 len = 0;
388 loader.layer_dirs = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600389 if (libInPaths){
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700390 len = strlen(libInPaths);
391 p = libInPaths;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600392 }
393 else {
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700394 if (geteuid() == getuid()) {
395 p = getenv("LIBXGL_LAYERS_PATH");
396 if (p != NULL)
397 len = strlen(p);
398 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600399 }
400
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700401 if (len == 0) {
402 len = strlen(DEFAULT_XGL_LAYERS_PATH);
403 p = DEFAULT_XGL_LAYERS_PATH;
404 }
405
406 if (len == 0) {
407 // Have no paths to search
408 return;
409 }
410 loader.layer_dirs = malloc(len+1);
Courtney Goeltzenleuchter688c74b2014-12-02 18:12:51 -0700411 if (loader.layer_dirs == NULL)
412 return;
413
414 // Alloc passed, so we know there is enough space to hold the string, don't need strncpy
415 strcpy(loader.layer_dirs, p);
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -0700416 libPaths = loader.layer_dirs;
417
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600418 /* cleanup any previously scanned libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -0600419 for (i = 0; i < loader.scanned_layer_count; i++) {
420 if (loader.scanned_layer_names[i] != NULL)
421 free(loader.scanned_layer_names[i]);
422 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600423 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600424 loader.scanned_layer_count = 0;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600425
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600426 for (p = libPaths; *p; p = next) {
427 next = strchr(p, ':');
428 if (next == NULL) {
429 len = strlen(p);
430 next = p + len;
431 }
432 else {
433 len = next - p;
434 *(char *) next = '\0';
435 next++;
436 }
437
438 curdir = opendir(p);
439 if (curdir) {
440 dent = readdir(curdir);
441 while (dent) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600442 /* look for wrappers starting with "libXGLlayer" */
443 if (!strncmp(dent->d_name, "libXGLLayer", strlen("libXGLLayer"))) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600444 void * handle;
445 snprintf(temp_str, sizeof(temp_str), "%s/%s",p,dent->d_name);
446 if ((handle = dlopen((const char *) temp_str, RTLD_LAZY)) == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600447 continue;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600448 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
449 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
450 break;
451 }
452 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
453 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
454 break;
455 }
456 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
457 loader.scanned_layer_count++;
458 dlclose(handle);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600459 }
460
461 dent = readdir(curdir);
462 }
463 closedir(curdir);
464 }
465 }
466
Jon Ashburn183dfd02014-10-22 18:13:16 -0600467 loader.layer_scanned = true;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600468}
469
Jon Ashburnb55278a2014-10-17 15:09:07 -0600470static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, GetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600471{
472 tab->GetProcAddr = fpGPA;
473 tab->InitAndEnumerateGpus = fpGPA(gpu, (const XGL_CHAR *) "xglInitAndEnumerateGpus");
474 tab->GetGpuInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetGpuInfo");
475 tab->CreateDevice = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDevice");
476 tab->DestroyDevice = fpGPA(gpu, (const XGL_CHAR *) "xglDestroyDevice");
477 tab->GetExtensionSupport = fpGPA(gpu, (const XGL_CHAR *) "xglGetExtensionSupport");
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600478 tab->EnumerateLayers = fpGPA(gpu, (const XGL_CHAR *) "xglEnumerateLayers");
479 if (tab->EnumerateLayers == NULL)
480 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600481 tab->GetDeviceQueue = fpGPA(gpu, (const XGL_CHAR *) "xglGetDeviceQueue");
482 tab->QueueSubmit = fpGPA(gpu, (const XGL_CHAR *) "xglQueueSubmit");
483 tab->QueueSetGlobalMemReferences = fpGPA(gpu, (const XGL_CHAR *) "xglQueueSetGlobalMemReferences");
484 tab->QueueWaitIdle = fpGPA(gpu, (const XGL_CHAR *) "xglQueueWaitIdle");
485 tab->DeviceWaitIdle = fpGPA(gpu, (const XGL_CHAR *) "xglDeviceWaitIdle");
486 tab->GetMemoryHeapCount = fpGPA(gpu, (const XGL_CHAR *) "xglGetMemoryHeapCount");
487 tab->GetMemoryHeapInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetMemoryHeapInfo");
488 tab->AllocMemory = fpGPA(gpu, (const XGL_CHAR *) "xglAllocMemory");
489 tab->FreeMemory = fpGPA(gpu, (const XGL_CHAR *) "xglFreeMemory");
490 tab->SetMemoryPriority = fpGPA(gpu, (const XGL_CHAR *) "xglSetMemoryPriority");
491 tab->MapMemory = fpGPA(gpu, (const XGL_CHAR *) "xglMapMemory");
492 tab->UnmapMemory = fpGPA(gpu, (const XGL_CHAR *) "xglUnmapMemory");
493 tab->PinSystemMemory = fpGPA(gpu, (const XGL_CHAR *) "xglPinSystemMemory");
494 tab->RemapVirtualMemoryPages = fpGPA(gpu, (const XGL_CHAR *) "xglRemapVirtualMemoryPages");
495 tab->GetMultiGpuCompatibility = fpGPA(gpu, (const XGL_CHAR *) "xglGetMultiGpuCompatibility");
496 tab->OpenSharedMemory = fpGPA(gpu, (const XGL_CHAR *) "xglOpenSharedMemory");
497 tab->OpenSharedQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglOpenSharedQueueSemaphore");
498 tab->OpenPeerMemory = fpGPA(gpu, (const XGL_CHAR *) "xglOpenPeerMemory");
499 tab->OpenPeerImage = fpGPA(gpu, (const XGL_CHAR *) "xglOpenPeerImage");
500 tab->DestroyObject = fpGPA(gpu, (const XGL_CHAR *) "xglDestroyObject");
501 tab->GetObjectInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetObjectInfo");
502 tab->BindObjectMemory = fpGPA(gpu, (const XGL_CHAR *) "xglBindObjectMemory");
503 tab->CreateFence = fpGPA(gpu, (const XGL_CHAR *) "xglCreateFence");
504 tab->GetFenceStatus = fpGPA(gpu, (const XGL_CHAR *) "xglGetFenceStatus");
505 tab->WaitForFences = fpGPA(gpu, (const XGL_CHAR *) "xglWaitForFences");
506 tab->CreateQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglCreateQueueSemaphore");
507 tab->SignalQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglSignalQueueSemaphore");
508 tab->WaitQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglWaitQueueSemaphore");
509 tab->CreateEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCreateEvent");
510 tab->GetEventStatus = fpGPA(gpu, (const XGL_CHAR *) "xglGetEventStatus");
511 tab->SetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglSetEvent");
512 tab->ResetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglResetEvent");
513 tab->CreateQueryPool = fpGPA(gpu, (const XGL_CHAR *) "xglCreateQueryPool");
514 tab->GetQueryPoolResults = fpGPA(gpu, (const XGL_CHAR *) "xglGetQueryPoolResults");
515 tab->GetFormatInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetFormatInfo");
516 tab->CreateImage = fpGPA(gpu, (const XGL_CHAR *) "xglCreateImage");
517 tab->GetImageSubresourceInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetImageSubresourceInfo");
518 tab->CreateImageView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateImageView");
519 tab->CreateColorAttachmentView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateColorAttachmentView");
520 tab->CreateDepthStencilView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDepthStencilView");
521 tab->CreateShader = fpGPA(gpu, (const XGL_CHAR *) "xglCreateShader");
522 tab->CreateGraphicsPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCreateGraphicsPipeline");
523 tab->CreateComputePipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCreateComputePipeline");
524 tab->StorePipeline = fpGPA(gpu, (const XGL_CHAR *) "xglStorePipeline");
525 tab->LoadPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglLoadPipeline");
526 tab->CreatePipelineDelta = fpGPA(gpu, (const XGL_CHAR *) "xglCreatePipelineDelta");
527 tab->CreateSampler = fpGPA(gpu, (const XGL_CHAR *) "xglCreateSampler");
528 tab->CreateDescriptorSet = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDescriptorSet");
529 tab->BeginDescriptorSetUpdate = fpGPA(gpu, (const XGL_CHAR *) "xglBeginDescriptorSetUpdate");
530 tab->EndDescriptorSetUpdate = fpGPA(gpu, (const XGL_CHAR *) "xglEndDescriptorSetUpdate");
531 tab->AttachSamplerDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachSamplerDescriptors");
532 tab->AttachImageViewDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachImageViewDescriptors");
533 tab->AttachMemoryViewDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachMemoryViewDescriptors");
534 tab->AttachNestedDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachNestedDescriptors");
535 tab->ClearDescriptorSetSlots = fpGPA(gpu, (const XGL_CHAR *) "xglClearDescriptorSetSlots");
536 tab->CreateViewportState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateViewportState");
537 tab->CreateRasterState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateRasterState");
538 tab->CreateMsaaState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateMsaaState");
539 tab->CreateColorBlendState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateColorBlendState");
540 tab->CreateDepthStencilState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDepthStencilState");
541 tab->CreateCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglCreateCommandBuffer");
542 tab->BeginCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglBeginCommandBuffer");
543 tab->EndCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglEndCommandBuffer");
544 tab->ResetCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglResetCommandBuffer");
545 tab->CmdBindPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindPipeline");
546 tab->CmdBindPipelineDelta = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindPipelineDelta");
547 tab->CmdBindStateObject = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindStateObject");
548 tab->CmdBindDescriptorSet = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindDescriptorSet");
549 tab->CmdBindDynamicMemoryView = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindDynamicMemoryView");
Chia-I Wu3b04af52014-11-08 10:48:20 +0800550 tab->CmdBindVertexData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindVertexData");
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600551 tab->CmdBindIndexData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindIndexData");
552 tab->CmdBindAttachments = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindAttachments");
553 tab->CmdPrepareMemoryRegions = fpGPA(gpu, (const XGL_CHAR *) "xglCmdPrepareMemoryRegions");
554 tab->CmdPrepareImages = fpGPA(gpu, (const XGL_CHAR *) "xglCmdPrepareImages");
555 tab->CmdDraw = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDraw");
556 tab->CmdDrawIndexed = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndexed");
557 tab->CmdDrawIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndirect");
558 tab->CmdDrawIndexedIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndexedIndirect");
559 tab->CmdDispatch = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDispatch");
560 tab->CmdDispatchIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDispatchIndirect");
561 tab->CmdCopyMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyMemory");
562 tab->CmdCopyImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyImage");
563 tab->CmdCopyMemoryToImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyMemoryToImage");
564 tab->CmdCopyImageToMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyImageToMemory");
565 tab->CmdCloneImageData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCloneImageData");
566 tab->CmdUpdateMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdUpdateMemory");
567 tab->CmdFillMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdFillMemory");
568 tab->CmdClearColorImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearColorImage");
569 tab->CmdClearColorImageRaw = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearColorImageRaw");
570 tab->CmdClearDepthStencil = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearDepthStencil");
571 tab->CmdResolveImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResolveImage");
572 tab->CmdSetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCmdSetEvent");
573 tab->CmdResetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResetEvent");
574 tab->CmdMemoryAtomic = fpGPA(gpu, (const XGL_CHAR *) "xglCmdMemoryAtomic");
575 tab->CmdBeginQuery = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBeginQuery");
576 tab->CmdEndQuery = fpGPA(gpu, (const XGL_CHAR *) "xglCmdEndQuery");
577 tab->CmdResetQueryPool = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResetQueryPool");
578 tab->CmdWriteTimestamp = fpGPA(gpu, (const XGL_CHAR *) "xglCmdWriteTimestamp");
579 tab->CmdInitAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdInitAtomicCounters");
580 tab->CmdLoadAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdLoadAtomicCounters");
581 tab->CmdSaveAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdSaveAtomicCounters");
582 tab->DbgSetValidationLevel = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetValidationLevel");
583 tab->DbgRegisterMsgCallback = fpGPA(gpu, (const XGL_CHAR *) "xglDbgRegisterMsgCallback");
584 tab->DbgUnregisterMsgCallback = fpGPA(gpu, (const XGL_CHAR *) "xglDbgUnregisterMsgCallback");
585 tab->DbgSetMessageFilter = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetMessageFilter");
586 tab->DbgSetObjectTag = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetObjectTag");
587 tab->DbgSetGlobalOption = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetGlobalOption");
588 tab->DbgSetDeviceOption = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetDeviceOption");
589 tab->CmdDbgMarkerBegin = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDbgMarkerBegin");
590 tab->CmdDbgMarkerEnd = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDbgMarkerEnd");
591 tab->WsiX11AssociateConnection = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11AssociateConnection");
592 tab->WsiX11GetMSC = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11GetMSC");
593 tab->WsiX11CreatePresentableImage = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11CreatePresentableImage");
594 tab->WsiX11QueuePresent = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11QueuePresent");
595}
596
Jon Ashburnb55278a2014-10-17 15:09:07 -0600597static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, XGL_UINT *gpu_index)
598{
599 for (struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
600 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600601 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburnb55278a2014-10-17 15:09:07 -0600602 *gpu_index = i;
603 return icd;
604 }
605 }
606 return NULL;
607}
608
Jon Ashburn183dfd02014-10-22 18:13:16 -0600609static bool loader_layers_activated(const struct loader_icd *icd, const XGL_UINT gpu_index)
Jon Ashburnb55278a2014-10-17 15:09:07 -0600610{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600611 if (icd->layer_count[gpu_index])
612 return true;
613 else
614 return false;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600615}
616
Jon Ashburnead95c52014-11-18 09:06:04 -0700617static void loader_init_layer_libs(struct loader_icd *icd, XGL_UINT gpu_index, struct layer_name_pair * pLayerNames, XGL_UINT count)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600618{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600619 if (!icd)
620 return;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600621
Jon Ashburn183dfd02014-10-22 18:13:16 -0600622 struct loader_layers *obj;
623 bool foundLib;
624 for (XGL_UINT i = 0; i < count; i++) {
625 foundLib = false;
626 for (XGL_UINT j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700627 if (icd->layer_libs[gpu_index][j].lib_handle && !strcmp(icd->layer_libs[gpu_index][j].name, (char *) pLayerNames[i].layer_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600628 foundLib = true;
629 break;
630 }
631 }
632 if (!foundLib) {
633 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnead95c52014-11-18 09:06:04 -0700634 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
635 obj->name[sizeof(obj->name) - 1] = '\0';
636 if ((obj->lib_handle = dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
637 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to open layer library %s got error %d", pLayerNames[i].lib_name, dlerror());
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600638 continue;
639 } else {
Jon Ashburnead95c52014-11-18 09:06:04 -0700640 loader_log(XGL_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s", pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600641 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700642 free(pLayerNames[i].layer_name);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600643 icd->layer_count[gpu_index]++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600644 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600645 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600646}
647
Jon Ashburnead95c52014-11-18 09:06:04 -0700648static bool find_layer_name(struct loader_icd *icd, XGL_UINT gpu_index, const char * layer_name, const char **lib_name)
649{
650 void *handle;
651 EnumerateLayersType fpEnumerateLayers;
652 XGL_CHAR layer_buf[16][256];
653 XGL_CHAR * layers[16];
654
655 for (int i = 0; i < 16; i++)
656 layers[i] = &layer_buf[i][0];
657
658 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
659 *lib_name = loader.scanned_layer_names[j];
660 if ((handle = dlopen(*lib_name, RTLD_LAZY)) == NULL)
661 continue;
662 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
663 //use default layer name based on library name libXGLLayer<name>.so
664 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
665 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), "libXGLLayer%s.so", layer_name);
666 dlclose(handle);
667 if (!strcmp(basename(*lib_name), lib_str)) {
668 free(lib_str);
669 return true;
670 }
671 else {
672 free(lib_str);
673 continue;
674 }
675 }
676 else {
677 XGL_SIZE cnt;
Jon Ashburn6847c2b2014-11-25 12:56:49 -0700678 fpEnumerateLayers(NULL, 16, 256, layers, &cnt, (XGL_VOID *) icd->gpus + gpu_index);
Jon Ashburnead95c52014-11-18 09:06:04 -0700679 for (unsigned int i = 0; i < cnt; i++) {
680 if (!strcmp((char *) layers[i], layer_name)) {
681 dlclose(handle);
682 return true;
683 }
684 }
685 }
686
687 dlclose(handle);
688 }
689
690 return false;
691}
692
693static XGL_UINT loader_get_layer_env(struct loader_icd *icd, XGL_UINT gpu_index, struct layer_name_pair *pLayerNames)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600694{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600695 const char *layerEnv;
696 XGL_UINT len, count = 0;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600697 char *p, *pOrig, *next, *name;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600698
Jon Ashburnead95c52014-11-18 09:06:04 -0700699 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn4fbcb032014-10-23 10:29:09 -0600700 if (!layerEnv)
701 return 0;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600702 p = malloc(strlen(layerEnv) + 1);
703 if (!p)
704 return 0;
705 strcpy(p, layerEnv);
Jon Ashburnb4d00532014-10-22 21:15:26 -0600706 pOrig = p;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600707
Jon Ashburn183dfd02014-10-22 18:13:16 -0600708 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700709 const char *lib_name = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600710 next = strchr(p, ':');
711 if (next == NULL) {
712 len = strlen(p);
713 next = p + len;
714 }
715 else {
716 len = next - p;
717 *(char *) next = '\0';
718 next++;
719 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600720 name = basename(p);
Jon Ashburnead95c52014-11-18 09:06:04 -0700721 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600722 p = next;
723 continue;
724 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600725
Jon Ashburnead95c52014-11-18 09:06:04 -0700726 len = strlen(name);
727 pLayerNames[count].layer_name = malloc(len + 1);
728 if (!pLayerNames[count].layer_name) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600729 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600730 return count;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600731 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700732 strncpy((char *) pLayerNames[count].layer_name, name, len);
733 pLayerNames[count].layer_name[len] = '\0';
734 pLayerNames[count].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600735 count++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600736 p = next;
737
Jon Ashburn183dfd02014-10-22 18:13:16 -0600738 };
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600739
Jon Ashburnb4d00532014-10-22 21:15:26 -0600740 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600741 return count;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600742}
743
Jon Ashburnead95c52014-11-18 09:06:04 -0700744static XGL_UINT loader_get_layer_libs(struct loader_icd *icd, XGL_UINT gpu_index, const XGL_DEVICE_CREATE_INFO* pCreateInfo, struct layer_name_pair **ppLayerNames)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600745{
Jon Ashburnead95c52014-11-18 09:06:04 -0700746 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn183dfd02014-10-22 18:13:16 -0600747
748 *ppLayerNames = &layerNames[0];
749 if (!pCreateInfo) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700750 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600751 }
752
753 XGL_LAYER_CREATE_INFO *pCi = (XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
754
755 while (pCi) {
756 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
757 const char *name;
758 XGL_UINT len;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600759 for (XGL_UINT i = 0; i < pCi->layerCount; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700760 const char * lib_name = NULL;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600761 name = (const char *) *(pCi->ppActiveLayerNames + i);
Jon Ashburnead95c52014-11-18 09:06:04 -0700762 if (!find_layer_name(icd, gpu_index, name, &lib_name))
763 return loader_get_layer_env(icd, gpu_index, layerNames);
764 len = strlen(name);
765 layerNames[i].layer_name = malloc(len + 1);
766 if (!layerNames[i].layer_name)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600767 return i;
Jon Ashburnead95c52014-11-18 09:06:04 -0700768 strncpy((char *) layerNames[i].layer_name, name, len);
769 layerNames[i].layer_name[len] = '\0';
770 layerNames[i].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600771 }
772 return pCi->layerCount;
773 }
774 pCi = pCi->pNext;
775 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700776 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600777}
778
779static void loader_deactivate_layer()
780{
781 struct loader_icd *icd;
782 struct loader_layers *libs;
783
784 for (icd = loader.icds; icd; icd = icd->next) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600785 if (icd->gpus)
786 free(icd->gpus);
787 icd->gpus = NULL;
788 if (icd->loader_dispatch)
789 free(icd->loader_dispatch);
790 icd->loader_dispatch = NULL;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600791 for (XGL_UINT j = 0; j < icd->gpu_count; j++) {
792 if (icd->layer_count[j] > 0) {
793 for (XGL_UINT i = 0; i < icd->layer_count[j]; i++) {
794 libs = &(icd->layer_libs[j][i]);
795 if (libs->lib_handle)
796 dlclose(libs->lib_handle);
797 libs->lib_handle = NULL;
798 }
Jon Ashburnb4d00532014-10-22 21:15:26 -0600799 if (icd->wrappedGpus[j])
800 free(icd->wrappedGpus[j]);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600801 }
802 icd->layer_count[j] = 0;
803 }
804 icd->gpu_count = 0;
805 }
806}
807
Jon Ashburn10053332014-11-17 10:17:37 -0700808extern XGL_UINT loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600809{
810 XGL_UINT gpu_index;
811 XGL_UINT count;
Jon Ashburnead95c52014-11-18 09:06:04 -0700812 struct layer_name_pair *pLayerNames;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600813 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600814
815 if (!icd)
816 return 0;
817 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
818
819 /* activate any layer libraries */
820 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600821 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
822 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600823 GetProcAddrType nextGPA = xglGetProcAddr;
824
Jon Ashburnead95c52014-11-18 09:06:04 -0700825 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600826 if (!count)
827 return 0;
Jon Ashburnead95c52014-11-18 09:06:04 -0700828 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600829
Jon Ashburnb4d00532014-10-22 21:15:26 -0600830 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
831 if (! icd->wrappedGpus[gpu_index])
832 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600833 for (XGL_INT i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600834 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600835 nextGpuObj->pGPA = nextGPA;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600836 nextGpuObj->baseObject = baseObj;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600837 nextGpuObj->nextObject = gpuObj;
838 gpuObj = nextGpuObj;
839
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700840 char funcStr[256];
841 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
842 if ((nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
843 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600844 if (!nextGPA) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700845 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to find xglGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600846 continue;
847 }
848
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600849 if (i == 0) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600850 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600851 //Insert the new wrapped objects into the list with loader object at head
852 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
853 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
854 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
855 gpuObj->nextObject = baseObj;
856 gpuObj->pGPA = icd->GetProcAddr;
857 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600858
859 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600860 }
861 else {
862 //make sure requested Layers matches currently activated Layers
Jon Ashburnead95c52014-11-18 09:06:04 -0700863 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600864 for (XGL_UINT i = 0; i < count; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700865 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600866 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
867 break;
868 }
869 }
870 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700871 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600872 }
873 }
874 return icd->layer_count[gpu_index];
875}
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600876
Jon Ashburn21734942014-10-17 15:31:22 -0600877LOADER_EXPORT XGL_VOID * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR * pName) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600878
879 if (gpu == NULL)
880 return NULL;
Jon Ashburn8eecd422014-10-22 12:42:13 -0600881 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600882 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600883
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600884 if (disp_table == NULL)
885 return NULL;
886
887 if (!strncmp("xglGetProcAddr", (const char *) pName, sizeof("xglGetProcAddr")))
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600888 return disp_table->GetProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600889 else if (!strncmp("xglInitAndEnumerateGpus", (const char *) pName, sizeof("xglInitAndEnumerateGpus")))
890 return disp_table->InitAndEnumerateGpus;
891 else if (!strncmp("xglGetGpuInfo", (const char *) pName, sizeof ("xglGetGpuInfo")))
892 return disp_table->GetGpuInfo;
893 else if (!strncmp("xglCreateDevice", (const char *) pName, sizeof ("xglCreateDevice")))
894 return disp_table->CreateDevice;
895 else if (!strncmp("xglDestroyDevice", (const char *) pName, sizeof ("xglDestroyDevice")))
896 return disp_table->DestroyDevice;
897 else if (!strncmp("xglGetExtensionSupport", (const char *) pName, sizeof ("xglGetExtensionSupport")))
898 return disp_table->GetExtensionSupport;
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600899 else if (!strncmp("xglEnumerateLayers", (const char *) pName, sizeof ("xglEnumerateLayers")))
900 return disp_table->EnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600901 else if (!strncmp("xglGetDeviceQueue", (const char *) pName, sizeof ("xglGetDeviceQueue")))
902 return disp_table->GetDeviceQueue;
903 else if (!strncmp("xglQueueSubmit", (const char *) pName, sizeof ("xglQueueSubmit")))
904 return disp_table->QueueSubmit;
905 else if (!strncmp("xglQueueSetGlobalMemReferences", (const char *) pName, sizeof ("xglQueueSetGlobalMemReferences")))
906 return disp_table->QueueSetGlobalMemReferences;
907 else if (!strncmp("xglQueueWaitIdle", (const char *) pName, sizeof ("xglQueueWaitIdle")))
908 return disp_table->QueueWaitIdle;
909 else if (!strncmp("xglDeviceWaitIdle", (const char *) pName, sizeof ("xglDeviceWaitIdle")))
910 return disp_table->DeviceWaitIdle;
911 else if (!strncmp("xglGetMemoryHeapCount", (const char *) pName, sizeof ("xglGetMemoryHeapCount")))
912 return disp_table->GetMemoryHeapCount;
913 else if (!strncmp("xglGetMemoryHeapInfo", (const char *) pName, sizeof ("xglGetMemoryHeapInfo")))
914 return disp_table->GetMemoryHeapInfo;
915 else if (!strncmp("xglAllocMemory", (const char *) pName, sizeof ("xglAllocMemory")))
916 return disp_table->AllocMemory;
917 else if (!strncmp("xglFreeMemory", (const char *) pName, sizeof ("xglFreeMemory")))
918 return disp_table->FreeMemory;
919 else if (!strncmp("xglSetMemoryPriority", (const char *) pName, sizeof ("xglSetMemoryPriority")))
920 return disp_table->SetMemoryPriority;
921 else if (!strncmp("xglMapMemory", (const char *) pName, sizeof ("xglMapMemory")))
922 return disp_table->MapMemory;
923 else if (!strncmp("xglUnmapMemory", (const char *) pName, sizeof ("xglUnmapMemory")))
924 return disp_table->UnmapMemory;
925 else if (!strncmp("xglPinSystemMemory", (const char *) pName, sizeof ("xglPinSystemMemory")))
926 return disp_table->PinSystemMemory;
927 else if (!strncmp("xglRemapVirtualMemoryPages", (const char *) pName, sizeof ("xglRemapVirtualMemoryPages")))
928 return disp_table->RemapVirtualMemoryPages;
929 else if (!strncmp("xglGetMultiGpuCompatibility", (const char *) pName, sizeof ("xglGetMultiGpuCompatibility")))
930 return disp_table->GetMultiGpuCompatibility;
931 else if (!strncmp("xglOpenSharedMemory", (const char *) pName, sizeof ("xglOpenSharedMemory")))
932 return disp_table->OpenSharedMemory;
933 else if (!strncmp("xglOpenSharedQueueSemaphore", (const char *) pName, sizeof ("xglOpenSharedQueueSemaphore")))
934 return disp_table->OpenSharedQueueSemaphore;
935 else if (!strncmp("xglOpenPeerMemory", (const char *) pName, sizeof ("xglOpenPeerMemory")))
936 return disp_table->OpenPeerMemory;
937 else if (!strncmp("xglOpenPeerImage", (const char *) pName, sizeof ("xglOpenPeerImage")))
938 return disp_table->OpenPeerImage;
939 else if (!strncmp("xglDestroyObject", (const char *) pName, sizeof ("xglDestroyObject")))
940 return disp_table->DestroyObject;
941 else if (!strncmp("xglGetObjectInfo", (const char *) pName, sizeof ("xglGetObjectInfo")))
942 return disp_table->GetObjectInfo;
943 else if (!strncmp("xglBindObjectMemory", (const char *) pName, sizeof ("xglBindObjectMemory")))
944 return disp_table->BindObjectMemory;
945 else if (!strncmp("xglCreateFence", (const char *) pName, sizeof ("xgllCreateFence")))
946 return disp_table->CreateFence;
947 else if (!strncmp("xglGetFenceStatus", (const char *) pName, sizeof ("xglGetFenceStatus")))
948 return disp_table->GetFenceStatus;
949 else if (!strncmp("xglWaitForFences", (const char *) pName, sizeof ("xglWaitForFences")))
950 return disp_table->WaitForFences;
951 else if (!strncmp("xglCreateQueueSemaphore", (const char *) pName, sizeof ("xgllCreateQueueSemaphore")))
952 return disp_table->CreateQueueSemaphore;
953 else if (!strncmp("xglSignalQueueSemaphore", (const char *) pName, sizeof ("xglSignalQueueSemaphore")))
954 return disp_table->SignalQueueSemaphore;
955 else if (!strncmp("xglWaitQueueSemaphore", (const char *) pName, sizeof ("xglWaitQueueSemaphore")))
956 return disp_table->WaitQueueSemaphore;
957 else if (!strncmp("xglCreateEvent", (const char *) pName, sizeof ("xgllCreateEvent")))
958 return disp_table->CreateEvent;
959 else if (!strncmp("xglGetEventStatus", (const char *) pName, sizeof ("xglGetEventStatus")))
960 return disp_table->GetEventStatus;
961 else if (!strncmp("xglSetEvent", (const char *) pName, sizeof ("xglSetEvent")))
962 return disp_table->SetEvent;
963 else if (!strncmp("xglResetEvent", (const char *) pName, sizeof ("xgllResetEvent")))
964 return disp_table->ResetEvent;
965 else if (!strncmp("xglCreateQueryPool", (const char *) pName, sizeof ("xglCreateQueryPool")))
966 return disp_table->CreateQueryPool;
967 else if (!strncmp("xglGetQueryPoolResults", (const char *) pName, sizeof ("xglGetQueryPoolResults")))
968 return disp_table->GetQueryPoolResults;
969 else if (!strncmp("xglGetFormatInfo", (const char *) pName, sizeof ("xglGetFormatInfo")))
970 return disp_table->GetFormatInfo;
971 else if (!strncmp("xglCreateImage", (const char *) pName, sizeof ("xglCreateImage")))
972 return disp_table->CreateImage;
973 else if (!strncmp("xglGetImageSubresourceInfo", (const char *) pName, sizeof ("xglGetImageSubresourceInfo")))
974 return disp_table->GetImageSubresourceInfo;
975 else if (!strncmp("xglCreateImageView", (const char *) pName, sizeof ("xglCreateImageView")))
976 return disp_table->CreateImageView;
977 else if (!strncmp("xglCreateColorAttachmentView", (const char *) pName, sizeof ("xglCreateColorAttachmentView")))
978 return disp_table->CreateColorAttachmentView;
979 else if (!strncmp("xglCreateDepthStencilView", (const char *) pName, sizeof ("xglCreateDepthStencilView")))
980 return disp_table->CreateDepthStencilView;
981 else if (!strncmp("xglCreateShader", (const char *) pName, sizeof ("xglCreateShader")))
982 return disp_table->CreateShader;
983 else if (!strncmp("xglCreateGraphicsPipeline", (const char *) pName, sizeof ("xglCreateGraphicsPipeline")))
984 return disp_table->CreateGraphicsPipeline;
985 else if (!strncmp("xglCreateComputePipeline", (const char *) pName, sizeof ("xglCreateComputePipeline")))
986 return disp_table->CreateComputePipeline;
987 else if (!strncmp("xglStorePipeline", (const char *) pName, sizeof ("xglStorePipeline")))
988 return disp_table->StorePipeline;
989 else if (!strncmp("xglLoadPipeline", (const char *) pName, sizeof ("xglLoadPipeline")))
990 return disp_table->LoadPipeline;
991 else if (!strncmp("xglCreatePipelineDelta", (const char *) pName, sizeof ("xglCreatePipelineDelta")))
992 return disp_table->CreatePipelineDelta;
993 else if (!strncmp("xglCreateSampler", (const char *) pName, sizeof ("xglCreateSampler")))
994 return disp_table->CreateSampler;
995 else if (!strncmp("xglCreateDescriptorSet", (const char *) pName, sizeof ("xglCreateDescriptorSet")))
996 return disp_table->CreateDescriptorSet;
997 else if (!strncmp("xglBeginDescriptorSetUpdate", (const char *) pName, sizeof ("xglBeginDescriptorSetUpdate")))
998 return disp_table->BeginDescriptorSetUpdate;
999 else if (!strncmp("xglEndDescriptorSetUpdate", (const char *) pName, sizeof ("xglEndDescriptorSetUpdate")))
1000 return disp_table->EndDescriptorSetUpdate;
1001 else if (!strncmp("xglAttachSamplerDescriptors", (const char *) pName, sizeof ("xglAttachSamplerDescriptors")))
1002 return disp_table->AttachSamplerDescriptors;
1003 else if (!strncmp("xglAttachImageViewDescriptors", (const char *) pName, sizeof ("xglAttachImageViewDescriptors")))
1004 return disp_table->AttachImageViewDescriptors;
1005 else if (!strncmp("xglAttachMemoryViewDescriptors", (const char *) pName, sizeof ("xglAttachMemoryViewDescriptors")))
1006 return disp_table->AttachMemoryViewDescriptors;
1007 else if (!strncmp("xglAttachNestedDescriptors", (const char *) pName, sizeof ("xglAttachNestedDescriptors")))
1008 return disp_table->AttachNestedDescriptors;
1009 else if (!strncmp("xglClearDescriptorSetSlots", (const char *) pName, sizeof ("xglClearDescriptorSetSlots")))
1010 return disp_table->ClearDescriptorSetSlots;
1011 else if (!strncmp("xglCreateViewportState", (const char *) pName, sizeof ("xglCreateViewportState")))
1012 return disp_table->CreateViewportState;
1013 else if (!strncmp("xglCreateRasterState", (const char *) pName, sizeof ("xglCreateRasterState")))
1014 return disp_table->CreateRasterState;
1015 else if (!strncmp("xglCreateMsaaState", (const char *) pName, sizeof ("xglCreateMsaaState")))
1016 return disp_table->CreateMsaaState;
1017 else if (!strncmp("xglCreateColorBlendState", (const char *) pName, sizeof ("xglCreateColorBlendState")))
1018 return disp_table->CreateColorBlendState;
1019 else if (!strncmp("xglCreateDepthStencilState", (const char *) pName, sizeof ("xglCreateDepthStencilState")))
1020 return disp_table->CreateDepthStencilState;
1021 else if (!strncmp("xglCreateCommandBuffer", (const char *) pName, sizeof ("xglCreateCommandBuffer")))
1022 return disp_table->CreateCommandBuffer;
1023 else if (!strncmp("xglBeginCommandBuffer", (const char *) pName, sizeof ("xglBeginCommandBuffer")))
1024 return disp_table->BeginCommandBuffer;
1025 else if (!strncmp("xglEndCommandBuffer", (const char *) pName, sizeof ("xglEndCommandBuffer")))
1026 return disp_table->EndCommandBuffer;
1027 else if (!strncmp("xglResetCommandBuffer", (const char *) pName, sizeof ("xglResetCommandBuffer")))
1028 return disp_table->ResetCommandBuffer;
1029 else if (!strncmp("xglCmdBindPipeline", (const char *) pName, sizeof ("xglCmdBindPipeline")))
1030 return disp_table->CmdBindPipeline;
1031 else if (!strncmp("xglCmdBindPipelineDelta", (const char *) pName, sizeof ("xglCmdBindPipelineDelta")))
1032 return disp_table->CmdBindPipelineDelta;
1033 else if (!strncmp("xglCmdBindStateObject", (const char *) pName, sizeof ("xglCmdBindStateObject")))
1034 return disp_table->CmdBindStateObject;
1035 else if (!strncmp("xglCmdBindDescriptorSet", (const char *) pName, sizeof ("xglCmdBindDescriptorSet")))
1036 return disp_table->CmdBindDescriptorSet;
1037 else if (!strncmp("xglCmdBindDynamicMemoryView", (const char *) pName, sizeof ("xglCmdBindDynamicMemoryView")))
1038 return disp_table->CmdBindDynamicMemoryView;
Chia-I Wu3b04af52014-11-08 10:48:20 +08001039 else if (!strncmp("xglCmdBindVertexData", (const char *) pName, sizeof ("xglCmdBindVertexData")))
1040 return disp_table->CmdBindVertexData;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001041 else if (!strncmp("xglCmdBindIndexData", (const char *) pName, sizeof ("xglCmdBindIndexData")))
1042 return disp_table->CmdBindIndexData;
1043 else if (!strncmp("xglCmdBindAttachments", (const char *) pName, sizeof ("xglCmdBindAttachments")))
1044 return disp_table->CmdBindAttachments;
1045 else if (!strncmp("xglCmdPrepareMemoryRegions", (const char *) pName, sizeof ("xglCmdPrepareMemoryRegions")))
1046 return disp_table->CmdPrepareMemoryRegions;
1047 else if (!strncmp("xglCmdPrepareImages", (const char *) pName, sizeof ("xglCmdPrepareImages")))
1048 return disp_table->CmdPrepareImages;
1049 else if (!strncmp("xglCmdDraw", (const char *) pName, sizeof ("xglCmdDraw")))
1050 return disp_table->CmdDraw;
1051 else if (!strncmp("xglCmdDrawIndexed", (const char *) pName, sizeof ("xglCmdDrawIndexed")))
1052 return disp_table->CmdDrawIndexed;
1053 else if (!strncmp("xglCmdDrawIndirect", (const char *) pName, sizeof ("xglCmdDrawIndirect")))
1054 return disp_table->CmdDrawIndirect;
1055 else if (!strncmp("xglCmdDrawIndexedIndirect", (const char *) pName, sizeof ("xglCmdDrawIndexedIndirect")))
1056 return disp_table->CmdDrawIndexedIndirect;
1057 else if (!strncmp("xglCmdDispatch", (const char *) pName, sizeof ("xglCmdDispatch")))
1058 return disp_table->CmdDispatch;
1059 else if (!strncmp("xglCmdDispatchIndirect", (const char *) pName, sizeof ("xglCmdDispatchIndirect")))
1060 return disp_table->CmdDispatchIndirect;
1061 else if (!strncmp("xglCmdCopyMemory", (const char *) pName, sizeof ("xglCmdCopyMemory")))
1062 return disp_table->CmdCopyMemory;
1063 else if (!strncmp("xglCmdCopyImage", (const char *) pName, sizeof ("xglCmdCopyImage")))
1064 return disp_table->CmdCopyImage;
1065 else if (!strncmp("xglCmdCopyMemoryToImage", (const char *) pName, sizeof ("xglCmdCopyMemoryToImage")))
1066 return disp_table->CmdCopyMemoryToImage;
1067 else if (!strncmp("xglCmdCopyImageToMemory", (const char *) pName, sizeof ("xglCmdCopyImageToMemory")))
1068 return disp_table->CmdCopyImageToMemory;
1069 else if (!strncmp("xglCmdCloneImageData", (const char *) pName, sizeof ("xglCmdCloneImageData")))
1070 return disp_table->CmdCloneImageData;
1071 else if (!strncmp("xglCmdUpdateMemory", (const char *) pName, sizeof ("xglCmdUpdateMemory")))
1072 return disp_table->CmdUpdateMemory;
1073 else if (!strncmp("xglCmdFillMemory", (const char *) pName, sizeof ("xglCmdFillMemory")))
1074 return disp_table->CmdFillMemory;
1075 else if (!strncmp("xglCmdClearColorImage", (const char *) pName, sizeof ("xglCmdClearColorImage")))
1076 return disp_table->CmdClearColorImage;
1077 else if (!strncmp("xglCmdClearColorImageRaw", (const char *) pName, sizeof ("xglCmdClearColorImageRaw")))
1078 return disp_table->CmdClearColorImageRaw;
1079 else if (!strncmp("xglCmdClearDepthStencil", (const char *) pName, sizeof ("xglCmdClearDepthStencil")))
1080 return disp_table->CmdClearDepthStencil;
1081 else if (!strncmp("xglCmdResolveImage", (const char *) pName, sizeof ("xglCmdResolveImage")))
1082 return disp_table->CmdResolveImage;
1083 else if (!strncmp("xglCmdSetEvent", (const char *) pName, sizeof ("xglCmdSetEvent")))
1084 return disp_table->CmdSetEvent;
1085 else if (!strncmp("xglCmdResetEvent", (const char *) pName, sizeof ("xglCmdResetEvent")))
1086 return disp_table->CmdResetEvent;
1087 else if (!strncmp("xglCmdMemoryAtomic", (const char *) pName, sizeof ("xglCmdMemoryAtomic")))
1088 return disp_table->CmdMemoryAtomic;
1089 else if (!strncmp("xglCmdBeginQuery", (const char *) pName, sizeof ("xglCmdBeginQuery")))
1090 return disp_table->CmdBeginQuery;
1091 else if (!strncmp("xglCmdEndQuery", (const char *) pName, sizeof ("xglCmdEndQuery")))
1092 return disp_table->CmdEndQuery;
1093 else if (!strncmp("xglCmdResetQueryPool", (const char *) pName, sizeof ("xglCmdResetQueryPool")))
1094 return disp_table->CmdResetQueryPool;
1095 else if (!strncmp("xglCmdWriteTimestamp", (const char *) pName, sizeof ("xglCmdWriteTimestamp")))
1096 return disp_table->CmdWriteTimestamp;
1097 else if (!strncmp("xglCmdInitAtomicCounters", (const char *) pName, sizeof ("xglCmdInitAtomicCounters")))
1098 return disp_table->CmdInitAtomicCounters;
1099 else if (!strncmp("xglCmdLoadAtomicCounters", (const char *) pName, sizeof ("xglCmdLoadAtomicCounters")))
1100 return disp_table->CmdLoadAtomicCounters;
1101 else if (!strncmp("xglCmdSaveAtomicCounters", (const char *) pName, sizeof ("xglCmdSaveAtomicCounters")))
1102 return disp_table->CmdSaveAtomicCounters;
1103 else if (!strncmp("xglDbgSetValidationLevel", (const char *) pName, sizeof ("xglDbgSetValidationLevel")))
1104 return disp_table->DbgSetValidationLevel;
1105 else if (!strncmp("xglDbgRegisterMsgCallback", (const char *) pName, sizeof ("xglDbgRegisterMsgCallback")))
1106 return disp_table->DbgRegisterMsgCallback;
1107 else if (!strncmp("xglDbgUnregisterMsgCallback", (const char *) pName, sizeof ("xglDbgUnregisterMsgCallback")))
1108 return disp_table->DbgUnregisterMsgCallback;
1109 else if (!strncmp("xglDbgSetMessageFilter", (const char *) pName, sizeof ("xglDbgSetMessageFilter")))
1110 return disp_table->DbgSetMessageFilter;
1111 else if (!strncmp("xglDbgSetObjectTag", (const char *) pName, sizeof ("xglDbgSetObjectTag")))
1112 return disp_table->DbgSetObjectTag;
1113 else if (!strncmp("xglDbgSetGlobalOption", (const char *) pName, sizeof ("xglDbgSetGlobalOption")))
1114 return disp_table->DbgSetGlobalOption;
1115 else if (!strncmp("xglDbgSetDeviceOption", (const char *) pName, sizeof ("xglDbgSetDeviceOption")))
1116 return disp_table->DbgSetDeviceOption;
1117 else if (!strncmp("xglCmdDbgMarkerBegin", (const char *) pName, sizeof ("xglCmdDbgMarkerBegin")))
1118 return disp_table->CmdDbgMarkerBegin;
1119 else if (!strncmp("xglCmdDbgMarkerEnd", (const char *) pName, sizeof ("xglCmdDbgMarkerEnd")))
1120 return disp_table->CmdDbgMarkerEnd;
1121 else if (!strncmp("xglWsiX11AssociateConnection", (const char *) pName, sizeof("xglWsiX11AssociateConnection")))
1122 return disp_table->WsiX11AssociateConnection;
1123 else if (!strncmp("xglWsiX11GetMSC", (const char *) pName, sizeof("xglWsiX11GetMSC")))
1124 return disp_table->WsiX11GetMSC;
1125 else if (!strncmp("xglWsiX11CreatePresentableImage", (const char *) pName, sizeof("xglWsiX11CreatePresentableImage")))
1126 return disp_table->WsiX11CreatePresentableImage;
1127 else if (!strncmp("xglWsiX11QueuePresent", (const char *) pName, sizeof("xglWsiX11QueuePresent")))
1128 return disp_table->WsiX11QueuePresent;
1129 else {
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001130 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001131 return NULL;
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001132 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001133 }
1134}
1135
Chia-I Wu468e3c32014-08-04 08:03:57 +08001136LOADER_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO* pAppInfo, const XGL_ALLOC_CALLBACKS* pAllocCb, XGL_UINT maxGpus, XGL_UINT* pGpuCount, XGL_PHYSICAL_GPU* pGpus)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001137{
1138 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburn815bddd2014-10-16 15:48:50 -06001139 struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001140 XGL_UINT count = 0;
1141 XGL_RESULT res;
1142
Jon Ashburn815bddd2014-10-16 15:48:50 -06001143 // cleanup any prior layer initializations
Jon Ashburnb55278a2014-10-17 15:09:07 -06001144 loader_deactivate_layer();
Jon Ashburn815bddd2014-10-16 15:48:50 -06001145
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001146 pthread_once(&once, loader_icd_scan);
1147
1148 if (!loader.icds)
1149 return XGL_ERROR_UNAVAILABLE;
1150
1151 icd = loader.icds;
1152 while (icd) {
1153 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001154 XGL_BASE_LAYER_OBJECT * wrappedGpus;
1155 GetProcAddrType getProcAddr = icd->GetProcAddr;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001156 XGL_UINT n, max = maxGpus - count;
1157
1158 if (max > XGL_MAX_PHYSICAL_GPUS) {
1159 max = XGL_MAX_PHYSICAL_GPUS;
1160 }
1161
1162 res = icd->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu74916ed2014-08-06 12:17:04 +08001163 if (res == XGL_SUCCESS && n) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001164 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn183dfd02014-10-22 18:13:16 -06001165 icd->gpus = wrappedGpus;
Jon Ashburnb55278a2014-10-17 15:09:07 -06001166 icd->gpu_count = n;
Jon Ashburn815bddd2014-10-16 15:48:50 -06001167 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001168 for (int i = 0; i < n; i++) {
1169 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -06001170 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001171 (wrappedGpus + i)->nextObject = gpus[i];
1172 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn8eecd422014-10-22 12:42:13 -06001173 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001174 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -06001175 *disp = icd->loader_dispatch + i;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001176 }
1177
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001178 count += n;
1179
1180 if (count >= maxGpus) {
1181 break;
1182 }
1183 }
1184
1185 icd = icd->next;
1186 }
1187
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001188 /* get layer libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -06001189 if (!loader.layer_scanned)
Courtney Goeltzenleuchterbce445a2014-12-01 09:29:42 -07001190 layer_lib_scan(NULL);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001191
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001192 *pGpuCount = count;
1193
1194 return (count > 0) ? XGL_SUCCESS : res;
1195}
1196
Jon Ashburn6847c2b2014-11-25 12:56:49 -07001197LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, XGL_SIZE maxLayerCount, XGL_SIZE maxStringSize, XGL_CHAR* const* pOutLayers, XGL_SIZE* pOutLayerCount, XGL_VOID* pReserved)
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001198{
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001199 XGL_UINT gpu_index;
1200 XGL_UINT count = 0;
1201 char *lib_name;
1202 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
1203 void *handle;
1204 EnumerateLayersType fpEnumerateLayers;
1205 XGL_CHAR layer_buf[16][256];
1206 XGL_CHAR * layers[16];
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001207
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001208 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001209 return XGL_ERROR_INVALID_POINTER;
1210
Jon Ashburnb9a54ac2014-12-02 13:03:09 -07001211 for (int i = 0; i < 16; i++)
1212 layers[i] = &layer_buf[i][0];
1213
1214 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1215 lib_name = loader.scanned_layer_names[j];
1216 if ((handle = dlopen(lib_name, RTLD_LAZY)) == NULL)
1217 continue;
1218 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
1219 //use default layer name based on library name libXGLLayer<name>.so
1220 char *pEnd, *cpyStr;
1221 int siz;
1222 dlclose(handle);
1223 lib_name = basename(lib_name);
1224 pEnd = strrchr(lib_name, '.');
1225 siz = pEnd - lib_name - strlen("libXGLLayer") + 1;
1226 if (pEnd == NULL || siz <= 0)
1227 continue;
1228 cpyStr = malloc(siz);
1229 if (cpyStr == NULL) {
1230 free(cpyStr);
1231 continue;
1232 }
1233 strncpy(cpyStr, lib_name + strlen("libXGLLayer"), siz);
1234 cpyStr[siz - 1] = '\0';
1235 if (siz > maxStringSize)
1236 siz = maxStringSize;
1237 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1238 pOutLayers[count][siz - 1] = '\0';
1239 count++;
1240 free(cpyStr);
1241 }
1242 else {
1243 XGL_SIZE cnt;
1244 XGL_UINT n;
1245 XGL_RESULT res;
1246 n = (maxStringSize < 256) ? maxStringSize : 256;
1247 res = fpEnumerateLayers(NULL, 16, n, layers, &cnt, (XGL_VOID *) icd->gpus + gpu_index);
1248 dlclose(handle);
1249 if (res != XGL_SUCCESS)
1250 continue;
1251 if (cnt + count > maxLayerCount)
1252 cnt = maxLayerCount - count;
1253 for (unsigned int i = count; i < cnt + count; i++) {
1254 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1255 if (n > 0)
1256 pOutLayers[i - count][n - 1] = '\0';
1257 }
1258 count += cnt;
1259 }
1260 }
1261
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001262 *pOutLayerCount = count;
1263
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001264 return XGL_SUCCESS;
1265}
1266
Chia-I Wu468e3c32014-08-04 08:03:57 +08001267LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001268{
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001269 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001270 XGL_RESULT res;
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001271 XGL_UINT gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001272
1273 if (!loader.scanned) {
1274 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1275 }
1276
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001277 for (icd = loader.icds; icd; icd = icd->next) {
1278 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1279 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
1280 if (res != XGL_SUCCESS) {
1281 gpu_idx = i;
1282 break;
1283 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001284 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001285 if (res != XGL_SUCCESS)
1286 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001287 }
1288
1289 /* roll back on errors */
1290 if (icd) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001291 for (const struct loader_icd * tmp = loader.icds; tmp != icd; tmp = tmp->next) {
1292 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
1293 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001294 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001295 /* and gpus on current icd */
1296 for (XGL_UINT i = 0; i < gpu_idx; i++)
1297 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001298
1299 return res;
1300 }
1301
1302 return XGL_SUCCESS;
1303}
1304
Chia-I Wu468e3c32014-08-04 08:03:57 +08001305LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001306{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001307 XGL_RESULT res = XGL_SUCCESS;
1308
1309 if (!loader.scanned) {
1310 return loader_msg_callback_remove(pfnMsgCallback);
1311 }
1312
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001313 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
1314 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1315 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1316 if (r != XGL_SUCCESS) {
1317 res = r;
1318 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001319 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001320 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001321 return res;
1322}
1323
Chia-I Wu468e3c32014-08-04 08:03:57 +08001324LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, XGL_SIZE dataSize, const XGL_VOID* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001325{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001326 XGL_RESULT res = XGL_SUCCESS;
1327
1328 if (!loader.scanned) {
1329 if (dataSize == 0)
1330 return XGL_ERROR_INVALID_VALUE;
1331
1332 switch (dbgOption) {
1333 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1334 loader.debug_echo_enable = *((const bool *) pData);
1335 break;
1336 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1337 loader.break_on_error = *((const bool *) pData);
1338 break;
1339 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1340 loader.break_on_warning = *((const bool *) pData);
1341 break;
1342 default:
1343 res = XGL_ERROR_INVALID_VALUE;
1344 break;
1345 }
1346
1347 return res;
1348 }
1349
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001350 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
1351 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1352 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
1353 /* unfortunately we cannot roll back */
1354 if (r != XGL_SUCCESS) {
1355 res = r;
1356 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001357 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001358 }
1359
1360 return res;
1361}