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