dri: Add a new capabilities for drivers that can't share buffers
[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 <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <xf86drm.h>
32 #include <dlfcn.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include "egl_dri2.h"
39 #include "egl_dri2_fallbacks.h"
40 #include "loader.h"
41
42 static struct gbm_bo *
43 lock_front_buffer(struct gbm_surface *_surf)
44 {
45 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
46 struct dri2_egl_surface *dri2_surf = surf->dri_private;
47 struct gbm_dri_device *device = (struct gbm_dri_device *) _surf->gbm;
48 struct gbm_bo *bo;
49
50 if (dri2_surf->current == NULL) {
51 _eglError(EGL_BAD_SURFACE, "no front buffer");
52 return NULL;
53 }
54
55 bo = dri2_surf->current->bo;
56
57 if (device->dri2) {
58 dri2_surf->current->locked = 1;
59 dri2_surf->current = NULL;
60 }
61
62 return bo;
63 }
64
65 static void
66 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
67 {
68 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
69 struct dri2_egl_surface *dri2_surf = surf->dri_private;
70 int i;
71
72 for (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 = 0;
75 }
76 }
77 }
78
79 static int
80 has_free_buffers(struct gbm_surface *_surf)
81 {
82 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
83 struct dri2_egl_surface *dri2_surf = surf->dri_private;
84 int i;
85
86 for (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_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
95 _EGLConfig *conf, void *native_window,
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 *window = native_window;
102 struct gbm_dri_surface *surf;
103
104 (void) drv;
105
106 dri2_surf = calloc(1, sizeof *dri2_surf);
107 if (!dri2_surf) {
108 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
109 return NULL;
110 }
111
112 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
113 goto cleanup_surf;
114
115 switch (type) {
116 case EGL_WINDOW_BIT:
117 if (!window)
118 return NULL;
119 surf = gbm_dri_surface(window);
120 dri2_surf->gbm_surf = surf;
121 dri2_surf->base.Width = surf->base.width;
122 dri2_surf->base.Height = surf->base.height;
123 surf->dri_private = dri2_surf;
124 break;
125 default:
126 goto cleanup_surf;
127 }
128
129 if (dri2_dpy->dri2) {
130 dri2_surf->dri_drawable =
131 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
132 dri2_conf->dri_double_config,
133 dri2_surf->gbm_surf);
134
135 } else {
136 assert(dri2_dpy->swrast != NULL);
137 dri2_surf->dri_drawable =
138 (*dri2_dpy->swrast->createNewDrawable) (dri2_dpy->dri_screen,
139 dri2_conf->dri_double_config,
140 dri2_surf->gbm_surf);
141
142 }
143 if (dri2_surf->dri_drawable == NULL) {
144 _eglError(EGL_BAD_ALLOC, "createNewDrawable()");
145 goto cleanup_surf;
146 }
147
148 return &dri2_surf->base;
149
150 cleanup_surf:
151 free(dri2_surf);
152
153 return NULL;
154 }
155
156 static _EGLSurface *
157 dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
158 _EGLConfig *conf, void *native_window,
159 const EGLint *attrib_list)
160 {
161 return dri2_drm_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
162 native_window, attrib_list);
163 }
164
165 static _EGLSurface *
166 dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
167 _EGLConfig *conf, void *native_window,
168 const EGLint *attrib_list)
169 {
170 /* From the EGL_MESA_platform_gbm spec, version 5:
171 *
172 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
173 * that belongs to the GBM platform. Any such call fails and generates
174 * EGL_BAD_PARAMETER.
175 */
176 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM");
177 return NULL;
178 }
179
180 static EGLBoolean
181 dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
182 {
183 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
184 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
185 int i;
186
187 if (!_eglPutSurface(surf))
188 return EGL_TRUE;
189
190 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
191
192 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
193 if (dri2_surf->color_buffers[i].bo)
194 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
195 }
196
197 for (i = 0; i < __DRI_BUFFER_COUNT; i++) {
198 if (dri2_surf->dri_buffers[i])
199 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
200 dri2_surf->dri_buffers[i]);
201 }
202
203 free(surf);
204
205 return EGL_TRUE;
206 }
207
208 static int
209 get_back_bo(struct dri2_egl_surface *dri2_surf)
210 {
211 struct dri2_egl_display *dri2_dpy =
212 dri2_egl_display(dri2_surf->base.Resource.Display);
213 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
214 int i;
215
216 if (dri2_surf->back == NULL) {
217 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
218 if (!dri2_surf->color_buffers[i].locked) {
219 dri2_surf->back = &dri2_surf->color_buffers[i];
220 break;
221 }
222 }
223 }
224
225 if (dri2_surf->back == NULL)
226 return -1;
227 if (dri2_surf->back->bo == NULL)
228 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
229 surf->base.width, surf->base.height,
230 surf->base.format, surf->base.flags);
231 if (dri2_surf->back->bo == NULL)
232 return -1;
233
234 return 0;
235 }
236
237 static int
238 get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
239 {
240 struct dri2_egl_display *dri2_dpy =
241 dri2_egl_display(dri2_surf->base.Resource.Display);
242 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
243
244 if (dri2_surf->current == NULL) {
245 assert(!dri2_surf->color_buffers[0].locked);
246 dri2_surf->current = &dri2_surf->color_buffers[0];
247 }
248
249 if (dri2_surf->current->bo == NULL)
250 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
251 surf->base.width, surf->base.height,
252 surf->base.format, surf->base.flags);
253 if (dri2_surf->current->bo == NULL)
254 return -1;
255
256 return 0;
257 }
258
259 static void
260 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
261 {
262 struct dri2_egl_display *dri2_dpy =
263 dri2_egl_display(dri2_surf->base.Resource.Display);
264 struct gbm_dri_bo *bo;
265 int name, pitch;
266
267 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
268
269 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
270 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
271
272 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
273 buffer->name = name;
274 buffer->pitch = pitch;
275 buffer->cpp = 4;
276 buffer->flags = 0;
277 }
278
279 static int
280 get_aux_bo(struct dri2_egl_surface *dri2_surf,
281 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
282 {
283 struct dri2_egl_display *dri2_dpy =
284 dri2_egl_display(dri2_surf->base.Resource.Display);
285 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
286
287 if (b == NULL) {
288 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
289 attachment, format,
290 dri2_surf->base.Width,
291 dri2_surf->base.Height);
292 dri2_surf->dri_buffers[attachment] = b;
293 }
294 if (b == NULL)
295 return -1;
296
297 memcpy(buffer, b, sizeof *buffer);
298
299 return 0;
300 }
301
302 static __DRIbuffer *
303 dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
304 int *width, int *height,
305 unsigned int *attachments, int count,
306 int *out_count, void *loaderPrivate)
307 {
308 struct dri2_egl_surface *dri2_surf = loaderPrivate;
309 int i, j;
310
311 dri2_surf->buffer_count = 0;
312 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
313 assert(attachments[i] < __DRI_BUFFER_COUNT);
314 assert(dri2_surf->buffer_count < 5);
315
316 switch (attachments[i]) {
317 case __DRI_BUFFER_BACK_LEFT:
318 if (get_back_bo(dri2_surf) < 0) {
319 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
320 return NULL;
321 }
322 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
323 break;
324 default:
325 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
326 &dri2_surf->buffers[j]) < 0) {
327 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
328 return NULL;
329 }
330 break;
331 }
332 }
333
334 *out_count = j;
335 if (j == 0)
336 return NULL;
337
338 *width = dri2_surf->base.Width;
339 *height = dri2_surf->base.Height;
340
341 return dri2_surf->buffers;
342 }
343
344 static __DRIbuffer *
345 dri2_drm_get_buffers(__DRIdrawable * driDrawable,
346 int *width, int *height,
347 unsigned int *attachments, int count,
348 int *out_count, void *loaderPrivate)
349 {
350 unsigned int *attachments_with_format;
351 __DRIbuffer *buffer;
352 const unsigned int format = 32;
353 int i;
354
355 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
356 if (!attachments_with_format) {
357 *out_count = 0;
358 return NULL;
359 }
360
361 for (i = 0; i < count; ++i) {
362 attachments_with_format[2*i] = attachments[i];
363 attachments_with_format[2*i + 1] = format;
364 }
365
366 buffer =
367 dri2_drm_get_buffers_with_format(driDrawable,
368 width, height,
369 attachments_with_format, count,
370 out_count, loaderPrivate);
371
372 free(attachments_with_format);
373
374 return buffer;
375 }
376
377 static int
378 dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
379 unsigned int format,
380 uint32_t *stamp,
381 void *loaderPrivate,
382 uint32_t buffer_mask,
383 struct __DRIimageList *buffers)
384 {
385 struct dri2_egl_surface *dri2_surf = loaderPrivate;
386 struct gbm_dri_bo *bo;
387
388 if (get_back_bo(dri2_surf) < 0)
389 return 0;
390
391 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
392 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
393 buffers->back = bo->image;
394
395 return 1;
396 }
397
398 static void
399 dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
400 {
401 (void) driDrawable;
402 (void) loaderPrivate;
403 }
404
405 static EGLBoolean
406 dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
407 {
408 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
409 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
410 int i;
411
412 if (dri2_dpy->swrast) {
413 (*dri2_dpy->core->swapBuffers)(dri2_surf->dri_drawable);
414 } else {
415 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
416 if (dri2_surf->current)
417 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
418 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
419 if (dri2_surf->color_buffers[i].age > 0)
420 dri2_surf->color_buffers[i].age++;
421 dri2_surf->current = dri2_surf->back;
422 dri2_surf->current->age = 1;
423 dri2_surf->back = NULL;
424 }
425
426 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
427 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
428 }
429
430 return EGL_TRUE;
431 }
432
433 static EGLint
434 dri2_drm_query_buffer_age(_EGLDriver *drv,
435 _EGLDisplay *disp, _EGLSurface *surface)
436 {
437 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
438
439 if (get_back_bo(dri2_surf) < 0) {
440 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
441 return 0;
442 }
443
444 return dri2_surf->back->age;
445 }
446
447 static _EGLImage *
448 dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
449 EGLClientBuffer buffer, const EGLint *attr_list)
450 {
451 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
452 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
453 struct dri2_egl_image *dri2_img;
454
455 dri2_img = malloc(sizeof *dri2_img);
456 if (!dri2_img) {
457 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
458 return NULL;
459 }
460
461 if (!_eglInitImage(&dri2_img->base, disp)) {
462 free(dri2_img);
463 return NULL;
464 }
465
466 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
467 if (dri2_img->dri_image == NULL) {
468 free(dri2_img);
469 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
470 return NULL;
471 }
472
473 return &dri2_img->base;
474 }
475
476 static _EGLImage *
477 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
478 _EGLContext *ctx, EGLenum target,
479 EGLClientBuffer buffer, const EGLint *attr_list)
480 {
481 (void) drv;
482
483 switch (target) {
484 case EGL_NATIVE_PIXMAP_KHR:
485 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
486 default:
487 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
488 }
489 }
490
491 static int
492 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
493 {
494 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
495
496 return drmAuthMagic(dri2_dpy->fd, id);
497 }
498
499 static void
500 swrast_put_image2(__DRIdrawable *driDrawable,
501 int op,
502 int x,
503 int y,
504 int width,
505 int height,
506 int stride,
507 char *data,
508 void *loaderPrivate)
509 {
510 struct dri2_egl_surface *dri2_surf = loaderPrivate;
511 int internal_stride, i;
512 struct gbm_dri_bo *bo;
513
514 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
515 op != __DRI_SWRAST_IMAGE_OP_SWAP)
516 return;
517
518 if (get_swrast_front_bo(dri2_surf) < 0)
519 return;
520
521 bo = gbm_dri_bo(dri2_surf->current->bo);
522 if (gbm_dri_bo_map(bo) == NULL)
523 return;
524
525 internal_stride = bo->base.base.stride;
526
527 for (i = 0; i < height; i++) {
528 memcpy(bo->map + (x + i) * internal_stride + y,
529 data + i * stride, stride);
530 }
531
532 gbm_dri_bo_unmap(bo);
533 }
534
535 static void
536 swrast_get_image(__DRIdrawable *driDrawable,
537 int x,
538 int y,
539 int width,
540 int height,
541 char *data,
542 void *loaderPrivate)
543 {
544 struct dri2_egl_surface *dri2_surf = loaderPrivate;
545 int internal_stride, stride, i;
546 struct gbm_dri_bo *bo;
547
548 if (get_swrast_front_bo(dri2_surf) < 0)
549 return;
550
551 bo = gbm_dri_bo(dri2_surf->current->bo);
552 if (gbm_dri_bo_map(bo) == NULL)
553 return;
554
555 internal_stride = bo->base.base.stride;
556 stride = width * 4;
557
558 for (i = 0; i < height; i++) {
559 memcpy(data + i * stride,
560 bo->map + (x + i) * internal_stride + y, stride);
561 }
562
563 gbm_dri_bo_unmap(bo);
564 }
565
566 static struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
567 .authenticate = dri2_drm_authenticate,
568 .create_window_surface = dri2_drm_create_window_surface,
569 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
570 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
571 .destroy_surface = dri2_drm_destroy_surface,
572 .create_image = dri2_drm_create_image_khr,
573 .swap_interval = dri2_fallback_swap_interval,
574 .swap_buffers = dri2_drm_swap_buffers,
575 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
576 .swap_buffers_region = dri2_fallback_swap_buffers_region,
577 .post_sub_buffer = dri2_fallback_post_sub_buffer,
578 .copy_buffers = dri2_fallback_copy_buffers,
579 .query_buffer_age = dri2_drm_query_buffer_age,
580 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
581 .get_sync_values = dri2_fallback_get_sync_values,
582 };
583
584 EGLBoolean
585 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
586 {
587 struct dri2_egl_display *dri2_dpy;
588 struct gbm_device *gbm;
589 int fd = -1;
590 int i;
591
592 loader_set_logger(_eglLog);
593
594 dri2_dpy = calloc(1, sizeof *dri2_dpy);
595 if (!dri2_dpy)
596 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
597
598 disp->DriverData = (void *) dri2_dpy;
599
600 gbm = disp->PlatformDisplay;
601 if (gbm == NULL) {
602 char buf[64];
603 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
604 if (n != -1 && n < sizeof(buf))
605 fd = open(buf, O_RDWR);
606 if (fd < 0)
607 fd = open("/dev/dri/card0", O_RDWR);
608 dri2_dpy->own_device = 1;
609 gbm = gbm_create_device(fd);
610 if (gbm == NULL)
611 return EGL_FALSE;
612 }
613
614 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
615 free(dri2_dpy);
616 return EGL_FALSE;
617 }
618
619 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
620 if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI) {
621 free(dri2_dpy);
622 return EGL_FALSE;
623 }
624
625 if (fd < 0) {
626 fd = dup(gbm_device_get_fd(gbm));
627 if (fd < 0) {
628 free(dri2_dpy);
629 return EGL_FALSE;
630 }
631 }
632
633 dri2_dpy->fd = fd;
634 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
635 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->base.driver_name);
636
637 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
638 dri2_dpy->core = dri2_dpy->gbm_dri->core;
639 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
640 dri2_dpy->image = dri2_dpy->gbm_dri->image;
641 dri2_dpy->flush = dri2_dpy->gbm_dri->flush;
642 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
643 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
644
645 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
646 dri2_dpy->gbm_dri->lookup_user_data = disp;
647
648 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
649 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
650 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
651 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
652 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
653 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
654
655 dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer;
656 dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer;
657 dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers;
658
659 dri2_setup_screen(disp);
660
661 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
662 EGLint format, attr_list[3];
663 unsigned int mask;
664
665 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
666 __DRI_ATTRIB_RED_MASK, &mask);
667 if (mask == 0x3ff00000)
668 format = GBM_FORMAT_XRGB2101010;
669 else if (mask == 0x00ff0000)
670 format = GBM_FORMAT_XRGB8888;
671 else if (mask == 0xf800)
672 format = GBM_FORMAT_RGB565;
673 else
674 continue;
675
676 attr_list[0] = EGL_NATIVE_VISUAL_ID;
677 attr_list[1] = format;
678 attr_list[2] = EGL_NONE;
679
680 dri2_add_config(disp, dri2_dpy->driver_configs[i],
681 i + 1, EGL_WINDOW_BIT, attr_list, NULL);
682 }
683
684 if (dri2_dpy->dri2)
685 disp->Extensions.EXT_buffer_age = EGL_TRUE;
686
687 #ifdef HAVE_WAYLAND_PLATFORM
688 if (dri2_dpy->image) {
689 if (dri2_dpy->image->base.version >= 10 &&
690 dri2_dpy->image->getCapabilities != NULL) {
691 int capabilities;
692
693 capabilities =
694 dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
695 disp->Extensions.WL_bind_wayland_display =
696 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
697 } else
698 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
699 }
700 #endif
701
702 /* we're supporting EGL 1.4 */
703 disp->VersionMajor = 1;
704 disp->VersionMinor = 4;
705
706 /* Fill vtbl last to prevent accidentally calling virtual function during
707 * initialization.
708 */
709 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
710
711 return EGL_TRUE;
712 }