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