egl/drm: check for dri2_dpy->flush before using the flush extension
[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 int age = 0;
219 unsigned i;
220
221 if (dri2_surf->back == NULL) {
222 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
223 if (!dri2_surf->color_buffers[i].locked &&
224 dri2_surf->color_buffers[i].age >= age) {
225 dri2_surf->back = &dri2_surf->color_buffers[i];
226 age = dri2_surf->color_buffers[i].age;
227 }
228 }
229 }
230
231 if (dri2_surf->back == NULL)
232 return -1;
233 if (dri2_surf->back->bo == NULL) {
234 if (surf->base.modifiers)
235 dri2_surf->back->bo = gbm_bo_create_with_modifiers(&dri2_dpy->gbm_dri->base,
236 surf->base.width, surf->base.height,
237 surf->base.format,
238 surf->base.modifiers,
239 surf->base.count);
240 else
241 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
242 surf->base.width,
243 surf->base.height,
244 surf->base.format,
245 surf->base.flags);
246
247 }
248 if (dri2_surf->back->bo == NULL)
249 return -1;
250
251 return 0;
252 }
253
254 static int
255 get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
256 {
257 struct dri2_egl_display *dri2_dpy =
258 dri2_egl_display(dri2_surf->base.Resource.Display);
259 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
260
261 if (dri2_surf->current == NULL) {
262 assert(!dri2_surf->color_buffers[0].locked);
263 dri2_surf->current = &dri2_surf->color_buffers[0];
264 }
265
266 if (dri2_surf->current->bo == NULL)
267 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
268 surf->base.width, surf->base.height,
269 surf->base.format, surf->base.flags);
270 if (dri2_surf->current->bo == NULL)
271 return -1;
272
273 return 0;
274 }
275
276 static void
277 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
278 {
279 struct dri2_egl_display *dri2_dpy =
280 dri2_egl_display(dri2_surf->base.Resource.Display);
281 struct gbm_dri_bo *bo;
282 int name, pitch;
283
284 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
285
286 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
287 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
288
289 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
290 buffer->name = name;
291 buffer->pitch = pitch;
292 buffer->cpp = 4;
293 buffer->flags = 0;
294 }
295
296 static int
297 get_aux_bo(struct dri2_egl_surface *dri2_surf,
298 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
299 {
300 struct dri2_egl_display *dri2_dpy =
301 dri2_egl_display(dri2_surf->base.Resource.Display);
302 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
303
304 if (b == NULL) {
305 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
306 attachment, format,
307 dri2_surf->base.Width,
308 dri2_surf->base.Height);
309 dri2_surf->dri_buffers[attachment] = b;
310 }
311 if (b == NULL)
312 return -1;
313
314 memcpy(buffer, b, sizeof *buffer);
315
316 return 0;
317 }
318
319 static __DRIbuffer *
320 dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
321 int *width, int *height,
322 unsigned int *attachments, int count,
323 int *out_count, void *loaderPrivate)
324 {
325 struct dri2_egl_surface *dri2_surf = loaderPrivate;
326 int i, j;
327
328 dri2_surf->buffer_count = 0;
329 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
330 assert(attachments[i] < __DRI_BUFFER_COUNT);
331 assert(dri2_surf->buffer_count < 5);
332
333 switch (attachments[i]) {
334 case __DRI_BUFFER_BACK_LEFT:
335 if (get_back_bo(dri2_surf) < 0) {
336 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
337 return NULL;
338 }
339 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
340 break;
341 default:
342 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
343 &dri2_surf->buffers[j]) < 0) {
344 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
345 return NULL;
346 }
347 break;
348 }
349 }
350
351 *out_count = j;
352 if (j == 0)
353 return NULL;
354
355 *width = dri2_surf->base.Width;
356 *height = dri2_surf->base.Height;
357
358 return dri2_surf->buffers;
359 }
360
361 static __DRIbuffer *
362 dri2_drm_get_buffers(__DRIdrawable * driDrawable,
363 int *width, int *height,
364 unsigned int *attachments, int count,
365 int *out_count, void *loaderPrivate)
366 {
367 unsigned int *attachments_with_format;
368 __DRIbuffer *buffer;
369 const unsigned int format = 32;
370 int i;
371
372 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
373 if (!attachments_with_format) {
374 *out_count = 0;
375 return NULL;
376 }
377
378 for (i = 0; i < count; ++i) {
379 attachments_with_format[2*i] = attachments[i];
380 attachments_with_format[2*i + 1] = format;
381 }
382
383 buffer =
384 dri2_drm_get_buffers_with_format(driDrawable,
385 width, height,
386 attachments_with_format, count,
387 out_count, loaderPrivate);
388
389 free(attachments_with_format);
390
391 return buffer;
392 }
393
394 static int
395 dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
396 unsigned int format,
397 uint32_t *stamp,
398 void *loaderPrivate,
399 uint32_t buffer_mask,
400 struct __DRIimageList *buffers)
401 {
402 struct dri2_egl_surface *dri2_surf = loaderPrivate;
403 struct gbm_dri_bo *bo;
404
405 if (get_back_bo(dri2_surf) < 0)
406 return 0;
407
408 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
409 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
410 buffers->back = bo->image;
411
412 return 1;
413 }
414
415 static void
416 dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
417 {
418 (void) driDrawable;
419 (void) loaderPrivate;
420 }
421
422 static EGLBoolean
423 dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
424 {
425 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
426 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
427 unsigned i;
428
429 if (!dri2_dpy->flush) {
430 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
431 } else {
432 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
433 if (dri2_surf->current)
434 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
435 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
436 if (dri2_surf->color_buffers[i].age > 0)
437 dri2_surf->color_buffers[i].age++;
438
439 /* Make sure we have a back buffer in case we're swapping without
440 * ever rendering. */
441 if (get_back_bo(dri2_surf) < 0) {
442 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
443 return EGL_FALSE;
444 }
445
446 dri2_surf->current = dri2_surf->back;
447 dri2_surf->current->age = 1;
448 dri2_surf->back = NULL;
449 }
450
451 dri2_flush_drawable_for_swapbuffers(disp, draw);
452 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
453 }
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 0;
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 if (!_eglInitImage(&dri2_img->base, disp)) {
487 free(dri2_img);
488 return NULL;
489 }
490
491 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
492 if (dri2_img->dri_image == NULL) {
493 free(dri2_img);
494 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
495 return NULL;
496 }
497
498 return &dri2_img->base;
499 }
500
501 static _EGLImage *
502 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
503 _EGLContext *ctx, EGLenum target,
504 EGLClientBuffer buffer, const EGLint *attr_list)
505 {
506 (void) drv;
507
508 switch (target) {
509 case EGL_NATIVE_PIXMAP_KHR:
510 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
511 default:
512 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
513 }
514 }
515
516 static int
517 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
518 {
519 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
520
521 return drmAuthMagic(dri2_dpy->fd, id);
522 }
523
524 static void
525 swrast_put_image2(__DRIdrawable *driDrawable,
526 int op,
527 int x,
528 int y,
529 int width,
530 int height,
531 int stride,
532 char *data,
533 void *loaderPrivate)
534 {
535 struct dri2_egl_surface *dri2_surf = loaderPrivate;
536 int internal_stride, i;
537 struct gbm_dri_bo *bo;
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 if (gbm_dri_bo_map_dumb(bo) == NULL)
548 return;
549
550 internal_stride = bo->base.stride;
551
552 for (i = 0; i < height; i++) {
553 memcpy(bo->map + (x + i) * internal_stride + y,
554 data + i * stride, stride);
555 }
556
557 gbm_dri_bo_unmap_dumb(bo);
558 }
559
560 static void
561 swrast_get_image(__DRIdrawable *driDrawable,
562 int x,
563 int y,
564 int width,
565 int height,
566 char *data,
567 void *loaderPrivate)
568 {
569 struct dri2_egl_surface *dri2_surf = loaderPrivate;
570 int internal_stride, stride, i;
571 struct gbm_dri_bo *bo;
572
573 if (get_swrast_front_bo(dri2_surf) < 0)
574 return;
575
576 bo = gbm_dri_bo(dri2_surf->current->bo);
577 if (gbm_dri_bo_map_dumb(bo) == NULL)
578 return;
579
580 internal_stride = bo->base.stride;
581 stride = width * 4;
582
583 for (i = 0; i < height; i++) {
584 memcpy(data + i * stride,
585 bo->map + (x + i) * internal_stride + y, stride);
586 }
587
588 gbm_dri_bo_unmap_dumb(bo);
589 }
590
591 static EGLBoolean
592 drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
593 {
594 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
595 static const struct {
596 int format;
597 unsigned int red_mask;
598 unsigned int alpha_mask;
599 } visuals[] = {
600 { GBM_FORMAT_XRGB2101010, 0x3ff00000, 0x00000000 },
601 { GBM_FORMAT_ARGB2101010, 0x3ff00000, 0xc0000000 },
602 { GBM_FORMAT_XRGB8888, 0x00ff0000, 0x00000000 },
603 { GBM_FORMAT_ARGB8888, 0x00ff0000, 0xff000000 },
604 { GBM_FORMAT_RGB565, 0x0000f800, 0x00000000 },
605 };
606 EGLint attr_list[] = {
607 EGL_NATIVE_VISUAL_ID, 0,
608 EGL_NONE,
609 };
610 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
611 unsigned int count, i, j;
612
613 count = 0;
614 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
615 unsigned int red, alpha;
616
617 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
618 __DRI_ATTRIB_RED_MASK, &red);
619 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
620 __DRI_ATTRIB_ALPHA_MASK, &alpha);
621
622 for (j = 0; j < ARRAY_SIZE(visuals); j++) {
623 struct dri2_egl_config *dri2_conf;
624
625 if (visuals[j].red_mask != red || visuals[j].alpha_mask != alpha)
626 continue;
627
628 attr_list[1] = visuals[j].format;
629
630 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
631 count + 1, EGL_WINDOW_BIT, attr_list, NULL);
632 if (dri2_conf) {
633 count++;
634 format_count[j]++;
635 }
636 }
637 }
638
639 for (i = 0; i < ARRAY_SIZE(format_count); i++) {
640 if (!format_count[i]) {
641 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
642 visuals[i].format);
643 }
644 }
645
646 return (count != 0);
647 }
648
649 static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
650 .authenticate = dri2_drm_authenticate,
651 .create_window_surface = dri2_drm_create_window_surface,
652 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
653 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
654 .destroy_surface = dri2_drm_destroy_surface,
655 .create_image = dri2_drm_create_image_khr,
656 .swap_interval = dri2_fallback_swap_interval,
657 .swap_buffers = dri2_drm_swap_buffers,
658 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
659 .swap_buffers_region = dri2_fallback_swap_buffers_region,
660 .post_sub_buffer = dri2_fallback_post_sub_buffer,
661 .copy_buffers = dri2_fallback_copy_buffers,
662 .query_buffer_age = dri2_drm_query_buffer_age,
663 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
664 .get_sync_values = dri2_fallback_get_sync_values,
665 .get_dri_drawable = dri2_surface_get_dri_drawable,
666 };
667
668 EGLBoolean
669 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
670 {
671 struct dri2_egl_display *dri2_dpy;
672 struct gbm_device *gbm;
673 const char *err;
674 int fd = -1;
675
676 loader_set_logger(_eglLog);
677
678 dri2_dpy = calloc(1, sizeof *dri2_dpy);
679 if (!dri2_dpy)
680 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
681
682 dri2_dpy->fd = -1;
683 disp->DriverData = (void *) dri2_dpy;
684
685 gbm = disp->PlatformDisplay;
686 if (gbm == NULL) {
687 char buf[64];
688 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
689 if (n != -1 && n < sizeof(buf))
690 fd = loader_open_device(buf);
691 if (fd < 0)
692 fd = loader_open_device("/dev/dri/card0");
693 gbm = gbm_create_device(fd);
694 if (gbm == NULL) {
695 err = "DRI2: failed to create gbm device";
696 goto cleanup;
697 }
698 dri2_dpy->own_device = 1;
699 } else {
700 fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3);
701 if (fd < 0) {
702 err = "DRI2: failed to fcntl() existing gbm device";
703 goto cleanup;
704 }
705 }
706
707 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
708 err = "DRI2: gbm device using incorrect/incompatible backend";
709 goto cleanup;
710 }
711
712 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
713 dri2_dpy->fd = fd;
714 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->driver_name);
715
716 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
717 dri2_dpy->core = dri2_dpy->gbm_dri->core;
718 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
719 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
720 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
721
722 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
723 dri2_dpy->gbm_dri->lookup_user_data = disp;
724
725 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
726 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
727 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
728 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
729 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
730 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
731
732 dri2_dpy->gbm_dri->base.surface_lock_front_buffer = lock_front_buffer;
733 dri2_dpy->gbm_dri->base.surface_release_buffer = release_buffer;
734 dri2_dpy->gbm_dri->base.surface_has_free_buffers = has_free_buffers;
735
736 if (!dri2_setup_extensions(disp)) {
737 err = "DRI2: failed to find required DRI extensions";
738 goto cleanup;
739 }
740
741 dri2_setup_screen(disp);
742
743 if (!drm_add_configs_for_visuals(drv, disp)) {
744 err = "DRI2: failed to add configs";
745 goto cleanup;
746 }
747
748 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
749 if (dri2_dpy->dri2)
750 disp->Extensions.EXT_buffer_age = EGL_TRUE;
751
752 #ifdef HAVE_WAYLAND_PLATFORM
753 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
754 #endif
755 dri2_set_WL_bind_wayland_display(drv, disp);
756
757 /* Fill vtbl last to prevent accidentally calling virtual function during
758 * initialization.
759 */
760 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
761
762 return EGL_TRUE;
763
764 cleanup:
765 dri2_display_destroy(disp);
766 return _eglError(EGL_NOT_INITIALIZED, err);
767 }