blob: bb4d94951fb5a0cbf55eb2840d8a6e434a6b2675 [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;
48 char lib_name[1024];
49};
50
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080051struct loader_icd {
52 void *handle;
53
Jon Ashburnb55278a2014-10-17 15:09:07 -060054 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Jon Ashburn183dfd02014-10-22 18:13:16 -060055 XGL_UINT layer_count[XGL_MAX_PHYSICAL_GPUS];
56 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnb4d00532014-10-22 21:15:26 -060057 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnb55278a2014-10-17 15:09:07 -060058 XGL_UINT gpu_count;
Jon Ashburn183dfd02014-10-22 18:13:16 -060059 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburn815bddd2014-10-16 15:48:50 -060060
Jon Ashburnd43f9b62014-10-14 19:15:22 -060061 GetProcAddrType GetProcAddr;
62 InitAndEnumerateGpusType InitAndEnumerateGpus;
Jon Ashburn815bddd2014-10-16 15:48:50 -060063 SetDispatchType SetDispatch;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080064
65 struct loader_icd *next;
66};
67
Jon Ashburnd43f9b62014-10-14 19:15:22 -060068
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080069struct loader_msg_callback {
70 XGL_DBG_MSG_CALLBACK_FUNCTION func;
71 XGL_VOID *data;
72
73 struct loader_msg_callback *next;
74};
75
Jon Ashburnd43f9b62014-10-14 19:15:22 -060076
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080077static struct {
78 bool scanned;
79 struct loader_icd *icds;
Jon Ashburn183dfd02014-10-22 18:13:16 -060080 bool layer_scanned;
Jon Ashburnd43f9b62014-10-14 19:15:22 -060081 char layer_dirs[4096];
Jon Ashburn183dfd02014-10-22 18:13:16 -060082 unsigned int scanned_layer_count;
83 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080084 struct loader_msg_callback *msg_callbacks;
85
86 bool debug_echo_enable;
87 bool break_on_error;
88 bool break_on_warning;
89} loader;
90
91static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
92 XGL_VOID *data)
93{
94 struct loader_msg_callback *cb;
95
96 cb = malloc(sizeof(*cb));
97 if (!cb)
98 return XGL_ERROR_OUT_OF_MEMORY;
99
100 cb->func = func;
101 cb->data = data;
102
103 cb->next = loader.msg_callbacks;
104 loader.msg_callbacks = cb;
105
106 return XGL_SUCCESS;
107}
108
109static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
110{
111 struct loader_msg_callback *cb = loader.msg_callbacks;
112
113 /*
114 * Find the first match (last registered).
115 *
116 * XXX What if the same callback function is registered more than once?
117 */
118 while (cb) {
119 if (cb->func == func) {
120 break;
121 }
122
123 cb = cb->next;
124 }
125
126 if (!cb)
127 return XGL_ERROR_INVALID_POINTER;
128
129 free(cb);
130
131 return XGL_SUCCESS;
132}
133
134static void loader_msg_callback_clear(void)
135{
136 struct loader_msg_callback *cb = loader.msg_callbacks;
137
138 while (cb) {
139 struct loader_msg_callback *next = cb->next;
140 free(cb);
141 cb = next;
142 }
143
144 loader.msg_callbacks = NULL;
145}
146
147static void loader_log(XGL_DBG_MSG_TYPE msg_type, XGL_INT msg_code,
148 const char *format, ...)
149{
150 const struct loader_msg_callback *cb = loader.msg_callbacks;
151 char msg[256];
152 va_list ap;
153 int ret;
154
155 va_start(ap, format);
156 ret = vsnprintf(msg, sizeof(msg), format, ap);
157 if (ret >= sizeof(msg) || ret < 0) {
158 msg[sizeof(msg) - 1] = '\0';
159 }
160 va_end(ap);
161
162 if (loader.debug_echo_enable || !cb) {
163 fputs(msg, stderr);
164 fputc('\n', stderr);
165 }
166
167 while (cb) {
168 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
169 msg_code, (const XGL_CHAR *) msg, cb->data);
170 cb = cb->next;
171 }
172
173 switch (msg_type) {
174 case XGL_DBG_MSG_ERROR:
175 if (loader.break_on_error) {
176 exit(1);
177 }
178 /* fall through */
179 case XGL_DBG_MSG_WARNING:
180 if (loader.break_on_warning) {
181 exit(1);
182 }
183 break;
184 default:
185 break;
186 }
187}
188
189static void
190loader_icd_destroy(struct loader_icd *icd)
191{
192 dlclose(icd->handle);
193 free(icd);
194}
195
196static struct loader_icd *
197loader_icd_create(const char *filename)
198{
199 struct loader_icd *icd;
200
201 icd = malloc(sizeof(*icd));
202 if (!icd)
203 return NULL;
204
Courtney Goeltzenleuchter6f928162014-10-28 10:29:27 -0600205 memset(icd, 0, sizeof(*icd));
206
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800207 icd->handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
208 if (!icd->handle) {
209 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror());
210 free(icd);
211 return NULL;
212 }
213
214#define LOOKUP(icd, func) do { \
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600215 icd->func = (func## Type) dlsym(icd->handle, "xgl" #func); \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800216 if (!icd->func) { \
217 loader_log(XGL_DBG_MSG_WARNING, 0, dlerror()); \
218 loader_icd_destroy(icd); \
219 return NULL; \
220 } \
221} while (0)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600222 LOOKUP(icd, GetProcAddr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800223 LOOKUP(icd, InitAndEnumerateGpus);
Jon Ashburn815bddd2014-10-16 15:48:50 -0600224 LOOKUP(icd, SetDispatch);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800225#undef LOOKUP
226
227 return icd;
228}
229
230static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
231{
232 const struct loader_msg_callback *cb = loader.msg_callbacks;
233 XGL_RESULT res;
234
235 while (cb) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700236 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
237 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
238 if (res != XGL_SUCCESS) {
239 break;
240 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800241 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242 cb = cb->next;
243 }
244
245 /* roll back on errors */
246 if (cb) {
247 const struct loader_msg_callback *tmp = loader.msg_callbacks;
248
249 while (tmp != cb) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700250 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
251 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
252 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800253 tmp = tmp->next;
254 }
255
256 return res;
257 }
258
259 return XGL_SUCCESS;
260}
261
262static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
263{
264#define SETB(icd, opt, val) do { \
265 if (val) { \
Jon Ashburn406a0fe2014-11-14 09:52:42 -0700266 for (XGL_UINT i = 0; i < icd->gpu_count; i++) { \
267 const XGL_RESULT res = \
268 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
269 if (res != XGL_SUCCESS) \
270 return res; \
271 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800272 } \
273} while (0)
274 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
275 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
276 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
277#undef SETB
278
279return XGL_SUCCESS;
280}
281
Chia-I Wu894a1172014-08-04 11:18:20 +0800282static struct loader_icd *loader_icd_add(const char *filename)
283{
284 struct loader_icd *icd;
285
286 icd = loader_icd_create(filename);
287 if (!icd)
288 return NULL;
289
290 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
291 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
292 loader_log(XGL_DBG_MSG_WARNING, 0,
293 "%s ignored: failed to migrate settings", filename);
294 loader_icd_destroy(icd);
295 }
296
297 /* prepend to the list */
298 icd->next = loader.icds;
299 loader.icds = icd;
300
301 return icd;
302}
303
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600304#ifndef DEFAULT_XGL_DRIVERS_PATH
305// TODO: Is this a good default location?
306// Need to search for both 32bit and 64bit ICDs
307#define DEFAULT_XGL_DRIVERS_PATH "/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
308#endif
309
310/**
311 * Try to \c loader_icd_scan XGL driver(s).
312 *
313 * This function scans the default system path or path
314 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
315 * order to find loadable XGL ICDs with the name of libXGL_*.
316 *
317 * \returns
318 * void; but side effect is to set loader_icd_scanned to true
319 */
320static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800321{
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600322 const char *libPaths, *p, *next;
323 DIR *sysdir;
324 struct dirent *dent;
325 char icd_library[1024];
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600326 char path[1024];
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600327 int len;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800328
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600329 libPaths = NULL;
330 if (geteuid() == getuid()) {
331 /* don't allow setuid apps to use LIBXGL_DRIVERS_PATH */
332 libPaths = getenv("LIBXGL_DRIVERS_PATH");
333 }
334 if (libPaths == NULL)
335 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800336
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600337 for (p = libPaths; *p; p = next) {
338 next = strchr(p, ':');
339 if (next == NULL) {
340 len = strlen(p);
341 next = p + len;
342 }
343 else {
344 len = next - p;
Jon Ashburn0f45b2a2014-10-03 16:31:35 -0600345 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
346 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600347 next++;
348 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800349
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600350 sysdir = opendir(p);
351 if (sysdir) {
352 dent = readdir(sysdir);
353 while (dent) {
354 /* look for ICDs starting with "libXGL_" */
355 if (!strncmp(dent->d_name, "libXGL_", 7)) {
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600356 snprintf(icd_library, 1024, "%s/%s",p,dent->d_name);
Chia-I Wu894a1172014-08-04 11:18:20 +0800357 loader_icd_add(icd_library);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600358 }
359
360 dent = readdir(sysdir);
361 }
362 closedir(sysdir);
363 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800364 }
365
366 /* we have nothing to log anymore */
367 loader_msg_callback_clear();
368
369 loader.scanned = true;
370}
371
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600372#ifndef DEFAULT_XGL_LAYERS_PATH
373// TODO: Is this a good default locations
374#define DEFAULT_XGL_LAYERS_PATH ".:/usr/lib/i386-linux-gnu/xgl:/usr/lib/x86_64-linux-gnu/xgl"
375#endif
376
Jon Ashburn183dfd02014-10-22 18:13:16 -0600377static void layer_lib_scan(const char * libInPaths, const bool useDefaultDirs)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600378{
379 const char *p, *next;
380 char *libPaths = &loader.layer_dirs[0];
381 DIR *curdir;
382 struct dirent *dent;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600383 int len, i;
384 char temp_str[1024];
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600385
386 if (libInPaths){
387 strncpy(libPaths, libInPaths, sizeof(loader.layer_dirs));
388 }
389 else {
390 *libPaths = '\0';
391 }
392
393 /* cleanup any previously scanned libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -0600394 for (i = 0; i < loader.scanned_layer_count; i++) {
395 if (loader.scanned_layer_names[i] != NULL)
396 free(loader.scanned_layer_names[i]);
397 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600398 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600399 loader.scanned_layer_count = 0;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600400
401 if (useDefaultDirs)
402 strncat(libPaths, DEFAULT_XGL_LAYERS_PATH, sizeof(loader.layer_dirs) - sizeof(DEFAULT_XGL_LAYERS_PATH));
403
404 for (p = libPaths; *p; p = next) {
405 next = strchr(p, ':');
406 if (next == NULL) {
407 len = strlen(p);
408 next = p + len;
409 }
410 else {
411 len = next - p;
412 *(char *) next = '\0';
413 next++;
414 }
415
416 curdir = opendir(p);
417 if (curdir) {
418 dent = readdir(curdir);
419 while (dent) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600420 /* look for wrappers starting with "libXGLlayer" */
421 if (!strncmp(dent->d_name, "libXGLLayer", strlen("libXGLLayer"))) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600422 void * handle;
423 snprintf(temp_str, sizeof(temp_str), "%s/%s",p,dent->d_name);
424 if ((handle = dlopen((const char *) temp_str, RTLD_LAZY)) == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600425 continue;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600426 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
427 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
428 break;
429 }
430 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
431 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
432 break;
433 }
434 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
435 loader.scanned_layer_count++;
436 dlclose(handle);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600437 }
438
439 dent = readdir(curdir);
440 }
441 closedir(curdir);
442 }
443 }
444
Jon Ashburn183dfd02014-10-22 18:13:16 -0600445 loader.layer_scanned = true;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600446}
447
Jon Ashburnb55278a2014-10-17 15:09:07 -0600448static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, GetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600449{
450 tab->GetProcAddr = fpGPA;
451 tab->InitAndEnumerateGpus = fpGPA(gpu, (const XGL_CHAR *) "xglInitAndEnumerateGpus");
452 tab->GetGpuInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetGpuInfo");
453 tab->CreateDevice = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDevice");
454 tab->DestroyDevice = fpGPA(gpu, (const XGL_CHAR *) "xglDestroyDevice");
455 tab->GetExtensionSupport = fpGPA(gpu, (const XGL_CHAR *) "xglGetExtensionSupport");
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600456 tab->EnumerateLayers = fpGPA(gpu, (const XGL_CHAR *) "xglEnumerateLayers");
457 if (tab->EnumerateLayers == NULL)
458 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600459 tab->GetDeviceQueue = fpGPA(gpu, (const XGL_CHAR *) "xglGetDeviceQueue");
460 tab->QueueSubmit = fpGPA(gpu, (const XGL_CHAR *) "xglQueueSubmit");
461 tab->QueueSetGlobalMemReferences = fpGPA(gpu, (const XGL_CHAR *) "xglQueueSetGlobalMemReferences");
462 tab->QueueWaitIdle = fpGPA(gpu, (const XGL_CHAR *) "xglQueueWaitIdle");
463 tab->DeviceWaitIdle = fpGPA(gpu, (const XGL_CHAR *) "xglDeviceWaitIdle");
464 tab->GetMemoryHeapCount = fpGPA(gpu, (const XGL_CHAR *) "xglGetMemoryHeapCount");
465 tab->GetMemoryHeapInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetMemoryHeapInfo");
466 tab->AllocMemory = fpGPA(gpu, (const XGL_CHAR *) "xglAllocMemory");
467 tab->FreeMemory = fpGPA(gpu, (const XGL_CHAR *) "xglFreeMemory");
468 tab->SetMemoryPriority = fpGPA(gpu, (const XGL_CHAR *) "xglSetMemoryPriority");
469 tab->MapMemory = fpGPA(gpu, (const XGL_CHAR *) "xglMapMemory");
470 tab->UnmapMemory = fpGPA(gpu, (const XGL_CHAR *) "xglUnmapMemory");
471 tab->PinSystemMemory = fpGPA(gpu, (const XGL_CHAR *) "xglPinSystemMemory");
472 tab->RemapVirtualMemoryPages = fpGPA(gpu, (const XGL_CHAR *) "xglRemapVirtualMemoryPages");
473 tab->GetMultiGpuCompatibility = fpGPA(gpu, (const XGL_CHAR *) "xglGetMultiGpuCompatibility");
474 tab->OpenSharedMemory = fpGPA(gpu, (const XGL_CHAR *) "xglOpenSharedMemory");
475 tab->OpenSharedQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglOpenSharedQueueSemaphore");
476 tab->OpenPeerMemory = fpGPA(gpu, (const XGL_CHAR *) "xglOpenPeerMemory");
477 tab->OpenPeerImage = fpGPA(gpu, (const XGL_CHAR *) "xglOpenPeerImage");
478 tab->DestroyObject = fpGPA(gpu, (const XGL_CHAR *) "xglDestroyObject");
479 tab->GetObjectInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetObjectInfo");
480 tab->BindObjectMemory = fpGPA(gpu, (const XGL_CHAR *) "xglBindObjectMemory");
481 tab->CreateFence = fpGPA(gpu, (const XGL_CHAR *) "xglCreateFence");
482 tab->GetFenceStatus = fpGPA(gpu, (const XGL_CHAR *) "xglGetFenceStatus");
483 tab->WaitForFences = fpGPA(gpu, (const XGL_CHAR *) "xglWaitForFences");
484 tab->CreateQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglCreateQueueSemaphore");
485 tab->SignalQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglSignalQueueSemaphore");
486 tab->WaitQueueSemaphore = fpGPA(gpu, (const XGL_CHAR *) "xglWaitQueueSemaphore");
487 tab->CreateEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCreateEvent");
488 tab->GetEventStatus = fpGPA(gpu, (const XGL_CHAR *) "xglGetEventStatus");
489 tab->SetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglSetEvent");
490 tab->ResetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglResetEvent");
491 tab->CreateQueryPool = fpGPA(gpu, (const XGL_CHAR *) "xglCreateQueryPool");
492 tab->GetQueryPoolResults = fpGPA(gpu, (const XGL_CHAR *) "xglGetQueryPoolResults");
493 tab->GetFormatInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetFormatInfo");
494 tab->CreateImage = fpGPA(gpu, (const XGL_CHAR *) "xglCreateImage");
495 tab->GetImageSubresourceInfo = fpGPA(gpu, (const XGL_CHAR *) "xglGetImageSubresourceInfo");
496 tab->CreateImageView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateImageView");
497 tab->CreateColorAttachmentView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateColorAttachmentView");
498 tab->CreateDepthStencilView = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDepthStencilView");
499 tab->CreateShader = fpGPA(gpu, (const XGL_CHAR *) "xglCreateShader");
500 tab->CreateGraphicsPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCreateGraphicsPipeline");
501 tab->CreateComputePipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCreateComputePipeline");
502 tab->StorePipeline = fpGPA(gpu, (const XGL_CHAR *) "xglStorePipeline");
503 tab->LoadPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglLoadPipeline");
504 tab->CreatePipelineDelta = fpGPA(gpu, (const XGL_CHAR *) "xglCreatePipelineDelta");
505 tab->CreateSampler = fpGPA(gpu, (const XGL_CHAR *) "xglCreateSampler");
506 tab->CreateDescriptorSet = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDescriptorSet");
507 tab->BeginDescriptorSetUpdate = fpGPA(gpu, (const XGL_CHAR *) "xglBeginDescriptorSetUpdate");
508 tab->EndDescriptorSetUpdate = fpGPA(gpu, (const XGL_CHAR *) "xglEndDescriptorSetUpdate");
509 tab->AttachSamplerDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachSamplerDescriptors");
510 tab->AttachImageViewDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachImageViewDescriptors");
511 tab->AttachMemoryViewDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachMemoryViewDescriptors");
512 tab->AttachNestedDescriptors = fpGPA(gpu, (const XGL_CHAR *) "xglAttachNestedDescriptors");
513 tab->ClearDescriptorSetSlots = fpGPA(gpu, (const XGL_CHAR *) "xglClearDescriptorSetSlots");
514 tab->CreateViewportState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateViewportState");
515 tab->CreateRasterState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateRasterState");
516 tab->CreateMsaaState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateMsaaState");
517 tab->CreateColorBlendState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateColorBlendState");
518 tab->CreateDepthStencilState = fpGPA(gpu, (const XGL_CHAR *) "xglCreateDepthStencilState");
519 tab->CreateCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglCreateCommandBuffer");
520 tab->BeginCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglBeginCommandBuffer");
521 tab->EndCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglEndCommandBuffer");
522 tab->ResetCommandBuffer = fpGPA(gpu, (const XGL_CHAR *) "xglResetCommandBuffer");
523 tab->CmdBindPipeline = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindPipeline");
524 tab->CmdBindPipelineDelta = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindPipelineDelta");
525 tab->CmdBindStateObject = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindStateObject");
526 tab->CmdBindDescriptorSet = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindDescriptorSet");
527 tab->CmdBindDynamicMemoryView = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindDynamicMemoryView");
Chia-I Wu3b04af52014-11-08 10:48:20 +0800528 tab->CmdBindVertexData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindVertexData");
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600529 tab->CmdBindIndexData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindIndexData");
530 tab->CmdBindAttachments = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBindAttachments");
531 tab->CmdPrepareMemoryRegions = fpGPA(gpu, (const XGL_CHAR *) "xglCmdPrepareMemoryRegions");
532 tab->CmdPrepareImages = fpGPA(gpu, (const XGL_CHAR *) "xglCmdPrepareImages");
533 tab->CmdDraw = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDraw");
534 tab->CmdDrawIndexed = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndexed");
535 tab->CmdDrawIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndirect");
536 tab->CmdDrawIndexedIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDrawIndexedIndirect");
537 tab->CmdDispatch = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDispatch");
538 tab->CmdDispatchIndirect = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDispatchIndirect");
539 tab->CmdCopyMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyMemory");
540 tab->CmdCopyImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyImage");
541 tab->CmdCopyMemoryToImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyMemoryToImage");
542 tab->CmdCopyImageToMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCopyImageToMemory");
543 tab->CmdCloneImageData = fpGPA(gpu, (const XGL_CHAR *) "xglCmdCloneImageData");
544 tab->CmdUpdateMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdUpdateMemory");
545 tab->CmdFillMemory = fpGPA(gpu, (const XGL_CHAR *) "xglCmdFillMemory");
546 tab->CmdClearColorImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearColorImage");
547 tab->CmdClearColorImageRaw = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearColorImageRaw");
548 tab->CmdClearDepthStencil = fpGPA(gpu, (const XGL_CHAR *) "xglCmdClearDepthStencil");
549 tab->CmdResolveImage = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResolveImage");
550 tab->CmdSetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCmdSetEvent");
551 tab->CmdResetEvent = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResetEvent");
552 tab->CmdMemoryAtomic = fpGPA(gpu, (const XGL_CHAR *) "xglCmdMemoryAtomic");
553 tab->CmdBeginQuery = fpGPA(gpu, (const XGL_CHAR *) "xglCmdBeginQuery");
554 tab->CmdEndQuery = fpGPA(gpu, (const XGL_CHAR *) "xglCmdEndQuery");
555 tab->CmdResetQueryPool = fpGPA(gpu, (const XGL_CHAR *) "xglCmdResetQueryPool");
556 tab->CmdWriteTimestamp = fpGPA(gpu, (const XGL_CHAR *) "xglCmdWriteTimestamp");
557 tab->CmdInitAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdInitAtomicCounters");
558 tab->CmdLoadAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdLoadAtomicCounters");
559 tab->CmdSaveAtomicCounters = fpGPA(gpu, (const XGL_CHAR *) "xglCmdSaveAtomicCounters");
560 tab->DbgSetValidationLevel = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetValidationLevel");
561 tab->DbgRegisterMsgCallback = fpGPA(gpu, (const XGL_CHAR *) "xglDbgRegisterMsgCallback");
562 tab->DbgUnregisterMsgCallback = fpGPA(gpu, (const XGL_CHAR *) "xglDbgUnregisterMsgCallback");
563 tab->DbgSetMessageFilter = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetMessageFilter");
564 tab->DbgSetObjectTag = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetObjectTag");
565 tab->DbgSetGlobalOption = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetGlobalOption");
566 tab->DbgSetDeviceOption = fpGPA(gpu, (const XGL_CHAR *) "xglDbgSetDeviceOption");
567 tab->CmdDbgMarkerBegin = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDbgMarkerBegin");
568 tab->CmdDbgMarkerEnd = fpGPA(gpu, (const XGL_CHAR *) "xglCmdDbgMarkerEnd");
569 tab->WsiX11AssociateConnection = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11AssociateConnection");
570 tab->WsiX11GetMSC = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11GetMSC");
571 tab->WsiX11CreatePresentableImage = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11CreatePresentableImage");
572 tab->WsiX11QueuePresent = fpGPA(gpu, (const XGL_CHAR *) "xglWsiX11QueuePresent");
573}
574
Jon Ashburnb55278a2014-10-17 15:09:07 -0600575static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, XGL_UINT *gpu_index)
576{
577 for (struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
578 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600579 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject == gpu->baseObject) {
Jon Ashburnb55278a2014-10-17 15:09:07 -0600580 *gpu_index = i;
581 return icd;
582 }
583 }
584 return NULL;
585}
586
Jon Ashburn183dfd02014-10-22 18:13:16 -0600587static bool loader_layers_activated(const struct loader_icd *icd, const XGL_UINT gpu_index)
Jon Ashburnb55278a2014-10-17 15:09:07 -0600588{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600589 if (icd->layer_count[gpu_index])
590 return true;
591 else
592 return false;
Jon Ashburnb55278a2014-10-17 15:09:07 -0600593}
594
Jon Ashburn183dfd02014-10-22 18:13:16 -0600595static void loader_init_layer_libs(struct loader_icd *icd, XGL_UINT gpu_index, XGL_CHAR ** ppLayerNames, XGL_UINT count)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600596{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600597 if (!icd)
598 return;
Jon Ashburn815bddd2014-10-16 15:48:50 -0600599
Jon Ashburn183dfd02014-10-22 18:13:16 -0600600 struct loader_layers *obj;
601 bool foundLib;
602 for (XGL_UINT i = 0; i < count; i++) {
603 foundLib = false;
604 for (XGL_UINT j = 0; j < icd->layer_count[gpu_index]; j++) {
605 if (icd->layer_libs[gpu_index][j].lib_handle && !strcmp(icd->layer_libs[gpu_index][j].lib_name, (char *) ppLayerNames[i])) {
606 foundLib = true;
607 break;
608 }
609 }
610 if (!foundLib) {
611 obj = &(icd->layer_libs[gpu_index][i]);
612 strncpy(obj->lib_name, (char *) ppLayerNames[i], sizeof(obj->lib_name) - 1);
613 obj->lib_name[sizeof(obj->lib_name) - 1] = '\0';
614 if ((obj->lib_handle = dlopen(obj->lib_name, RTLD_LAZY | RTLD_DEEPBIND)) == NULL) {
615 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to open layer library %s got error %d", obj->lib_name, dlerror());
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600616 continue;
617 } else {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600618 loader_log(XGL_DBG_MSG_UNKNOWN, 0, "Inserting layer lib %s", obj->lib_name);
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600619 }
Jon Ashburnb4d00532014-10-22 21:15:26 -0600620 free(ppLayerNames[i]);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600621 icd->layer_count[gpu_index]++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600622 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600623 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600624}
625
Jon Ashburn183dfd02014-10-22 18:13:16 -0600626static XGL_UINT loader_get_layer_env(XGL_CHAR * *ppLayerNames)
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600627{
Jon Ashburn183dfd02014-10-22 18:13:16 -0600628 const char *layerEnv;
629 XGL_UINT len, count = 0;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600630 char *p, *pOrig, *next, *name;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600631
Jon Ashburn183dfd02014-10-22 18:13:16 -0600632 layerEnv = getenv("LIBXGL_LAYER_LIBS");
Jon Ashburn4fbcb032014-10-23 10:29:09 -0600633 if (!layerEnv)
634 return 0;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600635 p = malloc(strlen(layerEnv) + 1);
636 if (!p)
637 return 0;
638 strcpy(p, layerEnv);
Jon Ashburnb4d00532014-10-22 21:15:26 -0600639 pOrig = p;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600640
Jon Ashburn183dfd02014-10-22 18:13:16 -0600641 while (p && *p && count < MAX_LAYER_LIBRARIES) {
642 bool foundScanned = false;
643 unsigned int j;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600644 next = strchr(p, ':');
645 if (next == NULL) {
646 len = strlen(p);
647 next = p + len;
648 }
649 else {
650 len = next - p;
651 *(char *) next = '\0';
652 next++;
653 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600654 name = basename(p);
655 for (j = 0; j < loader.scanned_layer_count; j++) {
656 if (!strcmp(basename(loader.scanned_layer_names[j]), name)) {
657 foundScanned = true;
658 break;
659 }
660 }
661 if (!foundScanned) {
662 p = next;
663 continue;
664 }
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600665
Jon Ashburn183dfd02014-10-22 18:13:16 -0600666 //copy to convert any dir path differences between scanned and base names
667 len = strlen(loader.scanned_layer_names[j]);
668 ppLayerNames[count] = malloc(len + 1);
Jon Ashburnb4d00532014-10-22 21:15:26 -0600669 if (!ppLayerNames[count]) {
670 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600671 return count;
Jon Ashburnb4d00532014-10-22 21:15:26 -0600672 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600673 strncpy((char *) ppLayerNames[count], loader.scanned_layer_names[j], len);
674 ppLayerNames[count][len] = '\0';
675 count++;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600676 p = next;
677
Jon Ashburn183dfd02014-10-22 18:13:16 -0600678 };
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600679
Jon Ashburnb4d00532014-10-22 21:15:26 -0600680 free(pOrig);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600681 return count;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600682}
683
Jon Ashburn183dfd02014-10-22 18:13:16 -0600684static XGL_UINT loader_get_layer_libs(const XGL_DEVICE_CREATE_INFO* pCreateInfo, XGL_CHAR *** ppLayerNames)
685{
686 static XGL_CHAR *layerNames[MAX_LAYER_LIBRARIES];
687
688 *ppLayerNames = &layerNames[0];
689 if (!pCreateInfo) {
690 return loader_get_layer_env(layerNames);
691 }
692
693 XGL_LAYER_CREATE_INFO *pCi = (XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
694
695 while (pCi) {
696 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
697 const char *name;
698 XGL_UINT len;
699 unsigned int j;
700 for (XGL_UINT i = 0; i < pCi->layerCount; i++) {
701 bool foundScanned = false;
702 name = (const char *) *(pCi->ppActiveLayerNames + i);
703 for (j = 0; j < loader.scanned_layer_count; j++) {
704 if (!strcmp(basename(loader.scanned_layer_names[j]), basename(name))) {
705 foundScanned = true;
706 break;
707 }
708 }
709 if (!foundScanned)
710 return loader_get_layer_env(layerNames);
711 //copy to convert any dir path differences between scanned and base names
712 len = strlen(loader.scanned_layer_names[j]);
713 layerNames[i] = malloc(len + 1);
714 if (!layerNames[i])
715 return i;
716 strncpy((char *) layerNames[i], loader.scanned_layer_names[j], len);
717 layerNames[i][len] = '\0';
718 }
719 return pCi->layerCount;
720 }
721 pCi = pCi->pNext;
722 }
723 return loader_get_layer_env(layerNames);
724}
725
726static void loader_deactivate_layer()
727{
728 struct loader_icd *icd;
729 struct loader_layers *libs;
730
731 for (icd = loader.icds; icd; icd = icd->next) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600732 if (icd->gpus)
733 free(icd->gpus);
734 icd->gpus = NULL;
735 if (icd->loader_dispatch)
736 free(icd->loader_dispatch);
737 icd->loader_dispatch = NULL;
738 icd->SetDispatch(NULL, true);
739 for (XGL_UINT j = 0; j < icd->gpu_count; j++) {
740 if (icd->layer_count[j] > 0) {
741 for (XGL_UINT i = 0; i < icd->layer_count[j]; i++) {
742 libs = &(icd->layer_libs[j][i]);
743 if (libs->lib_handle)
744 dlclose(libs->lib_handle);
745 libs->lib_handle = NULL;
746 }
Jon Ashburnb4d00532014-10-22 21:15:26 -0600747 if (icd->wrappedGpus[j])
748 free(icd->wrappedGpus[j]);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600749 }
750 icd->layer_count[j] = 0;
751 }
752 icd->gpu_count = 0;
753 }
754}
755
Jon Ashburn10053332014-11-17 10:17:37 -0700756extern XGL_UINT loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn183dfd02014-10-22 18:13:16 -0600757{
758 XGL_UINT gpu_index;
759 XGL_UINT count;
760 XGL_CHAR ** ppLayerNames;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600761 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600762
763 if (!icd)
764 return 0;
765 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
766
767 /* activate any layer libraries */
768 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600769 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
770 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600771 GetProcAddrType nextGPA = xglGetProcAddr;
772
773 count = loader_get_layer_libs(pCreateInfo, &ppLayerNames);
774 if (!count)
775 return 0;
776 loader_init_layer_libs(icd, gpu_index, ppLayerNames, count);
777
Jon Ashburnb4d00532014-10-22 21:15:26 -0600778 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
779 if (! icd->wrappedGpus[gpu_index])
780 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Jon Ashburn183dfd02014-10-22 18:13:16 -0600781 for (XGL_INT i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnb4d00532014-10-22 21:15:26 -0600782 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn183dfd02014-10-22 18:13:16 -0600783 nextGpuObj->pGPA = nextGPA;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600784 nextGpuObj->baseObject = baseObj;
Jon Ashburn183dfd02014-10-22 18:13:16 -0600785 nextGpuObj->nextObject = gpuObj;
786 gpuObj = nextGpuObj;
787
788 nextGPA = dlsym(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
789 if (!nextGPA) {
790 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to find xglGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].lib_name);
791 continue;
792 }
793
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600794 if (i == 0) {
Jon Ashburn183dfd02014-10-22 18:13:16 -0600795 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600796 //Insert the new wrapped objects into the list with loader object at head
797 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
798 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
799 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
800 gpuObj->nextObject = baseObj;
801 gpuObj->pGPA = icd->GetProcAddr;
802 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600803
804 }
Jon Ashburn183dfd02014-10-22 18:13:16 -0600805 }
806 else {
807 //make sure requested Layers matches currently activated Layers
808 count = loader_get_layer_libs(pCreateInfo, &ppLayerNames);
809 for (XGL_UINT i = 0; i < count; i++) {
810 if (strcmp(icd->layer_libs[gpu_index][i].lib_name, (char *) *(ppLayerNames + i))) {
811 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
812 break;
813 }
814 }
815 if (count != icd->layer_count[gpu_index]) {
816 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated!= number requested");
817 }
818 }
819 return icd->layer_count[gpu_index];
820}
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600821
Jon Ashburn21734942014-10-17 15:31:22 -0600822LOADER_EXPORT XGL_VOID * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR * pName) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600823
824 if (gpu == NULL)
825 return NULL;
Jon Ashburn8eecd422014-10-22 12:42:13 -0600826 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600827 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600828
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600829 if (disp_table == NULL)
830 return NULL;
831
832 if (!strncmp("xglGetProcAddr", (const char *) pName, sizeof("xglGetProcAddr")))
Jon Ashburn3e7c0802014-10-24 15:48:55 -0600833 return disp_table->GetProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600834 else if (!strncmp("xglInitAndEnumerateGpus", (const char *) pName, sizeof("xglInitAndEnumerateGpus")))
835 return disp_table->InitAndEnumerateGpus;
836 else if (!strncmp("xglGetGpuInfo", (const char *) pName, sizeof ("xglGetGpuInfo")))
837 return disp_table->GetGpuInfo;
838 else if (!strncmp("xglCreateDevice", (const char *) pName, sizeof ("xglCreateDevice")))
839 return disp_table->CreateDevice;
840 else if (!strncmp("xglDestroyDevice", (const char *) pName, sizeof ("xglDestroyDevice")))
841 return disp_table->DestroyDevice;
842 else if (!strncmp("xglGetExtensionSupport", (const char *) pName, sizeof ("xglGetExtensionSupport")))
843 return disp_table->GetExtensionSupport;
Jon Ashburn96f28fc2014-10-15 15:30:23 -0600844 else if (!strncmp("xglEnumerateLayers", (const char *) pName, sizeof ("xglEnumerateLayers")))
845 return disp_table->EnumerateLayers;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600846 else if (!strncmp("xglGetDeviceQueue", (const char *) pName, sizeof ("xglGetDeviceQueue")))
847 return disp_table->GetDeviceQueue;
848 else if (!strncmp("xglQueueSubmit", (const char *) pName, sizeof ("xglQueueSubmit")))
849 return disp_table->QueueSubmit;
850 else if (!strncmp("xglQueueSetGlobalMemReferences", (const char *) pName, sizeof ("xglQueueSetGlobalMemReferences")))
851 return disp_table->QueueSetGlobalMemReferences;
852 else if (!strncmp("xglQueueWaitIdle", (const char *) pName, sizeof ("xglQueueWaitIdle")))
853 return disp_table->QueueWaitIdle;
854 else if (!strncmp("xglDeviceWaitIdle", (const char *) pName, sizeof ("xglDeviceWaitIdle")))
855 return disp_table->DeviceWaitIdle;
856 else if (!strncmp("xglGetMemoryHeapCount", (const char *) pName, sizeof ("xglGetMemoryHeapCount")))
857 return disp_table->GetMemoryHeapCount;
858 else if (!strncmp("xglGetMemoryHeapInfo", (const char *) pName, sizeof ("xglGetMemoryHeapInfo")))
859 return disp_table->GetMemoryHeapInfo;
860 else if (!strncmp("xglAllocMemory", (const char *) pName, sizeof ("xglAllocMemory")))
861 return disp_table->AllocMemory;
862 else if (!strncmp("xglFreeMemory", (const char *) pName, sizeof ("xglFreeMemory")))
863 return disp_table->FreeMemory;
864 else if (!strncmp("xglSetMemoryPriority", (const char *) pName, sizeof ("xglSetMemoryPriority")))
865 return disp_table->SetMemoryPriority;
866 else if (!strncmp("xglMapMemory", (const char *) pName, sizeof ("xglMapMemory")))
867 return disp_table->MapMemory;
868 else if (!strncmp("xglUnmapMemory", (const char *) pName, sizeof ("xglUnmapMemory")))
869 return disp_table->UnmapMemory;
870 else if (!strncmp("xglPinSystemMemory", (const char *) pName, sizeof ("xglPinSystemMemory")))
871 return disp_table->PinSystemMemory;
872 else if (!strncmp("xglRemapVirtualMemoryPages", (const char *) pName, sizeof ("xglRemapVirtualMemoryPages")))
873 return disp_table->RemapVirtualMemoryPages;
874 else if (!strncmp("xglGetMultiGpuCompatibility", (const char *) pName, sizeof ("xglGetMultiGpuCompatibility")))
875 return disp_table->GetMultiGpuCompatibility;
876 else if (!strncmp("xglOpenSharedMemory", (const char *) pName, sizeof ("xglOpenSharedMemory")))
877 return disp_table->OpenSharedMemory;
878 else if (!strncmp("xglOpenSharedQueueSemaphore", (const char *) pName, sizeof ("xglOpenSharedQueueSemaphore")))
879 return disp_table->OpenSharedQueueSemaphore;
880 else if (!strncmp("xglOpenPeerMemory", (const char *) pName, sizeof ("xglOpenPeerMemory")))
881 return disp_table->OpenPeerMemory;
882 else if (!strncmp("xglOpenPeerImage", (const char *) pName, sizeof ("xglOpenPeerImage")))
883 return disp_table->OpenPeerImage;
884 else if (!strncmp("xglDestroyObject", (const char *) pName, sizeof ("xglDestroyObject")))
885 return disp_table->DestroyObject;
886 else if (!strncmp("xglGetObjectInfo", (const char *) pName, sizeof ("xglGetObjectInfo")))
887 return disp_table->GetObjectInfo;
888 else if (!strncmp("xglBindObjectMemory", (const char *) pName, sizeof ("xglBindObjectMemory")))
889 return disp_table->BindObjectMemory;
890 else if (!strncmp("xglCreateFence", (const char *) pName, sizeof ("xgllCreateFence")))
891 return disp_table->CreateFence;
892 else if (!strncmp("xglGetFenceStatus", (const char *) pName, sizeof ("xglGetFenceStatus")))
893 return disp_table->GetFenceStatus;
894 else if (!strncmp("xglWaitForFences", (const char *) pName, sizeof ("xglWaitForFences")))
895 return disp_table->WaitForFences;
896 else if (!strncmp("xglCreateQueueSemaphore", (const char *) pName, sizeof ("xgllCreateQueueSemaphore")))
897 return disp_table->CreateQueueSemaphore;
898 else if (!strncmp("xglSignalQueueSemaphore", (const char *) pName, sizeof ("xglSignalQueueSemaphore")))
899 return disp_table->SignalQueueSemaphore;
900 else if (!strncmp("xglWaitQueueSemaphore", (const char *) pName, sizeof ("xglWaitQueueSemaphore")))
901 return disp_table->WaitQueueSemaphore;
902 else if (!strncmp("xglCreateEvent", (const char *) pName, sizeof ("xgllCreateEvent")))
903 return disp_table->CreateEvent;
904 else if (!strncmp("xglGetEventStatus", (const char *) pName, sizeof ("xglGetEventStatus")))
905 return disp_table->GetEventStatus;
906 else if (!strncmp("xglSetEvent", (const char *) pName, sizeof ("xglSetEvent")))
907 return disp_table->SetEvent;
908 else if (!strncmp("xglResetEvent", (const char *) pName, sizeof ("xgllResetEvent")))
909 return disp_table->ResetEvent;
910 else if (!strncmp("xglCreateQueryPool", (const char *) pName, sizeof ("xglCreateQueryPool")))
911 return disp_table->CreateQueryPool;
912 else if (!strncmp("xglGetQueryPoolResults", (const char *) pName, sizeof ("xglGetQueryPoolResults")))
913 return disp_table->GetQueryPoolResults;
914 else if (!strncmp("xglGetFormatInfo", (const char *) pName, sizeof ("xglGetFormatInfo")))
915 return disp_table->GetFormatInfo;
916 else if (!strncmp("xglCreateImage", (const char *) pName, sizeof ("xglCreateImage")))
917 return disp_table->CreateImage;
918 else if (!strncmp("xglGetImageSubresourceInfo", (const char *) pName, sizeof ("xglGetImageSubresourceInfo")))
919 return disp_table->GetImageSubresourceInfo;
920 else if (!strncmp("xglCreateImageView", (const char *) pName, sizeof ("xglCreateImageView")))
921 return disp_table->CreateImageView;
922 else if (!strncmp("xglCreateColorAttachmentView", (const char *) pName, sizeof ("xglCreateColorAttachmentView")))
923 return disp_table->CreateColorAttachmentView;
924 else if (!strncmp("xglCreateDepthStencilView", (const char *) pName, sizeof ("xglCreateDepthStencilView")))
925 return disp_table->CreateDepthStencilView;
926 else if (!strncmp("xglCreateShader", (const char *) pName, sizeof ("xglCreateShader")))
927 return disp_table->CreateShader;
928 else if (!strncmp("xglCreateGraphicsPipeline", (const char *) pName, sizeof ("xglCreateGraphicsPipeline")))
929 return disp_table->CreateGraphicsPipeline;
930 else if (!strncmp("xglCreateComputePipeline", (const char *) pName, sizeof ("xglCreateComputePipeline")))
931 return disp_table->CreateComputePipeline;
932 else if (!strncmp("xglStorePipeline", (const char *) pName, sizeof ("xglStorePipeline")))
933 return disp_table->StorePipeline;
934 else if (!strncmp("xglLoadPipeline", (const char *) pName, sizeof ("xglLoadPipeline")))
935 return disp_table->LoadPipeline;
936 else if (!strncmp("xglCreatePipelineDelta", (const char *) pName, sizeof ("xglCreatePipelineDelta")))
937 return disp_table->CreatePipelineDelta;
938 else if (!strncmp("xglCreateSampler", (const char *) pName, sizeof ("xglCreateSampler")))
939 return disp_table->CreateSampler;
940 else if (!strncmp("xglCreateDescriptorSet", (const char *) pName, sizeof ("xglCreateDescriptorSet")))
941 return disp_table->CreateDescriptorSet;
942 else if (!strncmp("xglBeginDescriptorSetUpdate", (const char *) pName, sizeof ("xglBeginDescriptorSetUpdate")))
943 return disp_table->BeginDescriptorSetUpdate;
944 else if (!strncmp("xglEndDescriptorSetUpdate", (const char *) pName, sizeof ("xglEndDescriptorSetUpdate")))
945 return disp_table->EndDescriptorSetUpdate;
946 else if (!strncmp("xglAttachSamplerDescriptors", (const char *) pName, sizeof ("xglAttachSamplerDescriptors")))
947 return disp_table->AttachSamplerDescriptors;
948 else if (!strncmp("xglAttachImageViewDescriptors", (const char *) pName, sizeof ("xglAttachImageViewDescriptors")))
949 return disp_table->AttachImageViewDescriptors;
950 else if (!strncmp("xglAttachMemoryViewDescriptors", (const char *) pName, sizeof ("xglAttachMemoryViewDescriptors")))
951 return disp_table->AttachMemoryViewDescriptors;
952 else if (!strncmp("xglAttachNestedDescriptors", (const char *) pName, sizeof ("xglAttachNestedDescriptors")))
953 return disp_table->AttachNestedDescriptors;
954 else if (!strncmp("xglClearDescriptorSetSlots", (const char *) pName, sizeof ("xglClearDescriptorSetSlots")))
955 return disp_table->ClearDescriptorSetSlots;
956 else if (!strncmp("xglCreateViewportState", (const char *) pName, sizeof ("xglCreateViewportState")))
957 return disp_table->CreateViewportState;
958 else if (!strncmp("xglCreateRasterState", (const char *) pName, sizeof ("xglCreateRasterState")))
959 return disp_table->CreateRasterState;
960 else if (!strncmp("xglCreateMsaaState", (const char *) pName, sizeof ("xglCreateMsaaState")))
961 return disp_table->CreateMsaaState;
962 else if (!strncmp("xglCreateColorBlendState", (const char *) pName, sizeof ("xglCreateColorBlendState")))
963 return disp_table->CreateColorBlendState;
964 else if (!strncmp("xglCreateDepthStencilState", (const char *) pName, sizeof ("xglCreateDepthStencilState")))
965 return disp_table->CreateDepthStencilState;
966 else if (!strncmp("xglCreateCommandBuffer", (const char *) pName, sizeof ("xglCreateCommandBuffer")))
967 return disp_table->CreateCommandBuffer;
968 else if (!strncmp("xglBeginCommandBuffer", (const char *) pName, sizeof ("xglBeginCommandBuffer")))
969 return disp_table->BeginCommandBuffer;
970 else if (!strncmp("xglEndCommandBuffer", (const char *) pName, sizeof ("xglEndCommandBuffer")))
971 return disp_table->EndCommandBuffer;
972 else if (!strncmp("xglResetCommandBuffer", (const char *) pName, sizeof ("xglResetCommandBuffer")))
973 return disp_table->ResetCommandBuffer;
974 else if (!strncmp("xglCmdBindPipeline", (const char *) pName, sizeof ("xglCmdBindPipeline")))
975 return disp_table->CmdBindPipeline;
976 else if (!strncmp("xglCmdBindPipelineDelta", (const char *) pName, sizeof ("xglCmdBindPipelineDelta")))
977 return disp_table->CmdBindPipelineDelta;
978 else if (!strncmp("xglCmdBindStateObject", (const char *) pName, sizeof ("xglCmdBindStateObject")))
979 return disp_table->CmdBindStateObject;
980 else if (!strncmp("xglCmdBindDescriptorSet", (const char *) pName, sizeof ("xglCmdBindDescriptorSet")))
981 return disp_table->CmdBindDescriptorSet;
982 else if (!strncmp("xglCmdBindDynamicMemoryView", (const char *) pName, sizeof ("xglCmdBindDynamicMemoryView")))
983 return disp_table->CmdBindDynamicMemoryView;
Chia-I Wu3b04af52014-11-08 10:48:20 +0800984 else if (!strncmp("xglCmdBindVertexData", (const char *) pName, sizeof ("xglCmdBindVertexData")))
985 return disp_table->CmdBindVertexData;
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600986 else if (!strncmp("xglCmdBindIndexData", (const char *) pName, sizeof ("xglCmdBindIndexData")))
987 return disp_table->CmdBindIndexData;
988 else if (!strncmp("xglCmdBindAttachments", (const char *) pName, sizeof ("xglCmdBindAttachments")))
989 return disp_table->CmdBindAttachments;
990 else if (!strncmp("xglCmdPrepareMemoryRegions", (const char *) pName, sizeof ("xglCmdPrepareMemoryRegions")))
991 return disp_table->CmdPrepareMemoryRegions;
992 else if (!strncmp("xglCmdPrepareImages", (const char *) pName, sizeof ("xglCmdPrepareImages")))
993 return disp_table->CmdPrepareImages;
994 else if (!strncmp("xglCmdDraw", (const char *) pName, sizeof ("xglCmdDraw")))
995 return disp_table->CmdDraw;
996 else if (!strncmp("xglCmdDrawIndexed", (const char *) pName, sizeof ("xglCmdDrawIndexed")))
997 return disp_table->CmdDrawIndexed;
998 else if (!strncmp("xglCmdDrawIndirect", (const char *) pName, sizeof ("xglCmdDrawIndirect")))
999 return disp_table->CmdDrawIndirect;
1000 else if (!strncmp("xglCmdDrawIndexedIndirect", (const char *) pName, sizeof ("xglCmdDrawIndexedIndirect")))
1001 return disp_table->CmdDrawIndexedIndirect;
1002 else if (!strncmp("xglCmdDispatch", (const char *) pName, sizeof ("xglCmdDispatch")))
1003 return disp_table->CmdDispatch;
1004 else if (!strncmp("xglCmdDispatchIndirect", (const char *) pName, sizeof ("xglCmdDispatchIndirect")))
1005 return disp_table->CmdDispatchIndirect;
1006 else if (!strncmp("xglCmdCopyMemory", (const char *) pName, sizeof ("xglCmdCopyMemory")))
1007 return disp_table->CmdCopyMemory;
1008 else if (!strncmp("xglCmdCopyImage", (const char *) pName, sizeof ("xglCmdCopyImage")))
1009 return disp_table->CmdCopyImage;
1010 else if (!strncmp("xglCmdCopyMemoryToImage", (const char *) pName, sizeof ("xglCmdCopyMemoryToImage")))
1011 return disp_table->CmdCopyMemoryToImage;
1012 else if (!strncmp("xglCmdCopyImageToMemory", (const char *) pName, sizeof ("xglCmdCopyImageToMemory")))
1013 return disp_table->CmdCopyImageToMemory;
1014 else if (!strncmp("xglCmdCloneImageData", (const char *) pName, sizeof ("xglCmdCloneImageData")))
1015 return disp_table->CmdCloneImageData;
1016 else if (!strncmp("xglCmdUpdateMemory", (const char *) pName, sizeof ("xglCmdUpdateMemory")))
1017 return disp_table->CmdUpdateMemory;
1018 else if (!strncmp("xglCmdFillMemory", (const char *) pName, sizeof ("xglCmdFillMemory")))
1019 return disp_table->CmdFillMemory;
1020 else if (!strncmp("xglCmdClearColorImage", (const char *) pName, sizeof ("xglCmdClearColorImage")))
1021 return disp_table->CmdClearColorImage;
1022 else if (!strncmp("xglCmdClearColorImageRaw", (const char *) pName, sizeof ("xglCmdClearColorImageRaw")))
1023 return disp_table->CmdClearColorImageRaw;
1024 else if (!strncmp("xglCmdClearDepthStencil", (const char *) pName, sizeof ("xglCmdClearDepthStencil")))
1025 return disp_table->CmdClearDepthStencil;
1026 else if (!strncmp("xglCmdResolveImage", (const char *) pName, sizeof ("xglCmdResolveImage")))
1027 return disp_table->CmdResolveImage;
1028 else if (!strncmp("xglCmdSetEvent", (const char *) pName, sizeof ("xglCmdSetEvent")))
1029 return disp_table->CmdSetEvent;
1030 else if (!strncmp("xglCmdResetEvent", (const char *) pName, sizeof ("xglCmdResetEvent")))
1031 return disp_table->CmdResetEvent;
1032 else if (!strncmp("xglCmdMemoryAtomic", (const char *) pName, sizeof ("xglCmdMemoryAtomic")))
1033 return disp_table->CmdMemoryAtomic;
1034 else if (!strncmp("xglCmdBeginQuery", (const char *) pName, sizeof ("xglCmdBeginQuery")))
1035 return disp_table->CmdBeginQuery;
1036 else if (!strncmp("xglCmdEndQuery", (const char *) pName, sizeof ("xglCmdEndQuery")))
1037 return disp_table->CmdEndQuery;
1038 else if (!strncmp("xglCmdResetQueryPool", (const char *) pName, sizeof ("xglCmdResetQueryPool")))
1039 return disp_table->CmdResetQueryPool;
1040 else if (!strncmp("xglCmdWriteTimestamp", (const char *) pName, sizeof ("xglCmdWriteTimestamp")))
1041 return disp_table->CmdWriteTimestamp;
1042 else if (!strncmp("xglCmdInitAtomicCounters", (const char *) pName, sizeof ("xglCmdInitAtomicCounters")))
1043 return disp_table->CmdInitAtomicCounters;
1044 else if (!strncmp("xglCmdLoadAtomicCounters", (const char *) pName, sizeof ("xglCmdLoadAtomicCounters")))
1045 return disp_table->CmdLoadAtomicCounters;
1046 else if (!strncmp("xglCmdSaveAtomicCounters", (const char *) pName, sizeof ("xglCmdSaveAtomicCounters")))
1047 return disp_table->CmdSaveAtomicCounters;
1048 else if (!strncmp("xglDbgSetValidationLevel", (const char *) pName, sizeof ("xglDbgSetValidationLevel")))
1049 return disp_table->DbgSetValidationLevel;
1050 else if (!strncmp("xglDbgRegisterMsgCallback", (const char *) pName, sizeof ("xglDbgRegisterMsgCallback")))
1051 return disp_table->DbgRegisterMsgCallback;
1052 else if (!strncmp("xglDbgUnregisterMsgCallback", (const char *) pName, sizeof ("xglDbgUnregisterMsgCallback")))
1053 return disp_table->DbgUnregisterMsgCallback;
1054 else if (!strncmp("xglDbgSetMessageFilter", (const char *) pName, sizeof ("xglDbgSetMessageFilter")))
1055 return disp_table->DbgSetMessageFilter;
1056 else if (!strncmp("xglDbgSetObjectTag", (const char *) pName, sizeof ("xglDbgSetObjectTag")))
1057 return disp_table->DbgSetObjectTag;
1058 else if (!strncmp("xglDbgSetGlobalOption", (const char *) pName, sizeof ("xglDbgSetGlobalOption")))
1059 return disp_table->DbgSetGlobalOption;
1060 else if (!strncmp("xglDbgSetDeviceOption", (const char *) pName, sizeof ("xglDbgSetDeviceOption")))
1061 return disp_table->DbgSetDeviceOption;
1062 else if (!strncmp("xglCmdDbgMarkerBegin", (const char *) pName, sizeof ("xglCmdDbgMarkerBegin")))
1063 return disp_table->CmdDbgMarkerBegin;
1064 else if (!strncmp("xglCmdDbgMarkerEnd", (const char *) pName, sizeof ("xglCmdDbgMarkerEnd")))
1065 return disp_table->CmdDbgMarkerEnd;
1066 else if (!strncmp("xglWsiX11AssociateConnection", (const char *) pName, sizeof("xglWsiX11AssociateConnection")))
1067 return disp_table->WsiX11AssociateConnection;
1068 else if (!strncmp("xglWsiX11GetMSC", (const char *) pName, sizeof("xglWsiX11GetMSC")))
1069 return disp_table->WsiX11GetMSC;
1070 else if (!strncmp("xglWsiX11CreatePresentableImage", (const char *) pName, sizeof("xglWsiX11CreatePresentableImage")))
1071 return disp_table->WsiX11CreatePresentableImage;
1072 else if (!strncmp("xglWsiX11QueuePresent", (const char *) pName, sizeof("xglWsiX11QueuePresent")))
1073 return disp_table->WsiX11QueuePresent;
1074 else {
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001075 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001076 return NULL;
Jon Ashburn3e7c0802014-10-24 15:48:55 -06001077 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001078 }
1079}
1080
Chia-I Wu468e3c32014-08-04 08:03:57 +08001081LOADER_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 +08001082{
1083 static pthread_once_t once = PTHREAD_ONCE_INIT;
Jon Ashburn815bddd2014-10-16 15:48:50 -06001084 struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001085 XGL_UINT count = 0;
1086 XGL_RESULT res;
1087
Jon Ashburn815bddd2014-10-16 15:48:50 -06001088 // cleanup any prior layer initializations
Jon Ashburnb55278a2014-10-17 15:09:07 -06001089 loader_deactivate_layer();
Jon Ashburn815bddd2014-10-16 15:48:50 -06001090
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001091 pthread_once(&once, loader_icd_scan);
1092
1093 if (!loader.icds)
1094 return XGL_ERROR_UNAVAILABLE;
1095
1096 icd = loader.icds;
1097 while (icd) {
1098 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001099 XGL_BASE_LAYER_OBJECT * wrappedGpus;
1100 GetProcAddrType getProcAddr = icd->GetProcAddr;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001101 XGL_UINT n, max = maxGpus - count;
1102
1103 if (max > XGL_MAX_PHYSICAL_GPUS) {
1104 max = XGL_MAX_PHYSICAL_GPUS;
1105 }
1106
1107 res = icd->InitAndEnumerateGpus(pAppInfo, pAllocCb, max, &n, gpus);
Chia-I Wu74916ed2014-08-06 12:17:04 +08001108 if (res == XGL_SUCCESS && n) {
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001109 wrappedGpus = (XGL_BASE_LAYER_OBJECT*) malloc(n * sizeof(XGL_BASE_LAYER_OBJECT));
Jon Ashburn183dfd02014-10-22 18:13:16 -06001110 icd->gpus = wrappedGpus;
Jon Ashburnb55278a2014-10-17 15:09:07 -06001111 icd->gpu_count = n;
Jon Ashburn815bddd2014-10-16 15:48:50 -06001112 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n * sizeof(XGL_LAYER_DISPATCH_TABLE));
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001113 for (int i = 0; i < n; i++) {
1114 (wrappedGpus + i)->baseObject = gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -06001115 (wrappedGpus + i)->pGPA = getProcAddr;
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001116 (wrappedGpus + i)->nextObject = gpus[i];
1117 memcpy(pGpus + count, &wrappedGpus, sizeof(*pGpus));
Jon Ashburn8eecd422014-10-22 12:42:13 -06001118 loader_init_dispatch_table(icd->loader_dispatch + i, getProcAddr, gpus[i]);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001119 const XGL_LAYER_DISPATCH_TABLE * *disp = (const XGL_LAYER_DISPATCH_TABLE * *) gpus[i];
Jon Ashburn815bddd2014-10-16 15:48:50 -06001120 *disp = icd->loader_dispatch + i;
1121 icd->SetDispatch(icd->loader_dispatch + i, true);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001122 }
1123
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001124 count += n;
1125
1126 if (count >= maxGpus) {
1127 break;
1128 }
1129 }
1130
1131 icd = icd->next;
1132 }
1133
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001134 /* get layer libraries */
Jon Ashburn183dfd02014-10-22 18:13:16 -06001135 if (!loader.layer_scanned)
1136 layer_lib_scan(NULL, true);
Jon Ashburnd43f9b62014-10-14 19:15:22 -06001137
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001138 *pGpuCount = count;
1139
1140 return (count > 0) ? XGL_SUCCESS : res;
1141}
1142
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001143LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, XGL_SIZE maxLayerCount, XGL_SIZE maxStringSize, XGL_CHAR* const* pOutLayers, XGL_SIZE* pOutLayerCount)
1144{
Jon Ashburn183dfd02014-10-22 18:13:16 -06001145 XGL_SIZE count = loader.scanned_layer_count;
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001146 // TODO handle layers per GPU, multiple icds
1147
1148 if (pOutLayerCount == NULL)
1149 return XGL_ERROR_INVALID_POINTER;
1150
Jon Ashburn183dfd02014-10-22 18:13:16 -06001151 if (maxLayerCount < loader.scanned_layer_count)
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001152 count = maxLayerCount;
1153 *pOutLayerCount = count;
1154
1155 if (pOutLayers == NULL)
1156 return XGL_SUCCESS;
1157 for (XGL_SIZE i = 0; i < count; i++) {
Jon Ashburn183dfd02014-10-22 18:13:16 -06001158 strncpy((char *) (pOutLayers[i]), loader.scanned_layer_names[i], maxStringSize);
Jon Ashburn96f28fc2014-10-15 15:30:23 -06001159 if (maxStringSize > 0)
1160 pOutLayers[i][maxStringSize - 1] = '\0';
1161 }
1162 return XGL_SUCCESS;
1163}
1164
Chia-I Wu468e3c32014-08-04 08:03:57 +08001165LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001166{
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001167 const struct loader_icd *icd;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001168 XGL_RESULT res;
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001169 XGL_UINT gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001170
1171 if (!loader.scanned) {
1172 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1173 }
1174
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001175 for (icd = loader.icds; icd; icd = icd->next) {
1176 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1177 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(pfnMsgCallback, pUserData);
1178 if (res != XGL_SUCCESS) {
1179 gpu_idx = i;
1180 break;
1181 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001182 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001183 if (res != XGL_SUCCESS)
1184 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001185 }
1186
1187 /* roll back on errors */
1188 if (icd) {
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001189 for (const struct loader_icd * tmp = loader.icds; tmp != icd; tmp = tmp->next) {
1190 for (XGL_UINT i = 0; i < icd->gpu_count; i++)
1191 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001192 }
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001193 /* and gpus on current icd */
1194 for (XGL_UINT i = 0; i < gpu_idx; i++)
1195 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001196
1197 return res;
1198 }
1199
1200 return XGL_SUCCESS;
1201}
1202
Chia-I Wu468e3c32014-08-04 08:03:57 +08001203LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001204{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001205 XGL_RESULT res = XGL_SUCCESS;
1206
1207 if (!loader.scanned) {
1208 return loader_msg_callback_remove(pfnMsgCallback);
1209 }
1210
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001211 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
1212 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1213 XGL_RESULT r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1214 if (r != XGL_SUCCESS) {
1215 res = r;
1216 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001217 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001218 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001219 return res;
1220}
1221
Chia-I Wu468e3c32014-08-04 08:03:57 +08001222LOADER_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 +08001223{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001224 XGL_RESULT res = XGL_SUCCESS;
1225
1226 if (!loader.scanned) {
1227 if (dataSize == 0)
1228 return XGL_ERROR_INVALID_VALUE;
1229
1230 switch (dbgOption) {
1231 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1232 loader.debug_echo_enable = *((const bool *) pData);
1233 break;
1234 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1235 loader.break_on_error = *((const bool *) pData);
1236 break;
1237 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1238 loader.break_on_warning = *((const bool *) pData);
1239 break;
1240 default:
1241 res = XGL_ERROR_INVALID_VALUE;
1242 break;
1243 }
1244
1245 return res;
1246 }
1247
Jon Ashburn406a0fe2014-11-14 09:52:42 -07001248 for (const struct loader_icd * icd = loader.icds; icd; icd = icd->next) {
1249 for (XGL_UINT i = 0; i < icd->gpu_count; i++) {
1250 XGL_RESULT r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption, dataSize, pData);
1251 /* unfortunately we cannot roll back */
1252 if (r != XGL_SUCCESS) {
1253 res = r;
1254 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001255 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001256 }
1257
1258 return res;
1259}