egl/drm: rename dri2_drm_create_surface()
[mesa.git] / src / egl / drivers / dri2 / platform_drm.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <xf86drm.h>
33 #include <dlfcn.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 #include "egl_dri2.h"
40 #include "egl_dri2_fallbacks.h"
41 #include "loader.h"
42
43 static struct gbm_bo *
44 lock_front_buffer(struct gbm_surface *_surf)
45 {
46 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
47 struct dri2_egl_surface *dri2_surf = surf->dri_private;
48 struct gbm_dri_device *device = (struct gbm_dri_device *) _surf->gbm;
49 struct gbm_bo *bo;
50
51 if (dri2_surf->current == NULL) {
52 _eglError(EGL_BAD_SURFACE, "no front buffer");
53 return NULL;
54 }
55
56 bo = dri2_surf->current->bo;
57
58 if (device->dri2) {
59 dri2_surf->current->locked = true;
60 dri2_surf->current = NULL;
61 }
62
63 return bo;
64 }
65
66 static void
67 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
68 {
69 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
70 struct dri2_egl_surface *dri2_surf = surf->dri_private;
71
72 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
73 if (dri2_surf->color_buffers[i].bo == bo) {
74 dri2_surf->color_buffers[i].locked = false;
75 break;
76 }
77 }
78 }
79
80 static int
81 has_free_buffers(struct gbm_surface *_surf)
82 {
83 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
84 struct dri2_egl_surface *dri2_surf = surf->dri_private;
85
86 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
87 if (!dri2_surf->color_buffers[i].locked)
88 return 1;
89
90 return 0;
91 }
92
93 static _EGLSurface *
94 dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
95 _EGLConfig *conf, void *native_surface,
96 const EGLint *attrib_list)
97 {
98 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
99 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
100 struct dri2_egl_surface *dri2_surf;
101 struct gbm_surface *surface = native_surface;
102 struct gbm_dri_surface *surf;
103 const __DRIconfig *config;
104
105 (void) drv;
106
107 dri2_surf = calloc(1, sizeof *dri2_surf);
108 if (!dri2_surf) {
109 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
110 return NULL;
111 }
112
113 if (!_eglInitSurface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list))
114 goto cleanup_surf;
115
116 surf = gbm_dri_surface(surface);
117 dri2_surf->gbm_surf = surf;
118 dri2_surf->base.Width = surf->base.width;
119 dri2_surf->base.Height = surf->base.height;
120 surf->dri_private = dri2_surf;
121
122 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
123 dri2_surf->base.GLColorspace);
124
125 if (dri2_dpy->dri2) {
126 dri2_surf->dri_drawable =
127 dri2_dpy->dri2->createNewDrawable(dri2_dpy->dri_screen, config,
128 dri2_surf->gbm_surf);
129
130 } else {
131 assert(dri2_dpy->swrast != NULL);
132
133 dri2_surf->dri_drawable =
134 dri2_dpy->swrast->createNewDrawable(dri2_dpy->dri_screen, config,
135 dri2_surf->gbm_surf);
136
137 }
138 if (dri2_surf->dri_drawable == NULL) {
139 _eglError(EGL_BAD_ALLOC, "createNewDrawable()");
140 goto cleanup_surf;
141 }
142
143 return &dri2_surf->base;
144
145 cleanup_surf:
146 free(dri2_surf);
147
148 return NULL;
149 }
150
151 static _EGLSurface *
152 dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
153 _EGLConfig *conf, void *native_window,
154 const EGLint *attrib_list)
155 {
156 /* From the EGL_MESA_platform_gbm spec, version 5:
157 *
158 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
159 * that belongs to the GBM platform. Any such call fails and generates
160 * EGL_BAD_PARAMETER.
161 */
162 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM");
163 return NULL;
164 }
165
166 static EGLBoolean
167 dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
168 {
169 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
170 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
171
172 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
173
174 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
175 if (dri2_surf->color_buffers[i].bo)
176 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
177 }
178
179 for (unsigned i = 0; i < __DRI_BUFFER_COUNT; i++) {
180 if (dri2_surf->dri_buffers[i])
181 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
182 dri2_surf->dri_buffers[i]);
183 }
184
185 free(surf);
186
187 return EGL_TRUE;
188 }
189
190 static int
191 get_back_bo(struct dri2_egl_surface *dri2_surf)
192 {
193 struct dri2_egl_display *dri2_dpy =
194 dri2_egl_display(dri2_surf->base.Resource.Display);
195 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
196 int age = 0;
197
198 if (dri2_surf->back == NULL) {
199 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
200 if (!dri2_surf->color_buffers[i].locked &&
201 dri2_surf->color_buffers[i].age >= age) {
202 dri2_surf->back = &dri2_surf->color_buffers[i];
203 age = dri2_surf->color_buffers[i].age;
204 }
205 }
206 }
207
208 if (dri2_surf->back == NULL)
209 return -1;
210 if (dri2_surf->back->bo == NULL) {
211 if (surf->base.modifiers)
212 dri2_surf->back->bo = gbm_bo_create_with_modifiers(&dri2_dpy->gbm_dri->base,
213 surf->base.width,
214 surf->base.height,
215 surf->base.format,
216 surf->base.modifiers,
217 surf->base.count);
218 else
219 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
220 surf->base.width,
221 surf->base.height,
222 surf->base.format,
223 surf->base.flags);
224
225 }
226 if (dri2_surf->back->bo == NULL)
227 return -1;
228
229 return 0;
230 }
231
232 static int
233 get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
234 {
235 struct dri2_egl_display *dri2_dpy =
236 dri2_egl_display(dri2_surf->base.Resource.Display);
237 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
238
239 if (dri2_surf->current == NULL) {
240 assert(!dri2_surf->color_buffers[0].locked);
241 dri2_surf->current = &dri2_surf->color_buffers[0];
242 }
243
244 if (dri2_surf->current->bo == NULL)
245 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base,
246 surf->base.width, surf->base.height,
247 surf->base.format, surf->base.flags);
248 if (dri2_surf->current->bo == NULL)
249 return -1;
250
251 return 0;
252 }
253
254 static void
255 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
256 {
257 struct dri2_egl_display *dri2_dpy =
258 dri2_egl_display(dri2_surf->base.Resource.Display);
259 struct gbm_dri_bo *bo;
260 int name, pitch;
261
262 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
263
264 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
265 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
266
267 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
268 buffer->name = name;
269 buffer->pitch = pitch;
270 buffer->cpp = 4;
271 buffer->flags = 0;
272 }
273
274 static int
275 get_aux_bo(struct dri2_egl_surface *dri2_surf,
276 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
277 {
278 struct dri2_egl_display *dri2_dpy =
279 dri2_egl_display(dri2_surf->base.Resource.Display);
280 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
281
282 if (b == NULL) {
283 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
284 attachment, format,
285 dri2_surf->base.Width,
286 dri2_surf->base.Height);
287 dri2_surf->dri_buffers[attachment] = b;
288 }
289 if (b == NULL)
290 return -1;
291
292 memcpy(buffer, b, sizeof *buffer);
293
294 return 0;
295 }
296
297 static __DRIbuffer *
298 dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
299 int *width, int *height,
300 unsigned int *attachments, int count,
301 int *out_count, void *loaderPrivate)
302 {
303 struct dri2_egl_surface *dri2_surf = loaderPrivate;
304 int i, j;
305
306 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
307 assert(attachments[i] < __DRI_BUFFER_COUNT);
308 assert(j < ARRAY_SIZE(dri2_surf->buffers));
309
310 switch (attachments[i]) {
311 case __DRI_BUFFER_BACK_LEFT:
312 if (get_back_bo(dri2_surf) < 0) {
313 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
314 return NULL;
315 }
316 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
317 break;
318 default:
319 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
320 &dri2_surf->buffers[j]) < 0) {
321 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
322 return NULL;
323 }
324 break;
325 }
326 }
327
328 *out_count = j;
329 if (j == 0)
330 return NULL;
331
332 *width = dri2_surf->base.Width;
333 *height = dri2_surf->base.Height;
334
335 return dri2_surf->buffers;
336 }
337
338 static __DRIbuffer *
339 dri2_drm_get_buffers(__DRIdrawable * driDrawable,
340 int *width, int *height,
341 unsigned int *attachments, int count,
342 int *out_count, void *loaderPrivate)
343 {
344 unsigned int *attachments_with_format;
345 __DRIbuffer *buffer;
346 const unsigned int format = 32;
347
348 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
349 if (!attachments_with_format) {
350 *out_count = 0;
351 return NULL;
352 }
353
354 for (int i = 0; i < count; ++i) {
355 attachments_with_format[2*i] = attachments[i];
356 attachments_with_format[2*i + 1] = format;
357 }
358
359 buffer =
360 dri2_drm_get_buffers_with_format(driDrawable,
361 width, height,
362 attachments_with_format, count,
363 out_count, loaderPrivate);
364
365 free(attachments_with_format);
366
367 return buffer;
368 }
369
370 static int
371 dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
372 unsigned int format,
373 uint32_t *stamp,
374 void *loaderPrivate,
375 uint32_t buffer_mask,
376 struct __DRIimageList *buffers)
377 {
378 struct dri2_egl_surface *dri2_surf = loaderPrivate;
379 struct gbm_dri_bo *bo;
380
381 if (get_back_bo(dri2_surf) < 0)
382 return 0;
383
384 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
385 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
386 buffers->back = bo->image;
387
388 return 1;
389 }
390
391 static void
392 dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
393 {
394 (void) driDrawable;
395 (void) loaderPrivate;
396 }
397
398 static EGLBoolean
399 dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
400 {
401 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
402 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
403
404 if (!dri2_dpy->flush) {
405 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
406 return EGL_TRUE;
407 }
408
409 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
410 if (dri2_surf->current)
411 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
412 for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
413 if (dri2_surf->color_buffers[i].age > 0)
414 dri2_surf->color_buffers[i].age++;
415
416 /* Make sure we have a back buffer in case we're swapping without
417 * ever rendering. */
418 if (get_back_bo(dri2_surf) < 0)
419 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
420
421 dri2_surf->current = dri2_surf->back;
422 dri2_surf->current->age = 1;
423 dri2_surf->back = NULL;
424 }
425
426 dri2_flush_drawable_for_swapbuffers(disp, draw);
427 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
428
429 return EGL_TRUE;
430 }
431
432 static EGLint
433 dri2_drm_query_buffer_age(_EGLDriver *drv,
434 _EGLDisplay *disp, _EGLSurface *surface)
435 {
436 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
437
438 if (get_back_bo(dri2_surf) < 0) {
439 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
440 return -1;
441 }
442
443 return dri2_surf->back->age;
444 }
445
446 static _EGLImage *
447 dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
448 EGLClientBuffer buffer, const EGLint *attr_list)
449 {
450 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
451 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
452 struct dri2_egl_image *dri2_img;
453
454 dri2_img = malloc(sizeof *dri2_img);
455 if (!dri2_img) {
456 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
457 return NULL;
458 }
459
460 _eglInitImage(&dri2_img->base, disp);
461
462 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
463 if (dri2_img->dri_image == NULL) {
464 free(dri2_img);
465 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
466 return NULL;
467 }
468
469 return &dri2_img->base;
470 }
471
472 static _EGLImage *
473 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
474 _EGLContext *ctx, EGLenum target,
475 EGLClientBuffer buffer, const EGLint *attr_list)
476 {
477 (void) drv;
478
479 switch (target) {
480 case EGL_NATIVE_PIXMAP_KHR:
481 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
482 default:
483 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
484 }
485 }
486
487 static int
488 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
489 {
490 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
491
492 return drmAuthMagic(dri2_dpy->fd, id);
493 }
494
495 static void
496 swrast_put_image2(__DRIdrawable *driDrawable,
497 int op,
498 int x,
499 int y,
500 int width,
501 int height,
502 int stride,
503 char *data,
504 void *loaderPrivate)
505 {
506 struct dri2_egl_surface *dri2_surf = loaderPrivate;
507 int internal_stride;
508 struct gbm_dri_bo *bo;
509 uint32_t bpp;
510 int x_bytes, width_bytes;
511 char *src, *dst;
512
513 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
514 op != __DRI_SWRAST_IMAGE_OP_SWAP)
515 return;
516
517 if (get_swrast_front_bo(dri2_surf) < 0)
518 return;
519
520 bo = gbm_dri_bo(dri2_surf->current->bo);
521
522 bpp = gbm_bo_get_bpp(&bo->base);
523 if (bpp == 0)
524 return;
525
526 x_bytes = x * (bpp >> 3);
527 width_bytes = width * (bpp >> 3);
528
529 if (gbm_dri_bo_map_dumb(bo) == NULL)
530 return;
531
532 internal_stride = bo->base.stride;
533
534 dst = bo->map + x_bytes + (y * internal_stride);
535 src = data;
536
537 for (int i = 0; i < height; i++) {
538 memcpy(dst, src, width_bytes);
539 dst += internal_stride;
540 src += stride;
541 }
542
543 gbm_dri_bo_unmap_dumb(bo);
544 }
545
546 static void
547 swrast_get_image(__DRIdrawable *driDrawable,
548 int x,
549 int y,
550 int width,
551 int height,
552 char *data,
553 void *loaderPrivate)
554 {
555 struct dri2_egl_surface *dri2_surf = loaderPrivate;
556 int internal_stride, stride;
557 struct gbm_dri_bo *bo;
558 uint32_t bpp;
559 int x_bytes, width_bytes;
560 char *src, *dst;
561
562 if (get_swrast_front_bo(dri2_surf) < 0)
563 return;
564
565 bo = gbm_dri_bo(dri2_surf->current->bo);
566
567 bpp = gbm_bo_get_bpp(&bo->base);
568 if (bpp == 0)
569 return;
570
571 x_bytes = x * (bpp >> 3);
572 width_bytes = width * (bpp >> 3);
573
574 internal_stride = bo->base.stride;
575 stride = width_bytes;
576
577 if (gbm_dri_bo_map_dumb(bo) == NULL)
578 return;
579
580 dst = data;
581 src = bo->map + x_bytes + (y * internal_stride);
582
583 for (int i = 0; i < height; i++) {
584 memcpy(dst, src, width_bytes);
585 dst += stride;
586 src += internal_stride;
587 }
588
589 gbm_dri_bo_unmap_dumb(bo);
590 }
591
592 static EGLBoolean
593 drm_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
594 {
595 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
596 static const struct {
597 int format;
598 unsigned int red_mask;
599 unsigned int alpha_mask;
600 } visuals[] = {
601 { GBM_FORMAT_XRGB2101010, 0x3ff00000, 0x00000000 },
602 { GBM_FORMAT_ARGB2101010, 0x3ff00000, 0xc0000000 },
603 { GBM_FORMAT_XRGB8888, 0x00ff0000, 0x00000000 },
604 { GBM_FORMAT_ARGB8888, 0x00ff0000, 0xff000000 },
605 { GBM_FORMAT_RGB565, 0x0000f800, 0x00000000 },
606 };
607
608 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
609 unsigned int config_count = 0;
610
611 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
612 unsigned int red, alpha;
613
614 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
615 __DRI_ATTRIB_RED_MASK, &red);
616 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
617 __DRI_ATTRIB_ALPHA_MASK, &alpha);
618
619 for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
620 struct dri2_egl_config *dri2_conf;
621
622 if (visuals[j].red_mask != red || visuals[j].alpha_mask != alpha)
623 continue;
624
625 const EGLint attr_list[] = {
626 EGL_NATIVE_VISUAL_ID, visuals[j].format,
627 EGL_NONE,
628 };
629
630 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
631 config_count + 1, EGL_WINDOW_BIT, attr_list, NULL);
632 if (dri2_conf) {
633 if (dri2_conf->base.ConfigID == config_count + 1)
634 config_count++;
635 format_count[j]++;
636 }
637 }
638 }
639
640 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
641 if (!format_count[i]) {
642 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
643 visuals[i].format);
644 }
645 }
646
647 return (config_count != 0);
648 }
649
650 static const struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
651 .authenticate = dri2_drm_authenticate,
652 .create_window_surface = dri2_drm_create_window_surface,
653 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
654 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
655 .destroy_surface = dri2_drm_destroy_surface,
656 .create_image = dri2_drm_create_image_khr,
657 .swap_interval = dri2_fallback_swap_interval,
658 .swap_buffers = dri2_drm_swap_buffers,
659 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
660 .swap_buffers_region = dri2_fallback_swap_buffers_region,
661 .set_damage_region = dri2_fallback_set_damage_region,
662 .post_sub_buffer = dri2_fallback_post_sub_buffer,
663 .copy_buffers = dri2_fallback_copy_buffers,
664 .query_buffer_age = dri2_drm_query_buffer_age,
665 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
666 .get_sync_values = dri2_fallback_get_sync_values,
667 .get_dri_drawable = dri2_surface_get_dri_drawable,
668 };
669
670 EGLBoolean
671 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
672 {
673 struct dri2_egl_display *dri2_dpy;
674 struct gbm_device *gbm;
675 const char *err;
676
677 loader_set_logger(_eglLog);
678
679 dri2_dpy = calloc(1, sizeof *dri2_dpy);
680 if (!dri2_dpy)
681 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
682
683 dri2_dpy->fd = -1;
684 disp->DriverData = (void *) dri2_dpy;
685
686 gbm = disp->PlatformDisplay;
687 if (gbm == NULL) {
688 char buf[64];
689 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
690 if (n != -1 && n < sizeof(buf))
691 dri2_dpy->fd = loader_open_device(buf);
692 if (dri2_dpy->fd < 0)
693 dri2_dpy->fd = loader_open_device("/dev/dri/card0");
694 gbm = gbm_create_device(dri2_dpy->fd);
695 if (gbm == NULL) {
696 err = "DRI2: failed to create gbm device";
697 goto cleanup;
698 }
699 dri2_dpy->own_device = true;
700 } else {
701 dri2_dpy->fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3);
702 if (dri2_dpy->fd < 0) {
703 err = "DRI2: failed to fcntl() existing gbm device";
704 goto cleanup;
705 }
706 }
707
708 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
709 err = "DRI2: gbm device using incorrect/incompatible backend";
710 goto cleanup;
711 }
712
713 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
714 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->driver_name);
715
716 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
717 dri2_dpy->core = dri2_dpy->gbm_dri->core;
718 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
719 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
720 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
721
722 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
723 dri2_dpy->gbm_dri->lookup_user_data = disp;
724
725 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
726 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
727 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
728 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
729 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
730 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
731
732 dri2_dpy->gbm_dri->base.surface_lock_front_buffer = lock_front_buffer;
733 dri2_dpy->gbm_dri->base.surface_release_buffer = release_buffer;
734 dri2_dpy->gbm_dri->base.surface_has_free_buffers = has_free_buffers;
735
736 if (!dri2_setup_extensions(disp)) {
737 err = "DRI2: failed to find required DRI extensions";
738 goto cleanup;
739 }
740
741 dri2_setup_screen(disp);
742
743 if (!drm_add_configs_for_visuals(drv, disp)) {
744 err = "DRI2: failed to add configs";
745 goto cleanup;
746 }
747
748 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
749 if (dri2_dpy->dri2)
750 disp->Extensions.EXT_buffer_age = EGL_TRUE;
751
752 #ifdef HAVE_WAYLAND_PLATFORM
753 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
754 #endif
755 dri2_set_WL_bind_wayland_display(drv, disp);
756
757 /* Fill vtbl last to prevent accidentally calling virtual function during
758 * initialization.
759 */
760 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
761
762 return EGL_TRUE;
763
764 cleanup:
765 dri2_display_destroy(disp);
766 return _eglError(EGL_NOT_INITIALIZED, err);
767 }