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"
55 * The index of entries in this table is used as a bitmask in
56 * dri2_dpy->formats, which tracks the formats supported by our server.
58 static const struct dri2_wl_visual
{
59 const char *format_name
;
60 uint32_t wl_drm_format
;
61 uint32_t wl_shm_format
;
63 /* alt_dri_image_format is a substitute wl_buffer format to use for a
64 * wl-server unsupported dri_image_format, ie. some other dri_image_format in
65 * the table, of the same precision but with different channel ordering, or
66 * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported.
67 * The code checks if alt_dri_image_format can be used as a fallback for a
68 * dri_image_format for a given wl-server implementation.
70 int alt_dri_image_format
;
73 unsigned int rgba_sizes
[4];
74 } dri2_wl_visuals
[] = {
77 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
78 __DRI_IMAGE_FORMAT_XRGB2101010
, __DRI_IMAGE_FORMAT_XBGR2101010
, 32,
84 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
85 __DRI_IMAGE_FORMAT_ARGB2101010
, __DRI_IMAGE_FORMAT_ABGR2101010
, 32,
91 WL_DRM_FORMAT_XBGR2101010
, WL_SHM_FORMAT_XBGR2101010
,
92 __DRI_IMAGE_FORMAT_XBGR2101010
, __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
98 WL_DRM_FORMAT_ABGR2101010
, WL_SHM_FORMAT_ABGR2101010
,
99 __DRI_IMAGE_FORMAT_ABGR2101010
, __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
105 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
106 __DRI_IMAGE_FORMAT_XRGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
112 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
113 __DRI_IMAGE_FORMAT_ARGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
119 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
120 __DRI_IMAGE_FORMAT_RGB565
, __DRI_IMAGE_FORMAT_NONE
, 16,
126 static_assert(ARRAY_SIZE(dri2_wl_visuals
) <= EGL_DRI2_MAX_FORMATS
,
127 "dri2_egl_display::formats is not large enough for "
128 "the formats in dri2_wl_visuals");
131 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
132 const __DRIconfig
*config
)
135 unsigned int sizes
[4];
137 dri2_get_shifts_and_sizes(dri2_dpy
->core
, config
, shifts
, sizes
);
139 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
140 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
142 if (shifts
[0] == wl_visual
->rgba_shifts
[0] &&
143 shifts
[1] == wl_visual
->rgba_shifts
[1] &&
144 shifts
[2] == wl_visual
->rgba_shifts
[2] &&
145 shifts
[3] == wl_visual
->rgba_shifts
[3] &&
146 sizes
[0] == wl_visual
->rgba_sizes
[0] &&
147 sizes
[1] == wl_visual
->rgba_sizes
[1] &&
148 sizes
[2] == wl_visual
->rgba_sizes
[2] &&
149 sizes
[3] == wl_visual
->rgba_sizes
[3]) {
158 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
160 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
161 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
163 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
171 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
173 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
174 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
182 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
184 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
185 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
193 dri2_wl_is_format_supported(void* user_data
, uint32_t format
)
195 _EGLDisplay
*disp
= (_EGLDisplay
*) user_data
;
196 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
197 int j
= dri2_wl_visual_idx_from_fourcc(format
);
202 for (int i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
203 if (j
== dri2_wl_visual_idx_from_config(dri2_dpy
,
204 dri2_dpy
->driver_configs
[i
]))
211 roundtrip(struct dri2_egl_display
*dri2_dpy
)
213 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
217 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
219 struct dri2_egl_surface
*dri2_surf
= data
;
222 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
223 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
226 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
228 if (dri2_surf
->color_buffers
[i
].wl_release
) {
229 wl_buffer_destroy(buffer
);
230 dri2_surf
->color_buffers
[i
].wl_release
= false;
231 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
234 dri2_surf
->color_buffers
[i
].locked
= false;
237 static const struct wl_buffer_listener wl_buffer_listener
= {
238 .release
= wl_buffer_release
242 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
244 struct dri2_egl_surface
*dri2_surf
= data
;
245 struct dri2_egl_display
*dri2_dpy
=
246 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
248 /* Update the surface size as soon as native window is resized; from user
249 * pov, this makes the effect that resize is done immediately after native
250 * window resize, without requiring to wait until the first draw.
252 * A more detailed and lengthy explanation can be found at
253 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
255 if (!dri2_surf
->back
) {
256 dri2_surf
->base
.Width
= wl_win
->width
;
257 dri2_surf
->base
.Height
= wl_win
->height
;
259 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
263 destroy_window_callback(void *data
)
265 struct dri2_egl_surface
*dri2_surf
= data
;
266 dri2_surf
->wl_win
= NULL
;
269 static struct wl_surface
*
270 get_wl_surface_proxy(struct wl_egl_window
*window
)
272 /* Version 3 of wl_egl_window introduced a version field at the same
273 * location where a pointer to wl_surface was stored. Thus, if
274 * window->version is dereferenceable, we've been given an older version of
275 * wl_egl_window, and window->version points to wl_surface */
276 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
277 return wl_proxy_create_wrapper((void *)(window
->version
));
279 return wl_proxy_create_wrapper(window
->surface
);
283 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
286 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
287 _EGLConfig
*conf
, void *native_window
,
288 const EGLint
*attrib_list
)
290 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
291 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
292 struct wl_egl_window
*window
= native_window
;
293 struct dri2_egl_surface
*dri2_surf
;
295 const __DRIconfig
*config
;
297 dri2_surf
= calloc(1, sizeof *dri2_surf
);
299 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
303 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
304 attrib_list
, false, native_window
))
307 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
308 dri2_surf
->base
.GLColorspace
);
311 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
315 dri2_surf
->base
.Width
= window
->width
;
316 dri2_surf
->base
.Height
= window
->height
;
318 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
319 assert(visual_idx
!= -1);
321 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
322 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
324 assert(dri2_dpy
->wl_shm
);
325 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
328 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
329 if (!dri2_surf
->wl_queue
) {
330 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
334 if (dri2_dpy
->wl_drm
) {
335 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
336 if (!dri2_surf
->wl_drm_wrapper
) {
337 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
340 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
341 dri2_surf
->wl_queue
);
344 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
345 if (!dri2_surf
->wl_dpy_wrapper
) {
346 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
349 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
350 dri2_surf
->wl_queue
);
352 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
353 if (!dri2_surf
->wl_surface_wrapper
) {
354 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
355 goto cleanup_dpy_wrapper
;
357 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
358 dri2_surf
->wl_queue
);
360 dri2_surf
->wl_win
= window
;
361 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
362 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
364 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
366 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
367 goto cleanup_surf_wrapper
;
369 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
371 return &dri2_surf
->base
;
373 cleanup_surf_wrapper
:
374 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
376 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
378 if (dri2_surf
->wl_drm_wrapper
)
379 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
381 wl_event_queue_destroy(dri2_surf
->wl_queue
);
389 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
390 _EGLConfig
*conf
, void *native_window
,
391 const EGLint
*attrib_list
)
393 /* From the EGL_EXT_platform_wayland spec, version 3:
395 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
396 * that belongs to Wayland. Any such call fails and generates
399 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
405 * Called via eglDestroySurface(), drv->API.DestroySurface().
408 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
410 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
411 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
415 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
417 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
418 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
419 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
420 if (dri2_surf
->color_buffers
[i
].dri_image
)
421 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
422 if (dri2_surf
->color_buffers
[i
].linear_copy
)
423 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
424 if (dri2_surf
->color_buffers
[i
].data
)
425 munmap(dri2_surf
->color_buffers
[i
].data
,
426 dri2_surf
->color_buffers
[i
].data_size
);
430 dri2_egl_surface_free_local_buffers(dri2_surf
);
432 if (dri2_surf
->throttle_callback
)
433 wl_callback_destroy(dri2_surf
->throttle_callback
);
435 if (dri2_surf
->wl_win
) {
436 dri2_surf
->wl_win
->driver_private
= NULL
;
437 dri2_surf
->wl_win
->resize_callback
= NULL
;
438 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
441 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
442 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
443 if (dri2_surf
->wl_drm_wrapper
)
444 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
445 wl_event_queue_destroy(dri2_surf
->wl_queue
);
447 dri2_fini_surface(surf
);
454 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
456 struct dri2_egl_display
*dri2_dpy
=
457 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
459 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
460 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
461 if (dri2_surf
->color_buffers
[i
].locked
) {
462 dri2_surf
->color_buffers
[i
].wl_release
= true;
464 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
465 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
468 if (dri2_surf
->color_buffers
[i
].dri_image
)
469 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
470 if (dri2_surf
->color_buffers
[i
].linear_copy
)
471 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
472 if (dri2_surf
->color_buffers
[i
].data
)
473 munmap(dri2_surf
->color_buffers
[i
].data
,
474 dri2_surf
->color_buffers
[i
].data_size
);
476 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
477 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
478 dri2_surf
->color_buffers
[i
].data
= NULL
;
482 dri2_egl_surface_free_local_buffers(dri2_surf
);
486 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
488 struct dri2_egl_display
*dri2_dpy
=
489 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
492 unsigned int dri_image_format
;
493 unsigned int linear_dri_image_format
;
497 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
498 assert(visual_idx
!= -1);
499 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
500 linear_dri_image_format
= dri_image_format
;
501 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
502 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
504 /* Substitute dri image format if server does not support original format */
505 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
506 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
508 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
509 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
512 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
513 assert(BITSET_TEST(dri2_dpy
->formats
,
514 dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
516 /* There might be a buffer release already queued that wasn't processed */
517 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
519 while (dri2_surf
->back
== NULL
) {
520 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
521 /* Get an unlocked buffer, preferably one with a dri_buffer
522 * already allocated. */
523 if (dri2_surf
->color_buffers
[i
].locked
)
525 if (dri2_surf
->back
== NULL
)
526 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
527 else if (dri2_surf
->back
->dri_image
== NULL
)
528 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
534 /* If we don't have a buffer, then block on the server to release one for
535 * us, and try again. wl_display_dispatch_queue will process any pending
536 * events, however not all servers flush on issuing a buffer release
537 * event. So, we spam the server with roundtrips as they always cause a
540 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
541 dri2_surf
->wl_queue
) < 0)
545 if (dri2_surf
->back
== NULL
)
548 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
550 if (dri2_dpy
->is_different_gpu
&&
551 dri2_surf
->back
->linear_copy
== NULL
) {
552 /* The LINEAR modifier should be a perfect alias of the LINEAR use
553 * flag; try the new interface first before the old, then fall back. */
554 if (dri2_dpy
->image
->base
.version
>= 15 &&
555 dri2_dpy
->image
->createImageWithModifiers
) {
556 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
558 dri2_surf
->back
->linear_copy
=
559 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
560 dri2_surf
->base
.Width
,
561 dri2_surf
->base
.Height
,
562 linear_dri_image_format
,
567 dri2_surf
->back
->linear_copy
=
568 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
569 dri2_surf
->base
.Width
,
570 dri2_surf
->base
.Height
,
571 linear_dri_image_format
,
573 __DRI_IMAGE_USE_LINEAR
,
576 if (dri2_surf
->back
->linear_copy
== NULL
)
580 if (dri2_surf
->back
->dri_image
== NULL
) {
581 /* If our DRIImage implementation does not support
582 * createImageWithModifiers, then fall back to the old createImage,
583 * and hope it allocates an image which is acceptable to the winsys.
585 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
586 dri2_dpy
->image
->createImageWithModifiers
) {
587 dri2_surf
->back
->dri_image
=
588 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
589 dri2_surf
->base
.Width
,
590 dri2_surf
->base
.Height
,
596 dri2_surf
->back
->dri_image
=
597 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
598 dri2_surf
->base
.Width
,
599 dri2_surf
->base
.Height
,
601 dri2_dpy
->is_different_gpu
?
606 dri2_surf
->back
->age
= 0;
608 if (dri2_surf
->back
->dri_image
== NULL
)
611 dri2_surf
->back
->locked
= true;
618 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
620 struct dri2_egl_display
*dri2_dpy
=
621 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
625 image
= dri2_surf
->back
->dri_image
;
627 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
628 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
630 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
632 buffer
->pitch
= pitch
;
638 update_buffers(struct dri2_egl_surface
*dri2_surf
)
640 struct dri2_egl_display
*dri2_dpy
=
641 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
643 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
644 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
646 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
647 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
648 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
649 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
652 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
653 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
654 dri2_wl_release_buffers(dri2_surf
);
657 if (get_back_bo(dri2_surf
) < 0) {
658 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
662 /* If we have an extra unlocked buffer at this point, we had to do triple
663 * buffering for a while, but now can go back to just double buffering.
664 * That means we can free any unlocked buffer now. */
665 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
666 if (!dri2_surf
->color_buffers
[i
].locked
&&
667 dri2_surf
->color_buffers
[i
].wl_buffer
) {
668 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
669 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
670 if (dri2_dpy
->is_different_gpu
)
671 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
672 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
673 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
674 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
682 update_buffers_if_needed(struct dri2_egl_surface
*dri2_surf
)
684 if (dri2_surf
->back
!= NULL
)
687 return update_buffers(dri2_surf
);
691 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
692 int *width
, int *height
,
693 unsigned int *attachments
, int count
,
694 int *out_count
, void *loaderPrivate
)
696 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
699 if (update_buffers(dri2_surf
) < 0)
702 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
705 switch (attachments
[i
]) {
706 case __DRI_BUFFER_BACK_LEFT
:
707 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
710 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
714 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
717 dri2_surf
->buffers
[j
] = *local
;
726 *width
= dri2_surf
->base
.Width
;
727 *height
= dri2_surf
->base
.Height
;
729 return dri2_surf
->buffers
;
733 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
734 int *width
, int *height
,
735 unsigned int *attachments
, int count
,
736 int *out_count
, void *loaderPrivate
)
738 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
739 unsigned int *attachments_with_format
;
741 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
743 if (visual_idx
== -1)
746 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
747 if (!attachments_with_format
) {
752 for (int i
= 0; i
< count
; ++i
) {
753 attachments_with_format
[2*i
] = attachments
[i
];
754 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
758 dri2_wl_get_buffers_with_format(driDrawable
,
760 attachments_with_format
, count
,
761 out_count
, loaderPrivate
);
763 free(attachments_with_format
);
769 image_get_buffers(__DRIdrawable
*driDrawable
,
773 uint32_t buffer_mask
,
774 struct __DRIimageList
*buffers
)
776 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
778 if (update_buffers(dri2_surf
) < 0)
781 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
782 buffers
->back
= dri2_surf
->back
->dri_image
;
788 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
791 (void) loaderPrivate
;
794 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
795 .base
= { __DRI_DRI2_LOADER
, 3 },
797 .getBuffers
= dri2_wl_get_buffers
,
798 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
799 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
802 static const __DRIimageLoaderExtension image_loader_extension
= {
803 .base
= { __DRI_IMAGE_LOADER
, 1 },
805 .getBuffers
= image_get_buffers
,
806 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
810 wayland_throttle_callback(void *data
,
811 struct wl_callback
*callback
,
814 struct dri2_egl_surface
*dri2_surf
= data
;
816 dri2_surf
->throttle_callback
= NULL
;
817 wl_callback_destroy(callback
);
820 static const struct wl_callback_listener throttle_listener
= {
821 .done
= wayland_throttle_callback
825 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
826 __DRIimage
*image
, int *fourcc
)
832 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
837 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
842 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
843 if (visual_idx
== -1)
846 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
850 static struct wl_buffer
*
851 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
852 struct dri2_egl_surface
*dri2_surf
,
855 struct wl_buffer
*ret
;
857 int width
, height
, fourcc
, num_planes
;
858 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
860 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
861 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
863 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
867 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
872 if (dri2_dpy
->image
->base
.version
>= 15) {
875 query
= dri2_dpy
->image
->queryImage(image
,
876 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
878 query
&= dri2_dpy
->image
->queryImage(image
,
879 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
882 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
886 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
887 struct zwp_linux_buffer_params_v1
*params
;
890 /* We don't need a wrapper for wl_dmabuf objects, because we have to
891 * create the intermediate params object; we can set the queue on this,
892 * and the wl_buffer inherits it race-free. */
893 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
895 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
897 for (i
= 0; i
< num_planes
; i
++) {
902 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
908 query
= dri2_dpy
->image
->queryImage(p_image
,
909 __DRI_IMAGE_ATTRIB_FD
,
911 query
&= dri2_dpy
->image
->queryImage(p_image
,
912 __DRI_IMAGE_ATTRIB_STRIDE
,
914 query
&= dri2_dpy
->image
->queryImage(p_image
,
915 __DRI_IMAGE_ATTRIB_OFFSET
,
917 if (image
!= p_image
)
918 dri2_dpy
->image
->destroyImage(p_image
);
923 zwp_linux_buffer_params_v1_destroy(params
);
927 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
928 modifier
>> 32, modifier
& 0xffffffff);
932 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
934 zwp_linux_buffer_params_v1_destroy(params
);
935 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
936 struct wl_drm
*wl_drm
=
937 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
943 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
944 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
945 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
949 struct wl_drm
*wl_drm
=
950 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
956 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
957 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
958 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
965 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
969 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
970 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
973 for (int i
= 0; i
< n_rects
; i
++) {
974 const int *rect
= &rects
[i
* 4];
976 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
978 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
984 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
987 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
993 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
994 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
996 while (dri2_surf
->throttle_callback
!= NULL
)
997 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
998 dri2_surf
->wl_queue
) == -1)
1001 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
1002 if (dri2_surf
->color_buffers
[i
].age
> 0)
1003 dri2_surf
->color_buffers
[i
].age
++;
1005 /* Make sure we have a back buffer in case we're swapping without ever
1007 if (update_buffers_if_needed(dri2_surf
) < 0)
1008 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
1010 if (draw
->SwapInterval
> 0) {
1011 dri2_surf
->throttle_callback
=
1012 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1013 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1014 &throttle_listener
, dri2_surf
);
1017 dri2_surf
->back
->age
= 1;
1018 dri2_surf
->current
= dri2_surf
->back
;
1019 dri2_surf
->back
= NULL
;
1021 if (!dri2_surf
->current
->wl_buffer
) {
1024 if (dri2_dpy
->is_different_gpu
)
1025 image
= dri2_surf
->current
->linear_copy
;
1027 image
= dri2_surf
->current
->dri_image
;
1029 dri2_surf
->current
->wl_buffer
=
1030 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1032 dri2_surf
->current
->wl_release
= false;
1034 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1035 &wl_buffer_listener
, dri2_surf
);
1038 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1039 dri2_surf
->current
->wl_buffer
,
1040 dri2_surf
->dx
, dri2_surf
->dy
);
1042 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1043 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1044 /* reset resize growing parameters */
1048 /* If the compositor doesn't support damage_buffer, we deliberately
1049 * ignore the damage region and post maximum damage, due to
1050 * https://bugs.freedesktop.org/78190 */
1051 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1052 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1053 0, 0, INT32_MAX
, INT32_MAX
);
1055 if (dri2_dpy
->is_different_gpu
) {
1056 _EGLContext
*ctx
= _eglGetCurrentContext();
1057 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1058 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1059 dri2_surf
->current
->linear_copy
,
1060 dri2_surf
->current
->dri_image
,
1061 0, 0, dri2_surf
->base
.Width
,
1062 dri2_surf
->base
.Height
,
1063 0, 0, dri2_surf
->base
.Width
,
1064 dri2_surf
->base
.Height
, 0);
1067 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1068 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1070 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1072 /* If we're not waiting for a frame callback then we'll at least throttle
1073 * to a sync callback so that we always give a chance for the compositor to
1074 * handle the commit and send a release event before checking for a free
1076 if (dri2_surf
->throttle_callback
== NULL
) {
1077 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1078 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1079 &throttle_listener
, dri2_surf
);
1082 wl_display_flush(dri2_dpy
->wl_dpy
);
1088 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1089 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1091 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1093 if (update_buffers_if_needed(dri2_surf
) < 0) {
1094 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1098 return dri2_surf
->back
->age
;
1102 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1104 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1107 static struct wl_buffer
*
1108 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1112 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1113 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1114 __DRIimage
*image
= dri2_img
->dri_image
;
1115 struct wl_buffer
*buffer
;
1116 int format
, visual_idx
;
1118 /* Check the upstream display supports this buffer's format. */
1119 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1120 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1121 if (visual_idx
== -1)
1124 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
1127 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1129 /* The buffer object will have been created with our internal event queue
1130 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1131 * buffer to be used by the application so we'll reset it to the display's
1132 * default event queue. This isn't actually racy, as the only event the
1133 * buffer can get is a buffer release, which doesn't happen with an explicit
1136 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1141 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1146 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1148 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1151 if (dri2_dpy
->is_render_node
) {
1152 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1153 "authenticate for render-nodes");
1156 dri2_dpy
->authenticated
= false;
1158 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1159 if (roundtrip(dri2_dpy
) < 0)
1162 if (!dri2_dpy
->authenticated
)
1165 /* reset authenticated */
1166 dri2_dpy
->authenticated
= true;
1172 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1174 struct dri2_egl_display
*dri2_dpy
= data
;
1177 dri2_dpy
->device_name
= strdup(device
);
1178 if (!dri2_dpy
->device_name
)
1181 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1182 if (dri2_dpy
->fd
== -1) {
1183 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1184 dri2_dpy
->device_name
, strerror(errno
));
1185 free(dri2_dpy
->device_name
);
1186 dri2_dpy
->device_name
= NULL
;
1190 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1191 dri2_dpy
->authenticated
= true;
1193 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1194 close(dri2_dpy
->fd
);
1196 free(dri2_dpy
->device_name
);
1197 dri2_dpy
->device_name
= NULL
;
1198 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1201 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1206 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1208 struct dri2_egl_display
*dri2_dpy
= data
;
1209 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1211 if (visual_idx
== -1)
1214 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1218 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1220 struct dri2_egl_display
*dri2_dpy
= data
;
1222 dri2_dpy
->capabilities
= value
;
1226 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1228 struct dri2_egl_display
*dri2_dpy
= data
;
1230 dri2_dpy
->authenticated
= true;
1233 static const struct wl_drm_listener drm_listener
= {
1234 .device
= drm_handle_device
,
1235 .format
= drm_handle_format
,
1236 .authenticated
= drm_handle_authenticated
,
1237 .capabilities
= drm_handle_capabilities
1241 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1244 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1248 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1249 uint32_t format
, uint32_t modifier_hi
,
1250 uint32_t modifier_lo
)
1252 struct dri2_egl_display
*dri2_dpy
= data
;
1253 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1256 if (visual_idx
== -1)
1259 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1260 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1263 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1265 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1266 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1269 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1270 .format
= dmabuf_ignore_format
,
1271 .modifier
= dmabuf_handle_modifier
,
1275 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1276 uint32_t name
, const char *interface
,
1279 struct dri2_egl_display
*dri2_dpy
= data
;
1281 if (strcmp(interface
, "wl_drm") == 0) {
1283 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1284 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1285 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1286 dri2_dpy
->wl_dmabuf
=
1287 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1289 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1295 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1300 static const struct wl_registry_listener registry_listener_drm
= {
1301 .global
= registry_handle_global_drm
,
1302 .global_remove
= registry_handle_global_remove
1306 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1308 /* We can't use values greater than 1 on Wayland because we are using the
1309 * frame callback to synchronise the frame and the only way we be sure to
1310 * get a frame callback is to attach a new buffer. Therefore we can't just
1311 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1313 dri2_setup_swap_interval(disp
, 1);
1316 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1317 .authenticate
= dri2_wl_authenticate
,
1318 .create_window_surface
= dri2_wl_create_window_surface
,
1319 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1320 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1321 .destroy_surface
= dri2_wl_destroy_surface
,
1322 .create_image
= dri2_create_image_khr
,
1323 .swap_buffers
= dri2_wl_swap_buffers
,
1324 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1325 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1326 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1327 .copy_buffers
= dri2_fallback_copy_buffers
,
1328 .query_buffer_age
= dri2_wl_query_buffer_age
,
1329 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1330 .get_sync_values
= dri2_fallback_get_sync_values
,
1331 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1334 static const __DRIextension
*dri2_loader_extensions
[] = {
1335 &dri2_loader_extension
.base
,
1336 &image_loader_extension
.base
,
1337 &image_lookup_extension
.base
,
1338 &use_invalidate
.base
,
1342 static const __DRIextension
*image_loader_extensions
[] = {
1343 &image_loader_extension
.base
,
1344 &image_lookup_extension
.base
,
1345 &use_invalidate
.base
,
1350 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1352 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1353 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1354 unsigned int count
= 0;
1357 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1360 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1361 struct dri2_egl_config
*dri2_conf
;
1363 if (!BITSET_TEST(dri2_dpy
->formats
, j
))
1366 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1367 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_shifts
, dri2_wl_visuals
[j
].rgba_sizes
);
1369 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1376 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1377 struct dri2_egl_config
*dri2_conf
;
1378 int alt_dri_image_format
, c
, s
;
1380 /* No match for config. Try if we can blitImage convert to a visual */
1381 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1382 dri2_dpy
->driver_configs
[i
]);
1387 /* Find optimal target visual for blitImage conversion, if any. */
1388 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1389 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1391 if (s
== -1 || !BITSET_TEST(dri2_dpy
->formats
, s
))
1394 /* Visual s works for the Wayland server, and c can be converted into s
1395 * by our client gpu during PRIME blitImage conversion to a linear
1396 * wl_buffer, so add visual c as supported by the client renderer.
1398 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1399 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1400 dri2_wl_visuals
[c
].rgba_shifts
,
1401 dri2_wl_visuals
[c
].rgba_sizes
);
1403 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1406 if (format_count
[c
] == 1)
1407 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1408 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1409 dri2_wl_visuals
[s
].format_name
);
1414 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1415 if (!format_count
[i
]) {
1416 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1417 dri2_wl_visuals
[i
].format_name
);
1421 return (count
!= 0);
1425 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1428 struct dri2_egl_display
*dri2_dpy
;
1430 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1432 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1435 disp
->DriverData
= (void *) dri2_dpy
;
1436 if (disp
->PlatformDisplay
== NULL
) {
1437 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1438 if (dri2_dpy
->wl_dpy
== NULL
)
1440 dri2_dpy
->own_device
= true;
1442 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1445 dri2_dpy
->wl_modifiers
=
1446 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1447 if (!dri2_dpy
->wl_modifiers
)
1449 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1450 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1454 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1456 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1457 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1460 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1461 dri2_dpy
->wl_queue
);
1463 if (dri2_dpy
->own_device
)
1464 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1466 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1467 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1468 ®istry_listener_drm
, dri2_dpy
);
1469 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1472 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1475 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1478 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1479 &dri2_dpy
->is_different_gpu
);
1480 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1482 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1488 if (dri2_dpy
->is_different_gpu
) {
1489 free(dri2_dpy
->device_name
);
1490 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1491 if (!dri2_dpy
->device_name
) {
1492 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1493 "for requested GPU");
1498 /* we have to do the check now, because loader_get_user_preferred_fd
1499 * will return a render-node when the requested gpu is different
1500 * to the server, but also if the client asks for the same gpu than
1501 * the server by requesting its pci-id */
1502 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1504 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1505 if (dri2_dpy
->driver_name
== NULL
) {
1506 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1510 /* render nodes cannot use Gem names, and thus do not support
1511 * the __DRI_DRI2_LOADER extension */
1512 if (!dri2_dpy
->is_render_node
) {
1513 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1514 if (!dri2_load_driver(disp
)) {
1515 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1519 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1520 if (!dri2_load_driver_dri3(disp
)) {
1521 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1526 if (!dri2_create_screen(disp
))
1529 if (!dri2_setup_extensions(disp
))
1532 dri2_setup_screen(disp
);
1534 dri2_wl_setup_swap_interval(disp
);
1536 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1537 * createImageFromFds support indicates that Prime export/import
1538 * is supported by the driver. Fall back to
1539 * gem names if we don't have Prime support. */
1541 if (dri2_dpy
->image
->base
.version
< 7 ||
1542 dri2_dpy
->image
->createImageFromFds
== NULL
)
1543 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1545 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1546 * The server needs to accept them */
1547 if (dri2_dpy
->is_render_node
&&
1548 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1549 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1553 if (dri2_dpy
->is_different_gpu
&&
1554 (dri2_dpy
->image
->base
.version
< 9 ||
1555 dri2_dpy
->image
->blitImage
== NULL
)) {
1556 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1557 "Image extension in the driver is not "
1558 "compatible. Version 9 or later and blitImage() "
1563 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1564 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1568 dri2_set_WL_bind_wayland_display(drv
, disp
);
1569 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1570 * because the buffer of the EGLImage has likely a tiling mode the server
1571 * gpu won't support. These is no way to check for now. Thus do not support the
1573 if (!dri2_dpy
->is_different_gpu
)
1574 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1576 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1578 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1580 /* Fill vtbl last to prevent accidentally calling virtual function during
1583 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1588 dri2_display_destroy(disp
);
1593 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1595 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1597 assume(visual_idx
!= -1);
1599 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1603 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1604 int format
, int w
, int h
,
1605 void **data
, int *size
,
1606 struct wl_buffer
**buffer
)
1608 struct dri2_egl_display
*dri2_dpy
=
1609 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1610 struct wl_shm_pool
*pool
;
1611 int fd
, stride
, size_map
;
1614 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1615 size_map
= h
* stride
;
1617 /* Create a shareable buffer */
1618 fd
= os_create_anonymous_file(size_map
, NULL
);
1622 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1623 if (data_map
== MAP_FAILED
) {
1628 /* Share it in a wl_buffer */
1629 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1630 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1631 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1632 wl_shm_pool_destroy(pool
);
1641 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1643 struct dri2_egl_display
*dri2_dpy
=
1644 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1646 /* we need to do the following operations only once per frame */
1647 if (dri2_surf
->back
)
1650 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1651 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1653 dri2_wl_release_buffers(dri2_surf
);
1655 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1656 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1657 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1658 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1659 dri2_surf
->current
= NULL
;
1662 /* find back buffer */
1664 /* There might be a buffer release already queued that wasn't processed */
1665 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1667 /* try get free buffer already created */
1668 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1669 if (!dri2_surf
->color_buffers
[i
].locked
&&
1670 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1671 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1676 /* else choose any another free location */
1677 if (!dri2_surf
->back
) {
1678 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1679 if (!dri2_surf
->color_buffers
[i
].locked
) {
1680 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1681 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1683 dri2_surf
->base
.Width
,
1684 dri2_surf
->base
.Height
,
1685 &dri2_surf
->back
->data
,
1686 &dri2_surf
->back
->data_size
,
1687 &dri2_surf
->back
->wl_buffer
)) {
1688 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1691 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1692 &wl_buffer_listener
, dri2_surf
);
1698 if (!dri2_surf
->back
) {
1699 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1703 dri2_surf
->back
->locked
= true;
1705 /* If we have an extra unlocked buffer at this point, we had to do triple
1706 * buffering for a while, but now can go back to just double buffering.
1707 * That means we can free any unlocked buffer now. */
1708 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1709 if (!dri2_surf
->color_buffers
[i
].locked
&&
1710 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1711 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1712 munmap(dri2_surf
->color_buffers
[i
].data
,
1713 dri2_surf
->color_buffers
[i
].data_size
);
1714 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1715 dri2_surf
->color_buffers
[i
].data
= NULL
;
1723 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1725 /* if there has been a resize: */
1726 if (!dri2_surf
->current
)
1729 return dri2_surf
->current
->data
;
1733 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1735 assert(dri2_surf
->back
);
1736 return dri2_surf
->back
->data
;
1740 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1742 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1744 while (dri2_surf
->throttle_callback
!= NULL
)
1745 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1746 dri2_surf
->wl_queue
) == -1)
1749 if (dri2_surf
->base
.SwapInterval
> 0) {
1750 dri2_surf
->throttle_callback
=
1751 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1752 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1753 &throttle_listener
, dri2_surf
);
1756 dri2_surf
->current
= dri2_surf
->back
;
1757 dri2_surf
->back
= NULL
;
1759 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1760 dri2_surf
->current
->wl_buffer
,
1761 dri2_surf
->dx
, dri2_surf
->dy
);
1763 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1764 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1765 /* reset resize growing parameters */
1769 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1770 0, 0, INT32_MAX
, INT32_MAX
);
1771 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1773 /* If we're not waiting for a frame callback then we'll at least throttle
1774 * to a sync callback so that we always give a chance for the compositor to
1775 * handle the commit and send a release event before checking for a free
1777 if (dri2_surf
->throttle_callback
== NULL
) {
1778 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1779 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1780 &throttle_listener
, dri2_surf
);
1783 wl_display_flush(dri2_dpy
->wl_dpy
);
1787 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1788 int *x
, int *y
, int *w
, int *h
,
1789 void *loaderPrivate
)
1791 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1793 (void) swrast_update_buffers(dri2_surf
);
1796 *w
= dri2_surf
->base
.Width
;
1797 *h
= dri2_surf
->base
.Height
;
1801 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1802 int x
, int y
, int w
, int h
,
1803 char *data
, void *loaderPrivate
)
1805 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1806 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1807 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1808 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1809 int dst_stride
= copy_width
;
1812 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1814 memset(data
, 0, copy_width
* h
);
1818 assert(data
!= src
);
1819 assert(copy_width
<= src_stride
);
1822 src
+= y
* src_stride
;
1825 if (copy_width
> src_stride
-x_offset
)
1826 copy_width
= src_stride
-x_offset
;
1827 if (h
> dri2_surf
->base
.Height
-y
)
1828 h
= dri2_surf
->base
.Height
-y
;
1831 memcpy(dst
, src
, copy_width
);
1838 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1839 int x
, int y
, int w
, int h
, int stride
,
1840 char *data
, void *loaderPrivate
)
1842 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1843 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1844 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1845 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1848 assert(copy_width
<= stride
);
1850 (void) swrast_update_buffers(dri2_surf
);
1851 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1853 /* partial copy, copy old content */
1854 if (copy_width
< dst_stride
)
1855 dri2_wl_swrast_get_image(draw
, 0, 0,
1856 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1857 dst
, loaderPrivate
);
1860 dst
+= y
* dst_stride
;
1864 /* drivers expect we do these checks (and some rely on it) */
1865 if (copy_width
> dst_stride
-x_offset
)
1866 copy_width
= dst_stride
-x_offset
;
1867 if (h
> dri2_surf
->base
.Height
-y
)
1868 h
= dri2_surf
->base
.Height
-y
;
1871 memcpy(dst
, src
, copy_width
);
1875 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1879 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1880 int x
, int y
, int w
, int h
,
1881 char *data
, void *loaderPrivate
)
1883 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1886 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1887 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1888 stride
, data
, loaderPrivate
);
1892 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1894 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1895 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1897 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1902 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1904 struct dri2_egl_display
*dri2_dpy
= data
;
1905 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1907 if (visual_idx
== -1)
1910 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1913 static const struct wl_shm_listener shm_listener
= {
1914 .format
= shm_handle_format
1918 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1919 uint32_t name
, const char *interface
,
1922 struct dri2_egl_display
*dri2_dpy
= data
;
1924 if (strcmp(interface
, "wl_shm") == 0) {
1926 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1927 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1931 static const struct wl_registry_listener registry_listener_swrast
= {
1932 .global
= registry_handle_global_swrast
,
1933 .global_remove
= registry_handle_global_remove
1936 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1937 .authenticate
= NULL
,
1938 .create_window_surface
= dri2_wl_create_window_surface
,
1939 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1940 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1941 .destroy_surface
= dri2_wl_destroy_surface
,
1942 .create_image
= dri2_create_image_khr
,
1943 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1944 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1945 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1946 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1947 .copy_buffers
= dri2_fallback_copy_buffers
,
1948 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1949 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1950 .get_sync_values
= dri2_fallback_get_sync_values
,
1951 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1954 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1955 .base
= { __DRI_SWRAST_LOADER
, 2 },
1957 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1958 .putImage
= dri2_wl_swrast_put_image
,
1959 .getImage
= dri2_wl_swrast_get_image
,
1960 .putImage2
= dri2_wl_swrast_put_image2
,
1963 static const __DRIextension
*swrast_loader_extensions
[] = {
1964 &swrast_loader_extension
.base
,
1965 &image_lookup_extension
.base
,
1970 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1973 struct dri2_egl_display
*dri2_dpy
;
1975 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1977 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1980 disp
->DriverData
= (void *) dri2_dpy
;
1981 if (disp
->PlatformDisplay
== NULL
) {
1982 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1983 if (dri2_dpy
->wl_dpy
== NULL
)
1985 dri2_dpy
->own_device
= true;
1987 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1990 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
1992 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1998 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2000 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2001 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2004 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2005 dri2_dpy
->wl_queue
);
2007 if (dri2_dpy
->own_device
)
2008 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2010 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2011 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2012 ®istry_listener_swrast
, dri2_dpy
);
2014 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2017 if (roundtrip(dri2_dpy
) < 0 || !BITSET_TEST_RANGE(dri2_dpy
->formats
,
2018 0, EGL_DRI2_MAX_FORMATS
))
2021 dri2_dpy
->driver_name
= strdup("swrast");
2022 if (!dri2_load_driver_swrast(disp
))
2025 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2027 if (!dri2_create_screen(disp
))
2030 if (!dri2_setup_extensions(disp
))
2033 dri2_setup_screen(disp
);
2035 dri2_wl_setup_swap_interval(disp
);
2037 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2038 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2042 /* Fill vtbl last to prevent accidentally calling virtual function during
2045 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2050 dri2_display_destroy(disp
);
2055 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2057 EGLBoolean initialized
= EGL_FALSE
;
2059 if (!disp
->Options
.ForceSoftware
)
2060 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2063 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2070 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2072 if (dri2_dpy
->wl_drm
)
2073 wl_drm_destroy(dri2_dpy
->wl_drm
);
2074 if (dri2_dpy
->wl_dmabuf
)
2075 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2076 if (dri2_dpy
->wl_shm
)
2077 wl_shm_destroy(dri2_dpy
->wl_shm
);
2078 if (dri2_dpy
->wl_registry
)
2079 wl_registry_destroy(dri2_dpy
->wl_registry
);
2080 if (dri2_dpy
->wl_queue
)
2081 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2082 if (dri2_dpy
->wl_dpy_wrapper
)
2083 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2085 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2086 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2087 free(dri2_dpy
->wl_modifiers
);
2089 if (dri2_dpy
->own_device
)
2090 wl_display_disconnect(dri2_dpy
->wl_dpy
);