blob: 4dafc223805e6fae8c72aaeaff30bda24e62c209 [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
Jon Ashburn46d1f582015-01-28 11:01:35 -070080struct loader_scanned_icds {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -070081 loader_platform_dl_handle handle;
Jon Ashburn46d1f582015-01-28 11:01:35 -070082 xglGetProcAddrType GetProcAddr;
Jon Ashburn46888392015-01-29 15:45:51 -070083 xglCreateInstanceType CreateInstance;
84 xglDestroyInstanceType DestroyInstance;
Jon Ashburn4c392fb2015-01-28 19:57:09 -070085 xglEnumerateGpusType EnumerateGpus;
Jon Ashburn46888392015-01-29 15:45:51 -070086 XGL_INSTANCE instance;
Jon Ashburn46d1f582015-01-28 11:01:35 -070087 struct loader_scanned_icds *next;
88};
Jon Ashburnd38bfb12014-10-14 19:15:22 -060089
Jon Ashburn1beab2d2015-01-26 14:51:40 -070090// Note: Since the following is a static structure, all members are initialized
91// to zero.
Chia-I Wu5f72d0f2014-08-01 11:21:23 +080092static struct {
Jon Ashburn1beab2d2015-01-26 14:51:40 -070093 struct loader_instance *instances;
Jon Ashburn46d1f582015-01-28 11:01:35 -070094 bool icds_scanned;
95 struct loader_scanned_icds *scanned_icd_list;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060096 bool layer_scanned;
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -070097 char *layer_dirs;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -060098 unsigned int scanned_layer_count;
99 char *scanned_layer_names[MAX_LAYER_LIBRARIES];
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800100} loader;
101
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700102
Ian Elliott4470a302015-02-17 10:33:47 -0700103#if defined(WIN32)
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600104char *loader_get_registry_string(const HKEY hive,
105 const LPCTSTR sub_key,
106 const char *value)
107{
108 DWORD access_flags = KEY_QUERY_VALUE;
109 DWORD value_type;
110 HKEY key;
111 LONG rtn_value;
112 char *rtn_str = NULL;
113 size_t rtn_len = 0;
114 size_t allocated_len = 0;
115
116 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
117 if (rtn_value != ERROR_SUCCESS) {
118 // We didn't find the key. Try the 32-bit hive (where we've seen the
119 // key end up on some people's systems):
120 access_flags |= KEY_WOW64_32KEY;
121 rtn_value = RegOpenKeyEx(hive, sub_key, 0, access_flags, &key);
122 if (rtn_value != ERROR_SUCCESS) {
123 // We still couldn't find the key, so give up:
124 return NULL;
125 }
126 }
127
128 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
129 (PVOID) rtn_str, &rtn_len);
130 if (rtn_value == ERROR_SUCCESS) {
131 // If we get to here, we found the key, and need to allocate memory
132 // large enough for rtn_str, and query again:
133 allocated_len = rtn_len + 4;
134 rtn_str = malloc(allocated_len);
135 rtn_value = RegQueryValueEx(key, value, NULL, &value_type,
136 (PVOID) rtn_str, &rtn_len);
137 if (rtn_value == ERROR_SUCCESS) {
138 // We added 4 extra bytes to rtn_str, so that we can ensure that
139 // the string is NULL-terminated (albeit, in a brute-force manner):
140 rtn_str[allocated_len-1] = '\0';
141 } else {
142 // This should never occur, but in case it does, clean up:
143 free(rtn_str);
144 rtn_str = NULL;
145 }
146 } // else - shouldn't happen, but if it does, return rtn_str, which is NULL
147
148 // Close the registry key that was opened:
149 RegCloseKey(key);
150
151 return rtn_str;
152}
153
154
Ian Elliott4470a302015-02-17 10:33:47 -0700155// For ICD developers, look in the registry, and look for an environment
156// variable for a path(s) where to find the ICD(s):
157static char *loader_get_registry_and_env(const char *env_var,
158 const char *registry_value)
159{
160 char *env_str = getenv(env_var);
161 size_t env_len = (env_str == NULL) ? 0 : strlen(env_str);
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600162 char *registry_str = NULL;
163 DWORD registry_len = 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700164 char *rtn_str = NULL;
165 size_t rtn_len;
166
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600167 registry_str = loader_get_registry_string(HKEY_LOCAL_MACHINE,
168 "Software\\XGL",
169 registry_value);
Ian Elliott2de26bc2015-04-03 13:13:01 -0600170 registry_len = (registry_str) ? strlen(registry_str) : 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700171
172 rtn_len = env_len + registry_len + 1;
173 if (rtn_len <= 2) {
174 // We found neither the desired registry value, nor the environment
175 // variable; return NULL:
176 return NULL;
177 } else {
178 // We found something, and so we need to allocate memory for the string
179 // to return:
180 rtn_str = malloc(rtn_len);
181 }
182
Ian Elliott5aa4ea22015-03-31 15:32:41 -0600183 if (registry_len == 0) {
Ian Elliott4470a302015-02-17 10:33:47 -0700184 // We didn't find the desired registry value, and so we must have found
185 // only the environment variable:
186 _snprintf(rtn_str, rtn_len, "%s", env_str);
187 } else if (env_str != NULL) {
188 // We found both the desired registry value and the environment
189 // variable, so concatenate them both:
190 _snprintf(rtn_str, rtn_len, "%s;%s", registry_str, env_str);
191 } else {
192 // We must have only found the desired registry value:
193 _snprintf(rtn_str, rtn_len, "%s", registry_str);
194 }
195
Ian Elliott2de26bc2015-04-03 13:13:01 -0600196 if (registry_str) {
197 free(registry_str);
198 }
Ian Elliott4470a302015-02-17 10:33:47 -0700199
200 return(rtn_str);
201}
202#endif // WIN32
203
204
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600205static void loader_log(XGL_DBG_MSG_TYPE msg_type, int32_t msg_code,
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800206 const char *format, ...)
207{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800208 char msg[256];
209 va_list ap;
210 int ret;
211
212 va_start(ap, format);
213 ret = vsnprintf(msg, sizeof(msg), format, ap);
Ian Elliott42045842015-02-13 14:29:21 -0700214 if ((ret >= (int) sizeof(msg)) || ret < 0) {
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800215 msg[sizeof(msg) - 1] = '\0';
216 }
217 va_end(ap);
218
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -0600219 fputs(msg, stderr);
220 fputc('\n', stderr);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800221}
222
223static void
224loader_icd_destroy(struct loader_icd *icd)
225{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700226 loader_platform_close_library(icd->scanned_icds->handle);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800227 free(icd);
228}
229
230static struct loader_icd *
Jon Ashburn46d1f582015-01-28 11:01:35 -0700231loader_icd_create(const struct loader_scanned_icds *scanned)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800232{
233 struct loader_icd *icd;
234
235 icd = malloc(sizeof(*icd));
236 if (!icd)
237 return NULL;
238
Courtney Goeltzenleuchter55001bb2014-10-28 10:29:27 -0600239 memset(icd, 0, sizeof(*icd));
240
Jon Ashburn46d1f582015-01-28 11:01:35 -0700241 icd->scanned_icds = scanned;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800242
243 return icd;
244}
245
Jon Ashburn46888392015-01-29 15:45:51 -0700246static struct loader_icd *loader_icd_add(struct loader_instance *ptr_inst,
247 const struct loader_scanned_icds *scanned)
Chia-I Wu13a61a52014-08-04 11:18:20 +0800248{
249 struct loader_icd *icd;
250
Jon Ashburn46d1f582015-01-28 11:01:35 -0700251 icd = loader_icd_create(scanned);
Chia-I Wu13a61a52014-08-04 11:18:20 +0800252 if (!icd)
253 return NULL;
254
Chia-I Wu13a61a52014-08-04 11:18:20 +0800255 /* prepend to the list */
Jon Ashburn46888392015-01-29 15:45:51 -0700256 icd->next = ptr_inst->icds;
257 ptr_inst->icds = icd;
Chia-I Wu13a61a52014-08-04 11:18:20 +0800258
259 return icd;
260}
261
Jon Ashburn46d1f582015-01-28 11:01:35 -0700262static void loader_scanned_icd_add(const char *filename)
263{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700264 loader_platform_dl_handle handle;
Jon Ashburn46888392015-01-29 15:45:51 -0700265 void *fp_gpa, *fp_enumerate, *fp_create_inst, *fp_destroy_inst;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700266 struct loader_scanned_icds *new_node;
267
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700268 // Used to call: dlopen(filename, RTLD_LAZY);
269 handle = loader_platform_open_library(filename);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700270 if (!handle) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700271 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_open_library_error(filename));
Jon Ashburn46d1f582015-01-28 11:01:35 -0700272 return;
273 }
274
275#define LOOKUP(func_ptr, func) do { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700276 func_ptr = (xgl ##func## Type) loader_platform_get_proc_address(handle, "xgl" #func); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700277 if (!func_ptr) { \
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700278 loader_log(XGL_DBG_MSG_WARNING, 0, loader_platform_get_proc_address_error("xgl" #func)); \
Jon Ashburn46d1f582015-01-28 11:01:35 -0700279 return; \
280 } \
281} while (0)
282
283 LOOKUP(fp_gpa, GetProcAddr);
Jon Ashburn46888392015-01-29 15:45:51 -0700284 LOOKUP(fp_create_inst, CreateInstance);
285 LOOKUP(fp_destroy_inst, DestroyInstance);
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700286 LOOKUP(fp_enumerate, EnumerateGpus);
Jon Ashburn46d1f582015-01-28 11:01:35 -0700287#undef LOOKUP
288
289 new_node = (struct loader_scanned_icds *) malloc(sizeof(struct loader_scanned_icds));
290 if (!new_node) {
291 loader_log(XGL_DBG_MSG_WARNING, 0, "Out of memory can't add icd");
292 return;
293 }
294
295 new_node->handle = handle;
296 new_node->GetProcAddr = fp_gpa;
Jon Ashburn46888392015-01-29 15:45:51 -0700297 new_node->CreateInstance = fp_create_inst;
298 new_node->DestroyInstance = fp_destroy_inst;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700299 new_node->EnumerateGpus = fp_enumerate;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700300 new_node->next = loader.scanned_icd_list;
301 loader.scanned_icd_list = new_node;
302}
303
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700304
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600305/**
306 * Try to \c loader_icd_scan XGL driver(s).
307 *
308 * This function scans the default system path or path
309 * specified by the \c LIBXGL_DRIVERS_PATH environment variable in
310 * order to find loadable XGL ICDs with the name of libXGL_*.
311 *
312 * \returns
313 * void; but side effect is to set loader_icd_scanned to true
314 */
315static void loader_icd_scan(void)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800316{
Ian Elliott4470a302015-02-17 10:33:47 -0700317 const char *p, *next;
318 char *libPaths = NULL;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600319 DIR *sysdir;
320 struct dirent *dent;
321 char icd_library[1024];
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600322 char path[1024];
Ian Elliott19628802015-02-04 12:06:46 -0700323 uint32_t len;
Ian Elliott4470a302015-02-17 10:33:47 -0700324#if defined(WIN32)
325 bool must_free_libPaths;
326 libPaths = loader_get_registry_and_env(DRIVER_PATH_ENV,
327 DRIVER_PATH_REGISTRY_VALUE);
328 if (libPaths != NULL) {
329 must_free_libPaths = true;
330 } else {
331 must_free_libPaths = false;
332 libPaths = DEFAULT_XGL_DRIVERS_PATH;
333 }
334#else // WIN32
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600335 if (geteuid() == getuid()) {
Ian Elliott4470a302015-02-17 10:33:47 -0700336 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
337 libPaths = getenv(DRIVER_PATH_ENV);
338 }
339 if (libPaths == NULL) {
340 libPaths = DEFAULT_XGL_DRIVERS_PATH;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600341 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700342#endif // WIN32
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800343
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600344 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700345 next = strchr(p, PATH_SEPERATOR);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600346 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700347 len = (uint32_t) strlen(p);
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600348 next = p + len;
349 }
350 else {
Ian Elliott19628802015-02-04 12:06:46 -0700351 len = (uint32_t) (next - p);
Jon Ashburn5cda59c2014-10-03 16:31:35 -0600352 sprintf(path, "%.*s", (len > sizeof(path) - 1) ? (int) sizeof(path) - 1 : len, p);
353 p = path;
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600354 next++;
355 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800356
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700357 // TODO/TBD: Do we want to do this on Windows, or just let Windows take
358 // care of its own search path (which it apparently has)?
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600359 sysdir = opendir(p);
360 if (sysdir) {
361 dent = readdir(sysdir);
362 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700363 /* Look for ICDs starting with XGL_DRIVER_LIBRARY_PREFIX and
364 * ending with XGL_LIBRARY_SUFFIX
365 */
366 if (!strncmp(dent->d_name,
367 XGL_DRIVER_LIBRARY_PREFIX,
368 XGL_DRIVER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700369 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700370 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
371 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
372 !strncmp(suf,
373 XGL_LIBRARY_SUFFIX,
374 XGL_LIBRARY_SUFFIX_LEN)) {
375 snprintf(icd_library, 1024, "%s" DIRECTORY_SYMBOL "%s", p,dent->d_name);
376 loader_scanned_icd_add(icd_library);
377 }
378 }
Courtney Goeltzenleuchter4af9bd12014-08-01 14:18:11 -0600379
380 dent = readdir(sysdir);
381 }
382 closedir(sysdir);
383 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800384 }
385
Ian Elliott4470a302015-02-17 10:33:47 -0700386#if defined(WIN32)
387 // Free any allocated memory:
388 if (must_free_libPaths) {
389 free(libPaths);
390 }
391#endif // WIN32
392
393 // Note that we've scanned for ICDs:
Jon Ashburn46d1f582015-01-28 11:01:35 -0700394 loader.icds_scanned = true;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +0800395}
396
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600397
Ian Elliott4470a302015-02-17 10:33:47 -0700398static void layer_lib_scan(void)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600399{
400 const char *p, *next;
Ian Elliott4470a302015-02-17 10:33:47 -0700401 char *libPaths = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600402 DIR *curdir;
403 struct dirent *dent;
Ian Elliott4470a302015-02-17 10:33:47 -0700404 size_t len, i;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600405 char temp_str[1024];
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600406
Ian Elliott4470a302015-02-17 10:33:47 -0700407#if defined(WIN32)
408 bool must_free_libPaths;
409 libPaths = loader_get_registry_and_env(LAYERS_PATH_ENV,
410 LAYERS_PATH_REGISTRY_VALUE);
411 if (libPaths != NULL) {
412 must_free_libPaths = true;
413 } else {
414 must_free_libPaths = false;
415 libPaths = DEFAULT_XGL_LAYERS_PATH;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600416 }
Ian Elliott4470a302015-02-17 10:33:47 -0700417#else // WIN32
418 if (geteuid() == getuid()) {
419 /* Don't allow setuid apps to use the DRIVER_PATH_ENV env var: */
Courtney Goeltzenleuchter66b72f92015-02-18 20:03:02 -0700420 libPaths = getenv(LAYERS_PATH_ENV);
Ian Elliott4470a302015-02-17 10:33:47 -0700421 }
422 if (libPaths == NULL) {
423 libPaths = DEFAULT_XGL_LAYERS_PATH;
424 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700425#endif // WIN32
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600426
Ian Elliott4470a302015-02-17 10:33:47 -0700427 if (libPaths == NULL) {
428 // Have no paths to search:
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700429 return;
430 }
Ian Elliott4470a302015-02-17 10:33:47 -0700431 len = strlen(libPaths);
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700432 loader.layer_dirs = malloc(len+1);
Ian Elliott4470a302015-02-17 10:33:47 -0700433 if (loader.layer_dirs == NULL) {
434 free(libPaths);
Courtney Goeltzenleuchtera66265b2014-12-02 18:12:51 -0700435 return;
Ian Elliott4470a302015-02-17 10:33:47 -0700436 }
437 // Alloc passed, so we know there is enough space to hold the string, don't
438 // need strncpy
439 strcpy(loader.layer_dirs, libPaths);
440#if defined(WIN32)
441 // Free any allocated memory:
442 if (must_free_libPaths) {
443 free(libPaths);
444 must_free_libPaths = false;
445 }
446#endif // WIN32
Courtney Goeltzenleuchter57985ce2014-12-01 09:29:42 -0700447 libPaths = loader.layer_dirs;
448
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600449 /* cleanup any previously scanned libraries */
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600450 for (i = 0; i < loader.scanned_layer_count; i++) {
451 if (loader.scanned_layer_names[i] != NULL)
452 free(loader.scanned_layer_names[i]);
453 loader.scanned_layer_names[i] = NULL;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600454 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600455 loader.scanned_layer_count = 0;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600456
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600457 for (p = libPaths; *p; p = next) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700458 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600459 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700460 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600461 next = p + len;
462 }
463 else {
Ian Elliott19628802015-02-04 12:06:46 -0700464 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600465 *(char *) next = '\0';
466 next++;
467 }
468
469 curdir = opendir(p);
470 if (curdir) {
471 dent = readdir(curdir);
472 while (dent) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700473 /* Look for layers starting with XGL_LAYER_LIBRARY_PREFIX and
474 * ending with XGL_LIBRARY_SUFFIX
475 */
476 if (!strncmp(dent->d_name,
477 XGL_LAYER_LIBRARY_PREFIX,
478 XGL_LAYER_LIBRARY_PREFIX_LEN)) {
Ian Elliott19628802015-02-04 12:06:46 -0700479 uint32_t nlen = (uint32_t) strlen(dent->d_name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700480 const char *suf = dent->d_name + nlen - XGL_LIBRARY_SUFFIX_LEN;
481 if ((nlen > XGL_LIBRARY_SUFFIX_LEN) &&
482 !strncmp(suf,
483 XGL_LIBRARY_SUFFIX,
484 XGL_LIBRARY_SUFFIX_LEN)) {
485 loader_platform_dl_handle handle;
486 snprintf(temp_str, sizeof(temp_str), "%s" DIRECTORY_SYMBOL "%s",p,dent->d_name);
487 // Used to call: dlopen(temp_str, RTLD_LAZY)
488 if ((handle = loader_platform_open_library(temp_str)) == NULL) {
489 dent = readdir(curdir);
490 continue;
491 }
492 if (loader.scanned_layer_count == MAX_LAYER_LIBRARIES) {
493 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: max layer libraries exceed", temp_str);
494 break;
495 }
496 if ((loader.scanned_layer_names[loader.scanned_layer_count] = malloc(strlen(temp_str) + 1)) == NULL) {
497 loader_log(XGL_DBG_MSG_ERROR, 0, "%s ignored: out of memory", temp_str);
498 break;
499 }
500 strcpy(loader.scanned_layer_names[loader.scanned_layer_count], temp_str);
501 loader.scanned_layer_count++;
502 loader_platform_close_library(handle);
503 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600504 }
505
506 dent = readdir(curdir);
507 }
508 closedir(curdir);
509 }
510 }
511
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600512 loader.layer_scanned = true;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600513}
514
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600515static void loader_init_dispatch_table(XGL_LAYER_DISPATCH_TABLE *tab, xglGetProcAddrType fpGPA, XGL_PHYSICAL_GPU gpu)
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600516{
Chia-I Wuf46b81a2015-01-04 11:12:47 +0800517 loader_initialize_dispatch_table(tab, fpGPA, gpu);
518
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -0600519 if (tab->EnumerateLayers == NULL)
520 tab->EnumerateLayers = xglEnumerateLayers;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600521}
522
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600523static struct loader_icd * loader_get_icd(const XGL_BASE_LAYER_OBJECT *gpu, uint32_t *gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600524{
Jon Ashburn98bd4542015-01-29 16:44:24 -0700525 for (struct loader_instance *inst = loader.instances; inst; inst = inst->next) {
526 for (struct loader_icd *icd = inst->icds; icd; icd = icd->next) {
527 for (uint32_t i = 0; i < icd->gpu_count; i++)
528 if ((icd->gpus + i) == gpu || (icd->gpus +i)->baseObject ==
529 gpu->baseObject) {
530 *gpu_index = i;
531 return icd;
532 }
533 }
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600534 }
535 return NULL;
536}
537
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600538static bool loader_layers_activated(const struct loader_icd *icd, const uint32_t gpu_index)
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600539{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600540 if (icd->layer_count[gpu_index])
541 return true;
542 else
543 return false;
Jon Ashburn876b1ac2014-10-17 15:09:07 -0600544}
545
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600546static 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 -0600547{
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600548 if (!icd)
549 return;
Jon Ashburndf7d5842014-10-16 15:48:50 -0600550
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600551 struct loader_layers *obj;
552 bool foundLib;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600553 for (uint32_t i = 0; i < count; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600554 foundLib = false;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600555 for (uint32_t j = 0; j < icd->layer_count[gpu_index]; j++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700556 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 -0600557 foundLib = true;
558 break;
559 }
560 }
561 if (!foundLib) {
562 obj = &(icd->layer_libs[gpu_index][i]);
Jon Ashburnb8358052014-11-18 09:06:04 -0700563 strncpy(obj->name, (char *) pLayerNames[i].layer_name, sizeof(obj->name) - 1);
564 obj->name[sizeof(obj->name) - 1] = '\0';
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700565 // Used to call: dlopen(pLayerNames[i].lib_name, RTLD_LAZY | RTLD_DEEPBIND)
566 if ((obj->lib_handle = loader_platform_open_library(pLayerNames[i].lib_name)) == NULL) {
567 loader_log(XGL_DBG_MSG_ERROR, 0, loader_platform_open_library_error(pLayerNames[i].lib_name));
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600568 continue;
569 } else {
Jon Ashburnb8358052014-11-18 09:06:04 -0700570 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 -0600571 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700572 free(pLayerNames[i].layer_name);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600573 icd->layer_count[gpu_index]++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600574 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600575 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600576}
577
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600578static 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 -0700579{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700580 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600581 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600582 char layer_buf[16][256];
583 char * layers[16];
Jon Ashburnb8358052014-11-18 09:06:04 -0700584
585 for (int i = 0; i < 16; i++)
586 layers[i] = &layer_buf[i][0];
587
588 for (unsigned int j = 0; j < loader.scanned_layer_count; j++) {
589 *lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700590 // Used to call: dlopen(*lib_name, RTLD_LAZY)
591 if ((handle = loader_platform_open_library(*lib_name)) == NULL)
Jon Ashburnb8358052014-11-18 09:06:04 -0700592 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700593 if ((fpEnumerateLayers = (xglEnumerateLayersType) loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700594 char * lib_str = malloc(strlen(*lib_name) + 1 + strlen(layer_name));
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700595 //use default layer name
596 snprintf(lib_str, strlen(*lib_name) + strlen(layer_name),
Courtney Goeltzenleuchter8d095ff2015-04-03 14:53:08 -0600597 XGL_LAYER_LIBRARY_PREFIX "%s" XGL_LIBRARY_SUFFIX,
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700598 layer_name);
599 loader_platform_close_library(handle);
600 if (!strcmp(*lib_name, lib_str)) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700601 free(lib_str);
602 return true;
603 }
604 else {
605 free(lib_str);
606 continue;
607 }
608 }
609 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600610 size_t cnt;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700611 fpEnumerateLayers(NULL, 16, 256, &cnt, layers, (char *) icd->gpus + gpu_index);
Jon Ashburnb8358052014-11-18 09:06:04 -0700612 for (unsigned int i = 0; i < cnt; i++) {
613 if (!strcmp((char *) layers[i], layer_name)) {
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700614 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700615 return true;
616 }
617 }
618 }
619
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700620 loader_platform_close_library(handle);
Jon Ashburnb8358052014-11-18 09:06:04 -0700621 }
622
623 return false;
624}
625
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600626static 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 -0600627{
Ian Elliott4470a302015-02-17 10:33:47 -0700628 char *layerEnv;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600629 uint32_t len, count = 0;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600630 char *p, *pOrig, *next, *name;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600631
Ian Elliott4470a302015-02-17 10:33:47 -0700632#if defined(WIN32)
633 layerEnv = loader_get_registry_and_env(LAYER_NAMES_ENV,
634 LAYER_NAMES_REGISTRY_VALUE);
635#else // WIN32
636 layerEnv = getenv(LAYER_NAMES_ENV);
637#endif // WIN32
638 if (layerEnv == NULL) {
Jon Ashburn3fb55442014-10-23 10:29:09 -0600639 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700640 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600641 p = malloc(strlen(layerEnv) + 1);
Ian Elliott4470a302015-02-17 10:33:47 -0700642 if (p == NULL) {
643#if defined(WIN32)
644 free(layerEnv);
645#endif // WIN32
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600646 return 0;
Ian Elliott4470a302015-02-17 10:33:47 -0700647 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600648 strcpy(p, layerEnv);
Ian Elliott4470a302015-02-17 10:33:47 -0700649#if defined(WIN32)
650 free(layerEnv);
651#endif // WIN32
Jon Ashburnd09bd102014-10-22 21:15:26 -0600652 pOrig = p;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600653
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600654 while (p && *p && count < MAX_LAYER_LIBRARIES) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700655 const char *lib_name = NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700656 next = strchr(p, PATH_SEPERATOR);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600657 if (next == NULL) {
Ian Elliott19628802015-02-04 12:06:46 -0700658 len = (uint32_t) strlen(p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600659 next = p + len;
660 }
661 else {
Ian Elliott19628802015-02-04 12:06:46 -0700662 len = (uint32_t) (next - p);
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600663 *(char *) next = '\0';
664 next++;
665 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600666 name = basename(p);
Jon Ashburnb8358052014-11-18 09:06:04 -0700667 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600668 p = next;
669 continue;
670 }
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600671
Ian Elliott19628802015-02-04 12:06:46 -0700672 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700673 pLayerNames[count].layer_name = malloc(len + 1);
674 if (!pLayerNames[count].layer_name) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600675 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600676 return count;
Jon Ashburnd09bd102014-10-22 21:15:26 -0600677 }
Jon Ashburnb8358052014-11-18 09:06:04 -0700678 strncpy((char *) pLayerNames[count].layer_name, name, len);
679 pLayerNames[count].layer_name[len] = '\0';
680 pLayerNames[count].lib_name = lib_name;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600681 count++;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600682 p = next;
683
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600684 };
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600685
Jon Ashburnd09bd102014-10-22 21:15:26 -0600686 free(pOrig);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600687 return count;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600688}
689
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600690static 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 -0600691{
Jon Ashburnb8358052014-11-18 09:06:04 -0700692 static struct layer_name_pair layerNames[MAX_LAYER_LIBRARIES];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700693 int env_layer_count = 0;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600694
695 *ppLayerNames = &layerNames[0];
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700696 /* Load any layers specified in the environment first */
697 env_layer_count = loader_get_layer_env(icd, gpu_index, layerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600698
Chia-I Wub0a2cc92015-01-28 14:00:47 +0800699 const XGL_LAYER_CREATE_INFO *pCi =
700 (const XGL_LAYER_CREATE_INFO *) pCreateInfo->pNext;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600701
702 while (pCi) {
703 if (pCi->sType == XGL_STRUCTURE_TYPE_LAYER_CREATE_INFO) {
704 const char *name;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700705 uint32_t len, j = 0;
Courtney Goeltzenleuchter89ba9282015-02-17 14:21:21 -0700706 for (uint32_t i = env_layer_count; i < (env_layer_count + pCi->layerCount); i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700707 const char * lib_name = NULL;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700708 name = *(pCi->ppActiveLayerNames + j);
709 if (!find_layer_name(icd, gpu_index, name, &lib_name)) {
710 return i;
711 }
Ian Elliott19628802015-02-04 12:06:46 -0700712 len = (uint32_t) strlen(name);
Jon Ashburnb8358052014-11-18 09:06:04 -0700713 layerNames[i].layer_name = malloc(len + 1);
714 if (!layerNames[i].layer_name)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600715 return i;
Jon Ashburnb8358052014-11-18 09:06:04 -0700716 strncpy((char *) layerNames[i].layer_name, name, len);
717 layerNames[i].layer_name[len] = '\0';
718 layerNames[i].lib_name = lib_name;
Jon Ashburnc0e86012015-02-18 11:29:58 -0700719 j++;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600720 }
Jon Ashburnc0e86012015-02-18 11:29:58 -0700721 return pCi->layerCount + env_layer_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600722 }
723 pCi = pCi->pNext;
724 }
Jon Ashburnc0e86012015-02-18 11:29:58 -0700725 return env_layer_count;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600726}
727
Jon Ashburn46d1f582015-01-28 11:01:35 -0700728static void loader_deactivate_layer(const struct loader_instance *instance)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600729{
730 struct loader_icd *icd;
731 struct loader_layers *libs;
732
Jon Ashburn46d1f582015-01-28 11:01:35 -0700733 for (icd = instance->icds; icd; icd = icd->next) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600734 if (icd->gpus)
735 free(icd->gpus);
736 icd->gpus = NULL;
737 if (icd->loader_dispatch)
738 free(icd->loader_dispatch);
739 icd->loader_dispatch = NULL;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600740 for (uint32_t j = 0; j < icd->gpu_count; j++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600741 if (icd->layer_count[j] > 0) {
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600742 for (uint32_t i = 0; i < icd->layer_count[j]; i++) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600743 libs = &(icd->layer_libs[j][i]);
744 if (libs->lib_handle)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700745 loader_platform_close_library(libs->lib_handle);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600746 libs->lib_handle = NULL;
747 }
Jon Ashburnd09bd102014-10-22 21:15:26 -0600748 if (icd->wrappedGpus[j])
749 free(icd->wrappedGpus[j]);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600750 }
751 icd->layer_count[j] = 0;
752 }
753 icd->gpu_count = 0;
754 }
755}
756
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600757extern uint32_t loader_activate_layers(XGL_PHYSICAL_GPU gpu, const XGL_DEVICE_CREATE_INFO* pCreateInfo)
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600758{
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600759 uint32_t gpu_index;
760 uint32_t count;
Jon Ashburnb8358052014-11-18 09:06:04 -0700761 struct layer_name_pair *pLayerNames;
Jon Ashburnf2610012014-10-24 15:48:55 -0600762 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600763
764 if (!icd)
765 return 0;
766 assert(gpu_index < XGL_MAX_PHYSICAL_GPUS);
767
768 /* activate any layer libraries */
769 if (!loader_layers_activated(icd, gpu_index)) {
Jon Ashburnf2610012014-10-24 15:48:55 -0600770 XGL_BASE_LAYER_OBJECT *gpuObj = (XGL_BASE_LAYER_OBJECT *) gpu;
771 XGL_BASE_LAYER_OBJECT *nextGpuObj, *baseObj = gpuObj->baseObject;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600772 xglGetProcAddrType nextGPA = xglGetProcAddr;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600773
Jon Ashburnb8358052014-11-18 09:06:04 -0700774 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600775 if (!count)
776 return 0;
Jon Ashburnb8358052014-11-18 09:06:04 -0700777 loader_init_layer_libs(icd, gpu_index, pLayerNames, count);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600778
Jon Ashburnd09bd102014-10-22 21:15:26 -0600779 icd->wrappedGpus[gpu_index] = malloc(sizeof(XGL_BASE_LAYER_OBJECT) * icd->layer_count[gpu_index]);
780 if (! icd->wrappedGpus[gpu_index])
781 loader_log(XGL_DBG_MSG_ERROR, 0, "Failed to malloc Gpu objects for layer");
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600782 for (int32_t i = icd->layer_count[gpu_index] - 1; i >= 0; i--) {
Jon Ashburnd09bd102014-10-22 21:15:26 -0600783 nextGpuObj = (icd->wrappedGpus[gpu_index] + i);
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600784 nextGpuObj->pGPA = nextGPA;
Jon Ashburnf2610012014-10-24 15:48:55 -0600785 nextGpuObj->baseObject = baseObj;
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600786 nextGpuObj->nextObject = gpuObj;
787 gpuObj = nextGpuObj;
788
Jon Ashburn79113cc2014-12-01 14:22:40 -0700789 char funcStr[256];
790 snprintf(funcStr, 256, "%sGetProcAddr",icd->layer_libs[gpu_index][i].name);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700791 if ((nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, funcStr)) == NULL)
792 nextGPA = (xglGetProcAddrType) loader_platform_get_proc_address(icd->layer_libs[gpu_index][i].lib_handle, "xglGetProcAddr");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600793 if (!nextGPA) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700794 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 -0600795 continue;
796 }
797
Jon Ashburnf2610012014-10-24 15:48:55 -0600798 if (i == 0) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600799 loader_init_dispatch_table(icd->loader_dispatch + gpu_index, nextGPA, gpuObj);
Jon Ashburnf2610012014-10-24 15:48:55 -0600800 //Insert the new wrapped objects into the list with loader object at head
801 ((XGL_BASE_LAYER_OBJECT *) gpu)->nextObject = gpuObj;
802 ((XGL_BASE_LAYER_OBJECT *) gpu)->pGPA = nextGPA;
803 gpuObj = icd->wrappedGpus[gpu_index] + icd->layer_count[gpu_index] - 1;
804 gpuObj->nextObject = baseObj;
Jon Ashburn46d1f582015-01-28 11:01:35 -0700805 gpuObj->pGPA = icd->scanned_icds->GetProcAddr;
Jon Ashburnf2610012014-10-24 15:48:55 -0600806 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600807
808 }
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600809 }
810 else {
811 //make sure requested Layers matches currently activated Layers
Jon Ashburnb8358052014-11-18 09:06:04 -0700812 count = loader_get_layer_libs(icd, gpu_index, pCreateInfo, &pLayerNames);
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600813 for (uint32_t i = 0; i < count; i++) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700814 if (strcmp(icd->layer_libs[gpu_index][i].name, pLayerNames[i].layer_name)) {
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600815 loader_log(XGL_DBG_MSG_ERROR, 0, "Layers activated != Layers requested");
816 break;
817 }
818 }
819 if (count != icd->layer_count[gpu_index]) {
Jon Ashburnb8358052014-11-18 09:06:04 -0700820 loader_log(XGL_DBG_MSG_ERROR, 0, "Number of Layers activated != number requested");
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600821 }
822 }
823 return icd->layer_count[gpu_index];
824}
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600825
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700826LOADER_EXPORT XGL_RESULT XGLAPI xglCreateInstance(
827 const XGL_APPLICATION_INFO* pAppInfo,
828 const XGL_ALLOC_CALLBACKS* pAllocCb,
829 XGL_INSTANCE* pInstance)
830{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700831 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_icd);
832 static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(once_layer);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700833 struct loader_instance *ptr_instance = NULL;
Jon Ashburn46888392015-01-29 15:45:51 -0700834 struct loader_scanned_icds *scanned_icds;
835 struct loader_icd *icd;
Ian Elliott55dbc5b2015-02-19 15:49:03 -0700836 XGL_RESULT res = XGL_ERROR_INITIALIZATION_FAILED;
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600837
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700838 /* Scan/discover all ICD libraries in a single-threaded manner */
839 loader_platform_thread_once(&once_icd, loader_icd_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700840
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700841 /* get layer libraries in a single-threaded manner */
842 loader_platform_thread_once(&once_layer, layer_lib_scan);
Jon Ashburn46888392015-01-29 15:45:51 -0700843
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700844 ptr_instance = (struct loader_instance*) malloc(sizeof(struct loader_instance));
845 if (ptr_instance == NULL) {
846 return XGL_ERROR_OUT_OF_MEMORY;
847 }
848 memset(ptr_instance, 0, sizeof(struct loader_instance));
849
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700850 ptr_instance->next = loader.instances;
851 loader.instances = ptr_instance;
852
Jon Ashburn46888392015-01-29 15:45:51 -0700853 scanned_icds = loader.scanned_icd_list;
854 while (scanned_icds) {
855 icd = loader_icd_add(ptr_instance, scanned_icds);
856 if (icd) {
857 res = scanned_icds->CreateInstance(pAppInfo, pAllocCb,
858 &(scanned_icds->instance));
859 if (res != XGL_SUCCESS)
860 {
861 ptr_instance->icds = ptr_instance->icds->next;
862 loader_icd_destroy(icd);
863 scanned_icds->instance = NULL;
864 loader_log(XGL_DBG_MSG_WARNING, 0,
865 "ICD ignored: failed to CreateInstance on device");
866 }
867 }
868 scanned_icds = scanned_icds->next;
869 }
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700870
Ian Elliotteb450762015-02-05 15:19:15 -0700871 if (ptr_instance->icds == NULL) {
872 return XGL_ERROR_INCOMPATIBLE_DRIVER;
873 }
Jon Ashburn46888392015-01-29 15:45:51 -0700874
875 *pInstance = (XGL_INSTANCE) ptr_instance;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700876 return XGL_SUCCESS;
877}
878
879LOADER_EXPORT XGL_RESULT XGLAPI xglDestroyInstance(
880 XGL_INSTANCE instance)
881{
882 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
Jon Ashburn46888392015-01-29 15:45:51 -0700883 struct loader_scanned_icds *scanned_icds;
884 XGL_RESULT res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700885
886 // Remove this instance from the list of instances:
887 struct loader_instance *prev = NULL;
888 struct loader_instance *next = loader.instances;
889 while (next != NULL) {
890 if (next == ptr_instance) {
891 // Remove this instance from the list:
892 if (prev)
893 prev->next = next->next;
Jon Ashburnc5c49602015-02-03 09:26:59 -0700894 else
895 loader.instances = next->next;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700896 break;
897 }
898 prev = next;
899 next = next->next;
900 }
901 if (next == NULL) {
902 // This must be an invalid instance handle or empty list
903 return XGL_ERROR_INVALID_HANDLE;
904 }
905
Jon Ashburn46888392015-01-29 15:45:51 -0700906 // cleanup any prior layer initializations
907 loader_deactivate_layer(ptr_instance);
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700908
Jon Ashburn46888392015-01-29 15:45:51 -0700909 scanned_icds = loader.scanned_icd_list;
910 while (scanned_icds) {
911 if (scanned_icds->instance)
912 res = scanned_icds->DestroyInstance(scanned_icds->instance);
913 if (res != XGL_SUCCESS)
914 loader_log(XGL_DBG_MSG_WARNING, 0,
915 "ICD ignored: failed to DestroyInstance on device");
916 scanned_icds->instance = NULL;
917 scanned_icds = scanned_icds->next;
918 }
919
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700920 free(ptr_instance);
921
922 return XGL_SUCCESS;
923}
924
925LOADER_EXPORT XGL_RESULT XGLAPI xglEnumerateGpus(
926
927 XGL_INSTANCE instance,
928 uint32_t maxGpus,
929 uint32_t* pGpuCount,
930 XGL_PHYSICAL_GPU* pGpus)
931{
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700932 struct loader_instance *ptr_instance = (struct loader_instance *) instance;
933 struct loader_icd *icd;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700934 uint32_t count = 0;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700935 XGL_RESULT res;
936
937 //in spirit of XGL don't error check on the instance parameter
938 icd = ptr_instance->icds;
939 while (icd) {
940 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
941 XGL_BASE_LAYER_OBJECT * wrapped_gpus;
942 xglGetProcAddrType get_proc_addr = icd->scanned_icds->GetProcAddr;
Jon Ashburn8d66a052015-01-29 15:47:01 -0700943 uint32_t n, max = maxGpus - count;
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700944
945 if (max > XGL_MAX_PHYSICAL_GPUS) {
946 max = XGL_MAX_PHYSICAL_GPUS;
947 }
948
Jon Ashburn46888392015-01-29 15:45:51 -0700949 res = icd->scanned_icds->EnumerateGpus(icd->scanned_icds->instance,
950 max, &n,
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700951 gpus);
952 if (res == XGL_SUCCESS && n) {
953 wrapped_gpus = (XGL_BASE_LAYER_OBJECT*) malloc(n *
954 sizeof(XGL_BASE_LAYER_OBJECT));
955 icd->gpus = wrapped_gpus;
956 icd->gpu_count = n;
957 icd->loader_dispatch = (XGL_LAYER_DISPATCH_TABLE *) malloc(n *
958 sizeof(XGL_LAYER_DISPATCH_TABLE));
Ian Elliott19628802015-02-04 12:06:46 -0700959 for (unsigned int i = 0; i < n; i++) {
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700960 (wrapped_gpus + i)->baseObject = gpus[i];
961 (wrapped_gpus + i)->pGPA = get_proc_addr;
962 (wrapped_gpus + i)->nextObject = gpus[i];
963 memcpy(pGpus + count, &wrapped_gpus, sizeof(*pGpus));
964 loader_init_dispatch_table(icd->loader_dispatch + i,
965 get_proc_addr, gpus[i]);
Courtney Goeltzenleuchter64ca9232015-02-10 18:40:14 -0700966
967 /* Verify ICD compatibility */
968 if (!valid_loader_magic_value(gpus[i])) {
969 loader_log(XGL_DBG_MSG_WARNING, 0,
970 "Loader: Incompatible ICD, first dword must be initialized to ICD_LOADER_MAGIC. See loader/README.md for details.\n");
971 assert(0);
972 }
973
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700974 const XGL_LAYER_DISPATCH_TABLE **disp;
975 disp = (const XGL_LAYER_DISPATCH_TABLE **) gpus[i];
976 *disp = icd->loader_dispatch + i;
977 }
978
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700979 count += n;
980
981 if (count >= maxGpus) {
982 break;
983 }
984 }
985
986 icd = icd->next;
987 }
988
Jon Ashburn4c392fb2015-01-28 19:57:09 -0700989 *pGpuCount = count;
990
991 return (count > 0) ? XGL_SUCCESS : res;
Jon Ashburn1beab2d2015-01-26 14:51:40 -0700992}
993
994LOADER_EXPORT void * XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char * pName)
995{
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700996 if (gpu == NULL) {
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600997 return NULL;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -0700998 }
Jon Ashburn891537f2014-10-22 12:42:13 -0600999 XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;
Jon Ashburnf2610012014-10-24 15:48:55 -06001000 XGL_LAYER_DISPATCH_TABLE * disp_table = * (XGL_LAYER_DISPATCH_TABLE **) gpuw->baseObject;
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001001 void *addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001002
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001003 if (disp_table == NULL)
1004 return NULL;
1005
Chia-I Wuf46b81a2015-01-04 11:12:47 +08001006 addr = loader_lookup_dispatch_table(disp_table, pName);
1007 if (addr)
1008 return addr;
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001009 else {
Jon Ashburnf2610012014-10-24 15:48:55 -06001010 if (disp_table->GetProcAddr == NULL)
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001011 return NULL;
Jon Ashburnf2610012014-10-24 15:48:55 -06001012 return disp_table->GetProcAddr(gpuw->nextObject, pName);
Jon Ashburnd38bfb12014-10-14 19:15:22 -06001013 }
1014}
1015
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001016LOADER_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 -06001017{
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001018 uint32_t gpu_index;
Ian Elliott19628802015-02-04 12:06:46 -07001019 size_t count = 0;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001020 char *lib_name;
1021 struct loader_icd *icd = loader_get_icd((const XGL_BASE_LAYER_OBJECT *) gpu, &gpu_index);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001022 loader_platform_dl_handle handle;
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -06001023 xglEnumerateLayersType fpEnumerateLayers;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001024 char layer_buf[16][256];
1025 char * layers[16];
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001026
Jon Ashburn1323eb72014-12-02 13:03:09 -07001027 if (pOutLayerCount == NULL || pOutLayers == NULL)
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001028 return XGL_ERROR_INVALID_POINTER;
1029
Jon Ashburn46d1f582015-01-28 11:01:35 -07001030 if (!icd)
1031 return XGL_ERROR_UNAVAILABLE;
1032
Jon Ashburn1323eb72014-12-02 13:03:09 -07001033 for (int i = 0; i < 16; i++)
1034 layers[i] = &layer_buf[i][0];
1035
1036 for (unsigned int j = 0; j < loader.scanned_layer_count && count < maxLayerCount; j++) {
1037 lib_name = loader.scanned_layer_names[j];
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001038 // Used to call: dlopen(*lib_name, RTLD_LAZY)
1039 if ((handle = loader_platform_open_library(lib_name)) == NULL)
Jon Ashburn1323eb72014-12-02 13:03:09 -07001040 continue;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001041 if ((fpEnumerateLayers = loader_platform_get_proc_address(handle, "xglEnumerateLayers")) == NULL) {
1042 //use default layer name based on library name XGL_LAYER_LIBRARY_PREFIX<name>.XGL_LIBRARY_SUFFIX
Jon Ashburn1323eb72014-12-02 13:03:09 -07001043 char *pEnd, *cpyStr;
Ian Elliott42045842015-02-13 14:29:21 -07001044 size_t siz;
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001045 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001046 lib_name = basename(lib_name);
1047 pEnd = strrchr(lib_name, '.');
Ian Elliott19628802015-02-04 12:06:46 -07001048 siz = (int) (pEnd - lib_name - strlen(XGL_LAYER_LIBRARY_PREFIX) + 1);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001049 if (pEnd == NULL || siz <= 0)
1050 continue;
1051 cpyStr = malloc(siz);
1052 if (cpyStr == NULL) {
1053 free(cpyStr);
1054 continue;
1055 }
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001056 strncpy(cpyStr, lib_name + strlen(XGL_LAYER_LIBRARY_PREFIX), siz);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001057 cpyStr[siz - 1] = '\0';
1058 if (siz > maxStringSize)
Ian Elliott19628802015-02-04 12:06:46 -07001059 siz = (int) maxStringSize;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001060 strncpy((char *) (pOutLayers[count]), cpyStr, siz);
1061 pOutLayers[count][siz - 1] = '\0';
1062 count++;
1063 free(cpyStr);
1064 }
1065 else {
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001066 size_t cnt;
1067 uint32_t n;
Jon Ashburn1323eb72014-12-02 13:03:09 -07001068 XGL_RESULT res;
Ian Elliott19628802015-02-04 12:06:46 -07001069 n = (uint32_t) ((maxStringSize < 256) ? maxStringSize : 256);
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001070 res = fpEnumerateLayers(NULL, 16, n, &cnt, layers, (char *) icd->gpus + gpu_index);
1071 loader_platform_close_library(handle);
Jon Ashburn1323eb72014-12-02 13:03:09 -07001072 if (res != XGL_SUCCESS)
1073 continue;
1074 if (cnt + count > maxLayerCount)
1075 cnt = maxLayerCount - count;
Ian Elliott19628802015-02-04 12:06:46 -07001076 for (uint32_t i = (uint32_t) count; i < cnt + count; i++) {
Jon Ashburn1323eb72014-12-02 13:03:09 -07001077 strncpy((char *) (pOutLayers[i]), (char *) layers[i - count], n);
1078 if (n > 0)
1079 pOutLayers[i - count][n - 1] = '\0';
1080 }
1081 count += cnt;
1082 }
1083 }
1084
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001085 *pOutLayerCount = count;
1086
Jon Ashburnf7bcf9b2014-10-15 15:30:23 -06001087 return XGL_SUCCESS;
1088}
1089
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001090LOADER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_INSTANCE instance, XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001091{
Jon Ashburn01e2d662014-11-14 09:52:42 -07001092 const struct loader_icd *icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001093 struct loader_instance *inst;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001094 XGL_RESULT res;
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001095 uint32_t gpu_idx;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001096
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001097 if (instance == XGL_NULL_HANDLE)
1098 return XGL_ERROR_INVALID_HANDLE;
1099
1100 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001101
Jon Ashburn98bd4542015-01-29 16:44:24 -07001102 for (inst = loader.instances; inst; inst = inst->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001103 if (inst == instance)
1104 break;
1105 }
1106
1107 if (inst == XGL_NULL_HANDLE)
1108 return XGL_ERROR_INVALID_HANDLE;
1109
1110 for (icd = inst->icds; icd; icd = icd->next) {
1111 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1112 res = (icd->loader_dispatch + i)->DbgRegisterMsgCallback(icd->scanned_icds->instance,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001113 pfnMsgCallback, pUserData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001114 if (res != XGL_SUCCESS) {
1115 gpu_idx = i;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001116 break;
Jon Ashburnbb510252015-03-17 13:47:55 -06001117 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001118 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001119 if (res != XGL_SUCCESS)
1120 break;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001121 }
1122
Jon Ashburnbb510252015-03-17 13:47:55 -06001123
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001124 /* roll back on errors */
1125 if (icd) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001126 for (const struct loader_icd *tmp = inst->icds; tmp != icd;
Jon Ashburn98bd4542015-01-29 16:44:24 -07001127 tmp = tmp->next) {
Jon Ashburnbb510252015-03-17 13:47:55 -06001128 for (uint32_t i = 0; i < icd->gpu_count; i++)
1129 (tmp->loader_dispatch + i)->DbgUnregisterMsgCallback(tmp->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001130 }
Jon Ashburn01e2d662014-11-14 09:52:42 -07001131 /* and gpus on current icd */
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001132 for (uint32_t i = 0; i < gpu_idx; i++)
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001133 (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001134
1135 return res;
1136 }
1137
1138 return XGL_SUCCESS;
1139}
1140
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001141LOADER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_INSTANCE instance, XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001142{
Jon Ashburn98bd4542015-01-29 16:44:24 -07001143 XGL_RESULT res = XGL_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001144 struct loader_instance *inst;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001145 if (instance == XGL_NULL_HANDLE)
1146 return XGL_ERROR_INVALID_HANDLE;
1147
1148 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001149
Jon Ashburnbb510252015-03-17 13:47:55 -06001150 for (inst = loader.instances; inst; inst = inst->next) {
1151 if (inst == instance)
1152 break;
1153 }
1154
1155 if (inst == XGL_NULL_HANDLE)
1156 return XGL_ERROR_INVALID_HANDLE;
1157
1158 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1159 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1160 XGL_RESULT r;
1161 r = (icd->loader_dispatch + i)->DbgUnregisterMsgCallback(icd->scanned_icds->instance, pfnMsgCallback);
1162 if (r != XGL_SUCCESS) {
1163 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001164 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001165 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001166 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001167 return res;
1168}
1169
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001170LOADER_EXPORT XGL_RESULT XGLAPI xglDbgSetGlobalOption(XGL_INSTANCE instance, XGL_DBG_GLOBAL_OPTION dbgOption, size_t dataSize, const void* pData)
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001171{
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001172 XGL_RESULT res = XGL_SUCCESS;
Jon Ashburnbb510252015-03-17 13:47:55 -06001173 struct loader_instance *inst;
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001174 if (instance == XGL_NULL_HANDLE)
1175 return XGL_ERROR_INVALID_HANDLE;
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001176
Courtney Goeltzenleuchterc80a5572015-04-13 14:10:06 -06001177 assert(loader.icds_scanned);
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001178
Jon Ashburnbb510252015-03-17 13:47:55 -06001179 for (inst = loader.instances; inst; inst = inst->next) {
1180 if (inst == instance)
1181 break;
1182 }
1183
1184 if (inst == XGL_NULL_HANDLE)
1185 return XGL_ERROR_INVALID_HANDLE;
1186 for (const struct loader_icd * icd = inst->icds; icd; icd = icd->next) {
1187 for (uint32_t i = 0; i < icd->gpu_count; i++) {
1188 XGL_RESULT r;
1189 r = (icd->loader_dispatch + i)->DbgSetGlobalOption(icd->scanned_icds->instance, dbgOption,
Jon Ashburn98bd4542015-01-29 16:44:24 -07001190 dataSize, pData);
Jon Ashburnbb510252015-03-17 13:47:55 -06001191 /* unfortunately we cannot roll back */
1192 if (r != XGL_SUCCESS) {
1193 res = r;
Jon Ashburn01e2d662014-11-14 09:52:42 -07001194 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001195 }
Chia-I Wu5f72d0f2014-08-01 11:21:23 +08001196 }
1197
1198 return res;
1199}