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