1 /**************************************************************************
2 *
3 * Copyright 2012 Francisco Jerez
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "pipe_loader_priv.h"
29
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_string.h"
33 #include "util/u_dl.h"
34 #include "util/u_file.h"
35 #include "util/xmlconfig.h"
36 #include "util/driconf.h"
37
38 #include <string.h>
39
40 #ifdef _MSC_VER
41 #include <stdlib.h>
42 #define PATH_MAX _MAX_PATH
43 #endif
44
45 #define MODULE_PREFIX "pipe_"
46
47 static int (*backends[])(struct pipe_loader_device **, int) = {
48 #ifdef HAVE_LIBDRM
49 &pipe_loader_drm_probe,
50 #endif
51 &pipe_loader_sw_probe
52 };
53
54 const driOptionDescription gallium_driconf[] = {
55 #include "driinfo_gallium.h"
56 };
57
58 int
pipe_loader_probe(struct pipe_loader_device ** devs,int ndev,bool with_zink)59 pipe_loader_probe(struct pipe_loader_device **devs, int ndev, bool with_zink)
60 {
61 int i, n = 0;
62
63 for (i = 0; i < ARRAY_SIZE(backends); i++)
64 n += backends[i](&devs[n], MAX2(0, ndev - n));
65
66 #if defined(HAVE_ZINK) && defined(HAVE_LIBDRM)
67 if (with_zink)
68 n += pipe_loader_drm_zink_probe(&devs[n], MAX2(0, ndev - n));
69 #endif
70
71 return n;
72 }
73
74 void
pipe_loader_release(struct pipe_loader_device ** devs,int ndev)75 pipe_loader_release(struct pipe_loader_device **devs, int ndev)
76 {
77 int i;
78
79 for (i = 0; i < ndev; i++)
80 devs[i]->ops->release(&devs[i]);
81 }
82
83 void
pipe_loader_base_release(struct pipe_loader_device ** dev)84 pipe_loader_base_release(struct pipe_loader_device **dev)
85 {
86 driDestroyOptionCache(&(*dev)->option_cache);
87 driDestroyOptionInfo(&(*dev)->option_info);
88
89 FREE(*dev);
90 *dev = NULL;
91 }
92
93 static driOptionDescription *
merge_driconf(const driOptionDescription * driver_driconf,unsigned driver_count,unsigned * merged_count)94 merge_driconf(const driOptionDescription *driver_driconf, unsigned driver_count,
95 unsigned *merged_count)
96 {
97 unsigned gallium_count = ARRAY_SIZE(gallium_driconf);
98 driOptionDescription *merged = malloc((driver_count + gallium_count) *
99 sizeof(*merged));
100 if (!merged) {
101 *merged_count = 0;
102 return NULL;
103 }
104
105 if (gallium_count)
106 memcpy(merged, gallium_driconf, sizeof(*merged) * gallium_count);
107 if (driver_count) {
108 memcpy(&merged[gallium_count], driver_driconf,
109 sizeof(*merged) * driver_count);
110 }
111
112 *merged_count = driver_count + gallium_count;
113 return merged;
114 }
115
116 /**
117 * Ensure that dev->option_cache is initialized appropriately for the driver.
118 *
119 * This function can be called multiple times.
120 *
121 * \param dev Device for which options should be loaded.
122 */
123 static void
pipe_loader_load_options(struct pipe_loader_device * dev)124 pipe_loader_load_options(struct pipe_loader_device *dev)
125 {
126 if (dev->option_info.info)
127 return;
128
129 unsigned driver_count, merged_count;
130 const driOptionDescription *driver_driconf =
131 dev->ops->get_driconf(dev, &driver_count);
132
133 const driOptionDescription *merged_driconf =
134 merge_driconf(driver_driconf, driver_count, &merged_count);
135 driParseOptionInfo(&dev->option_info, merged_driconf, merged_count);
136 free((void *)merged_driconf);
137 }
138
139 void
pipe_loader_config_options(struct pipe_loader_device * dev)140 pipe_loader_config_options(struct pipe_loader_device *dev)
141 {
142 if (!dev->option_cache.info) {
143 driParseConfigFiles(&dev->option_cache, &dev->option_info, 0,
144 dev->driver_name, NULL, NULL, NULL, 0, NULL, 0);
145 }
146 }
147
148 char *
pipe_loader_get_driinfo_xml(const char * driver_name)149 pipe_loader_get_driinfo_xml(const char *driver_name)
150 {
151 unsigned driver_count = 0;
152 const driOptionDescription *driver_driconf = NULL;
153
154 #ifdef HAVE_LIBDRM
155 driver_driconf = pipe_loader_drm_get_driconf_by_name(driver_name,
156 &driver_count);
157 #endif
158
159 unsigned merged_count;
160 const driOptionDescription *merged_driconf =
161 merge_driconf(driver_driconf, driver_count, &merged_count);
162
163 char *xml = driGetOptionsXml(merged_driconf, merged_count);
164
165 free((void *)driver_driconf);
166 free((void *)merged_driconf);
167
168 return xml;
169 }
170
171 struct pipe_screen *
pipe_loader_create_screen_vk(struct pipe_loader_device * dev,bool sw_vk,bool driver_name_is_inferred)172 pipe_loader_create_screen_vk(struct pipe_loader_device *dev, bool sw_vk, bool driver_name_is_inferred)
173 {
174 struct pipe_screen_config config;
175
176 pipe_loader_load_options(dev);
177 config.driver_name_is_inferred = driver_name_is_inferred;
178 config.options_info = &dev->option_info;
179 config.options = &dev->option_cache;
180
181 return dev->ops->create_screen(dev, &config, sw_vk);
182 }
183
184 struct pipe_screen *
pipe_loader_create_screen(struct pipe_loader_device * dev,bool driver_name_is_inferred)185 pipe_loader_create_screen(struct pipe_loader_device *dev, bool driver_name_is_inferred)
186 {
187 return pipe_loader_create_screen_vk(dev, false, driver_name_is_inferred);
188 }
189
190 struct util_dl_library *
pipe_loader_find_module(const char * driver_name,const char * library_paths)191 pipe_loader_find_module(const char *driver_name,
192 const char *library_paths)
193 {
194 struct util_dl_library *lib;
195 const char *next;
196 char path[PATH_MAX];
197 int len, ret;
198
199 for (next = library_paths; *next; library_paths = next + 1) {
200 next = strchrnul(library_paths, ':');
201 len = next - library_paths;
202
203 if (len)
204 ret = snprintf(path, sizeof(path), "%.*s/%s%s%s",
205 len, library_paths,
206 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
207 else
208 ret = snprintf(path, sizeof(path), "%s%s%s",
209 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
210
211 if (ret > 0 && ret < sizeof(path) && u_file_access(path, 0) != -1) {
212 lib = util_dl_open(path);
213 if (lib) {
214 return lib;
215 }
216 fprintf(stderr, "ERROR: Failed to load pipe driver at `%s': %s\n",
217 path, util_dl_error());
218 }
219 }
220
221 return NULL;
222 }
223