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)59 pipe_loader_probe(struct pipe_loader_device **devs, int ndev)
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 return n;
67 }
68
69 void
pipe_loader_release(struct pipe_loader_device ** devs,int ndev)70 pipe_loader_release(struct pipe_loader_device **devs, int ndev)
71 {
72 int i;
73
74 for (i = 0; i < ndev; i++)
75 devs[i]->ops->release(&devs[i]);
76 }
77
78 void
pipe_loader_base_release(struct pipe_loader_device ** dev)79 pipe_loader_base_release(struct pipe_loader_device **dev)
80 {
81 driDestroyOptionCache(&(*dev)->option_cache);
82 driDestroyOptionInfo(&(*dev)->option_info);
83
84 FREE(*dev);
85 *dev = NULL;
86 }
87
88 static driOptionDescription *
merge_driconf(const driOptionDescription * driver_driconf,unsigned driver_count,unsigned * merged_count)89 merge_driconf(const driOptionDescription *driver_driconf, unsigned driver_count,
90 unsigned *merged_count)
91 {
92 unsigned gallium_count = ARRAY_SIZE(gallium_driconf);
93 driOptionDescription *merged = malloc((driver_count + gallium_count) *
94 sizeof(*merged));
95 if (!merged) {
96 *merged_count = 0;
97 return NULL;
98 }
99
100 memcpy(merged, gallium_driconf, sizeof(*merged) * gallium_count);
101 memcpy(&merged[gallium_count], driver_driconf, sizeof(*merged) * driver_count);
102
103 *merged_count = driver_count + gallium_count;
104 return merged;
105 }
106
107 /**
108 * Ensure that dev->option_cache is initialized appropriately for the driver.
109 *
110 * This function can be called multiple times.
111 *
112 * \param dev Device for which options should be loaded.
113 */
114 static void
pipe_loader_load_options(struct pipe_loader_device * dev)115 pipe_loader_load_options(struct pipe_loader_device *dev)
116 {
117 if (dev->option_info.info)
118 return;
119
120 unsigned driver_count, merged_count;
121 const driOptionDescription *driver_driconf =
122 dev->ops->get_driconf(dev, &driver_count);
123
124 const driOptionDescription *merged_driconf =
125 merge_driconf(driver_driconf, driver_count, &merged_count);
126 driParseOptionInfo(&dev->option_info, merged_driconf, merged_count);
127 free((void *)merged_driconf);
128 }
129
130 void
pipe_loader_config_options(struct pipe_loader_device * dev)131 pipe_loader_config_options(struct pipe_loader_device *dev)
132 {
133 if (!dev->option_cache.info) {
134 driParseConfigFiles(&dev->option_cache, &dev->option_info, 0,
135 dev->driver_name, NULL, NULL, NULL, 0, NULL, 0);
136 }
137 }
138
139 char *
pipe_loader_get_driinfo_xml(const char * driver_name)140 pipe_loader_get_driinfo_xml(const char *driver_name)
141 {
142 unsigned driver_count = 0;
143 const driOptionDescription *driver_driconf = NULL;
144
145 #ifdef HAVE_LIBDRM
146 driver_driconf = pipe_loader_drm_get_driconf_by_name(driver_name,
147 &driver_count);
148 #endif
149
150 unsigned merged_count;
151 const driOptionDescription *merged_driconf =
152 merge_driconf(driver_driconf, driver_count, &merged_count);
153 free((void *)driver_driconf);
154
155 char *xml = driGetOptionsXml(merged_driconf, merged_count);
156
157 free((void *)merged_driconf);
158
159 return xml;
160 }
161
162 struct pipe_screen *
pipe_loader_create_screen_vk(struct pipe_loader_device * dev,bool sw_vk)163 pipe_loader_create_screen_vk(struct pipe_loader_device *dev, bool sw_vk)
164 {
165 struct pipe_screen_config config;
166
167 pipe_loader_load_options(dev);
168 config.options_info = &dev->option_info;
169 config.options = &dev->option_cache;
170
171 return dev->ops->create_screen(dev, &config, sw_vk);
172 }
173
174 struct pipe_screen *
pipe_loader_create_screen(struct pipe_loader_device * dev)175 pipe_loader_create_screen(struct pipe_loader_device *dev)
176 {
177 return pipe_loader_create_screen_vk(dev, false);
178 }
179
180 struct util_dl_library *
pipe_loader_find_module(const char * driver_name,const char * library_paths)181 pipe_loader_find_module(const char *driver_name,
182 const char *library_paths)
183 {
184 struct util_dl_library *lib;
185 const char *next;
186 char path[PATH_MAX];
187 int len, ret;
188
189 for (next = library_paths; *next; library_paths = next + 1) {
190 next = strchrnul(library_paths, ':');
191 len = next - library_paths;
192
193 if (len)
194 ret = snprintf(path, sizeof(path), "%.*s/%s%s%s",
195 len, library_paths,
196 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
197 else
198 ret = snprintf(path, sizeof(path), "%s%s%s",
199 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
200
201 if (ret > 0 && ret < sizeof(path) && u_file_access(path, 0) != -1) {
202 lib = util_dl_open(path);
203 if (lib) {
204 return lib;
205 }
206 fprintf(stderr, "ERROR: Failed to load pipe driver at `%s': %s\n",
207 path, util_dl_error());
208 }
209 }
210
211 return NULL;
212 }
213