egl: Convert configs to use shifts and sizes instead of masks
[mesa.git] / src / egl / drivers / dri2 / platform_drm.c
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 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <xf86drm.h>
33 #include <dlfcn.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 #include "egl_dri2.h"
40 #include "egl_dri2_fallbacks.h"
41 #include "loader.h"
42
43 static struct gbm_bo *
44 lock_front_buffer(struct gbm_surface *_surf)
45 {
46 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
47 struct dri2_egl_surface *dri2_surf = surf->dri_private;
48 struct gbm_dri_device *device = gbm_dri_device(_surf->gbm);
49 struct gbm_bo *bo;
50
51 if (dri2_surf->current == NULL) {
52 _eglError(EGL_BAD_SURFACE, "no front buffer");
53 return NULL;
54 }
55
56 bo = dri2_surf->current->bo;
57
58 if (device->dri2) {
59 dri2_surf->current->locked = true;
60 dri2_surf->current = NULL;
61 }
62
63 return bo;
64 }
65
66 static void
67 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
68 {
69 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
70 struct dri2_egl_surface *dri2_surf = surf->dri_private;
71
72 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
73 if (dri2_surf->color_buffers[i].bo == bo) {
74 dri2_surf->color_buffers[i].locked = false;
75 break;
76 }
77 }
78 }
79
80 static int
81 has_free_buffers(struct gbm_surface *_surf)
82 {
83 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
84 struct dri2_egl_surface *dri2_surf = surf->dri_private;
85
86 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
87 if (!dri2_surf->color_buffers[i].locked)
88 return 1;
89
90 return 0;
91 }
92
93 static bool
94 dri2_drm_config_is_compatible(struct dri2_egl_display *dri2_dpy,
95 const __DRIconfig *config,
96 struct gbm_surface *surface)
97 {
98 const struct gbm_dri_visual *visual = NULL;
99 int shifts[4];
100 unsigned int sizes[4];
101 int i;
102
103 /* Check that the EGLConfig being used to render to the surface is
104 * compatible with the surface format. Since mixing ARGB and XRGB of
105 * otherwise-compatible formats is relatively common, explicitly allow
106 * this.
107 */
108 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
109
110 for (i = 0; i < dri2_dpy->gbm_dri->num_visuals; i++) {
111 visual = &dri2_dpy->gbm_dri->visual_table[i];
112 if (visual->gbm_format == surface->format)
113 break;
114 }
115
116 if (i == dri2_dpy->gbm_dri->num_visuals)
117 return false;
118
119 if (shifts[0] != visual->rgba_shifts.red ||
120 shifts[1] != visual->rgba_shifts.green ||
121 shifts[2] != visual->rgba_shifts.blue ||
122 (shifts[3] > -1 && shifts[3] != visual->rgba_shifts.alpha) ||
123 sizes[0] != visual->rgba_sizes.red ||
124 sizes[1] != visual->rgba_sizes.green ||
125 sizes[2] != visual->rgba_sizes.blue ||
126 (sizes[3] > 0 && sizes[3] != visual->rgba_sizes.alpha)) {
127 return false;
128 }
129
130 return true;
131 }
132
133 static _EGLSurface *
134 dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
135 _EGLConfig *conf, void *native_surface,
136 const EGLint *attrib_list)
137 {
138 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
139 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
140 struct dri2_egl_surface *dri2_surf;
141 struct gbm_surface *surface = native_surface;
142 struct gbm_dri_surface *surf;
143 const __DRIconfig *config;
144
145 (void) drv;
146
147 dri2_surf = calloc(1, sizeof *dri2_surf);
148 if (!dri2_surf) {
149 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
150 return NULL;
151 }
152
153 if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf,
154 attrib_list, false, native_surface))
155 goto cleanup_surf;
156
157 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
158 dri2_surf->base.GLColorspace);
159
160 if (!config) {
161 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
162 goto cleanup_surf;
163 }
164
165 if (!dri2_drm_config_is_compatible(dri2_dpy, config, surface)) {
166 _eglError(EGL_BAD_MATCH, "EGL config not compatible with GBM format");
167 goto cleanup_surf;
168 }
169
170 surf = gbm_dri_surface(surface);
171 dri2_surf->gbm_surf = surf;
172 dri2_surf->base.Width = surf->base.width;
173 dri2_surf->base.Height = surf->base.height;
174 surf->dri_private = dri2_surf;
175
176 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf->gbm_surf))
177 goto cleanup_surf;
178
179 return &dri2_surf->base;
180
181 cleanup_surf:
182 free(dri2_surf);
183
184 return NULL;
185 }
186
187 static _EGLSurface *
188 dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
189 _EGLConfig *conf, void *native_window,
190 const EGLint *attrib_list)
191 {
192 /* From the EGL_MESA_platform_gbm spec, version 5:
193 *
194 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
195 * that belongs to the GBM platform. Any such call fails and generates
196 * EGL_BAD_PARAMETER.
197 */
198 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM");
199 return NULL;
200 }
201
202 static EGLBoolean
203 dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
204 {
205 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
206 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
207
208 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
209
210 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
211 if (dri2_surf->color_buffers[i].bo)
212 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
213 }
214
215 dri2_egl_surface_free_local_buffers(dri2_surf);
216
217 dri2_fini_surface(surf);
218 free(surf);
219
220 return EGL_TRUE;
221 }
222
223 static int
224 get_back_bo(struct dri2_egl_surface *dri2_surf)
225 {
226 struct dri2_egl_display *dri2_dpy =
227 dri2_egl_display(dri2_surf->base.Resource.Display);
228 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
229 int age = 0;
230
231 if (dri2_surf->back == NULL) {
232 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
233 if (!dri2_surf->color_buffers[i].locked &&
234 dri2_surf->color_buffers[i].age >= age) {
235 dri2_surf->back = &dri2_surf->color_buffers[i];
236 age = dri2_surf->color_buffers[i].age;
237 }
238 }
239 }
240
241 if (dri2_surf->back == NULL)
242 return -1;
243 if (dri2_surf->back->bo == NULL) {
244 if (surf->base.modifiers)
245 dri2_surf->back->bo = gbm_bo_create_with_modifiers(&dri2_dpy->gbm_dri->base,
246 surf->base.width,
247 surf->base.height,
248 surf->base.format,
249 surf->base.modifiers,
250 surf->base.count);
251 else
252 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
253 surf->base.width,
254 surf->base.height,
255 surf->base.format,
256 surf->base.flags);
257
258 }
259 if (dri2_surf->back->bo == NULL)
260 return -1;
261
262 return 0;
263 }
264
265 static int
266 get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
267 {
268 struct dri2_egl_display *dri2_dpy =
269 dri2_egl_display(dri2_surf->base.Resource.Display);
270 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
271
272 if (dri2_surf->current == NULL) {
273 assert(!dri2_surf->color_buffers[0].locked);
274 dri2_surf->current = &dri2_surf->color_buffers[0];
275 }
276
277 if (dri2_surf->current->bo == NULL)
278 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
279 surf->base.width, surf->base.height,
280 surf->base.format, surf->base.flags);
281 if (dri2_surf->current->bo == NULL)
282 return -1;
283
284 return 0;
285 }
286
287 static void
288 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
289 {
290 struct dri2_egl_display *dri2_dpy =
291 dri2_egl_display(dri2_surf->base.Resource.Display);
292 struct gbm_dri_bo *bo;
293 int name, pitch;
294
295 bo = gbm_dri_bo(dri2_surf->back->bo);
296
297 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
298 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
299
300 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
301 buffer->name = name;
302 buffer->pitch = pitch;
303 buffer->cpp = 4;
304 buffer->flags = 0;
305 }
306
307 static __DRIbuffer *
308 dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
309 int *width, int *height,
310 unsigned int *attachments, int count,
311 int *out_count, void *loaderPrivate)
312 {
313 struct dri2_egl_surface *dri2_surf = loaderPrivate;
314 int i, j;
315
316 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
317 __DRIbuffer *local;
318
319 assert(attachments[i] < __DRI_BUFFER_COUNT);
320 assert(j < ARRAY_SIZE(dri2_surf->buffers));
321
322 switch (attachments[i]) {
323 case __DRI_BUFFER_BACK_LEFT:
324 if (get_back_bo(dri2_surf) < 0) {
325 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
326 return NULL;
327 }
328 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
329 break;
330 default:
331 local = dri2_egl_surface_alloc_local_buffer(dri2_surf, attachments[i],
332 attachments[i + 1]);
333
334 if (!local) {
335 _eglError(EGL_BAD_ALLOC, "failed to allocate local buffer");
336 return NULL;
337 }
338 dri2_surf->buffers[j] = *local;
339 break;
340 }
341 }
342
343 *out_count = j;
344 if (j == 0)
345 return NULL;
346
347 *width = dri2_surf->base.Width;
348 *height = dri2_surf->base.Height;
349
350 return dri2_surf->buffers;
351 }
352
353 static __DRIbuffer *
354 dri2_drm_get_buffers(__DRIdrawable * driDrawable,
355 int *width, int *height,
356 unsigned int *attachments, int count,
357 int *out_count, void *loaderPrivate)
358 {
359 unsigned int *attachments_with_format;
360 __DRIbuffer *buffer;
361 const unsigned int format = 32;
362
363 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
364 if (!attachments_with_format) {
365 *out_count = 0;
366 return NULL;
367 }
368
369 for (int i = 0; i < count; ++i) {
370 attachments_with_format[2*i] = attachments[i];
371 attachments_with_format[2*i + 1] = format;
372 }
373
374 buffer =
375 dri2_drm_get_buffers_with_format(driDrawable,
376 width, height,
377 attachments_with_format, count,
378 out_count, loaderPrivate);
379
380 free(attachments_with_format);
381
382 return buffer;
383 }
384
385 static int
386 dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
387 unsigned int format,
388 uint32_t *stamp,
389 void *loaderPrivate,
390 uint32_t buffer_mask,
391 struct __DRIimageList *buffers)
392 {
393 struct dri2_egl_surface *dri2_surf = loaderPrivate;
394 struct gbm_dri_bo *bo;
395
396 if (get_back_bo(dri2_surf) < 0)
397 return 0;
398
399 bo = gbm_dri_bo(dri2_surf->back->bo);
400 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
401 buffers->back = bo->image;
402
403 return 1;
404 }
405
406 static void
407 dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
408 {
409 (void) driDrawable;
410 (void) loaderPrivate;
411 }
412
413 static EGLBoolean
414 dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
415 {
416 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
417 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
418
419 if (!dri2_dpy->flush) {
420 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
421 return EGL_TRUE;
422 }
423
424 if (dri2_surf->current)
425 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
426 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
427 if (dri2_surf->color_buffers[i].age > 0)
428 dri2_surf->color_buffers[i].age++;
429
430 /* Make sure we have a back buffer in case we're swapping without
431 * ever rendering. */
432 if (get_back_bo(dri2_surf) < 0)
433 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
434
435 dri2_surf->current = dri2_surf->back;
436 dri2_surf->current->age = 1;
437 dri2_surf->back = NULL;
438
439 dri2_flush_drawable_for_swapbuffers(disp, draw);
440 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
441
442 return EGL_TRUE;
443 }
444
445 static EGLint
446 dri2_drm_query_buffer_age(_EGLDriver *drv,
447 _EGLDisplay *disp, _EGLSurface *surface)
448 {
449 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
450
451 if (get_back_bo(dri2_surf) < 0) {
452 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
453 return -1;
454 }
455
456 return dri2_surf->back->age;
457 }
458
459 static _EGLImage *
460 dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
461 EGLClientBuffer buffer, const EGLint *attr_list)
462 {
463 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
464 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
465 struct dri2_egl_image *dri2_img;
466
467 dri2_img = malloc(sizeof *dri2_img);
468 if (!dri2_img) {
469 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
470 return NULL;
471 }
472
473 _eglInitImage(&dri2_img->base, disp);
474
475 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
476 if (dri2_img->dri_image == NULL) {
477 free(dri2_img);
478 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
479 return NULL;
480 }
481
482 return &dri2_img->base;
483 }
484
485 static _EGLImage *
486 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
487 _EGLContext *ctx, EGLenum target,
488 EGLClientBuffer buffer, const EGLint *attr_list)
489 {
490 (void) drv;
491
492 switch (target) {
493 case EGL_NATIVE_PIXMAP_KHR:
494 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
495 default:
496 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
497 }
498 }
499
500 static int
501 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
502 {
503 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
504
505 return drmAuthMagic(dri2_dpy->fd, id);
506 }
507
508 static void
509 swrast_put_image2(__DRIdrawable *driDrawable,
510 int op,
511 int x,
512 int y,
513 int width,
514 int height,
515 int stride,
516 char *data,
517 void *loaderPrivate)
518 {
519 struct dri2_egl_surface *dri2_surf = loaderPrivate;
520 int internal_stride;
521 struct gbm_dri_bo *bo;
522 uint32_t bpp;
523 int x_bytes, width_bytes;
524 char *src, *dst;
525
526 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
527 op != __DRI_SWRAST_IMAGE_OP_SWAP)
528 return;
529
530 if (get_swrast_front_bo(dri2_surf) < 0)
531 return;
532
533 bo = gbm_dri_bo(dri2_surf->current->bo);
534
535 bpp = gbm_bo_get_bpp(&bo->base);
536 if (bpp == 0)
537 return;
538
539 x_bytes = x * (bpp >> 3);
540 width_bytes = width * (bpp >> 3);
541
542 if (gbm_dri_bo_map_dumb(bo) == NULL)
543 return;
544
545 internal_stride = bo->base.stride;
546
547 dst = bo->map + x_bytes + (y * internal_stride);
548 src = data;
549
550 for (int i = 0; i < height; i++) {
551 memcpy(dst, src, width_bytes);
552 dst += internal_stride;
553 src += stride;
554 }
555
556 gbm_dri_bo_unmap_dumb(bo);
557 }
558
559 static void
560 swrast_get_image(__DRIdrawable *driDrawable,
561 int x,
562 int y,
563 int width,
564 int height,
565 char *data,
566 void *loaderPrivate)
567 {
568 struct dri2_egl_surface *dri2_surf = loaderPrivate;
569 int internal_stride, stride;
570 struct gbm_dri_bo *bo;
571 uint32_t bpp;
572 int x_bytes, width_bytes;
573 char *src, *dst;
574
575 if (get_swrast_front_bo(dri2_surf) < 0)
576 return;
577
578 bo = gbm_dri_bo(dri2_surf->current->bo);
579
580 bpp = gbm_bo_get_bpp(&bo->base);
581 if (bpp == 0)
582 return;
583
584 x_bytes = x * (bpp >> 3);
585 width_bytes = width * (bpp >> 3);
586
587 internal_stride = bo->base.stride;
588 stride = width_bytes;
589
590 if (gbm_dri_bo_map_dumb(bo) == NULL)
591 return;
592
593 dst = data;
594 src = bo->map + x_bytes + (y * internal_stride);
595
596 for (int i = 0; i < height; i++) {
597 memcpy(dst, src, width_bytes);
598 dst += stride;
599 src += internal_stride;
600 }
601
602 gbm_dri_bo_unmap_dumb(bo);
603 }
604
605 static EGLBoolean
606 drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
607 {
608 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
609 const struct gbm_dri_visual *visuals = dri2_dpy->gbm_dri->visual_table;
610 int num_visuals = dri2_dpy->gbm_dri->num_visuals;
611 unsigned int format_count[num_visuals];
612 unsigned int config_count = 0;
613
614 memset(format_count, 0, num_visuals * sizeof(unsigned int));
615
616 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
617 const __DRIconfig *config = dri2_dpy->driver_configs[i];
618 int shifts[4];
619 unsigned int sizes[4];
620
621 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
622
623 for (unsigned j = 0; j < num_visuals; j++) {
624 struct dri2_egl_config *dri2_conf;
625
626 if (visuals[j].rgba_shifts.red != shifts[0] ||
627 visuals[j].rgba_shifts.green != shifts[1] ||
628 visuals[j].rgba_shifts.blue != shifts[2] ||
629 visuals[j].rgba_shifts.alpha != shifts[3] ||
630 visuals[j].rgba_sizes.red != sizes[0] ||
631 visuals[j].rgba_sizes.green != sizes[1] ||
632 visuals[j].rgba_sizes.blue != sizes[2] ||
633 visuals[j].rgba_sizes.alpha != sizes[3])
634 continue;
635
636 const EGLint attr_list[] = {
637 EGL_NATIVE_VISUAL_ID, visuals[j].gbm_format,
638 EGL_NONE,
639 };
640
641 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
642 config_count + 1, EGL_WINDOW_BIT, attr_list, NULL, NULL);
643 if (dri2_conf) {
644 if (dri2_conf->base.ConfigID == config_count + 1)
645 config_count++;
646 format_count[j]++;
647 }
648 }
649 }
650
651 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
652 if (!format_count[i]) {
653 struct gbm_format_name_desc desc;
654 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
655 gbm_format_get_name(visuals[i].gbm_format, &desc));
656 }
657 }
658
659 return (config_count != 0);
660 }
661
662 static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
663 .authenticate = dri2_drm_authenticate,
664 .create_window_surface = dri2_drm_create_window_surface,
665 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
666 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
667 .destroy_surface = dri2_drm_destroy_surface,
668 .create_image = dri2_drm_create_image_khr,
669 .swap_buffers = dri2_drm_swap_buffers,
670 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
671 .swap_buffers_region = dri2_fallback_swap_buffers_region,
672 .post_sub_buffer = dri2_fallback_post_sub_buffer,
673 .copy_buffers = dri2_fallback_copy_buffers,
674 .query_buffer_age = dri2_drm_query_buffer_age,
675 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
676 .get_sync_values = dri2_fallback_get_sync_values,
677 .get_dri_drawable = dri2_surface_get_dri_drawable,
678 };
679
680 EGLBoolean
681 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
682 {
683 _EGLDevice *dev;
684 struct dri2_egl_display *dri2_dpy;
685 struct gbm_device *gbm;
686 const char *err;
687
688 /* Not supported yet */
689 if (disp->Options.ForceSoftware)
690 return EGL_FALSE;
691
692 dri2_dpy = calloc(1, sizeof *dri2_dpy);
693 if (!dri2_dpy)
694 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
695
696 dri2_dpy->fd = -1;
697 disp->DriverData = (void *) dri2_dpy;
698
699 gbm = disp->PlatformDisplay;
700 if (gbm == NULL) {
701 char buf[64];
702 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
703 if (n != -1 && n < sizeof(buf))
704 dri2_dpy->fd = loader_open_device(buf);
705 gbm = gbm_create_device(dri2_dpy->fd);
706 if (gbm == NULL) {
707 err = "DRI2: failed to create gbm device";
708 goto cleanup;
709 }
710 dri2_dpy->own_device = true;
711 } else {
712 dri2_dpy->fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3);
713 if (dri2_dpy->fd < 0) {
714 err = "DRI2: failed to fcntl() existing gbm device";
715 goto cleanup;
716 }
717 }
718 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
719
720 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
721 err = "DRI2: gbm device using incorrect/incompatible backend";
722 goto cleanup;
723 }
724
725 dev = _eglAddDevice(dri2_dpy->fd, false);
726 if (!dev) {
727 err = "DRI2: failed to find EGLDevice";
728 goto cleanup;
729 }
730
731 disp->Device = dev;
732
733 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->driver_name);
734
735 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
736 dri2_dpy->core = dri2_dpy->gbm_dri->core;
737 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
738 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
739 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
740
741 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
742 dri2_dpy->gbm_dri->lookup_user_data = disp;
743
744 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
745 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
746 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
747 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
748 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
749 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
750
751 dri2_dpy->gbm_dri->base.surface_lock_front_buffer = lock_front_buffer;
752 dri2_dpy->gbm_dri->base.surface_release_buffer = release_buffer;
753 dri2_dpy->gbm_dri->base.surface_has_free_buffers = has_free_buffers;
754
755 if (!dri2_setup_extensions(disp)) {
756 err = "DRI2: failed to find required DRI extensions";
757 goto cleanup;
758 }
759
760 dri2_setup_screen(disp);
761
762 if (!drm_add_configs_for_visuals(drv, disp)) {
763 err = "DRI2: failed to add configs";
764 goto cleanup;
765 }
766
767 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
768 if (dri2_dpy->dri2)
769 disp->Extensions.EXT_buffer_age = EGL_TRUE;
770
771 #ifdef HAVE_WAYLAND_PLATFORM
772 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
773 #endif
774 dri2_set_WL_bind_wayland_display(drv, disp);
775
776 /* Fill vtbl last to prevent accidentally calling virtual function during
777 * initialization.
778 */
779 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
780
781 return EGL_TRUE;
782
783 cleanup:
784 dri2_display_destroy(disp);
785 return _eglError(EGL_NOT_INITIALIZED, err);
786 }
787
788 void
789 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy)
790 {
791 if (dri2_dpy->own_device)
792 gbm_device_destroy(&dri2_dpy->gbm_dri->base);
793 }