egl/wayland: add missing newline between functions
[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 "util/os_file.h"
40
41 #include "egl_dri2.h"
42 #include "egl_dri2_fallbacks.h"
43 #include "loader.h"
44
45 static struct gbm_bo *
46 lock_front_buffer(struct gbm_surface *_surf)
47 {
48 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
49 struct dri2_egl_surface *dri2_surf = surf->dri_private;
50 struct gbm_dri_device *device = gbm_dri_device(_surf->gbm);
51 struct gbm_bo *bo;
52
53 if (dri2_surf->current == NULL) {
54 _eglError(EGL_BAD_SURFACE, "no front buffer");
55 return NULL;
56 }
57
58 bo = dri2_surf->current->bo;
59
60 if (device->dri2) {
61 dri2_surf->current->locked = true;
62 dri2_surf->current = NULL;
63 }
64
65 return bo;
66 }
67
68 static void
69 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
70 {
71 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
72 struct dri2_egl_surface *dri2_surf = surf->dri_private;
73
74 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
75 if (dri2_surf->color_buffers[i].bo == bo) {
76 dri2_surf->color_buffers[i].locked = false;
77 break;
78 }
79 }
80 }
81
82 static int
83 has_free_buffers(struct gbm_surface *_surf)
84 {
85 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
86 struct dri2_egl_surface *dri2_surf = surf->dri_private;
87
88 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
89 if (!dri2_surf->color_buffers[i].locked)
90 return 1;
91
92 return 0;
93 }
94
95 static bool
96 dri2_drm_config_is_compatible(struct dri2_egl_display *dri2_dpy,
97 const __DRIconfig *config,
98 struct gbm_surface *surface)
99 {
100 const struct gbm_dri_visual *visual = NULL;
101 int shifts[4];
102 unsigned int sizes[4];
103 bool is_float;
104 int i;
105
106 /* Check that the EGLConfig being used to render to the surface is
107 * compatible with the surface format. Since mixing ARGB and XRGB of
108 * otherwise-compatible formats is relatively common, explicitly allow
109 * this.
110 */
111 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
112
113 dri2_get_render_type_float(dri2_dpy->core, config, &is_float);
114
115 for (i = 0; i < dri2_dpy->gbm_dri->num_visuals; i++) {
116 visual = &dri2_dpy->gbm_dri->visual_table[i];
117 if (visual->gbm_format == surface->format)
118 break;
119 }
120
121 if (i == dri2_dpy->gbm_dri->num_visuals)
122 return false;
123
124 if (shifts[0] != visual->rgba_shifts.red ||
125 shifts[1] != visual->rgba_shifts.green ||
126 shifts[2] != visual->rgba_shifts.blue ||
127 (shifts[3] > -1 && visual->rgba_shifts.alpha > -1 &&
128 shifts[3] != visual->rgba_shifts.alpha) ||
129 sizes[0] != visual->rgba_sizes.red ||
130 sizes[1] != visual->rgba_sizes.green ||
131 sizes[2] != visual->rgba_sizes.blue ||
132 (sizes[3] > 0 && visual->rgba_sizes.alpha > 0 &&
133 sizes[3] != visual->rgba_sizes.alpha) ||
134 is_float != visual->is_float) {
135 return false;
136 }
137
138 return true;
139 }
140
141 static _EGLSurface *
142 dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
143 _EGLConfig *conf, void *native_surface,
144 const EGLint *attrib_list)
145 {
146 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
147 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
148 struct dri2_egl_surface *dri2_surf;
149 struct gbm_surface *surface = native_surface;
150 struct gbm_dri_surface *surf;
151 const __DRIconfig *config;
152
153 (void) drv;
154
155 dri2_surf = calloc(1, sizeof *dri2_surf);
156 if (!dri2_surf) {
157 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
158 return NULL;
159 }
160
161 if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf,
162 attrib_list, false, native_surface))
163 goto cleanup_surf;
164
165 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
166 dri2_surf->base.GLColorspace);
167
168 if (!config) {
169 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
170 goto cleanup_surf;
171 }
172
173 if (!dri2_drm_config_is_compatible(dri2_dpy, config, surface)) {
174 _eglError(EGL_BAD_MATCH, "EGL config not compatible with GBM format");
175 goto cleanup_surf;
176 }
177
178 surf = gbm_dri_surface(surface);
179 dri2_surf->gbm_surf = surf;
180 dri2_surf->base.Width = surf->base.width;
181 dri2_surf->base.Height = surf->base.height;
182 surf->dri_private = dri2_surf;
183
184 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf->gbm_surf))
185 goto cleanup_surf;
186
187 return &dri2_surf->base;
188
189 cleanup_surf:
190 free(dri2_surf);
191
192 return NULL;
193 }
194
195 static _EGLSurface *
196 dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
197 _EGLConfig *conf, void *native_window,
198 const EGLint *attrib_list)
199 {
200 /* From the EGL_MESA_platform_gbm spec, version 5:
201 *
202 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
203 * that belongs to the GBM platform. Any such call fails and generates
204 * EGL_BAD_PARAMETER.
205 */
206 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM");
207 return NULL;
208 }
209
210 static EGLBoolean
211 dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
212 {
213 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
214 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
215
216 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
217
218 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
219 if (dri2_surf->color_buffers[i].bo)
220 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
221 }
222
223 dri2_egl_surface_free_local_buffers(dri2_surf);
224
225 dri2_fini_surface(surf);
226 free(surf);
227
228 return EGL_TRUE;
229 }
230
231 static int
232 get_back_bo(struct dri2_egl_surface *dri2_surf)
233 {
234 struct dri2_egl_display *dri2_dpy =
235 dri2_egl_display(dri2_surf->base.Resource.Display);
236 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
237 int age = 0;
238
239 if (dri2_surf->back == NULL) {
240 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
241 if (!dri2_surf->color_buffers[i].locked &&
242 dri2_surf->color_buffers[i].age >= age) {
243 dri2_surf->back = &dri2_surf->color_buffers[i];
244 age = dri2_surf->color_buffers[i].age;
245 }
246 }
247 }
248
249 if (dri2_surf->back == NULL)
250 return -1;
251 if (dri2_surf->back->bo == NULL) {
252 if (surf->base.modifiers)
253 dri2_surf->back->bo = gbm_bo_create_with_modifiers(&dri2_dpy->gbm_dri->base,
254 surf->base.width,
255 surf->base.height,
256 surf->base.format,
257 surf->base.modifiers,
258 surf->base.count);
259 else
260 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
261 surf->base.width,
262 surf->base.height,
263 surf->base.format,
264 surf->base.flags);
265
266 }
267 if (dri2_surf->back->bo == NULL)
268 return -1;
269
270 return 0;
271 }
272
273 static int
274 get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
275 {
276 struct dri2_egl_display *dri2_dpy =
277 dri2_egl_display(dri2_surf->base.Resource.Display);
278 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
279
280 if (dri2_surf->current == NULL) {
281 assert(!dri2_surf->color_buffers[0].locked);
282 dri2_surf->current = &dri2_surf->color_buffers[0];
283 }
284
285 if (dri2_surf->current->bo == NULL)
286 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
287 surf->base.width, surf->base.height,
288 surf->base.format, surf->base.flags);
289 if (dri2_surf->current->bo == NULL)
290 return -1;
291
292 return 0;
293 }
294
295 static void
296 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
297 {
298 struct dri2_egl_display *dri2_dpy =
299 dri2_egl_display(dri2_surf->base.Resource.Display);
300 struct gbm_dri_bo *bo;
301 int name, pitch;
302
303 bo = gbm_dri_bo(dri2_surf->back->bo);
304
305 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
306 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
307
308 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
309 buffer->name = name;
310 buffer->pitch = pitch;
311 buffer->cpp = 4;
312 buffer->flags = 0;
313 }
314
315 static __DRIbuffer *
316 dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
317 int *width, int *height,
318 unsigned int *attachments, int count,
319 int *out_count, void *loaderPrivate)
320 {
321 struct dri2_egl_surface *dri2_surf = loaderPrivate;
322 int i, j;
323
324 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
325 __DRIbuffer *local;
326
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 local = dri2_egl_surface_alloc_local_buffer(dri2_surf, attachments[i],
340 attachments[i + 1]);
341
342 if (!local) {
343 _eglError(EGL_BAD_ALLOC, "failed to allocate local buffer");
344 return NULL;
345 }
346 dri2_surf->buffers[j] = *local;
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
371 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
372 if (!attachments_with_format) {
373 *out_count = 0;
374 return NULL;
375 }
376
377 for (int i = 0; i < count; ++i) {
378 attachments_with_format[2*i] = attachments[i];
379 attachments_with_format[2*i + 1] = format;
380 }
381
382 buffer =
383 dri2_drm_get_buffers_with_format(driDrawable,
384 width, height,
385 attachments_with_format, count,
386 out_count, loaderPrivate);
387
388 free(attachments_with_format);
389
390 return buffer;
391 }
392
393 static int
394 dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
395 unsigned int format,
396 uint32_t *stamp,
397 void *loaderPrivate,
398 uint32_t buffer_mask,
399 struct __DRIimageList *buffers)
400 {
401 struct dri2_egl_surface *dri2_surf = loaderPrivate;
402 struct gbm_dri_bo *bo;
403
404 if (get_back_bo(dri2_surf) < 0)
405 return 0;
406
407 bo = gbm_dri_bo(dri2_surf->back->bo);
408 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
409 buffers->back = bo->image;
410
411 return 1;
412 }
413
414 static void
415 dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
416 {
417 (void) driDrawable;
418 (void) loaderPrivate;
419 }
420
421 static EGLBoolean
422 dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
423 {
424 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
425 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
426
427 if (!dri2_dpy->flush) {
428 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
429 return EGL_TRUE;
430 }
431
432 if (dri2_surf->current)
433 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
434 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
435 if (dri2_surf->color_buffers[i].age > 0)
436 dri2_surf->color_buffers[i].age++;
437
438 /* Make sure we have a back buffer in case we're swapping without
439 * ever rendering. */
440 if (get_back_bo(dri2_surf) < 0)
441 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
442
443 dri2_surf->current = dri2_surf->back;
444 dri2_surf->current->age = 1;
445 dri2_surf->back = NULL;
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 uint32_t bpp;
531 int x_bytes, width_bytes;
532 char *src, *dst;
533
534 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
535 op != __DRI_SWRAST_IMAGE_OP_SWAP)
536 return;
537
538 if (get_swrast_front_bo(dri2_surf) < 0)
539 return;
540
541 bo = gbm_dri_bo(dri2_surf->current->bo);
542
543 bpp = gbm_bo_get_bpp(&bo->base);
544 if (bpp == 0)
545 return;
546
547 x_bytes = x * (bpp >> 3);
548 width_bytes = width * (bpp >> 3);
549
550 if (gbm_dri_bo_map_dumb(bo) == NULL)
551 return;
552
553 internal_stride = bo->base.stride;
554
555 dst = bo->map + x_bytes + (y * internal_stride);
556 src = data;
557
558 for (int i = 0; i < height; i++) {
559 memcpy(dst, src, width_bytes);
560 dst += internal_stride;
561 src += stride;
562 }
563
564 gbm_dri_bo_unmap_dumb(bo);
565 }
566
567 static void
568 swrast_get_image(__DRIdrawable *driDrawable,
569 int x,
570 int y,
571 int width,
572 int height,
573 char *data,
574 void *loaderPrivate)
575 {
576 struct dri2_egl_surface *dri2_surf = loaderPrivate;
577 int internal_stride, stride;
578 struct gbm_dri_bo *bo;
579 uint32_t bpp;
580 int x_bytes, width_bytes;
581 char *src, *dst;
582
583 if (get_swrast_front_bo(dri2_surf) < 0)
584 return;
585
586 bo = gbm_dri_bo(dri2_surf->current->bo);
587
588 bpp = gbm_bo_get_bpp(&bo->base);
589 if (bpp == 0)
590 return;
591
592 x_bytes = x * (bpp >> 3);
593 width_bytes = width * (bpp >> 3);
594
595 internal_stride = bo->base.stride;
596 stride = width_bytes;
597
598 if (gbm_dri_bo_map_dumb(bo) == NULL)
599 return;
600
601 dst = data;
602 src = bo->map + x_bytes + (y * internal_stride);
603
604 for (int i = 0; i < height; i++) {
605 memcpy(dst, src, width_bytes);
606 dst += stride;
607 src += internal_stride;
608 }
609
610 gbm_dri_bo_unmap_dumb(bo);
611 }
612
613 static EGLBoolean
614 drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
615 {
616 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
617 const struct gbm_dri_visual *visuals = dri2_dpy->gbm_dri->visual_table;
618 int num_visuals = dri2_dpy->gbm_dri->num_visuals;
619 unsigned int format_count[num_visuals];
620 unsigned int config_count = 0;
621
622 memset(format_count, 0, num_visuals * sizeof(unsigned int));
623
624 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
625 const __DRIconfig *config = dri2_dpy->driver_configs[i];
626 int shifts[4];
627 unsigned int sizes[4];
628 bool is_float;
629
630 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
631
632 dri2_get_render_type_float(dri2_dpy->core, config, &is_float);
633
634 for (unsigned j = 0; j < num_visuals; j++) {
635 struct dri2_egl_config *dri2_conf;
636
637 if (visuals[j].rgba_shifts.red != shifts[0] ||
638 visuals[j].rgba_shifts.green != shifts[1] ||
639 visuals[j].rgba_shifts.blue != shifts[2] ||
640 visuals[j].rgba_shifts.alpha != shifts[3] ||
641 visuals[j].rgba_sizes.red != sizes[0] ||
642 visuals[j].rgba_sizes.green != sizes[1] ||
643 visuals[j].rgba_sizes.blue != sizes[2] ||
644 visuals[j].rgba_sizes.alpha != sizes[3] ||
645 visuals[j].is_float != is_float)
646 continue;
647
648 const EGLint attr_list[] = {
649 EGL_NATIVE_VISUAL_ID, visuals[j].gbm_format,
650 EGL_NONE,
651 };
652
653 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
654 config_count + 1, EGL_WINDOW_BIT, attr_list, NULL, NULL);
655 if (dri2_conf) {
656 if (dri2_conf->base.ConfigID == config_count + 1)
657 config_count++;
658 format_count[j]++;
659 }
660 }
661 }
662
663 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
664 if (!format_count[i]) {
665 struct gbm_format_name_desc desc;
666 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
667 gbm_format_get_name(visuals[i].gbm_format, &desc));
668 }
669 }
670
671 return (config_count != 0);
672 }
673
674 static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
675 .authenticate = dri2_drm_authenticate,
676 .create_window_surface = dri2_drm_create_window_surface,
677 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
678 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
679 .destroy_surface = dri2_drm_destroy_surface,
680 .create_image = dri2_drm_create_image_khr,
681 .swap_buffers = dri2_drm_swap_buffers,
682 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
683 .swap_buffers_region = dri2_fallback_swap_buffers_region,
684 .post_sub_buffer = dri2_fallback_post_sub_buffer,
685 .copy_buffers = dri2_fallback_copy_buffers,
686 .query_buffer_age = dri2_drm_query_buffer_age,
687 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
688 .get_sync_values = dri2_fallback_get_sync_values,
689 .get_dri_drawable = dri2_surface_get_dri_drawable,
690 };
691
692 EGLBoolean
693 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
694 {
695 _EGLDevice *dev;
696 struct dri2_egl_display *dri2_dpy;
697 struct gbm_device *gbm;
698 const char *err;
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 = os_dupfd_cloexec(gbm_device_get_fd(gbm));
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, disp->Options.ForceSoftware);
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 }