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