egl/drm: use gbm_dri_surface() wrapper
[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 = (struct 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 = (struct 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->base.Type == EGL_WINDOW_BIT) {
438 if (dri2_surf->current)
439 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
440 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
441 if (dri2_surf->color_buffers[i].age > 0)
442 dri2_surf->color_buffers[i].age++;
443
444 /* Make sure we have a back buffer in case we're swapping without
445 * ever rendering. */
446 if (get_back_bo(dri2_surf) < 0)
447 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
448
449 dri2_surf->current = dri2_surf->back;
450 dri2_surf->current->age = 1;
451 dri2_surf->back = NULL;
452 }
453
454 dri2_flush_drawable_for_swapbuffers(disp, draw);
455 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
456
457 return EGL_TRUE;
458 }
459
460 static EGLint
461 dri2_drm_query_buffer_age(_EGLDriver *drv,
462 _EGLDisplay *disp, _EGLSurface *surface)
463 {
464 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
465
466 if (get_back_bo(dri2_surf) < 0) {
467 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
468 return -1;
469 }
470
471 return dri2_surf->back->age;
472 }
473
474 static _EGLImage *
475 dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
476 EGLClientBuffer buffer, const EGLint *attr_list)
477 {
478 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
479 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
480 struct dri2_egl_image *dri2_img;
481
482 dri2_img = malloc(sizeof *dri2_img);
483 if (!dri2_img) {
484 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
485 return NULL;
486 }
487
488 _eglInitImage(&dri2_img->base, disp);
489
490 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
491 if (dri2_img->dri_image == NULL) {
492 free(dri2_img);
493 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
494 return NULL;
495 }
496
497 return &dri2_img->base;
498 }
499
500 static _EGLImage *
501 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
502 _EGLContext *ctx, EGLenum target,
503 EGLClientBuffer buffer, const EGLint *attr_list)
504 {
505 (void) drv;
506
507 switch (target) {
508 case EGL_NATIVE_PIXMAP_KHR:
509 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
510 default:
511 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
512 }
513 }
514
515 static int
516 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
517 {
518 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
519
520 return drmAuthMagic(dri2_dpy->fd, id);
521 }
522
523 static void
524 swrast_put_image2(__DRIdrawable *driDrawable,
525 int op,
526 int x,
527 int y,
528 int width,
529 int height,
530 int stride,
531 char *data,
532 void *loaderPrivate)
533 {
534 struct dri2_egl_surface *dri2_surf = loaderPrivate;
535 int internal_stride;
536 struct gbm_dri_bo *bo;
537 uint32_t bpp;
538 int x_bytes, width_bytes;
539 char *src, *dst;
540
541 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
542 op != __DRI_SWRAST_IMAGE_OP_SWAP)
543 return;
544
545 if (get_swrast_front_bo(dri2_surf) < 0)
546 return;
547
548 bo = gbm_dri_bo(dri2_surf->current->bo);
549
550 bpp = gbm_bo_get_bpp(&bo->base);
551 if (bpp == 0)
552 return;
553
554 x_bytes = x * (bpp >> 3);
555 width_bytes = width * (bpp >> 3);
556
557 if (gbm_dri_bo_map_dumb(bo) == NULL)
558 return;
559
560 internal_stride = bo->base.stride;
561
562 dst = bo->map + x_bytes + (y * internal_stride);
563 src = data;
564
565 for (int i = 0; i < height; i++) {
566 memcpy(dst, src, width_bytes);
567 dst += internal_stride;
568 src += stride;
569 }
570
571 gbm_dri_bo_unmap_dumb(bo);
572 }
573
574 static void
575 swrast_get_image(__DRIdrawable *driDrawable,
576 int x,
577 int y,
578 int width,
579 int height,
580 char *data,
581 void *loaderPrivate)
582 {
583 struct dri2_egl_surface *dri2_surf = loaderPrivate;
584 int internal_stride, stride;
585 struct gbm_dri_bo *bo;
586 uint32_t bpp;
587 int x_bytes, width_bytes;
588 char *src, *dst;
589
590 if (get_swrast_front_bo(dri2_surf) < 0)
591 return;
592
593 bo = gbm_dri_bo(dri2_surf->current->bo);
594
595 bpp = gbm_bo_get_bpp(&bo->base);
596 if (bpp == 0)
597 return;
598
599 x_bytes = x * (bpp >> 3);
600 width_bytes = width * (bpp >> 3);
601
602 internal_stride = bo->base.stride;
603 stride = width_bytes;
604
605 if (gbm_dri_bo_map_dumb(bo) == NULL)
606 return;
607
608 dst = data;
609 src = bo->map + x_bytes + (y * internal_stride);
610
611 for (int i = 0; i < height; i++) {
612 memcpy(dst, src, width_bytes);
613 dst += stride;
614 src += internal_stride;
615 }
616
617 gbm_dri_bo_unmap_dumb(bo);
618 }
619
620 static EGLBoolean
621 drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
622 {
623 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
624 const struct gbm_dri_visual *visuals = dri2_dpy->gbm_dri->visual_table;
625 int num_visuals = dri2_dpy->gbm_dri->num_visuals;
626 unsigned int format_count[num_visuals];
627 unsigned int config_count = 0;
628
629 memset(format_count, 0, num_visuals * sizeof(unsigned int));
630
631 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
632 unsigned int red, green, blue, alpha;
633
634 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
635 __DRI_ATTRIB_RED_MASK, &red);
636 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
637 __DRI_ATTRIB_GREEN_MASK, &green);
638 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
639 __DRI_ATTRIB_BLUE_MASK, &blue);
640 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
641 __DRI_ATTRIB_ALPHA_MASK, &alpha);
642
643 for (unsigned j = 0; j < num_visuals; j++) {
644 struct dri2_egl_config *dri2_conf;
645
646 if (visuals[j].rgba_masks.red != red ||
647 visuals[j].rgba_masks.green != green ||
648 visuals[j].rgba_masks.blue != blue ||
649 visuals[j].rgba_masks.alpha != alpha)
650 continue;
651
652 const EGLint attr_list[] = {
653 EGL_NATIVE_VISUAL_ID, visuals[j].gbm_format,
654 EGL_NONE,
655 };
656
657 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
658 config_count + 1, EGL_WINDOW_BIT, attr_list, NULL);
659 if (dri2_conf) {
660 if (dri2_conf->base.ConfigID == config_count + 1)
661 config_count++;
662 format_count[j]++;
663 }
664 }
665 }
666
667 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
668 if (!format_count[i]) {
669 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
670 visuals[i].gbm_format);
671 }
672 }
673
674 return (config_count != 0);
675 }
676
677 static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
678 .authenticate = dri2_drm_authenticate,
679 .create_window_surface = dri2_drm_create_window_surface,
680 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
681 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
682 .destroy_surface = dri2_drm_destroy_surface,
683 .create_image = dri2_drm_create_image_khr,
684 .swap_buffers = dri2_drm_swap_buffers,
685 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
686 .swap_buffers_region = dri2_fallback_swap_buffers_region,
687 .set_damage_region = dri2_fallback_set_damage_region,
688 .post_sub_buffer = dri2_fallback_post_sub_buffer,
689 .copy_buffers = dri2_fallback_copy_buffers,
690 .query_buffer_age = dri2_drm_query_buffer_age,
691 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
692 .get_sync_values = dri2_fallback_get_sync_values,
693 .get_dri_drawable = dri2_surface_get_dri_drawable,
694 };
695
696 EGLBoolean
697 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
698 {
699 struct dri2_egl_display *dri2_dpy;
700 struct gbm_device *gbm;
701 const char *err;
702
703 /* Not supported yet */
704 if (disp->Options.ForceSoftware)
705 return EGL_FALSE;
706
707 loader_set_logger(_eglLog);
708
709 dri2_dpy = calloc(1, sizeof *dri2_dpy);
710 if (!dri2_dpy)
711 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
712
713 dri2_dpy->fd = -1;
714 disp->DriverData = (void *) dri2_dpy;
715
716 gbm = disp->PlatformDisplay;
717 if (gbm == NULL) {
718 char buf[64];
719 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
720 if (n != -1 && n < sizeof(buf))
721 dri2_dpy->fd = loader_open_device(buf);
722 gbm = gbm_create_device(dri2_dpy->fd);
723 if (gbm == NULL) {
724 err = "DRI2: failed to create gbm device";
725 goto cleanup;
726 }
727 dri2_dpy->own_device = true;
728 } else {
729 dri2_dpy->fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3);
730 if (dri2_dpy->fd < 0) {
731 err = "DRI2: failed to fcntl() existing gbm device";
732 goto cleanup;
733 }
734 }
735
736 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
737 err = "DRI2: gbm device using incorrect/incompatible backend";
738 goto cleanup;
739 }
740
741 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
742 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->driver_name);
743
744 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
745 dri2_dpy->core = dri2_dpy->gbm_dri->core;
746 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
747 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
748 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
749
750 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
751 dri2_dpy->gbm_dri->lookup_user_data = disp;
752
753 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
754 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
755 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
756 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
757 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
758 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
759
760 dri2_dpy->gbm_dri->base.surface_lock_front_buffer = lock_front_buffer;
761 dri2_dpy->gbm_dri->base.surface_release_buffer = release_buffer;
762 dri2_dpy->gbm_dri->base.surface_has_free_buffers = has_free_buffers;
763
764 if (!dri2_setup_extensions(disp)) {
765 err = "DRI2: failed to find required DRI extensions";
766 goto cleanup;
767 }
768
769 dri2_setup_screen(disp);
770
771 if (!drm_add_configs_for_visuals(drv, disp)) {
772 err = "DRI2: failed to add configs";
773 goto cleanup;
774 }
775
776 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
777 if (dri2_dpy->dri2)
778 disp->Extensions.EXT_buffer_age = EGL_TRUE;
779
780 #ifdef HAVE_WAYLAND_PLATFORM
781 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
782 #endif
783 dri2_set_WL_bind_wayland_display(drv, disp);
784
785 /* Fill vtbl last to prevent accidentally calling virtual function during
786 * initialization.
787 */
788 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
789
790 return EGL_TRUE;
791
792 cleanup:
793 dri2_display_destroy(disp);
794 return _eglError(EGL_NOT_INITIALIZED, err);
795 }
796
797 void
798 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy)
799 {
800 if (dri2_dpy->own_device)
801 gbm_device_destroy(&dri2_dpy->gbm_dri->base);
802 }