Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/pipe-loader/pipe_loader.c
4561 views
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_cpu_detect.h"
31
#include "util/u_inlines.h"
32
#include "util/u_memory.h"
33
#include "util/u_string.h"
34
#include "util/u_dl.h"
35
#include "util/u_file.h"
36
#include "util/xmlconfig.h"
37
#include "util/driconf.h"
38
39
#include <string.h>
40
41
#ifdef _MSC_VER
42
#include <stdlib.h>
43
#define PATH_MAX _MAX_PATH
44
#endif
45
46
#define MODULE_PREFIX "pipe_"
47
48
static int (*backends[])(struct pipe_loader_device **, int) = {
49
#ifdef HAVE_LIBDRM
50
&pipe_loader_drm_probe,
51
#endif
52
&pipe_loader_sw_probe
53
};
54
55
const driOptionDescription gallium_driconf[] = {
56
#include "driinfo_gallium.h"
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
static driOptionDescription *
90
merge_driconf(const driOptionDescription *driver_driconf, unsigned driver_count,
91
unsigned *merged_count)
92
{
93
unsigned gallium_count = ARRAY_SIZE(gallium_driconf);
94
driOptionDescription *merged = malloc((driver_count + gallium_count) *
95
sizeof(*merged));
96
if (!merged) {
97
*merged_count = 0;
98
return NULL;
99
}
100
101
memcpy(merged, gallium_driconf, sizeof(*merged) * gallium_count);
102
memcpy(&merged[gallium_count], driver_driconf, sizeof(*merged) * driver_count);
103
104
*merged_count = driver_count + gallium_count;
105
return merged;
106
}
107
108
void
109
pipe_loader_load_options(struct pipe_loader_device *dev)
110
{
111
if (dev->option_info.info)
112
return;
113
114
unsigned driver_count, merged_count;
115
const driOptionDescription *driver_driconf =
116
dev->ops->get_driconf(dev, &driver_count);
117
118
const driOptionDescription *merged_driconf =
119
merge_driconf(driver_driconf, driver_count, &merged_count);
120
121
driParseOptionInfo(&dev->option_info, merged_driconf, merged_count);
122
driParseConfigFiles(&dev->option_cache, &dev->option_info, 0,
123
dev->driver_name, NULL, NULL, 0, NULL, 0);
124
free((void *)merged_driconf);
125
}
126
127
char *
128
pipe_loader_get_driinfo_xml(const char *driver_name)
129
{
130
unsigned driver_count = 0;
131
const driOptionDescription *driver_driconf = NULL;
132
133
#ifdef HAVE_LIBDRM
134
driver_driconf = pipe_loader_drm_get_driconf_by_name(driver_name,
135
&driver_count);
136
#endif
137
138
unsigned merged_count;
139
const driOptionDescription *merged_driconf =
140
merge_driconf(driver_driconf, driver_count, &merged_count);
141
free((void *)driver_driconf);
142
143
char *xml = driGetOptionsXml(merged_driconf, merged_count);
144
145
free((void *)merged_driconf);
146
147
return xml;
148
}
149
150
struct pipe_screen *
151
pipe_loader_create_screen_vk(struct pipe_loader_device *dev, bool sw_vk)
152
{
153
struct pipe_screen_config config;
154
155
util_cpu_detect();
156
pipe_loader_load_options(dev);
157
config.options = &dev->option_cache;
158
159
return dev->ops->create_screen(dev, &config, sw_vk);
160
}
161
162
struct pipe_screen *
163
pipe_loader_create_screen(struct pipe_loader_device *dev)
164
{
165
return pipe_loader_create_screen_vk(dev, false);
166
}
167
168
struct util_dl_library *
169
pipe_loader_find_module(const char *driver_name,
170
const char *library_paths)
171
{
172
struct util_dl_library *lib;
173
const char *next;
174
char path[PATH_MAX];
175
int len, ret;
176
177
for (next = library_paths; *next; library_paths = next + 1) {
178
next = strchrnul(library_paths, ':');
179
len = next - library_paths;
180
181
if (len)
182
ret = snprintf(path, sizeof(path), "%.*s/%s%s%s",
183
len, library_paths,
184
MODULE_PREFIX, driver_name, UTIL_DL_EXT);
185
else
186
ret = snprintf(path, sizeof(path), "%s%s%s",
187
MODULE_PREFIX, driver_name, UTIL_DL_EXT);
188
189
if (ret > 0 && ret < sizeof(path) && u_file_access(path, 0) != -1) {
190
lib = util_dl_open(path);
191
if (lib) {
192
return lib;
193
}
194
fprintf(stderr, "ERROR: Failed to load pipe driver at `%s': %s\n",
195
path, util_dl_error());
196
}
197
}
198
199
return NULL;
200
}
201
202