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
;
72 unsigned int rgba_masks
[4];
73 } dri2_wl_visuals
[] = {
76 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
77 __DRI_IMAGE_FORMAT_XRGB2101010
, __DRI_IMAGE_FORMAT_XBGR2101010
, 32,
78 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0x00000000 }
82 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
83 __DRI_IMAGE_FORMAT_ARGB2101010
, __DRI_IMAGE_FORMAT_ABGR2101010
, 32,
84 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 }
88 WL_DRM_FORMAT_XBGR2101010
, WL_SHM_FORMAT_XBGR2101010
,
89 __DRI_IMAGE_FORMAT_XBGR2101010
, __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
90 { 0x000003ff, 0x000ffc00, 0x3ff00000, 0x00000000 }
94 WL_DRM_FORMAT_ABGR2101010
, WL_SHM_FORMAT_ABGR2101010
,
95 __DRI_IMAGE_FORMAT_ABGR2101010
, __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
96 { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }
100 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
101 __DRI_IMAGE_FORMAT_XRGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
102 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }
106 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
107 __DRI_IMAGE_FORMAT_ARGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
108 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }
112 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
113 __DRI_IMAGE_FORMAT_RGB565
, __DRI_IMAGE_FORMAT_NONE
, 16,
114 { 0xf800, 0x07e0, 0x001f, 0x0000 }
118 static_assert(ARRAY_SIZE(dri2_wl_visuals
) <= EGL_DRI2_MAX_FORMATS
,
119 "dri2_egl_display::formats is not large enough for "
120 "the formats in dri2_wl_visuals");
123 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
124 const __DRIconfig
*config
)
126 unsigned int red
, green
, blue
, alpha
;
128 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &red
);
129 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &green
);
130 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &blue
);
131 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &alpha
);
133 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
134 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
136 if (red
== wl_visual
->rgba_masks
[0] &&
137 green
== wl_visual
->rgba_masks
[1] &&
138 blue
== wl_visual
->rgba_masks
[2] &&
139 alpha
== wl_visual
->rgba_masks
[3]) {
148 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
150 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
151 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
153 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
161 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
163 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
164 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
172 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
174 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
175 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
183 dri2_wl_is_format_supported(void* user_data
, uint32_t format
)
185 _EGLDisplay
*disp
= (_EGLDisplay
*) user_data
;
186 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
187 int j
= dri2_wl_visual_idx_from_fourcc(format
);
192 for (int i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
193 if (j
== dri2_wl_visual_idx_from_config(dri2_dpy
,
194 dri2_dpy
->driver_configs
[i
]))
201 roundtrip(struct dri2_egl_display
*dri2_dpy
)
203 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
207 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
209 struct dri2_egl_surface
*dri2_surf
= data
;
212 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
213 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
216 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
218 if (dri2_surf
->color_buffers
[i
].wl_release
) {
219 wl_buffer_destroy(buffer
);
220 dri2_surf
->color_buffers
[i
].wl_release
= false;
221 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
224 dri2_surf
->color_buffers
[i
].locked
= false;
227 static const struct wl_buffer_listener wl_buffer_listener
= {
228 .release
= wl_buffer_release
232 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
234 struct dri2_egl_surface
*dri2_surf
= data
;
235 struct dri2_egl_display
*dri2_dpy
=
236 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
238 /* Update the surface size as soon as native window is resized; from user
239 * pov, this makes the effect that resize is done immediately after native
240 * window resize, without requiring to wait until the first draw.
242 * A more detailed and lengthy explanation can be found at
243 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
245 if (!dri2_surf
->back
) {
246 dri2_surf
->base
.Width
= wl_win
->width
;
247 dri2_surf
->base
.Height
= wl_win
->height
;
249 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
253 destroy_window_callback(void *data
)
255 struct dri2_egl_surface
*dri2_surf
= data
;
256 dri2_surf
->wl_win
= NULL
;
259 static struct wl_surface
*
260 get_wl_surface_proxy(struct wl_egl_window
*window
)
262 /* Version 3 of wl_egl_window introduced a version field at the same
263 * location where a pointer to wl_surface was stored. Thus, if
264 * window->version is dereferenceable, we've been given an older version of
265 * wl_egl_window, and window->version points to wl_surface */
266 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
267 return wl_proxy_create_wrapper((void *)(window
->version
));
269 return wl_proxy_create_wrapper(window
->surface
);
273 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
276 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
277 _EGLConfig
*conf
, void *native_window
,
278 const EGLint
*attrib_list
)
280 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
281 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
282 struct wl_egl_window
*window
= native_window
;
283 struct dri2_egl_surface
*dri2_surf
;
285 const __DRIconfig
*config
;
287 dri2_surf
= calloc(1, sizeof *dri2_surf
);
289 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
293 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
294 attrib_list
, false, native_window
))
297 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
298 dri2_surf
->base
.GLColorspace
);
301 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
305 dri2_surf
->base
.Width
= window
->width
;
306 dri2_surf
->base
.Height
= window
->height
;
308 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
309 assert(visual_idx
!= -1);
311 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
312 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
314 assert(dri2_dpy
->wl_shm
);
315 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
318 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
319 if (!dri2_surf
->wl_queue
) {
320 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
324 if (dri2_dpy
->wl_drm
) {
325 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
326 if (!dri2_surf
->wl_drm_wrapper
) {
327 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
330 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
331 dri2_surf
->wl_queue
);
334 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
335 if (!dri2_surf
->wl_dpy_wrapper
) {
336 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
339 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
340 dri2_surf
->wl_queue
);
342 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
343 if (!dri2_surf
->wl_surface_wrapper
) {
344 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
345 goto cleanup_dpy_wrapper
;
347 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
348 dri2_surf
->wl_queue
);
350 dri2_surf
->wl_win
= window
;
351 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
352 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
354 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
356 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
357 goto cleanup_surf_wrapper
;
359 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
361 return &dri2_surf
->base
;
363 cleanup_surf_wrapper
:
364 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
366 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
368 if (dri2_surf
->wl_drm_wrapper
)
369 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
371 wl_event_queue_destroy(dri2_surf
->wl_queue
);
379 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
380 _EGLConfig
*conf
, void *native_window
,
381 const EGLint
*attrib_list
)
383 /* From the EGL_EXT_platform_wayland spec, version 3:
385 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
386 * that belongs to Wayland. Any such call fails and generates
389 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
395 * Called via eglDestroySurface(), drv->API.DestroySurface().
398 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
400 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
401 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
405 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
407 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
408 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
409 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
410 if (dri2_surf
->color_buffers
[i
].dri_image
)
411 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
412 if (dri2_surf
->color_buffers
[i
].linear_copy
)
413 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
414 if (dri2_surf
->color_buffers
[i
].data
)
415 munmap(dri2_surf
->color_buffers
[i
].data
,
416 dri2_surf
->color_buffers
[i
].data_size
);
420 dri2_egl_surface_free_local_buffers(dri2_surf
);
422 if (dri2_surf
->throttle_callback
)
423 wl_callback_destroy(dri2_surf
->throttle_callback
);
425 if (dri2_surf
->wl_win
) {
426 dri2_surf
->wl_win
->driver_private
= NULL
;
427 dri2_surf
->wl_win
->resize_callback
= NULL
;
428 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
431 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
432 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
433 if (dri2_surf
->wl_drm_wrapper
)
434 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
435 wl_event_queue_destroy(dri2_surf
->wl_queue
);
437 dri2_fini_surface(surf
);
444 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
446 struct dri2_egl_display
*dri2_dpy
=
447 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
449 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
450 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
451 if (dri2_surf
->color_buffers
[i
].locked
) {
452 dri2_surf
->color_buffers
[i
].wl_release
= true;
454 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
455 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
458 if (dri2_surf
->color_buffers
[i
].dri_image
)
459 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
460 if (dri2_surf
->color_buffers
[i
].linear_copy
)
461 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
462 if (dri2_surf
->color_buffers
[i
].data
)
463 munmap(dri2_surf
->color_buffers
[i
].data
,
464 dri2_surf
->color_buffers
[i
].data_size
);
466 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
467 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
468 dri2_surf
->color_buffers
[i
].data
= NULL
;
472 dri2_egl_surface_free_local_buffers(dri2_surf
);
476 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
478 struct dri2_egl_display
*dri2_dpy
=
479 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
482 unsigned int dri_image_format
;
483 unsigned int linear_dri_image_format
;
487 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
488 assert(visual_idx
!= -1);
489 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
490 linear_dri_image_format
= dri_image_format
;
491 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
492 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
494 /* Substitute dri image format if server does not support original format */
495 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
496 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
498 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
499 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
502 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
503 assert(BITSET_TEST(dri2_dpy
->formats
,
504 dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
506 /* There might be a buffer release already queued that wasn't processed */
507 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
509 while (dri2_surf
->back
== NULL
) {
510 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
511 /* Get an unlocked buffer, preferably one with a dri_buffer
512 * already allocated. */
513 if (dri2_surf
->color_buffers
[i
].locked
)
515 if (dri2_surf
->back
== NULL
)
516 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
517 else if (dri2_surf
->back
->dri_image
== NULL
)
518 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
524 /* If we don't have a buffer, then block on the server to release one for
525 * us, and try again. wl_display_dispatch_queue will process any pending
526 * events, however not all servers flush on issuing a buffer release
527 * event. So, we spam the server with roundtrips as they always cause a
530 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
531 dri2_surf
->wl_queue
) < 0)
535 if (dri2_surf
->back
== NULL
)
538 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
540 if (dri2_dpy
->is_different_gpu
&&
541 dri2_surf
->back
->linear_copy
== NULL
) {
542 /* The LINEAR modifier should be a perfect alias of the LINEAR use
543 * flag; try the new interface first before the old, then fall back. */
544 if (dri2_dpy
->image
->base
.version
>= 15 &&
545 dri2_dpy
->image
->createImageWithModifiers
) {
546 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
548 dri2_surf
->back
->linear_copy
=
549 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
550 dri2_surf
->base
.Width
,
551 dri2_surf
->base
.Height
,
552 linear_dri_image_format
,
557 dri2_surf
->back
->linear_copy
=
558 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
559 dri2_surf
->base
.Width
,
560 dri2_surf
->base
.Height
,
561 linear_dri_image_format
,
563 __DRI_IMAGE_USE_LINEAR
,
566 if (dri2_surf
->back
->linear_copy
== NULL
)
570 if (dri2_surf
->back
->dri_image
== NULL
) {
571 /* If our DRIImage implementation does not support
572 * createImageWithModifiers, then fall back to the old createImage,
573 * and hope it allocates an image which is acceptable to the winsys.
575 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
576 dri2_dpy
->image
->createImageWithModifiers
) {
577 dri2_surf
->back
->dri_image
=
578 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
579 dri2_surf
->base
.Width
,
580 dri2_surf
->base
.Height
,
586 dri2_surf
->back
->dri_image
=
587 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
588 dri2_surf
->base
.Width
,
589 dri2_surf
->base
.Height
,
591 dri2_dpy
->is_different_gpu
?
596 dri2_surf
->back
->age
= 0;
598 if (dri2_surf
->back
->dri_image
== NULL
)
601 dri2_surf
->back
->locked
= true;
608 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
610 struct dri2_egl_display
*dri2_dpy
=
611 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
615 image
= dri2_surf
->back
->dri_image
;
617 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
618 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
620 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
622 buffer
->pitch
= pitch
;
628 update_buffers(struct dri2_egl_surface
*dri2_surf
)
630 struct dri2_egl_display
*dri2_dpy
=
631 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
633 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
634 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
636 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
637 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
638 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
639 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
642 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
643 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
644 dri2_wl_release_buffers(dri2_surf
);
647 if (get_back_bo(dri2_surf
) < 0) {
648 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
652 /* If we have an extra unlocked buffer at this point, we had to do triple
653 * buffering for a while, but now can go back to just double buffering.
654 * That means we can free any unlocked buffer now. */
655 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
656 if (!dri2_surf
->color_buffers
[i
].locked
&&
657 dri2_surf
->color_buffers
[i
].wl_buffer
) {
658 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
659 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
660 if (dri2_dpy
->is_different_gpu
)
661 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
662 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
663 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
664 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
672 update_buffers_if_needed(struct dri2_egl_surface
*dri2_surf
)
674 if (dri2_surf
->back
!= NULL
)
677 return update_buffers(dri2_surf
);
681 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
682 int *width
, int *height
,
683 unsigned int *attachments
, int count
,
684 int *out_count
, void *loaderPrivate
)
686 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
689 if (update_buffers(dri2_surf
) < 0)
692 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
695 switch (attachments
[i
]) {
696 case __DRI_BUFFER_BACK_LEFT
:
697 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
700 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
704 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
707 dri2_surf
->buffers
[j
] = *local
;
716 *width
= dri2_surf
->base
.Width
;
717 *height
= dri2_surf
->base
.Height
;
719 return dri2_surf
->buffers
;
723 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
724 int *width
, int *height
,
725 unsigned int *attachments
, int count
,
726 int *out_count
, void *loaderPrivate
)
728 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
729 unsigned int *attachments_with_format
;
731 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
733 if (visual_idx
== -1)
736 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
737 if (!attachments_with_format
) {
742 for (int i
= 0; i
< count
; ++i
) {
743 attachments_with_format
[2*i
] = attachments
[i
];
744 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
748 dri2_wl_get_buffers_with_format(driDrawable
,
750 attachments_with_format
, count
,
751 out_count
, loaderPrivate
);
753 free(attachments_with_format
);
759 image_get_buffers(__DRIdrawable
*driDrawable
,
763 uint32_t buffer_mask
,
764 struct __DRIimageList
*buffers
)
766 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
768 if (update_buffers(dri2_surf
) < 0)
771 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
772 buffers
->back
= dri2_surf
->back
->dri_image
;
778 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
781 (void) loaderPrivate
;
784 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
785 .base
= { __DRI_DRI2_LOADER
, 3 },
787 .getBuffers
= dri2_wl_get_buffers
,
788 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
789 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
792 static const __DRIimageLoaderExtension image_loader_extension
= {
793 .base
= { __DRI_IMAGE_LOADER
, 1 },
795 .getBuffers
= image_get_buffers
,
796 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
800 wayland_throttle_callback(void *data
,
801 struct wl_callback
*callback
,
804 struct dri2_egl_surface
*dri2_surf
= data
;
806 dri2_surf
->throttle_callback
= NULL
;
807 wl_callback_destroy(callback
);
810 static const struct wl_callback_listener throttle_listener
= {
811 .done
= wayland_throttle_callback
815 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
816 __DRIimage
*image
, int *fourcc
)
822 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
827 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
832 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
833 if (visual_idx
== -1)
836 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
840 static struct wl_buffer
*
841 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
842 struct dri2_egl_surface
*dri2_surf
,
845 struct wl_buffer
*ret
;
847 int width
, height
, fourcc
, num_planes
;
848 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
850 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
851 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
853 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
857 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
862 if (dri2_dpy
->image
->base
.version
>= 15) {
865 query
= dri2_dpy
->image
->queryImage(image
,
866 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
868 query
&= dri2_dpy
->image
->queryImage(image
,
869 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
872 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
876 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
877 struct zwp_linux_buffer_params_v1
*params
;
880 /* We don't need a wrapper for wl_dmabuf objects, because we have to
881 * create the intermediate params object; we can set the queue on this,
882 * and the wl_buffer inherits it race-free. */
883 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
885 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
887 for (i
= 0; i
< num_planes
; i
++) {
892 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
898 query
= dri2_dpy
->image
->queryImage(p_image
,
899 __DRI_IMAGE_ATTRIB_FD
,
901 query
&= dri2_dpy
->image
->queryImage(p_image
,
902 __DRI_IMAGE_ATTRIB_STRIDE
,
904 query
&= dri2_dpy
->image
->queryImage(p_image
,
905 __DRI_IMAGE_ATTRIB_OFFSET
,
907 if (image
!= p_image
)
908 dri2_dpy
->image
->destroyImage(p_image
);
913 zwp_linux_buffer_params_v1_destroy(params
);
917 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
918 modifier
>> 32, modifier
& 0xffffffff);
922 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
924 zwp_linux_buffer_params_v1_destroy(params
);
925 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
926 struct wl_drm
*wl_drm
=
927 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
933 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
934 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
935 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
939 struct wl_drm
*wl_drm
=
940 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
946 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
947 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
948 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
955 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
959 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
960 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
963 for (int i
= 0; i
< n_rects
; i
++) {
964 const int *rect
= &rects
[i
* 4];
966 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
968 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
974 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
977 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
983 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
984 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
986 while (dri2_surf
->throttle_callback
!= NULL
)
987 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
988 dri2_surf
->wl_queue
) == -1)
991 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
992 if (dri2_surf
->color_buffers
[i
].age
> 0)
993 dri2_surf
->color_buffers
[i
].age
++;
995 /* Make sure we have a back buffer in case we're swapping without ever
997 if (update_buffers_if_needed(dri2_surf
) < 0)
998 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
1000 if (draw
->SwapInterval
> 0) {
1001 dri2_surf
->throttle_callback
=
1002 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1003 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1004 &throttle_listener
, dri2_surf
);
1007 dri2_surf
->back
->age
= 1;
1008 dri2_surf
->current
= dri2_surf
->back
;
1009 dri2_surf
->back
= NULL
;
1011 if (!dri2_surf
->current
->wl_buffer
) {
1014 if (dri2_dpy
->is_different_gpu
)
1015 image
= dri2_surf
->current
->linear_copy
;
1017 image
= dri2_surf
->current
->dri_image
;
1019 dri2_surf
->current
->wl_buffer
=
1020 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1022 dri2_surf
->current
->wl_release
= false;
1024 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1025 &wl_buffer_listener
, dri2_surf
);
1028 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1029 dri2_surf
->current
->wl_buffer
,
1030 dri2_surf
->dx
, dri2_surf
->dy
);
1032 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1033 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1034 /* reset resize growing parameters */
1038 /* If the compositor doesn't support damage_buffer, we deliberately
1039 * ignore the damage region and post maximum damage, due to
1040 * https://bugs.freedesktop.org/78190 */
1041 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1042 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1043 0, 0, INT32_MAX
, INT32_MAX
);
1045 if (dri2_dpy
->is_different_gpu
) {
1046 _EGLContext
*ctx
= _eglGetCurrentContext();
1047 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1048 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1049 dri2_surf
->current
->linear_copy
,
1050 dri2_surf
->current
->dri_image
,
1051 0, 0, dri2_surf
->base
.Width
,
1052 dri2_surf
->base
.Height
,
1053 0, 0, dri2_surf
->base
.Width
,
1054 dri2_surf
->base
.Height
, 0);
1057 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1058 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1060 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1062 /* If we're not waiting for a frame callback then we'll at least throttle
1063 * to a sync callback so that we always give a chance for the compositor to
1064 * handle the commit and send a release event before checking for a free
1066 if (dri2_surf
->throttle_callback
== NULL
) {
1067 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1068 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1069 &throttle_listener
, dri2_surf
);
1072 wl_display_flush(dri2_dpy
->wl_dpy
);
1078 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1079 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1081 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1083 if (update_buffers_if_needed(dri2_surf
) < 0) {
1084 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1088 return dri2_surf
->back
->age
;
1092 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1094 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1097 static struct wl_buffer
*
1098 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1102 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1103 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1104 __DRIimage
*image
= dri2_img
->dri_image
;
1105 struct wl_buffer
*buffer
;
1106 int format
, visual_idx
;
1108 /* Check the upstream display supports this buffer's format. */
1109 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1110 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1111 if (visual_idx
== -1)
1114 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
1117 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1119 /* The buffer object will have been created with our internal event queue
1120 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1121 * buffer to be used by the application so we'll reset it to the display's
1122 * default event queue. This isn't actually racy, as the only event the
1123 * buffer can get is a buffer release, which doesn't happen with an explicit
1126 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1131 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1136 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1138 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1141 if (dri2_dpy
->is_render_node
) {
1142 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1143 "authenticate for render-nodes");
1146 dri2_dpy
->authenticated
= false;
1148 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1149 if (roundtrip(dri2_dpy
) < 0)
1152 if (!dri2_dpy
->authenticated
)
1155 /* reset authenticated */
1156 dri2_dpy
->authenticated
= true;
1162 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1164 struct dri2_egl_display
*dri2_dpy
= data
;
1167 dri2_dpy
->device_name
= strdup(device
);
1168 if (!dri2_dpy
->device_name
)
1171 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1172 if (dri2_dpy
->fd
== -1) {
1173 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1174 dri2_dpy
->device_name
, strerror(errno
));
1175 free(dri2_dpy
->device_name
);
1176 dri2_dpy
->device_name
= NULL
;
1180 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1181 dri2_dpy
->authenticated
= true;
1183 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1184 close(dri2_dpy
->fd
);
1186 free(dri2_dpy
->device_name
);
1187 dri2_dpy
->device_name
= NULL
;
1188 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1191 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1196 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1198 struct dri2_egl_display
*dri2_dpy
= data
;
1199 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1201 if (visual_idx
== -1)
1204 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1208 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1210 struct dri2_egl_display
*dri2_dpy
= data
;
1212 dri2_dpy
->capabilities
= value
;
1216 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1218 struct dri2_egl_display
*dri2_dpy
= data
;
1220 dri2_dpy
->authenticated
= true;
1223 static const struct wl_drm_listener drm_listener
= {
1224 .device
= drm_handle_device
,
1225 .format
= drm_handle_format
,
1226 .authenticated
= drm_handle_authenticated
,
1227 .capabilities
= drm_handle_capabilities
1231 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1234 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1238 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1239 uint32_t format
, uint32_t modifier_hi
,
1240 uint32_t modifier_lo
)
1242 struct dri2_egl_display
*dri2_dpy
= data
;
1243 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1246 if (visual_idx
== -1)
1249 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1250 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1253 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1255 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1256 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1259 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1260 .format
= dmabuf_ignore_format
,
1261 .modifier
= dmabuf_handle_modifier
,
1265 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1266 uint32_t name
, const char *interface
,
1269 struct dri2_egl_display
*dri2_dpy
= data
;
1271 if (strcmp(interface
, "wl_drm") == 0) {
1273 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1274 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1275 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1276 dri2_dpy
->wl_dmabuf
=
1277 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1279 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1285 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1290 static const struct wl_registry_listener registry_listener_drm
= {
1291 .global
= registry_handle_global_drm
,
1292 .global_remove
= registry_handle_global_remove
1296 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1298 /* We can't use values greater than 1 on Wayland because we are using the
1299 * frame callback to synchronise the frame and the only way we be sure to
1300 * get a frame callback is to attach a new buffer. Therefore we can't just
1301 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1303 dri2_setup_swap_interval(disp
, 1);
1306 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1307 .authenticate
= dri2_wl_authenticate
,
1308 .create_window_surface
= dri2_wl_create_window_surface
,
1309 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1310 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1311 .destroy_surface
= dri2_wl_destroy_surface
,
1312 .create_image
= dri2_create_image_khr
,
1313 .swap_buffers
= dri2_wl_swap_buffers
,
1314 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1315 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1316 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1317 .copy_buffers
= dri2_fallback_copy_buffers
,
1318 .query_buffer_age
= dri2_wl_query_buffer_age
,
1319 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1320 .get_sync_values
= dri2_fallback_get_sync_values
,
1321 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1324 static const __DRIextension
*dri2_loader_extensions
[] = {
1325 &dri2_loader_extension
.base
,
1326 &image_loader_extension
.base
,
1327 &image_lookup_extension
.base
,
1328 &use_invalidate
.base
,
1332 static const __DRIextension
*image_loader_extensions
[] = {
1333 &image_loader_extension
.base
,
1334 &image_lookup_extension
.base
,
1335 &use_invalidate
.base
,
1340 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1342 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1343 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1344 unsigned int count
= 0;
1347 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1350 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1351 struct dri2_egl_config
*dri2_conf
;
1353 if (!BITSET_TEST(dri2_dpy
->formats
, j
))
1356 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1357 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1359 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1366 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1367 struct dri2_egl_config
*dri2_conf
;
1368 int alt_dri_image_format
, c
, s
;
1370 /* No match for config. Try if we can blitImage convert to a visual */
1371 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1372 dri2_dpy
->driver_configs
[i
]);
1377 /* Find optimal target visual for blitImage conversion, if any. */
1378 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1379 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1381 if (s
== -1 || !BITSET_TEST(dri2_dpy
->formats
, s
))
1384 /* Visual s works for the Wayland server, and c can be converted into s
1385 * by our client gpu during PRIME blitImage conversion to a linear
1386 * wl_buffer, so add visual c as supported by the client renderer.
1388 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1389 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1390 dri2_wl_visuals
[c
].rgba_masks
);
1392 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1395 if (format_count
[c
] == 1)
1396 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1397 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1398 dri2_wl_visuals
[s
].format_name
);
1403 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1404 if (!format_count
[i
]) {
1405 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1406 dri2_wl_visuals
[i
].format_name
);
1410 return (count
!= 0);
1414 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1417 struct dri2_egl_display
*dri2_dpy
;
1419 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1421 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1424 disp
->DriverData
= (void *) dri2_dpy
;
1425 if (disp
->PlatformDisplay
== NULL
) {
1426 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1427 if (dri2_dpy
->wl_dpy
== NULL
)
1429 dri2_dpy
->own_device
= true;
1431 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1434 dri2_dpy
->wl_modifiers
=
1435 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1436 if (!dri2_dpy
->wl_modifiers
)
1438 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1439 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1443 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1445 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1446 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1449 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1450 dri2_dpy
->wl_queue
);
1452 if (dri2_dpy
->own_device
)
1453 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1455 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1456 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1457 ®istry_listener_drm
, dri2_dpy
);
1458 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1461 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1464 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1467 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1468 &dri2_dpy
->is_different_gpu
);
1469 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1471 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1477 if (dri2_dpy
->is_different_gpu
) {
1478 free(dri2_dpy
->device_name
);
1479 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1480 if (!dri2_dpy
->device_name
) {
1481 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1482 "for requested GPU");
1487 /* we have to do the check now, because loader_get_user_preferred_fd
1488 * will return a render-node when the requested gpu is different
1489 * to the server, but also if the client asks for the same gpu than
1490 * the server by requesting its pci-id */
1491 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1493 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1494 if (dri2_dpy
->driver_name
== NULL
) {
1495 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1499 /* render nodes cannot use Gem names, and thus do not support
1500 * the __DRI_DRI2_LOADER extension */
1501 if (!dri2_dpy
->is_render_node
) {
1502 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1503 if (!dri2_load_driver(disp
)) {
1504 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1508 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1509 if (!dri2_load_driver_dri3(disp
)) {
1510 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1515 if (!dri2_create_screen(disp
))
1518 if (!dri2_setup_extensions(disp
))
1521 dri2_setup_screen(disp
);
1523 dri2_wl_setup_swap_interval(disp
);
1525 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1526 * createImageFromFds support indicates that Prime export/import
1527 * is supported by the driver. Fall back to
1528 * gem names if we don't have Prime support. */
1530 if (dri2_dpy
->image
->base
.version
< 7 ||
1531 dri2_dpy
->image
->createImageFromFds
== NULL
)
1532 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1534 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1535 * The server needs to accept them */
1536 if (dri2_dpy
->is_render_node
&&
1537 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1538 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1542 if (dri2_dpy
->is_different_gpu
&&
1543 (dri2_dpy
->image
->base
.version
< 9 ||
1544 dri2_dpy
->image
->blitImage
== NULL
)) {
1545 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1546 "Image extension in the driver is not "
1547 "compatible. Version 9 or later and blitImage() "
1552 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1553 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1557 dri2_set_WL_bind_wayland_display(drv
, disp
);
1558 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1559 * because the buffer of the EGLImage has likely a tiling mode the server
1560 * gpu won't support. These is no way to check for now. Thus do not support the
1562 if (!dri2_dpy
->is_different_gpu
)
1563 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1565 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1567 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1569 /* Fill vtbl last to prevent accidentally calling virtual function during
1572 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1577 dri2_display_destroy(disp
);
1582 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1584 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1586 assume(visual_idx
!= -1);
1588 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1592 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1593 int format
, int w
, int h
,
1594 void **data
, int *size
,
1595 struct wl_buffer
**buffer
)
1597 struct dri2_egl_display
*dri2_dpy
=
1598 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1599 struct wl_shm_pool
*pool
;
1600 int fd
, stride
, size_map
;
1603 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1604 size_map
= h
* stride
;
1606 /* Create a shareable buffer */
1607 fd
= os_create_anonymous_file(size_map
, NULL
);
1611 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1612 if (data_map
== MAP_FAILED
) {
1617 /* Share it in a wl_buffer */
1618 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1619 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1620 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1621 wl_shm_pool_destroy(pool
);
1630 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1632 struct dri2_egl_display
*dri2_dpy
=
1633 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1635 /* we need to do the following operations only once per frame */
1636 if (dri2_surf
->back
)
1639 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1640 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1642 dri2_wl_release_buffers(dri2_surf
);
1644 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1645 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1646 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1647 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1648 dri2_surf
->current
= NULL
;
1651 /* find back buffer */
1653 /* There might be a buffer release already queued that wasn't processed */
1654 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1656 /* try get free buffer already created */
1657 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1658 if (!dri2_surf
->color_buffers
[i
].locked
&&
1659 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1660 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1665 /* else choose any another free location */
1666 if (!dri2_surf
->back
) {
1667 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1668 if (!dri2_surf
->color_buffers
[i
].locked
) {
1669 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1670 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1672 dri2_surf
->base
.Width
,
1673 dri2_surf
->base
.Height
,
1674 &dri2_surf
->back
->data
,
1675 &dri2_surf
->back
->data_size
,
1676 &dri2_surf
->back
->wl_buffer
)) {
1677 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1680 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1681 &wl_buffer_listener
, dri2_surf
);
1687 if (!dri2_surf
->back
) {
1688 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1692 dri2_surf
->back
->locked
= true;
1694 /* If we have an extra unlocked buffer at this point, we had to do triple
1695 * buffering for a while, but now can go back to just double buffering.
1696 * That means we can free any unlocked buffer now. */
1697 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1698 if (!dri2_surf
->color_buffers
[i
].locked
&&
1699 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1700 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1701 munmap(dri2_surf
->color_buffers
[i
].data
,
1702 dri2_surf
->color_buffers
[i
].data_size
);
1703 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1704 dri2_surf
->color_buffers
[i
].data
= NULL
;
1712 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1714 /* if there has been a resize: */
1715 if (!dri2_surf
->current
)
1718 return dri2_surf
->current
->data
;
1722 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1724 assert(dri2_surf
->back
);
1725 return dri2_surf
->back
->data
;
1729 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1731 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1733 while (dri2_surf
->throttle_callback
!= NULL
)
1734 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1735 dri2_surf
->wl_queue
) == -1)
1738 if (dri2_surf
->base
.SwapInterval
> 0) {
1739 dri2_surf
->throttle_callback
=
1740 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1741 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1742 &throttle_listener
, dri2_surf
);
1745 dri2_surf
->current
= dri2_surf
->back
;
1746 dri2_surf
->back
= NULL
;
1748 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1749 dri2_surf
->current
->wl_buffer
,
1750 dri2_surf
->dx
, dri2_surf
->dy
);
1752 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1753 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1754 /* reset resize growing parameters */
1758 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1759 0, 0, INT32_MAX
, INT32_MAX
);
1760 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1762 /* If we're not waiting for a frame callback then we'll at least throttle
1763 * to a sync callback so that we always give a chance for the compositor to
1764 * handle the commit and send a release event before checking for a free
1766 if (dri2_surf
->throttle_callback
== NULL
) {
1767 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1768 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1769 &throttle_listener
, dri2_surf
);
1772 wl_display_flush(dri2_dpy
->wl_dpy
);
1776 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1777 int *x
, int *y
, int *w
, int *h
,
1778 void *loaderPrivate
)
1780 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1782 (void) swrast_update_buffers(dri2_surf
);
1785 *w
= dri2_surf
->base
.Width
;
1786 *h
= dri2_surf
->base
.Height
;
1790 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1791 int x
, int y
, int w
, int h
,
1792 char *data
, void *loaderPrivate
)
1794 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1795 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1796 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1797 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1798 int dst_stride
= copy_width
;
1801 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1803 memset(data
, 0, copy_width
* h
);
1807 assert(data
!= src
);
1808 assert(copy_width
<= src_stride
);
1811 src
+= y
* src_stride
;
1814 if (copy_width
> src_stride
-x_offset
)
1815 copy_width
= src_stride
-x_offset
;
1816 if (h
> dri2_surf
->base
.Height
-y
)
1817 h
= dri2_surf
->base
.Height
-y
;
1820 memcpy(dst
, src
, copy_width
);
1827 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1828 int x
, int y
, int w
, int h
, int stride
,
1829 char *data
, void *loaderPrivate
)
1831 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1832 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1833 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1834 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1837 assert(copy_width
<= stride
);
1839 (void) swrast_update_buffers(dri2_surf
);
1840 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1842 /* partial copy, copy old content */
1843 if (copy_width
< dst_stride
)
1844 dri2_wl_swrast_get_image(draw
, 0, 0,
1845 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1846 dst
, loaderPrivate
);
1849 dst
+= y
* dst_stride
;
1853 /* drivers expect we do these checks (and some rely on it) */
1854 if (copy_width
> dst_stride
-x_offset
)
1855 copy_width
= dst_stride
-x_offset
;
1856 if (h
> dri2_surf
->base
.Height
-y
)
1857 h
= dri2_surf
->base
.Height
-y
;
1860 memcpy(dst
, src
, copy_width
);
1864 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1868 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1869 int x
, int y
, int w
, int h
,
1870 char *data
, void *loaderPrivate
)
1872 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1875 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1876 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1877 stride
, data
, loaderPrivate
);
1881 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1883 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1884 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1886 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1891 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1893 struct dri2_egl_display
*dri2_dpy
= data
;
1894 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1896 if (visual_idx
== -1)
1899 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1902 static const struct wl_shm_listener shm_listener
= {
1903 .format
= shm_handle_format
1907 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1908 uint32_t name
, const char *interface
,
1911 struct dri2_egl_display
*dri2_dpy
= data
;
1913 if (strcmp(interface
, "wl_shm") == 0) {
1915 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1916 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1920 static const struct wl_registry_listener registry_listener_swrast
= {
1921 .global
= registry_handle_global_swrast
,
1922 .global_remove
= registry_handle_global_remove
1925 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1926 .authenticate
= NULL
,
1927 .create_window_surface
= dri2_wl_create_window_surface
,
1928 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1929 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1930 .destroy_surface
= dri2_wl_destroy_surface
,
1931 .create_image
= dri2_create_image_khr
,
1932 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1933 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1934 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1935 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1936 .copy_buffers
= dri2_fallback_copy_buffers
,
1937 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1938 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1939 .get_sync_values
= dri2_fallback_get_sync_values
,
1940 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1943 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1944 .base
= { __DRI_SWRAST_LOADER
, 2 },
1946 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1947 .putImage
= dri2_wl_swrast_put_image
,
1948 .getImage
= dri2_wl_swrast_get_image
,
1949 .putImage2
= dri2_wl_swrast_put_image2
,
1952 static const __DRIextension
*swrast_loader_extensions
[] = {
1953 &swrast_loader_extension
.base
,
1954 &image_lookup_extension
.base
,
1959 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1962 struct dri2_egl_display
*dri2_dpy
;
1964 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1966 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1969 disp
->DriverData
= (void *) dri2_dpy
;
1970 if (disp
->PlatformDisplay
== NULL
) {
1971 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1972 if (dri2_dpy
->wl_dpy
== NULL
)
1974 dri2_dpy
->own_device
= true;
1976 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1979 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
1981 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1987 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1989 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1990 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1993 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1994 dri2_dpy
->wl_queue
);
1996 if (dri2_dpy
->own_device
)
1997 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1999 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2000 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2001 ®istry_listener_swrast
, dri2_dpy
);
2003 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2006 if (roundtrip(dri2_dpy
) < 0 || !BITSET_TEST_RANGE(dri2_dpy
->formats
,
2007 0, EGL_DRI2_MAX_FORMATS
))
2010 dri2_dpy
->driver_name
= strdup("swrast");
2011 if (!dri2_load_driver_swrast(disp
))
2014 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2016 if (!dri2_create_screen(disp
))
2019 if (!dri2_setup_extensions(disp
))
2022 dri2_setup_screen(disp
);
2024 dri2_wl_setup_swap_interval(disp
);
2026 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2027 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2031 /* Fill vtbl last to prevent accidentally calling virtual function during
2034 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2039 dri2_display_destroy(disp
);
2044 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2046 EGLBoolean initialized
= EGL_FALSE
;
2048 if (!disp
->Options
.ForceSoftware
)
2049 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2052 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2059 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2061 if (dri2_dpy
->wl_drm
)
2062 wl_drm_destroy(dri2_dpy
->wl_drm
);
2063 if (dri2_dpy
->wl_dmabuf
)
2064 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2065 if (dri2_dpy
->wl_shm
)
2066 wl_shm_destroy(dri2_dpy
->wl_shm
);
2067 if (dri2_dpy
->wl_registry
)
2068 wl_registry_destroy(dri2_dpy
->wl_registry
);
2069 if (dri2_dpy
->wl_queue
)
2070 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2071 if (dri2_dpy
->wl_dpy_wrapper
)
2072 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2074 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2075 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2076 free(dri2_dpy
->wl_modifiers
);
2078 if (dri2_dpy
->own_device
)
2079 wl_display_disconnect(dri2_dpy
->wl_dpy
);