1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <unistd.h>
#include <dlfcn.h>
#include <GL/gl.h> /* dri_interface needs GL types */
#include <GL/internal/dri_interface.h>
#include "util/u_drm_screen.h"
#include "dri_internal.h"
#include "internal.h"
static __DRIimage *
dri_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
{
struct gbm_dri_device *dri = data;
return dri->lookup_image(screen, image, dri->lookup_user_data);
}
const __DRIuseInvalidateExtension use_invalidate = {
{ __DRI_USE_INVALIDATE, 1 }
};
const __DRIimageLookupExtension image_lookup_extension = {
{ __DRI_IMAGE_LOOKUP, 1 },
dri_lookup_egl_image
};
struct dri_extension_match {
const char *name;
int version;
int offset;
};
static struct dri_extension_match dri_core_extensions[] = {
{ __DRI_IMAGE, 1, offsetof(struct gbm_dri_device, image) },
{ NULL, 0, 0 }
};
static struct dri_extension_match gbm_dri_device_extensions[] = {
{ __DRI_CORE, 1, offsetof(struct gbm_dri_device, core) },
{ __DRI_DRI2, 1, offsetof(struct gbm_dri_device, dri2) },
{ NULL, 0, 0 }
};
static int
dri_bind_extensions(struct gbm_dri_device *dri,
struct dri_extension_match *matches,
const __DRIextension **extensions)
{
int i, j, ret = 0;
void *field;
for (i = 0; extensions[i]; i++) {
for (j = 0; matches[j].name; j++) {
if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
extensions[i]->version >= matches[j].version) {
field = ((char *) dri + matches[j].offset);
*(const __DRIextension **) field = extensions[i];
}
}
}
for (j = 0; matches[j].name; j++) {
field = ((char *) dri + matches[j].offset);
if (*(const __DRIextension **) field == NULL) {
ret = -1;
}
}
return ret;
}
static int
dri_load_driver(struct gbm_dri_device *dri)
{
const __DRIextension **extensions;
char path[PATH_MAX], *search_paths, *p, *next, *end;
search_paths = NULL;
if (geteuid() == getuid()) {
/* don't allow setuid apps to use GBM_DRIVERS_PATH */
search_paths = getenv("GBM_DRIVERS_PATH");
}
if (search_paths == NULL)
search_paths = DEFAULT_DRIVER_DIR;
dri->driver = NULL;
end = search_paths + strlen(search_paths);
for (p = search_paths; p < end && dri->driver == NULL; p = next + 1) {
int len;
next = strchr(p, ':');
if (next == NULL)
next = end;
len = next - p;
#if GLX_USE_TLS
snprintf(path, sizeof path,
"%.*s/tls/%s_dri.so", len, p, dri->base.driver_name);
dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
#endif
if (dri->driver == NULL) {
snprintf(path, sizeof path,
"%.*s/%s_dri.so", len, p, dri->base.driver_name);
dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
if (dri->driver == NULL)
fprintf(stderr, "failed to open %s: %s\n", path, dlerror());
}
}
if (dri->driver == NULL) {
fprintf(stderr, "gbm: failed to open any driver (search paths %s)",
search_paths);
return -1;
}
extensions = dlsym(dri->driver, __DRI_DRIVER_EXTENSIONS);
if (extensions == NULL) {
fprintf(stderr, "gbm: driver exports no extensions (%s)", dlerror());
dlclose(dri->driver);
return -1;
}
if (dri_bind_extensions(dri, gbm_dri_device_extensions, extensions) < 0) {
dlclose(dri->driver);
fprintf(stderr, "failed to bind extensions\n");
return -1;
}
return 0;
}
static int
dri_screen_create(struct gbm_dri_device *dri)
{
const __DRIextension **extensions;
int ret = 0;
dri->base.driver_name = util_drm_fd_get_screen_name(dri->base.base.fd);
printf("driver name. %s\n", dri->base.driver_name);
ret = dri_load_driver(dri);
if (ret) {
fprintf(stderr, "failed to load driver: %s\n", dri->base.driver_name);
return ret;
};
dri->extensions[0] = &image_lookup_extension.base;
dri->extensions[1] = &use_invalidate.base;
dri->extensions[2] = NULL;
if (dri->dri2 == NULL)
return -1;
dri->screen = dri->dri2->createNewScreen(0, dri->base.base.fd, dri->extensions,
&dri->driver_configs, dri);
extensions = dri->core->getExtensions(dri->screen);
if (dri_bind_extensions(dri, dri_core_extensions, extensions) < 0) {
ret = -1;
goto free_screen;
}
dri->lookup_image = NULL;
dri->lookup_user_data = NULL;
return 0;
free_screen:
dri->core->destroyScreen(dri->screen);
return ret;
}
static void
gbm_dri_bo_destroy(struct gbm_bo *_bo)
{
struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
dri->image->destroyImage(bo->image);
}
/* FIXME: need __DRIimage::dupImage */
static __DRIimage *
dri_reference_image(struct gbm_dri_device *dri, __DRIimage *orig,
uint32_t width, uint32_t height, uint32_t format,
void *user_data)
{
__DRIimage *image;
int32_t name, pitch;
dri->image->queryImage(orig, __DRI_IMAGE_ATTRIB_NAME, &name);
dri->image->queryImage(orig, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
image = dri->image->createImageFromName(dri->screen, width, height,
format, name, pitch / 4, user_data);
return image;
}
static struct gbm_bo *
gbm_dri_bo_create_from_egl_image(struct gbm_device *gbm,
void *egl_dpy, void *egl_img,
uint32_t width, uint32_t height,
enum gbm_bo_format format)
{
struct gbm_dri_device *dri = gbm_dri_device(gbm);
struct gbm_dri_bo *bo;
int dri_format;
bo = calloc(1, sizeof *bo);
if (bo == NULL)
return NULL;
bo->base.base.gbm = gbm;
bo->base.base.width = width;
bo->base.base.height = height;
switch (format) {
case GBM_BO_FORMAT_XRGB8888:
dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
break;
case GBM_BO_FORMAT_ARGB8888:
dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
break;
default:
return NULL;
}
if (dri->lookup_image == NULL)
return NULL;
__DRIimage *tmp = dri->lookup_image(dri->screen, egl_img, dri->lookup_user_data);
bo->image = dri_reference_image(dri, tmp, width, height, dri_format, bo);
if (bo->image == NULL)
return NULL;
dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
&bo->base.base.handle.s32);
dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
(int *) &bo->base.base.pitch);
return &bo->base.base;
}
static struct gbm_bo *
gbm_dri_bo_create(struct gbm_device *gbm,
uint32_t width, uint32_t height,
enum gbm_bo_format format, uint32_t usage)
{
struct gbm_dri_device *dri = gbm_dri_device(gbm);
struct gbm_dri_bo *bo;
int dri_format;
unsigned dri_use = 0;
bo = calloc(1, sizeof *bo);
if (bo == NULL)
return NULL;
bo->base.base.gbm = gbm;
bo->base.base.width = width;
bo->base.base.height = height;
switch (format) {
case GBM_BO_FORMAT_XRGB8888:
dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
break;
case GBM_BO_FORMAT_ARGB8888:
dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
break;
default:
return NULL;
}
if (usage & GBM_BO_USE_SCANOUT)
dri_use |= __DRI_IMAGE_USE_SCANOUT;
#if 0
if (usage & GBM_BO_USE_CURSOR_64X64)
dri_use = |= __DRI_IMAGE_USE_CURSOR_64X64;
#endif
bo->image =
dri->image->createImage(dri->screen,
width, height,
dri_format, dri_use,
bo);
if (bo->image == NULL)
return NULL;
dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
&bo->base.base.handle.s32);
dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
(int *) &bo->base.base.pitch);
return &bo->base.base;
}
static void
dri_destroy(struct gbm_device *gbm)
{
struct gbm_dri_device *dri = gbm_dri_device(gbm);
//int i;
dri->core->destroyScreen(dri->screen);
#if 0
for (i = 0; dri->driver_configs[i]; ++i)
free(dri->driver_configs[i]);
#endif
free(dri->driver_configs);
dlclose(dri->driver);
free(dri->base.driver_name);
free(dri);
}
static struct gbm_device *
dri_device_create(int fd)
{
struct gbm_dri_device *dri;
int ret;
dri = calloc(1, sizeof *dri);
dri->base.base.fd = fd;
dri->base.base.bo_create = gbm_dri_bo_create;
dri->base.base.bo_create_from_egl_image = gbm_dri_bo_create_from_egl_image;
dri->base.base.bo_destroy = gbm_dri_bo_destroy;
dri->base.base.destroy = dri_destroy;
dri->base.type = GBM_DRM_DRIVER_TYPE_DRI;
dri->base.base.name = "drm";
ret = dri_screen_create(dri);
if (ret) {
free(dri);
return NULL;
}
return &dri->base.base;
}
static int
dri_probe(int fd)
{
char *name;
int ret = 0;
name = util_drm_fd_get_screen_name(fd);
if (name == NULL) {
ret = -1;
} else {
free(name);
ret = 0;
}
return ret;
}
GBM_EXPORT struct gbm_backend gbm_backend = {
.class_name = "drm",
.backend_name = "dri",
.probe = dri_probe,
.create_device = dri_device_create,
};
|