e7cf9f86d996f8414da28aff2e3fd398a5ec547f
[mesa.git] / src / gallium / auxiliary / pipe-loader / pipe_loader.c
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/xmlconfig.h"
35 #include "util/xmlpool.h"
36
37 #include <string.h>
38
39 #ifdef _MSC_VER
40 #include <stdlib.h>
41 #define PATH_MAX _MAX_PATH
42 #endif
43
44 #define MODULE_PREFIX "pipe_"
45
46 static int (*backends[])(struct pipe_loader_device **, int) = {
47 #ifdef HAVE_LIBDRM
48 &pipe_loader_drm_probe,
49 #endif
50 &pipe_loader_sw_probe
51 };
52
53 const char gallium_driinfo_xml[] =
54 DRI_CONF_BEGIN
55 #include "driinfo_gallium.h"
56 DRI_CONF_END
57 ;
58
59 int
60 pipe_loader_probe(struct pipe_loader_device **devs, int ndev)
61 {
62 int i, n = 0;
63
64 for (i = 0; i < ARRAY_SIZE(backends); i++)
65 n += backends[i](&devs[n], MAX2(0, ndev - n));
66
67 return n;
68 }
69
70 void
71 pipe_loader_release(struct pipe_loader_device **devs, int ndev)
72 {
73 int i;
74
75 for (i = 0; i < ndev; i++)
76 devs[i]->ops->release(&devs[i]);
77 }
78
79 void
80 pipe_loader_base_release(struct pipe_loader_device **dev)
81 {
82 driDestroyOptionCache(&(*dev)->option_cache);
83 driDestroyOptionInfo(&(*dev)->option_info);
84
85 FREE(*dev);
86 *dev = NULL;
87 }
88
89 const struct drm_conf_ret *
90 pipe_loader_configuration(struct pipe_loader_device *dev,
91 enum drm_conf conf)
92 {
93 return dev->ops->configuration(dev, conf);
94 }
95
96 void
97 pipe_loader_load_options(struct pipe_loader_device *dev)
98 {
99 if (dev->option_info.info)
100 return;
101
102 const char *xml_options = gallium_driinfo_xml;
103 const struct drm_conf_ret *xml_options_conf =
104 pipe_loader_configuration(dev, DRM_CONF_XML_OPTIONS);
105
106 if (xml_options_conf)
107 xml_options = xml_options_conf->val.val_pointer;
108
109 driParseOptionInfo(&dev->option_info, xml_options);
110 driParseConfigFiles(&dev->option_cache, &dev->option_info, 0,
111 dev->driver_name);
112 }
113
114 char *
115 pipe_loader_get_driinfo_xml(const char *driver_name)
116 {
117 #ifdef HAVE_LIBDRM
118 char *xml = pipe_loader_drm_get_driinfo_xml(driver_name);
119 #else
120 char *xml = NULL;
121 #endif
122
123 if (!xml)
124 xml = strdup(gallium_driinfo_xml);
125
126 return xml;
127 }
128
129 struct pipe_screen *
130 pipe_loader_create_screen(struct pipe_loader_device *dev)
131 {
132 struct pipe_screen_config config;
133
134 pipe_loader_load_options(dev);
135 config.options = &dev->option_cache;
136
137 return dev->ops->create_screen(dev, &config);
138 }
139
140 struct util_dl_library *
141 pipe_loader_find_module(const char *driver_name,
142 const char *library_paths)
143 {
144 struct util_dl_library *lib;
145 const char *next;
146 char path[PATH_MAX];
147 int len, ret;
148
149 for (next = library_paths; *next; library_paths = next + 1) {
150 next = util_strchrnul(library_paths, ':');
151 len = next - library_paths;
152
153 if (len)
154 ret = util_snprintf(path, sizeof(path), "%.*s/%s%s%s",
155 len, library_paths,
156 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
157 else
158 ret = util_snprintf(path, sizeof(path), "%s%s%s",
159 MODULE_PREFIX, driver_name, UTIL_DL_EXT);
160
161 if (ret > 0 && ret < sizeof(path)) {
162 lib = util_dl_open(path);
163 if (lib) {
164 return lib;
165 }
166 }
167 }
168
169 return NULL;
170 }