2 * Copyright © 2011 Intel Corporation
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:
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
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.
25 * Kristian Høgsberg <krh@bitplanet.net>
34 #include <sys/types.h>
40 #include "egl_dri2_fallbacks.h"
43 static struct gbm_bo *
44 lock_front_buffer(struct gbm_surface *_surf)
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;
51 if (dri2_surf->current == NULL) {
52 _eglError(EGL_BAD_SURFACE, "no front buffer");
56 bo = dri2_surf->current->bo;
59 dri2_surf->current->locked = 1;
60 dri2_surf->current = NULL;
67 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
69 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
70 struct dri2_egl_surface *dri2_surf = surf->dri_private;
73 for (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 = 0;
81 has_free_buffers(struct gbm_surface *_surf)
83 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
84 struct dri2_egl_surface *dri2_surf = surf->dri_private;
87 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
88 if (!dri2_surf->color_buffers[i].locked)
95 dri2_drm_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
96 _EGLConfig *conf, void *native_window,
97 const EGLint *attrib_list)
99 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
100 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
101 struct dri2_egl_surface *dri2_surf;
102 struct gbm_surface *window = native_window;
103 struct gbm_dri_surface *surf;
104 const __DRIconfig *config;
108 dri2_surf = calloc(1, sizeof *dri2_surf);
110 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
114 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
120 _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
124 surf = gbm_dri_surface(window);
125 dri2_surf->gbm_surf = surf;
126 dri2_surf->base.Width = surf->base.width;
127 dri2_surf->base.Height = surf->base.height;
128 surf->dri_private = dri2_surf;
134 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
135 dri2_surf->base.GLColorspace);
137 if (dri2_dpy->dri2) {
138 dri2_surf->dri_drawable =
139 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
140 dri2_surf->gbm_surf);
143 assert(dri2_dpy->swrast != NULL);
145 dri2_surf->dri_drawable =
146 (*dri2_dpy->swrast->createNewDrawable)(dri2_dpy->dri_screen, config,
147 dri2_surf->gbm_surf);
150 if (dri2_surf->dri_drawable == NULL) {
151 _eglError(EGL_BAD_ALLOC, "createNewDrawable()");
155 return &dri2_surf->base;
164 dri2_drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
165 _EGLConfig *conf, void *native_window,
166 const EGLint *attrib_list)
168 return dri2_drm_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
169 native_window, attrib_list);
173 dri2_drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
174 _EGLConfig *conf, void *native_window,
175 const EGLint *attrib_list)
177 /* From the EGL_MESA_platform_gbm spec, version 5:
179 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
180 * that belongs to the GBM platform. Any such call fails and generates
183 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on GBM");
188 dri2_drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
190 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
191 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
194 if (!_eglPutSurface(surf))
197 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
199 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
200 if (dri2_surf->color_buffers[i].bo)
201 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
204 for (i = 0; i < __DRI_BUFFER_COUNT; i++) {
205 if (dri2_surf->dri_buffers[i])
206 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
207 dri2_surf->dri_buffers[i]);
216 get_back_bo(struct dri2_egl_surface *dri2_surf)
218 struct dri2_egl_display *dri2_dpy =
219 dri2_egl_display(dri2_surf->base.Resource.Display);
220 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
223 if (dri2_surf->back == NULL) {
224 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
225 if (!dri2_surf->color_buffers[i].locked) {
226 dri2_surf->back = &dri2_surf->color_buffers[i];
232 if (dri2_surf->back == NULL)
234 if (dri2_surf->back->bo == NULL)
235 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
236 surf->base.width, surf->base.height,
237 surf->base.format, surf->base.flags);
238 if (dri2_surf->back->bo == NULL)
245 get_swrast_front_bo(struct dri2_egl_surface *dri2_surf)
247 struct dri2_egl_display *dri2_dpy =
248 dri2_egl_display(dri2_surf->base.Resource.Display);
249 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
251 if (dri2_surf->current == NULL) {
252 assert(!dri2_surf->color_buffers[0].locked);
253 dri2_surf->current = &dri2_surf->color_buffers[0];
256 if (dri2_surf->current->bo == NULL)
257 dri2_surf->current->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
258 surf->base.width, surf->base.height,
259 surf->base.format, surf->base.flags);
260 if (dri2_surf->current->bo == NULL)
267 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
269 struct dri2_egl_display *dri2_dpy =
270 dri2_egl_display(dri2_surf->base.Resource.Display);
271 struct gbm_dri_bo *bo;
274 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
276 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
277 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
279 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
281 buffer->pitch = pitch;
287 get_aux_bo(struct dri2_egl_surface *dri2_surf,
288 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
290 struct dri2_egl_display *dri2_dpy =
291 dri2_egl_display(dri2_surf->base.Resource.Display);
292 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
295 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
297 dri2_surf->base.Width,
298 dri2_surf->base.Height);
299 dri2_surf->dri_buffers[attachment] = b;
304 memcpy(buffer, b, sizeof *buffer);
310 dri2_drm_get_buffers_with_format(__DRIdrawable *driDrawable,
311 int *width, int *height,
312 unsigned int *attachments, int count,
313 int *out_count, void *loaderPrivate)
315 struct dri2_egl_surface *dri2_surf = loaderPrivate;
318 dri2_surf->buffer_count = 0;
319 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
320 assert(attachments[i] < __DRI_BUFFER_COUNT);
321 assert(dri2_surf->buffer_count < 5);
323 switch (attachments[i]) {
324 case __DRI_BUFFER_BACK_LEFT:
325 if (get_back_bo(dri2_surf) < 0) {
326 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
329 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
332 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
333 &dri2_surf->buffers[j]) < 0) {
334 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
345 *width = dri2_surf->base.Width;
346 *height = dri2_surf->base.Height;
348 return dri2_surf->buffers;
352 dri2_drm_get_buffers(__DRIdrawable * driDrawable,
353 int *width, int *height,
354 unsigned int *attachments, int count,
355 int *out_count, void *loaderPrivate)
357 unsigned int *attachments_with_format;
359 const unsigned int format = 32;
362 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
363 if (!attachments_with_format) {
368 for (i = 0; i < count; ++i) {
369 attachments_with_format[2*i] = attachments[i];
370 attachments_with_format[2*i + 1] = format;
374 dri2_drm_get_buffers_with_format(driDrawable,
376 attachments_with_format, count,
377 out_count, loaderPrivate);
379 free(attachments_with_format);
385 dri2_drm_image_get_buffers(__DRIdrawable *driDrawable,
389 uint32_t buffer_mask,
390 struct __DRIimageList *buffers)
392 struct dri2_egl_surface *dri2_surf = loaderPrivate;
393 struct gbm_dri_bo *bo;
395 if (get_back_bo(dri2_surf) < 0)
398 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
399 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
400 buffers->back = bo->image;
406 dri2_drm_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
409 (void) loaderPrivate;
413 dri2_drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
415 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
416 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
419 if (dri2_dpy->swrast) {
420 (*dri2_dpy->core->swapBuffers)(dri2_surf->dri_drawable);
422 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
423 if (dri2_surf->current)
424 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
425 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
426 if (dri2_surf->color_buffers[i].age > 0)
427 dri2_surf->color_buffers[i].age++;
429 /* Make sure we have a back buffer in case we're swapping without
431 if (get_back_bo(dri2_surf) < 0) {
432 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
436 dri2_surf->current = dri2_surf->back;
437 dri2_surf->current->age = 1;
438 dri2_surf->back = NULL;
441 dri2_flush_drawable_for_swapbuffers(disp, draw);
442 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
449 dri2_drm_query_buffer_age(_EGLDriver *drv,
450 _EGLDisplay *disp, _EGLSurface *surface)
452 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
454 if (get_back_bo(dri2_surf) < 0) {
455 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
459 return dri2_surf->back->age;
463 dri2_drm_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
464 EGLClientBuffer buffer, const EGLint *attr_list)
466 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
467 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
468 struct dri2_egl_image *dri2_img;
470 dri2_img = malloc(sizeof *dri2_img);
472 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
476 if (!_eglInitImage(&dri2_img->base, disp)) {
481 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
482 if (dri2_img->dri_image == NULL) {
484 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
488 return &dri2_img->base;
492 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
493 _EGLContext *ctx, EGLenum target,
494 EGLClientBuffer buffer, const EGLint *attr_list)
499 case EGL_NATIVE_PIXMAP_KHR:
500 return dri2_drm_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
502 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
507 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
509 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
511 return drmAuthMagic(dri2_dpy->fd, id);
515 swrast_put_image2(__DRIdrawable *driDrawable,
525 struct dri2_egl_surface *dri2_surf = loaderPrivate;
526 int internal_stride, i;
527 struct gbm_dri_bo *bo;
529 if (op != __DRI_SWRAST_IMAGE_OP_DRAW &&
530 op != __DRI_SWRAST_IMAGE_OP_SWAP)
533 if (get_swrast_front_bo(dri2_surf) < 0)
536 bo = gbm_dri_bo(dri2_surf->current->bo);
537 if (gbm_dri_bo_map_dumb(bo) == NULL)
540 internal_stride = bo->base.base.stride;
542 for (i = 0; i < height; i++) {
543 memcpy(bo->map + (x + i) * internal_stride + y,
544 data + i * stride, stride);
547 gbm_dri_bo_unmap_dumb(bo);
551 swrast_get_image(__DRIdrawable *driDrawable,
559 struct dri2_egl_surface *dri2_surf = loaderPrivate;
560 int internal_stride, stride, i;
561 struct gbm_dri_bo *bo;
563 if (get_swrast_front_bo(dri2_surf) < 0)
566 bo = gbm_dri_bo(dri2_surf->current->bo);
567 if (gbm_dri_bo_map_dumb(bo) == NULL)
570 internal_stride = bo->base.base.stride;
573 for (i = 0; i < height; i++) {
574 memcpy(data + i * stride,
575 bo->map + (x + i) * internal_stride + y, stride);
578 gbm_dri_bo_unmap_dumb(bo);
581 static struct dri2_egl_display_vtbl dri2_drm_display_vtbl = {
582 .authenticate = dri2_drm_authenticate,
583 .create_window_surface = dri2_drm_create_window_surface,
584 .create_pixmap_surface = dri2_drm_create_pixmap_surface,
585 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
586 .destroy_surface = dri2_drm_destroy_surface,
587 .create_image = dri2_drm_create_image_khr,
588 .swap_interval = dri2_fallback_swap_interval,
589 .swap_buffers = dri2_drm_swap_buffers,
590 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
591 .swap_buffers_region = dri2_fallback_swap_buffers_region,
592 .post_sub_buffer = dri2_fallback_post_sub_buffer,
593 .copy_buffers = dri2_fallback_copy_buffers,
594 .query_buffer_age = dri2_drm_query_buffer_age,
595 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
596 .get_sync_values = dri2_fallback_get_sync_values,
597 .get_dri_drawable = dri2_surface_get_dri_drawable,
601 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
603 struct dri2_egl_display *dri2_dpy;
604 struct gbm_device *gbm;
608 loader_set_logger(_eglLog);
610 dri2_dpy = calloc(1, sizeof *dri2_dpy);
612 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
614 disp->DriverData = (void *) dri2_dpy;
616 gbm = disp->PlatformDisplay;
619 int n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, 0);
620 if (n != -1 && n < sizeof(buf))
621 fd = loader_open_device(buf);
623 fd = loader_open_device("/dev/dri/card0");
624 dri2_dpy->own_device = 1;
625 gbm = gbm_create_device(fd);
629 fd = fcntl(gbm_device_get_fd(gbm), F_DUPFD_CLOEXEC, 3);
634 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0)
637 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
638 if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI)
642 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
643 dri2_dpy->driver_name = strdup(dri2_dpy->gbm_dri->base.driver_name);
645 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
646 dri2_dpy->core = dri2_dpy->gbm_dri->core;
647 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
648 dri2_dpy->image = dri2_dpy->gbm_dri->image;
649 dri2_dpy->flush = dri2_dpy->gbm_dri->flush;
650 dri2_dpy->swrast = dri2_dpy->gbm_dri->swrast;
651 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
653 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
654 dri2_dpy->gbm_dri->lookup_user_data = disp;
656 dri2_dpy->gbm_dri->get_buffers = dri2_drm_get_buffers;
657 dri2_dpy->gbm_dri->flush_front_buffer = dri2_drm_flush_front_buffer;
658 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_drm_get_buffers_with_format;
659 dri2_dpy->gbm_dri->image_get_buffers = dri2_drm_image_get_buffers;
660 dri2_dpy->gbm_dri->swrast_put_image2 = swrast_put_image2;
661 dri2_dpy->gbm_dri->swrast_get_image = swrast_get_image;
663 dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer;
664 dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer;
665 dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers;
667 dri2_setup_screen(disp);
669 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
670 EGLint format, attr_list[3];
671 unsigned int red, alpha;
673 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
674 __DRI_ATTRIB_RED_MASK, &red);
675 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[i],
676 __DRI_ATTRIB_ALPHA_MASK, &alpha);
677 if (red == 0x3ff00000 && alpha == 0x00000000)
678 format = GBM_FORMAT_XRGB2101010;
679 else if (red == 0x3ff00000 && alpha == 0xc0000000)
680 format = GBM_FORMAT_ARGB2101010;
681 else if (red == 0x00ff0000 && alpha == 0x00000000)
682 format = GBM_FORMAT_XRGB8888;
683 else if (red == 0x00ff0000 && alpha == 0xff000000)
684 format = GBM_FORMAT_ARGB8888;
685 else if (red == 0xf800)
686 format = GBM_FORMAT_RGB565;
690 attr_list[0] = EGL_NATIVE_VISUAL_ID;
691 attr_list[1] = format;
692 attr_list[2] = EGL_NONE;
694 dri2_add_config(disp, dri2_dpy->driver_configs[i],
695 i + 1, EGL_WINDOW_BIT, attr_list, NULL);
698 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
700 disp->Extensions.EXT_buffer_age = EGL_TRUE;
702 #ifdef HAVE_WAYLAND_PLATFORM
703 if (dri2_dpy->image) {
704 if (dri2_dpy->image->base.version >= 10 &&
705 dri2_dpy->image->getCapabilities != NULL) {
709 dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
710 disp->Extensions.WL_bind_wayland_display =
711 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
713 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
717 /* Fill vtbl last to prevent accidentally calling virtual function during
720 dri2_dpy->vtbl = &dri2_drm_display_vtbl;
729 disp->DriverData = NULL;