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