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 "eglglobals.h"
48 #include <wayland-egl-backend.h>
49 #include <wayland-client.h>
50 #include "wayland-drm-client-protocol.h"
51 #include "linux-dmabuf-unstable-v1-client-protocol.h"
54 * The index of entries in this table is used as a bitmask in
55 * dri2_dpy->formats, which tracks the formats supported by our server.
57 static const struct dri2_wl_visual
{
58 const char *format_name
;
59 uint32_t wl_drm_format
;
60 uint32_t wl_shm_format
;
62 /* alt_dri_image_format is a substitute wl_buffer format to use for a
63 * wl-server unsupported dri_image_format, ie. some other dri_image_format in
64 * the table, of the same precision but with different channel ordering, or
65 * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported.
66 * The code checks if alt_dri_image_format can be used as a fallback for a
67 * dri_image_format for a given wl-server implementation.
69 int alt_dri_image_format
;
71 unsigned int rgba_masks
[4];
72 } dri2_wl_visuals
[] = {
75 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
76 __DRI_IMAGE_FORMAT_XRGB2101010
, __DRI_IMAGE_FORMAT_XBGR2101010
, 32,
77 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0x00000000 }
81 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
82 __DRI_IMAGE_FORMAT_ARGB2101010
, __DRI_IMAGE_FORMAT_ABGR2101010
, 32,
83 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 }
87 WL_DRM_FORMAT_XBGR2101010
, WL_SHM_FORMAT_XBGR2101010
,
88 __DRI_IMAGE_FORMAT_XBGR2101010
, __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
89 { 0x000003ff, 0x000ffc00, 0x3ff00000, 0x00000000 }
93 WL_DRM_FORMAT_ABGR2101010
, WL_SHM_FORMAT_ABGR2101010
,
94 __DRI_IMAGE_FORMAT_ABGR2101010
, __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
95 { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }
99 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
100 __DRI_IMAGE_FORMAT_XRGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
101 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }
105 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
106 __DRI_IMAGE_FORMAT_ARGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
107 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }
111 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
112 __DRI_IMAGE_FORMAT_RGB565
, __DRI_IMAGE_FORMAT_NONE
, 16,
113 { 0xf800, 0x07e0, 0x001f, 0x0000 }
118 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
119 const __DRIconfig
*config
)
121 unsigned int red
, green
, blue
, alpha
;
123 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &red
);
124 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &green
);
125 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &blue
);
126 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &alpha
);
128 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
129 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
131 if (red
== wl_visual
->rgba_masks
[0] &&
132 green
== wl_visual
->rgba_masks
[1] &&
133 blue
== wl_visual
->rgba_masks
[2] &&
134 alpha
== wl_visual
->rgba_masks
[3]) {
143 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
145 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
146 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
148 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
156 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
158 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
159 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
167 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
169 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
170 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
178 dri2_wl_is_format_supported(void* user_data
, uint32_t format
)
180 _EGLDisplay
*disp
= (_EGLDisplay
*) user_data
;
181 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
182 int j
= dri2_wl_visual_idx_from_fourcc(format
);
187 for (int i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
188 if (j
== dri2_wl_visual_idx_from_config(dri2_dpy
,
189 dri2_dpy
->driver_configs
[i
]))
196 roundtrip(struct dri2_egl_display
*dri2_dpy
)
198 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
202 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
204 struct dri2_egl_surface
*dri2_surf
= data
;
207 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
208 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
211 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
213 if (dri2_surf
->color_buffers
[i
].wl_release
) {
214 wl_buffer_destroy(buffer
);
215 dri2_surf
->color_buffers
[i
].wl_release
= false;
216 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
219 dri2_surf
->color_buffers
[i
].locked
= false;
222 static const struct wl_buffer_listener wl_buffer_listener
= {
223 .release
= wl_buffer_release
227 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
229 struct dri2_egl_surface
*dri2_surf
= data
;
230 struct dri2_egl_display
*dri2_dpy
=
231 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
233 /* Update the surface size as soon as native window is resized; from user
234 * pov, this makes the effect that resize is done immediately after native
235 * window resize, without requiring to wait until the first draw.
237 * A more detailed and lengthy explanation can be found at
238 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
240 if (!dri2_surf
->back
) {
241 dri2_surf
->base
.Width
= wl_win
->width
;
242 dri2_surf
->base
.Height
= wl_win
->height
;
244 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
248 destroy_window_callback(void *data
)
250 struct dri2_egl_surface
*dri2_surf
= data
;
251 dri2_surf
->wl_win
= NULL
;
254 static struct wl_surface
*
255 get_wl_surface_proxy(struct wl_egl_window
*window
)
257 /* Version 3 of wl_egl_window introduced a version field at the same
258 * location where a pointer to wl_surface was stored. Thus, if
259 * window->version is dereferenceable, we've been given an older version of
260 * wl_egl_window, and window->version points to wl_surface */
261 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
262 return wl_proxy_create_wrapper((void *)(window
->version
));
264 return wl_proxy_create_wrapper(window
->surface
);
268 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
271 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
272 _EGLConfig
*conf
, void *native_window
,
273 const EGLint
*attrib_list
)
275 __DRIcreateNewDrawableFunc createNewDrawable
;
276 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
277 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
278 struct wl_egl_window
*window
= native_window
;
279 struct dri2_egl_surface
*dri2_surf
;
281 const __DRIconfig
*config
;
283 dri2_surf
= calloc(1, sizeof *dri2_surf
);
285 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
289 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
290 attrib_list
, false, native_window
))
293 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
294 dri2_surf
->base
.GLColorspace
);
297 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
301 dri2_surf
->base
.Width
= window
->width
;
302 dri2_surf
->base
.Height
= window
->height
;
304 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
305 assert(visual_idx
!= -1);
307 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
308 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
310 assert(dri2_dpy
->wl_shm
);
311 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
314 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
315 if (!dri2_surf
->wl_queue
) {
316 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
320 if (dri2_dpy
->wl_drm
) {
321 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
322 if (!dri2_surf
->wl_drm_wrapper
) {
323 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
326 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
327 dri2_surf
->wl_queue
);
330 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
331 if (!dri2_surf
->wl_dpy_wrapper
) {
332 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
335 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
336 dri2_surf
->wl_queue
);
338 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
339 if (!dri2_surf
->wl_surface_wrapper
) {
340 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
341 goto cleanup_dpy_wrapper
;
343 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
344 dri2_surf
->wl_queue
);
346 dri2_surf
->wl_win
= window
;
347 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
348 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
350 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
352 if (dri2_dpy
->image_driver
)
353 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
354 else if (dri2_dpy
->dri2
)
355 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
357 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
359 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
361 if (dri2_surf
->dri_drawable
== NULL
) {
362 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
363 goto cleanup_surf_wrapper
;
366 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
368 return &dri2_surf
->base
;
370 cleanup_surf_wrapper
:
371 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
373 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
375 if (dri2_surf
->wl_drm_wrapper
)
376 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
378 wl_event_queue_destroy(dri2_surf
->wl_queue
);
386 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
387 _EGLConfig
*conf
, void *native_window
,
388 const EGLint
*attrib_list
)
390 /* From the EGL_EXT_platform_wayland spec, version 3:
392 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
393 * that belongs to Wayland. Any such call fails and generates
396 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
402 * Called via eglDestroySurface(), drv->API.DestroySurface().
405 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
407 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
408 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
412 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
414 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
415 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
416 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
417 if (dri2_surf
->color_buffers
[i
].dri_image
)
418 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
419 if (dri2_surf
->color_buffers
[i
].linear_copy
)
420 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
421 if (dri2_surf
->color_buffers
[i
].data
)
422 munmap(dri2_surf
->color_buffers
[i
].data
,
423 dri2_surf
->color_buffers
[i
].data_size
);
427 dri2_egl_surface_free_local_buffers(dri2_surf
);
429 if (dri2_surf
->throttle_callback
)
430 wl_callback_destroy(dri2_surf
->throttle_callback
);
432 if (dri2_surf
->wl_win
) {
433 dri2_surf
->wl_win
->driver_private
= NULL
;
434 dri2_surf
->wl_win
->resize_callback
= NULL
;
435 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
438 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
439 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
440 if (dri2_surf
->wl_drm_wrapper
)
441 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
442 wl_event_queue_destroy(dri2_surf
->wl_queue
);
444 dri2_fini_surface(surf
);
451 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
453 struct dri2_egl_display
*dri2_dpy
=
454 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
456 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
457 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
458 if (dri2_surf
->color_buffers
[i
].locked
) {
459 dri2_surf
->color_buffers
[i
].wl_release
= true;
461 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
462 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
465 if (dri2_surf
->color_buffers
[i
].dri_image
)
466 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
467 if (dri2_surf
->color_buffers
[i
].linear_copy
)
468 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
469 if (dri2_surf
->color_buffers
[i
].data
)
470 munmap(dri2_surf
->color_buffers
[i
].data
,
471 dri2_surf
->color_buffers
[i
].data_size
);
473 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
474 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
475 dri2_surf
->color_buffers
[i
].data
= NULL
;
479 dri2_egl_surface_free_local_buffers(dri2_surf
);
483 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
485 struct dri2_egl_display
*dri2_dpy
=
486 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
489 unsigned int dri_image_format
;
490 unsigned int linear_dri_image_format
;
494 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
495 assert(visual_idx
!= -1);
496 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
497 linear_dri_image_format
= dri_image_format
;
498 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
499 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
501 /* Substitute dri image format if server does not support original format */
502 if (!(dri2_dpy
->formats
& (1 << visual_idx
)))
503 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
505 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
506 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
509 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
510 assert(dri2_dpy
->formats
&
511 (1 << dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
513 /* There might be a buffer release already queued that wasn't processed */
514 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
516 while (dri2_surf
->back
== NULL
) {
517 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
518 /* Get an unlocked buffer, preferably one with a dri_buffer
519 * already allocated. */
520 if (dri2_surf
->color_buffers
[i
].locked
)
522 if (dri2_surf
->back
== NULL
)
523 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
524 else if (dri2_surf
->back
->dri_image
== NULL
)
525 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
531 /* If we don't have a buffer, then block on the server to release one for
532 * us, and try again. wl_display_dispatch_queue will process any pending
533 * events, however not all servers flush on issuing a buffer release
534 * event. So, we spam the server with roundtrips as they always cause a
537 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
538 dri2_surf
->wl_queue
) < 0)
542 if (dri2_surf
->back
== NULL
)
545 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
547 if (dri2_dpy
->is_different_gpu
&&
548 dri2_surf
->back
->linear_copy
== NULL
) {
549 /* The LINEAR modifier should be a perfect alias of the LINEAR use
550 * flag; try the new interface first before the old, then fall back. */
551 if (dri2_dpy
->image
->base
.version
>= 15 &&
552 dri2_dpy
->image
->createImageWithModifiers
) {
553 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
555 dri2_surf
->back
->linear_copy
=
556 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
557 dri2_surf
->base
.Width
,
558 dri2_surf
->base
.Height
,
559 linear_dri_image_format
,
564 dri2_surf
->back
->linear_copy
=
565 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
566 dri2_surf
->base
.Width
,
567 dri2_surf
->base
.Height
,
568 linear_dri_image_format
,
570 __DRI_IMAGE_USE_LINEAR
,
573 if (dri2_surf
->back
->linear_copy
== NULL
)
577 if (dri2_surf
->back
->dri_image
== NULL
) {
578 /* If our DRIImage implementation does not support
579 * createImageWithModifiers, then fall back to the old createImage,
580 * and hope it allocates an image which is acceptable to the winsys.
582 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
583 dri2_dpy
->image
->createImageWithModifiers
) {
584 dri2_surf
->back
->dri_image
=
585 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
586 dri2_surf
->base
.Width
,
587 dri2_surf
->base
.Height
,
593 dri2_surf
->back
->dri_image
=
594 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
595 dri2_surf
->base
.Width
,
596 dri2_surf
->base
.Height
,
598 dri2_dpy
->is_different_gpu
?
603 dri2_surf
->back
->age
= 0;
605 if (dri2_surf
->back
->dri_image
== NULL
)
608 dri2_surf
->back
->locked
= true;
615 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
617 struct dri2_egl_display
*dri2_dpy
=
618 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
622 image
= dri2_surf
->back
->dri_image
;
624 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
625 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
627 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
629 buffer
->pitch
= pitch
;
635 update_buffers(struct dri2_egl_surface
*dri2_surf
)
637 struct dri2_egl_display
*dri2_dpy
=
638 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
640 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
641 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
643 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
644 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
645 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
646 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
649 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
650 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
651 dri2_wl_release_buffers(dri2_surf
);
654 if (get_back_bo(dri2_surf
) < 0) {
655 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
659 /* If we have an extra unlocked buffer at this point, we had to do triple
660 * buffering for a while, but now can go back to just double buffering.
661 * That means we can free any unlocked buffer now. */
662 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
663 if (!dri2_surf
->color_buffers
[i
].locked
&&
664 dri2_surf
->color_buffers
[i
].wl_buffer
) {
665 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
666 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
667 if (dri2_dpy
->is_different_gpu
)
668 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
669 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
670 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
671 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
679 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
680 int *width
, int *height
,
681 unsigned int *attachments
, int count
,
682 int *out_count
, void *loaderPrivate
)
684 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
687 if (update_buffers(dri2_surf
) < 0)
690 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
693 switch (attachments
[i
]) {
694 case __DRI_BUFFER_BACK_LEFT
:
695 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
698 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
702 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
705 dri2_surf
->buffers
[j
] = *local
;
714 *width
= dri2_surf
->base
.Width
;
715 *height
= dri2_surf
->base
.Height
;
717 return dri2_surf
->buffers
;
721 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
722 int *width
, int *height
,
723 unsigned int *attachments
, int count
,
724 int *out_count
, void *loaderPrivate
)
726 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
727 unsigned int *attachments_with_format
;
729 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
731 if (visual_idx
== -1)
734 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
735 if (!attachments_with_format
) {
740 for (int i
= 0; i
< count
; ++i
) {
741 attachments_with_format
[2*i
] = attachments
[i
];
742 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
746 dri2_wl_get_buffers_with_format(driDrawable
,
748 attachments_with_format
, count
,
749 out_count
, loaderPrivate
);
751 free(attachments_with_format
);
757 image_get_buffers(__DRIdrawable
*driDrawable
,
761 uint32_t buffer_mask
,
762 struct __DRIimageList
*buffers
)
764 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
766 if (update_buffers(dri2_surf
) < 0)
769 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
770 buffers
->back
= dri2_surf
->back
->dri_image
;
776 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
779 (void) loaderPrivate
;
782 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
783 .base
= { __DRI_DRI2_LOADER
, 3 },
785 .getBuffers
= dri2_wl_get_buffers
,
786 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
787 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
790 static const __DRIimageLoaderExtension image_loader_extension
= {
791 .base
= { __DRI_IMAGE_LOADER
, 1 },
793 .getBuffers
= image_get_buffers
,
794 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
798 wayland_throttle_callback(void *data
,
799 struct wl_callback
*callback
,
802 struct dri2_egl_surface
*dri2_surf
= data
;
804 dri2_surf
->throttle_callback
= NULL
;
805 wl_callback_destroy(callback
);
808 static const struct wl_callback_listener throttle_listener
= {
809 .done
= wayland_throttle_callback
813 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
814 __DRIimage
*image
, int *fourcc
)
820 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
825 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
830 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
831 if (visual_idx
== -1)
834 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
838 static struct wl_buffer
*
839 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
840 struct dri2_egl_surface
*dri2_surf
,
843 struct wl_buffer
*ret
;
845 int width
, height
, fourcc
, num_planes
;
846 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
848 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
849 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
851 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
855 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
860 if (dri2_dpy
->image
->base
.version
>= 15) {
863 query
= dri2_dpy
->image
->queryImage(image
,
864 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
866 query
&= dri2_dpy
->image
->queryImage(image
,
867 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
870 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
874 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
875 struct zwp_linux_buffer_params_v1
*params
;
878 /* We don't need a wrapper for wl_dmabuf objects, because we have to
879 * create the intermediate params object; we can set the queue on this,
880 * and the wl_buffer inherits it race-free. */
881 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
883 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
885 for (i
= 0; i
< num_planes
; i
++) {
890 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
896 query
= dri2_dpy
->image
->queryImage(p_image
,
897 __DRI_IMAGE_ATTRIB_FD
,
899 query
&= dri2_dpy
->image
->queryImage(p_image
,
900 __DRI_IMAGE_ATTRIB_STRIDE
,
902 query
&= dri2_dpy
->image
->queryImage(p_image
,
903 __DRI_IMAGE_ATTRIB_OFFSET
,
905 if (image
!= p_image
)
906 dri2_dpy
->image
->destroyImage(p_image
);
911 zwp_linux_buffer_params_v1_destroy(params
);
915 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
916 modifier
>> 32, modifier
& 0xffffffff);
920 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
922 zwp_linux_buffer_params_v1_destroy(params
);
923 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
924 struct wl_drm
*wl_drm
=
925 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
931 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
932 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
933 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
937 struct wl_drm
*wl_drm
=
938 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
944 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
945 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
946 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
953 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
957 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
958 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
961 for (int i
= 0; i
< n_rects
; i
++) {
962 const int *rect
= &rects
[i
* 4];
964 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
966 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
972 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
975 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
981 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
982 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
984 while (dri2_surf
->throttle_callback
!= NULL
)
985 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
986 dri2_surf
->wl_queue
) == -1)
989 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
990 if (dri2_surf
->color_buffers
[i
].age
> 0)
991 dri2_surf
->color_buffers
[i
].age
++;
993 /* Make sure we have a back buffer in case we're swapping without ever
995 if (get_back_bo(dri2_surf
) < 0)
996 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
998 if (draw
->SwapInterval
> 0) {
999 dri2_surf
->throttle_callback
=
1000 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1001 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1002 &throttle_listener
, dri2_surf
);
1005 dri2_surf
->back
->age
= 1;
1006 dri2_surf
->current
= dri2_surf
->back
;
1007 dri2_surf
->back
= NULL
;
1009 if (!dri2_surf
->current
->wl_buffer
) {
1012 if (dri2_dpy
->is_different_gpu
)
1013 image
= dri2_surf
->current
->linear_copy
;
1015 image
= dri2_surf
->current
->dri_image
;
1017 dri2_surf
->current
->wl_buffer
=
1018 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1020 dri2_surf
->current
->wl_release
= false;
1022 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1023 &wl_buffer_listener
, dri2_surf
);
1026 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1027 dri2_surf
->current
->wl_buffer
,
1028 dri2_surf
->dx
, dri2_surf
->dy
);
1030 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1031 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1032 /* reset resize growing parameters */
1036 /* If the compositor doesn't support damage_buffer, we deliberately
1037 * ignore the damage region and post maximum damage, due to
1038 * https://bugs.freedesktop.org/78190 */
1039 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1040 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1041 0, 0, INT32_MAX
, INT32_MAX
);
1043 if (dri2_dpy
->is_different_gpu
) {
1044 _EGLContext
*ctx
= _eglGetCurrentContext();
1045 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1046 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1047 dri2_surf
->current
->linear_copy
,
1048 dri2_surf
->current
->dri_image
,
1049 0, 0, dri2_surf
->base
.Width
,
1050 dri2_surf
->base
.Height
,
1051 0, 0, dri2_surf
->base
.Width
,
1052 dri2_surf
->base
.Height
, 0);
1055 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1056 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1058 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1060 /* If we're not waiting for a frame callback then we'll at least throttle
1061 * to a sync callback so that we always give a chance for the compositor to
1062 * handle the commit and send a release event before checking for a free
1064 if (dri2_surf
->throttle_callback
== NULL
) {
1065 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1066 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1067 &throttle_listener
, dri2_surf
);
1070 wl_display_flush(dri2_dpy
->wl_dpy
);
1076 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1077 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1079 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1081 if (get_back_bo(dri2_surf
) < 0) {
1082 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1086 return dri2_surf
->back
->age
;
1090 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1092 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1095 static struct wl_buffer
*
1096 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1100 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1101 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1102 __DRIimage
*image
= dri2_img
->dri_image
;
1103 struct wl_buffer
*buffer
;
1104 int format
, visual_idx
;
1106 /* Check the upstream display supports this buffer's format. */
1107 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1108 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1109 if (visual_idx
== -1)
1112 if (!(dri2_dpy
->formats
& (1u << visual_idx
)))
1115 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1117 /* The buffer object will have been created with our internal event queue
1118 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1119 * buffer to be used by the application so we'll reset it to the display's
1120 * default event queue. This isn't actually racy, as the only event the
1121 * buffer can get is a buffer release, which doesn't happen with an explicit
1124 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1129 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1134 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1136 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1139 if (dri2_dpy
->is_render_node
) {
1140 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1141 "authenticate for render-nodes");
1144 dri2_dpy
->authenticated
= false;
1146 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1147 if (roundtrip(dri2_dpy
) < 0)
1150 if (!dri2_dpy
->authenticated
)
1153 /* reset authenticated */
1154 dri2_dpy
->authenticated
= true;
1160 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1162 struct dri2_egl_display
*dri2_dpy
= data
;
1165 dri2_dpy
->device_name
= strdup(device
);
1166 if (!dri2_dpy
->device_name
)
1169 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1170 if (dri2_dpy
->fd
== -1) {
1171 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1172 dri2_dpy
->device_name
, strerror(errno
));
1173 free(dri2_dpy
->device_name
);
1174 dri2_dpy
->device_name
= NULL
;
1178 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1179 dri2_dpy
->authenticated
= true;
1181 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1182 close(dri2_dpy
->fd
);
1184 free(dri2_dpy
->device_name
);
1185 dri2_dpy
->device_name
= NULL
;
1186 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1189 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1194 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1196 struct dri2_egl_display
*dri2_dpy
= data
;
1197 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1199 if (visual_idx
== -1)
1202 dri2_dpy
->formats
|= (1u << visual_idx
);
1206 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1208 struct dri2_egl_display
*dri2_dpy
= data
;
1210 dri2_dpy
->capabilities
= value
;
1214 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1216 struct dri2_egl_display
*dri2_dpy
= data
;
1218 dri2_dpy
->authenticated
= true;
1221 static const struct wl_drm_listener drm_listener
= {
1222 .device
= drm_handle_device
,
1223 .format
= drm_handle_format
,
1224 .authenticated
= drm_handle_authenticated
,
1225 .capabilities
= drm_handle_capabilities
1229 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1232 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1236 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1237 uint32_t format
, uint32_t modifier_hi
,
1238 uint32_t modifier_lo
)
1240 struct dri2_egl_display
*dri2_dpy
= data
;
1241 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1244 if (visual_idx
== -1)
1247 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1248 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1251 dri2_dpy
->formats
|= (1u << visual_idx
);
1253 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1254 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1257 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1258 .format
= dmabuf_ignore_format
,
1259 .modifier
= dmabuf_handle_modifier
,
1263 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1264 uint32_t name
, const char *interface
,
1267 struct dri2_egl_display
*dri2_dpy
= data
;
1269 if (strcmp(interface
, "wl_drm") == 0) {
1271 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1272 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1273 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1274 dri2_dpy
->wl_dmabuf
=
1275 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1277 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1283 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1288 static const struct wl_registry_listener registry_listener_drm
= {
1289 .global
= registry_handle_global_drm
,
1290 .global_remove
= registry_handle_global_remove
1294 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1296 /* We can't use values greater than 1 on Wayland because we are using the
1297 * frame callback to synchronise the frame and the only way we be sure to
1298 * get a frame callback is to attach a new buffer. Therefore we can't just
1299 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1301 dri2_setup_swap_interval(disp
, 1);
1304 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1305 .authenticate
= dri2_wl_authenticate
,
1306 .create_window_surface
= dri2_wl_create_window_surface
,
1307 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1308 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1309 .destroy_surface
= dri2_wl_destroy_surface
,
1310 .create_image
= dri2_create_image_khr
,
1311 .swap_buffers
= dri2_wl_swap_buffers
,
1312 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1313 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1314 .set_damage_region
= dri2_fallback_set_damage_region
,
1315 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1316 .copy_buffers
= dri2_fallback_copy_buffers
,
1317 .query_buffer_age
= dri2_wl_query_buffer_age
,
1318 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1319 .get_sync_values
= dri2_fallback_get_sync_values
,
1320 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1323 static const __DRIextension
*dri2_loader_extensions
[] = {
1324 &dri2_loader_extension
.base
,
1325 &image_loader_extension
.base
,
1326 &image_lookup_extension
.base
,
1327 &use_invalidate
.base
,
1331 static const __DRIextension
*image_loader_extensions
[] = {
1332 &image_loader_extension
.base
,
1333 &image_lookup_extension
.base
,
1334 &use_invalidate
.base
,
1339 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1341 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1342 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1343 unsigned int count
= 0;
1346 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1349 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1350 struct dri2_egl_config
*dri2_conf
;
1352 if (!(dri2_dpy
->formats
& (1u << j
)))
1355 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1356 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1358 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1365 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1366 struct dri2_egl_config
*dri2_conf
;
1367 int alt_dri_image_format
, c
, s
;
1369 /* No match for config. Try if we can blitImage convert to a visual */
1370 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1371 dri2_dpy
->driver_configs
[i
]);
1376 /* Find optimal target visual for blitImage conversion, if any. */
1377 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1378 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1380 if (s
== -1 || !(dri2_dpy
->formats
& (1 << s
)))
1383 /* Visual s works for the Wayland server, and c can be converted into s
1384 * by our client gpu during PRIME blitImage conversion to a linear
1385 * wl_buffer, so add visual c as supported by the client renderer.
1387 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1388 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1389 dri2_wl_visuals
[c
].rgba_masks
);
1391 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1394 if (format_count
[c
] == 1)
1395 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1396 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1397 dri2_wl_visuals
[s
].format_name
);
1402 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1403 if (!format_count
[i
]) {
1404 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1405 dri2_wl_visuals
[i
].format_name
);
1409 return (count
!= 0);
1413 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1416 struct dri2_egl_display
*dri2_dpy
;
1418 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1420 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1423 disp
->DriverData
= (void *) dri2_dpy
;
1424 if (disp
->PlatformDisplay
== NULL
) {
1425 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1426 if (dri2_dpy
->wl_dpy
== NULL
)
1428 dri2_dpy
->own_device
= true;
1430 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1433 dri2_dpy
->wl_modifiers
=
1434 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1435 if (!dri2_dpy
->wl_modifiers
)
1437 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1438 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1442 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1444 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1445 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1448 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1449 dri2_dpy
->wl_queue
);
1451 if (dri2_dpy
->own_device
)
1452 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1454 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1455 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1456 ®istry_listener_drm
, dri2_dpy
);
1457 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1460 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1463 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1466 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1467 &dri2_dpy
->is_different_gpu
);
1468 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1470 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1476 if (dri2_dpy
->is_different_gpu
) {
1477 free(dri2_dpy
->device_name
);
1478 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1479 if (!dri2_dpy
->device_name
) {
1480 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1481 "for requested GPU");
1486 /* we have to do the check now, because loader_get_user_preferred_fd
1487 * will return a render-node when the requested gpu is different
1488 * to the server, but also if the client asks for the same gpu than
1489 * the server by requesting its pci-id */
1490 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1492 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1493 if (dri2_dpy
->driver_name
== NULL
) {
1494 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1498 /* render nodes cannot use Gem names, and thus do not support
1499 * the __DRI_DRI2_LOADER extension */
1500 if (!dri2_dpy
->is_render_node
) {
1501 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1502 if (!dri2_load_driver(disp
)) {
1503 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1507 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1508 if (!dri2_load_driver_dri3(disp
)) {
1509 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1514 if (!dri2_create_screen(disp
))
1517 if (!dri2_setup_extensions(disp
))
1520 dri2_setup_screen(disp
);
1522 dri2_wl_setup_swap_interval(disp
);
1524 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1525 * createImageFromFds support indicates that Prime export/import
1526 * is supported by the driver. Fall back to
1527 * gem names if we don't have Prime support. */
1529 if (dri2_dpy
->image
->base
.version
< 7 ||
1530 dri2_dpy
->image
->createImageFromFds
== NULL
)
1531 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1533 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1534 * The server needs to accept them */
1535 if (dri2_dpy
->is_render_node
&&
1536 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1537 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1541 if (dri2_dpy
->is_different_gpu
&&
1542 (dri2_dpy
->image
->base
.version
< 9 ||
1543 dri2_dpy
->image
->blitImage
== NULL
)) {
1544 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1545 "Image extension in the driver is not "
1546 "compatible. Version 9 or later and blitImage() "
1551 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1552 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1556 dri2_set_WL_bind_wayland_display(drv
, disp
);
1557 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1558 * because the buffer of the EGLImage has likely a tiling mode the server
1559 * gpu won't support. These is no way to check for now. Thus do not support the
1561 if (!dri2_dpy
->is_different_gpu
)
1562 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1564 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1566 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1568 /* Fill vtbl last to prevent accidentally calling virtual function during
1571 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1576 dri2_display_destroy(disp
);
1581 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1583 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1585 assume(visual_idx
!= -1);
1587 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1591 * Taken from weston shared/os-compatibility.c
1594 #ifndef HAVE_MKOSTEMP
1597 set_cloexec_or_close(int fd
)
1604 flags
= fcntl(fd
, F_GETFD
);
1608 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1621 * Taken from weston shared/os-compatibility.c
1625 create_tmpfile_cloexec(char *tmpname
)
1629 #ifdef HAVE_MKOSTEMP
1630 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1634 fd
= mkstemp(tmpname
);
1636 fd
= set_cloexec_or_close(fd
);
1645 * Taken from weston shared/os-compatibility.c
1647 * Create a new, unique, anonymous file of the given size, and
1648 * return the file descriptor for it. The file descriptor is set
1649 * CLOEXEC. The file is immediately suitable for mmap()'ing
1650 * the given size at offset zero.
1652 * The file should not have a permanent backing store like a disk,
1653 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1655 * The file name is deleted from the file system.
1657 * The file is suitable for buffer sharing between processes by
1658 * transmitting the file descriptor over Unix sockets using the
1659 * SCM_RIGHTS methods.
1661 * If the C library implements posix_fallocate(), it is used to
1662 * guarantee that disk space is available for the file at the
1663 * given size. If disk space is insufficient, errno is set to ENOSPC.
1664 * If posix_fallocate() is not supported, program may receive
1665 * SIGBUS on accessing mmap()'ed file contents instead.
1668 os_create_anonymous_file(off_t size
)
1670 static const char templ
[] = "/mesa-shared-XXXXXX";
1676 path
= getenv("XDG_RUNTIME_DIR");
1682 name
= malloc(strlen(path
) + sizeof(templ
));
1687 strcat(name
, templ
);
1689 fd
= create_tmpfile_cloexec(name
);
1696 ret
= ftruncate(fd
, size
);
1707 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1708 int format
, int w
, int h
,
1709 void **data
, int *size
,
1710 struct wl_buffer
**buffer
)
1712 struct dri2_egl_display
*dri2_dpy
=
1713 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1714 struct wl_shm_pool
*pool
;
1715 int fd
, stride
, size_map
;
1718 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1719 size_map
= h
* stride
;
1721 /* Create a shareable buffer */
1722 fd
= os_create_anonymous_file(size_map
);
1726 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1727 if (data_map
== MAP_FAILED
) {
1732 /* Share it in a wl_buffer */
1733 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1734 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1735 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1736 wl_shm_pool_destroy(pool
);
1745 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1747 struct dri2_egl_display
*dri2_dpy
=
1748 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1750 /* we need to do the following operations only once per frame */
1751 if (dri2_surf
->back
)
1754 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1755 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1757 dri2_wl_release_buffers(dri2_surf
);
1759 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1760 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1761 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1762 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1763 dri2_surf
->current
= NULL
;
1766 /* find back buffer */
1768 /* There might be a buffer release already queued that wasn't processed */
1769 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1771 /* try get free buffer already created */
1772 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1773 if (!dri2_surf
->color_buffers
[i
].locked
&&
1774 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1775 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1780 /* else choose any another free location */
1781 if (!dri2_surf
->back
) {
1782 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1783 if (!dri2_surf
->color_buffers
[i
].locked
) {
1784 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1785 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1787 dri2_surf
->base
.Width
,
1788 dri2_surf
->base
.Height
,
1789 &dri2_surf
->back
->data
,
1790 &dri2_surf
->back
->data_size
,
1791 &dri2_surf
->back
->wl_buffer
)) {
1792 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1795 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1796 &wl_buffer_listener
, dri2_surf
);
1802 if (!dri2_surf
->back
) {
1803 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1807 dri2_surf
->back
->locked
= true;
1809 /* If we have an extra unlocked buffer at this point, we had to do triple
1810 * buffering for a while, but now can go back to just double buffering.
1811 * That means we can free any unlocked buffer now. */
1812 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1813 if (!dri2_surf
->color_buffers
[i
].locked
&&
1814 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1815 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1816 munmap(dri2_surf
->color_buffers
[i
].data
,
1817 dri2_surf
->color_buffers
[i
].data_size
);
1818 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1819 dri2_surf
->color_buffers
[i
].data
= NULL
;
1827 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1829 /* if there has been a resize: */
1830 if (!dri2_surf
->current
)
1833 return dri2_surf
->current
->data
;
1837 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1839 assert(dri2_surf
->back
);
1840 return dri2_surf
->back
->data
;
1844 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1846 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1848 while (dri2_surf
->throttle_callback
!= NULL
)
1849 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1850 dri2_surf
->wl_queue
) == -1)
1853 if (dri2_surf
->base
.SwapInterval
> 0) {
1854 dri2_surf
->throttle_callback
=
1855 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1856 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1857 &throttle_listener
, dri2_surf
);
1860 dri2_surf
->current
= dri2_surf
->back
;
1861 dri2_surf
->back
= NULL
;
1863 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1864 dri2_surf
->current
->wl_buffer
,
1865 dri2_surf
->dx
, dri2_surf
->dy
);
1867 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1868 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1869 /* reset resize growing parameters */
1873 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1874 0, 0, INT32_MAX
, INT32_MAX
);
1875 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1877 /* If we're not waiting for a frame callback then we'll at least throttle
1878 * to a sync callback so that we always give a chance for the compositor to
1879 * handle the commit and send a release event before checking for a free
1881 if (dri2_surf
->throttle_callback
== NULL
) {
1882 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1883 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1884 &throttle_listener
, dri2_surf
);
1887 wl_display_flush(dri2_dpy
->wl_dpy
);
1891 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1892 int *x
, int *y
, int *w
, int *h
,
1893 void *loaderPrivate
)
1895 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1897 (void) swrast_update_buffers(dri2_surf
);
1900 *w
= dri2_surf
->base
.Width
;
1901 *h
= dri2_surf
->base
.Height
;
1905 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1906 int x
, int y
, int w
, int h
,
1907 char *data
, void *loaderPrivate
)
1909 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1910 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1911 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1912 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1913 int dst_stride
= copy_width
;
1916 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1918 memset(data
, 0, copy_width
* h
);
1922 assert(data
!= src
);
1923 assert(copy_width
<= src_stride
);
1926 src
+= y
* src_stride
;
1929 if (copy_width
> src_stride
-x_offset
)
1930 copy_width
= src_stride
-x_offset
;
1931 if (h
> dri2_surf
->base
.Height
-y
)
1932 h
= dri2_surf
->base
.Height
-y
;
1935 memcpy(dst
, src
, copy_width
);
1942 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1943 int x
, int y
, int w
, int h
, int stride
,
1944 char *data
, void *loaderPrivate
)
1946 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1947 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1948 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1949 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1952 assert(copy_width
<= stride
);
1954 (void) swrast_update_buffers(dri2_surf
);
1955 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1957 /* partial copy, copy old content */
1958 if (copy_width
< dst_stride
)
1959 dri2_wl_swrast_get_image(draw
, 0, 0,
1960 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1961 dst
, loaderPrivate
);
1964 dst
+= y
* dst_stride
;
1968 /* drivers expect we do these checks (and some rely on it) */
1969 if (copy_width
> dst_stride
-x_offset
)
1970 copy_width
= dst_stride
-x_offset
;
1971 if (h
> dri2_surf
->base
.Height
-y
)
1972 h
= dri2_surf
->base
.Height
-y
;
1975 memcpy(dst
, src
, copy_width
);
1979 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1983 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1984 int x
, int y
, int w
, int h
,
1985 char *data
, void *loaderPrivate
)
1987 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1990 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1991 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1992 stride
, data
, loaderPrivate
);
1996 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1998 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1999 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
2001 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
2006 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
2008 struct dri2_egl_display
*dri2_dpy
= data
;
2009 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
2011 if (visual_idx
== -1)
2014 dri2_dpy
->formats
|= (1u << visual_idx
);
2017 static const struct wl_shm_listener shm_listener
= {
2018 .format
= shm_handle_format
2022 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
2023 uint32_t name
, const char *interface
,
2026 struct dri2_egl_display
*dri2_dpy
= data
;
2028 if (strcmp(interface
, "wl_shm") == 0) {
2030 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
2031 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
2035 static const struct wl_registry_listener registry_listener_swrast
= {
2036 .global
= registry_handle_global_swrast
,
2037 .global_remove
= registry_handle_global_remove
2040 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
2041 .authenticate
= NULL
,
2042 .create_window_surface
= dri2_wl_create_window_surface
,
2043 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
2044 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
2045 .destroy_surface
= dri2_wl_destroy_surface
,
2046 .create_image
= dri2_create_image_khr
,
2047 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
2048 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
2049 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
2050 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
2051 .copy_buffers
= dri2_fallback_copy_buffers
,
2052 .query_buffer_age
= dri2_fallback_query_buffer_age
,
2053 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
2054 .get_sync_values
= dri2_fallback_get_sync_values
,
2055 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
2058 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
2059 .base
= { __DRI_SWRAST_LOADER
, 2 },
2061 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
2062 .putImage
= dri2_wl_swrast_put_image
,
2063 .getImage
= dri2_wl_swrast_get_image
,
2064 .putImage2
= dri2_wl_swrast_put_image2
,
2067 static const __DRIextension
*swrast_loader_extensions
[] = {
2068 &swrast_loader_extension
.base
,
2069 &image_lookup_extension
.base
,
2074 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2077 struct dri2_egl_display
*dri2_dpy
;
2079 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2081 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2084 disp
->DriverData
= (void *) dri2_dpy
;
2085 if (disp
->PlatformDisplay
== NULL
) {
2086 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2087 if (dri2_dpy
->wl_dpy
== NULL
)
2089 dri2_dpy
->own_device
= true;
2091 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2094 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
2096 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
2102 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2104 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2105 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2108 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2109 dri2_dpy
->wl_queue
);
2111 if (dri2_dpy
->own_device
)
2112 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2114 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2115 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2116 ®istry_listener_swrast
, dri2_dpy
);
2118 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2121 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
2124 dri2_dpy
->driver_name
= strdup("swrast");
2125 if (!dri2_load_driver_swrast(disp
))
2128 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2130 if (!dri2_create_screen(disp
))
2133 if (!dri2_setup_extensions(disp
))
2136 dri2_setup_screen(disp
);
2138 dri2_wl_setup_swap_interval(disp
);
2140 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2141 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2145 /* Fill vtbl last to prevent accidentally calling virtual function during
2148 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2153 dri2_display_destroy(disp
);
2158 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2160 EGLBoolean initialized
= EGL_FALSE
;
2162 if (!disp
->Options
.ForceSoftware
)
2163 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2166 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2173 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2175 if (dri2_dpy
->wl_drm
)
2176 wl_drm_destroy(dri2_dpy
->wl_drm
);
2177 if (dri2_dpy
->wl_dmabuf
)
2178 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2179 if (dri2_dpy
->wl_shm
)
2180 wl_shm_destroy(dri2_dpy
->wl_shm
);
2181 if (dri2_dpy
->wl_registry
)
2182 wl_registry_destroy(dri2_dpy
->wl_registry
);
2183 if (dri2_dpy
->wl_queue
)
2184 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2185 if (dri2_dpy
->wl_dpy_wrapper
)
2186 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2188 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2189 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2190 free(dri2_dpy
->wl_modifiers
);
2192 if (dri2_dpy
->own_device
)
2193 wl_display_disconnect(dri2_dpy
->wl_dpy
);