blob: f4f6d1a2fdc0dcd11af4bb0243d05cceca4bc528 [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 Ashburn815bddd2014-10-16 15:48:50 -060044typedef XGL_VOID (* SetDispatchType)(XGL_LAYER_DISPATCH_TABLE * disp, XGL_BOOL debug);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080045
Jon Ashburn183dfd02014-10-22 18:13:16 -060046struct loader_layers {
47 void *lib_handle;
Jon Ashburnead95c52014-11-18 09:06:04 -070048 char name[256];
49};
50
51struct layer_name_pair {
52 char *layer_name;
53 const char *lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -060054};
55
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080056struct loader_icd {
57 void *handle;
58
Jon Ashburnb55278a2014-10-17 15:09:07 -060059 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Jon Ashburn183dfd02014-10-22 18:13:16 -060060 XGL_UINT layer_count[XGL_MAX_PHYSICAL_GPUS];
61 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnb4d00532014-10-22 21:15:26 -060062 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnb55278a2014-10-17 15:09:07 -060063 XGL_UINT gpu_count;
Jon Ashburn183dfd02014-10-22 18:13:16 -060064 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburn815bddd2014-10-16 15:48:50 -060065
Jon Ashburnd43f9b62014-10-14 19:15:22 -060066 GetProcAddrType GetProcAddr;
67 InitAndEnumerateGpusType InitAndEnumerateGpus;
Jon Ashburn815bddd2014-10-16 15:48:50 -060068 SetDispatchType SetDispatch;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080069
70 struct loader_icd *next;
71};
72
Jon Ashburnd43f9b62014-10-14 19:15:22 -060073
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080074struct loader_msg_callback {
75 XGL_DBG_MSG_CALLBACK_FUNCTION func;
76 XGL_VOID *data;
77
78 struct loader_msg_callback *next;
79};
80
Jon Ashburnd43f9b62014-10-14 19:15:22 -060081
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080082static struct {
83 bool scanned;
84 struct loader_icd *icds;
Jon Ashburn183dfd02014-10-22 18:13:16 -060085 bool layer_scanned;
Jon Ashburnd43f9b62014-10-14 19:15:22 -060086 char layer_dirs[4096];
Jon Ashburn183dfd02014-10-22 18:13:16 -060087 unsigned int scanned_layer_count;
88 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080089 struct loader_msg_callback *msg_callbacks;
90
91 bool debug_echo_enable;
92 bool break_on_error;
93 bool break_on_warning;
94} loader;
95
96static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
97 XGL_VOID *data)
98{
99 struct loader_msg_callback *cb;
100
101 cb = malloc(sizeof(*cb));
102 if (!cb)
103 return XGL_ERROR_OUT_OF_MEMORY;
104
105 cb->func = func;
106 cb->data = data;
107
108 cb->next = loader.msg_callbacks;
109 loader.msg_callbacks = cb;
110
111 return XGL_SUCCESS;
112}
113
114static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
115{
116 struct loader_msg_callback *cb = loader.msg_callbacks;
117
118 /*
119 * Find the first match (last registered).
120 *
121 * XXX What if the same callback function is registered more than once?
122 */
123 while (cb) {
124 if (cb->func == func) {
125 break;
126 }
127
128 cb = cb->next;
129 }
130
131 if (!cb)
132 return XGL_ERROR_INVALID_POINTER;
133
134 free(cb);
135
136 return XGL_SUCCESS;
137}
138
139static void loader_msg_callback_clear(void)
140{
141 struct loader_msg_callback *cb = loader.msg_callbacks;
142
143 while (cb) {
144 struct loader_msg_callback *next = cb->next;
145 free(cb);
146 cb = next;
147 }
148
149 loader.msg_callbacks = NULL;
150}
151
152static void loader_log(XGL_DBG_MSG_TYPE msg_type, XGL_INT msg_code,
153 const char *format, ...)
154{
155 const struct loader_msg_callback *cb = loader.msg_callbacks;
156 char msg[256];
157 va_list ap;
158 int ret;
159
160 va_start(ap, format);
161 ret = vsnprintf(msg, sizeof(msg), format, ap);
162 if (ret >= sizeof(msg) || ret < 0) {
163 msg[sizeof(msg) - 1] = '\0';
164 }
165 va_end(ap);
166
167 if (loader.debug_echo_enable || !cb) {
168 fputs(msg, stderr);
169 fputc('\n', stderr);
170 }
171
172 while (cb) {
173 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
174 msg_code, (const XGL_CHAR *) msg, cb->data);
175 cb = cb->next;
176 }
177
178 switch (msg_type) {
179 case XGL_DBG_MSG_ERROR:
180 if (loader.break_on_error) {
181 exit(1);
182 }
183 /* fall through */
184 case XGL_DBG_MSG_WARNING:
185 if (loader.break_on_warning) {
186 exit(1);
187 }
188 break;
189 default:
190 break;
191 }
192}
193
194static void
195loader_icd_destroy(struct loader_icd *icd)
196{
197 dlclose(icd->handle);
198 free(icd);
199}
200
201static struct loader_icd *
202loader_icd_create(const char *filename)
203{
204 struct loader_icd *icd;
205
206 icd = malloc(sizeof(*icd));
207 if (!icd)
208 return NULL;
209
Courtney Goeltzenleuchter6f928162014-10-28 10:29:27 -0600210 memset(icd, 0, sizeof(*icd));
211
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800212 icd->handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
213 if (!icd->handle) {
214 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror());
215 free(icd);
216 return NULL;
217 }
218
219#define LOOKUP(icd, func) do { \
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600220 icd->func = (func## Type) dlsym(icd->handle, "xgl" #func); \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800221 if (!icd->func) { \
222 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror()); \
223 loader_icd_destroy(icd); \
224 return NULL; \
225 } \
226} while (0)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600227 LOOKUP(icd, GetProcAddr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800228 LOOKUP(icd, InitAndEnumerateGpus);
Jon Ashburn815bddd2014-10-16 15:48:50 -0600229 LOOKUP(icd, SetDispatch);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800230#undef LOOKUP
231
232 return icd;
233}
234
235static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
236{
237 const struct loader_msg_callback *cb = loader.msg_callbacks;
238 XGL_RESULT res;
239
240 while (cb) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700241 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
242 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
243 if (res != XGL_SUCCESS) {
244 break;
245 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800246 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800247 cb = cb->next;
248 }
249
250 /* roll back on errors */
251 if (cb) {
252 const struct loader_msg_callback *tmp = loader.msg_callbacks;
253
254 while (tmp != cb) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700255 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
256 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
257 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800258 tmp = tmp->next;
259 }
260
261 return res;
262 }
263
264 return XGL_SUCCESS;
265}
266
267static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
268{
269#define SETB(icd, opt, val) do { \
270 if (val) { \
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700271 for (XGL_UINT i = 0; i < icd->gpu_count; i++) { \
272 const XGL_RESULT res = \
273 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
274 if (res != XGL_SUCCESS) \
275 return res; \
276 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800277 } \
278} while (0)
279 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
280 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
281 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
282#undef SETB
283
284return XGL_SUCCESS;
285}
286
Chia-I Wu894a1172014-08-04 11:18:20 +0800287static struct loader_icd *loader_icd_add(const char *filename)
288{
289 struct loader_icd *icd;
290
291 icd = loader_icd_create(filename);
292 if (!icd)
293 return NULL;
294
295 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
296 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
297 loader_log(XGL_DBG_MSG_WARNING, 0,
298 "%s ignored: failed to migrate settings", filename);
299 loader_icd_destroy(icd);
300 }
301
302 /* prepend to the list */
303 icd->next = loader.icds;
304 loader.icds = icd;
305
306 return icd;
307}
308
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600309#ifndef DEFAULT_XGL_DRIVERS_PATH
310// TODO: Is this a good default location?
311// Need to search for both 32bit and 64bit ICDs
312#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
313#endif
314
315/**
316 * Try to \c loader_icd_scan XGL driver(s).
317 *
318 * This function scans the default system path or path
319 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
320 * order to find loadable XGL ICDs with the name of libXGL_*.
321 *
322 * \returns
323 * void; but side effect is to set loader_icd_scanned to true
324 */
325static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800326{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600327 const char *libPaths, *p, *next;
328 DIR *sysdir;
329 struct dirent *dent;
330 char icd_library[1024];
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600331 char path[1024];
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600332 int len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800333
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600334 libPaths = NULL;
335 if (geteuid() == getuid()) {
336 /* don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
337 libPaths = getenv("LIBXGL_DRIVERS_PATH");
338 }
339 if (libPaths == NULL)
340 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800341
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600342 for (p = libPaths; *p; p = next) {
343 next = strchr(p, ':');
344 if (next == NULL) {
345 len = strlen(p);
346 next = p + len;
347 }
348 else {
349 len = next - p;
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600350 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
351 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600352 next++;
353 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800354
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600355 sysdir = opendir(p);
356 if (sysdir) {
357 dent = readdir(sysdir);
358 while (dent) {
359 /* look for ICDs starting with "libXGL_" */
360 if (!strncmp(dent->d_name, "libXGL_", 7)) {
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600361 snprintf(icd_library, 1024, "%s/%s",p,dent->d_name);
Chia-I Wu894a1172014-08-04 11:18:20 +0800362 loader_icd_add(icd_library);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600363 }
364
365 dent = readdir(sysdir);
366 }
367 closedir(sysdir);
368 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800369 }
370
371 /* we have nothing to log anymore */
372 loader_msg_callback_clear();
373
374 loader.scanned = true;
375}
376
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600377#ifndef DEFAULT_XGL_LAYERS_PATH
378// TODO: Is this a good default locations
379#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
380#endif
381
Jon Ashburn183dfd02014-10-22 18:13:16 -0600382static void layer_lib_scan(const char * libInPaths, const bool useDefaultDirs)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600383{
384 const char *p, *next;
385 char *libPaths = &loader.layer_dirs[0];
386 DIR *curdir;
387 struct dirent *dent;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600388 int len, i;
389 char temp_str[1024];
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600390
391 if (libInPaths){
392 strncpy(libPaths, libInPaths, sizeof(loader.layer_dirs));
393 }
394 else {
395 *libPaths = '\0';
396 }
397
398 /* cleanup any previously scanned libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -0600399 for (i = 0; i < loader.scanned_layer_count; i++) {
400 if (loader.scanned_layer_names[i] != NULL)
401 free(loader.scanned_layer_names[i]);
402 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600403 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600404 loader.scanned_layer_count = 0;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600405
406 if (useDefaultDirs)
407 strncat(libPaths, DEFAULT_XGL_LAYERS_PATH, sizeof(loader.layer_dirs) - sizeof(DEFAULT_XGL_LAYERS_PATH));
408
409 for (p = libPaths; *p; p = next) {
410 next = strchr(p, ':');
411 if (next == NULL) {
412 len = strlen(p);
413 next = p + len;
414 }
415 else {
416 len = next - p;
417 *(char *) next = '\0';
418 next++;
419 }
420
421 curdir = opendir(p);
422 if (curdir) {
423 dent = readdir(curdir);
424 while (dent) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600425 /* look for wrappers starting with "libXGLlayer" */
426 if (!strncmp(dent->d_name, "libXGLLayer", strlen("libXGLLayer"))) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600427 void * handle;
428 snprintf(temp_str, sizeof(temp_str), "%s/%s",p,dent->d_name);
429 if ((handle = dlopen((const char *) temp_str, RTLD_LAZY)) == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600430 continue;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600431 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
432 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
433 break;
434 }
435 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
436 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
437 break;
438 }
439 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
440 loader.scanned_layer_count++;
441 dlclose(handle);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600442 }
443
444 dent = readdir(curdir);
445 }
446 closedir(curdir);
447 }
448 }
449
Jon Ashburn183dfd02014-10-22 18:13:16 -0600450 loader.layer_scanned = true;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600451}
452
Jon Ashburnb55278a2014-10-17 15:09:07 -0600453static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, GetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600454{
455 tab->GetProcAddr = fpGPA;
456 tab->InitAndEnumerateGpus = fpGPA(gpu, (const XGL_CHAR *) "xglInitAndEnumerateGpus");
457 tab->GetGpuInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetGpuInfo");
458 tab->CreateDevice = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDevice");
459 tab->DestroyDevice = fpGPA(gpu, (const XGL_CHAR *) "xglDestroyDevice");
460 tab->GetExtensionSupport = fpGPA(gpu, (const XGL_CHAR *) "xglGetExtensionSupport");
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600461 tab->EnumerateLayers = fpGPA(gpu, (const XGL_CHAR *) "xglEnumerateLayers");
462 if (tab->EnumerateLayers == NULL)
463 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600464 tab->GetDeviceQueue = fpGPA(gpu, (const XGL_CHAR *) "xglGetDeviceQueue");
465 tab->QueueSubmit = fpGPA(gpu, (const XGL_CHAR *) "xglQueueSubmit");
466 tab->QueueSetGlobalMemReferences = fpGPA(gpu, (const XGL_CHAR *) "xglQueueSetGlobalMemReferences");
467 tab->QueueWaitIdle = fpGPA(gpu, (const XGL_CHAR *) "xglQueueWaitIdle");
468 tab->DeviceWaitIdle = fpGPA(gpu, (const XGL_CHAR *) "xglDeviceWaitIdle");
469 tab->GetMemoryHeapCount = fpGPA(gpu, (const XGL_CHAR *) "xglGetMemoryHeapCount");
470 tab->GetMemoryHeapInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetMemoryHeapInfo");
471 tab->AllocMemory = fpGPA(gpu, (const XGL_CHAR *) "xglAllocMemory");
472 tab->FreeMemory = fpGPA(gpu, (const XGL_CHAR *) "xglFreeMemory");
473 tab->SetMemoryPriority = fpGPA(gpu, (const XGL_CHAR *) "xglSetMemoryPriority");
474 tab->MapMemory = fpGPA(gpu, (const XGL_CHAR *) "xglMapMemory");
475 tab->UnmapMemory = fpGPA(gpu, (const XGL_CHAR *) "xglUnmapMemory");
476 tab->PinSystemMemory = fpGPA(gpu, (const XGL_CHAR *) "xglPinSystemMemory");
477 tab->RemapVirtualMemoryPages = fpGPA(gpu, (const XGL_CHAR *) "xglRemapVirtualMemoryPages");
478 tab->GetMultiGpuCompatibility = fpGPA(gpu, (const XGL_CHAR *) "xglGetMultiGpuCompatibility");
479 tab->OpenSharedMemory = fpGPA(gpu, (const XGL_CHAR *) "xglOpenSharedMemory");
480 tab->OpenSharedQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglOpenSharedQueueSemaphore");
481 tab->OpenPeerMemory = fpGPA(gpu, (const XGL_CHAR *) "xglOpenPeerMemory");
482 tab->OpenPeerImage = fpGPA(gpu, (const XGL_CHAR *) "xglOpenPeerImage");
483 tab->DestroyObject = fpGPA(gpu, (const XGL_CHAR *) "xglDestroyObject");
484 tab->GetObjectInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetObjectInfo");
485 tab->BindObjectMemory = fpGPA(gpu, (const XGL_CHAR *) "xglBindObjectMemory");
486 tab->CreateFence = fpGPA(gpu, (const XGL_CHAR *) "xglCreateFence");
487 tab->GetFenceStatus = fpGPA(gpu, (const XGL_CHAR *) "xglGetFenceStatus");
488 tab->WaitForFences = fpGPA(gpu, (const XGL_CHAR *) "xglWaitForFences");
489 tab->CreateQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglCreateQueueSemaphore");
490 tab->SignalQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglSignalQueueSemaphore");
491 tab->WaitQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglWaitQueueSemaphore");
492 tab->CreateEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCreateEvent");
493 tab->GetEventStatus = fpGPA(gpu, (const XGL_CHAR *) "xglGetEventStatus");
494 tab->SetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglSetEvent");
495 tab->ResetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglResetEvent");
496 tab->CreateQueryPool = fpGPA(gpu, (const XGL_CHAR *) "xglCreateQueryPool");
497 tab->GetQueryPoolResults = fpGPA(gpu, (const XGL_CHAR *) "xglGetQueryPoolResults");
498 tab->GetFormatInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetFormatInfo");
499 tab->CreateImage = fpGPA(gpu, (const XGL_CHAR *) "xglCreateImage");
500 tab->GetImageSubresourceInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetImageSubresourceInfo");
501 tab->CreateImageView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateImageView");
502 tab->CreateColorAttachmentView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateColorAttachmentView");
503 tab->CreateDepthStencilView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDepthStencilView");
504 tab->CreateShader = fpGPA(gpu, (const XGL_CHAR *) "xglCreateShader");
505 tab->CreateGraphicsPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCreateGraphicsPipeline");
506 tab->CreateComputePipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCreateComputePipeline");
507 tab->StorePipeline = fpGPA(gpu, (const XGL_CHAR *) "xglStorePipeline");
508 tab->LoadPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglLoadPipeline");
509 tab->CreatePipelineDelta = fpGPA(gpu, (const XGL_CHAR *) "xglCreatePipelineDelta");
510 tab->CreateSampler = fpGPA(gpu, (const XGL_CHAR *) "xglCreateSampler");
511 tab->CreateDescriptorSet = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDescriptorSet");
512 tab->BeginDescriptorSetUpdate = fpGPA(gpu, (const XGL_CHAR *) "xglBeginDescriptorSetUpdate");
513 tab->EndDescriptorSetUpdate = fpGPA(gpu, (const XGL_CHAR *) "xglEndDescriptorSetUpdate");
514 tab->AttachSamplerDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachSamplerDescriptors");
515 tab->AttachImageViewDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachImageViewDescriptors");
516 tab->AttachMemoryViewDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachMemoryViewDescriptors");
517 tab->AttachNestedDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachNestedDescriptors");
518 tab->ClearDescriptorSetSlots = fpGPA(gpu, (const XGL_CHAR *) "xglClearDescriptorSetSlots");
519 tab->CreateViewportState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateViewportState");
520 tab->CreateRasterState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateRasterState");
521 tab->CreateMsaaState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateMsaaState");
522 tab->CreateColorBlendState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateColorBlendState");
523 tab->CreateDepthStencilState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDepthStencilState");
524 tab->CreateCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglCreateCommandBuffer");
525 tab->BeginCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglBeginCommandBuffer");
526 tab->EndCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglEndCommandBuffer");
527 tab->ResetCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglResetCommandBuffer");
528 tab->CmdBindPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindPipeline");
529 tab->CmdBindPipelineDelta = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindPipelineDelta");
530 tab->CmdBindStateObject = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindStateObject");
531 tab->CmdBindDescriptorSet = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindDescriptorSet");
532 tab->CmdBindDynamicMemoryView = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindDynamicMemoryView");
Chia-I Wu3b04af52014-11-08 10:48:20 +0800533 tab->CmdBindVertexData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindVertexData");
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600534 tab->CmdBindIndexData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindIndexData");
535 tab->CmdBindAttachments = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindAttachments");
536 tab->CmdPrepareMemoryRegions = fpGPA(gpu, (const XGL_CHAR *) "xglCmdPrepareMemoryRegions");
537 tab->CmdPrepareImages = fpGPA(gpu, (const XGL_CHAR *) "xglCmdPrepareImages");
538 tab->CmdDraw = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDraw");
539 tab->CmdDrawIndexed = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndexed");
540 tab->CmdDrawIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndirect");
541 tab->CmdDrawIndexedIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndexedIndirect");
542 tab->CmdDispatch = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDispatch");
543 tab->CmdDispatchIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDispatchIndirect");
544 tab->CmdCopyMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyMemory");
545 tab->CmdCopyImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyImage");
546 tab->CmdCopyMemoryToImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyMemoryToImage");
547 tab->CmdCopyImageToMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyImageToMemory");
548 tab->CmdCloneImageData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCloneImageData");
549 tab->CmdUpdateMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdUpdateMemory");
550 tab->CmdFillMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdFillMemory");
551 tab->CmdClearColorImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearColorImage");
552 tab->CmdClearColorImageRaw = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearColorImageRaw");
553 tab->CmdClearDepthStencil = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearDepthStencil");
554 tab->CmdResolveImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResolveImage");
555 tab->CmdSetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCmdSetEvent");
556 tab->CmdResetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResetEvent");
557 tab->CmdMemoryAtomic = fpGPA(gpu, (const XGL_CHAR *) "xglCmdMemoryAtomic");
558 tab->CmdBeginQuery = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBeginQuery");
559 tab->CmdEndQuery = fpGPA(gpu, (const XGL_CHAR *) "xglCmdEndQuery");
560 tab->CmdResetQueryPool = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResetQueryPool");
561 tab->CmdWriteTimestamp = fpGPA(gpu, (const XGL_CHAR *) "xglCmdWriteTimestamp");
562 tab->CmdInitAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdInitAtomicCounters");
563 tab->CmdLoadAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdLoadAtomicCounters");
564 tab->CmdSaveAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdSaveAtomicCounters");
565 tab->DbgSetValidationLevel = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetValidationLevel");
566 tab->DbgRegisterMsgCallback = fpGPA(gpu, (const XGL_CHAR *) "xglDbgRegisterMsgCallback");
567 tab->DbgUnregisterMsgCallback = fpGPA(gpu, (const XGL_CHAR *) "xglDbgUnregisterMsgCallback");
568 tab->DbgSetMessageFilter = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetMessageFilter");
569 tab->DbgSetObjectTag = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetObjectTag");
570 tab->DbgSetGlobalOption = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetGlobalOption");
571 tab->DbgSetDeviceOption = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetDeviceOption");
572 tab->CmdDbgMarkerBegin = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDbgMarkerBegin");
573 tab->CmdDbgMarkerEnd = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDbgMarkerEnd");
574 tab->WsiX11AssociateConnection = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11AssociateConnection");
575 tab->WsiX11GetMSC = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11GetMSC");
576 tab->WsiX11CreatePresentableImage = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11CreatePresentableImage");
577 tab->WsiX11QueuePresent = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11QueuePresent");
578}
579
Jon Ashburnb55278a2014-10-17 15:09:07 -0600580static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, XGL_UINT *gpu_index)
581{
582 for (struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
583 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600584 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburnb55278a2014-10-17 15:09:07 -0600585 *gpu_index = i;
586 return icd;
587 }
588 }
589 return NULL;
590}
591
Jon Ashburn183dfd02014-10-22 18:13:16 -0600592static bool loader_layers_activated(const struct loader_icd *icd, const XGL_UINT gpu_index)
Jon Ashburnb55278a2014-10-17 15:09:07 -0600593{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600594 if (icd->layer_count[gpu_index])
595 return true;
596 else
597 return false;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600598}
599
Jon Ashburnead95c52014-11-18 09:06:04 -0700600static 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 -0600601{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600602 if (!icd)
603 return;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600604
Jon Ashburn183dfd02014-10-22 18:13:16 -0600605 struct loader_layers *obj;
606 bool foundLib;
607 for (XGL_UINT i = 0; i < count; i++) {
608 foundLib = false;
609 for (XGL_UINT j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700610 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 -0600611 foundLib = true;
612 break;
613 }
614 }
615 if (!foundLib) {
616 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnead95c52014-11-18 09:06:04 -0700617 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
618 obj->name[sizeof(obj->name) - 1] = '\0';
619 if ((obj->lib_handle = dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
620 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 -0600621 continue;
622 } else {
Jon Ashburnead95c52014-11-18 09:06:04 -0700623 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 -0600624 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700625 free(pLayerNames[i].layer_name);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600626 icd->layer_count[gpu_index]++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600627 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600628 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600629}
630
Jon Ashburnead95c52014-11-18 09:06:04 -0700631static bool find_layer_name(struct loader_icd *icd, XGL_UINT gpu_index, const char * layer_name, const char **lib_name)
632{
633 void *handle;
634 EnumerateLayersType fpEnumerateLayers;
635 XGL_CHAR layer_buf[16][256];
636 XGL_CHAR * layers[16];
637
638 for (int i = 0; i < 16; i++)
639 layers[i] = &layer_buf[i][0];
640
641 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
642 *lib_name = loader.scanned_layer_names[j];
643 if ((handle = dlopen(*lib_name, RTLD_LAZY)) == NULL)
644 continue;
645 if ((fpEnumerateLayers = dlsym(handle, "xglEnumerateLayers")) == NULL) {
646 //use default layer name based on library name libXGLLayer<name>.so
647 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
648 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name), "libXGLLayer%s.so", layer_name);
649 dlclose(handle);
650 if (!strcmp(basename(*lib_name), lib_str)) {
651 free(lib_str);
652 return true;
653 }
654 else {
655 free(lib_str);
656 continue;
657 }
658 }
659 else {
660 XGL_SIZE cnt;
Jon Ashburn6847c2b2014-11-25 12:56:49 -0700661 fpEnumerateLayers(NULL, 16, 256, layers, &cnt, (XGL_VOID *) icd->gpus + gpu_index);
Jon Ashburnead95c52014-11-18 09:06:04 -0700662 for (unsigned int i = 0; i < cnt; i++) {
663 if (!strcmp((char *) layers[i], layer_name)) {
664 dlclose(handle);
665 return true;
666 }
667 }
668 }
669
670 dlclose(handle);
671 }
672
673 return false;
674}
675
676static 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 -0600677{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600678 const char *layerEnv;
679 XGL_UINT len, count = 0;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600680 char *p, *pOrig, *next, *name;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600681
Jon Ashburnead95c52014-11-18 09:06:04 -0700682 layerEnv = getenv("LIBXGL_LAYER_NAMES");
Jon Ashburn4fbcb032014-10-23 10:29:09 -0600683 if (!layerEnv)
684 return 0;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600685 p = malloc(strlen(layerEnv) + 1);
686 if (!p)
687 return 0;
688 strcpy(p, layerEnv);
Jon Ashburnb4d00532014-10-22 21:15:26 -0600689 pOrig = p;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600690
Jon Ashburn183dfd02014-10-22 18:13:16 -0600691 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700692 const char *lib_name = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600693 next = strchr(p, ':');
694 if (next == NULL) {
695 len = strlen(p);
696 next = p + len;
697 }
698 else {
699 len = next - p;
700 *(char *) next = '\0';
701 next++;
702 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600703 name = basename(p);
Jon Ashburnead95c52014-11-18 09:06:04 -0700704 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600705 p = next;
706 continue;
707 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600708
Jon Ashburnead95c52014-11-18 09:06:04 -0700709 len = strlen(name);
710 pLayerNames[count].layer_name = malloc(len + 1);
711 if (!pLayerNames[count].layer_name) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600712 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600713 return count;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600714 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700715 strncpy((char *) pLayerNames[count].layer_name, name, len);
716 pLayerNames[count].layer_name[len] = '\0';
717 pLayerNames[count].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600718 count++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600719 p = next;
720
Jon Ashburn183dfd02014-10-22 18:13:16 -0600721 };
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600722
Jon Ashburnb4d00532014-10-22 21:15:26 -0600723 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600724 return count;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600725}
726
Jon Ashburnead95c52014-11-18 09:06:04 -0700727static 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 -0600728{
Jon Ashburnead95c52014-11-18 09:06:04 -0700729 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Jon Ashburn183dfd02014-10-22 18:13:16 -0600730
731 *ppLayerNames = &layerNames[0];
732 if (!pCreateInfo) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700733 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600734 }
735
736 XGL_LAYER_CREATE_INFO *pCi = (XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
737
738 while (pCi) {
739 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
740 const char *name;
741 XGL_UINT len;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600742 for (XGL_UINT i = 0; i < pCi->layerCount; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700743 const char * lib_name = NULL;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600744 name = (const char *) *(pCi->ppActiveLayerNames + i);
Jon Ashburnead95c52014-11-18 09:06:04 -0700745 if (!find_layer_name(icd, gpu_index, name, &lib_name))
746 return loader_get_layer_env(icd, gpu_index, layerNames);
747 len = strlen(name);
748 layerNames[i].layer_name = malloc(len + 1);
749 if (!layerNames[i].layer_name)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600750 return i;
Jon Ashburnead95c52014-11-18 09:06:04 -0700751 strncpy((char *) layerNames[i].layer_name, name, len);
752 layerNames[i].layer_name[len] = '\0';
753 layerNames[i].lib_name = lib_name;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600754 }
755 return pCi->layerCount;
756 }
757 pCi = pCi->pNext;
758 }
Jon Ashburnead95c52014-11-18 09:06:04 -0700759 return loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600760}
761
762static void loader_deactivate_layer()
763{
764 struct loader_icd *icd;
765 struct loader_layers *libs;
766
767 for (icd = loader.icds; icd; icd = icd->next) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600768 if (icd->gpus)
769 free(icd->gpus);
770 icd->gpus = NULL;
771 if (icd->loader_dispatch)
772 free(icd->loader_dispatch);
773 icd->loader_dispatch = NULL;
774 icd->SetDispatch(NULL, true);
775 for (XGL_UINT j = 0; j < icd->gpu_count; j++) {
776 if (icd->layer_count[j] > 0) {
777 for (XGL_UINT i = 0; i < icd->layer_count[j]; i++) {
778 libs = &(icd->layer_libs[j][i]);
779 if (libs->lib_handle)
780 dlclose(libs->lib_handle);
781 libs->lib_handle = NULL;
782 }
Jon Ashburnb4d00532014-10-22 21:15:26 -0600783 if (icd->wrappedGpus[j])
784 free(icd->wrappedGpus[j]);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600785 }
786 icd->layer_count[j] = 0;
787 }
788 icd->gpu_count = 0;
789 }
790}
791
Jon Ashburn10053332014-11-17 10:17:37 -0700792extern XGL_UINT loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600793{
794 XGL_UINT gpu_index;
795 XGL_UINT count;
Jon Ashburnead95c52014-11-18 09:06:04 -0700796 struct layer_name_pair *pLayerNames;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600797 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600798
799 if (!icd)
800 return 0;
801 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
802
803 /* activate any layer libraries */
804 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600805 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
806 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600807 GetProcAddrType nextGPA = xglGetProcAddr;
808
Jon Ashburnead95c52014-11-18 09:06:04 -0700809 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600810 if (!count)
811 return 0;
Jon Ashburnead95c52014-11-18 09:06:04 -0700812 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600813
Jon Ashburnb4d00532014-10-22 21:15:26 -0600814 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
815 if (! icd->wrappedGpus[gpu_index])
816 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600817 for (XGL_INT i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600818 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600819 nextGpuObj->pGPA = nextGPA;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600820 nextGpuObj->baseObject = baseObj;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600821 nextGpuObj->nextObject = gpuObj;
822 gpuObj = nextGpuObj;
823
824 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
825 if (!nextGPA) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700826 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 -0600827 continue;
828 }
829
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600830 if (i == 0) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600831 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600832 //Insert the new wrapped objects into the list with loader object at head
833 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
834 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
835 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
836 gpuObj->nextObject = baseObj;
837 gpuObj->pGPA = icd->GetProcAddr;
838 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600839
840 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600841 }
842 else {
843 //make sure requested Layers matches currently activated Layers
Jon Ashburnead95c52014-11-18 09:06:04 -0700844 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600845 for (XGL_UINT i = 0; i < count; i++) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700846 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600847 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
848 break;
849 }
850 }
851 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnead95c52014-11-18 09:06:04 -0700852 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600853 }
854 }
855 return icd->layer_count[gpu_index];
856}
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600857
Jon Ashburn21734942014-10-17 15:31:22 -0600858LOADER_EXPORT XGL_VOID * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR * pName) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600859
860 if (gpu == NULL)
861 return NULL;
Jon Ashburn8eecd422014-10-22 12:42:13 -0600862 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600863 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600864
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600865 if (disp_table == NULL)
866 return NULL;
867
868 if (!strncmp("xglGetProcAddr", (const char *) pName, sizeof("xglGetProcAddr")))
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600869 return disp_table->GetProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600870 else if (!strncmp("xglInitAndEnumerateGpus", (const char *) pName, sizeof("xglInitAndEnumerateGpus")))
871 return disp_table->InitAndEnumerateGpus;
872 else if (!strncmp("xglGetGpuInfo", (const char *) pName, sizeof ("xglGetGpuInfo")))
873 return disp_table->GetGpuInfo;
874 else if (!strncmp("xglCreateDevice", (const char *) pName, sizeof ("xglCreateDevice")))
875 return disp_table->CreateDevice;
876 else if (!strncmp("xglDestroyDevice", (const char *) pName, sizeof ("xglDestroyDevice")))
877 return disp_table->DestroyDevice;
878 else if (!strncmp("xglGetExtensionSupport", (const char *) pName, sizeof ("xglGetExtensionSupport")))
879 return disp_table->GetExtensionSupport;
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600880 else if (!strncmp("xglEnumerateLayers", (const char *) pName, sizeof ("xglEnumerateLayers")))
881 return disp_table->EnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600882 else if (!strncmp("xglGetDeviceQueue", (const char *) pName, sizeof ("xglGetDeviceQueue")))
883 return disp_table->GetDeviceQueue;
884 else if (!strncmp("xglQueueSubmit", (const char *) pName, sizeof ("xglQueueSubmit")))
885 return disp_table->QueueSubmit;
886 else if (!strncmp("xglQueueSetGlobalMemReferences", (const char *) pName, sizeof ("xglQueueSetGlobalMemReferences")))
887 return disp_table->QueueSetGlobalMemReferences;
888 else if (!strncmp("xglQueueWaitIdle", (const char *) pName, sizeof ("xglQueueWaitIdle")))
889 return disp_table->QueueWaitIdle;
890 else if (!strncmp("xglDeviceWaitIdle", (const char *) pName, sizeof ("xglDeviceWaitIdle")))
891 return disp_table->DeviceWaitIdle;
892 else if (!strncmp("xglGetMemoryHeapCount", (const char *) pName, sizeof ("xglGetMemoryHeapCount")))
893 return disp_table->GetMemoryHeapCount;
894 else if (!strncmp("xglGetMemoryHeapInfo", (const char *) pName, sizeof ("xglGetMemoryHeapInfo")))
895 return disp_table->GetMemoryHeapInfo;
896 else if (!strncmp("xglAllocMemory", (const char *) pName, sizeof ("xglAllocMemory")))
897 return disp_table->AllocMemory;
898 else if (!strncmp("xglFreeMemory", (const char *) pName, sizeof ("xglFreeMemory")))
899 return disp_table->FreeMemory;
900 else if (!strncmp("xglSetMemoryPriority", (const char *) pName, sizeof ("xglSetMemoryPriority")))
901 return disp_table->SetMemoryPriority;
902 else if (!strncmp("xglMapMemory", (const char *) pName, sizeof ("xglMapMemory")))
903 return disp_table->MapMemory;
904 else if (!strncmp("xglUnmapMemory", (const char *) pName, sizeof ("xglUnmapMemory")))
905 return disp_table->UnmapMemory;
906 else if (!strncmp("xglPinSystemMemory", (const char *) pName, sizeof ("xglPinSystemMemory")))
907 return disp_table->PinSystemMemory;
908 else if (!strncmp("xglRemapVirtualMemoryPages", (const char *) pName, sizeof ("xglRemapVirtualMemoryPages")))
909 return disp_table->RemapVirtualMemoryPages;
910 else if (!strncmp("xglGetMultiGpuCompatibility", (const char *) pName, sizeof ("xglGetMultiGpuCompatibility")))
911 return disp_table->GetMultiGpuCompatibility;
912 else if (!strncmp("xglOpenSharedMemory", (const char *) pName, sizeof ("xglOpenSharedMemory")))
913 return disp_table->OpenSharedMemory;
914 else if (!strncmp("xglOpenSharedQueueSemaphore", (const char *) pName, sizeof ("xglOpenSharedQueueSemaphore")))
915 return disp_table->OpenSharedQueueSemaphore;
916 else if (!strncmp("xglOpenPeerMemory", (const char *) pName, sizeof ("xglOpenPeerMemory")))
917 return disp_table->OpenPeerMemory;
918 else if (!strncmp("xglOpenPeerImage", (const char *) pName, sizeof ("xglOpenPeerImage")))
919 return disp_table->OpenPeerImage;
920 else if (!strncmp("xglDestroyObject", (const char *) pName, sizeof ("xglDestroyObject")))
921 return disp_table->DestroyObject;
922 else if (!strncmp("xglGetObjectInfo", (const char *) pName, sizeof ("xglGetObjectInfo")))
923 return disp_table->GetObjectInfo;
924 else if (!strncmp("xglBindObjectMemory", (const char *) pName, sizeof ("xglBindObjectMemory")))
925 return disp_table->BindObjectMemory;
926 else if (!strncmp("xglCreateFence", (const char *) pName, sizeof ("xgllCreateFence")))
927 return disp_table->CreateFence;
928 else if (!strncmp("xglGetFenceStatus", (const char *) pName, sizeof ("xglGetFenceStatus")))
929 return disp_table->GetFenceStatus;
930 else if (!strncmp("xglWaitForFences", (const char *) pName, sizeof ("xglWaitForFences")))
931 return disp_table->WaitForFences;
932 else if (!strncmp("xglCreateQueueSemaphore", (const char *) pName, sizeof ("xgllCreateQueueSemaphore")))
933 return disp_table->CreateQueueSemaphore;
934 else if (!strncmp("xglSignalQueueSemaphore", (const char *) pName, sizeof ("xglSignalQueueSemaphore")))
935 return disp_table->SignalQueueSemaphore;
936 else if (!strncmp("xglWaitQueueSemaphore", (const char *) pName, sizeof ("xglWaitQueueSemaphore")))
937 return disp_table->WaitQueueSemaphore;
938 else if (!strncmp("xglCreateEvent", (const char *) pName, sizeof ("xgllCreateEvent")))
939 return disp_table->CreateEvent;
940 else if (!strncmp("xglGetEventStatus", (const char *) pName, sizeof ("xglGetEventStatus")))
941 return disp_table->GetEventStatus;
942 else if (!strncmp("xglSetEvent", (const char *) pName, sizeof ("xglSetEvent")))
943 return disp_table->SetEvent;
944 else if (!strncmp("xglResetEvent", (const char *) pName, sizeof ("xgllResetEvent")))
945 return disp_table->ResetEvent;
946 else if (!strncmp("xglCreateQueryPool", (const char *) pName, sizeof ("xglCreateQueryPool")))
947 return disp_table->CreateQueryPool;
948 else if (!strncmp("xglGetQueryPoolResults", (const char *) pName, sizeof ("xglGetQueryPoolResults")))
949 return disp_table->GetQueryPoolResults;
950 else if (!strncmp("xglGetFormatInfo", (const char *) pName, sizeof ("xglGetFormatInfo")))
951 return disp_table->GetFormatInfo;
952 else if (!strncmp("xglCreateImage", (const char *) pName, sizeof ("xglCreateImage")))
953 return disp_table->CreateImage;
954 else if (!strncmp("xglGetImageSubresourceInfo", (const char *) pName, sizeof ("xglGetImageSubresourceInfo")))
955 return disp_table->GetImageSubresourceInfo;
956 else if (!strncmp("xglCreateImageView", (const char *) pName, sizeof ("xglCreateImageView")))
957 return disp_table->CreateImageView;
958 else if (!strncmp("xglCreateColorAttachmentView", (const char *) pName, sizeof ("xglCreateColorAttachmentView")))
959 return disp_table->CreateColorAttachmentView;
960 else if (!strncmp("xglCreateDepthStencilView", (const char *) pName, sizeof ("xglCreateDepthStencilView")))
961 return disp_table->CreateDepthStencilView;
962 else if (!strncmp("xglCreateShader", (const char *) pName, sizeof ("xglCreateShader")))
963 return disp_table->CreateShader;
964 else if (!strncmp("xglCreateGraphicsPipeline", (const char *) pName, sizeof ("xglCreateGraphicsPipeline")))
965 return disp_table->CreateGraphicsPipeline;
966 else if (!strncmp("xglCreateComputePipeline", (const char *) pName, sizeof ("xglCreateComputePipeline")))
967 return disp_table->CreateComputePipeline;
968 else if (!strncmp("xglStorePipeline", (const char *) pName, sizeof ("xglStorePipeline")))
969 return disp_table->StorePipeline;
970 else if (!strncmp("xglLoadPipeline", (const char *) pName, sizeof ("xglLoadPipeline")))
971 return disp_table->LoadPipeline;
972 else if (!strncmp("xglCreatePipelineDelta", (const char *) pName, sizeof ("xglCreatePipelineDelta")))
973 return disp_table->CreatePipelineDelta;
974 else if (!strncmp("xglCreateSampler", (const char *) pName, sizeof ("xglCreateSampler")))
975 return disp_table->CreateSampler;
976 else if (!strncmp("xglCreateDescriptorSet", (const char *) pName, sizeof ("xglCreateDescriptorSet")))
977 return disp_table->CreateDescriptorSet;
978 else if (!strncmp("xglBeginDescriptorSetUpdate", (const char *) pName, sizeof ("xglBeginDescriptorSetUpdate")))
979 return disp_table->BeginDescriptorSetUpdate;
980 else if (!strncmp("xglEndDescriptorSetUpdate", (const char *) pName, sizeof ("xglEndDescriptorSetUpdate")))
981 return disp_table->EndDescriptorSetUpdate;
982 else if (!strncmp("xglAttachSamplerDescriptors", (const char *) pName, sizeof ("xglAttachSamplerDescriptors")))
983 return disp_table->AttachSamplerDescriptors;
984 else if (!strncmp("xglAttachImageViewDescriptors", (const char *) pName, sizeof ("xglAttachImageViewDescriptors")))
985 return disp_table->AttachImageViewDescriptors;
986 else if (!strncmp("xglAttachMemoryViewDescriptors", (const char *) pName, sizeof ("xglAttachMemoryViewDescriptors")))
987 return disp_table->AttachMemoryViewDescriptors;
988 else if (!strncmp("xglAttachNestedDescriptors", (const char *) pName, sizeof ("xglAttachNestedDescriptors")))
989 return disp_table->AttachNestedDescriptors;
990 else if (!strncmp("xglClearDescriptorSetSlots", (const char *) pName, sizeof ("xglClearDescriptorSetSlots")))
991 return disp_table->ClearDescriptorSetSlots;
992 else if (!strncmp("xglCreateViewportState", (const char *) pName, sizeof ("xglCreateViewportState")))
993 return disp_table->CreateViewportState;
994 else if (!strncmp("xglCreateRasterState", (const char *) pName, sizeof ("xglCreateRasterState")))
995 return disp_table->CreateRasterState;
996 else if (!strncmp("xglCreateMsaaState", (const char *) pName, sizeof ("xglCreateMsaaState")))
997 return disp_table->CreateMsaaState;
998 else if (!strncmp("xglCreateColorBlendState", (const char *) pName, sizeof ("xglCreateColorBlendState")))
999 return disp_table->CreateColorBlendState;
1000 else if (!strncmp("xglCreateDepthStencilState", (const char *) pName, sizeof ("xglCreateDepthStencilState")))
1001 return disp_table->CreateDepthStencilState;
1002 else if (!strncmp("xglCreateCommandBuffer", (const char *) pName, sizeof ("xglCreateCommandBuffer")))
1003 return disp_table->CreateCommandBuffer;
1004 else if (!strncmp("xglBeginCommandBuffer", (const char *) pName, sizeof ("xglBeginCommandBuffer")))
1005 return disp_table->BeginCommandBuffer;
1006 else if (!strncmp("xglEndCommandBuffer", (const char *) pName, sizeof ("xglEndCommandBuffer")))
1007 return disp_table->EndCommandBuffer;
1008 else if (!strncmp("xglResetCommandBuffer", (const char *) pName, sizeof ("xglResetCommandBuffer")))
1009 return disp_table->ResetCommandBuffer;
1010 else if (!strncmp("xglCmdBindPipeline", (const char *) pName, sizeof ("xglCmdBindPipeline")))
1011 return disp_table->CmdBindPipeline;
1012 else if (!strncmp("xglCmdBindPipelineDelta", (const char *) pName, sizeof ("xglCmdBindPipelineDelta")))
1013 return disp_table->CmdBindPipelineDelta;
1014 else if (!strncmp("xglCmdBindStateObject", (const char *) pName, sizeof ("xglCmdBindStateObject")))
1015 return disp_table->CmdBindStateObject;
1016 else if (!strncmp("xglCmdBindDescriptorSet", (const char *) pName, sizeof ("xglCmdBindDescriptorSet")))
1017 return disp_table->CmdBindDescriptorSet;
1018 else if (!strncmp("xglCmdBindDynamicMemoryView", (const char *) pName, sizeof ("xglCmdBindDynamicMemoryView")))
1019 return disp_table->CmdBindDynamicMemoryView;
Chia-I Wu3b04af52014-11-08 10:48:20 +08001020 else if (!strncmp("xglCmdBindVertexData", (const char *) pName, sizeof ("xglCmdBindVertexData")))
1021 return disp_table->CmdBindVertexData;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001022 else if (!strncmp("xglCmdBindIndexData", (const char *) pName, sizeof ("xglCmdBindIndexData")))
1023 return disp_table->CmdBindIndexData;
1024 else if (!strncmp("xglCmdBindAttachments", (const char *) pName, sizeof ("xglCmdBindAttachments")))
1025 return disp_table->CmdBindAttachments;
1026 else if (!strncmp("xglCmdPrepareMemoryRegions", (const char *) pName, sizeof ("xglCmdPrepareMemoryRegions")))
1027 return disp_table->CmdPrepareMemoryRegions;
1028 else if (!strncmp("xglCmdPrepareImages", (const char *) pName, sizeof ("xglCmdPrepareImages")))
1029 return disp_table->CmdPrepareImages;
1030 else if (!strncmp("xglCmdDraw", (const char *) pName, sizeof ("xglCmdDraw")))
1031 return disp_table->CmdDraw;
1032 else if (!strncmp("xglCmdDrawIndexed", (const char *) pName, sizeof ("xglCmdDrawIndexed")))
1033 return disp_table->CmdDrawIndexed;
1034 else if (!strncmp("xglCmdDrawIndirect", (const char *) pName, sizeof ("xglCmdDrawIndirect")))
1035 return disp_table->CmdDrawIndirect;
1036 else if (!strncmp("xglCmdDrawIndexedIndirect", (const char *) pName, sizeof ("xglCmdDrawIndexedIndirect")))
1037 return disp_table->CmdDrawIndexedIndirect;
1038 else if (!strncmp("xglCmdDispatch", (const char *) pName, sizeof ("xglCmdDispatch")))
1039 return disp_table->CmdDispatch;
1040 else if (!strncmp("xglCmdDispatchIndirect", (const char *) pName, sizeof ("xglCmdDispatchIndirect")))
1041 return disp_table->CmdDispatchIndirect;
1042 else if (!strncmp("xglCmdCopyMemory", (const char *) pName, sizeof ("xglCmdCopyMemory")))
1043 return disp_table->CmdCopyMemory;
1044 else if (!strncmp("xglCmdCopyImage", (const char *) pName, sizeof ("xglCmdCopyImage")))
1045 return disp_table->CmdCopyImage;
1046 else if (!strncmp("xglCmdCopyMemoryToImage", (const char *) pName, sizeof ("xglCmdCopyMemoryToImage")))
1047 return disp_table->CmdCopyMemoryToImage;
1048 else if (!strncmp("xglCmdCopyImageToMemory", (const char *) pName, sizeof ("xglCmdCopyImageToMemory")))
1049 return disp_table->CmdCopyImageToMemory;
1050 else if (!strncmp("xglCmdCloneImageData", (const char *) pName, sizeof ("xglCmdCloneImageData")))
1051 return disp_table->CmdCloneImageData;
1052 else if (!strncmp("xglCmdUpdateMemory", (const char *) pName, sizeof ("xglCmdUpdateMemory")))
1053 return disp_table->CmdUpdateMemory;
1054 else if (!strncmp("xglCmdFillMemory", (const char *) pName, sizeof ("xglCmdFillMemory")))
1055 return disp_table->CmdFillMemory;
1056 else if (!strncmp("xglCmdClearColorImage", (const char *) pName, sizeof ("xglCmdClearColorImage")))
1057 return disp_table->CmdClearColorImage;
1058 else if (!strncmp("xglCmdClearColorImageRaw", (const char *) pName, sizeof ("xglCmdClearColorImageRaw")))
1059 return disp_table->CmdClearColorImageRaw;
1060 else if (!strncmp("xglCmdClearDepthStencil", (const char *) pName, sizeof ("xglCmdClearDepthStencil")))
1061 return disp_table->CmdClearDepthStencil;
1062 else if (!strncmp("xglCmdResolveImage", (const char *) pName, sizeof ("xglCmdResolveImage")))
1063 return disp_table->CmdResolveImage;
1064 else if (!strncmp("xglCmdSetEvent", (const char *) pName, sizeof ("xglCmdSetEvent")))
1065 return disp_table->CmdSetEvent;
1066 else if (!strncmp("xglCmdResetEvent", (const char *) pName, sizeof ("xglCmdResetEvent")))
1067 return disp_table->CmdResetEvent;
1068 else if (!strncmp("xglCmdMemoryAtomic", (const char *) pName, sizeof ("xglCmdMemoryAtomic")))
1069 return disp_table->CmdMemoryAtomic;
1070 else if (!strncmp("xglCmdBeginQuery", (const char *) pName, sizeof ("xglCmdBeginQuery")))
1071 return disp_table->CmdBeginQuery;
1072 else if (!strncmp("xglCmdEndQuery", (const char *) pName, sizeof ("xglCmdEndQuery")))
1073 return disp_table->CmdEndQuery;
1074 else if (!strncmp("xglCmdResetQueryPool", (const char *) pName, sizeof ("xglCmdResetQueryPool")))
1075 return disp_table->CmdResetQueryPool;
1076 else if (!strncmp("xglCmdWriteTimestamp", (const char *) pName, sizeof ("xglCmdWriteTimestamp")))
1077 return disp_table->CmdWriteTimestamp;
1078 else if (!strncmp("xglCmdInitAtomicCounters", (const char *) pName, sizeof ("xglCmdInitAtomicCounters")))
1079 return disp_table->CmdInitAtomicCounters;
1080 else if (!strncmp("xglCmdLoadAtomicCounters", (const char *) pName, sizeof ("xglCmdLoadAtomicCounters")))
1081 return disp_table->CmdLoadAtomicCounters;
1082 else if (!strncmp("xglCmdSaveAtomicCounters", (const char *) pName, sizeof ("xglCmdSaveAtomicCounters")))
1083 return disp_table->CmdSaveAtomicCounters;
1084 else if (!strncmp("xglDbgSetValidationLevel", (const char *) pName, sizeof ("xglDbgSetValidationLevel")))
1085 return disp_table->DbgSetValidationLevel;
1086 else if (!strncmp("xglDbgRegisterMsgCallback", (const char *) pName, sizeof ("xglDbgRegisterMsgCallback")))
1087 return disp_table->DbgRegisterMsgCallback;
1088 else if (!strncmp("xglDbgUnregisterMsgCallback", (const char *) pName, sizeof ("xglDbgUnregisterMsgCallback")))
1089 return disp_table->DbgUnregisterMsgCallback;
1090 else if (!strncmp("xglDbgSetMessageFilter", (const char *) pName, sizeof ("xglDbgSetMessageFilter")))
1091 return disp_table->DbgSetMessageFilter;
1092 else if (!strncmp("xglDbgSetObjectTag", (const char *) pName, sizeof ("xglDbgSetObjectTag")))
1093 return disp_table->DbgSetObjectTag;
1094 else if (!strncmp("xglDbgSetGlobalOption", (const char *) pName, sizeof ("xglDbgSetGlobalOption")))
1095 return disp_table->DbgSetGlobalOption;
1096 else if (!strncmp("xglDbgSetDeviceOption", (const char *) pName, sizeof ("xglDbgSetDeviceOption")))
1097 return disp_table->DbgSetDeviceOption;
1098 else if (!strncmp("xglCmdDbgMarkerBegin", (const char *) pName, sizeof ("xglCmdDbgMarkerBegin")))
1099 return disp_table->CmdDbgMarkerBegin;
1100 else if (!strncmp("xglCmdDbgMarkerEnd", (const char *) pName, sizeof ("xglCmdDbgMarkerEnd")))
1101 return disp_table->CmdDbgMarkerEnd;
1102 else if (!strncmp("xglWsiX11AssociateConnection", (const char *) pName, sizeof("xglWsiX11AssociateConnection")))
1103 return disp_table->WsiX11AssociateConnection;
1104 else if (!strncmp("xglWsiX11GetMSC", (const char *) pName, sizeof("xglWsiX11GetMSC")))
1105 return disp_table->WsiX11GetMSC;
1106 else if (!strncmp("xglWsiX11CreatePresentableImage", (const char *) pName, sizeof("xglWsiX11CreatePresentableImage")))
1107 return disp_table->WsiX11CreatePresentableImage;
1108 else if (!strncmp("xglWsiX11QueuePresent", (const char *) pName, sizeof("xglWsiX11QueuePresent")))
1109 return disp_table->WsiX11QueuePresent;
1110 else {
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001111 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001112 return NULL;
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001113 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001114 }
1115}
1116
Chia-I Wu468e3c32014-08-04 08:03:57 +08001117LOADER_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 +08001118{
1119 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburn815bddd2014-10-16 15:48:50 -06001120 struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001121 XGL_UINT count = 0;
1122 XGL_RESULT res;
1123
Jon Ashburn815bddd2014-10-16 15:48:50 -06001124 // cleanup any prior layer initializations
Jon Ashburnb55278a2014-10-17 15:09:07 -06001125 loader_deactivate_layer();
Jon Ashburn815bddd2014-10-16 15:48:50 -06001126
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001127 pthread_once(&once, loader_icd_scan);
1128
1129 if (!loader.icds)
1130 return XGL_ERROR_UNAVAILABLE;
1131
1132 icd = loader.icds;
1133 while (icd) {
1134 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001135 XGL_BASE_LAYER_OBJECT * wrappedGpus;
1136 GetProcAddrType getProcAddr = icd->GetProcAddr;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001137 XGL_UINT n, max = maxGpus - count;
1138
1139 if (max > XGL_MAX_PHYSICAL_GPUS) {
1140 max = XGL_MAX_PHYSICAL_GPUS;
1141 }
1142
1143 res = icd->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu74916ed2014-08-06 12:17:04 +08001144 if (res == XGL_SUCCESS && n) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001145 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn183dfd02014-10-22 18:13:16 -06001146 icd->gpus = wrappedGpus;
Jon Ashburnb55278a2014-10-17 15:09:07 -06001147 icd->gpu_count = n;
Jon Ashburn815bddd2014-10-16 15:48:50 -06001148 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001149 for (int i = 0; i < n; i++) {
1150 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -06001151 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001152 (wrappedGpus + i)->nextObject = gpus[i];
1153 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn8eecd422014-10-22 12:42:13 -06001154 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001155 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -06001156 *disp = icd->loader_dispatch + i;
1157 icd->SetDispatch(icd->loader_dispatch + i, true);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001158 }
1159
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001160 count += n;
1161
1162 if (count >= maxGpus) {
1163 break;
1164 }
1165 }
1166
1167 icd = icd->next;
1168 }
1169
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001170 /* get layer libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -06001171 if (!loader.layer_scanned)
1172 layer_lib_scan(NULL, true);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001173
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001174 *pGpuCount = count;
1175
1176 return (count > 0) ? XGL_SUCCESS : res;
1177}
1178
Jon Ashburn6847c2b2014-11-25 12:56:49 -07001179LOADER_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 -06001180{
Jon Ashburn183dfd02014-10-22 18:13:16 -06001181 XGL_SIZE count = loader.scanned_layer_count;
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001182 // TODO handle layers per GPU, multiple icds
1183
1184 if (pOutLayerCount == NULL)
1185 return XGL_ERROR_INVALID_POINTER;
1186
Jon Ashburn183dfd02014-10-22 18:13:16 -06001187 if (maxLayerCount < loader.scanned_layer_count)
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001188 count = maxLayerCount;
1189 *pOutLayerCount = count;
1190
1191 if (pOutLayers == NULL)
1192 return XGL_SUCCESS;
1193 for (XGL_SIZE i = 0; i < count; i++) {
Jon Ashburn183dfd02014-10-22 18:13:16 -06001194 strncpy((char *) (pOutLayers[i]), loader.scanned_layer_names[i], maxStringSize);
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001195 if (maxStringSize > 0)
1196 pOutLayers[i][maxStringSize - 1] = '\0';
1197 }
1198 return XGL_SUCCESS;
1199}
1200
Chia-I Wu468e3c32014-08-04 08:03:57 +08001201LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001202{
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001203 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001204 XGL_RESULT res;
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001205 XGL_UINT gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001206
1207 if (!loader.scanned) {
1208 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1209 }
1210
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001211 for (icd = loader.icds; icd; icd = icd->next) {
1212 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1213 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
1214 if (res != XGL_SUCCESS) {
1215 gpu_idx = i;
1216 break;
1217 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001218 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001219 if (res != XGL_SUCCESS)
1220 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001221 }
1222
1223 /* roll back on errors */
1224 if (icd) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001225 for (const struct loader_icd * tmp = loader.icds; tmp != icd; tmp = tmp->next) {
1226 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
1227 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001228 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001229 /* and gpus on current icd */
1230 for (XGL_UINT i = 0; i < gpu_idx; i++)
1231 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001232
1233 return res;
1234 }
1235
1236 return XGL_SUCCESS;
1237}
1238
Chia-I Wu468e3c32014-08-04 08:03:57 +08001239LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001240{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001241 XGL_RESULT res = XGL_SUCCESS;
1242
1243 if (!loader.scanned) {
1244 return loader_msg_callback_remove(pfnMsgCallback);
1245 }
1246
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001247 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
1248 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1249 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1250 if (r != XGL_SUCCESS) {
1251 res = r;
1252 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001253 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001254 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001255 return res;
1256}
1257
Chia-I Wu468e3c32014-08-04 08:03:57 +08001258LOADER_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 +08001259{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001260 XGL_RESULT res = XGL_SUCCESS;
1261
1262 if (!loader.scanned) {
1263 if (dataSize == 0)
1264 return XGL_ERROR_INVALID_VALUE;
1265
1266 switch (dbgOption) {
1267 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1268 loader.debug_echo_enable = *((const bool *) pData);
1269 break;
1270 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1271 loader.break_on_error = *((const bool *) pData);
1272 break;
1273 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1274 loader.break_on_warning = *((const bool *) pData);
1275 break;
1276 default:
1277 res = XGL_ERROR_INVALID_VALUE;
1278 break;
1279 }
1280
1281 return res;
1282 }
1283
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001284 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
1285 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1286 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
1287 /* unfortunately we cannot roll back */
1288 if (r != XGL_SUCCESS) {
1289 res = r;
1290 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001291 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001292 }
1293
1294 return res;
1295}