1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Benjamin Franzke <benjaminfranzke@googlemail.com>
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <assert.h>
37
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <dlfcn.h>
41 #include <xf86drm.h>
42 #include "drm-uapi/drm_fourcc.h"
43
44 #include <GL/gl.h> /* dri_interface needs GL types */
45 #include <GL/internal/dri_interface.h>
46
47 #include "gbm_driint.h"
48
49 #include "gbmint.h"
50 #include "loader.h"
51 #include "util/debug.h"
52 #include "util/macros.h"
53
54 /* For importing wl_buffer */
55 #if HAVE_WAYLAND_PLATFORM
56 #include "wayland-drm.h"
57 #endif
58
59 static __DRIimage *
dri_lookup_egl_image(__DRIscreen * screen,void * image,void * data)60 dri_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
61 {
62 struct gbm_dri_device *dri = data;
63
64 if (dri->lookup_image == NULL)
65 return NULL;
66
67 return dri->lookup_image(screen, image, dri->lookup_user_data);
68 }
69
70 static __DRIbuffer *
dri_get_buffers(__DRIdrawable * driDrawable,int * width,int * height,unsigned int * attachments,int count,int * out_count,void * data)71 dri_get_buffers(__DRIdrawable * driDrawable,
72 int *width, int *height,
73 unsigned int *attachments, int count,
74 int *out_count, void *data)
75 {
76 struct gbm_dri_surface *surf = data;
77 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
78
79 if (dri->get_buffers == NULL)
80 return NULL;
81
82 return dri->get_buffers(driDrawable, width, height, attachments,
83 count, out_count, surf->dri_private);
84 }
85
86 static void
dri_flush_front_buffer(__DRIdrawable * driDrawable,void * data)87 dri_flush_front_buffer(__DRIdrawable * driDrawable, void *data)
88 {
89 struct gbm_dri_surface *surf = data;
90 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
91
92 if (dri->flush_front_buffer != NULL)
93 dri->flush_front_buffer(driDrawable, surf->dri_private);
94 }
95
96 static __DRIbuffer *
dri_get_buffers_with_format(__DRIdrawable * driDrawable,int * width,int * height,unsigned int * attachments,int count,int * out_count,void * data)97 dri_get_buffers_with_format(__DRIdrawable * driDrawable,
98 int *width, int *height,
99 unsigned int *attachments, int count,
100 int *out_count, void *data)
101 {
102 struct gbm_dri_surface *surf = data;
103 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
104
105 if (dri->get_buffers_with_format == NULL)
106 return NULL;
107
108 return
109 dri->get_buffers_with_format(driDrawable, width, height, attachments,
110 count, out_count, surf->dri_private);
111 }
112
113 static unsigned
dri_get_capability(void * loaderPrivate,enum dri_loader_cap cap)114 dri_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
115 {
116 /* Note: loaderPrivate is _EGLDisplay* */
117 switch (cap) {
118 case DRI_LOADER_CAP_FP16:
119 return 1;
120 default:
121 return 0;
122 }
123 }
124
125 static int
image_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)126 image_get_buffers(__DRIdrawable *driDrawable,
127 unsigned int format,
128 uint32_t *stamp,
129 void *loaderPrivate,
130 uint32_t buffer_mask,
131 struct __DRIimageList *buffers)
132 {
133 struct gbm_dri_surface *surf = loaderPrivate;
134 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
135
136 if (dri->image_get_buffers == NULL)
137 return 0;
138
139 return dri->image_get_buffers(driDrawable, format, stamp,
140 surf->dri_private, buffer_mask, buffers);
141 }
142
143 static void
swrast_get_drawable_info(__DRIdrawable * driDrawable,int * x,int * y,int * width,int * height,void * loaderPrivate)144 swrast_get_drawable_info(__DRIdrawable *driDrawable,
145 int *x,
146 int *y,
147 int *width,
148 int *height,
149 void *loaderPrivate)
150 {
151 struct gbm_dri_surface *surf = loaderPrivate;
152
153 *x = 0;
154 *y = 0;
155 *width = surf->base.width;
156 *height = surf->base.height;
157 }
158
159 static void
swrast_put_image2(__DRIdrawable * driDrawable,int op,int x,int y,int width,int height,int stride,char * data,void * loaderPrivate)160 swrast_put_image2(__DRIdrawable *driDrawable,
161 int op,
162 int x,
163 int y,
164 int width,
165 int height,
166 int stride,
167 char *data,
168 void *loaderPrivate)
169 {
170 struct gbm_dri_surface *surf = loaderPrivate;
171 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
172
173 dri->swrast_put_image2(driDrawable,
174 op, x, y,
175 width, height, stride,
176 data, surf->dri_private);
177 }
178
179 static void
swrast_put_image(__DRIdrawable * driDrawable,int op,int x,int y,int width,int height,char * data,void * loaderPrivate)180 swrast_put_image(__DRIdrawable *driDrawable,
181 int op,
182 int x,
183 int y,
184 int width,
185 int height,
186 char *data,
187 void *loaderPrivate)
188 {
189 swrast_put_image2(driDrawable, op, x, y, width, height,
190 width * 4, data, loaderPrivate);
191 }
192
193 static void
swrast_get_image(__DRIdrawable * driDrawable,int x,int y,int width,int height,char * data,void * loaderPrivate)194 swrast_get_image(__DRIdrawable *driDrawable,
195 int x,
196 int y,
197 int width,
198 int height,
199 char *data,
200 void *loaderPrivate)
201 {
202 struct gbm_dri_surface *surf = loaderPrivate;
203 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
204
205 dri->swrast_get_image(driDrawable,
206 x, y,
207 width, height,
208 data, surf->dri_private);
209 }
210
211 static const __DRIuseInvalidateExtension use_invalidate = {
212 .base = { __DRI_USE_INVALIDATE, 1 }
213 };
214
215 static const __DRIimageLookupExtension image_lookup_extension = {
216 .base = { __DRI_IMAGE_LOOKUP, 1 },
217
218 .lookupEGLImage = dri_lookup_egl_image
219 };
220
221 static const __DRIdri2LoaderExtension dri2_loader_extension = {
222 .base = { __DRI_DRI2_LOADER, 4 },
223
224 .getBuffers = dri_get_buffers,
225 .flushFrontBuffer = dri_flush_front_buffer,
226 .getBuffersWithFormat = dri_get_buffers_with_format,
227 .getCapability = dri_get_capability,
228 };
229
230 static const __DRIimageLoaderExtension image_loader_extension = {
231 .base = { __DRI_IMAGE_LOADER, 2 },
232
233 .getBuffers = image_get_buffers,
234 .flushFrontBuffer = dri_flush_front_buffer,
235 .getCapability = dri_get_capability,
236 };
237
238 static const __DRIswrastLoaderExtension swrast_loader_extension = {
239 .base = { __DRI_SWRAST_LOADER, 2 },
240
241 .getDrawableInfo = swrast_get_drawable_info,
242 .putImage = swrast_put_image,
243 .getImage = swrast_get_image,
244 .putImage2 = swrast_put_image2
245 };
246
247 static const __DRIextension *gbm_dri_screen_extensions[] = {
248 &image_lookup_extension.base,
249 &use_invalidate.base,
250 &dri2_loader_extension.base,
251 &image_loader_extension.base,
252 &swrast_loader_extension.base,
253 NULL,
254 };
255
256 struct dri_extension_match {
257 const char *name;
258 int version;
259 int offset;
260 bool optional;
261 };
262
263 static struct dri_extension_match dri_core_extensions[] = {
264 { __DRI2_FLUSH, 1, offsetof(struct gbm_dri_device, flush), false },
265 { __DRI_IMAGE, 1, offsetof(struct gbm_dri_device, image), false },
266 { __DRI2_FENCE, 1, offsetof(struct gbm_dri_device, fence), true },
267 };
268
269 static struct dri_extension_match gbm_dri_device_extensions[] = {
270 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core), false },
271 { __DRI_DRI2, 1, offsetof(struct gbm_dri_device, dri2), false },
272 };
273
274 static struct dri_extension_match gbm_swrast_device_extensions[] = {
275 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core), false },
276 { __DRI_SWRAST, 1, offsetof(struct gbm_dri_device, swrast), false },
277 };
278
279 static bool
dri_bind_extensions(struct gbm_dri_device * dri,struct dri_extension_match * matches,size_t num_matches,const __DRIextension ** extensions)280 dri_bind_extensions(struct gbm_dri_device *dri,
281 struct dri_extension_match *matches, size_t num_matches,
282 const __DRIextension **extensions)
283 {
284 bool ret = true;
285 void *field;
286
287 for (size_t i = 0; extensions[i]; i++) {
288 for (size_t j = 0; j < num_matches; j++) {
289 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
290 extensions[i]->version >= matches[j].version) {
291 field = ((char *) dri + matches[j].offset);
292 *(const __DRIextension **) field = extensions[i];
293 }
294 }
295 }
296
297 for (size_t j = 0; j < num_matches; j++) {
298 field = ((char *) dri + matches[j].offset);
299 if ((*(const __DRIextension **) field == NULL) && !matches[j].optional) {
300 ret = false;
301 }
302 }
303
304 return ret;
305 }
306
307 static const __DRIextension **
dri_open_driver(struct gbm_dri_device * dri)308 dri_open_driver(struct gbm_dri_device *dri)
309 {
310 /* Temporarily work around dri driver libs that need symbols in libglapi
311 * but don't automatically link it in.
312 */
313 /* XXX: Library name differs on per platforms basis. Update this as
314 * osx/cygwin/windows/bsd gets support for GBM..
315 */
316 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
317
318 static const char *search_path_vars[] = {
319 /* Read GBM_DRIVERS_PATH first for compatibility, but LIBGL_DRIVERS_PATH
320 * is recommended over GBM_DRIVERS_PATH.
321 */
322 "GBM_DRIVERS_PATH",
323 /* Read LIBGL_DRIVERS_PATH if GBM_DRIVERS_PATH was not set.
324 * LIBGL_DRIVERS_PATH is recommended over GBM_DRIVERS_PATH.
325 */
326 "LIBGL_DRIVERS_PATH",
327 NULL
328 };
329 return loader_open_driver(dri->driver_name, &dri->driver, search_path_vars);
330 }
331
332 static int
dri_load_driver(struct gbm_dri_device * dri)333 dri_load_driver(struct gbm_dri_device *dri)
334 {
335 const __DRIextension **extensions;
336
337 extensions = dri_open_driver(dri);
338 if (!extensions)
339 return -1;
340
341 if (!dri_bind_extensions(dri, gbm_dri_device_extensions,
342 ARRAY_SIZE(gbm_dri_device_extensions),
343 extensions)) {
344 dlclose(dri->driver);
345 fprintf(stderr, "failed to bind extensions\n");
346 return -1;
347 }
348
349 dri->driver_extensions = extensions;
350
351 return 0;
352 }
353
354 static int
dri_load_driver_swrast(struct gbm_dri_device * dri)355 dri_load_driver_swrast(struct gbm_dri_device *dri)
356 {
357 const __DRIextension **extensions;
358
359 extensions = dri_open_driver(dri);
360 if (!extensions)
361 return -1;
362
363 if (!dri_bind_extensions(dri, gbm_swrast_device_extensions,
364 ARRAY_SIZE(gbm_swrast_device_extensions),
365 extensions)) {
366 dlclose(dri->driver);
367 fprintf(stderr, "failed to bind extensions\n");
368 return -1;
369 }
370
371 dri->driver_extensions = extensions;
372
373 return 0;
374 }
375
376 static int
dri_screen_create_dri2(struct gbm_dri_device * dri,char * driver_name)377 dri_screen_create_dri2(struct gbm_dri_device *dri, char *driver_name)
378 {
379 const __DRIextension **extensions;
380 int ret = 0;
381
382 dri->driver_name = driver_name;
383 if (dri->driver_name == NULL)
384 return -1;
385
386 ret = dri_load_driver(dri);
387 if (ret) {
388 fprintf(stderr, "failed to load driver: %s\n", dri->driver_name);
389 return ret;
390 }
391
392 dri->loader_extensions = gbm_dri_screen_extensions;
393
394 if (dri->dri2 == NULL)
395 return -1;
396
397 if (dri->dri2->base.version >= 4) {
398 dri->screen = dri->dri2->createNewScreen2(0, dri->base.fd,
399 dri->loader_extensions,
400 dri->driver_extensions,
401 &dri->driver_configs, dri);
402 } else {
403 dri->screen = dri->dri2->createNewScreen(0, dri->base.fd,
404 dri->loader_extensions,
405 &dri->driver_configs, dri);
406 }
407 if (dri->screen == NULL)
408 return -1;
409
410 extensions = dri->core->getExtensions(dri->screen);
411 if (!dri_bind_extensions(dri, dri_core_extensions,
412 ARRAY_SIZE(dri_core_extensions),
413 extensions)) {
414 ret = -1;
415 goto free_screen;
416 }
417
418 dri->lookup_image = NULL;
419 dri->lookup_user_data = NULL;
420
421 return 0;
422
423 free_screen:
424 dri->core->destroyScreen(dri->screen);
425
426 return ret;
427 }
428
429 static int
dri_screen_create_swrast(struct gbm_dri_device * dri)430 dri_screen_create_swrast(struct gbm_dri_device *dri)
431 {
432 int ret;
433
434 dri->driver_name = strdup("swrast");
435 if (dri->driver_name == NULL)
436 return -1;
437
438 ret = dri_load_driver_swrast(dri);
439 if (ret) {
440 fprintf(stderr, "failed to load swrast driver\n");
441 return ret;
442 }
443
444 dri->loader_extensions = gbm_dri_screen_extensions;
445
446 if (dri->swrast == NULL)
447 return -1;
448
449 if (dri->swrast->base.version >= 4) {
450 dri->screen = dri->swrast->createNewScreen2(0, dri->loader_extensions,
451 dri->driver_extensions,
452 &dri->driver_configs, dri);
453 } else {
454 dri->screen = dri->swrast->createNewScreen(0, dri->loader_extensions,
455 &dri->driver_configs, dri);
456 }
457 if (dri->screen == NULL)
458 return -1;
459
460 dri->lookup_image = NULL;
461 dri->lookup_user_data = NULL;
462
463 return 0;
464 }
465
466 static int
dri_screen_create(struct gbm_dri_device * dri)467 dri_screen_create(struct gbm_dri_device *dri)
468 {
469 char *driver_name;
470
471 driver_name = loader_get_driver_for_fd(dri->base.fd);
472 if (!driver_name)
473 return -1;
474
475 return dri_screen_create_dri2(dri, driver_name);
476 }
477
478 static int
dri_screen_create_sw(struct gbm_dri_device * dri)479 dri_screen_create_sw(struct gbm_dri_device *dri)
480 {
481 char *driver_name;
482 int ret;
483
484 driver_name = strdup("kms_swrast");
485 if (!driver_name)
486 return -errno;
487
488 ret = dri_screen_create_dri2(dri, driver_name);
489 if (ret == 0)
490 return ret;
491
492 return dri_screen_create_swrast(dri);
493 }
494
495 static const struct gbm_dri_visual gbm_dri_visuals_table[] = {
496 {
497 GBM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8,
498 { 0, -1, -1, -1 },
499 { 8, 0, 0, 0 },
500 },
501 {
502 GBM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88,
503 { 0, 8, -1, -1 },
504 { 8, 8, 0, 0 },
505 },
506 {
507 GBM_FORMAT_ARGB1555, __DRI_IMAGE_FORMAT_ARGB1555,
508 { 10, 5, 0, 11 },
509 { 5, 5, 5, 1 },
510 },
511 {
512 GBM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565,
513 { 11, 5, 0, -1 },
514 { 5, 6, 5, 0 },
515 },
516 {
517 GBM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888,
518 { 16, 8, 0, -1 },
519 { 8, 8, 8, 0 },
520 },
521 {
522 GBM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888,
523 { 16, 8, 0, 24 },
524 { 8, 8, 8, 8 },
525 },
526 {
527 GBM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888,
528 { 0, 8, 16, -1 },
529 { 8, 8, 8, 0 },
530 },
531 {
532 GBM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888,
533 { 0, 8, 16, 24 },
534 { 8, 8, 8, 8 },
535 },
536 {
537 GBM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010,
538 { 20, 10, 0, -1 },
539 { 10, 10, 10, 0 },
540 },
541 {
542 GBM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010,
543 { 20, 10, 0, 30 },
544 { 10, 10, 10, 2 },
545 },
546 {
547 GBM_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XBGR2101010,
548 { 0, 10, 20, -1 },
549 { 10, 10, 10, 0 },
550 },
551 {
552 GBM_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ABGR2101010,
553 { 0, 10, 20, 30 },
554 { 10, 10, 10, 2 },
555 },
556 {
557 GBM_FORMAT_XBGR16161616F, __DRI_IMAGE_FORMAT_XBGR16161616F,
558 { 0, 16, 32, -1 },
559 { 16, 16, 16, 0 },
560 true,
561 },
562 {
563 GBM_FORMAT_ABGR16161616F, __DRI_IMAGE_FORMAT_ABGR16161616F,
564 { 0, 16, 32, 48 },
565 { 16, 16, 16, 16 },
566 true,
567 },
568 };
569
570 static int
gbm_format_to_dri_format(uint32_t gbm_format)571 gbm_format_to_dri_format(uint32_t gbm_format)
572 {
573 gbm_format = gbm_format_canonicalize(gbm_format);
574 for (size_t i = 0; i < ARRAY_SIZE(gbm_dri_visuals_table); i++) {
575 if (gbm_dri_visuals_table[i].gbm_format == gbm_format)
576 return gbm_dri_visuals_table[i].dri_image_format;
577 }
578
579 return 0;
580 }
581
582 static uint32_t
gbm_dri_to_gbm_format(int dri_format)583 gbm_dri_to_gbm_format(int dri_format)
584 {
585 for (size_t i = 0; i < ARRAY_SIZE(gbm_dri_visuals_table); i++) {
586 if (gbm_dri_visuals_table[i].dri_image_format == dri_format)
587 return gbm_dri_visuals_table[i].gbm_format;
588 }
589
590 return 0;
591 }
592
593 static int
gbm_dri_is_format_supported(struct gbm_device * gbm,uint32_t format,uint32_t usage)594 gbm_dri_is_format_supported(struct gbm_device *gbm,
595 uint32_t format,
596 uint32_t usage)
597 {
598 struct gbm_dri_device *dri = gbm_dri_device(gbm);
599 int count;
600
601 if ((usage & GBM_BO_USE_CURSOR) && (usage & GBM_BO_USE_RENDERING))
602 return 0;
603
604 format = gbm_format_canonicalize(format);
605 if (gbm_format_to_dri_format(format) == 0)
606 return 0;
607
608 /* If there is no query, fall back to the small table which was originally
609 * here. */
610 if (dri->image->base.version <= 15 || !dri->image->queryDmaBufModifiers) {
611 switch (format) {
612 case GBM_FORMAT_XRGB8888:
613 case GBM_FORMAT_ARGB8888:
614 case GBM_FORMAT_XBGR8888:
615 return 1;
616 default:
617 return 0;
618 }
619 }
620
621 /* This returns false if the format isn't supported */
622 if (!dri->image->queryDmaBufModifiers(dri->screen, format, 0, NULL, NULL,
623 &count))
624 return 0;
625
626 return 1;
627 }
628
629 static int
gbm_dri_get_format_modifier_plane_count(struct gbm_device * gbm,uint32_t format,uint64_t modifier)630 gbm_dri_get_format_modifier_plane_count(struct gbm_device *gbm,
631 uint32_t format,
632 uint64_t modifier)
633 {
634 struct gbm_dri_device *dri = gbm_dri_device(gbm);
635 uint64_t plane_count;
636
637 if (dri->image->base.version < 16 ||
638 !dri->image->queryDmaBufFormatModifierAttribs)
639 return -1;
640
641 format = gbm_format_canonicalize(format);
642 if (gbm_format_to_dri_format(format) == 0)
643 return -1;
644
645 if (!dri->image->queryDmaBufFormatModifierAttribs(
646 dri->screen, format, modifier,
647 __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT, &plane_count))
648 return -1;
649
650 return plane_count;
651 }
652
653 static int
gbm_dri_bo_write(struct gbm_bo * _bo,const void * buf,size_t count)654 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
655 {
656 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
657
658 if (bo->image != NULL) {
659 errno = EINVAL;
660 return -1;
661 }
662
663 memcpy(bo->map, buf, count);
664
665 return 0;
666 }
667
668 static int
gbm_dri_bo_get_fd(struct gbm_bo * _bo)669 gbm_dri_bo_get_fd(struct gbm_bo *_bo)
670 {
671 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
672 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
673 int fd;
674
675 if (bo->image == NULL)
676 return -1;
677
678 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FD, &fd))
679 return -1;
680
681 return fd;
682 }
683
684 static int
get_number_planes(struct gbm_dri_device * dri,__DRIimage * image)685 get_number_planes(struct gbm_dri_device *dri, __DRIimage *image)
686 {
687 int num_planes = 0;
688
689 /* Dumb buffers are single-plane only. */
690 if (!image)
691 return 1;
692
693 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
694
695 if (num_planes <= 0)
696 num_planes = 1;
697
698 return num_planes;
699 }
700
701 static int
gbm_dri_bo_get_planes(struct gbm_bo * _bo)702 gbm_dri_bo_get_planes(struct gbm_bo *_bo)
703 {
704 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
705 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
706
707 return get_number_planes(dri, bo->image);
708 }
709
710 static union gbm_bo_handle
gbm_dri_bo_get_handle_for_plane(struct gbm_bo * _bo,int plane)711 gbm_dri_bo_get_handle_for_plane(struct gbm_bo *_bo, int plane)
712 {
713 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
714 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
715 union gbm_bo_handle ret;
716 ret.s32 = -1;
717
718 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar) {
719 /* Preserve legacy behavior if plane is 0 */
720 if (plane == 0) {
721 /* NOTE: return _bo->handle, *NOT* bo->handle which is invalid at this point */
722 return _bo->handle;
723 }
724
725 errno = ENOSYS;
726 return ret;
727 }
728
729 if (plane >= get_number_planes(dri, bo->image)) {
730 errno = EINVAL;
731 return ret;
732 }
733
734 /* dumb BOs can only utilize non-planar formats */
735 if (!bo->image) {
736 assert(plane == 0);
737 ret.s32 = bo->handle;
738 return ret;
739 }
740
741 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
742 if (image) {
743 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
744 dri->image->destroyImage(image);
745 } else {
746 assert(plane == 0);
747 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
748 }
749
750 return ret;
751 }
752
753 static uint32_t
gbm_dri_bo_get_stride(struct gbm_bo * _bo,int plane)754 gbm_dri_bo_get_stride(struct gbm_bo *_bo, int plane)
755 {
756 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
757 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
758 __DRIimage *image;
759 int stride = 0;
760
761 if (!dri->image || dri->image->base.version < 11 || !dri->image->fromPlanar) {
762 /* Preserve legacy behavior if plane is 0 */
763 if (plane == 0)
764 return _bo->stride;
765
766 errno = ENOSYS;
767 return 0;
768 }
769
770 if (plane >= get_number_planes(dri, bo->image)) {
771 errno = EINVAL;
772 return 0;
773 }
774
775 if (bo->image == NULL) {
776 assert(plane == 0);
777 return _bo->stride;
778 }
779
780 image = dri->image->fromPlanar(bo->image, plane, NULL);
781 if (image) {
782 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
783 dri->image->destroyImage(image);
784 } else {
785 assert(plane == 0);
786 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
787 }
788
789 return (uint32_t)stride;
790 }
791
792 static uint32_t
gbm_dri_bo_get_offset(struct gbm_bo * _bo,int plane)793 gbm_dri_bo_get_offset(struct gbm_bo *_bo, int plane)
794 {
795 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
796 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
797 int offset = 0;
798
799 /* These error cases do not actually return an error code, as the user
800 * will also fail to obtain the handle/FD from the BO. In that case, the
801 * offset is irrelevant, as they have no buffer to offset into, so
802 * returning 0 is harmless.
803 */
804 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar)
805 return 0;
806
807 if (plane >= get_number_planes(dri, bo->image))
808 return 0;
809
810 /* Dumb images have no offset */
811 if (bo->image == NULL) {
812 assert(plane == 0);
813 return 0;
814 }
815
816 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
817 if (image) {
818 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
819 dri->image->destroyImage(image);
820 } else {
821 assert(plane == 0);
822 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
823 }
824
825 return (uint32_t)offset;
826 }
827
828 static uint64_t
gbm_dri_bo_get_modifier(struct gbm_bo * _bo)829 gbm_dri_bo_get_modifier(struct gbm_bo *_bo)
830 {
831 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
832 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
833
834 if (!dri->image || dri->image->base.version < 14) {
835 errno = ENOSYS;
836 return DRM_FORMAT_MOD_INVALID;
837 }
838
839 /* Dumb buffers have no modifiers */
840 if (!bo->image)
841 return DRM_FORMAT_MOD_LINEAR;
842
843 uint64_t ret = 0;
844 int mod;
845 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
846 &mod))
847 return DRM_FORMAT_MOD_INVALID;
848
849 ret = (uint64_t)mod << 32;
850
851 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
852 &mod))
853 return DRM_FORMAT_MOD_INVALID;
854
855 ret |= (uint64_t)(mod & 0xffffffff);
856
857 return ret;
858 }
859
860 static void
gbm_dri_bo_destroy(struct gbm_bo * _bo)861 gbm_dri_bo_destroy(struct gbm_bo *_bo)
862 {
863 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
864 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
865 struct drm_mode_destroy_dumb arg;
866
867 if (bo->image != NULL) {
868 dri->image->destroyImage(bo->image);
869 } else {
870 gbm_dri_bo_unmap_dumb(bo);
871 memset(&arg, 0, sizeof(arg));
872 arg.handle = bo->handle;
873 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
874 }
875
876 free(bo);
877 }
878
879 static struct gbm_bo *
gbm_dri_bo_import(struct gbm_device * gbm,uint32_t type,void * buffer,uint32_t usage)880 gbm_dri_bo_import(struct gbm_device *gbm,
881 uint32_t type, void *buffer, uint32_t usage)
882 {
883 struct gbm_dri_device *dri = gbm_dri_device(gbm);
884 struct gbm_dri_bo *bo;
885 __DRIimage *image;
886 unsigned dri_use = 0;
887 int gbm_format;
888
889 /* Required for query image WIDTH & HEIGHT */
890 if (dri->image == NULL || dri->image->base.version < 4) {
891 errno = ENOSYS;
892 return NULL;
893 }
894
895 switch (type) {
896 #if HAVE_WAYLAND_PLATFORM
897 case GBM_BO_IMPORT_WL_BUFFER:
898 {
899 struct wl_drm_buffer *wb;
900
901 if (!dri->wl_drm) {
902 errno = EINVAL;
903 return NULL;
904 }
905
906 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
907 if (!wb) {
908 errno = EINVAL;
909 return NULL;
910 }
911
912 image = dri->image->dupImage(wb->driver_buffer, NULL);
913
914 /* GBM_FORMAT_* is identical to WL_DRM_FORMAT_*, so no conversion
915 * required. */
916 gbm_format = wb->format;
917 break;
918 }
919 #endif
920
921 case GBM_BO_IMPORT_EGL_IMAGE:
922 {
923 int dri_format;
924 if (dri->lookup_image == NULL) {
925 errno = EINVAL;
926 return NULL;
927 }
928
929 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
930 image = dri->image->dupImage(image, NULL);
931 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
932 gbm_format = gbm_dri_to_gbm_format(dri_format);
933 if (gbm_format == 0) {
934 errno = EINVAL;
935 dri->image->destroyImage(image);
936 return NULL;
937 }
938 break;
939 }
940
941 case GBM_BO_IMPORT_FD:
942 {
943 struct gbm_import_fd_data *fd_data = buffer;
944 int stride = fd_data->stride, offset = 0;
945 int fourcc;
946
947 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
948 * tokens accepted by createImageFromFds, except for not supporting
949 * the sARGB format. */
950 fourcc = gbm_format_canonicalize(fd_data->format);
951
952 image = dri->image->createImageFromFds(dri->screen,
953 fd_data->width,
954 fd_data->height,
955 fourcc,
956 &fd_data->fd, 1,
957 &stride, &offset,
958 NULL);
959 if (image == NULL) {
960 errno = EINVAL;
961 return NULL;
962 }
963 gbm_format = fd_data->format;
964 break;
965 }
966
967 case GBM_BO_IMPORT_FD_MODIFIER:
968 {
969 struct gbm_import_fd_modifier_data *fd_data = buffer;
970 unsigned int error;
971 int fourcc;
972
973 /* Import with modifier requires createImageFromDmaBufs2 */
974 if (dri->image == NULL || dri->image->base.version < 15 ||
975 dri->image->createImageFromDmaBufs2 == NULL) {
976 errno = ENOSYS;
977 return NULL;
978 }
979
980 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
981 * tokens accepted by createImageFromDmaBufs2, except for not supporting
982 * the sARGB format. */
983 fourcc = gbm_format_canonicalize(fd_data->format);
984
985 image = dri->image->createImageFromDmaBufs2(dri->screen, fd_data->width,
986 fd_data->height, fourcc,
987 fd_data->modifier,
988 fd_data->fds,
989 fd_data->num_fds,
990 fd_data->strides,
991 fd_data->offsets,
992 0, 0, 0, 0,
993 &error, NULL);
994 if (image == NULL) {
995 errno = ENOSYS;
996 return NULL;
997 }
998
999 gbm_format = fourcc;
1000 break;
1001 }
1002
1003 default:
1004 errno = ENOSYS;
1005 return NULL;
1006 }
1007
1008
1009 bo = calloc(1, sizeof *bo);
1010 if (bo == NULL) {
1011 dri->image->destroyImage(image);
1012 return NULL;
1013 }
1014
1015 bo->image = image;
1016
1017 if (usage & GBM_BO_USE_SCANOUT)
1018 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1019 if (usage & GBM_BO_USE_CURSOR)
1020 dri_use |= __DRI_IMAGE_USE_CURSOR;
1021 if (dri->image->base.version >= 2 &&
1022 !dri->image->validateUsage(bo->image, dri_use)) {
1023 errno = EINVAL;
1024 dri->image->destroyImage(bo->image);
1025 free(bo);
1026 return NULL;
1027 }
1028
1029 bo->base.gbm = gbm;
1030 bo->base.format = gbm_format;
1031
1032 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
1033 (int*)&bo->base.width);
1034 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
1035 (int*)&bo->base.height);
1036 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1037 (int*)&bo->base.stride);
1038 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1039 &bo->base.handle.s32);
1040
1041 return &bo->base;
1042 }
1043
1044 static struct gbm_bo *
create_dumb(struct gbm_device * gbm,uint32_t width,uint32_t height,uint32_t format,uint32_t usage)1045 create_dumb(struct gbm_device *gbm,
1046 uint32_t width, uint32_t height,
1047 uint32_t format, uint32_t usage)
1048 {
1049 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1050 struct drm_mode_create_dumb create_arg;
1051 struct gbm_dri_bo *bo;
1052 struct drm_mode_destroy_dumb destroy_arg;
1053 int ret;
1054 int is_cursor, is_scanout;
1055
1056 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
1057 format == GBM_FORMAT_ARGB8888;
1058 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
1059 (format == GBM_FORMAT_XRGB8888 || format == GBM_FORMAT_XBGR8888);
1060 if (!is_cursor && !is_scanout) {
1061 errno = EINVAL;
1062 return NULL;
1063 }
1064
1065 bo = calloc(1, sizeof *bo);
1066 if (bo == NULL)
1067 return NULL;
1068
1069 memset(&create_arg, 0, sizeof(create_arg));
1070 create_arg.bpp = 32;
1071 create_arg.width = width;
1072 create_arg.height = height;
1073
1074 ret = drmIoctl(dri->base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
1075 if (ret)
1076 goto free_bo;
1077
1078 bo->base.gbm = gbm;
1079 bo->base.width = width;
1080 bo->base.height = height;
1081 bo->base.stride = create_arg.pitch;
1082 bo->base.format = format;
1083 bo->base.handle.u32 = create_arg.handle;
1084 bo->handle = create_arg.handle;
1085 bo->size = create_arg.size;
1086
1087 if (gbm_dri_bo_map_dumb(bo) == NULL)
1088 goto destroy_dumb;
1089
1090 return &bo->base;
1091
1092 destroy_dumb:
1093 memset(&destroy_arg, 0, sizeof destroy_arg);
1094 destroy_arg.handle = create_arg.handle;
1095 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
1096 free_bo:
1097 free(bo);
1098
1099 return NULL;
1100 }
1101
1102 static struct gbm_bo *
gbm_dri_bo_create(struct gbm_device * gbm,uint32_t width,uint32_t height,uint32_t format,uint32_t usage,const uint64_t * modifiers,const unsigned int count)1103 gbm_dri_bo_create(struct gbm_device *gbm,
1104 uint32_t width, uint32_t height,
1105 uint32_t format, uint32_t usage,
1106 const uint64_t *modifiers,
1107 const unsigned int count)
1108 {
1109 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1110 struct gbm_dri_bo *bo;
1111 int dri_format;
1112 unsigned dri_use = 0;
1113
1114 /* Callers of this may specify a modifier, or a dri usage, but not both. The
1115 * newer modifier interface deprecates the older usage flags.
1116 */
1117 assert(!(usage && count));
1118
1119 format = gbm_format_canonicalize(format);
1120
1121 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
1122 return create_dumb(gbm, width, height, format, usage);
1123
1124 bo = calloc(1, sizeof *bo);
1125 if (bo == NULL)
1126 return NULL;
1127
1128 bo->base.gbm = gbm;
1129 bo->base.width = width;
1130 bo->base.height = height;
1131 bo->base.format = format;
1132
1133 dri_format = gbm_format_to_dri_format(format);
1134 if (dri_format == 0) {
1135 errno = EINVAL;
1136 goto failed;
1137 }
1138
1139 if (usage & GBM_BO_USE_SCANOUT)
1140 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1141 if (usage & GBM_BO_USE_CURSOR)
1142 dri_use |= __DRI_IMAGE_USE_CURSOR;
1143 if (usage & GBM_BO_USE_LINEAR)
1144 dri_use |= __DRI_IMAGE_USE_LINEAR;
1145 if (usage & GBM_BO_USE_PROTECTED)
1146 dri_use |= __DRI_IMAGE_USE_PROTECTED;
1147
1148 /* Gallium drivers requires shared in order to get the handle/stride */
1149 dri_use |= __DRI_IMAGE_USE_SHARE;
1150
1151 if (modifiers) {
1152 if (!dri->image || dri->image->base.version < 14 ||
1153 !dri->image->createImageWithModifiers) {
1154 fprintf(stderr, "Modifiers specified, but DRI is too old\n");
1155 errno = ENOSYS;
1156 goto failed;
1157 }
1158
1159 /* It's acceptable to create an image with INVALID modifier in the list,
1160 * but it cannot be on the only modifier (since it will certainly fail
1161 * later). While we could easily catch this after modifier creation, doing
1162 * the check here is a convenient debug check likely pointing at whatever
1163 * interface the client is using to build its modifier list.
1164 */
1165 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1166 fprintf(stderr, "Only invalid modifier specified\n");
1167 errno = EINVAL;
1168 goto failed;
1169 }
1170
1171 bo->image =
1172 dri->image->createImageWithModifiers(dri->screen,
1173 width, height,
1174 dri_format,
1175 modifiers, count,
1176 bo);
1177
1178 if (bo->image) {
1179 /* The client passed in a list of invalid modifiers */
1180 assert(gbm_dri_bo_get_modifier(&bo->base) != DRM_FORMAT_MOD_INVALID);
1181 }
1182 } else {
1183 bo->image = dri->image->createImage(dri->screen, width, height,
1184 dri_format, dri_use, bo);
1185 }
1186
1187 if (bo->image == NULL)
1188 goto failed;
1189
1190 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1191 &bo->base.handle.s32);
1192 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1193 (int *) &bo->base.stride);
1194
1195 return &bo->base;
1196
1197 failed:
1198 free(bo);
1199 return NULL;
1200 }
1201
1202 static void *
gbm_dri_bo_map(struct gbm_bo * _bo,uint32_t x,uint32_t y,uint32_t width,uint32_t height,uint32_t flags,uint32_t * stride,void ** map_data)1203 gbm_dri_bo_map(struct gbm_bo *_bo,
1204 uint32_t x, uint32_t y,
1205 uint32_t width, uint32_t height,
1206 uint32_t flags, uint32_t *stride, void **map_data)
1207 {
1208 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1209 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1210
1211 /* If it's a dumb buffer, we already have a mapping */
1212 if (bo->map) {
1213 *map_data = (char *)bo->map + (bo->base.stride * y) + (x * 4);
1214 *stride = bo->base.stride;
1215 return *map_data;
1216 }
1217
1218 if (!dri->image || dri->image->base.version < 12 || !dri->image->mapImage) {
1219 errno = ENOSYS;
1220 return NULL;
1221 }
1222
1223 mtx_lock(&dri->mutex);
1224 if (!dri->context)
1225 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
1226 NULL, NULL);
1227 assert(dri->context);
1228 mtx_unlock(&dri->mutex);
1229
1230 /* GBM flags and DRI flags are the same, so just pass them on */
1231 return dri->image->mapImage(dri->context, bo->image, x, y,
1232 width, height, flags, (int *)stride,
1233 map_data);
1234 }
1235
1236 static void
gbm_dri_bo_unmap(struct gbm_bo * _bo,void * map_data)1237 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
1238 {
1239 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1240 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1241
1242 /* Check if it's a dumb buffer and check the pointer is in range */
1243 if (bo->map) {
1244 assert(map_data >= bo->map);
1245 assert(map_data < (bo->map + bo->size));
1246 return;
1247 }
1248
1249 if (!dri->context || !dri->image ||
1250 dri->image->base.version < 12 || !dri->image->unmapImage)
1251 return;
1252
1253 dri->image->unmapImage(dri->context, bo->image, map_data);
1254
1255 /*
1256 * Not all DRI drivers use direct maps. They may queue up DMA operations
1257 * on the mapping context. Since there is no explicit gbm flush
1258 * mechanism, we need to flush here.
1259 */
1260 if (dri->flush->base.version >= 4)
1261 dri->flush->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
1262 }
1263
1264
1265 static struct gbm_surface *
gbm_dri_surface_create(struct gbm_device * gbm,uint32_t width,uint32_t height,uint32_t format,uint32_t flags,const uint64_t * modifiers,const unsigned count)1266 gbm_dri_surface_create(struct gbm_device *gbm,
1267 uint32_t width, uint32_t height,
1268 uint32_t format, uint32_t flags,
1269 const uint64_t *modifiers, const unsigned count)
1270 {
1271 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1272 struct gbm_dri_surface *surf;
1273
1274 if (modifiers &&
1275 (!dri->image || dri->image->base.version < 14 ||
1276 !dri->image->createImageWithModifiers)) {
1277 errno = ENOSYS;
1278 return NULL;
1279 }
1280
1281 if (count)
1282 assert(modifiers);
1283
1284 /* It's acceptable to create an image with INVALID modifier in the list,
1285 * but it cannot be on the only modifier (since it will certainly fail
1286 * later). While we could easily catch this after modifier creation, doing
1287 * the check here is a convenient debug check likely pointing at whatever
1288 * interface the client is using to build its modifier list.
1289 */
1290 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1291 fprintf(stderr, "Only invalid modifier specified\n");
1292 errno = EINVAL;
1293 }
1294
1295 surf = calloc(1, sizeof *surf);
1296 if (surf == NULL) {
1297 errno = ENOMEM;
1298 return NULL;
1299 }
1300
1301 surf->base.gbm = gbm;
1302 surf->base.width = width;
1303 surf->base.height = height;
1304 surf->base.format = gbm_format_canonicalize(format);
1305 surf->base.flags = flags;
1306 if (!modifiers) {
1307 assert(!count);
1308 return &surf->base;
1309 }
1310
1311 surf->base.modifiers = calloc(count, sizeof(*modifiers));
1312 if (count && !surf->base.modifiers) {
1313 errno = ENOMEM;
1314 free(surf);
1315 return NULL;
1316 }
1317
1318 /* TODO: We are deferring validation of modifiers until the image is actually
1319 * created. This deferred creation can fail due to a modifier-format
1320 * mismatch. The result is the client has a surface but no object to back it.
1321 */
1322 surf->base.count = count;
1323 memcpy(surf->base.modifiers, modifiers, count * sizeof(*modifiers));
1324
1325 return &surf->base;
1326 }
1327
1328 static void
gbm_dri_surface_destroy(struct gbm_surface * _surf)1329 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1330 {
1331 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1332
1333 free(surf->base.modifiers);
1334 free(surf);
1335 }
1336
1337 static void
dri_destroy(struct gbm_device * gbm)1338 dri_destroy(struct gbm_device *gbm)
1339 {
1340 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1341 unsigned i;
1342
1343 if (dri->context)
1344 dri->core->destroyContext(dri->context);
1345
1346 dri->core->destroyScreen(dri->screen);
1347 for (i = 0; dri->driver_configs[i]; i++)
1348 free((__DRIconfig *) dri->driver_configs[i]);
1349 free(dri->driver_configs);
1350 dlclose(dri->driver);
1351 free(dri->driver_name);
1352
1353 free(dri);
1354 }
1355
1356 static struct gbm_device *
dri_device_create(int fd)1357 dri_device_create(int fd)
1358 {
1359 struct gbm_dri_device *dri;
1360 int ret;
1361 bool force_sw;
1362
1363 dri = calloc(1, sizeof *dri);
1364 if (!dri)
1365 return NULL;
1366
1367 dri->base.fd = fd;
1368 dri->base.bo_create = gbm_dri_bo_create;
1369 dri->base.bo_import = gbm_dri_bo_import;
1370 dri->base.bo_map = gbm_dri_bo_map;
1371 dri->base.bo_unmap = gbm_dri_bo_unmap;
1372 dri->base.is_format_supported = gbm_dri_is_format_supported;
1373 dri->base.get_format_modifier_plane_count =
1374 gbm_dri_get_format_modifier_plane_count;
1375 dri->base.bo_write = gbm_dri_bo_write;
1376 dri->base.bo_get_fd = gbm_dri_bo_get_fd;
1377 dri->base.bo_get_planes = gbm_dri_bo_get_planes;
1378 dri->base.bo_get_handle = gbm_dri_bo_get_handle_for_plane;
1379 dri->base.bo_get_stride = gbm_dri_bo_get_stride;
1380 dri->base.bo_get_offset = gbm_dri_bo_get_offset;
1381 dri->base.bo_get_modifier = gbm_dri_bo_get_modifier;
1382 dri->base.bo_destroy = gbm_dri_bo_destroy;
1383 dri->base.destroy = dri_destroy;
1384 dri->base.surface_create = gbm_dri_surface_create;
1385 dri->base.surface_destroy = gbm_dri_surface_destroy;
1386
1387 dri->base.name = "drm";
1388
1389 dri->visual_table = gbm_dri_visuals_table;
1390 dri->num_visuals = ARRAY_SIZE(gbm_dri_visuals_table);
1391
1392 mtx_init(&dri->mutex, mtx_plain);
1393
1394 force_sw = env_var_as_boolean("GBM_ALWAYS_SOFTWARE", false);
1395 if (!force_sw) {
1396 ret = dri_screen_create(dri);
1397 if (ret)
1398 ret = dri_screen_create_sw(dri);
1399 } else {
1400 ret = dri_screen_create_sw(dri);
1401 }
1402
1403 if (ret)
1404 goto err_dri;
1405
1406 return &dri->base;
1407
1408 err_dri:
1409 free(dri);
1410
1411 return NULL;
1412 }
1413
1414 struct gbm_backend gbm_dri_backend = {
1415 .backend_name = "dri",
1416 .create_device = dri_device_create,
1417 };
1418