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