2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
39 #include "drm-uapi/drm_fourcc.h"
43 #include "egl_dri2_fallbacks.h"
45 #include "util/u_vector.h"
46 #include "util/anon_file.h"
47 #include "eglglobals.h"
49 #include <wayland-egl-backend.h>
50 #include <wayland-client.h>
51 #include "wayland-drm-client-protocol.h"
52 #include "linux-dmabuf-unstable-v1-client-protocol.h"
54 /* cheesy workaround until wayland 1.18 is released */
55 #if WAYLAND_VERSION_MAJOR > 1 || \
56 (WAYLAND_VERSION_MAJOR == 1 && WAYLAND_VERSION_MINOR < 18)
57 #define WL_SHM_FORMAT_ABGR16161616F 0x48344241
58 #define WL_SHM_FORMAT_XBGR16161616F 0x48344258
62 * The index of entries in this table is used as a bitmask in
63 * dri2_dpy->formats, which tracks the formats supported by our server.
65 static const struct dri2_wl_visual
{
66 const char *format_name
;
67 uint32_t wl_drm_format
;
68 uint32_t wl_shm_format
;
70 /* alt_dri_image_format is a substitute wl_buffer format to use for a
71 * wl-server unsupported dri_image_format, ie. some other dri_image_format in
72 * the table, of the same precision but with different channel ordering, or
73 * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported.
74 * The code checks if alt_dri_image_format can be used as a fallback for a
75 * dri_image_format for a given wl-server implementation.
77 int alt_dri_image_format
;
80 unsigned int rgba_sizes
[4];
81 } dri2_wl_visuals
[] = {
84 WL_DRM_FORMAT_ABGR16F
, WL_SHM_FORMAT_ABGR16161616F
,
85 __DRI_IMAGE_FORMAT_ABGR16161616F
, 0, 64,
91 WL_DRM_FORMAT_XBGR16F
, WL_SHM_FORMAT_XBGR16161616F
,
92 __DRI_IMAGE_FORMAT_XBGR16161616F
, 0, 64,
98 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
99 __DRI_IMAGE_FORMAT_XRGB2101010
, __DRI_IMAGE_FORMAT_XBGR2101010
, 32,
105 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
106 __DRI_IMAGE_FORMAT_ARGB2101010
, __DRI_IMAGE_FORMAT_ABGR2101010
, 32,
112 WL_DRM_FORMAT_XBGR2101010
, WL_SHM_FORMAT_XBGR2101010
,
113 __DRI_IMAGE_FORMAT_XBGR2101010
, __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
119 WL_DRM_FORMAT_ABGR2101010
, WL_SHM_FORMAT_ABGR2101010
,
120 __DRI_IMAGE_FORMAT_ABGR2101010
, __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
126 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
127 __DRI_IMAGE_FORMAT_XRGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
133 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
134 __DRI_IMAGE_FORMAT_ARGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
140 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
141 __DRI_IMAGE_FORMAT_RGB565
, __DRI_IMAGE_FORMAT_NONE
, 16,
147 static_assert(ARRAY_SIZE(dri2_wl_visuals
) <= EGL_DRI2_MAX_FORMATS
,
148 "dri2_egl_display::formats is not large enough for "
149 "the formats in dri2_wl_visuals");
152 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
153 const __DRIconfig
*config
)
156 unsigned int sizes
[4];
158 dri2_get_shifts_and_sizes(dri2_dpy
->core
, config
, shifts
, sizes
);
160 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
161 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
163 if (shifts
[0] == wl_visual
->rgba_shifts
[0] &&
164 shifts
[1] == wl_visual
->rgba_shifts
[1] &&
165 shifts
[2] == wl_visual
->rgba_shifts
[2] &&
166 shifts
[3] == wl_visual
->rgba_shifts
[3] &&
167 sizes
[0] == wl_visual
->rgba_sizes
[0] &&
168 sizes
[1] == wl_visual
->rgba_sizes
[1] &&
169 sizes
[2] == wl_visual
->rgba_sizes
[2] &&
170 sizes
[3] == wl_visual
->rgba_sizes
[3]) {
179 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
181 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
182 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
184 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
192 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
194 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
195 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
203 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
205 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
206 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
214 dri2_wl_is_format_supported(void* user_data
, uint32_t format
)
216 _EGLDisplay
*disp
= (_EGLDisplay
*) user_data
;
217 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
218 int j
= dri2_wl_visual_idx_from_fourcc(format
);
223 for (int i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
224 if (j
== dri2_wl_visual_idx_from_config(dri2_dpy
,
225 dri2_dpy
->driver_configs
[i
]))
232 roundtrip(struct dri2_egl_display
*dri2_dpy
)
234 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
238 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
240 struct dri2_egl_surface
*dri2_surf
= data
;
243 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
244 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
247 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
249 if (dri2_surf
->color_buffers
[i
].wl_release
) {
250 wl_buffer_destroy(buffer
);
251 dri2_surf
->color_buffers
[i
].wl_release
= false;
252 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
255 dri2_surf
->color_buffers
[i
].locked
= false;
258 static const struct wl_buffer_listener wl_buffer_listener
= {
259 .release
= wl_buffer_release
263 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
265 struct dri2_egl_surface
*dri2_surf
= data
;
266 struct dri2_egl_display
*dri2_dpy
=
267 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
269 /* Update the surface size as soon as native window is resized; from user
270 * pov, this makes the effect that resize is done immediately after native
271 * window resize, without requiring to wait until the first draw.
273 * A more detailed and lengthy explanation can be found at
274 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
276 if (!dri2_surf
->back
) {
277 dri2_surf
->base
.Width
= wl_win
->width
;
278 dri2_surf
->base
.Height
= wl_win
->height
;
280 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
284 destroy_window_callback(void *data
)
286 struct dri2_egl_surface
*dri2_surf
= data
;
287 dri2_surf
->wl_win
= NULL
;
290 static struct wl_surface
*
291 get_wl_surface_proxy(struct wl_egl_window
*window
)
293 /* Version 3 of wl_egl_window introduced a version field at the same
294 * location where a pointer to wl_surface was stored. Thus, if
295 * window->version is dereferenceable, we've been given an older version of
296 * wl_egl_window, and window->version points to wl_surface */
297 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
298 return wl_proxy_create_wrapper((void *)(window
->version
));
300 return wl_proxy_create_wrapper(window
->surface
);
304 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
307 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
308 _EGLConfig
*conf
, void *native_window
,
309 const EGLint
*attrib_list
)
311 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
312 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
313 struct wl_egl_window
*window
= native_window
;
314 struct dri2_egl_surface
*dri2_surf
;
316 const __DRIconfig
*config
;
318 dri2_surf
= calloc(1, sizeof *dri2_surf
);
320 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
324 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
325 attrib_list
, false, native_window
))
328 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
329 dri2_surf
->base
.GLColorspace
);
332 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
336 dri2_surf
->base
.Width
= window
->width
;
337 dri2_surf
->base
.Height
= window
->height
;
339 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
340 assert(visual_idx
!= -1);
342 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
343 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
345 assert(dri2_dpy
->wl_shm
);
346 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
349 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
350 if (!dri2_surf
->wl_queue
) {
351 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
355 if (dri2_dpy
->wl_drm
) {
356 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
357 if (!dri2_surf
->wl_drm_wrapper
) {
358 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
361 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
362 dri2_surf
->wl_queue
);
365 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
366 if (!dri2_surf
->wl_dpy_wrapper
) {
367 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
370 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
371 dri2_surf
->wl_queue
);
373 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
374 if (!dri2_surf
->wl_surface_wrapper
) {
375 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
376 goto cleanup_dpy_wrapper
;
378 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
379 dri2_surf
->wl_queue
);
381 dri2_surf
->wl_win
= window
;
382 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
383 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
385 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
387 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
388 goto cleanup_surf_wrapper
;
390 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
392 return &dri2_surf
->base
;
394 cleanup_surf_wrapper
:
395 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
397 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
399 if (dri2_surf
->wl_drm_wrapper
)
400 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
402 wl_event_queue_destroy(dri2_surf
->wl_queue
);
410 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
411 _EGLConfig
*conf
, void *native_window
,
412 const EGLint
*attrib_list
)
414 /* From the EGL_EXT_platform_wayland spec, version 3:
416 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
417 * that belongs to Wayland. Any such call fails and generates
420 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
426 * Called via eglDestroySurface(), drv->API.DestroySurface().
429 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
431 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
432 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
436 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
438 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
439 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
440 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
441 if (dri2_surf
->color_buffers
[i
].dri_image
)
442 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
443 if (dri2_surf
->color_buffers
[i
].linear_copy
)
444 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
445 if (dri2_surf
->color_buffers
[i
].data
)
446 munmap(dri2_surf
->color_buffers
[i
].data
,
447 dri2_surf
->color_buffers
[i
].data_size
);
451 dri2_egl_surface_free_local_buffers(dri2_surf
);
453 if (dri2_surf
->throttle_callback
)
454 wl_callback_destroy(dri2_surf
->throttle_callback
);
456 if (dri2_surf
->wl_win
) {
457 dri2_surf
->wl_win
->driver_private
= NULL
;
458 dri2_surf
->wl_win
->resize_callback
= NULL
;
459 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
462 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
463 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
464 if (dri2_surf
->wl_drm_wrapper
)
465 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
466 wl_event_queue_destroy(dri2_surf
->wl_queue
);
468 dri2_fini_surface(surf
);
475 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
477 struct dri2_egl_display
*dri2_dpy
=
478 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
480 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
481 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
482 if (dri2_surf
->color_buffers
[i
].locked
) {
483 dri2_surf
->color_buffers
[i
].wl_release
= true;
485 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
486 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
489 if (dri2_surf
->color_buffers
[i
].dri_image
)
490 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
491 if (dri2_surf
->color_buffers
[i
].linear_copy
)
492 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
493 if (dri2_surf
->color_buffers
[i
].data
)
494 munmap(dri2_surf
->color_buffers
[i
].data
,
495 dri2_surf
->color_buffers
[i
].data_size
);
497 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
498 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
499 dri2_surf
->color_buffers
[i
].data
= NULL
;
503 dri2_egl_surface_free_local_buffers(dri2_surf
);
507 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
509 struct dri2_egl_display
*dri2_dpy
=
510 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
513 unsigned int dri_image_format
;
514 unsigned int linear_dri_image_format
;
518 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
519 assert(visual_idx
!= -1);
520 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
521 linear_dri_image_format
= dri_image_format
;
522 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
523 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
525 /* Substitute dri image format if server does not support original format */
526 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
527 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
529 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
530 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
533 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
534 assert(BITSET_TEST(dri2_dpy
->formats
,
535 dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
537 /* There might be a buffer release already queued that wasn't processed */
538 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
540 while (dri2_surf
->back
== NULL
) {
541 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
542 /* Get an unlocked buffer, preferably one with a dri_buffer
543 * already allocated. */
544 if (dri2_surf
->color_buffers
[i
].locked
)
546 if (dri2_surf
->back
== NULL
)
547 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
548 else if (dri2_surf
->back
->dri_image
== NULL
)
549 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
555 /* If we don't have a buffer, then block on the server to release one for
556 * us, and try again. wl_display_dispatch_queue will process any pending
557 * events, however not all servers flush on issuing a buffer release
558 * event. So, we spam the server with roundtrips as they always cause a
561 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
562 dri2_surf
->wl_queue
) < 0)
566 if (dri2_surf
->back
== NULL
)
569 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
571 if (dri2_dpy
->is_different_gpu
&&
572 dri2_surf
->back
->linear_copy
== NULL
) {
573 /* The LINEAR modifier should be a perfect alias of the LINEAR use
574 * flag; try the new interface first before the old, then fall back. */
575 if (dri2_dpy
->image
->base
.version
>= 15 &&
576 dri2_dpy
->image
->createImageWithModifiers
) {
577 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
579 dri2_surf
->back
->linear_copy
=
580 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
581 dri2_surf
->base
.Width
,
582 dri2_surf
->base
.Height
,
583 linear_dri_image_format
,
588 dri2_surf
->back
->linear_copy
=
589 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
590 dri2_surf
->base
.Width
,
591 dri2_surf
->base
.Height
,
592 linear_dri_image_format
,
594 __DRI_IMAGE_USE_LINEAR
,
597 if (dri2_surf
->back
->linear_copy
== NULL
)
601 if (dri2_surf
->back
->dri_image
== NULL
) {
602 /* If our DRIImage implementation does not support
603 * createImageWithModifiers, then fall back to the old createImage,
604 * and hope it allocates an image which is acceptable to the winsys.
606 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
607 dri2_dpy
->image
->createImageWithModifiers
) {
608 dri2_surf
->back
->dri_image
=
609 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
610 dri2_surf
->base
.Width
,
611 dri2_surf
->base
.Height
,
617 dri2_surf
->back
->dri_image
=
618 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
619 dri2_surf
->base
.Width
,
620 dri2_surf
->base
.Height
,
622 dri2_dpy
->is_different_gpu
?
627 dri2_surf
->back
->age
= 0;
629 if (dri2_surf
->back
->dri_image
== NULL
)
632 dri2_surf
->back
->locked
= true;
639 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
641 struct dri2_egl_display
*dri2_dpy
=
642 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
646 image
= dri2_surf
->back
->dri_image
;
648 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
649 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
651 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
653 buffer
->pitch
= pitch
;
659 update_buffers(struct dri2_egl_surface
*dri2_surf
)
661 struct dri2_egl_display
*dri2_dpy
=
662 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
664 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
665 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
667 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
668 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
669 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
670 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
673 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
674 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
675 dri2_wl_release_buffers(dri2_surf
);
678 if (get_back_bo(dri2_surf
) < 0) {
679 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
683 /* If we have an extra unlocked buffer at this point, we had to do triple
684 * buffering for a while, but now can go back to just double buffering.
685 * That means we can free any unlocked buffer now. */
686 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
687 if (!dri2_surf
->color_buffers
[i
].locked
&&
688 dri2_surf
->color_buffers
[i
].wl_buffer
) {
689 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
690 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
691 if (dri2_dpy
->is_different_gpu
)
692 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
693 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
694 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
695 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
703 update_buffers_if_needed(struct dri2_egl_surface
*dri2_surf
)
705 if (dri2_surf
->back
!= NULL
)
708 return update_buffers(dri2_surf
);
712 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
713 int *width
, int *height
,
714 unsigned int *attachments
, int count
,
715 int *out_count
, void *loaderPrivate
)
717 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
720 if (update_buffers(dri2_surf
) < 0)
723 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
726 switch (attachments
[i
]) {
727 case __DRI_BUFFER_BACK_LEFT
:
728 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
731 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
735 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
738 dri2_surf
->buffers
[j
] = *local
;
747 *width
= dri2_surf
->base
.Width
;
748 *height
= dri2_surf
->base
.Height
;
750 return dri2_surf
->buffers
;
754 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
755 int *width
, int *height
,
756 unsigned int *attachments
, int count
,
757 int *out_count
, void *loaderPrivate
)
759 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
760 unsigned int *attachments_with_format
;
762 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
764 if (visual_idx
== -1)
767 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
768 if (!attachments_with_format
) {
773 for (int i
= 0; i
< count
; ++i
) {
774 attachments_with_format
[2*i
] = attachments
[i
];
775 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
779 dri2_wl_get_buffers_with_format(driDrawable
,
781 attachments_with_format
, count
,
782 out_count
, loaderPrivate
);
784 free(attachments_with_format
);
790 image_get_buffers(__DRIdrawable
*driDrawable
,
794 uint32_t buffer_mask
,
795 struct __DRIimageList
*buffers
)
797 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
799 if (update_buffers(dri2_surf
) < 0)
802 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
803 buffers
->back
= dri2_surf
->back
->dri_image
;
809 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
812 (void) loaderPrivate
;
816 dri2_wl_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
819 case DRI_LOADER_CAP_FP16
:
826 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
827 .base
= { __DRI_DRI2_LOADER
, 4 },
829 .getBuffers
= dri2_wl_get_buffers
,
830 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
831 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
832 .getCapability
= dri2_wl_get_capability
,
835 static const __DRIimageLoaderExtension image_loader_extension
= {
836 .base
= { __DRI_IMAGE_LOADER
, 2 },
838 .getBuffers
= image_get_buffers
,
839 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
840 .getCapability
= dri2_wl_get_capability
,
844 wayland_throttle_callback(void *data
,
845 struct wl_callback
*callback
,
848 struct dri2_egl_surface
*dri2_surf
= data
;
850 dri2_surf
->throttle_callback
= NULL
;
851 wl_callback_destroy(callback
);
854 static const struct wl_callback_listener throttle_listener
= {
855 .done
= wayland_throttle_callback
859 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
860 __DRIimage
*image
, int *fourcc
)
866 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
871 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
876 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
877 if (visual_idx
== -1)
880 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
884 static struct wl_buffer
*
885 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
886 struct dri2_egl_surface
*dri2_surf
,
889 struct wl_buffer
*ret
;
891 int width
, height
, fourcc
, num_planes
;
892 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
894 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
895 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
897 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
901 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
906 if (dri2_dpy
->image
->base
.version
>= 15) {
909 query
= dri2_dpy
->image
->queryImage(image
,
910 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
912 query
&= dri2_dpy
->image
->queryImage(image
,
913 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
916 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
920 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
921 struct zwp_linux_buffer_params_v1
*params
;
924 /* We don't need a wrapper for wl_dmabuf objects, because we have to
925 * create the intermediate params object; we can set the queue on this,
926 * and the wl_buffer inherits it race-free. */
927 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
929 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
931 for (i
= 0; i
< num_planes
; i
++) {
936 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
942 query
= dri2_dpy
->image
->queryImage(p_image
,
943 __DRI_IMAGE_ATTRIB_FD
,
945 query
&= dri2_dpy
->image
->queryImage(p_image
,
946 __DRI_IMAGE_ATTRIB_STRIDE
,
948 query
&= dri2_dpy
->image
->queryImage(p_image
,
949 __DRI_IMAGE_ATTRIB_OFFSET
,
951 if (image
!= p_image
)
952 dri2_dpy
->image
->destroyImage(p_image
);
957 zwp_linux_buffer_params_v1_destroy(params
);
961 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
962 modifier
>> 32, modifier
& 0xffffffff);
966 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
968 zwp_linux_buffer_params_v1_destroy(params
);
969 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
970 struct wl_drm
*wl_drm
=
971 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
977 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
978 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
979 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
983 struct wl_drm
*wl_drm
=
984 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
990 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
991 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
992 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
999 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
1000 const EGLint
*rects
,
1003 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
1004 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
1007 for (int i
= 0; i
< n_rects
; i
++) {
1008 const int *rect
= &rects
[i
* 4];
1010 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
1012 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
1018 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
1021 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
1024 const EGLint
*rects
,
1027 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1028 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1030 while (dri2_surf
->throttle_callback
!= NULL
)
1031 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1032 dri2_surf
->wl_queue
) == -1)
1035 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
1036 if (dri2_surf
->color_buffers
[i
].age
> 0)
1037 dri2_surf
->color_buffers
[i
].age
++;
1039 /* Make sure we have a back buffer in case we're swapping without ever
1041 if (update_buffers_if_needed(dri2_surf
) < 0)
1042 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
1044 if (draw
->SwapInterval
> 0) {
1045 dri2_surf
->throttle_callback
=
1046 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1047 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1048 &throttle_listener
, dri2_surf
);
1051 dri2_surf
->back
->age
= 1;
1052 dri2_surf
->current
= dri2_surf
->back
;
1053 dri2_surf
->back
= NULL
;
1055 if (!dri2_surf
->current
->wl_buffer
) {
1058 if (dri2_dpy
->is_different_gpu
)
1059 image
= dri2_surf
->current
->linear_copy
;
1061 image
= dri2_surf
->current
->dri_image
;
1063 dri2_surf
->current
->wl_buffer
=
1064 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1066 dri2_surf
->current
->wl_release
= false;
1068 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1069 &wl_buffer_listener
, dri2_surf
);
1072 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1073 dri2_surf
->current
->wl_buffer
,
1074 dri2_surf
->dx
, dri2_surf
->dy
);
1076 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1077 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1078 /* reset resize growing parameters */
1082 /* If the compositor doesn't support damage_buffer, we deliberately
1083 * ignore the damage region and post maximum damage, due to
1084 * https://bugs.freedesktop.org/78190 */
1085 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1086 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1087 0, 0, INT32_MAX
, INT32_MAX
);
1089 if (dri2_dpy
->is_different_gpu
) {
1090 _EGLContext
*ctx
= _eglGetCurrentContext();
1091 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1092 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1093 dri2_surf
->current
->linear_copy
,
1094 dri2_surf
->current
->dri_image
,
1095 0, 0, dri2_surf
->base
.Width
,
1096 dri2_surf
->base
.Height
,
1097 0, 0, dri2_surf
->base
.Width
,
1098 dri2_surf
->base
.Height
, 0);
1101 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1102 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1104 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1106 /* If we're not waiting for a frame callback then we'll at least throttle
1107 * to a sync callback so that we always give a chance for the compositor to
1108 * handle the commit and send a release event before checking for a free
1110 if (dri2_surf
->throttle_callback
== NULL
) {
1111 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1112 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1113 &throttle_listener
, dri2_surf
);
1116 wl_display_flush(dri2_dpy
->wl_dpy
);
1122 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1123 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1125 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1127 if (update_buffers_if_needed(dri2_surf
) < 0) {
1128 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1132 return dri2_surf
->back
->age
;
1136 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1138 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1141 static struct wl_buffer
*
1142 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1146 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1147 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1148 __DRIimage
*image
= dri2_img
->dri_image
;
1149 struct wl_buffer
*buffer
;
1150 int format
, visual_idx
;
1152 /* Check the upstream display supports this buffer's format. */
1153 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1154 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1155 if (visual_idx
== -1)
1158 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
1161 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1163 /* The buffer object will have been created with our internal event queue
1164 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1165 * buffer to be used by the application so we'll reset it to the display's
1166 * default event queue. This isn't actually racy, as the only event the
1167 * buffer can get is a buffer release, which doesn't happen with an explicit
1170 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1175 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1180 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1182 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1185 if (dri2_dpy
->is_render_node
) {
1186 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1187 "authenticate for render-nodes");
1190 dri2_dpy
->authenticated
= false;
1192 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1193 if (roundtrip(dri2_dpy
) < 0)
1196 if (!dri2_dpy
->authenticated
)
1199 /* reset authenticated */
1200 dri2_dpy
->authenticated
= true;
1206 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1208 struct dri2_egl_display
*dri2_dpy
= data
;
1211 dri2_dpy
->device_name
= strdup(device
);
1212 if (!dri2_dpy
->device_name
)
1215 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1216 if (dri2_dpy
->fd
== -1) {
1217 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1218 dri2_dpy
->device_name
, strerror(errno
));
1219 free(dri2_dpy
->device_name
);
1220 dri2_dpy
->device_name
= NULL
;
1224 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1225 dri2_dpy
->authenticated
= true;
1227 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1228 close(dri2_dpy
->fd
);
1230 free(dri2_dpy
->device_name
);
1231 dri2_dpy
->device_name
= NULL
;
1232 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1235 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1240 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1242 struct dri2_egl_display
*dri2_dpy
= data
;
1243 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1245 if (visual_idx
== -1)
1248 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1252 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1254 struct dri2_egl_display
*dri2_dpy
= data
;
1256 dri2_dpy
->capabilities
= value
;
1260 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1262 struct dri2_egl_display
*dri2_dpy
= data
;
1264 dri2_dpy
->authenticated
= true;
1267 static const struct wl_drm_listener drm_listener
= {
1268 .device
= drm_handle_device
,
1269 .format
= drm_handle_format
,
1270 .authenticated
= drm_handle_authenticated
,
1271 .capabilities
= drm_handle_capabilities
1275 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1278 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1282 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1283 uint32_t format
, uint32_t modifier_hi
,
1284 uint32_t modifier_lo
)
1286 struct dri2_egl_display
*dri2_dpy
= data
;
1287 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1290 if (visual_idx
== -1)
1293 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1294 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1297 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1299 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1300 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1303 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1304 .format
= dmabuf_ignore_format
,
1305 .modifier
= dmabuf_handle_modifier
,
1309 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1310 uint32_t name
, const char *interface
,
1313 struct dri2_egl_display
*dri2_dpy
= data
;
1315 if (strcmp(interface
, "wl_drm") == 0) {
1317 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1318 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1319 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1320 dri2_dpy
->wl_dmabuf
=
1321 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1323 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1329 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1334 static const struct wl_registry_listener registry_listener_drm
= {
1335 .global
= registry_handle_global_drm
,
1336 .global_remove
= registry_handle_global_remove
1340 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1342 /* We can't use values greater than 1 on Wayland because we are using the
1343 * frame callback to synchronise the frame and the only way we be sure to
1344 * get a frame callback is to attach a new buffer. Therefore we can't just
1345 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1347 dri2_setup_swap_interval(disp
, 1);
1350 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1351 .authenticate
= dri2_wl_authenticate
,
1352 .create_window_surface
= dri2_wl_create_window_surface
,
1353 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1354 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1355 .destroy_surface
= dri2_wl_destroy_surface
,
1356 .create_image
= dri2_create_image_khr
,
1357 .swap_buffers
= dri2_wl_swap_buffers
,
1358 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1359 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1360 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1361 .copy_buffers
= dri2_fallback_copy_buffers
,
1362 .query_buffer_age
= dri2_wl_query_buffer_age
,
1363 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1364 .get_sync_values
= dri2_fallback_get_sync_values
,
1365 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1368 static const __DRIextension
*dri2_loader_extensions
[] = {
1369 &dri2_loader_extension
.base
,
1370 &image_loader_extension
.base
,
1371 &image_lookup_extension
.base
,
1372 &use_invalidate
.base
,
1376 static const __DRIextension
*image_loader_extensions
[] = {
1377 &image_loader_extension
.base
,
1378 &image_lookup_extension
.base
,
1379 &use_invalidate
.base
,
1384 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1386 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1387 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1388 unsigned int count
= 0;
1391 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1394 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1395 struct dri2_egl_config
*dri2_conf
;
1397 if (!BITSET_TEST(dri2_dpy
->formats
, j
))
1400 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1401 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_shifts
, dri2_wl_visuals
[j
].rgba_sizes
);
1403 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1410 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1411 struct dri2_egl_config
*dri2_conf
;
1412 int alt_dri_image_format
, c
, s
;
1414 /* No match for config. Try if we can blitImage convert to a visual */
1415 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1416 dri2_dpy
->driver_configs
[i
]);
1421 /* Find optimal target visual for blitImage conversion, if any. */
1422 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1423 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1425 if (s
== -1 || !BITSET_TEST(dri2_dpy
->formats
, s
))
1428 /* Visual s works for the Wayland server, and c can be converted into s
1429 * by our client gpu during PRIME blitImage conversion to a linear
1430 * wl_buffer, so add visual c as supported by the client renderer.
1432 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1433 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1434 dri2_wl_visuals
[c
].rgba_shifts
,
1435 dri2_wl_visuals
[c
].rgba_sizes
);
1437 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1440 if (format_count
[c
] == 1)
1441 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1442 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1443 dri2_wl_visuals
[s
].format_name
);
1448 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1449 if (!format_count
[i
]) {
1450 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1451 dri2_wl_visuals
[i
].format_name
);
1455 return (count
!= 0);
1459 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1462 struct dri2_egl_display
*dri2_dpy
;
1464 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1466 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1469 disp
->DriverData
= (void *) dri2_dpy
;
1470 if (disp
->PlatformDisplay
== NULL
) {
1471 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1472 if (dri2_dpy
->wl_dpy
== NULL
)
1474 dri2_dpy
->own_device
= true;
1476 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1479 dri2_dpy
->wl_modifiers
=
1480 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1481 if (!dri2_dpy
->wl_modifiers
)
1483 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1484 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1488 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1490 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1491 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1494 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1495 dri2_dpy
->wl_queue
);
1497 if (dri2_dpy
->own_device
)
1498 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1500 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1501 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1502 ®istry_listener_drm
, dri2_dpy
);
1503 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1506 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1509 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1512 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1513 &dri2_dpy
->is_different_gpu
);
1514 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1516 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1522 if (dri2_dpy
->is_different_gpu
) {
1523 free(dri2_dpy
->device_name
);
1524 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1525 if (!dri2_dpy
->device_name
) {
1526 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1527 "for requested GPU");
1532 /* we have to do the check now, because loader_get_user_preferred_fd
1533 * will return a render-node when the requested gpu is different
1534 * to the server, but also if the client asks for the same gpu than
1535 * the server by requesting its pci-id */
1536 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1538 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1539 if (dri2_dpy
->driver_name
== NULL
) {
1540 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1544 /* render nodes cannot use Gem names, and thus do not support
1545 * the __DRI_DRI2_LOADER extension */
1546 if (!dri2_dpy
->is_render_node
) {
1547 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1548 if (!dri2_load_driver(disp
)) {
1549 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1553 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1554 if (!dri2_load_driver_dri3(disp
)) {
1555 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1560 if (!dri2_create_screen(disp
))
1563 if (!dri2_setup_extensions(disp
))
1566 dri2_setup_screen(disp
);
1568 dri2_wl_setup_swap_interval(disp
);
1570 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1571 * createImageFromFds support indicates that Prime export/import
1572 * is supported by the driver. Fall back to
1573 * gem names if we don't have Prime support. */
1575 if (dri2_dpy
->image
->base
.version
< 7 ||
1576 dri2_dpy
->image
->createImageFromFds
== NULL
)
1577 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1579 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1580 * The server needs to accept them */
1581 if (dri2_dpy
->is_render_node
&&
1582 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1583 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1587 if (dri2_dpy
->is_different_gpu
&&
1588 (dri2_dpy
->image
->base
.version
< 9 ||
1589 dri2_dpy
->image
->blitImage
== NULL
)) {
1590 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1591 "Image extension in the driver is not "
1592 "compatible. Version 9 or later and blitImage() "
1597 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1598 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1602 dri2_set_WL_bind_wayland_display(drv
, disp
);
1603 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1604 * because the buffer of the EGLImage has likely a tiling mode the server
1605 * gpu won't support. These is no way to check for now. Thus do not support the
1607 if (!dri2_dpy
->is_different_gpu
)
1608 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1610 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1612 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1614 /* Fill vtbl last to prevent accidentally calling virtual function during
1617 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1622 dri2_display_destroy(disp
);
1627 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1629 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1631 assume(visual_idx
!= -1);
1633 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1637 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1638 int format
, int w
, int h
,
1639 void **data
, int *size
,
1640 struct wl_buffer
**buffer
)
1642 struct dri2_egl_display
*dri2_dpy
=
1643 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1644 struct wl_shm_pool
*pool
;
1645 int fd
, stride
, size_map
;
1648 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1649 size_map
= h
* stride
;
1651 /* Create a shareable buffer */
1652 fd
= os_create_anonymous_file(size_map
, NULL
);
1656 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1657 if (data_map
== MAP_FAILED
) {
1662 /* Share it in a wl_buffer */
1663 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1664 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1665 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1666 wl_shm_pool_destroy(pool
);
1675 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1677 struct dri2_egl_display
*dri2_dpy
=
1678 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1680 /* we need to do the following operations only once per frame */
1681 if (dri2_surf
->back
)
1684 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1685 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1687 dri2_wl_release_buffers(dri2_surf
);
1689 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1690 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1691 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1692 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1693 dri2_surf
->current
= NULL
;
1696 /* find back buffer */
1698 /* There might be a buffer release already queued that wasn't processed */
1699 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1701 /* try get free buffer already created */
1702 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1703 if (!dri2_surf
->color_buffers
[i
].locked
&&
1704 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1705 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1710 /* else choose any another free location */
1711 if (!dri2_surf
->back
) {
1712 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1713 if (!dri2_surf
->color_buffers
[i
].locked
) {
1714 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1715 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1717 dri2_surf
->base
.Width
,
1718 dri2_surf
->base
.Height
,
1719 &dri2_surf
->back
->data
,
1720 &dri2_surf
->back
->data_size
,
1721 &dri2_surf
->back
->wl_buffer
)) {
1722 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1725 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1726 &wl_buffer_listener
, dri2_surf
);
1732 if (!dri2_surf
->back
) {
1733 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1737 dri2_surf
->back
->locked
= true;
1739 /* If we have an extra unlocked buffer at this point, we had to do triple
1740 * buffering for a while, but now can go back to just double buffering.
1741 * That means we can free any unlocked buffer now. */
1742 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1743 if (!dri2_surf
->color_buffers
[i
].locked
&&
1744 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1745 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1746 munmap(dri2_surf
->color_buffers
[i
].data
,
1747 dri2_surf
->color_buffers
[i
].data_size
);
1748 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1749 dri2_surf
->color_buffers
[i
].data
= NULL
;
1757 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1759 /* if there has been a resize: */
1760 if (!dri2_surf
->current
)
1763 return dri2_surf
->current
->data
;
1767 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1769 assert(dri2_surf
->back
);
1770 return dri2_surf
->back
->data
;
1774 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1776 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1778 while (dri2_surf
->throttle_callback
!= NULL
)
1779 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1780 dri2_surf
->wl_queue
) == -1)
1783 if (dri2_surf
->base
.SwapInterval
> 0) {
1784 dri2_surf
->throttle_callback
=
1785 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1786 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1787 &throttle_listener
, dri2_surf
);
1790 dri2_surf
->current
= dri2_surf
->back
;
1791 dri2_surf
->back
= NULL
;
1793 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1794 dri2_surf
->current
->wl_buffer
,
1795 dri2_surf
->dx
, dri2_surf
->dy
);
1797 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1798 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1799 /* reset resize growing parameters */
1803 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1804 0, 0, INT32_MAX
, INT32_MAX
);
1805 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1807 /* If we're not waiting for a frame callback then we'll at least throttle
1808 * to a sync callback so that we always give a chance for the compositor to
1809 * handle the commit and send a release event before checking for a free
1811 if (dri2_surf
->throttle_callback
== NULL
) {
1812 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1813 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1814 &throttle_listener
, dri2_surf
);
1817 wl_display_flush(dri2_dpy
->wl_dpy
);
1821 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1822 int *x
, int *y
, int *w
, int *h
,
1823 void *loaderPrivate
)
1825 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1827 (void) swrast_update_buffers(dri2_surf
);
1830 *w
= dri2_surf
->base
.Width
;
1831 *h
= dri2_surf
->base
.Height
;
1835 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1836 int x
, int y
, int w
, int h
,
1837 char *data
, void *loaderPrivate
)
1839 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1840 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1841 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1842 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1843 int dst_stride
= copy_width
;
1846 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1848 memset(data
, 0, copy_width
* h
);
1852 assert(data
!= src
);
1853 assert(copy_width
<= src_stride
);
1856 src
+= y
* src_stride
;
1859 if (copy_width
> src_stride
-x_offset
)
1860 copy_width
= src_stride
-x_offset
;
1861 if (h
> dri2_surf
->base
.Height
-y
)
1862 h
= dri2_surf
->base
.Height
-y
;
1865 memcpy(dst
, src
, copy_width
);
1872 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1873 int x
, int y
, int w
, int h
, int stride
,
1874 char *data
, void *loaderPrivate
)
1876 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1877 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1878 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1879 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1882 assert(copy_width
<= stride
);
1884 (void) swrast_update_buffers(dri2_surf
);
1885 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1887 /* partial copy, copy old content */
1888 if (copy_width
< dst_stride
)
1889 dri2_wl_swrast_get_image(draw
, 0, 0,
1890 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1891 dst
, loaderPrivate
);
1894 dst
+= y
* dst_stride
;
1898 /* drivers expect we do these checks (and some rely on it) */
1899 if (copy_width
> dst_stride
-x_offset
)
1900 copy_width
= dst_stride
-x_offset
;
1901 if (h
> dri2_surf
->base
.Height
-y
)
1902 h
= dri2_surf
->base
.Height
-y
;
1905 memcpy(dst
, src
, copy_width
);
1909 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1913 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1914 int x
, int y
, int w
, int h
,
1915 char *data
, void *loaderPrivate
)
1917 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1920 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1921 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1922 stride
, data
, loaderPrivate
);
1926 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1928 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1929 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1931 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1936 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1938 struct dri2_egl_display
*dri2_dpy
= data
;
1939 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1941 if (visual_idx
== -1)
1944 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1947 static const struct wl_shm_listener shm_listener
= {
1948 .format
= shm_handle_format
1952 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1953 uint32_t name
, const char *interface
,
1956 struct dri2_egl_display
*dri2_dpy
= data
;
1958 if (strcmp(interface
, "wl_shm") == 0) {
1960 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1961 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1965 static const struct wl_registry_listener registry_listener_swrast
= {
1966 .global
= registry_handle_global_swrast
,
1967 .global_remove
= registry_handle_global_remove
1970 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1971 .authenticate
= NULL
,
1972 .create_window_surface
= dri2_wl_create_window_surface
,
1973 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1974 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1975 .destroy_surface
= dri2_wl_destroy_surface
,
1976 .create_image
= dri2_create_image_khr
,
1977 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1978 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1979 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1980 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1981 .copy_buffers
= dri2_fallback_copy_buffers
,
1982 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1983 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1984 .get_sync_values
= dri2_fallback_get_sync_values
,
1985 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1988 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1989 .base
= { __DRI_SWRAST_LOADER
, 2 },
1991 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1992 .putImage
= dri2_wl_swrast_put_image
,
1993 .getImage
= dri2_wl_swrast_get_image
,
1994 .putImage2
= dri2_wl_swrast_put_image2
,
1997 static const __DRIextension
*swrast_loader_extensions
[] = {
1998 &swrast_loader_extension
.base
,
1999 &image_lookup_extension
.base
,
2004 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2007 struct dri2_egl_display
*dri2_dpy
;
2009 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2011 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2014 disp
->DriverData
= (void *) dri2_dpy
;
2015 if (disp
->PlatformDisplay
== NULL
) {
2016 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2017 if (dri2_dpy
->wl_dpy
== NULL
)
2019 dri2_dpy
->own_device
= true;
2021 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2024 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
2026 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
2032 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2034 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2035 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2038 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2039 dri2_dpy
->wl_queue
);
2041 if (dri2_dpy
->own_device
)
2042 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2044 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2045 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2046 ®istry_listener_swrast
, dri2_dpy
);
2048 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2051 if (roundtrip(dri2_dpy
) < 0 || !BITSET_TEST_RANGE(dri2_dpy
->formats
,
2052 0, EGL_DRI2_MAX_FORMATS
))
2055 dri2_dpy
->driver_name
= strdup("swrast");
2056 if (!dri2_load_driver_swrast(disp
))
2059 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2061 if (!dri2_create_screen(disp
))
2064 if (!dri2_setup_extensions(disp
))
2067 dri2_setup_screen(disp
);
2069 dri2_wl_setup_swap_interval(disp
);
2071 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2072 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2076 /* Fill vtbl last to prevent accidentally calling virtual function during
2079 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2084 dri2_display_destroy(disp
);
2089 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2091 EGLBoolean initialized
= EGL_FALSE
;
2093 if (!disp
->Options
.ForceSoftware
)
2094 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2097 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2104 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2106 if (dri2_dpy
->wl_drm
)
2107 wl_drm_destroy(dri2_dpy
->wl_drm
);
2108 if (dri2_dpy
->wl_dmabuf
)
2109 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2110 if (dri2_dpy
->wl_shm
)
2111 wl_shm_destroy(dri2_dpy
->wl_shm
);
2112 if (dri2_dpy
->wl_registry
)
2113 wl_registry_destroy(dri2_dpy
->wl_registry
);
2114 if (dri2_dpy
->wl_queue
)
2115 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2116 if (dri2_dpy
->wl_dpy_wrapper
)
2117 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2119 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2120 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2121 free(dri2_dpy
->wl_modifiers
);
2123 if (dri2_dpy
->own_device
)
2124 wl_display_disconnect(dri2_dpy
->wl_dpy
);