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