blob: bff896ff0a6b30df7eac7b92cb1822e7723fdabb [file] [log] [blame]
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu701f3f62014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Jon Ashburn01e2d662014-11-14 09:52:42 -070026 * Jon Ashburn <jon@lunarg.com>
Chia-I Wu701f3f62014-09-02 08:32:09 +080027 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Ian Elliott5aa4ea22015-03-31 15:32:41 -060028 * Ian Elliott <ian@lunarg.com>
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080029 */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060030#define _GNU_SOURCE
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080031#include <stdio.h>
32#include <stdlib.h>
33#include <stdarg.h>
34#include <stdbool.h>
35#include <string.h>
36
Chia-I Wu13a61a52014-08-04 11:18:20 +080037#include <sys/types.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070038#if defined(WIN32)
39#include "dirent_on_windows.h"
40#else // WIN32
Chia-I Wu13a61a52014-08-04 11:18:20 +080041#include <dirent.h>
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070042#endif // WIN32
43#include "loader_platform.h"
Chia-I Wuf46b81a2015-01-04 11:12:47 +080044#include "table_ops.h"
Chia-I Wu19300602014-08-04 08:03:57 +080045#include "loader.h"
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -070046#include "xglIcd.h"
Ian Elliott655cad72015-02-12 17:08:34 -070047// The following is #included again to catch certain OS-specific functions
48// being used:
49#include "loader_platform.h"
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080050
Jon Ashburn1beab2d2015-01-26 14:51:40 -070051struct loader_instance {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070052 struct loader_icd *icds;
53 struct loader_instance *next;
54};
55
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060056struct loader_layers {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070057 loader_platform_dl_handle lib_handle;
Jon Ashburnb8358052014-11-18 09:06:04 -070058 char name[256];
59};
60
61struct layer_name_pair {
62 char *layer_name;
63 const char *lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060064};
65
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080066struct loader_icd {
Jon Ashburn46d1f582015-01-28 11:01:35 -070067 const struct loader_scanned_icds *scanned_icds;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080068
Jon Ashburn876b1ac2014-10-17 15:09:07 -060069 XGL_LAYER_DISPATCH_TABLE *loader_dispatch;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060070 uint32_t layer_count[XGL_MAX_PHYSICAL_GPUS];
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060071 struct loader_layers layer_libs[XGL_MAX_PHYSICAL_GPUS][MAX_LAYER_LIBRARIES];
Jon Ashburnd09bd102014-10-22 21:15:26 -060072 XGL_BASE_LAYER_OBJECT *wrappedGpus[XGL_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060073 uint32_t gpu_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060074 XGL_BASE_LAYER_OBJECT *gpus;
Jon Ashburndf7d5842014-10-16 15:48:50 -060075
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080076 struct loader_icd *next;
77};
78
Jon Ashburnd38bfb12014-10-14 19:15:22 -060079
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080080struct loader_msg_callback {
81 XGL_DBG_MSG_CALLBACK_FUNCTION func;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060082 void *data;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080083
84 struct loader_msg_callback *next;
85};
86
Jon Ashburn46d1f582015-01-28 11:01:35 -070087struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070088 loader_platform_dl_handle handle;
Jon Ashburn46d1f582015-01-28 11:01:35 -070089 xglGetProcAddrType GetProcAddr;
Jon Ashburn46888392015-01-29 15:45:51 -070090 xglCreateInstanceType CreateInstance;
91 xglDestroyInstanceType DestroyInstance;
Jon Ashburn4c392fb2015-01-28 19:57:09 -070092 xglEnumerateGpusType EnumerateGpus;
Jon Ashburn46888392015-01-29 15:45:51 -070093 XGL_INSTANCE instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070094 struct loader_scanned_icds *next;
95};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060096
Jon Ashburn1beab2d2015-01-26 14:51:40 -070097// Note: Since the following is a static structure, all members are initialized
98// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080099static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700100 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700101 bool icds_scanned;
102 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600103 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700104 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600105 unsigned int scanned_layer_count;
106 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800107 struct loader_msg_callback *msg_callbacks;
108
109 bool debug_echo_enable;
110 bool break_on_error;
111 bool break_on_warning;
112} loader;
113
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700114
Ian Elliott4470a302015-02-17 10:33:47 -0700115#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600116char *loader_get_registry_string(const HKEY hive,
117 const LPCTSTR sub_key,
118 const char *value)
119{
120 DWORD access_flags = KEY_QUERY_VALUE;
121 DWORD value_type;
122 HKEY key;
123 LONG rtn_value;
124 char *rtn_str = NULL;
125 size_t rtn_len = 0;
126 size_t allocated_len = 0;
127
128 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
129 if (rtn_value != ERROR_SUCCESS) {
130 // We didn't find the key. Try the 32-bit hive (where we've seen the
131 // key end up on some people's systems):
132 access_flags |= KEY_WOW64_32KEY;
133 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
134 if (rtn_value != ERROR_SUCCESS) {
135 // We still couldn't find the key, so give up:
136 return NULL;
137 }
138 }
139
140 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
141 (PVOID) rtn_str, &rtn_len);
142 if (rtn_value == ERROR_SUCCESS) {
143 // If we get to here, we found the key, and need to allocate memory
144 // large enough for rtn_str, and query again:
145 allocated_len = rtn_len + 4;
146 rtn_str = malloc(allocated_len);
147 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
148 (PVOID) rtn_str, &rtn_len);
149 if (rtn_value == ERROR_SUCCESS) {
150 // We added 4 extra bytes to rtn_str, so that we can ensure that
151 // the string is NULL-terminated (albeit, in a brute-force manner):
152 rtn_str[allocated_len-1] = '\0';
153 } else {
154 // This should never occur, but in case it does, clean up:
155 free(rtn_str);
156 rtn_str = NULL;
157 }
158 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
159
160 // Close the registry key that was opened:
161 RegCloseKey(key);
162
163 return rtn_str;
164}
165
166
Ian Elliott4470a302015-02-17 10:33:47 -0700167// For ICD developers, look in the registry, and look for an environment
168// variable for a path(s) where to find the ICD(s):
169static char *loader_get_registry_and_env(const char *env_var,
170 const char *registry_value)
171{
172 char *env_str = getenv(env_var);
173 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600174 char *registry_str = NULL;
175 DWORD registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700176 char *rtn_str = NULL;
177 size_t rtn_len;
178
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600179 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
180 "Software\\XGL",
181 registry_value);
Ian Elliott2de26bc2015-04-03 13:13:01 -0600182 registry_len = (registry_str) ? strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700183
184 rtn_len = env_len + registry_len + 1;
185 if (rtn_len <= 2) {
186 // We found neither the desired registry value, nor the environment
187 // variable; return NULL:
188 return NULL;
189 } else {
190 // We found something, and so we need to allocate memory for the string
191 // to return:
192 rtn_str = malloc(rtn_len);
193 }
194
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600195 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700196 // We didn't find the desired registry value, and so we must have found
197 // only the environment variable:
198 _snprintf(rtn_str, rtn_len, "%s", env_str);
199 } else if (env_str != NULL) {
200 // We found both the desired registry value and the environment
201 // variable, so concatenate them both:
202 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
203 } else {
204 // We must have only found the desired registry value:
205 _snprintf(rtn_str, rtn_len, "%s", registry_str);
206 }
207
Ian Elliott2de26bc2015-04-03 13:13:01 -0600208 if (registry_str) {
209 free(registry_str);
210 }
Ian Elliott4470a302015-02-17 10:33:47 -0700211
212 return(rtn_str);
213}
214#endif // WIN32
215
216
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800217static XGL_RESULT loader_msg_callback_add(XGL_DBG_MSG_CALLBACK_FUNCTION func,
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600218 void *data)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800219{
220 struct loader_msg_callback *cb;
221
222 cb = malloc(sizeof(*cb));
223 if (!cb)
224 return XGL_ERROR_OUT_OF_MEMORY;
225
226 cb->func = func;
227 cb->data = data;
228
229 cb->next = loader.msg_callbacks;
230 loader.msg_callbacks = cb;
231
232 return XGL_SUCCESS;
233}
234
235static XGL_RESULT loader_msg_callback_remove(XGL_DBG_MSG_CALLBACK_FUNCTION func)
236{
237 struct loader_msg_callback *cb = loader.msg_callbacks;
238
239 /*
240 * Find the first match (last registered).
241 *
242 * XXX What if the same callback function is registered more than once?
243 */
244 while (cb) {
245 if (cb->func == func) {
246 break;
247 }
248
249 cb = cb->next;
250 }
251
252 if (!cb)
253 return XGL_ERROR_INVALID_POINTER;
254
255 free(cb);
256
257 return XGL_SUCCESS;
258}
259
260static void loader_msg_callback_clear(void)
261{
262 struct loader_msg_callback *cb = loader.msg_callbacks;
263
264 while (cb) {
265 struct loader_msg_callback *next = cb->next;
266 free(cb);
267 cb = next;
268 }
269
270 loader.msg_callbacks = NULL;
271}
272
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600273static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800274 const char *format, ...)
275{
276 const struct loader_msg_callback *cb = loader.msg_callbacks;
277 char msg[256];
278 va_list ap;
279 int ret;
280
281 va_start(ap, format);
282 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700283 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800284 msg[sizeof(msg) - 1] = '\0';
285 }
286 va_end(ap);
287
288 if (loader.debug_echo_enable || !cb) {
289 fputs(msg, stderr);
290 fputc('\n', stderr);
291 }
292
293 while (cb) {
294 cb->func(msg_type, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0,
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800295 msg_code, msg, cb->data);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800296 cb = cb->next;
297 }
298
299 switch (msg_type) {
300 case XGL_DBG_MSG_ERROR:
301 if (loader.break_on_error) {
302 exit(1);
303 }
304 /* fall through */
305 case XGL_DBG_MSG_WARNING:
306 if (loader.break_on_warning) {
307 exit(1);
308 }
309 break;
310 default:
311 break;
312 }
313}
314
315static void
316loader_icd_destroy(struct loader_icd *icd)
317{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700318 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800319 free(icd);
320}
321
322static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700323loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800324{
325 struct loader_icd *icd;
326
327 icd = malloc(sizeof(*icd));
328 if (!icd)
329 return NULL;
330
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600331 memset(icd, 0, sizeof(*icd));
332
Jon Ashburn46d1f582015-01-28 11:01:35 -0700333 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800334
335 return icd;
336}
337
338static XGL_RESULT loader_icd_register_msg_callbacks(const struct loader_icd *icd)
339{
340 const struct loader_msg_callback *cb = loader.msg_callbacks;
341 XGL_RESULT res;
342
343 while (cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600344 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700345 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(cb->func, cb->data);
346 if (res != XGL_SUCCESS) {
347 break;
348 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800349 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800350 cb = cb->next;
351 }
352
353 /* roll back on errors */
354 if (cb) {
355 const struct loader_msg_callback *tmp = loader.msg_callbacks;
356
357 while (tmp != cb) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600358 for (uint32_t i = 0; i < icd->gpu_count; i++) {
Jon Ashburn01e2d662014-11-14 09:52:42 -0700359 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(cb->func);
360 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800361 tmp = tmp->next;
362 }
363
364 return res;
365 }
366
367 return XGL_SUCCESS;
368}
369
370static XGL_RESULT loader_icd_set_global_options(const struct loader_icd *icd)
371{
372#define SETB(icd, opt, val) do { \
373 if (val) { \
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600374 for (uint32_t i = 0; i < icd->gpu_count; i++) { \
Jon Ashburn01e2d662014-11-14 09:52:42 -0700375 const XGL_RESULT res = \
376 (icd->loader_dispatch + i)->DbgSetGlobalOption(opt, sizeof(val), &val); \
377 if (res != XGL_SUCCESS) \
378 return res; \
379 } \
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800380 } \
381} while (0)
382 SETB(icd, XGL_DBG_OPTION_DEBUG_ECHO_ENABLE, loader.debug_echo_enable);
383 SETB(icd, XGL_DBG_OPTION_BREAK_ON_ERROR, loader.break_on_error);
384 SETB(icd, XGL_DBG_OPTION_BREAK_ON_WARNING, loader.break_on_warning);
385#undef SETB
386
387return XGL_SUCCESS;
388}
389
Jon Ashburn46888392015-01-29 15:45:51 -0700390static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
391 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800392{
393 struct loader_icd *icd;
394
Jon Ashburn46d1f582015-01-28 11:01:35 -0700395 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800396 if (!icd)
397 return NULL;
398
Chia-I Wu13a61a52014-08-04 11:18:20 +0800399 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700400 icd->next = ptr_inst->icds;
401 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800402
403 return icd;
404}
405
Jon Ashburn46d1f582015-01-28 11:01:35 -0700406static void loader_scanned_icd_add(const char *filename)
407{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700408 loader_platform_dl_handle handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700409 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700410 struct loader_scanned_icds *new_node;
411
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700412 // Used to call: dlopen(filename, RTLD_LAZY);
413 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700414 if (!handle) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700415 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700416 return;
417 }
418
419#define LOOKUP(func_ptr, func) do { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700420 func_ptr = (xgl ##func## Type) loader_platform_get_proc_address(handle, "xgl" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700421 if (!func_ptr) { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700422 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("xgl" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700423 return; \
424 } \
425} while (0)
426
427 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700428 LOOKUP(fp_create_inst, CreateInstance);
429 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700430 LOOKUP(fp_enumerate, EnumerateGpus);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700431#undef LOOKUP
432
433 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
434 if (!new_node) {
435 loader_log(XGL_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
436 return;
437 }
438
439 new_node->handle = handle;
440 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700441 new_node->CreateInstance = fp_create_inst;
442 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700443 new_node->EnumerateGpus = fp_enumerate;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700444 new_node->next = loader.scanned_icd_list;
445 loader.scanned_icd_list = new_node;
446}
447
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700448
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600449/**
450 * Try to \c loader_icd_scan XGL driver(s).
451 *
452 * This function scans the default system path or path
453 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
454 * order to find loadable XGL ICDs with the name of libXGL_*.
455 *
456 * \returns
457 * void; but side effect is to set loader_icd_scanned to true
458 */
459static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800460{
Ian Elliott4470a302015-02-17 10:33:47 -0700461 const char *p, *next;
462 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600463 DIR *sysdir;
464 struct dirent *dent;
465 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600466 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700467 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700468#if defined(WIN32)
469 bool must_free_libPaths;
470 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
471 DRIVER_PATH_REGISTRY_VALUE);
472 if (libPaths != NULL) {
473 must_free_libPaths = true;
474 } else {
475 must_free_libPaths = false;
476 libPaths = DEFAULT_XGL_DRIVERS_PATH;
477 }
478#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600479 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700480 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
481 libPaths = getenv(DRIVER_PATH_ENV);
482 }
483 if (libPaths == NULL) {
484 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600485 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700486#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800487
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600488 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700489 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600490 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700491 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600492 next = p + len;
493 }
494 else {
Ian Elliott19628802015-02-04 12:06:46 -0700495 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600496 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
497 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600498 next++;
499 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800500
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700501 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
502 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600503 sysdir = opendir(p);
504 if (sysdir) {
505 dent = readdir(sysdir);
506 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700507 /* Look for ICDs starting with XGL_DRIVER_LIBRARY_PREFIX and
508 * ending with XGL_LIBRARY_SUFFIX
509 */
510 if (!strncmp(dent->d_name,
511 XGL_DRIVER_LIBRARY_PREFIX,
512 XGL_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700513 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700514 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
515 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
516 !strncmp(suf,
517 XGL_LIBRARY_SUFFIX,
518 XGL_LIBRARY_SUFFIX_LEN)) {
519 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
520 loader_scanned_icd_add(icd_library);
521 }
522 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600523
524 dent = readdir(sysdir);
525 }
526 closedir(sysdir);
527 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800528 }
529
Ian Elliott4470a302015-02-17 10:33:47 -0700530#if defined(WIN32)
531 // Free any allocated memory:
532 if (must_free_libPaths) {
533 free(libPaths);
534 }
535#endif // WIN32
536
537 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700538 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800539}
540
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600541
Ian Elliott4470a302015-02-17 10:33:47 -0700542static void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600543{
544 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700545 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600546 DIR *curdir;
547 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700548 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600549 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600550
Ian Elliott4470a302015-02-17 10:33:47 -0700551#if defined(WIN32)
552 bool must_free_libPaths;
553 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
554 LAYERS_PATH_REGISTRY_VALUE);
555 if (libPaths != NULL) {
556 must_free_libPaths = true;
557 } else {
558 must_free_libPaths = false;
559 libPaths = DEFAULT_XGL_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600560 }
Ian Elliott4470a302015-02-17 10:33:47 -0700561#else // WIN32
562 if (geteuid() == getuid()) {
563 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700564 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700565 }
566 if (libPaths == NULL) {
567 libPaths = DEFAULT_XGL_LAYERS_PATH;
568 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700569#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600570
Ian Elliott4470a302015-02-17 10:33:47 -0700571 if (libPaths == NULL) {
572 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700573 return;
574 }
Ian Elliott4470a302015-02-17 10:33:47 -0700575 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700576 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700577 if (loader.layer_dirs == NULL) {
578 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700579 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700580 }
581 // Alloc passed, so we know there is enough space to hold the string, don't
582 // need strncpy
583 strcpy(loader.layer_dirs, libPaths);
584#if defined(WIN32)
585 // Free any allocated memory:
586 if (must_free_libPaths) {
587 free(libPaths);
588 must_free_libPaths = false;
589 }
590#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700591 libPaths = loader.layer_dirs;
592
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600593 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600594 for (i = 0; i < loader.scanned_layer_count; i++) {
595 if (loader.scanned_layer_names[i] != NULL)
596 free(loader.scanned_layer_names[i]);
597 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600598 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600599 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600600
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600601 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700602 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600603 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700604 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600605 next = p + len;
606 }
607 else {
Ian Elliott19628802015-02-04 12:06:46 -0700608 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600609 *(char *) next = '\0';
610 next++;
611 }
612
613 curdir = opendir(p);
614 if (curdir) {
615 dent = readdir(curdir);
616 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700617 /* Look for layers starting with XGL_LAYER_LIBRARY_PREFIX and
618 * ending with XGL_LIBRARY_SUFFIX
619 */
620 if (!strncmp(dent->d_name,
621 XGL_LAYER_LIBRARY_PREFIX,
622 XGL_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700623 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700624 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
625 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
626 !strncmp(suf,
627 XGL_LIBRARY_SUFFIX,
628 XGL_LIBRARY_SUFFIX_LEN)) {
629 loader_platform_dl_handle handle;
630 snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
631 // Used to call: dlopen(temp_str, RTLD_LAZY)
632 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
633 dent = readdir(curdir);
634 continue;
635 }
636 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
637 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
638 break;
639 }
640 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
641 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
642 break;
643 }
644 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
645 loader.scanned_layer_count++;
646 loader_platform_close_library(handle);
647 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600648 }
649
650 dent = readdir(curdir);
651 }
652 closedir(curdir);
653 }
654 }
655
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600656 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600657}
658
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600659static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600660{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800661 loader_initialize_dispatch_table(tab, fpGPA, gpu);
662
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600663 if (tab->EnumerateLayers == NULL)
664 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600665}
666
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600667static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600668{
Jon Ashburn98bd4542015-01-29 16:44:24 -0700669 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
670 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
671 for (uint32_t i = 0; i < icd->gpu_count; i++)
672 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
673 gpu->baseObject) {
674 *gpu_index = i;
675 return icd;
676 }
677 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600678 }
679 return NULL;
680}
681
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600682static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600683{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600684 if (icd->layer_count[gpu_index])
685 return true;
686 else
687 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600688}
689
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600690static void loader_init_layer_libs(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair * pLayerNames, uint32_t count)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600691{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600692 if (!icd)
693 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600694
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600695 struct loader_layers *obj;
696 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600697 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600698 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600699 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700700 if (icd->layer_libs[gpu_index][j].lib_handle && !strcmp(icd->layer_libs[gpu_index][j].name, (char *) pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600701 foundLib = true;
702 break;
703 }
704 }
705 if (!foundLib) {
706 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700707 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
708 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700709 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
710 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
711 loader_log(XGL_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600712 continue;
713 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700714 loader_log(XGL_DBG_MSG_UNKNOWN, 0, "Inserting layer %s from library %s", pLayerNames[i].layer_name, pLayerNames[i].lib_name);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600715 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700716 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600717 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600718 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600719 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600720}
721
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600722static bool find_layer_name(struct loader_icd *icd, uint32_t gpu_index, const char * layer_name, const char **lib_name)
Jon Ashburnb8358052014-11-18 09:06:04 -0700723{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700724 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600725 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600726 char layer_buf[16][256];
727 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700728
729 for (int i = 0; i < 16; i++)
730 layers[i] = &layer_buf[i][0];
731
732 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
733 *lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700734 // Used to call: dlopen(*lib_name, RTLD_LAZY)
735 if ((handle = loader_platform_open_library(*lib_name)) == NULL)
Jon Ashburnb8358052014-11-18 09:06:04 -0700736 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700737 if ((fpEnumerateLayers = (xglEnumerateLayersType) loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700738 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700739 //use default layer name
740 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name),
Courtney Goeltzenleuchter8d095ff2015-04-03 14:53:08 -0600741 XGL_LAYER_LIBRARY_PREFIX "%s" XGL_LIBRARY_SUFFIX,
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700742 layer_name);
743 loader_platform_close_library(handle);
744 if (!strcmp(*lib_name, lib_str)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700745 free(lib_str);
746 return true;
747 }
748 else {
749 free(lib_str);
750 continue;
751 }
752 }
753 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600754 size_t cnt;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700755 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (char *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700756 for (unsigned int i = 0; i < cnt; i++) {
757 if (!strcmp((char *) layers[i], layer_name)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700758 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700759 return true;
760 }
761 }
762 }
763
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700764 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700765 }
766
767 return false;
768}
769
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600770static uint32_t loader_get_layer_env(struct loader_icd *icd, uint32_t gpu_index, struct layer_name_pair *pLayerNames)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600771{
Ian Elliott4470a302015-02-17 10:33:47 -0700772 char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600773 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600774 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600775
Ian Elliott4470a302015-02-17 10:33:47 -0700776#if defined(WIN32)
777 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
778 LAYER_NAMES_REGISTRY_VALUE);
779#else // WIN32
780 layerEnv = getenv(LAYER_NAMES_ENV);
781#endif // WIN32
782 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600783 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700784 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600785 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700786 if (p == NULL) {
787#if defined(WIN32)
788 free(layerEnv);
789#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600790 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700791 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600792 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700793#if defined(WIN32)
794 free(layerEnv);
795#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600796 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600797
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600798 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700799 const char *lib_name = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700800 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600801 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700802 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600803 next = p + len;
804 }
805 else {
Ian Elliott19628802015-02-04 12:06:46 -0700806 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600807 *(char *) next = '\0';
808 next++;
809 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600810 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700811 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600812 p = next;
813 continue;
814 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600815
Ian Elliott19628802015-02-04 12:06:46 -0700816 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700817 pLayerNames[count].layer_name = malloc(len + 1);
818 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600819 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600820 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600821 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700822 strncpy((char *) pLayerNames[count].layer_name, name, len);
823 pLayerNames[count].layer_name[len] = '\0';
824 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600825 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600826 p = next;
827
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600828 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600829
Jon Ashburnd09bd102014-10-22 21:15:26 -0600830 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600831 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600832}
833
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600834static uint32_t loader_get_layer_libs(struct loader_icd *icd, uint32_t gpu_index, const XGL_DEVICE_CREATE_INFO* pCreateInfo, struct layer_name_pair **ppLayerNames)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600835{
Jon Ashburnb8358052014-11-18 09:06:04 -0700836 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700837 int env_layer_count = 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600838
839 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700840 /* Load any layers specified in the environment first */
841 env_layer_count = loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600842
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800843 const XGL_LAYER_CREATE_INFO *pCi =
844 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600845
846 while (pCi) {
847 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
848 const char *name;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700849 uint32_t len, j = 0;
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700850 for (uint32_t i = env_layer_count; i < (env_layer_count + pCi->layerCount); i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700851 const char * lib_name = NULL;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700852 name = *(pCi->ppActiveLayerNames + j);
853 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
854 return i;
855 }
Ian Elliott19628802015-02-04 12:06:46 -0700856 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700857 layerNames[i].layer_name = malloc(len + 1);
858 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600859 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700860 strncpy((char *) layerNames[i].layer_name, name, len);
861 layerNames[i].layer_name[len] = '\0';
862 layerNames[i].lib_name = lib_name;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700863 j++;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600864 }
Jon Ashburnc0e86012015-02-18 11:29:58 -0700865 return pCi->layerCount + env_layer_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600866 }
867 pCi = pCi->pNext;
868 }
Jon Ashburnc0e86012015-02-18 11:29:58 -0700869 return env_layer_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600870}
871
Jon Ashburn46d1f582015-01-28 11:01:35 -0700872static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600873{
874 struct loader_icd *icd;
875 struct loader_layers *libs;
876
Jon Ashburn46d1f582015-01-28 11:01:35 -0700877 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600878 if (icd->gpus)
879 free(icd->gpus);
880 icd->gpus = NULL;
881 if (icd->loader_dispatch)
882 free(icd->loader_dispatch);
883 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600884 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600885 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600886 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600887 libs = &(icd->layer_libs[j][i]);
888 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700889 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600890 libs->lib_handle = NULL;
891 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600892 if (icd->wrappedGpus[j])
893 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600894 }
895 icd->layer_count[j] = 0;
896 }
897 icd->gpu_count = 0;
898 }
899}
900
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600901extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600902{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600903 uint32_t gpu_index;
904 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700905 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600906 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600907
908 if (!icd)
909 return 0;
910 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
911
912 /* activate any layer libraries */
913 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600914 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
915 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600916 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600917
Jon Ashburnb8358052014-11-18 09:06:04 -0700918 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600919 if (!count)
920 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700921 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600922
Jon Ashburnd09bd102014-10-22 21:15:26 -0600923 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
924 if (! icd->wrappedGpus[gpu_index])
925 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600926 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600927 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600928 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600929 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600930 nextGpuObj->nextObject = gpuObj;
931 gpuObj = nextGpuObj;
932
Jon Ashburn79113cc2014-12-01 14:22:40 -0700933 char funcStr[256];
934 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700935 if ((nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
936 nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600937 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700938 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to find xglGetProcAddr in layer %s", icd->layer_libs[gpu_index][i].name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600939 continue;
940 }
941
Jon Ashburnf2610012014-10-24 15:48:55 -0600942 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600943 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600944 //Insert the new wrapped objects into the list with loader object at head
945 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
946 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
947 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
948 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700949 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600950 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600951
952 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600953 }
954 else {
955 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700956 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600957 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700958 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600959 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
960 break;
961 }
962 }
963 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700964 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600965 }
966 }
967 return icd->layer_count[gpu_index];
968}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600969
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700970LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
971 const XGL_APPLICATION_INFO* pAppInfo,
972 const XGL_ALLOC_CALLBACKS* pAllocCb,
973 XGL_INSTANCE* pInstance)
974{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700975 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
976 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700977 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700978 struct loader_scanned_icds *scanned_icds;
979 struct loader_icd *icd;
Ian Elliott55dbc5b2015-02-19 15:49:03 -0700980 XGL_RESULT res = XGL_ERROR_INITIALIZATION_FAILED;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600981
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700982 /* Scan/discover all ICD libraries in a single-threaded manner */
983 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700984
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700985 /* get layer libraries in a single-threaded manner */
986 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700987
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700988 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
989 if (ptr_instance == NULL) {
990 return XGL_ERROR_OUT_OF_MEMORY;
991 }
992 memset(ptr_instance, 0, sizeof(struct loader_instance));
993
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700994 ptr_instance->next = loader.instances;
995 loader.instances = ptr_instance;
996
Jon Ashburn46888392015-01-29 15:45:51 -0700997 scanned_icds = loader.scanned_icd_list;
998 while (scanned_icds) {
999 icd = loader_icd_add(ptr_instance, scanned_icds);
1000 if (icd) {
1001 res = scanned_icds->CreateInstance(pAppInfo, pAllocCb,
1002 &(scanned_icds->instance));
1003 if (res != XGL_SUCCESS)
1004 {
1005 ptr_instance->icds = ptr_instance->icds->next;
1006 loader_icd_destroy(icd);
1007 scanned_icds->instance = NULL;
1008 loader_log(XGL_DBG_MSG_WARNING, 0,
1009 "ICD ignored: failed to CreateInstance on device");
1010 }
1011 }
1012 scanned_icds = scanned_icds->next;
1013 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001014
Ian Elliotteb450762015-02-05 15:19:15 -07001015 if (ptr_instance->icds == NULL) {
1016 return XGL_ERROR_INCOMPATIBLE_DRIVER;
1017 }
Jon Ashburn46888392015-01-29 15:45:51 -07001018
1019 *pInstance = (XGL_INSTANCE) ptr_instance;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001020 return XGL_SUCCESS;
1021}
1022
1023LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
1024 XGL_INSTANCE instance)
1025{
1026 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -07001027 struct loader_scanned_icds *scanned_icds;
1028 XGL_RESULT res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001029
1030 // Remove this instance from the list of instances:
1031 struct loader_instance *prev = NULL;
1032 struct loader_instance *next = loader.instances;
1033 while (next != NULL) {
1034 if (next == ptr_instance) {
1035 // Remove this instance from the list:
1036 if (prev)
1037 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -07001038 else
1039 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001040 break;
1041 }
1042 prev = next;
1043 next = next->next;
1044 }
1045 if (next == NULL) {
1046 // This must be an invalid instance handle or empty list
1047 return XGL_ERROR_INVALID_HANDLE;
1048 }
1049
Jon Ashburn46888392015-01-29 15:45:51 -07001050 // cleanup any prior layer initializations
1051 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001052
Jon Ashburn46888392015-01-29 15:45:51 -07001053 scanned_icds = loader.scanned_icd_list;
1054 while (scanned_icds) {
1055 if (scanned_icds->instance)
1056 res = scanned_icds->DestroyInstance(scanned_icds->instance);
1057 if (res != XGL_SUCCESS)
1058 loader_log(XGL_DBG_MSG_WARNING, 0,
1059 "ICD ignored: failed to DestroyInstance on device");
1060 scanned_icds->instance = NULL;
1061 scanned_icds = scanned_icds->next;
1062 }
1063
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001064 free(ptr_instance);
1065
1066 return XGL_SUCCESS;
1067}
1068
1069LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
1070
1071 XGL_INSTANCE instance,
1072 uint32_t maxGpus,
1073 uint32_t* pGpuCount,
1074 XGL_PHYSICAL_GPU* pGpus)
1075{
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001076 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
1077 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -07001078 uint32_t count = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001079 XGL_RESULT res;
1080
1081 //in spirit of XGL don't error check on the instance parameter
1082 icd = ptr_instance->icds;
1083 while (icd) {
1084 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
1085 XGL_BASE_LAYER_OBJECT * wrapped_gpus;
1086 xglGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -07001087 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001088
1089 if (max > XGL_MAX_PHYSICAL_GPUS) {
1090 max = XGL_MAX_PHYSICAL_GPUS;
1091 }
1092
Jon Ashburn46888392015-01-29 15:45:51 -07001093 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
1094 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001095 gpus);
1096 if (res == XGL_SUCCESS && n) {
1097 wrapped_gpus = (XGL_BASE_LAYER_OBJECT*) malloc(n *
1098 sizeof(XGL_BASE_LAYER_OBJECT));
1099 icd->gpus = wrapped_gpus;
1100 icd->gpu_count = n;
1101 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n *
1102 sizeof(XGL_LAYER_DISPATCH_TABLE));
Ian Elliott19628802015-02-04 12:06:46 -07001103 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001104 (wrapped_gpus + i)->baseObject = gpus[i];
1105 (wrapped_gpus + i)->pGPA = get_proc_addr;
1106 (wrapped_gpus + i)->nextObject = gpus[i];
1107 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
1108 loader_init_dispatch_table(icd->loader_dispatch + i,
1109 get_proc_addr, gpus[i]);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -07001110
1111 /* Verify ICD compatibility */
1112 if (!valid_loader_magic_value(gpus[i])) {
1113 loader_log(XGL_DBG_MSG_WARNING, 0,
1114 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
1115 assert(0);
1116 }
1117
Jon Ashburn4c392fb2015-01-28 19:57:09 -07001118 const XGL_LAYER_DISPATCH_TABLE **disp;
1119 disp = (const XGL_LAYER_DISPATCH_TABLE **) gpus[i];
1120 *disp = icd->loader_dispatch + i;
1121 }
1122
1123 if (loader_icd_set_global_options(icd) != XGL_SUCCESS ||
1124 loader_icd_register_msg_callbacks(icd) != XGL_SUCCESS) {
1125 loader_log(XGL_DBG_MSG_WARNING, 0,
1126 "ICD ignored: failed to migrate settings");
1127 loader_icd_destroy(icd);
1128 }
1129 count += n;
1130
1131 if (count >= maxGpus) {
1132 break;
1133 }
1134 }
1135
1136 icd = icd->next;
1137 }
1138
1139 /* we have nothing to log anymore */
1140 loader_msg_callback_clear();
1141
1142 *pGpuCount = count;
1143
1144 return (count > 0) ? XGL_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -07001145}
1146
1147LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
1148{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001149 if (gpu == NULL) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001150 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001151 }
Jon Ashburn891537f2014-10-22 12:42:13 -06001152 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -06001153 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001154 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001155
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001156 if (disp_table == NULL)
1157 return NULL;
1158
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001159 addr = loader_lookup_dispatch_table(disp_table, pName);
1160 if (addr)
1161 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001162 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001163 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001164 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -06001165 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001166 }
1167}
1168
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001169LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateLayers(XGL_PHYSICAL_GPU gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001170{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001171 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001172 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001173 char *lib_name;
1174 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001175 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -06001176 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001177 char layer_buf[16][256];
1178 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001179
Jon Ashburn1323eb72014-12-02 13:03:09 -07001180 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001181 return XGL_ERROR_INVALID_POINTER;
1182
Jon Ashburn46d1f582015-01-28 11:01:35 -07001183 if (!icd)
1184 return XGL_ERROR_UNAVAILABLE;
1185
Jon Ashburn1323eb72014-12-02 13:03:09 -07001186 for (int i = 0; i < 16; i++)
1187 layers[i] = &layer_buf[i][0];
1188
1189 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1190 lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001191 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1192 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001193 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001194 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
1195 //use default layer name based on library name XGL_LAYER_LIBRARY_PREFIX<name>.XGL_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001196 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001197 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001198 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001199 lib_name = basename(lib_name);
1200 pEnd = strrchr(lib_name, '.');
Ian Elliott19628802015-02-04 12:06:46 -07001201 siz = (int) (pEnd - lib_name - strlen(XGL_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001202 if (pEnd == NULL || siz <= 0)
1203 continue;
1204 cpyStr = malloc(siz);
1205 if (cpyStr == NULL) {
1206 free(cpyStr);
1207 continue;
1208 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001209 strncpy(cpyStr, lib_name + strlen(XGL_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001210 cpyStr[siz - 1] = '\0';
1211 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001212 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001213 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1214 pOutLayers[count][siz - 1] = '\0';
1215 count++;
1216 free(cpyStr);
1217 }
1218 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001219 size_t cnt;
1220 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001221 XGL_RESULT res;
Ian Elliott19628802015-02-04 12:06:46 -07001222 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001223 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1224 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001225 if (res != XGL_SUCCESS)
1226 continue;
1227 if (cnt + count > maxLayerCount)
1228 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001229 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001230 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1231 if (n > 0)
1232 pOutLayers[i - count][n - 1] = '\0';
1233 }
1234 count += cnt;
1235 }
1236 }
1237
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001238 *pOutLayerCount = count;
1239
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001240 return XGL_SUCCESS;
1241}
1242
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001243LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001244{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001245 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001246 struct loader_instance *inst;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001247 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001248 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001249
Jon Ashburn46d1f582015-01-28 11:01:35 -07001250 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001251 return loader_msg_callback_add(pfnMsgCallback, pUserData);
1252 }
1253
Jon Ashburn98bd4542015-01-29 16:44:24 -07001254 for (inst = loader.instances; inst; inst = inst->next) {
1255 for (icd = inst->icds; icd; icd = icd->next) {
1256 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1257 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(
1258 pfnMsgCallback, pUserData);
1259 if (res != XGL_SUCCESS) {
1260 gpu_idx = i;
1261 break;
1262 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001263 }
Jon Ashburn98bd4542015-01-29 16:44:24 -07001264 if (res != XGL_SUCCESS)
1265 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001266 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001267 if (res != XGL_SUCCESS)
1268 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001269 }
1270
1271 /* roll back on errors */
1272 if (icd) {
Jon Ashburn98bd4542015-01-29 16:44:24 -07001273 for (struct loader_instance *tmp_inst = loader.instances;
1274 tmp_inst != inst; tmp_inst = tmp_inst->next) {
1275 for (const struct loader_icd * tmp = tmp_inst->icds; tmp != icd;
1276 tmp = tmp->next) {
1277 for (uint32_t i = 0; i < icd->gpu_count; i++)
1278 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1279 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001280 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001281 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001282 for (uint32_t i = 0; i < gpu_idx; i++)
Jon Ashburn01e2d662014-11-14 09:52:42 -07001283 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001284
1285 return res;
1286 }
1287
1288 return XGL_SUCCESS;
1289}
1290
Chia-I Wu19300602014-08-04 08:03:57 +08001291LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001292{
Jon Ashburn98bd4542015-01-29 16:44:24 -07001293 XGL_RESULT res = XGL_SUCCESS;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001294
Jon Ashburn46d1f582015-01-28 11:01:35 -07001295 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001296 return loader_msg_callback_remove(pfnMsgCallback);
1297 }
1298
Jon Ashburn98bd4542015-01-29 16:44:24 -07001299 for (struct loader_instance *inst = loader.instances; inst;
1300 inst = inst->next) {
1301 for (const struct loader_icd * icd = inst->icds; icd;
1302 icd = icd->next) {
1303 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1304 XGL_RESULT r;
1305 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(pfnMsgCallback);
1306 if (r != XGL_SUCCESS) {
1307 res = r;
1308 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001309 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001310 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001311 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001312 return res;
1313}
1314
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001315LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001316{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001317 XGL_RESULT res = XGL_SUCCESS;
1318
Jon Ashburn46d1f582015-01-28 11:01:35 -07001319 if (!loader.icds_scanned) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001320 if (dataSize == 0)
1321 return XGL_ERROR_INVALID_VALUE;
1322
1323 switch (dbgOption) {
1324 case XGL_DBG_OPTION_DEBUG_ECHO_ENABLE:
1325 loader.debug_echo_enable = *((const bool *) pData);
1326 break;
1327 case XGL_DBG_OPTION_BREAK_ON_ERROR:
1328 loader.break_on_error = *((const bool *) pData);
1329 break;
1330 case XGL_DBG_OPTION_BREAK_ON_WARNING:
1331 loader.break_on_warning = *((const bool *) pData);
1332 break;
1333 default:
1334 res = XGL_ERROR_INVALID_VALUE;
1335 break;
1336 }
1337
1338 return res;
1339 }
1340
Jon Ashburn98bd4542015-01-29 16:44:24 -07001341 for (struct loader_instance *inst = loader.instances; inst;
1342 inst = inst->next) {
1343 for (const struct loader_icd * icd = inst->icds; icd;
1344 icd = icd->next) {
1345 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1346 XGL_RESULT r;
1347 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(dbgOption,
1348 dataSize, pData);
1349 /* unfortunately we cannot roll back */
1350 if (r != XGL_SUCCESS) {
1351 res = r;
1352 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001353 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001354 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001355 }
1356
1357 return res;
1358}