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 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
276 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
277 struct wl_egl_window
*window
= native_window
;
278 struct dri2_egl_surface
*dri2_surf
;
280 const __DRIconfig
*config
;
282 dri2_surf
= calloc(1, sizeof *dri2_surf
);
284 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
288 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
289 attrib_list
, false, native_window
))
292 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
293 dri2_surf
->base
.GLColorspace
);
296 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
300 dri2_surf
->base
.Width
= window
->width
;
301 dri2_surf
->base
.Height
= window
->height
;
303 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
304 assert(visual_idx
!= -1);
306 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
307 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
309 assert(dri2_dpy
->wl_shm
);
310 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
313 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
314 if (!dri2_surf
->wl_queue
) {
315 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
319 if (dri2_dpy
->wl_drm
) {
320 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
321 if (!dri2_surf
->wl_drm_wrapper
) {
322 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
325 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
326 dri2_surf
->wl_queue
);
329 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
330 if (!dri2_surf
->wl_dpy_wrapper
) {
331 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
334 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
335 dri2_surf
->wl_queue
);
337 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
338 if (!dri2_surf
->wl_surface_wrapper
) {
339 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
340 goto cleanup_dpy_wrapper
;
342 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
343 dri2_surf
->wl_queue
);
345 dri2_surf
->wl_win
= window
;
346 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
347 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
349 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
351 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
352 goto cleanup_surf_wrapper
;
354 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
356 return &dri2_surf
->base
;
358 cleanup_surf_wrapper
:
359 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
361 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
363 if (dri2_surf
->wl_drm_wrapper
)
364 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
366 wl_event_queue_destroy(dri2_surf
->wl_queue
);
374 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
375 _EGLConfig
*conf
, void *native_window
,
376 const EGLint
*attrib_list
)
378 /* From the EGL_EXT_platform_wayland spec, version 3:
380 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
381 * that belongs to Wayland. Any such call fails and generates
384 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
390 * Called via eglDestroySurface(), drv->API.DestroySurface().
393 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
395 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
396 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
400 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
402 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
403 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
404 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
405 if (dri2_surf
->color_buffers
[i
].dri_image
)
406 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
407 if (dri2_surf
->color_buffers
[i
].linear_copy
)
408 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
409 if (dri2_surf
->color_buffers
[i
].data
)
410 munmap(dri2_surf
->color_buffers
[i
].data
,
411 dri2_surf
->color_buffers
[i
].data_size
);
415 dri2_egl_surface_free_local_buffers(dri2_surf
);
417 if (dri2_surf
->throttle_callback
)
418 wl_callback_destroy(dri2_surf
->throttle_callback
);
420 if (dri2_surf
->wl_win
) {
421 dri2_surf
->wl_win
->driver_private
= NULL
;
422 dri2_surf
->wl_win
->resize_callback
= NULL
;
423 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
426 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
427 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
428 if (dri2_surf
->wl_drm_wrapper
)
429 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
430 wl_event_queue_destroy(dri2_surf
->wl_queue
);
432 dri2_fini_surface(surf
);
439 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
441 struct dri2_egl_display
*dri2_dpy
=
442 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
444 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
445 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
446 if (dri2_surf
->color_buffers
[i
].locked
) {
447 dri2_surf
->color_buffers
[i
].wl_release
= true;
449 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
450 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
453 if (dri2_surf
->color_buffers
[i
].dri_image
)
454 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
455 if (dri2_surf
->color_buffers
[i
].linear_copy
)
456 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
457 if (dri2_surf
->color_buffers
[i
].data
)
458 munmap(dri2_surf
->color_buffers
[i
].data
,
459 dri2_surf
->color_buffers
[i
].data_size
);
461 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
462 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
463 dri2_surf
->color_buffers
[i
].data
= NULL
;
467 dri2_egl_surface_free_local_buffers(dri2_surf
);
471 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
473 struct dri2_egl_display
*dri2_dpy
=
474 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
477 unsigned int dri_image_format
;
478 unsigned int linear_dri_image_format
;
482 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
483 assert(visual_idx
!= -1);
484 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
485 linear_dri_image_format
= dri_image_format
;
486 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
487 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
489 /* Substitute dri image format if server does not support original format */
490 if (!(dri2_dpy
->formats
& (1 << visual_idx
)))
491 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
493 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
494 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
497 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
498 assert(dri2_dpy
->formats
&
499 (1 << dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
501 /* There might be a buffer release already queued that wasn't processed */
502 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
504 while (dri2_surf
->back
== NULL
) {
505 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
506 /* Get an unlocked buffer, preferably one with a dri_buffer
507 * already allocated. */
508 if (dri2_surf
->color_buffers
[i
].locked
)
510 if (dri2_surf
->back
== NULL
)
511 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
512 else if (dri2_surf
->back
->dri_image
== NULL
)
513 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
519 /* If we don't have a buffer, then block on the server to release one for
520 * us, and try again. wl_display_dispatch_queue will process any pending
521 * events, however not all servers flush on issuing a buffer release
522 * event. So, we spam the server with roundtrips as they always cause a
525 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
526 dri2_surf
->wl_queue
) < 0)
530 if (dri2_surf
->back
== NULL
)
533 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
535 if (dri2_dpy
->is_different_gpu
&&
536 dri2_surf
->back
->linear_copy
== NULL
) {
537 /* The LINEAR modifier should be a perfect alias of the LINEAR use
538 * flag; try the new interface first before the old, then fall back. */
539 if (dri2_dpy
->image
->base
.version
>= 15 &&
540 dri2_dpy
->image
->createImageWithModifiers
) {
541 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
543 dri2_surf
->back
->linear_copy
=
544 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
545 dri2_surf
->base
.Width
,
546 dri2_surf
->base
.Height
,
547 linear_dri_image_format
,
552 dri2_surf
->back
->linear_copy
=
553 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
554 dri2_surf
->base
.Width
,
555 dri2_surf
->base
.Height
,
556 linear_dri_image_format
,
558 __DRI_IMAGE_USE_LINEAR
,
561 if (dri2_surf
->back
->linear_copy
== NULL
)
565 if (dri2_surf
->back
->dri_image
== NULL
) {
566 /* If our DRIImage implementation does not support
567 * createImageWithModifiers, then fall back to the old createImage,
568 * and hope it allocates an image which is acceptable to the winsys.
570 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
571 dri2_dpy
->image
->createImageWithModifiers
) {
572 dri2_surf
->back
->dri_image
=
573 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
574 dri2_surf
->base
.Width
,
575 dri2_surf
->base
.Height
,
581 dri2_surf
->back
->dri_image
=
582 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
583 dri2_surf
->base
.Width
,
584 dri2_surf
->base
.Height
,
586 dri2_dpy
->is_different_gpu
?
591 dri2_surf
->back
->age
= 0;
593 if (dri2_surf
->back
->dri_image
== NULL
)
596 dri2_surf
->back
->locked
= true;
603 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
605 struct dri2_egl_display
*dri2_dpy
=
606 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
610 image
= dri2_surf
->back
->dri_image
;
612 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
613 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
615 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
617 buffer
->pitch
= pitch
;
623 update_buffers(struct dri2_egl_surface
*dri2_surf
)
625 struct dri2_egl_display
*dri2_dpy
=
626 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
628 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
629 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
631 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
632 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
633 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
634 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
637 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
638 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
639 dri2_wl_release_buffers(dri2_surf
);
642 if (get_back_bo(dri2_surf
) < 0) {
643 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
647 /* If we have an extra unlocked buffer at this point, we had to do triple
648 * buffering for a while, but now can go back to just double buffering.
649 * That means we can free any unlocked buffer now. */
650 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
651 if (!dri2_surf
->color_buffers
[i
].locked
&&
652 dri2_surf
->color_buffers
[i
].wl_buffer
) {
653 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
654 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
655 if (dri2_dpy
->is_different_gpu
)
656 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
657 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
658 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
659 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
667 update_buffers_if_needed(struct dri2_egl_surface
*dri2_surf
)
669 if (dri2_surf
->back
!= NULL
)
672 return update_buffers(dri2_surf
);
676 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
677 int *width
, int *height
,
678 unsigned int *attachments
, int count
,
679 int *out_count
, void *loaderPrivate
)
681 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
684 if (update_buffers(dri2_surf
) < 0)
687 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
690 switch (attachments
[i
]) {
691 case __DRI_BUFFER_BACK_LEFT
:
692 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
695 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
699 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
702 dri2_surf
->buffers
[j
] = *local
;
711 *width
= dri2_surf
->base
.Width
;
712 *height
= dri2_surf
->base
.Height
;
714 return dri2_surf
->buffers
;
718 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
719 int *width
, int *height
,
720 unsigned int *attachments
, int count
,
721 int *out_count
, void *loaderPrivate
)
723 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
724 unsigned int *attachments_with_format
;
726 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
728 if (visual_idx
== -1)
731 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
732 if (!attachments_with_format
) {
737 for (int i
= 0; i
< count
; ++i
) {
738 attachments_with_format
[2*i
] = attachments
[i
];
739 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
743 dri2_wl_get_buffers_with_format(driDrawable
,
745 attachments_with_format
, count
,
746 out_count
, loaderPrivate
);
748 free(attachments_with_format
);
754 image_get_buffers(__DRIdrawable
*driDrawable
,
758 uint32_t buffer_mask
,
759 struct __DRIimageList
*buffers
)
761 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
763 if (update_buffers(dri2_surf
) < 0)
766 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
767 buffers
->back
= dri2_surf
->back
->dri_image
;
773 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
776 (void) loaderPrivate
;
779 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
780 .base
= { __DRI_DRI2_LOADER
, 3 },
782 .getBuffers
= dri2_wl_get_buffers
,
783 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
784 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
787 static const __DRIimageLoaderExtension image_loader_extension
= {
788 .base
= { __DRI_IMAGE_LOADER
, 1 },
790 .getBuffers
= image_get_buffers
,
791 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
795 wayland_throttle_callback(void *data
,
796 struct wl_callback
*callback
,
799 struct dri2_egl_surface
*dri2_surf
= data
;
801 dri2_surf
->throttle_callback
= NULL
;
802 wl_callback_destroy(callback
);
805 static const struct wl_callback_listener throttle_listener
= {
806 .done
= wayland_throttle_callback
810 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
811 __DRIimage
*image
, int *fourcc
)
817 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
822 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
827 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
828 if (visual_idx
== -1)
831 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
835 static struct wl_buffer
*
836 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
837 struct dri2_egl_surface
*dri2_surf
,
840 struct wl_buffer
*ret
;
842 int width
, height
, fourcc
, num_planes
;
843 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
845 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
846 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
848 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
852 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
857 if (dri2_dpy
->image
->base
.version
>= 15) {
860 query
= dri2_dpy
->image
->queryImage(image
,
861 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
863 query
&= dri2_dpy
->image
->queryImage(image
,
864 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
867 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
871 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
872 struct zwp_linux_buffer_params_v1
*params
;
875 /* We don't need a wrapper for wl_dmabuf objects, because we have to
876 * create the intermediate params object; we can set the queue on this,
877 * and the wl_buffer inherits it race-free. */
878 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
880 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
882 for (i
= 0; i
< num_planes
; i
++) {
887 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
893 query
= dri2_dpy
->image
->queryImage(p_image
,
894 __DRI_IMAGE_ATTRIB_FD
,
896 query
&= dri2_dpy
->image
->queryImage(p_image
,
897 __DRI_IMAGE_ATTRIB_STRIDE
,
899 query
&= dri2_dpy
->image
->queryImage(p_image
,
900 __DRI_IMAGE_ATTRIB_OFFSET
,
902 if (image
!= p_image
)
903 dri2_dpy
->image
->destroyImage(p_image
);
908 zwp_linux_buffer_params_v1_destroy(params
);
912 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
913 modifier
>> 32, modifier
& 0xffffffff);
917 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
919 zwp_linux_buffer_params_v1_destroy(params
);
920 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
921 struct wl_drm
*wl_drm
=
922 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
928 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
929 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
930 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
934 struct wl_drm
*wl_drm
=
935 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
941 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
942 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
943 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
950 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
954 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
955 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
958 for (int i
= 0; i
< n_rects
; i
++) {
959 const int *rect
= &rects
[i
* 4];
961 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
963 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
969 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
972 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
978 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
979 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
981 while (dri2_surf
->throttle_callback
!= NULL
)
982 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
983 dri2_surf
->wl_queue
) == -1)
986 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
987 if (dri2_surf
->color_buffers
[i
].age
> 0)
988 dri2_surf
->color_buffers
[i
].age
++;
990 /* Make sure we have a back buffer in case we're swapping without ever
992 if (update_buffers_if_needed(dri2_surf
) < 0)
993 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
995 if (draw
->SwapInterval
> 0) {
996 dri2_surf
->throttle_callback
=
997 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
998 wl_callback_add_listener(dri2_surf
->throttle_callback
,
999 &throttle_listener
, dri2_surf
);
1002 dri2_surf
->back
->age
= 1;
1003 dri2_surf
->current
= dri2_surf
->back
;
1004 dri2_surf
->back
= NULL
;
1006 if (!dri2_surf
->current
->wl_buffer
) {
1009 if (dri2_dpy
->is_different_gpu
)
1010 image
= dri2_surf
->current
->linear_copy
;
1012 image
= dri2_surf
->current
->dri_image
;
1014 dri2_surf
->current
->wl_buffer
=
1015 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1017 dri2_surf
->current
->wl_release
= false;
1019 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1020 &wl_buffer_listener
, dri2_surf
);
1023 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1024 dri2_surf
->current
->wl_buffer
,
1025 dri2_surf
->dx
, dri2_surf
->dy
);
1027 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1028 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1029 /* reset resize growing parameters */
1033 /* If the compositor doesn't support damage_buffer, we deliberately
1034 * ignore the damage region and post maximum damage, due to
1035 * https://bugs.freedesktop.org/78190 */
1036 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1037 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1038 0, 0, INT32_MAX
, INT32_MAX
);
1040 if (dri2_dpy
->is_different_gpu
) {
1041 _EGLContext
*ctx
= _eglGetCurrentContext();
1042 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1043 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1044 dri2_surf
->current
->linear_copy
,
1045 dri2_surf
->current
->dri_image
,
1046 0, 0, dri2_surf
->base
.Width
,
1047 dri2_surf
->base
.Height
,
1048 0, 0, dri2_surf
->base
.Width
,
1049 dri2_surf
->base
.Height
, 0);
1052 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1053 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1055 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1057 /* If we're not waiting for a frame callback then we'll at least throttle
1058 * to a sync callback so that we always give a chance for the compositor to
1059 * handle the commit and send a release event before checking for a free
1061 if (dri2_surf
->throttle_callback
== NULL
) {
1062 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1063 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1064 &throttle_listener
, dri2_surf
);
1067 wl_display_flush(dri2_dpy
->wl_dpy
);
1073 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1074 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1076 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1078 if (update_buffers_if_needed(dri2_surf
) < 0) {
1079 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1083 return dri2_surf
->back
->age
;
1087 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1089 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1092 static struct wl_buffer
*
1093 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1097 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1098 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1099 __DRIimage
*image
= dri2_img
->dri_image
;
1100 struct wl_buffer
*buffer
;
1101 int format
, visual_idx
;
1103 /* Check the upstream display supports this buffer's format. */
1104 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1105 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1106 if (visual_idx
== -1)
1109 if (!(dri2_dpy
->formats
& (1u << visual_idx
)))
1112 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1114 /* The buffer object will have been created with our internal event queue
1115 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1116 * buffer to be used by the application so we'll reset it to the display's
1117 * default event queue. This isn't actually racy, as the only event the
1118 * buffer can get is a buffer release, which doesn't happen with an explicit
1121 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1126 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1131 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1133 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1136 if (dri2_dpy
->is_render_node
) {
1137 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1138 "authenticate for render-nodes");
1141 dri2_dpy
->authenticated
= false;
1143 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1144 if (roundtrip(dri2_dpy
) < 0)
1147 if (!dri2_dpy
->authenticated
)
1150 /* reset authenticated */
1151 dri2_dpy
->authenticated
= true;
1157 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1159 struct dri2_egl_display
*dri2_dpy
= data
;
1162 dri2_dpy
->device_name
= strdup(device
);
1163 if (!dri2_dpy
->device_name
)
1166 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1167 if (dri2_dpy
->fd
== -1) {
1168 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1169 dri2_dpy
->device_name
, strerror(errno
));
1170 free(dri2_dpy
->device_name
);
1171 dri2_dpy
->device_name
= NULL
;
1175 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1176 dri2_dpy
->authenticated
= true;
1178 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1179 close(dri2_dpy
->fd
);
1181 free(dri2_dpy
->device_name
);
1182 dri2_dpy
->device_name
= NULL
;
1183 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1186 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1191 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1193 struct dri2_egl_display
*dri2_dpy
= data
;
1194 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1196 if (visual_idx
== -1)
1199 dri2_dpy
->formats
|= (1u << visual_idx
);
1203 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1205 struct dri2_egl_display
*dri2_dpy
= data
;
1207 dri2_dpy
->capabilities
= value
;
1211 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1213 struct dri2_egl_display
*dri2_dpy
= data
;
1215 dri2_dpy
->authenticated
= true;
1218 static const struct wl_drm_listener drm_listener
= {
1219 .device
= drm_handle_device
,
1220 .format
= drm_handle_format
,
1221 .authenticated
= drm_handle_authenticated
,
1222 .capabilities
= drm_handle_capabilities
1226 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1229 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1233 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1234 uint32_t format
, uint32_t modifier_hi
,
1235 uint32_t modifier_lo
)
1237 struct dri2_egl_display
*dri2_dpy
= data
;
1238 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1241 if (visual_idx
== -1)
1244 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1245 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1248 dri2_dpy
->formats
|= (1u << visual_idx
);
1250 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1251 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1254 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1255 .format
= dmabuf_ignore_format
,
1256 .modifier
= dmabuf_handle_modifier
,
1260 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1261 uint32_t name
, const char *interface
,
1264 struct dri2_egl_display
*dri2_dpy
= data
;
1266 if (strcmp(interface
, "wl_drm") == 0) {
1268 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1269 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1270 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1271 dri2_dpy
->wl_dmabuf
=
1272 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1274 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1280 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1285 static const struct wl_registry_listener registry_listener_drm
= {
1286 .global
= registry_handle_global_drm
,
1287 .global_remove
= registry_handle_global_remove
1291 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1293 /* We can't use values greater than 1 on Wayland because we are using the
1294 * frame callback to synchronise the frame and the only way we be sure to
1295 * get a frame callback is to attach a new buffer. Therefore we can't just
1296 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1298 dri2_setup_swap_interval(disp
, 1);
1301 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1302 .authenticate
= dri2_wl_authenticate
,
1303 .create_window_surface
= dri2_wl_create_window_surface
,
1304 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1305 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1306 .destroy_surface
= dri2_wl_destroy_surface
,
1307 .create_image
= dri2_create_image_khr
,
1308 .swap_buffers
= dri2_wl_swap_buffers
,
1309 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1310 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1311 .set_damage_region
= dri2_fallback_set_damage_region
,
1312 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1313 .copy_buffers
= dri2_fallback_copy_buffers
,
1314 .query_buffer_age
= dri2_wl_query_buffer_age
,
1315 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1316 .get_sync_values
= dri2_fallback_get_sync_values
,
1317 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1320 static const __DRIextension
*dri2_loader_extensions
[] = {
1321 &dri2_loader_extension
.base
,
1322 &image_loader_extension
.base
,
1323 &image_lookup_extension
.base
,
1324 &use_invalidate
.base
,
1328 static const __DRIextension
*image_loader_extensions
[] = {
1329 &image_loader_extension
.base
,
1330 &image_lookup_extension
.base
,
1331 &use_invalidate
.base
,
1336 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1338 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1339 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1340 unsigned int count
= 0;
1343 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1346 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1347 struct dri2_egl_config
*dri2_conf
;
1349 if (!(dri2_dpy
->formats
& (1u << j
)))
1352 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1353 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1355 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1362 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1363 struct dri2_egl_config
*dri2_conf
;
1364 int alt_dri_image_format
, c
, s
;
1366 /* No match for config. Try if we can blitImage convert to a visual */
1367 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1368 dri2_dpy
->driver_configs
[i
]);
1373 /* Find optimal target visual for blitImage conversion, if any. */
1374 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1375 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1377 if (s
== -1 || !(dri2_dpy
->formats
& (1 << s
)))
1380 /* Visual s works for the Wayland server, and c can be converted into s
1381 * by our client gpu during PRIME blitImage conversion to a linear
1382 * wl_buffer, so add visual c as supported by the client renderer.
1384 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1385 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1386 dri2_wl_visuals
[c
].rgba_masks
);
1388 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1391 if (format_count
[c
] == 1)
1392 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1393 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1394 dri2_wl_visuals
[s
].format_name
);
1399 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1400 if (!format_count
[i
]) {
1401 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1402 dri2_wl_visuals
[i
].format_name
);
1406 return (count
!= 0);
1410 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1413 struct dri2_egl_display
*dri2_dpy
;
1415 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1417 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1420 disp
->DriverData
= (void *) dri2_dpy
;
1421 if (disp
->PlatformDisplay
== NULL
) {
1422 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1423 if (dri2_dpy
->wl_dpy
== NULL
)
1425 dri2_dpy
->own_device
= true;
1427 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1430 dri2_dpy
->wl_modifiers
=
1431 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1432 if (!dri2_dpy
->wl_modifiers
)
1434 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1435 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1439 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1441 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1442 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1445 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1446 dri2_dpy
->wl_queue
);
1448 if (dri2_dpy
->own_device
)
1449 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1451 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1452 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1453 ®istry_listener_drm
, dri2_dpy
);
1454 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1457 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1460 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1463 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1464 &dri2_dpy
->is_different_gpu
);
1465 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1467 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1473 if (dri2_dpy
->is_different_gpu
) {
1474 free(dri2_dpy
->device_name
);
1475 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1476 if (!dri2_dpy
->device_name
) {
1477 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1478 "for requested GPU");
1483 /* we have to do the check now, because loader_get_user_preferred_fd
1484 * will return a render-node when the requested gpu is different
1485 * to the server, but also if the client asks for the same gpu than
1486 * the server by requesting its pci-id */
1487 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1489 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1490 if (dri2_dpy
->driver_name
== NULL
) {
1491 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1495 /* render nodes cannot use Gem names, and thus do not support
1496 * the __DRI_DRI2_LOADER extension */
1497 if (!dri2_dpy
->is_render_node
) {
1498 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1499 if (!dri2_load_driver(disp
)) {
1500 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1504 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1505 if (!dri2_load_driver_dri3(disp
)) {
1506 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1511 if (!dri2_create_screen(disp
))
1514 if (!dri2_setup_extensions(disp
))
1517 dri2_setup_screen(disp
);
1519 dri2_wl_setup_swap_interval(disp
);
1521 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1522 * createImageFromFds support indicates that Prime export/import
1523 * is supported by the driver. Fall back to
1524 * gem names if we don't have Prime support. */
1526 if (dri2_dpy
->image
->base
.version
< 7 ||
1527 dri2_dpy
->image
->createImageFromFds
== NULL
)
1528 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1530 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1531 * The server needs to accept them */
1532 if (dri2_dpy
->is_render_node
&&
1533 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1534 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1538 if (dri2_dpy
->is_different_gpu
&&
1539 (dri2_dpy
->image
->base
.version
< 9 ||
1540 dri2_dpy
->image
->blitImage
== NULL
)) {
1541 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1542 "Image extension in the driver is not "
1543 "compatible. Version 9 or later and blitImage() "
1548 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1549 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1553 dri2_set_WL_bind_wayland_display(drv
, disp
);
1554 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1555 * because the buffer of the EGLImage has likely a tiling mode the server
1556 * gpu won't support. These is no way to check for now. Thus do not support the
1558 if (!dri2_dpy
->is_different_gpu
)
1559 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1561 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1563 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1565 /* Fill vtbl last to prevent accidentally calling virtual function during
1568 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1573 dri2_display_destroy(disp
);
1578 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1580 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1582 assume(visual_idx
!= -1);
1584 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1588 * Taken from weston shared/os-compatibility.c
1591 #ifndef HAVE_MKOSTEMP
1594 set_cloexec_or_close(int fd
)
1601 flags
= fcntl(fd
, F_GETFD
);
1605 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1618 * Taken from weston shared/os-compatibility.c
1622 create_tmpfile_cloexec(char *tmpname
)
1626 #ifdef HAVE_MKOSTEMP
1627 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1631 fd
= mkstemp(tmpname
);
1633 fd
= set_cloexec_or_close(fd
);
1642 * Taken from weston shared/os-compatibility.c
1644 * Create a new, unique, anonymous file of the given size, and
1645 * return the file descriptor for it. The file descriptor is set
1646 * CLOEXEC. The file is immediately suitable for mmap()'ing
1647 * the given size at offset zero.
1649 * The file should not have a permanent backing store like a disk,
1650 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1652 * The file name is deleted from the file system.
1654 * The file is suitable for buffer sharing between processes by
1655 * transmitting the file descriptor over Unix sockets using the
1656 * SCM_RIGHTS methods.
1658 * If the C library implements posix_fallocate(), it is used to
1659 * guarantee that disk space is available for the file at the
1660 * given size. If disk space is insufficient, errno is set to ENOSPC.
1661 * If posix_fallocate() is not supported, program may receive
1662 * SIGBUS on accessing mmap()'ed file contents instead.
1665 os_create_anonymous_file(off_t size
)
1667 static const char templ
[] = "/mesa-shared-XXXXXX";
1673 path
= getenv("XDG_RUNTIME_DIR");
1679 name
= malloc(strlen(path
) + sizeof(templ
));
1684 strcat(name
, templ
);
1686 fd
= create_tmpfile_cloexec(name
);
1693 ret
= ftruncate(fd
, size
);
1704 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1705 int format
, int w
, int h
,
1706 void **data
, int *size
,
1707 struct wl_buffer
**buffer
)
1709 struct dri2_egl_display
*dri2_dpy
=
1710 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1711 struct wl_shm_pool
*pool
;
1712 int fd
, stride
, size_map
;
1715 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1716 size_map
= h
* stride
;
1718 /* Create a shareable buffer */
1719 fd
= os_create_anonymous_file(size_map
);
1723 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1724 if (data_map
== MAP_FAILED
) {
1729 /* Share it in a wl_buffer */
1730 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1731 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1732 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1733 wl_shm_pool_destroy(pool
);
1742 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1744 struct dri2_egl_display
*dri2_dpy
=
1745 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1747 /* we need to do the following operations only once per frame */
1748 if (dri2_surf
->back
)
1751 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1752 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1754 dri2_wl_release_buffers(dri2_surf
);
1756 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1757 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1758 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1759 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1760 dri2_surf
->current
= NULL
;
1763 /* find back buffer */
1765 /* There might be a buffer release already queued that wasn't processed */
1766 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1768 /* try get free buffer already created */
1769 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1770 if (!dri2_surf
->color_buffers
[i
].locked
&&
1771 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1772 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1777 /* else choose any another free location */
1778 if (!dri2_surf
->back
) {
1779 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1780 if (!dri2_surf
->color_buffers
[i
].locked
) {
1781 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1782 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1784 dri2_surf
->base
.Width
,
1785 dri2_surf
->base
.Height
,
1786 &dri2_surf
->back
->data
,
1787 &dri2_surf
->back
->data_size
,
1788 &dri2_surf
->back
->wl_buffer
)) {
1789 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1792 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1793 &wl_buffer_listener
, dri2_surf
);
1799 if (!dri2_surf
->back
) {
1800 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1804 dri2_surf
->back
->locked
= true;
1806 /* If we have an extra unlocked buffer at this point, we had to do triple
1807 * buffering for a while, but now can go back to just double buffering.
1808 * That means we can free any unlocked buffer now. */
1809 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1810 if (!dri2_surf
->color_buffers
[i
].locked
&&
1811 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1812 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1813 munmap(dri2_surf
->color_buffers
[i
].data
,
1814 dri2_surf
->color_buffers
[i
].data_size
);
1815 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1816 dri2_surf
->color_buffers
[i
].data
= NULL
;
1824 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1826 /* if there has been a resize: */
1827 if (!dri2_surf
->current
)
1830 return dri2_surf
->current
->data
;
1834 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1836 assert(dri2_surf
->back
);
1837 return dri2_surf
->back
->data
;
1841 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1843 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1845 while (dri2_surf
->throttle_callback
!= NULL
)
1846 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1847 dri2_surf
->wl_queue
) == -1)
1850 if (dri2_surf
->base
.SwapInterval
> 0) {
1851 dri2_surf
->throttle_callback
=
1852 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1853 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1854 &throttle_listener
, dri2_surf
);
1857 dri2_surf
->current
= dri2_surf
->back
;
1858 dri2_surf
->back
= NULL
;
1860 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1861 dri2_surf
->current
->wl_buffer
,
1862 dri2_surf
->dx
, dri2_surf
->dy
);
1864 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1865 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1866 /* reset resize growing parameters */
1870 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1871 0, 0, INT32_MAX
, INT32_MAX
);
1872 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1874 /* If we're not waiting for a frame callback then we'll at least throttle
1875 * to a sync callback so that we always give a chance for the compositor to
1876 * handle the commit and send a release event before checking for a free
1878 if (dri2_surf
->throttle_callback
== NULL
) {
1879 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1880 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1881 &throttle_listener
, dri2_surf
);
1884 wl_display_flush(dri2_dpy
->wl_dpy
);
1888 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1889 int *x
, int *y
, int *w
, int *h
,
1890 void *loaderPrivate
)
1892 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1894 (void) swrast_update_buffers(dri2_surf
);
1897 *w
= dri2_surf
->base
.Width
;
1898 *h
= dri2_surf
->base
.Height
;
1902 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1903 int x
, int y
, int w
, int h
,
1904 char *data
, void *loaderPrivate
)
1906 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1907 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1908 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1909 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1910 int dst_stride
= copy_width
;
1913 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1915 memset(data
, 0, copy_width
* h
);
1919 assert(data
!= src
);
1920 assert(copy_width
<= src_stride
);
1923 src
+= y
* src_stride
;
1926 if (copy_width
> src_stride
-x_offset
)
1927 copy_width
= src_stride
-x_offset
;
1928 if (h
> dri2_surf
->base
.Height
-y
)
1929 h
= dri2_surf
->base
.Height
-y
;
1932 memcpy(dst
, src
, copy_width
);
1939 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1940 int x
, int y
, int w
, int h
, int stride
,
1941 char *data
, void *loaderPrivate
)
1943 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1944 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1945 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1946 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1949 assert(copy_width
<= stride
);
1951 (void) swrast_update_buffers(dri2_surf
);
1952 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1954 /* partial copy, copy old content */
1955 if (copy_width
< dst_stride
)
1956 dri2_wl_swrast_get_image(draw
, 0, 0,
1957 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1958 dst
, loaderPrivate
);
1961 dst
+= y
* dst_stride
;
1965 /* drivers expect we do these checks (and some rely on it) */
1966 if (copy_width
> dst_stride
-x_offset
)
1967 copy_width
= dst_stride
-x_offset
;
1968 if (h
> dri2_surf
->base
.Height
-y
)
1969 h
= dri2_surf
->base
.Height
-y
;
1972 memcpy(dst
, src
, copy_width
);
1976 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1980 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1981 int x
, int y
, int w
, int h
,
1982 char *data
, void *loaderPrivate
)
1984 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1987 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1988 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1989 stride
, data
, loaderPrivate
);
1993 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1995 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1996 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1998 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
2003 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
2005 struct dri2_egl_display
*dri2_dpy
= data
;
2006 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
2008 if (visual_idx
== -1)
2011 dri2_dpy
->formats
|= (1u << visual_idx
);
2014 static const struct wl_shm_listener shm_listener
= {
2015 .format
= shm_handle_format
2019 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
2020 uint32_t name
, const char *interface
,
2023 struct dri2_egl_display
*dri2_dpy
= data
;
2025 if (strcmp(interface
, "wl_shm") == 0) {
2027 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
2028 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
2032 static const struct wl_registry_listener registry_listener_swrast
= {
2033 .global
= registry_handle_global_swrast
,
2034 .global_remove
= registry_handle_global_remove
2037 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
2038 .authenticate
= NULL
,
2039 .create_window_surface
= dri2_wl_create_window_surface
,
2040 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
2041 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
2042 .destroy_surface
= dri2_wl_destroy_surface
,
2043 .create_image
= dri2_create_image_khr
,
2044 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
2045 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
2046 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
2047 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
2048 .copy_buffers
= dri2_fallback_copy_buffers
,
2049 .query_buffer_age
= dri2_fallback_query_buffer_age
,
2050 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
2051 .get_sync_values
= dri2_fallback_get_sync_values
,
2052 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
2055 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
2056 .base
= { __DRI_SWRAST_LOADER
, 2 },
2058 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
2059 .putImage
= dri2_wl_swrast_put_image
,
2060 .getImage
= dri2_wl_swrast_get_image
,
2061 .putImage2
= dri2_wl_swrast_put_image2
,
2064 static const __DRIextension
*swrast_loader_extensions
[] = {
2065 &swrast_loader_extension
.base
,
2066 &image_lookup_extension
.base
,
2071 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2074 struct dri2_egl_display
*dri2_dpy
;
2076 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2078 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2081 disp
->DriverData
= (void *) dri2_dpy
;
2082 if (disp
->PlatformDisplay
== NULL
) {
2083 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2084 if (dri2_dpy
->wl_dpy
== NULL
)
2086 dri2_dpy
->own_device
= true;
2088 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2091 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
2093 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
2099 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2101 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2102 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2105 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2106 dri2_dpy
->wl_queue
);
2108 if (dri2_dpy
->own_device
)
2109 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2111 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2112 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2113 ®istry_listener_swrast
, dri2_dpy
);
2115 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2118 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
2121 dri2_dpy
->driver_name
= strdup("swrast");
2122 if (!dri2_load_driver_swrast(disp
))
2125 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2127 if (!dri2_create_screen(disp
))
2130 if (!dri2_setup_extensions(disp
))
2133 dri2_setup_screen(disp
);
2135 dri2_wl_setup_swap_interval(disp
);
2137 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2138 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2142 /* Fill vtbl last to prevent accidentally calling virtual function during
2145 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2150 dri2_display_destroy(disp
);
2155 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2157 EGLBoolean initialized
= EGL_FALSE
;
2159 if (!disp
->Options
.ForceSoftware
)
2160 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2163 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2170 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2172 if (dri2_dpy
->wl_drm
)
2173 wl_drm_destroy(dri2_dpy
->wl_drm
);
2174 if (dri2_dpy
->wl_dmabuf
)
2175 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2176 if (dri2_dpy
->wl_shm
)
2177 wl_shm_destroy(dri2_dpy
->wl_shm
);
2178 if (dri2_dpy
->wl_registry
)
2179 wl_registry_destroy(dri2_dpy
->wl_registry
);
2180 if (dri2_dpy
->wl_queue
)
2181 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2182 if (dri2_dpy
->wl_dpy_wrapper
)
2183 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2185 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2186 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2187 free(dri2_dpy
->wl_modifiers
);
2189 if (dri2_dpy
->own_device
)
2190 wl_display_disconnect(dri2_dpy
->wl_dpy
);