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