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 void
pipe_loader_load_options(struct pipe_loader_device * dev)108 pipe_loader_load_options(struct pipe_loader_device *dev)
109 {
110 if (dev->option_info.info)
111 return;
112
113 unsigned driver_count, merged_count;
114 const driOptionDescription *driver_driconf =
115 dev->ops->get_driconf(dev, &driver_count);
116
117 const driOptionDescription *merged_driconf =
118 merge_driconf(driver_driconf, driver_count, &merged_count);
119
120 driParseOptionInfo(&dev->option_info, merged_driconf, merged_count);
121 driParseConfigFiles(&dev->option_cache, &dev->option_info, 0,
122 dev->driver_name, NULL, NULL, 0, NULL, 0);
123 free((void *)merged_driconf);
124 }
125
126 char *
pipe_loader_get_driinfo_xml(const char * driver_name)127 pipe_loader_get_driinfo_xml(const char *driver_name)
128 {
129 unsigned driver_count = 0;
130 const driOptionDescription *driver_driconf = NULL;
131
132 #ifdef HAVE_LIBDRM
133 driver_driconf = pipe_loader_drm_get_driconf_by_name(driver_name,
134 &driver_count);
135 #endif
136
137 unsigned merged_count;
138 const driOptionDescription *merged_driconf =
139 merge_driconf(driver_driconf, driver_count, &merged_count);
140 free((void *)driver_driconf);
141
142 char *xml = driGetOptionsXml(merged_driconf, merged_count);
143
144 free((void *)merged_driconf);
145
146 return xml;
147 }
148
149 struct pipe_screen *
pipe_loader_create_screen(struct pipe_loader_device * dev)150 pipe_loader_create_screen(struct pipe_loader_device *dev)
151 {
152 struct pipe_screen_config config;
153
154 pipe_loader_load_options(dev);
155 config.options = &dev->option_cache;
156
157 return dev->ops->create_screen(dev, &config);
158 }
159
160 struct util_dl_library *
pipe_loader_find_module(const char * driver_name,const char * library_paths)161 pipe_loader_find_module(const char *driver_name,
162 const char *library_paths)
163 {
164 struct util_dl_library *lib;
165 const char *next;
166 char path[PATH_MAX];
167 int len, ret;
168
169 for (next = library_paths; *next; library_paths = next + 1) {
170 next = strchrnul(library_paths, ':');
171 len = next - library_paths;
172
173 if (len)
174 ret = snprintf(path, sizeof(path), "%.*s/%s%s%s",
175 len, library_paths,
176 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
177 else
178 ret = snprintf(path, sizeof(path), "%s%s%s",
179 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
180
181 if (ret > 0 && ret < sizeof(path) && u_file_access(path, 0) != -1) {
182 lib = util_dl_open(path);
183 if (lib) {
184 return lib;
185 }
186 fprintf(stderr, "ERROR: Failed to load pipe driver at `%s': %s\n",
187 path, util_dl_error());
188 }
189 }
190
191 return NULL;
192 }
193