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"
44 #include "util/u_vector.h"
45 #include "util/anon_file.h"
46 #include "eglglobals.h"
48 #include <wayland-egl-backend.h>
49 #include <wayland-client.h>
50 #include "wayland-drm-client-protocol.h"
51 #include "linux-dmabuf-unstable-v1-client-protocol.h"
53 /* cheesy workaround until wayland 1.18 is released */
54 #if WAYLAND_VERSION_MAJOR > 1 || \
55 (WAYLAND_VERSION_MAJOR == 1 && WAYLAND_VERSION_MINOR < 18)
56 #define WL_SHM_FORMAT_ABGR16161616F 0x48344241
57 #define WL_SHM_FORMAT_XBGR16161616F 0x48344258
61 * The index of entries in this table is used as a bitmask in
62 * dri2_dpy->formats, which tracks the formats supported by our server.
64 static const struct dri2_wl_visual
{
65 const char *format_name
;
66 uint32_t wl_drm_format
;
67 uint32_t wl_shm_format
;
69 /* alt_dri_image_format is a substitute wl_buffer format to use for a
70 * wl-server unsupported dri_image_format, ie. some other dri_image_format in
71 * the table, of the same precision but with different channel ordering, or
72 * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported.
73 * The code checks if alt_dri_image_format can be used as a fallback for a
74 * dri_image_format for a given wl-server implementation.
76 int alt_dri_image_format
;
79 unsigned int rgba_sizes
[4];
80 } dri2_wl_visuals
[] = {
83 WL_DRM_FORMAT_ABGR16F
, WL_SHM_FORMAT_ABGR16161616F
,
84 __DRI_IMAGE_FORMAT_ABGR16161616F
, 0, 64,
90 WL_DRM_FORMAT_XBGR16F
, WL_SHM_FORMAT_XBGR16161616F
,
91 __DRI_IMAGE_FORMAT_XBGR16161616F
, 0, 64,
97 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
98 __DRI_IMAGE_FORMAT_XRGB2101010
, __DRI_IMAGE_FORMAT_XBGR2101010
, 32,
104 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
105 __DRI_IMAGE_FORMAT_ARGB2101010
, __DRI_IMAGE_FORMAT_ABGR2101010
, 32,
111 WL_DRM_FORMAT_XBGR2101010
, WL_SHM_FORMAT_XBGR2101010
,
112 __DRI_IMAGE_FORMAT_XBGR2101010
, __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
118 WL_DRM_FORMAT_ABGR2101010
, WL_SHM_FORMAT_ABGR2101010
,
119 __DRI_IMAGE_FORMAT_ABGR2101010
, __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
125 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
126 __DRI_IMAGE_FORMAT_XRGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
132 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
133 __DRI_IMAGE_FORMAT_ARGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
139 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
140 __DRI_IMAGE_FORMAT_RGB565
, __DRI_IMAGE_FORMAT_NONE
, 16,
146 static_assert(ARRAY_SIZE(dri2_wl_visuals
) <= EGL_DRI2_MAX_FORMATS
,
147 "dri2_egl_display::formats is not large enough for "
148 "the formats in dri2_wl_visuals");
151 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
152 const __DRIconfig
*config
)
155 unsigned int sizes
[4];
157 dri2_get_shifts_and_sizes(dri2_dpy
->core
, config
, shifts
, sizes
);
159 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
160 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
162 if (shifts
[0] == wl_visual
->rgba_shifts
[0] &&
163 shifts
[1] == wl_visual
->rgba_shifts
[1] &&
164 shifts
[2] == wl_visual
->rgba_shifts
[2] &&
165 shifts
[3] == wl_visual
->rgba_shifts
[3] &&
166 sizes
[0] == wl_visual
->rgba_sizes
[0] &&
167 sizes
[1] == wl_visual
->rgba_sizes
[1] &&
168 sizes
[2] == wl_visual
->rgba_sizes
[2] &&
169 sizes
[3] == wl_visual
->rgba_sizes
[3]) {
178 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
180 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
181 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
183 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
191 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
193 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
194 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
202 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
204 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
205 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
213 dri2_wl_is_format_supported(void* user_data
, uint32_t format
)
215 _EGLDisplay
*disp
= (_EGLDisplay
*) user_data
;
216 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
217 int j
= dri2_wl_visual_idx_from_fourcc(format
);
222 for (int i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
223 if (j
== dri2_wl_visual_idx_from_config(dri2_dpy
,
224 dri2_dpy
->driver_configs
[i
]))
231 roundtrip(struct dri2_egl_display
*dri2_dpy
)
233 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
237 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
239 struct dri2_egl_surface
*dri2_surf
= data
;
242 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
243 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
246 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
248 if (dri2_surf
->color_buffers
[i
].wl_release
) {
249 wl_buffer_destroy(buffer
);
250 dri2_surf
->color_buffers
[i
].wl_release
= false;
251 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
254 dri2_surf
->color_buffers
[i
].locked
= false;
257 static const struct wl_buffer_listener wl_buffer_listener
= {
258 .release
= wl_buffer_release
262 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
264 struct dri2_egl_surface
*dri2_surf
= data
;
265 struct dri2_egl_display
*dri2_dpy
=
266 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
268 if (dri2_surf
->base
.Width
== wl_win
->width
&&
269 dri2_surf
->base
.Height
== wl_win
->height
)
272 /* Update the surface size as soon as native window is resized; from user
273 * pov, this makes the effect that resize is done immediately after native
274 * window resize, without requiring to wait until the first draw.
276 * A more detailed and lengthy explanation can be found at
277 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
279 if (!dri2_surf
->back
) {
280 dri2_surf
->base
.Width
= wl_win
->width
;
281 dri2_surf
->base
.Height
= wl_win
->height
;
283 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
287 destroy_window_callback(void *data
)
289 struct dri2_egl_surface
*dri2_surf
= data
;
290 dri2_surf
->wl_win
= NULL
;
293 static struct wl_surface
*
294 get_wl_surface_proxy(struct wl_egl_window
*window
)
296 /* Version 3 of wl_egl_window introduced a version field at the same
297 * location where a pointer to wl_surface was stored. Thus, if
298 * window->version is dereferenceable, we've been given an older version of
299 * wl_egl_window, and window->version points to wl_surface */
300 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
301 return wl_proxy_create_wrapper((void *)(window
->version
));
303 return wl_proxy_create_wrapper(window
->surface
);
307 * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
310 dri2_wl_create_window_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
311 void *native_window
, const EGLint
*attrib_list
)
313 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
314 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
315 struct wl_egl_window
*window
= native_window
;
316 struct dri2_egl_surface
*dri2_surf
;
318 const __DRIconfig
*config
;
320 dri2_surf
= calloc(1, sizeof *dri2_surf
);
322 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
326 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
327 attrib_list
, false, native_window
))
330 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
331 dri2_surf
->base
.GLColorspace
);
334 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
338 dri2_surf
->base
.Width
= window
->width
;
339 dri2_surf
->base
.Height
= window
->height
;
341 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
342 assert(visual_idx
!= -1);
344 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
345 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
347 assert(dri2_dpy
->wl_shm
);
348 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
351 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
352 if (!dri2_surf
->wl_queue
) {
353 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
357 if (dri2_dpy
->wl_drm
) {
358 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
359 if (!dri2_surf
->wl_drm_wrapper
) {
360 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
363 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
364 dri2_surf
->wl_queue
);
367 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
368 if (!dri2_surf
->wl_dpy_wrapper
) {
369 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
372 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
373 dri2_surf
->wl_queue
);
375 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
376 if (!dri2_surf
->wl_surface_wrapper
) {
377 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
378 goto cleanup_dpy_wrapper
;
380 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
381 dri2_surf
->wl_queue
);
383 dri2_surf
->wl_win
= window
;
384 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
385 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
387 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
389 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
390 goto cleanup_surf_wrapper
;
392 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
394 return &dri2_surf
->base
;
396 cleanup_surf_wrapper
:
397 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
399 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
401 if (dri2_surf
->wl_drm_wrapper
)
402 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
404 wl_event_queue_destroy(dri2_surf
->wl_queue
);
412 dri2_wl_create_pixmap_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
413 void *native_window
, const EGLint
*attrib_list
)
415 /* From the EGL_EXT_platform_wayland spec, version 3:
417 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
418 * that belongs to Wayland. Any such call fails and generates
421 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
427 * Called via eglDestroySurface(), drv->DestroySurface().
430 dri2_wl_destroy_surface(_EGLDisplay
*disp
, _EGLSurface
*surf
)
432 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
433 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
435 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
437 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
438 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
439 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
440 if (dri2_surf
->color_buffers
[i
].dri_image
)
441 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
442 if (dri2_surf
->color_buffers
[i
].linear_copy
)
443 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
444 if (dri2_surf
->color_buffers
[i
].data
)
445 munmap(dri2_surf
->color_buffers
[i
].data
,
446 dri2_surf
->color_buffers
[i
].data_size
);
450 dri2_egl_surface_free_local_buffers(dri2_surf
);
452 if (dri2_surf
->throttle_callback
)
453 wl_callback_destroy(dri2_surf
->throttle_callback
);
455 if (dri2_surf
->wl_win
) {
456 dri2_surf
->wl_win
->driver_private
= NULL
;
457 dri2_surf
->wl_win
->resize_callback
= NULL
;
458 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
461 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
462 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
463 if (dri2_surf
->wl_drm_wrapper
)
464 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
465 wl_event_queue_destroy(dri2_surf
->wl_queue
);
467 dri2_fini_surface(surf
);
474 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
476 struct dri2_egl_display
*dri2_dpy
=
477 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
479 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
480 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
481 if (dri2_surf
->color_buffers
[i
].locked
) {
482 dri2_surf
->color_buffers
[i
].wl_release
= true;
484 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
485 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
488 if (dri2_surf
->color_buffers
[i
].dri_image
)
489 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
490 if (dri2_surf
->color_buffers
[i
].linear_copy
)
491 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
492 if (dri2_surf
->color_buffers
[i
].data
)
493 munmap(dri2_surf
->color_buffers
[i
].data
,
494 dri2_surf
->color_buffers
[i
].data_size
);
496 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
497 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
498 dri2_surf
->color_buffers
[i
].data
= NULL
;
502 dri2_egl_surface_free_local_buffers(dri2_surf
);
506 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
508 struct dri2_egl_display
*dri2_dpy
=
509 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
512 unsigned int dri_image_format
;
513 unsigned int linear_dri_image_format
;
517 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
518 assert(visual_idx
!= -1);
519 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
520 linear_dri_image_format
= dri_image_format
;
521 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
522 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
524 if (num_modifiers
== 1 && modifiers
[0] == DRM_FORMAT_MOD_INVALID
) {
525 /* For the purposes of this function, an INVALID modifier on its own
526 * means the modifiers aren't supported.
531 /* Substitute dri image format if server does not support original format */
532 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
533 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
535 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
536 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
539 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
540 assert(BITSET_TEST(dri2_dpy
->formats
,
541 dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
543 /* There might be a buffer release already queued that wasn't processed */
544 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
546 while (dri2_surf
->back
== NULL
) {
547 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
548 /* Get an unlocked buffer, preferably one with a dri_buffer
549 * already allocated. */
550 if (dri2_surf
->color_buffers
[i
].locked
)
552 if (dri2_surf
->back
== NULL
)
553 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
554 else if (dri2_surf
->back
->dri_image
== NULL
)
555 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
561 /* If we don't have a buffer, then block on the server to release one for
562 * us, and try again. wl_display_dispatch_queue will process any pending
563 * events, however not all servers flush on issuing a buffer release
564 * event. So, we spam the server with roundtrips as they always cause a
567 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
568 dri2_surf
->wl_queue
) < 0)
572 if (dri2_surf
->back
== NULL
)
575 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
577 if (dri2_dpy
->is_different_gpu
&&
578 dri2_surf
->back
->linear_copy
== NULL
) {
579 /* The LINEAR modifier should be a perfect alias of the LINEAR use
580 * flag; try the new interface first before the old, then fall back. */
581 if (dri2_dpy
->image
->base
.version
>= 15 &&
582 dri2_dpy
->image
->createImageWithModifiers
) {
583 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
585 dri2_surf
->back
->linear_copy
=
586 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
587 dri2_surf
->base
.Width
,
588 dri2_surf
->base
.Height
,
589 linear_dri_image_format
,
594 dri2_surf
->back
->linear_copy
=
595 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
596 dri2_surf
->base
.Width
,
597 dri2_surf
->base
.Height
,
598 linear_dri_image_format
,
600 __DRI_IMAGE_USE_LINEAR
,
603 if (dri2_surf
->back
->linear_copy
== NULL
)
607 if (dri2_surf
->back
->dri_image
== NULL
) {
608 /* If our DRIImage implementation does not support
609 * createImageWithModifiers, then fall back to the old createImage,
610 * and hope it allocates an image which is acceptable to the winsys.
612 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
613 dri2_dpy
->image
->createImageWithModifiers
) {
614 dri2_surf
->back
->dri_image
=
615 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
616 dri2_surf
->base
.Width
,
617 dri2_surf
->base
.Height
,
623 dri2_surf
->back
->dri_image
=
624 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
625 dri2_surf
->base
.Width
,
626 dri2_surf
->base
.Height
,
628 dri2_dpy
->is_different_gpu
?
633 dri2_surf
->back
->age
= 0;
635 if (dri2_surf
->back
->dri_image
== NULL
)
638 dri2_surf
->back
->locked
= true;
645 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
647 struct dri2_egl_display
*dri2_dpy
=
648 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
652 image
= dri2_surf
->back
->dri_image
;
654 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
655 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
657 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
659 buffer
->pitch
= pitch
;
665 update_buffers(struct dri2_egl_surface
*dri2_surf
)
667 struct dri2_egl_display
*dri2_dpy
=
668 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
670 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
671 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
673 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
674 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
675 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
676 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
679 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
680 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
681 dri2_wl_release_buffers(dri2_surf
);
684 if (get_back_bo(dri2_surf
) < 0) {
685 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
689 /* If we have an extra unlocked buffer at this point, we had to do triple
690 * buffering for a while, but now can go back to just double buffering.
691 * That means we can free any unlocked buffer now. */
692 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
693 if (!dri2_surf
->color_buffers
[i
].locked
&&
694 dri2_surf
->color_buffers
[i
].wl_buffer
) {
695 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
696 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
697 if (dri2_dpy
->is_different_gpu
)
698 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
699 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
700 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
701 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
709 update_buffers_if_needed(struct dri2_egl_surface
*dri2_surf
)
711 if (dri2_surf
->back
!= NULL
)
714 return update_buffers(dri2_surf
);
718 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
719 int *width
, int *height
,
720 unsigned int *attachments
, int count
,
721 int *out_count
, void *loaderPrivate
)
723 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
726 if (update_buffers(dri2_surf
) < 0)
729 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
732 switch (attachments
[i
]) {
733 case __DRI_BUFFER_BACK_LEFT
:
734 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
737 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
741 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
744 dri2_surf
->buffers
[j
] = *local
;
753 *width
= dri2_surf
->base
.Width
;
754 *height
= dri2_surf
->base
.Height
;
756 return dri2_surf
->buffers
;
760 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
761 int *width
, int *height
,
762 unsigned int *attachments
, int count
,
763 int *out_count
, void *loaderPrivate
)
765 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
766 unsigned int *attachments_with_format
;
768 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
770 if (visual_idx
== -1)
773 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
774 if (!attachments_with_format
) {
779 for (int i
= 0; i
< count
; ++i
) {
780 attachments_with_format
[2*i
] = attachments
[i
];
781 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
785 dri2_wl_get_buffers_with_format(driDrawable
,
787 attachments_with_format
, count
,
788 out_count
, loaderPrivate
);
790 free(attachments_with_format
);
796 image_get_buffers(__DRIdrawable
*driDrawable
,
800 uint32_t buffer_mask
,
801 struct __DRIimageList
*buffers
)
803 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
805 if (update_buffers(dri2_surf
) < 0)
808 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
809 buffers
->back
= dri2_surf
->back
->dri_image
;
815 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
818 (void) loaderPrivate
;
822 dri2_wl_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
825 case DRI_LOADER_CAP_FP16
:
832 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
833 .base
= { __DRI_DRI2_LOADER
, 4 },
835 .getBuffers
= dri2_wl_get_buffers
,
836 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
837 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
838 .getCapability
= dri2_wl_get_capability
,
841 static const __DRIimageLoaderExtension image_loader_extension
= {
842 .base
= { __DRI_IMAGE_LOADER
, 2 },
844 .getBuffers
= image_get_buffers
,
845 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
846 .getCapability
= dri2_wl_get_capability
,
850 wayland_throttle_callback(void *data
,
851 struct wl_callback
*callback
,
854 struct dri2_egl_surface
*dri2_surf
= data
;
856 dri2_surf
->throttle_callback
= NULL
;
857 wl_callback_destroy(callback
);
860 static const struct wl_callback_listener throttle_listener
= {
861 .done
= wayland_throttle_callback
865 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
866 __DRIimage
*image
, int *fourcc
)
872 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
877 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
882 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
883 if (visual_idx
== -1)
886 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
890 static struct wl_buffer
*
891 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
892 struct dri2_egl_surface
*dri2_surf
,
895 struct wl_buffer
*ret
;
897 int width
, height
, fourcc
, num_planes
;
898 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
900 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
901 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
903 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
907 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
912 if (dri2_dpy
->image
->base
.version
>= 15) {
915 query
= dri2_dpy
->image
->queryImage(image
,
916 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
918 query
&= dri2_dpy
->image
->queryImage(image
,
919 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
922 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
926 bool supported_modifier
= false;
927 bool mod_invalid_supported
= false;
928 int visual_idx
= dri2_wl_visual_idx_from_fourcc(fourcc
);
929 assert(visual_idx
!= -1);
932 u_vector_foreach(mod
, &dri2_dpy
->wl_modifiers
[visual_idx
]) {
933 if (*mod
== DRM_FORMAT_MOD_INVALID
) {
934 mod_invalid_supported
= true;
936 if (*mod
== modifier
) {
937 supported_modifier
= true;
941 if (!supported_modifier
&& mod_invalid_supported
) {
942 /* If the server has advertised DRM_FORMAT_MOD_INVALID then we trust
943 * that the client has allocated the buffer with the right implicit
944 * modifier for the format, even though it's allocated a buffer the
945 * server hasn't explicitly claimed to support. */
946 modifier
= DRM_FORMAT_MOD_INVALID
;
947 supported_modifier
= true;
950 if (dri2_dpy
->wl_dmabuf
&& supported_modifier
) {
951 struct zwp_linux_buffer_params_v1
*params
;
954 /* We don't need a wrapper for wl_dmabuf objects, because we have to
955 * create the intermediate params object; we can set the queue on this,
956 * and the wl_buffer inherits it race-free. */
957 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
959 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
961 for (i
= 0; i
< num_planes
; i
++) {
966 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
972 query
= dri2_dpy
->image
->queryImage(p_image
,
973 __DRI_IMAGE_ATTRIB_FD
,
975 query
&= dri2_dpy
->image
->queryImage(p_image
,
976 __DRI_IMAGE_ATTRIB_STRIDE
,
978 query
&= dri2_dpy
->image
->queryImage(p_image
,
979 __DRI_IMAGE_ATTRIB_OFFSET
,
981 if (image
!= p_image
)
982 dri2_dpy
->image
->destroyImage(p_image
);
987 zwp_linux_buffer_params_v1_destroy(params
);
991 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
992 modifier
>> 32, modifier
& 0xffffffff);
996 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
998 zwp_linux_buffer_params_v1_destroy(params
);
999 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
1000 struct wl_drm
*wl_drm
=
1001 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
1007 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
1008 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
1009 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
1010 stride
, 0, 0, 0, 0);
1013 struct wl_drm
*wl_drm
=
1014 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
1020 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
1021 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
1022 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
1029 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
1030 const EGLint
*rects
,
1033 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
1034 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
1037 for (int i
= 0; i
< n_rects
; i
++) {
1038 const int *rect
= &rects
[i
* 4];
1040 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
1042 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
1049 * Called via eglSwapBuffers(), drv->SwapBuffers().
1052 dri2_wl_swap_buffers_with_damage(_EGLDisplay
*disp
,
1054 const EGLint
*rects
,
1057 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1058 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1060 while (dri2_surf
->throttle_callback
!= NULL
)
1061 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1062 dri2_surf
->wl_queue
) == -1)
1065 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
1066 if (dri2_surf
->color_buffers
[i
].age
> 0)
1067 dri2_surf
->color_buffers
[i
].age
++;
1069 /* Make sure we have a back buffer in case we're swapping without ever
1071 if (update_buffers_if_needed(dri2_surf
) < 0)
1072 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
1074 if (draw
->SwapInterval
> 0) {
1075 dri2_surf
->throttle_callback
=
1076 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1077 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1078 &throttle_listener
, dri2_surf
);
1081 dri2_surf
->back
->age
= 1;
1082 dri2_surf
->current
= dri2_surf
->back
;
1083 dri2_surf
->back
= NULL
;
1085 if (!dri2_surf
->current
->wl_buffer
) {
1088 if (dri2_dpy
->is_different_gpu
)
1089 image
= dri2_surf
->current
->linear_copy
;
1091 image
= dri2_surf
->current
->dri_image
;
1093 dri2_surf
->current
->wl_buffer
=
1094 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1096 dri2_surf
->current
->wl_release
= false;
1098 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1099 &wl_buffer_listener
, dri2_surf
);
1102 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1103 dri2_surf
->current
->wl_buffer
,
1104 dri2_surf
->dx
, dri2_surf
->dy
);
1106 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1107 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1108 /* reset resize growing parameters */
1112 /* If the compositor doesn't support damage_buffer, we deliberately
1113 * ignore the damage region and post maximum damage, due to
1114 * https://bugs.freedesktop.org/78190 */
1115 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1116 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1117 0, 0, INT32_MAX
, INT32_MAX
);
1119 if (dri2_dpy
->is_different_gpu
) {
1120 _EGLContext
*ctx
= _eglGetCurrentContext();
1121 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1122 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1123 dri2_surf
->current
->linear_copy
,
1124 dri2_surf
->current
->dri_image
,
1125 0, 0, dri2_surf
->base
.Width
,
1126 dri2_surf
->base
.Height
,
1127 0, 0, dri2_surf
->base
.Width
,
1128 dri2_surf
->base
.Height
, 0);
1131 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1132 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1134 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1136 /* If we're not waiting for a frame callback then we'll at least throttle
1137 * to a sync callback so that we always give a chance for the compositor to
1138 * handle the commit and send a release event before checking for a free
1140 if (dri2_surf
->throttle_callback
== NULL
) {
1141 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1142 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1143 &throttle_listener
, dri2_surf
);
1146 wl_display_flush(dri2_dpy
->wl_dpy
);
1152 dri2_wl_query_buffer_age(const _EGLDriver
*drv
,
1153 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1155 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1157 if (update_buffers_if_needed(dri2_surf
) < 0) {
1158 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1162 return dri2_surf
->back
->age
;
1166 dri2_wl_swap_buffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1168 return dri2_wl_swap_buffers_with_damage(disp
, draw
, NULL
, 0);
1171 static struct wl_buffer
*
1172 dri2_wl_create_wayland_buffer_from_image(const _EGLDriver
*drv
,
1176 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1177 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1178 __DRIimage
*image
= dri2_img
->dri_image
;
1179 struct wl_buffer
*buffer
;
1180 int format
, visual_idx
;
1182 /* Check the upstream display supports this buffer's format. */
1183 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1184 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1185 if (visual_idx
== -1)
1188 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
1191 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1193 /* The buffer object will have been created with our internal event queue
1194 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1195 * buffer to be used by the application so we'll reset it to the display's
1196 * default event queue. This isn't actually racy, as the only event the
1197 * buffer can get is a buffer release, which doesn't happen with an explicit
1200 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1205 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1210 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1212 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1215 if (dri2_dpy
->is_render_node
) {
1216 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1217 "authenticate for render-nodes");
1220 dri2_dpy
->authenticated
= false;
1222 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1223 if (roundtrip(dri2_dpy
) < 0)
1226 if (!dri2_dpy
->authenticated
)
1229 /* reset authenticated */
1230 dri2_dpy
->authenticated
= true;
1236 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1238 struct dri2_egl_display
*dri2_dpy
= data
;
1241 dri2_dpy
->device_name
= strdup(device
);
1242 if (!dri2_dpy
->device_name
)
1245 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1246 if (dri2_dpy
->fd
== -1) {
1247 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1248 dri2_dpy
->device_name
, strerror(errno
));
1249 free(dri2_dpy
->device_name
);
1250 dri2_dpy
->device_name
= NULL
;
1254 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1255 dri2_dpy
->authenticated
= true;
1257 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1258 close(dri2_dpy
->fd
);
1260 free(dri2_dpy
->device_name
);
1261 dri2_dpy
->device_name
= NULL
;
1262 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1265 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1270 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1272 struct dri2_egl_display
*dri2_dpy
= data
;
1273 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1275 if (visual_idx
== -1)
1278 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1282 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1284 struct dri2_egl_display
*dri2_dpy
= data
;
1286 dri2_dpy
->capabilities
= value
;
1290 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1292 struct dri2_egl_display
*dri2_dpy
= data
;
1294 dri2_dpy
->authenticated
= true;
1297 static const struct wl_drm_listener drm_listener
= {
1298 .device
= drm_handle_device
,
1299 .format
= drm_handle_format
,
1300 .authenticated
= drm_handle_authenticated
,
1301 .capabilities
= drm_handle_capabilities
1305 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1308 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1312 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1313 uint32_t format
, uint32_t modifier_hi
,
1314 uint32_t modifier_lo
)
1316 struct dri2_egl_display
*dri2_dpy
= data
;
1317 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1320 if (visual_idx
== -1)
1323 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1325 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1326 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1329 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1330 .format
= dmabuf_ignore_format
,
1331 .modifier
= dmabuf_handle_modifier
,
1335 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1336 uint32_t name
, const char *interface
,
1339 struct dri2_egl_display
*dri2_dpy
= data
;
1341 if (strcmp(interface
, "wl_drm") == 0) {
1343 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1344 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1345 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1346 dri2_dpy
->wl_dmabuf
=
1347 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1349 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1355 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1360 static const struct wl_registry_listener registry_listener_drm
= {
1361 .global
= registry_handle_global_drm
,
1362 .global_remove
= registry_handle_global_remove
1366 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1368 /* We can't use values greater than 1 on Wayland because we are using the
1369 * frame callback to synchronise the frame and the only way we be sure to
1370 * get a frame callback is to attach a new buffer. Therefore we can't just
1371 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1373 dri2_setup_swap_interval(disp
, 1);
1376 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1377 .authenticate
= dri2_wl_authenticate
,
1378 .create_window_surface
= dri2_wl_create_window_surface
,
1379 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1380 .destroy_surface
= dri2_wl_destroy_surface
,
1381 .create_image
= dri2_create_image_khr
,
1382 .swap_buffers
= dri2_wl_swap_buffers
,
1383 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1384 .query_buffer_age
= dri2_wl_query_buffer_age
,
1385 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1386 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1389 static const __DRIextension
*dri2_loader_extensions
[] = {
1390 &dri2_loader_extension
.base
,
1391 &image_loader_extension
.base
,
1392 &image_lookup_extension
.base
,
1393 &use_invalidate
.base
,
1397 static const __DRIextension
*image_loader_extensions
[] = {
1398 &image_loader_extension
.base
,
1399 &image_lookup_extension
.base
,
1400 &use_invalidate
.base
,
1405 dri2_wl_add_configs_for_visuals(_EGLDisplay
*disp
)
1407 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1408 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1409 unsigned int count
= 0;
1412 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1415 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1416 struct dri2_egl_config
*dri2_conf
;
1418 if (!BITSET_TEST(dri2_dpy
->formats
, j
))
1421 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1422 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_shifts
, dri2_wl_visuals
[j
].rgba_sizes
);
1424 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1431 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1432 struct dri2_egl_config
*dri2_conf
;
1433 int alt_dri_image_format
, c
, s
;
1435 /* No match for config. Try if we can blitImage convert to a visual */
1436 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1437 dri2_dpy
->driver_configs
[i
]);
1442 /* Find optimal target visual for blitImage conversion, if any. */
1443 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1444 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1446 if (s
== -1 || !BITSET_TEST(dri2_dpy
->formats
, s
))
1449 /* Visual s works for the Wayland server, and c can be converted into s
1450 * by our client gpu during PRIME blitImage conversion to a linear
1451 * wl_buffer, so add visual c as supported by the client renderer.
1453 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1454 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1455 dri2_wl_visuals
[c
].rgba_shifts
,
1456 dri2_wl_visuals
[c
].rgba_sizes
);
1458 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1461 if (format_count
[c
] == 1)
1462 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1463 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1464 dri2_wl_visuals
[s
].format_name
);
1469 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1470 if (!format_count
[i
]) {
1471 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1472 dri2_wl_visuals
[i
].format_name
);
1476 return (count
!= 0);
1480 dri2_initialize_wayland_drm(_EGLDisplay
*disp
)
1483 struct dri2_egl_display
*dri2_dpy
;
1485 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1487 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1490 disp
->DriverData
= (void *) dri2_dpy
;
1491 if (disp
->PlatformDisplay
== NULL
) {
1492 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1493 if (dri2_dpy
->wl_dpy
== NULL
)
1495 dri2_dpy
->own_device
= true;
1497 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1500 dri2_dpy
->wl_modifiers
=
1501 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1502 if (!dri2_dpy
->wl_modifiers
)
1504 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1505 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1509 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1511 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1512 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1515 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1516 dri2_dpy
->wl_queue
);
1518 if (dri2_dpy
->own_device
)
1519 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1521 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1522 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1523 ®istry_listener_drm
, dri2_dpy
);
1524 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1527 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1530 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1533 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1534 &dri2_dpy
->is_different_gpu
);
1535 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1537 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1543 if (dri2_dpy
->is_different_gpu
) {
1544 free(dri2_dpy
->device_name
);
1545 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1546 if (!dri2_dpy
->device_name
) {
1547 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1548 "for requested GPU");
1553 /* we have to do the check now, because loader_get_user_preferred_fd
1554 * will return a render-node when the requested gpu is different
1555 * to the server, but also if the client asks for the same gpu than
1556 * the server by requesting its pci-id */
1557 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1559 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1560 if (dri2_dpy
->driver_name
== NULL
) {
1561 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1565 /* render nodes cannot use Gem names, and thus do not support
1566 * the __DRI_DRI2_LOADER extension */
1567 if (!dri2_dpy
->is_render_node
) {
1568 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1569 if (!dri2_load_driver(disp
)) {
1570 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1574 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1575 if (!dri2_load_driver_dri3(disp
)) {
1576 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1581 if (!dri2_create_screen(disp
))
1584 if (!dri2_setup_extensions(disp
))
1587 dri2_setup_screen(disp
);
1589 dri2_wl_setup_swap_interval(disp
);
1591 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1592 * createImageFromFds support indicates that Prime export/import
1593 * is supported by the driver. Fall back to
1594 * gem names if we don't have Prime support. */
1596 if (dri2_dpy
->image
->base
.version
< 7 ||
1597 dri2_dpy
->image
->createImageFromFds
== NULL
)
1598 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1600 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1601 * The server needs to accept them */
1602 if (dri2_dpy
->is_render_node
&&
1603 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1604 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1608 if (dri2_dpy
->is_different_gpu
&&
1609 (dri2_dpy
->image
->base
.version
< 9 ||
1610 dri2_dpy
->image
->blitImage
== NULL
)) {
1611 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1612 "Image extension in the driver is not "
1613 "compatible. Version 9 or later and blitImage() "
1618 if (!dri2_wl_add_configs_for_visuals(disp
)) {
1619 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1623 dri2_set_WL_bind_wayland_display(disp
);
1624 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1625 * because the buffer of the EGLImage has likely a tiling mode the server
1626 * gpu won't support. These is no way to check for now. Thus do not support the
1628 if (!dri2_dpy
->is_different_gpu
)
1629 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1631 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1633 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1635 /* Fill vtbl last to prevent accidentally calling virtual function during
1638 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1643 dri2_display_destroy(disp
);
1648 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1650 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1652 assume(visual_idx
!= -1);
1654 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1658 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1659 int format
, int w
, int h
,
1660 void **data
, int *size
,
1661 struct wl_buffer
**buffer
)
1663 struct dri2_egl_display
*dri2_dpy
=
1664 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1665 struct wl_shm_pool
*pool
;
1666 int fd
, stride
, size_map
;
1669 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1670 size_map
= h
* stride
;
1672 /* Create a shareable buffer */
1673 fd
= os_create_anonymous_file(size_map
, NULL
);
1677 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1678 if (data_map
== MAP_FAILED
) {
1683 /* Share it in a wl_buffer */
1684 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1685 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1686 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1687 wl_shm_pool_destroy(pool
);
1696 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1698 struct dri2_egl_display
*dri2_dpy
=
1699 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1701 /* we need to do the following operations only once per frame */
1702 if (dri2_surf
->back
)
1705 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1706 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1708 dri2_wl_release_buffers(dri2_surf
);
1710 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1711 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1712 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1713 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1714 dri2_surf
->current
= NULL
;
1717 /* find back buffer */
1719 /* There might be a buffer release already queued that wasn't processed */
1720 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1722 /* try get free buffer already created */
1723 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1724 if (!dri2_surf
->color_buffers
[i
].locked
&&
1725 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1726 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1731 /* else choose any another free location */
1732 if (!dri2_surf
->back
) {
1733 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1734 if (!dri2_surf
->color_buffers
[i
].locked
) {
1735 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1736 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1738 dri2_surf
->base
.Width
,
1739 dri2_surf
->base
.Height
,
1740 &dri2_surf
->back
->data
,
1741 &dri2_surf
->back
->data_size
,
1742 &dri2_surf
->back
->wl_buffer
)) {
1743 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1746 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1747 &wl_buffer_listener
, dri2_surf
);
1753 if (!dri2_surf
->back
) {
1754 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1758 dri2_surf
->back
->locked
= true;
1760 /* If we have an extra unlocked buffer at this point, we had to do triple
1761 * buffering for a while, but now can go back to just double buffering.
1762 * That means we can free any unlocked buffer now. */
1763 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1764 if (!dri2_surf
->color_buffers
[i
].locked
&&
1765 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1766 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1767 munmap(dri2_surf
->color_buffers
[i
].data
,
1768 dri2_surf
->color_buffers
[i
].data_size
);
1769 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1770 dri2_surf
->color_buffers
[i
].data
= NULL
;
1778 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1780 /* if there has been a resize: */
1781 if (!dri2_surf
->current
)
1784 return dri2_surf
->current
->data
;
1788 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1790 assert(dri2_surf
->back
);
1791 return dri2_surf
->back
->data
;
1795 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1797 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1799 while (dri2_surf
->throttle_callback
!= NULL
)
1800 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1801 dri2_surf
->wl_queue
) == -1)
1804 if (dri2_surf
->base
.SwapInterval
> 0) {
1805 dri2_surf
->throttle_callback
=
1806 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1807 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1808 &throttle_listener
, dri2_surf
);
1811 dri2_surf
->current
= dri2_surf
->back
;
1812 dri2_surf
->back
= NULL
;
1814 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1815 dri2_surf
->current
->wl_buffer
,
1816 dri2_surf
->dx
, dri2_surf
->dy
);
1818 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1819 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1820 /* reset resize growing parameters */
1824 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1825 0, 0, INT32_MAX
, INT32_MAX
);
1826 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1828 /* If we're not waiting for a frame callback then we'll at least throttle
1829 * to a sync callback so that we always give a chance for the compositor to
1830 * handle the commit and send a release event before checking for a free
1832 if (dri2_surf
->throttle_callback
== NULL
) {
1833 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1834 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1835 &throttle_listener
, dri2_surf
);
1838 wl_display_flush(dri2_dpy
->wl_dpy
);
1842 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1843 int *x
, int *y
, int *w
, int *h
,
1844 void *loaderPrivate
)
1846 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1848 (void) swrast_update_buffers(dri2_surf
);
1851 *w
= dri2_surf
->base
.Width
;
1852 *h
= dri2_surf
->base
.Height
;
1856 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1857 int x
, int y
, int w
, int h
,
1858 char *data
, void *loaderPrivate
)
1860 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1861 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1862 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1863 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1864 int dst_stride
= copy_width
;
1867 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1869 memset(data
, 0, copy_width
* h
);
1873 assert(data
!= src
);
1874 assert(copy_width
<= src_stride
);
1877 src
+= y
* src_stride
;
1880 if (copy_width
> src_stride
-x_offset
)
1881 copy_width
= src_stride
-x_offset
;
1882 if (h
> dri2_surf
->base
.Height
-y
)
1883 h
= dri2_surf
->base
.Height
-y
;
1886 memcpy(dst
, src
, copy_width
);
1893 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1894 int x
, int y
, int w
, int h
, int stride
,
1895 char *data
, void *loaderPrivate
)
1897 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1898 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1899 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1900 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1903 assert(copy_width
<= stride
);
1905 (void) swrast_update_buffers(dri2_surf
);
1906 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1908 /* partial copy, copy old content */
1909 if (copy_width
< dst_stride
)
1910 dri2_wl_swrast_get_image(draw
, 0, 0,
1911 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1912 dst
, loaderPrivate
);
1915 dst
+= y
* dst_stride
;
1919 /* drivers expect we do these checks (and some rely on it) */
1920 if (copy_width
> dst_stride
-x_offset
)
1921 copy_width
= dst_stride
-x_offset
;
1922 if (h
> dri2_surf
->base
.Height
-y
)
1923 h
= dri2_surf
->base
.Height
-y
;
1926 memcpy(dst
, src
, copy_width
);
1930 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1934 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1935 int x
, int y
, int w
, int h
,
1936 char *data
, void *loaderPrivate
)
1938 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1941 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1942 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1943 stride
, data
, loaderPrivate
);
1947 dri2_wl_swrast_swap_buffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1949 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1950 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1952 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1957 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1959 struct dri2_egl_display
*dri2_dpy
= data
;
1960 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1962 if (visual_idx
== -1)
1965 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1968 static const struct wl_shm_listener shm_listener
= {
1969 .format
= shm_handle_format
1973 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1974 uint32_t name
, const char *interface
,
1977 struct dri2_egl_display
*dri2_dpy
= data
;
1979 if (strcmp(interface
, "wl_shm") == 0) {
1981 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1982 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1986 static const struct wl_registry_listener registry_listener_swrast
= {
1987 .global
= registry_handle_global_swrast
,
1988 .global_remove
= registry_handle_global_remove
1991 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1992 .authenticate
= NULL
,
1993 .create_window_surface
= dri2_wl_create_window_surface
,
1994 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1995 .destroy_surface
= dri2_wl_destroy_surface
,
1996 .create_image
= dri2_create_image_khr
,
1997 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1998 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
2001 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
2002 .base
= { __DRI_SWRAST_LOADER
, 2 },
2004 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
2005 .putImage
= dri2_wl_swrast_put_image
,
2006 .getImage
= dri2_wl_swrast_get_image
,
2007 .putImage2
= dri2_wl_swrast_put_image2
,
2010 static const __DRIextension
*swrast_loader_extensions
[] = {
2011 &swrast_loader_extension
.base
,
2012 &image_lookup_extension
.base
,
2017 dri2_initialize_wayland_swrast(_EGLDisplay
*disp
)
2020 struct dri2_egl_display
*dri2_dpy
;
2022 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2024 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2027 disp
->DriverData
= (void *) dri2_dpy
;
2028 if (disp
->PlatformDisplay
== NULL
) {
2029 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2030 if (dri2_dpy
->wl_dpy
== NULL
)
2032 dri2_dpy
->own_device
= true;
2034 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2037 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
2039 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
2045 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2047 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2048 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2051 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2052 dri2_dpy
->wl_queue
);
2054 if (dri2_dpy
->own_device
)
2055 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2057 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2058 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2059 ®istry_listener_swrast
, dri2_dpy
);
2061 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2064 if (roundtrip(dri2_dpy
) < 0 || !BITSET_TEST_RANGE(dri2_dpy
->formats
,
2065 0, EGL_DRI2_MAX_FORMATS
))
2068 dri2_dpy
->driver_name
= strdup("swrast");
2069 if (!dri2_load_driver_swrast(disp
))
2072 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2074 if (!dri2_create_screen(disp
))
2077 if (!dri2_setup_extensions(disp
))
2080 dri2_setup_screen(disp
);
2082 dri2_wl_setup_swap_interval(disp
);
2084 if (!dri2_wl_add_configs_for_visuals(disp
)) {
2085 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2089 /* Fill vtbl last to prevent accidentally calling virtual function during
2092 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2097 dri2_display_destroy(disp
);
2102 dri2_initialize_wayland(_EGLDisplay
*disp
)
2104 EGLBoolean initialized
= EGL_FALSE
;
2106 if (!disp
->Options
.ForceSoftware
)
2107 initialized
= dri2_initialize_wayland_drm(disp
);
2110 initialized
= dri2_initialize_wayland_swrast(disp
);
2117 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2119 if (dri2_dpy
->wl_drm
)
2120 wl_drm_destroy(dri2_dpy
->wl_drm
);
2121 if (dri2_dpy
->wl_dmabuf
)
2122 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2123 if (dri2_dpy
->wl_shm
)
2124 wl_shm_destroy(dri2_dpy
->wl_shm
);
2125 if (dri2_dpy
->wl_registry
)
2126 wl_registry_destroy(dri2_dpy
->wl_registry
);
2127 if (dri2_dpy
->wl_queue
)
2128 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2129 if (dri2_dpy
->wl_dpy_wrapper
)
2130 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2132 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2133 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2134 free(dri2_dpy
->wl_modifiers
);
2136 if (dri2_dpy
->own_device
)
2137 wl_display_disconnect(dri2_dpy
->wl_dpy
);