2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
39 #include "drm-uapi/drm_fourcc.h"
43 #include "egl_dri2_fallbacks.h"
45 #include "util/u_vector.h"
46 #include "util/anon_file.h"
47 #include "eglglobals.h"
49 #include <wayland-egl-backend.h>
50 #include <wayland-client.h>
51 #include "wayland-drm-client-protocol.h"
52 #include "linux-dmabuf-unstable-v1-client-protocol.h"
54 /* cheesy workaround until wayland 1.18 is released */
55 #if WAYLAND_VERSION_MAJOR > 1 || \
56 (WAYLAND_VERSION_MAJOR == 1 && WAYLAND_VERSION_MINOR < 18)
57 #define WL_SHM_FORMAT_ABGR16161616F 0x48344241
58 #define WL_SHM_FORMAT_XBGR16161616F 0x48344258
62 * The index of entries in this table is used as a bitmask in
63 * dri2_dpy->formats, which tracks the formats supported by our server.
65 static const struct dri2_wl_visual
{
66 const char *format_name
;
67 uint32_t wl_drm_format
;
68 uint32_t wl_shm_format
;
70 /* alt_dri_image_format is a substitute wl_buffer format to use for a
71 * wl-server unsupported dri_image_format, ie. some other dri_image_format in
72 * the table, of the same precision but with different channel ordering, or
73 * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported.
74 * The code checks if alt_dri_image_format can be used as a fallback for a
75 * dri_image_format for a given wl-server implementation.
77 int alt_dri_image_format
;
80 unsigned int rgba_sizes
[4];
81 } dri2_wl_visuals
[] = {
84 WL_DRM_FORMAT_ABGR16F
, WL_SHM_FORMAT_ABGR16161616F
,
85 __DRI_IMAGE_FORMAT_ABGR16161616F
, 0, 64,
91 WL_DRM_FORMAT_XBGR16F
, WL_SHM_FORMAT_XBGR16161616F
,
92 __DRI_IMAGE_FORMAT_XBGR16161616F
, 0, 64,
98 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
99 __DRI_IMAGE_FORMAT_XRGB2101010
, __DRI_IMAGE_FORMAT_XBGR2101010
, 32,
105 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
106 __DRI_IMAGE_FORMAT_ARGB2101010
, __DRI_IMAGE_FORMAT_ABGR2101010
, 32,
112 WL_DRM_FORMAT_XBGR2101010
, WL_SHM_FORMAT_XBGR2101010
,
113 __DRI_IMAGE_FORMAT_XBGR2101010
, __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
119 WL_DRM_FORMAT_ABGR2101010
, WL_SHM_FORMAT_ABGR2101010
,
120 __DRI_IMAGE_FORMAT_ABGR2101010
, __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
126 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
127 __DRI_IMAGE_FORMAT_XRGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
133 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
134 __DRI_IMAGE_FORMAT_ARGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
140 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
141 __DRI_IMAGE_FORMAT_RGB565
, __DRI_IMAGE_FORMAT_NONE
, 16,
147 static_assert(ARRAY_SIZE(dri2_wl_visuals
) <= EGL_DRI2_MAX_FORMATS
,
148 "dri2_egl_display::formats is not large enough for "
149 "the formats in dri2_wl_visuals");
152 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
153 const __DRIconfig
*config
)
156 unsigned int sizes
[4];
158 dri2_get_shifts_and_sizes(dri2_dpy
->core
, config
, shifts
, sizes
);
160 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
161 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
163 if (shifts
[0] == wl_visual
->rgba_shifts
[0] &&
164 shifts
[1] == wl_visual
->rgba_shifts
[1] &&
165 shifts
[2] == wl_visual
->rgba_shifts
[2] &&
166 shifts
[3] == wl_visual
->rgba_shifts
[3] &&
167 sizes
[0] == wl_visual
->rgba_sizes
[0] &&
168 sizes
[1] == wl_visual
->rgba_sizes
[1] &&
169 sizes
[2] == wl_visual
->rgba_sizes
[2] &&
170 sizes
[3] == wl_visual
->rgba_sizes
[3]) {
179 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
181 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
182 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
184 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
192 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
194 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
195 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
203 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
205 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
206 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
214 dri2_wl_is_format_supported(void* user_data
, uint32_t format
)
216 _EGLDisplay
*disp
= (_EGLDisplay
*) user_data
;
217 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
218 int j
= dri2_wl_visual_idx_from_fourcc(format
);
223 for (int i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
224 if (j
== dri2_wl_visual_idx_from_config(dri2_dpy
,
225 dri2_dpy
->driver_configs
[i
]))
232 roundtrip(struct dri2_egl_display
*dri2_dpy
)
234 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
238 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
240 struct dri2_egl_surface
*dri2_surf
= data
;
243 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
244 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
247 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
249 if (dri2_surf
->color_buffers
[i
].wl_release
) {
250 wl_buffer_destroy(buffer
);
251 dri2_surf
->color_buffers
[i
].wl_release
= false;
252 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
255 dri2_surf
->color_buffers
[i
].locked
= false;
258 static const struct wl_buffer_listener wl_buffer_listener
= {
259 .release
= wl_buffer_release
263 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
265 struct dri2_egl_surface
*dri2_surf
= data
;
266 struct dri2_egl_display
*dri2_dpy
=
267 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
269 /* Update the surface size as soon as native window is resized; from user
270 * pov, this makes the effect that resize is done immediately after native
271 * window resize, without requiring to wait until the first draw.
273 * A more detailed and lengthy explanation can be found at
274 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
276 if (!dri2_surf
->back
) {
277 dri2_surf
->base
.Width
= wl_win
->width
;
278 dri2_surf
->base
.Height
= wl_win
->height
;
280 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
284 destroy_window_callback(void *data
)
286 struct dri2_egl_surface
*dri2_surf
= data
;
287 dri2_surf
->wl_win
= NULL
;
290 static struct wl_surface
*
291 get_wl_surface_proxy(struct wl_egl_window
*window
)
293 /* Version 3 of wl_egl_window introduced a version field at the same
294 * location where a pointer to wl_surface was stored. Thus, if
295 * window->version is dereferenceable, we've been given an older version of
296 * wl_egl_window, and window->version points to wl_surface */
297 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
298 return wl_proxy_create_wrapper((void *)(window
->version
));
300 return wl_proxy_create_wrapper(window
->surface
);
304 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
307 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
308 _EGLConfig
*conf
, void *native_window
,
309 const EGLint
*attrib_list
)
311 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
312 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
313 struct wl_egl_window
*window
= native_window
;
314 struct dri2_egl_surface
*dri2_surf
;
316 const __DRIconfig
*config
;
318 dri2_surf
= calloc(1, sizeof *dri2_surf
);
320 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
324 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
325 attrib_list
, false, native_window
))
328 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
329 dri2_surf
->base
.GLColorspace
);
332 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
336 dri2_surf
->base
.Width
= window
->width
;
337 dri2_surf
->base
.Height
= window
->height
;
339 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
340 assert(visual_idx
!= -1);
342 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
343 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
345 assert(dri2_dpy
->wl_shm
);
346 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
349 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
350 if (!dri2_surf
->wl_queue
) {
351 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
355 if (dri2_dpy
->wl_drm
) {
356 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
357 if (!dri2_surf
->wl_drm_wrapper
) {
358 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
361 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
362 dri2_surf
->wl_queue
);
365 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
366 if (!dri2_surf
->wl_dpy_wrapper
) {
367 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
370 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
371 dri2_surf
->wl_queue
);
373 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
374 if (!dri2_surf
->wl_surface_wrapper
) {
375 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
376 goto cleanup_dpy_wrapper
;
378 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
379 dri2_surf
->wl_queue
);
381 dri2_surf
->wl_win
= window
;
382 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
383 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
385 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
387 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
388 goto cleanup_surf_wrapper
;
390 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
392 return &dri2_surf
->base
;
394 cleanup_surf_wrapper
:
395 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
397 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
399 if (dri2_surf
->wl_drm_wrapper
)
400 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
402 wl_event_queue_destroy(dri2_surf
->wl_queue
);
410 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
411 _EGLConfig
*conf
, void *native_window
,
412 const EGLint
*attrib_list
)
414 /* From the EGL_EXT_platform_wayland spec, version 3:
416 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
417 * that belongs to Wayland. Any such call fails and generates
420 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
426 * Called via eglDestroySurface(), drv->API.DestroySurface().
429 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
431 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
432 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
436 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
438 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
439 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
440 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
441 if (dri2_surf
->color_buffers
[i
].dri_image
)
442 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
443 if (dri2_surf
->color_buffers
[i
].linear_copy
)
444 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
445 if (dri2_surf
->color_buffers
[i
].data
)
446 munmap(dri2_surf
->color_buffers
[i
].data
,
447 dri2_surf
->color_buffers
[i
].data_size
);
451 dri2_egl_surface_free_local_buffers(dri2_surf
);
453 if (dri2_surf
->throttle_callback
)
454 wl_callback_destroy(dri2_surf
->throttle_callback
);
456 if (dri2_surf
->wl_win
) {
457 dri2_surf
->wl_win
->driver_private
= NULL
;
458 dri2_surf
->wl_win
->resize_callback
= NULL
;
459 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
462 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
463 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
464 if (dri2_surf
->wl_drm_wrapper
)
465 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
466 wl_event_queue_destroy(dri2_surf
->wl_queue
);
468 dri2_fini_surface(surf
);
475 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
477 struct dri2_egl_display
*dri2_dpy
=
478 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
480 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
481 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
482 if (dri2_surf
->color_buffers
[i
].locked
) {
483 dri2_surf
->color_buffers
[i
].wl_release
= true;
485 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
486 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
489 if (dri2_surf
->color_buffers
[i
].dri_image
)
490 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
491 if (dri2_surf
->color_buffers
[i
].linear_copy
)
492 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
493 if (dri2_surf
->color_buffers
[i
].data
)
494 munmap(dri2_surf
->color_buffers
[i
].data
,
495 dri2_surf
->color_buffers
[i
].data_size
);
497 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
498 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
499 dri2_surf
->color_buffers
[i
].data
= NULL
;
503 dri2_egl_surface_free_local_buffers(dri2_surf
);
507 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
509 struct dri2_egl_display
*dri2_dpy
=
510 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
513 unsigned int dri_image_format
;
514 unsigned int linear_dri_image_format
;
518 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
519 assert(visual_idx
!= -1);
520 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
521 linear_dri_image_format
= dri_image_format
;
522 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
523 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
525 /* Substitute dri image format if server does not support original format */
526 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
527 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
529 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
530 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
533 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
534 assert(BITSET_TEST(dri2_dpy
->formats
,
535 dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
537 /* There might be a buffer release already queued that wasn't processed */
538 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
540 while (dri2_surf
->back
== NULL
) {
541 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
542 /* Get an unlocked buffer, preferably one with a dri_buffer
543 * already allocated. */
544 if (dri2_surf
->color_buffers
[i
].locked
)
546 if (dri2_surf
->back
== NULL
)
547 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
548 else if (dri2_surf
->back
->dri_image
== NULL
)
549 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
555 /* If we don't have a buffer, then block on the server to release one for
556 * us, and try again. wl_display_dispatch_queue will process any pending
557 * events, however not all servers flush on issuing a buffer release
558 * event. So, we spam the server with roundtrips as they always cause a
561 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
562 dri2_surf
->wl_queue
) < 0)
566 if (dri2_surf
->back
== NULL
)
569 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
571 if (dri2_dpy
->is_different_gpu
&&
572 dri2_surf
->back
->linear_copy
== NULL
) {
573 /* The LINEAR modifier should be a perfect alias of the LINEAR use
574 * flag; try the new interface first before the old, then fall back. */
575 if (dri2_dpy
->image
->base
.version
>= 15 &&
576 dri2_dpy
->image
->createImageWithModifiers
) {
577 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
579 dri2_surf
->back
->linear_copy
=
580 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
581 dri2_surf
->base
.Width
,
582 dri2_surf
->base
.Height
,
583 linear_dri_image_format
,
588 dri2_surf
->back
->linear_copy
=
589 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
590 dri2_surf
->base
.Width
,
591 dri2_surf
->base
.Height
,
592 linear_dri_image_format
,
594 __DRI_IMAGE_USE_LINEAR
,
597 if (dri2_surf
->back
->linear_copy
== NULL
)
601 if (dri2_surf
->back
->dri_image
== NULL
) {
602 /* If our DRIImage implementation does not support
603 * createImageWithModifiers, then fall back to the old createImage,
604 * and hope it allocates an image which is acceptable to the winsys.
606 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
607 dri2_dpy
->image
->createImageWithModifiers
) {
608 dri2_surf
->back
->dri_image
=
609 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
610 dri2_surf
->base
.Width
,
611 dri2_surf
->base
.Height
,
617 dri2_surf
->back
->dri_image
=
618 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
619 dri2_surf
->base
.Width
,
620 dri2_surf
->base
.Height
,
622 dri2_dpy
->is_different_gpu
?
627 dri2_surf
->back
->age
= 0;
629 if (dri2_surf
->back
->dri_image
== NULL
)
632 dri2_surf
->back
->locked
= true;
639 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
641 struct dri2_egl_display
*dri2_dpy
=
642 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
646 image
= dri2_surf
->back
->dri_image
;
648 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
649 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
651 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
653 buffer
->pitch
= pitch
;
659 update_buffers(struct dri2_egl_surface
*dri2_surf
)
661 struct dri2_egl_display
*dri2_dpy
=
662 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
664 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
665 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
667 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
668 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
669 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
670 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
673 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
674 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
675 dri2_wl_release_buffers(dri2_surf
);
678 if (get_back_bo(dri2_surf
) < 0) {
679 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
683 /* If we have an extra unlocked buffer at this point, we had to do triple
684 * buffering for a while, but now can go back to just double buffering.
685 * That means we can free any unlocked buffer now. */
686 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
687 if (!dri2_surf
->color_buffers
[i
].locked
&&
688 dri2_surf
->color_buffers
[i
].wl_buffer
) {
689 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
690 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
691 if (dri2_dpy
->is_different_gpu
)
692 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
693 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
694 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
695 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
703 update_buffers_if_needed(struct dri2_egl_surface
*dri2_surf
)
705 if (dri2_surf
->back
!= NULL
)
708 return update_buffers(dri2_surf
);
712 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
713 int *width
, int *height
,
714 unsigned int *attachments
, int count
,
715 int *out_count
, void *loaderPrivate
)
717 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
720 if (update_buffers(dri2_surf
) < 0)
723 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
726 switch (attachments
[i
]) {
727 case __DRI_BUFFER_BACK_LEFT
:
728 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
731 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
735 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
738 dri2_surf
->buffers
[j
] = *local
;
747 *width
= dri2_surf
->base
.Width
;
748 *height
= dri2_surf
->base
.Height
;
750 return dri2_surf
->buffers
;
754 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
755 int *width
, int *height
,
756 unsigned int *attachments
, int count
,
757 int *out_count
, void *loaderPrivate
)
759 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
760 unsigned int *attachments_with_format
;
762 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
764 if (visual_idx
== -1)
767 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
768 if (!attachments_with_format
) {
773 for (int i
= 0; i
< count
; ++i
) {
774 attachments_with_format
[2*i
] = attachments
[i
];
775 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
779 dri2_wl_get_buffers_with_format(driDrawable
,
781 attachments_with_format
, count
,
782 out_count
, loaderPrivate
);
784 free(attachments_with_format
);
790 image_get_buffers(__DRIdrawable
*driDrawable
,
794 uint32_t buffer_mask
,
795 struct __DRIimageList
*buffers
)
797 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
799 if (update_buffers(dri2_surf
) < 0)
802 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
803 buffers
->back
= dri2_surf
->back
->dri_image
;
809 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
812 (void) loaderPrivate
;
815 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
816 .base
= { __DRI_DRI2_LOADER
, 3 },
818 .getBuffers
= dri2_wl_get_buffers
,
819 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
820 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
823 static const __DRIimageLoaderExtension image_loader_extension
= {
824 .base
= { __DRI_IMAGE_LOADER
, 1 },
826 .getBuffers
= image_get_buffers
,
827 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
831 wayland_throttle_callback(void *data
,
832 struct wl_callback
*callback
,
835 struct dri2_egl_surface
*dri2_surf
= data
;
837 dri2_surf
->throttle_callback
= NULL
;
838 wl_callback_destroy(callback
);
841 static const struct wl_callback_listener throttle_listener
= {
842 .done
= wayland_throttle_callback
846 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
847 __DRIimage
*image
, int *fourcc
)
853 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
858 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
863 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
864 if (visual_idx
== -1)
867 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
871 static struct wl_buffer
*
872 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
873 struct dri2_egl_surface
*dri2_surf
,
876 struct wl_buffer
*ret
;
878 int width
, height
, fourcc
, num_planes
;
879 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
881 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
882 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
884 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
888 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
893 if (dri2_dpy
->image
->base
.version
>= 15) {
896 query
= dri2_dpy
->image
->queryImage(image
,
897 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
899 query
&= dri2_dpy
->image
->queryImage(image
,
900 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
903 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
907 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
908 struct zwp_linux_buffer_params_v1
*params
;
911 /* We don't need a wrapper for wl_dmabuf objects, because we have to
912 * create the intermediate params object; we can set the queue on this,
913 * and the wl_buffer inherits it race-free. */
914 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
916 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
918 for (i
= 0; i
< num_planes
; i
++) {
923 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
929 query
= dri2_dpy
->image
->queryImage(p_image
,
930 __DRI_IMAGE_ATTRIB_FD
,
932 query
&= dri2_dpy
->image
->queryImage(p_image
,
933 __DRI_IMAGE_ATTRIB_STRIDE
,
935 query
&= dri2_dpy
->image
->queryImage(p_image
,
936 __DRI_IMAGE_ATTRIB_OFFSET
,
938 if (image
!= p_image
)
939 dri2_dpy
->image
->destroyImage(p_image
);
944 zwp_linux_buffer_params_v1_destroy(params
);
948 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
949 modifier
>> 32, modifier
& 0xffffffff);
953 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
955 zwp_linux_buffer_params_v1_destroy(params
);
956 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
957 struct wl_drm
*wl_drm
=
958 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
964 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
965 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
966 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
970 struct wl_drm
*wl_drm
=
971 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
977 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
978 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
979 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
986 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
990 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
991 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
994 for (int i
= 0; i
< n_rects
; i
++) {
995 const int *rect
= &rects
[i
* 4];
997 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
999 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
1005 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
1008 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
1011 const EGLint
*rects
,
1014 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1015 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1017 while (dri2_surf
->throttle_callback
!= NULL
)
1018 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1019 dri2_surf
->wl_queue
) == -1)
1022 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
1023 if (dri2_surf
->color_buffers
[i
].age
> 0)
1024 dri2_surf
->color_buffers
[i
].age
++;
1026 /* Make sure we have a back buffer in case we're swapping without ever
1028 if (update_buffers_if_needed(dri2_surf
) < 0)
1029 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
1031 if (draw
->SwapInterval
> 0) {
1032 dri2_surf
->throttle_callback
=
1033 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1034 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1035 &throttle_listener
, dri2_surf
);
1038 dri2_surf
->back
->age
= 1;
1039 dri2_surf
->current
= dri2_surf
->back
;
1040 dri2_surf
->back
= NULL
;
1042 if (!dri2_surf
->current
->wl_buffer
) {
1045 if (dri2_dpy
->is_different_gpu
)
1046 image
= dri2_surf
->current
->linear_copy
;
1048 image
= dri2_surf
->current
->dri_image
;
1050 dri2_surf
->current
->wl_buffer
=
1051 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1053 dri2_surf
->current
->wl_release
= false;
1055 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1056 &wl_buffer_listener
, dri2_surf
);
1059 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1060 dri2_surf
->current
->wl_buffer
,
1061 dri2_surf
->dx
, dri2_surf
->dy
);
1063 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1064 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1065 /* reset resize growing parameters */
1069 /* If the compositor doesn't support damage_buffer, we deliberately
1070 * ignore the damage region and post maximum damage, due to
1071 * https://bugs.freedesktop.org/78190 */
1072 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1073 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1074 0, 0, INT32_MAX
, INT32_MAX
);
1076 if (dri2_dpy
->is_different_gpu
) {
1077 _EGLContext
*ctx
= _eglGetCurrentContext();
1078 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1079 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1080 dri2_surf
->current
->linear_copy
,
1081 dri2_surf
->current
->dri_image
,
1082 0, 0, dri2_surf
->base
.Width
,
1083 dri2_surf
->base
.Height
,
1084 0, 0, dri2_surf
->base
.Width
,
1085 dri2_surf
->base
.Height
, 0);
1088 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1089 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1091 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1093 /* If we're not waiting for a frame callback then we'll at least throttle
1094 * to a sync callback so that we always give a chance for the compositor to
1095 * handle the commit and send a release event before checking for a free
1097 if (dri2_surf
->throttle_callback
== NULL
) {
1098 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1099 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1100 &throttle_listener
, dri2_surf
);
1103 wl_display_flush(dri2_dpy
->wl_dpy
);
1109 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1110 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1112 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1114 if (update_buffers_if_needed(dri2_surf
) < 0) {
1115 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1119 return dri2_surf
->back
->age
;
1123 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1125 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1128 static struct wl_buffer
*
1129 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1133 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1134 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1135 __DRIimage
*image
= dri2_img
->dri_image
;
1136 struct wl_buffer
*buffer
;
1137 int format
, visual_idx
;
1139 /* Check the upstream display supports this buffer's format. */
1140 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1141 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1142 if (visual_idx
== -1)
1145 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
1148 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1150 /* The buffer object will have been created with our internal event queue
1151 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1152 * buffer to be used by the application so we'll reset it to the display's
1153 * default event queue. This isn't actually racy, as the only event the
1154 * buffer can get is a buffer release, which doesn't happen with an explicit
1157 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1162 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1167 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1169 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1172 if (dri2_dpy
->is_render_node
) {
1173 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1174 "authenticate for render-nodes");
1177 dri2_dpy
->authenticated
= false;
1179 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1180 if (roundtrip(dri2_dpy
) < 0)
1183 if (!dri2_dpy
->authenticated
)
1186 /* reset authenticated */
1187 dri2_dpy
->authenticated
= true;
1193 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1195 struct dri2_egl_display
*dri2_dpy
= data
;
1198 dri2_dpy
->device_name
= strdup(device
);
1199 if (!dri2_dpy
->device_name
)
1202 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1203 if (dri2_dpy
->fd
== -1) {
1204 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1205 dri2_dpy
->device_name
, strerror(errno
));
1206 free(dri2_dpy
->device_name
);
1207 dri2_dpy
->device_name
= NULL
;
1211 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1212 dri2_dpy
->authenticated
= true;
1214 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1215 close(dri2_dpy
->fd
);
1217 free(dri2_dpy
->device_name
);
1218 dri2_dpy
->device_name
= NULL
;
1219 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1222 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1227 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1229 struct dri2_egl_display
*dri2_dpy
= data
;
1230 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1232 if (visual_idx
== -1)
1235 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1239 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1241 struct dri2_egl_display
*dri2_dpy
= data
;
1243 dri2_dpy
->capabilities
= value
;
1247 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1249 struct dri2_egl_display
*dri2_dpy
= data
;
1251 dri2_dpy
->authenticated
= true;
1254 static const struct wl_drm_listener drm_listener
= {
1255 .device
= drm_handle_device
,
1256 .format
= drm_handle_format
,
1257 .authenticated
= drm_handle_authenticated
,
1258 .capabilities
= drm_handle_capabilities
1262 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1265 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1269 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1270 uint32_t format
, uint32_t modifier_hi
,
1271 uint32_t modifier_lo
)
1273 struct dri2_egl_display
*dri2_dpy
= data
;
1274 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1277 if (visual_idx
== -1)
1280 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1281 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1284 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1286 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1287 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1290 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1291 .format
= dmabuf_ignore_format
,
1292 .modifier
= dmabuf_handle_modifier
,
1296 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1297 uint32_t name
, const char *interface
,
1300 struct dri2_egl_display
*dri2_dpy
= data
;
1302 if (strcmp(interface
, "wl_drm") == 0) {
1304 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1305 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1306 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1307 dri2_dpy
->wl_dmabuf
=
1308 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1310 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1316 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1321 static const struct wl_registry_listener registry_listener_drm
= {
1322 .global
= registry_handle_global_drm
,
1323 .global_remove
= registry_handle_global_remove
1327 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1329 /* We can't use values greater than 1 on Wayland because we are using the
1330 * frame callback to synchronise the frame and the only way we be sure to
1331 * get a frame callback is to attach a new buffer. Therefore we can't just
1332 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1334 dri2_setup_swap_interval(disp
, 1);
1337 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1338 .authenticate
= dri2_wl_authenticate
,
1339 .create_window_surface
= dri2_wl_create_window_surface
,
1340 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1341 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1342 .destroy_surface
= dri2_wl_destroy_surface
,
1343 .create_image
= dri2_create_image_khr
,
1344 .swap_buffers
= dri2_wl_swap_buffers
,
1345 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1346 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1347 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1348 .copy_buffers
= dri2_fallback_copy_buffers
,
1349 .query_buffer_age
= dri2_wl_query_buffer_age
,
1350 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1351 .get_sync_values
= dri2_fallback_get_sync_values
,
1352 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1355 static const __DRIextension
*dri2_loader_extensions
[] = {
1356 &dri2_loader_extension
.base
,
1357 &image_loader_extension
.base
,
1358 &image_lookup_extension
.base
,
1359 &use_invalidate
.base
,
1363 static const __DRIextension
*image_loader_extensions
[] = {
1364 &image_loader_extension
.base
,
1365 &image_lookup_extension
.base
,
1366 &use_invalidate
.base
,
1371 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1373 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1374 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1375 unsigned int count
= 0;
1378 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1381 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1382 struct dri2_egl_config
*dri2_conf
;
1384 if (!BITSET_TEST(dri2_dpy
->formats
, j
))
1387 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1388 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_shifts
, dri2_wl_visuals
[j
].rgba_sizes
);
1390 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1397 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1398 struct dri2_egl_config
*dri2_conf
;
1399 int alt_dri_image_format
, c
, s
;
1401 /* No match for config. Try if we can blitImage convert to a visual */
1402 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1403 dri2_dpy
->driver_configs
[i
]);
1408 /* Find optimal target visual for blitImage conversion, if any. */
1409 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1410 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1412 if (s
== -1 || !BITSET_TEST(dri2_dpy
->formats
, s
))
1415 /* Visual s works for the Wayland server, and c can be converted into s
1416 * by our client gpu during PRIME blitImage conversion to a linear
1417 * wl_buffer, so add visual c as supported by the client renderer.
1419 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1420 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1421 dri2_wl_visuals
[c
].rgba_shifts
,
1422 dri2_wl_visuals
[c
].rgba_sizes
);
1424 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1427 if (format_count
[c
] == 1)
1428 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1429 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1430 dri2_wl_visuals
[s
].format_name
);
1435 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1436 if (!format_count
[i
]) {
1437 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1438 dri2_wl_visuals
[i
].format_name
);
1442 return (count
!= 0);
1446 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1449 struct dri2_egl_display
*dri2_dpy
;
1451 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1453 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1456 disp
->DriverData
= (void *) dri2_dpy
;
1457 if (disp
->PlatformDisplay
== NULL
) {
1458 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1459 if (dri2_dpy
->wl_dpy
== NULL
)
1461 dri2_dpy
->own_device
= true;
1463 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1466 dri2_dpy
->wl_modifiers
=
1467 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1468 if (!dri2_dpy
->wl_modifiers
)
1470 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1471 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1475 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1477 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1478 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1481 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1482 dri2_dpy
->wl_queue
);
1484 if (dri2_dpy
->own_device
)
1485 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1487 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1488 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1489 ®istry_listener_drm
, dri2_dpy
);
1490 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1493 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1496 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1499 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1500 &dri2_dpy
->is_different_gpu
);
1501 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1503 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1509 if (dri2_dpy
->is_different_gpu
) {
1510 free(dri2_dpy
->device_name
);
1511 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1512 if (!dri2_dpy
->device_name
) {
1513 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1514 "for requested GPU");
1519 /* we have to do the check now, because loader_get_user_preferred_fd
1520 * will return a render-node when the requested gpu is different
1521 * to the server, but also if the client asks for the same gpu than
1522 * the server by requesting its pci-id */
1523 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1525 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1526 if (dri2_dpy
->driver_name
== NULL
) {
1527 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1531 /* render nodes cannot use Gem names, and thus do not support
1532 * the __DRI_DRI2_LOADER extension */
1533 if (!dri2_dpy
->is_render_node
) {
1534 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1535 if (!dri2_load_driver(disp
)) {
1536 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1540 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1541 if (!dri2_load_driver_dri3(disp
)) {
1542 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1547 if (!dri2_create_screen(disp
))
1550 if (!dri2_setup_extensions(disp
))
1553 dri2_setup_screen(disp
);
1555 dri2_wl_setup_swap_interval(disp
);
1557 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1558 * createImageFromFds support indicates that Prime export/import
1559 * is supported by the driver. Fall back to
1560 * gem names if we don't have Prime support. */
1562 if (dri2_dpy
->image
->base
.version
< 7 ||
1563 dri2_dpy
->image
->createImageFromFds
== NULL
)
1564 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1566 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1567 * The server needs to accept them */
1568 if (dri2_dpy
->is_render_node
&&
1569 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1570 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1574 if (dri2_dpy
->is_different_gpu
&&
1575 (dri2_dpy
->image
->base
.version
< 9 ||
1576 dri2_dpy
->image
->blitImage
== NULL
)) {
1577 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1578 "Image extension in the driver is not "
1579 "compatible. Version 9 or later and blitImage() "
1584 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1585 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1589 dri2_set_WL_bind_wayland_display(drv
, disp
);
1590 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1591 * because the buffer of the EGLImage has likely a tiling mode the server
1592 * gpu won't support. These is no way to check for now. Thus do not support the
1594 if (!dri2_dpy
->is_different_gpu
)
1595 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1597 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1599 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1601 /* Fill vtbl last to prevent accidentally calling virtual function during
1604 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1609 dri2_display_destroy(disp
);
1614 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1616 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1618 assume(visual_idx
!= -1);
1620 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1624 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1625 int format
, int w
, int h
,
1626 void **data
, int *size
,
1627 struct wl_buffer
**buffer
)
1629 struct dri2_egl_display
*dri2_dpy
=
1630 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1631 struct wl_shm_pool
*pool
;
1632 int fd
, stride
, size_map
;
1635 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1636 size_map
= h
* stride
;
1638 /* Create a shareable buffer */
1639 fd
= os_create_anonymous_file(size_map
, NULL
);
1643 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1644 if (data_map
== MAP_FAILED
) {
1649 /* Share it in a wl_buffer */
1650 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1651 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1652 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1653 wl_shm_pool_destroy(pool
);
1662 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1664 struct dri2_egl_display
*dri2_dpy
=
1665 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1667 /* we need to do the following operations only once per frame */
1668 if (dri2_surf
->back
)
1671 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1672 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1674 dri2_wl_release_buffers(dri2_surf
);
1676 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1677 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1678 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1679 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1680 dri2_surf
->current
= NULL
;
1683 /* find back buffer */
1685 /* There might be a buffer release already queued that wasn't processed */
1686 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1688 /* try get free buffer already created */
1689 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1690 if (!dri2_surf
->color_buffers
[i
].locked
&&
1691 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1692 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1697 /* else choose any another free location */
1698 if (!dri2_surf
->back
) {
1699 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1700 if (!dri2_surf
->color_buffers
[i
].locked
) {
1701 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1702 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1704 dri2_surf
->base
.Width
,
1705 dri2_surf
->base
.Height
,
1706 &dri2_surf
->back
->data
,
1707 &dri2_surf
->back
->data_size
,
1708 &dri2_surf
->back
->wl_buffer
)) {
1709 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1712 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1713 &wl_buffer_listener
, dri2_surf
);
1719 if (!dri2_surf
->back
) {
1720 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1724 dri2_surf
->back
->locked
= true;
1726 /* If we have an extra unlocked buffer at this point, we had to do triple
1727 * buffering for a while, but now can go back to just double buffering.
1728 * That means we can free any unlocked buffer now. */
1729 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1730 if (!dri2_surf
->color_buffers
[i
].locked
&&
1731 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1732 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1733 munmap(dri2_surf
->color_buffers
[i
].data
,
1734 dri2_surf
->color_buffers
[i
].data_size
);
1735 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1736 dri2_surf
->color_buffers
[i
].data
= NULL
;
1744 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1746 /* if there has been a resize: */
1747 if (!dri2_surf
->current
)
1750 return dri2_surf
->current
->data
;
1754 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1756 assert(dri2_surf
->back
);
1757 return dri2_surf
->back
->data
;
1761 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1763 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1765 while (dri2_surf
->throttle_callback
!= NULL
)
1766 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1767 dri2_surf
->wl_queue
) == -1)
1770 if (dri2_surf
->base
.SwapInterval
> 0) {
1771 dri2_surf
->throttle_callback
=
1772 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1773 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1774 &throttle_listener
, dri2_surf
);
1777 dri2_surf
->current
= dri2_surf
->back
;
1778 dri2_surf
->back
= NULL
;
1780 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1781 dri2_surf
->current
->wl_buffer
,
1782 dri2_surf
->dx
, dri2_surf
->dy
);
1784 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1785 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1786 /* reset resize growing parameters */
1790 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1791 0, 0, INT32_MAX
, INT32_MAX
);
1792 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1794 /* If we're not waiting for a frame callback then we'll at least throttle
1795 * to a sync callback so that we always give a chance for the compositor to
1796 * handle the commit and send a release event before checking for a free
1798 if (dri2_surf
->throttle_callback
== NULL
) {
1799 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1800 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1801 &throttle_listener
, dri2_surf
);
1804 wl_display_flush(dri2_dpy
->wl_dpy
);
1808 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1809 int *x
, int *y
, int *w
, int *h
,
1810 void *loaderPrivate
)
1812 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1814 (void) swrast_update_buffers(dri2_surf
);
1817 *w
= dri2_surf
->base
.Width
;
1818 *h
= dri2_surf
->base
.Height
;
1822 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1823 int x
, int y
, int w
, int h
,
1824 char *data
, void *loaderPrivate
)
1826 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1827 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1828 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1829 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1830 int dst_stride
= copy_width
;
1833 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1835 memset(data
, 0, copy_width
* h
);
1839 assert(data
!= src
);
1840 assert(copy_width
<= src_stride
);
1843 src
+= y
* src_stride
;
1846 if (copy_width
> src_stride
-x_offset
)
1847 copy_width
= src_stride
-x_offset
;
1848 if (h
> dri2_surf
->base
.Height
-y
)
1849 h
= dri2_surf
->base
.Height
-y
;
1852 memcpy(dst
, src
, copy_width
);
1859 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1860 int x
, int y
, int w
, int h
, int stride
,
1861 char *data
, void *loaderPrivate
)
1863 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1864 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1865 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1866 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1869 assert(copy_width
<= stride
);
1871 (void) swrast_update_buffers(dri2_surf
);
1872 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1874 /* partial copy, copy old content */
1875 if (copy_width
< dst_stride
)
1876 dri2_wl_swrast_get_image(draw
, 0, 0,
1877 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1878 dst
, loaderPrivate
);
1881 dst
+= y
* dst_stride
;
1885 /* drivers expect we do these checks (and some rely on it) */
1886 if (copy_width
> dst_stride
-x_offset
)
1887 copy_width
= dst_stride
-x_offset
;
1888 if (h
> dri2_surf
->base
.Height
-y
)
1889 h
= dri2_surf
->base
.Height
-y
;
1892 memcpy(dst
, src
, copy_width
);
1896 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1900 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1901 int x
, int y
, int w
, int h
,
1902 char *data
, void *loaderPrivate
)
1904 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1907 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1908 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1909 stride
, data
, loaderPrivate
);
1913 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1915 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1916 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1918 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1923 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1925 struct dri2_egl_display
*dri2_dpy
= data
;
1926 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1928 if (visual_idx
== -1)
1931 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1934 static const struct wl_shm_listener shm_listener
= {
1935 .format
= shm_handle_format
1939 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1940 uint32_t name
, const char *interface
,
1943 struct dri2_egl_display
*dri2_dpy
= data
;
1945 if (strcmp(interface
, "wl_shm") == 0) {
1947 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1948 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1952 static const struct wl_registry_listener registry_listener_swrast
= {
1953 .global
= registry_handle_global_swrast
,
1954 .global_remove
= registry_handle_global_remove
1957 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1958 .authenticate
= NULL
,
1959 .create_window_surface
= dri2_wl_create_window_surface
,
1960 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1961 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1962 .destroy_surface
= dri2_wl_destroy_surface
,
1963 .create_image
= dri2_create_image_khr
,
1964 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1965 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1966 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1967 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1968 .copy_buffers
= dri2_fallback_copy_buffers
,
1969 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1970 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1971 .get_sync_values
= dri2_fallback_get_sync_values
,
1972 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1975 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1976 .base
= { __DRI_SWRAST_LOADER
, 2 },
1978 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1979 .putImage
= dri2_wl_swrast_put_image
,
1980 .getImage
= dri2_wl_swrast_get_image
,
1981 .putImage2
= dri2_wl_swrast_put_image2
,
1984 static const __DRIextension
*swrast_loader_extensions
[] = {
1985 &swrast_loader_extension
.base
,
1986 &image_lookup_extension
.base
,
1991 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1994 struct dri2_egl_display
*dri2_dpy
;
1996 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1998 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2001 disp
->DriverData
= (void *) dri2_dpy
;
2002 if (disp
->PlatformDisplay
== NULL
) {
2003 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2004 if (dri2_dpy
->wl_dpy
== NULL
)
2006 dri2_dpy
->own_device
= true;
2008 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2011 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
2013 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
2019 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2021 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2022 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2025 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2026 dri2_dpy
->wl_queue
);
2028 if (dri2_dpy
->own_device
)
2029 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2031 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2032 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2033 ®istry_listener_swrast
, dri2_dpy
);
2035 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2038 if (roundtrip(dri2_dpy
) < 0 || !BITSET_TEST_RANGE(dri2_dpy
->formats
,
2039 0, EGL_DRI2_MAX_FORMATS
))
2042 dri2_dpy
->driver_name
= strdup("swrast");
2043 if (!dri2_load_driver_swrast(disp
))
2046 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2048 if (!dri2_create_screen(disp
))
2051 if (!dri2_setup_extensions(disp
))
2054 dri2_setup_screen(disp
);
2056 dri2_wl_setup_swap_interval(disp
);
2058 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2059 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2063 /* Fill vtbl last to prevent accidentally calling virtual function during
2066 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2071 dri2_display_destroy(disp
);
2076 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2078 EGLBoolean initialized
= EGL_FALSE
;
2080 if (!disp
->Options
.ForceSoftware
)
2081 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2084 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2091 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2093 if (dri2_dpy
->wl_drm
)
2094 wl_drm_destroy(dri2_dpy
->wl_drm
);
2095 if (dri2_dpy
->wl_dmabuf
)
2096 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2097 if (dri2_dpy
->wl_shm
)
2098 wl_shm_destroy(dri2_dpy
->wl_shm
);
2099 if (dri2_dpy
->wl_registry
)
2100 wl_registry_destroy(dri2_dpy
->wl_registry
);
2101 if (dri2_dpy
->wl_queue
)
2102 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2103 if (dri2_dpy
->wl_dpy_wrapper
)
2104 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2106 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2107 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2108 free(dri2_dpy
->wl_modifiers
);
2110 if (dri2_dpy
->own_device
)
2111 wl_display_disconnect(dri2_dpy
->wl_dpy
);