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