egl/drm: Format code in platform_drm.c according to style guide.
[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 dri2_surf->buffer_count = 0;
327 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
328 assert(attachments[i] < __DRI_BUFFER_COUNT);
329 assert(dri2_surf->buffer_count < 5);
330
331 switch (attachments[i]) {
332 case __DRI_BUFFER_BACK_LEFT:
333 if (get_back_bo(dri2_surf) < 0) {
334 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
335 return NULL;
336 }
337 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
338 break;
339 default:
340 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
341 &dri2_surf->buffers[j]) < 0) {
342 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
343 return NULL;
344 }
345 break;
346 }
347 }
348
349 *out_count = j;
350 if (j == 0)
351 return NULL;
352
353 *width = dri2_surf->base.Width;
354 *height = dri2_surf->base.Height;
355
356 return dri2_surf->buffers;
357 }
358
359 static __DRIbuffer *
360 dri2_drm_get_buffers(__DRIdrawable * driDrawable,
361 int *width, int *height,
362 unsigned int *attachments, int count,
363 int *out_count, void *loaderPrivate)
364 {
365 unsigned int *attachments_with_format;
366 __DRIbuffer *buffer;
367 const unsigned int format = 32;
368
369 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
370 if (!attachments_with_format) {
371 *out_count = 0;
372 return NULL;
373 }
374
375 for (int i = 0; i < count; ++i) {
376 attachments_with_format[2*i] = attachments[i];
377 attachments_with_format[2*i + 1] = format;
378 }
379
380 buffer =
381 dri2_drm_get_buffers_with_format(driDrawable,
382 width, height,
383 attachments_with_format, count,
384 out_count, loaderPrivate);
385
386 free(attachments_with_format);
387
388 return buffer;
389 }
390
391 static int
392 dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
393 unsigned int format,
394 uint32_t *stamp,
395 void *loaderPrivate,
396 uint32_t buffer_mask,
397 struct __DRIimageList *buffers)
398 {
399 struct dri2_egl_surface *dri2_surf = loaderPrivate;
400 struct gbm_dri_bo *bo;
401
402 if (get_back_bo(dri2_surf) < 0)
403 return 0;
404
405 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
406 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
407 buffers->back = bo->image;
408
409 return 1;
410 }
411
412 static void
413 dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
414 {
415 (void) driDrawable;
416 (void) loaderPrivate;
417 }
418
419 static EGLBoolean
420 dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
421 {
422 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
423 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
424
425 if (!dri2_dpy->flush) {
426 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
427 return EGL_TRUE;
428 }
429
430 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
431 if (dri2_surf->current)
432 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
433 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
434 if (dri2_surf->color_buffers[i].age > 0)
435 dri2_surf->color_buffers[i].age++;
436
437 /* Make sure we have a back buffer in case we're swapping without
438 * ever rendering. */
439 if (get_back_bo(dri2_surf) < 0)
440 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
441
442 dri2_surf->current = dri2_surf->back;
443 dri2_surf->current->age = 1;
444 dri2_surf->back = NULL;
445 }
446
447 dri2_flush_drawable_for_swapbuffers(disp, draw);
448 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
449
450 return EGL_TRUE;
451 }
452
453 static EGLint
454 dri2_drm_query_buffer_age(_EGLDriver *drv,
455 _EGLDisplay *disp, _EGLSurface *surface)
456 {
457 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
458
459 if (get_back_bo(dri2_surf) < 0) {
460 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
461 return -1;
462 }
463
464 return dri2_surf->back->age;
465 }
466
467 static _EGLImage *
468 dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
469 EGLClientBuffer buffer, const EGLint *attr_list)
470 {
471 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
472 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
473 struct dri2_egl_image *dri2_img;
474
475 dri2_img = malloc(sizeof *dri2_img);
476 if (!dri2_img) {
477 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
478 return NULL;
479 }
480
481 _eglInitImage(&dri2_img->base, disp);
482
483 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
484 if (dri2_img->dri_image == NULL) {
485 free(dri2_img);
486 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
487 return NULL;
488 }
489
490 return &dri2_img->base;
491 }
492
493 static _EGLImage *
494 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
495 _EGLContext *ctx, EGLenum target,
496 EGLClientBuffer buffer, const EGLint *attr_list)
497 {
498 (void) drv;
499
500 switch (target) {
501 case EGL_NATIVE_PIXMAP_KHR:
502 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
503 default:
504 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
505 }
506 }
507
508 static int
509 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
510 {
511 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
512
513 return drmAuthMagic(dri2_dpy->fd, id);
514 }
515
516 static void
517 swrast_put_image2(__DRIdrawable *driDrawable,
518 int op,
519 int x,
520 int y,
521 int width,
522 int height,
523 int stride,
524 char *data,
525 void *loaderPrivate)
526 {
527 struct dri2_egl_surface *dri2_surf = loaderPrivate;
528 int internal_stride;
529 struct gbm_dri_bo *bo;
530
531 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
532 op != __DRI_SWRAST_IMAGE_OP_SWAP)
533 return;
534
535 if (get_swrast_front_bo(dri2_surf) < 0)
536 return;
537
538 bo = gbm_dri_bo(dri2_surf->current->bo);
539 if (gbm_dri_bo_map_dumb(bo) == NULL)
540 return;
541
542 internal_stride = bo->base.stride;
543
544 for (int i = 0; i < height; i++) {
545 memcpy(bo->map + (x + i) * internal_stride + y,
546 data + i * stride, stride);
547 }
548
549 gbm_dri_bo_unmap_dumb(bo);
550 }
551
552 static void
553 swrast_get_image(__DRIdrawable *driDrawable,
554 int x,
555 int y,
556 int width,
557 int height,
558 char *data,
559 void *loaderPrivate)
560 {
561 struct dri2_egl_surface *dri2_surf = loaderPrivate;
562 int internal_stride, stride;
563 struct gbm_dri_bo *bo;
564
565 if (get_swrast_front_bo(dri2_surf) < 0)
566 return;
567
568 bo = gbm_dri_bo(dri2_surf->current->bo);
569 if (gbm_dri_bo_map_dumb(bo) == NULL)
570 return;
571
572 internal_stride = bo->base.stride;
573 stride = width * 4;
574
575 for (int i = 0; i < height; i++) {
576 memcpy(data + i * stride,
577 bo->map + (x + i) * internal_stride + y, stride);
578 }
579
580 gbm_dri_bo_unmap_dumb(bo);
581 }
582
583 static EGLBoolean
584 drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
585 {
586 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
587 static const struct {
588 int format;
589 unsigned int red_mask;
590 unsigned int alpha_mask;
591 } visuals[] = {
592 { GBM_FORMAT_XRGB2101010, 0x3ff00000, 0x00000000 },
593 { GBM_FORMAT_ARGB2101010, 0x3ff00000, 0xc0000000 },
594 { GBM_FORMAT_XRGB8888, 0x00ff0000, 0x00000000 },
595 { GBM_FORMAT_ARGB8888, 0x00ff0000, 0xff000000 },
596 { GBM_FORMAT_RGB565, 0x0000f800, 0x00000000 },
597 };
598
599 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
600 unsigned int config_count = 0;
601
602 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
603 unsigned int red, alpha;
604
605 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
606 __DRI_ATTRIB_RED_MASK, &red);
607 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
608 __DRI_ATTRIB_ALPHA_MASK, &alpha);
609
610 for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
611 struct dri2_egl_config *dri2_conf;
612
613 if (visuals[j].red_mask != red || visuals[j].alpha_mask != alpha)
614 continue;
615
616 const EGLint attr_list[] = {
617 EGL_NATIVE_VISUAL_ID, visuals[j].format,
618 EGL_NONE,
619 };
620
621 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
622 config_count + 1, EGL_WINDOW_BIT, attr_list, NULL);
623 if (dri2_conf) {
624 if (dri2_conf->base.ConfigID == config_count + 1)
625 config_count++;
626 format_count[j]++;
627 }
628 }
629 }
630
631 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
632 if (!format_count[i]) {
633 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
634 visuals[i].format);
635 }
636 }
637
638 return (config_count != 0);
639 }
640
641 static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
642 .authenticate = dri2_drm_authenticate,
643 .create_window_surface = dri2_drm_create_window_surface,
644 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
645 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
646 .destroy_surface = dri2_drm_destroy_surface,
647 .create_image = dri2_drm_create_image_khr,
648 .swap_interval = dri2_fallback_swap_interval,
649 .swap_buffers = dri2_drm_swap_buffers,
650 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
651 .swap_buffers_region = dri2_fallback_swap_buffers_region,
652 .set_damage_region = dri2_fallback_set_damage_region,
653 .post_sub_buffer = dri2_fallback_post_sub_buffer,
654 .copy_buffers = dri2_fallback_copy_buffers,
655 .query_buffer_age = dri2_drm_query_buffer_age,
656 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
657 .get_sync_values = dri2_fallback_get_sync_values,
658 .get_dri_drawable = dri2_surface_get_dri_drawable,
659 };
660
661 EGLBoolean
662 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
663 {
664 struct dri2_egl_display *dri2_dpy;
665 struct gbm_device *gbm;
666 const char *err;
667
668 loader_set_logger(_eglLog);
669
670 dri2_dpy = calloc(1, sizeof *dri2_dpy);
671 if (!dri2_dpy)
672 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
673
674 dri2_dpy->fd = -1;
675 disp->DriverData = (void *) dri2_dpy;
676
677 gbm = disp->PlatformDisplay;
678 if (gbm == NULL) {
679 char buf[64];
680 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
681 if (n != -1 && n < sizeof(buf))
682 dri2_dpy->fd = loader_open_device(buf);
683 if (dri2_dpy->fd < 0)
684 dri2_dpy->fd = loader_open_device("/dev/dri/card0");
685 gbm = gbm_create_device(dri2_dpy->fd);
686 if (gbm == NULL) {
687 err = "DRI2: failed to create gbm device";
688 goto cleanup;
689 }
690 dri2_dpy->own_device = true;
691 } else {
692 dri2_dpy->fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3);
693 if (dri2_dpy->fd < 0) {
694 err = "DRI2: failed to fcntl() existing gbm device";
695 goto cleanup;
696 }
697 }
698
699 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
700 err = "DRI2: gbm device using incorrect/incompatible backend";
701 goto cleanup;
702 }
703
704 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
705 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->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->swrast = dri2_dpy->gbm_dri->swrast;
711 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
712
713 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
714 dri2_dpy->gbm_dri->lookup_user_data = disp;
715
716 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
717 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
718 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
719 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
720 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
721 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
722
723 dri2_dpy->gbm_dri->base.surface_lock_front_buffer = lock_front_buffer;
724 dri2_dpy->gbm_dri->base.surface_release_buffer = release_buffer;
725 dri2_dpy->gbm_dri->base.surface_has_free_buffers = has_free_buffers;
726
727 if (!dri2_setup_extensions(disp)) {
728 err = "DRI2: failed to find required DRI extensions";
729 goto cleanup;
730 }
731
732 dri2_setup_screen(disp);
733
734 if (!drm_add_configs_for_visuals(drv, disp)) {
735 err = "DRI2: failed to add configs";
736 goto cleanup;
737 }
738
739 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
740 if (dri2_dpy->dri2)
741 disp->Extensions.EXT_buffer_age = EGL_TRUE;
742
743 #ifdef HAVE_WAYLAND_PLATFORM
744 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
745 #endif
746 dri2_set_WL_bind_wayland_display(drv, disp);
747
748 /* Fill vtbl last to prevent accidentally calling virtual function during
749 * initialization.
750 */
751 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
752
753 return EGL_TRUE;
754
755 cleanup:
756 dri2_display_destroy(disp);
757 return _eglError(EGL_NOT_INITIALIZED, err);
758 }