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_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 roundtrip(struct dri2_egl_display
*dri2_dpy
)
180 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
184 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
186 struct dri2_egl_surface
*dri2_surf
= data
;
189 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
190 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
193 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
195 if (dri2_surf
->color_buffers
[i
].wl_release
) {
196 wl_buffer_destroy(buffer
);
197 dri2_surf
->color_buffers
[i
].wl_release
= false;
198 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
201 dri2_surf
->color_buffers
[i
].locked
= false;
204 static const struct wl_buffer_listener wl_buffer_listener
= {
205 .release
= wl_buffer_release
209 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
211 struct dri2_egl_surface
*dri2_surf
= data
;
212 struct dri2_egl_display
*dri2_dpy
=
213 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
215 /* Update the surface size as soon as native window is resized; from user
216 * pov, this makes the effect that resize is done immediately after native
217 * window resize, without requiring to wait until the first draw.
219 * A more detailed and lengthy explanation can be found at
220 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
222 if (!dri2_surf
->back
) {
223 dri2_surf
->base
.Width
= wl_win
->width
;
224 dri2_surf
->base
.Height
= wl_win
->height
;
226 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
230 destroy_window_callback(void *data
)
232 struct dri2_egl_surface
*dri2_surf
= data
;
233 dri2_surf
->wl_win
= NULL
;
236 static struct wl_surface
*
237 get_wl_surface_proxy(struct wl_egl_window
*window
)
239 /* Version 3 of wl_egl_window introduced a version field at the same
240 * location where a pointer to wl_surface was stored. Thus, if
241 * window->version is dereferenceable, we've been given an older version of
242 * wl_egl_window, and window->version points to wl_surface */
243 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
244 return wl_proxy_create_wrapper((void *)(window
->version
));
246 return wl_proxy_create_wrapper(window
->surface
);
250 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
253 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
254 _EGLConfig
*conf
, void *native_window
,
255 const EGLint
*attrib_list
)
257 __DRIcreateNewDrawableFunc createNewDrawable
;
258 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
259 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
260 struct wl_egl_window
*window
= native_window
;
261 struct dri2_egl_surface
*dri2_surf
;
263 const __DRIconfig
*config
;
265 dri2_surf
= calloc(1, sizeof *dri2_surf
);
267 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
271 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
275 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
276 dri2_surf
->base
.GLColorspace
);
279 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
283 dri2_surf
->base
.Width
= window
->width
;
284 dri2_surf
->base
.Height
= window
->height
;
286 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
287 assert(visual_idx
!= -1);
289 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
290 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
292 assert(dri2_dpy
->wl_shm
);
293 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
296 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
297 if (!dri2_surf
->wl_queue
) {
298 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
302 if (dri2_dpy
->wl_drm
) {
303 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
304 if (!dri2_surf
->wl_drm_wrapper
) {
305 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
308 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
309 dri2_surf
->wl_queue
);
312 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
313 if (!dri2_surf
->wl_dpy_wrapper
) {
314 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
317 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
318 dri2_surf
->wl_queue
);
320 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
321 if (!dri2_surf
->wl_surface_wrapper
) {
322 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
323 goto cleanup_dpy_wrapper
;
325 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
326 dri2_surf
->wl_queue
);
328 dri2_surf
->wl_win
= window
;
329 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
330 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
332 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
334 if (dri2_dpy
->image_driver
)
335 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
336 else if (dri2_dpy
->dri2
)
337 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
339 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
341 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
343 if (dri2_surf
->dri_drawable
== NULL
) {
344 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
345 goto cleanup_surf_wrapper
;
348 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
350 return &dri2_surf
->base
;
352 cleanup_surf_wrapper
:
353 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
355 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
357 if (dri2_surf
->wl_drm_wrapper
)
358 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
360 wl_event_queue_destroy(dri2_surf
->wl_queue
);
368 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
369 _EGLConfig
*conf
, void *native_window
,
370 const EGLint
*attrib_list
)
372 /* From the EGL_EXT_platform_wayland spec, version 3:
374 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
375 * that belongs to Wayland. Any such call fails and generates
378 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
384 * Called via eglDestroySurface(), drv->API.DestroySurface().
387 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
389 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
390 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
394 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
396 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
397 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
398 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
399 if (dri2_surf
->color_buffers
[i
].dri_image
)
400 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
401 if (dri2_surf
->color_buffers
[i
].linear_copy
)
402 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
403 if (dri2_surf
->color_buffers
[i
].data
)
404 munmap(dri2_surf
->color_buffers
[i
].data
,
405 dri2_surf
->color_buffers
[i
].data_size
);
409 dri2_egl_surface_free_local_buffers(dri2_surf
);
411 if (dri2_surf
->throttle_callback
)
412 wl_callback_destroy(dri2_surf
->throttle_callback
);
414 if (dri2_surf
->wl_win
) {
415 dri2_surf
->wl_win
->driver_private
= NULL
;
416 dri2_surf
->wl_win
->resize_callback
= NULL
;
417 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
420 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
421 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
422 if (dri2_surf
->wl_drm_wrapper
)
423 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
424 wl_event_queue_destroy(dri2_surf
->wl_queue
);
426 dri2_fini_surface(surf
);
433 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
435 struct dri2_egl_display
*dri2_dpy
=
436 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
438 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
439 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
440 if (dri2_surf
->color_buffers
[i
].locked
) {
441 dri2_surf
->color_buffers
[i
].wl_release
= true;
443 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
444 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
447 if (dri2_surf
->color_buffers
[i
].dri_image
)
448 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
449 if (dri2_surf
->color_buffers
[i
].linear_copy
)
450 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
451 if (dri2_surf
->color_buffers
[i
].data
)
452 munmap(dri2_surf
->color_buffers
[i
].data
,
453 dri2_surf
->color_buffers
[i
].data_size
);
455 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
456 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
457 dri2_surf
->color_buffers
[i
].data
= NULL
;
461 dri2_egl_surface_free_local_buffers(dri2_surf
);
465 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
467 struct dri2_egl_display
*dri2_dpy
=
468 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
471 unsigned int dri_image_format
;
472 unsigned int linear_dri_image_format
;
476 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
477 assert(visual_idx
!= -1);
478 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
479 linear_dri_image_format
= dri_image_format
;
480 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
481 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
483 /* Substitute dri image format if server does not support original format */
484 if (!(dri2_dpy
->formats
& (1 << visual_idx
)))
485 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
487 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
488 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
491 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
492 assert(dri2_dpy
->formats
&
493 (1 << dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
495 /* There might be a buffer release already queued that wasn't processed */
496 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
498 while (dri2_surf
->back
== NULL
) {
499 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
500 /* Get an unlocked buffer, preferably one with a dri_buffer
501 * already allocated. */
502 if (dri2_surf
->color_buffers
[i
].locked
)
504 if (dri2_surf
->back
== NULL
)
505 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
506 else if (dri2_surf
->back
->dri_image
== NULL
)
507 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
513 /* If we don't have a buffer, then block on the server to release one for
514 * us, and try again. wl_display_dispatch_queue will process any pending
515 * events, however not all servers flush on issuing a buffer release
516 * event. So, we spam the server with roundtrips as they always cause a
519 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
520 dri2_surf
->wl_queue
) < 0)
524 if (dri2_surf
->back
== NULL
)
527 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
529 if (dri2_dpy
->is_different_gpu
&&
530 dri2_surf
->back
->linear_copy
== NULL
) {
531 /* The LINEAR modifier should be a perfect alias of the LINEAR use
532 * flag; try the new interface first before the old, then fall back. */
533 if (dri2_dpy
->image
->base
.version
>= 15 &&
534 dri2_dpy
->image
->createImageWithModifiers
) {
535 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
537 dri2_surf
->back
->linear_copy
=
538 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
539 dri2_surf
->base
.Width
,
540 dri2_surf
->base
.Height
,
541 linear_dri_image_format
,
546 dri2_surf
->back
->linear_copy
=
547 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
548 dri2_surf
->base
.Width
,
549 dri2_surf
->base
.Height
,
550 linear_dri_image_format
,
552 __DRI_IMAGE_USE_LINEAR
,
555 if (dri2_surf
->back
->linear_copy
== NULL
)
559 if (dri2_surf
->back
->dri_image
== NULL
) {
560 /* If our DRIImage implementation does not support
561 * createImageWithModifiers, then fall back to the old createImage,
562 * and hope it allocates an image which is acceptable to the winsys.
564 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
565 dri2_dpy
->image
->createImageWithModifiers
) {
566 dri2_surf
->back
->dri_image
=
567 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
568 dri2_surf
->base
.Width
,
569 dri2_surf
->base
.Height
,
575 dri2_surf
->back
->dri_image
=
576 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
577 dri2_surf
->base
.Width
,
578 dri2_surf
->base
.Height
,
580 dri2_dpy
->is_different_gpu
?
585 dri2_surf
->back
->age
= 0;
587 if (dri2_surf
->back
->dri_image
== NULL
)
590 dri2_surf
->back
->locked
= true;
597 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
599 struct dri2_egl_display
*dri2_dpy
=
600 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
604 image
= dri2_surf
->back
->dri_image
;
606 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
607 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
609 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
611 buffer
->pitch
= pitch
;
617 update_buffers(struct dri2_egl_surface
*dri2_surf
)
619 struct dri2_egl_display
*dri2_dpy
=
620 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
622 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
623 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
625 dri2_wl_release_buffers(dri2_surf
);
627 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
628 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
629 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
630 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
633 if (get_back_bo(dri2_surf
) < 0) {
634 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
638 /* If we have an extra unlocked buffer at this point, we had to do triple
639 * buffering for a while, but now can go back to just double buffering.
640 * That means we can free any unlocked buffer now. */
641 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
642 if (!dri2_surf
->color_buffers
[i
].locked
&&
643 dri2_surf
->color_buffers
[i
].wl_buffer
) {
644 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
645 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
646 if (dri2_dpy
->is_different_gpu
)
647 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
648 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
649 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
650 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
658 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
659 int *width
, int *height
,
660 unsigned int *attachments
, int count
,
661 int *out_count
, void *loaderPrivate
)
663 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
666 if (update_buffers(dri2_surf
) < 0)
669 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
672 switch (attachments
[i
]) {
673 case __DRI_BUFFER_BACK_LEFT
:
674 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
677 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
681 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
684 dri2_surf
->buffers
[j
] = *local
;
693 *width
= dri2_surf
->base
.Width
;
694 *height
= dri2_surf
->base
.Height
;
696 return dri2_surf
->buffers
;
700 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
701 int *width
, int *height
,
702 unsigned int *attachments
, int count
,
703 int *out_count
, void *loaderPrivate
)
705 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
706 unsigned int *attachments_with_format
;
708 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
710 if (visual_idx
== -1)
713 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
714 if (!attachments_with_format
) {
719 for (int i
= 0; i
< count
; ++i
) {
720 attachments_with_format
[2*i
] = attachments
[i
];
721 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
725 dri2_wl_get_buffers_with_format(driDrawable
,
727 attachments_with_format
, count
,
728 out_count
, loaderPrivate
);
730 free(attachments_with_format
);
736 image_get_buffers(__DRIdrawable
*driDrawable
,
740 uint32_t buffer_mask
,
741 struct __DRIimageList
*buffers
)
743 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
745 if (update_buffers(dri2_surf
) < 0)
748 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
749 buffers
->back
= dri2_surf
->back
->dri_image
;
755 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
758 (void) loaderPrivate
;
761 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
762 .base
= { __DRI_DRI2_LOADER
, 3 },
764 .getBuffers
= dri2_wl_get_buffers
,
765 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
766 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
769 static const __DRIimageLoaderExtension image_loader_extension
= {
770 .base
= { __DRI_IMAGE_LOADER
, 1 },
772 .getBuffers
= image_get_buffers
,
773 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
777 wayland_throttle_callback(void *data
,
778 struct wl_callback
*callback
,
781 struct dri2_egl_surface
*dri2_surf
= data
;
783 dri2_surf
->throttle_callback
= NULL
;
784 wl_callback_destroy(callback
);
787 static const struct wl_callback_listener throttle_listener
= {
788 .done
= wayland_throttle_callback
792 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
793 __DRIimage
*image
, int *fourcc
)
799 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
804 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
809 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
810 if (visual_idx
== -1)
813 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
817 static struct wl_buffer
*
818 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
819 struct dri2_egl_surface
*dri2_surf
,
822 struct wl_buffer
*ret
;
824 int width
, height
, fourcc
, num_planes
;
825 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
827 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
828 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
830 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
834 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
839 if (dri2_dpy
->image
->base
.version
>= 15) {
842 query
= dri2_dpy
->image
->queryImage(image
,
843 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
845 query
&= dri2_dpy
->image
->queryImage(image
,
846 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
849 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
853 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
854 struct zwp_linux_buffer_params_v1
*params
;
857 /* We don't need a wrapper for wl_dmabuf objects, because we have to
858 * create the intermediate params object; we can set the queue on this,
859 * and the wl_buffer inherits it race-free. */
860 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
862 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
864 for (i
= 0; i
< num_planes
; i
++) {
869 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
875 query
= dri2_dpy
->image
->queryImage(p_image
,
876 __DRI_IMAGE_ATTRIB_FD
,
878 query
&= dri2_dpy
->image
->queryImage(p_image
,
879 __DRI_IMAGE_ATTRIB_STRIDE
,
881 query
&= dri2_dpy
->image
->queryImage(p_image
,
882 __DRI_IMAGE_ATTRIB_OFFSET
,
884 if (image
!= p_image
)
885 dri2_dpy
->image
->destroyImage(p_image
);
890 zwp_linux_buffer_params_v1_destroy(params
);
894 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
895 modifier
>> 32, modifier
& 0xffffffff);
899 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
901 zwp_linux_buffer_params_v1_destroy(params
);
902 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
903 struct wl_drm
*wl_drm
=
904 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
910 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
911 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
912 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
916 struct wl_drm
*wl_drm
=
917 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
923 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
924 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
925 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
932 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
936 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
937 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
940 for (int i
= 0; i
< n_rects
; i
++) {
941 const int *rect
= &rects
[i
* 4];
943 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
945 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
951 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
954 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
960 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
961 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
963 while (dri2_surf
->throttle_callback
!= NULL
)
964 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
965 dri2_surf
->wl_queue
) == -1)
968 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
969 if (dri2_surf
->color_buffers
[i
].age
> 0)
970 dri2_surf
->color_buffers
[i
].age
++;
972 /* Make sure we have a back buffer in case we're swapping without ever
974 if (get_back_bo(dri2_surf
) < 0)
975 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
977 if (draw
->SwapInterval
> 0) {
978 dri2_surf
->throttle_callback
=
979 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
980 wl_callback_add_listener(dri2_surf
->throttle_callback
,
981 &throttle_listener
, dri2_surf
);
984 dri2_surf
->back
->age
= 1;
985 dri2_surf
->current
= dri2_surf
->back
;
986 dri2_surf
->back
= NULL
;
988 if (!dri2_surf
->current
->wl_buffer
) {
991 if (dri2_dpy
->is_different_gpu
)
992 image
= dri2_surf
->current
->linear_copy
;
994 image
= dri2_surf
->current
->dri_image
;
996 dri2_surf
->current
->wl_buffer
=
997 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
999 dri2_surf
->current
->wl_release
= false;
1001 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1002 &wl_buffer_listener
, dri2_surf
);
1005 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1006 dri2_surf
->current
->wl_buffer
,
1007 dri2_surf
->dx
, dri2_surf
->dy
);
1009 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1010 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1011 /* reset resize growing parameters */
1015 /* If the compositor doesn't support damage_buffer, we deliberately
1016 * ignore the damage region and post maximum damage, due to
1017 * https://bugs.freedesktop.org/78190 */
1018 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1019 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1020 0, 0, INT32_MAX
, INT32_MAX
);
1022 if (dri2_dpy
->is_different_gpu
) {
1023 _EGLContext
*ctx
= _eglGetCurrentContext();
1024 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1025 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1026 dri2_surf
->current
->linear_copy
,
1027 dri2_surf
->current
->dri_image
,
1028 0, 0, dri2_surf
->base
.Width
,
1029 dri2_surf
->base
.Height
,
1030 0, 0, dri2_surf
->base
.Width
,
1031 dri2_surf
->base
.Height
, 0);
1034 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1035 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1037 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1039 /* If we're not waiting for a frame callback then we'll at least throttle
1040 * to a sync callback so that we always give a chance for the compositor to
1041 * handle the commit and send a release event before checking for a free
1043 if (dri2_surf
->throttle_callback
== NULL
) {
1044 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1045 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1046 &throttle_listener
, dri2_surf
);
1049 wl_display_flush(dri2_dpy
->wl_dpy
);
1055 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1056 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1058 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1060 if (get_back_bo(dri2_surf
) < 0) {
1061 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1065 return dri2_surf
->back
->age
;
1069 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1071 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1074 static struct wl_buffer
*
1075 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1079 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1080 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1081 __DRIimage
*image
= dri2_img
->dri_image
;
1082 struct wl_buffer
*buffer
;
1083 int format
, visual_idx
;
1085 /* Check the upstream display supports this buffer's format. */
1086 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1087 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1088 if (visual_idx
== -1)
1091 if (!(dri2_dpy
->formats
& (1u << visual_idx
)))
1094 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1096 /* The buffer object will have been created with our internal event queue
1097 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1098 * buffer to be used by the application so we'll reset it to the display's
1099 * default event queue. This isn't actually racy, as the only event the
1100 * buffer can get is a buffer release, which doesn't happen with an explicit
1103 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1108 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1113 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1115 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1118 if (dri2_dpy
->is_render_node
) {
1119 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1120 "authenticate for render-nodes");
1123 dri2_dpy
->authenticated
= false;
1125 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1126 if (roundtrip(dri2_dpy
) < 0)
1129 if (!dri2_dpy
->authenticated
)
1132 /* reset authenticated */
1133 dri2_dpy
->authenticated
= true;
1139 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1141 struct dri2_egl_display
*dri2_dpy
= data
;
1144 dri2_dpy
->device_name
= strdup(device
);
1145 if (!dri2_dpy
->device_name
)
1148 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1149 if (dri2_dpy
->fd
== -1) {
1150 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1151 dri2_dpy
->device_name
, strerror(errno
));
1152 free(dri2_dpy
->device_name
);
1153 dri2_dpy
->device_name
= NULL
;
1157 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1158 dri2_dpy
->authenticated
= true;
1160 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1161 close(dri2_dpy
->fd
);
1163 free(dri2_dpy
->device_name
);
1164 dri2_dpy
->device_name
= NULL
;
1165 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1168 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1173 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1175 struct dri2_egl_display
*dri2_dpy
= data
;
1176 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1178 if (visual_idx
== -1)
1181 dri2_dpy
->formats
|= (1u << visual_idx
);
1185 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1187 struct dri2_egl_display
*dri2_dpy
= data
;
1189 dri2_dpy
->capabilities
= value
;
1193 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1195 struct dri2_egl_display
*dri2_dpy
= data
;
1197 dri2_dpy
->authenticated
= true;
1200 static const struct wl_drm_listener drm_listener
= {
1201 .device
= drm_handle_device
,
1202 .format
= drm_handle_format
,
1203 .authenticated
= drm_handle_authenticated
,
1204 .capabilities
= drm_handle_capabilities
1208 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1211 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1215 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1216 uint32_t format
, uint32_t modifier_hi
,
1217 uint32_t modifier_lo
)
1219 struct dri2_egl_display
*dri2_dpy
= data
;
1220 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1223 if (visual_idx
== -1)
1226 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1227 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1230 dri2_dpy
->formats
|= (1u << visual_idx
);
1232 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1233 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1236 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1237 .format
= dmabuf_ignore_format
,
1238 .modifier
= dmabuf_handle_modifier
,
1242 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1243 uint32_t name
, const char *interface
,
1246 struct dri2_egl_display
*dri2_dpy
= data
;
1248 if (strcmp(interface
, "wl_drm") == 0) {
1250 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1251 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1252 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1253 dri2_dpy
->wl_dmabuf
=
1254 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1256 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1262 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1267 static const struct wl_registry_listener registry_listener_drm
= {
1268 .global
= registry_handle_global_drm
,
1269 .global_remove
= registry_handle_global_remove
1273 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1275 /* We can't use values greater than 1 on Wayland because we are using the
1276 * frame callback to synchronise the frame and the only way we be sure to
1277 * get a frame callback is to attach a new buffer. Therefore we can't just
1278 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1280 dri2_setup_swap_interval(disp
, 1);
1283 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1284 .authenticate
= dri2_wl_authenticate
,
1285 .create_window_surface
= dri2_wl_create_window_surface
,
1286 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1287 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1288 .destroy_surface
= dri2_wl_destroy_surface
,
1289 .create_image
= dri2_create_image_khr
,
1290 .swap_buffers
= dri2_wl_swap_buffers
,
1291 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1292 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1293 .set_damage_region
= dri2_fallback_set_damage_region
,
1294 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1295 .copy_buffers
= dri2_fallback_copy_buffers
,
1296 .query_buffer_age
= dri2_wl_query_buffer_age
,
1297 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1298 .get_sync_values
= dri2_fallback_get_sync_values
,
1299 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1302 static const __DRIextension
*dri2_loader_extensions
[] = {
1303 &dri2_loader_extension
.base
,
1304 &image_loader_extension
.base
,
1305 &image_lookup_extension
.base
,
1306 &use_invalidate
.base
,
1310 static const __DRIextension
*image_loader_extensions
[] = {
1311 &image_loader_extension
.base
,
1312 &image_lookup_extension
.base
,
1313 &use_invalidate
.base
,
1318 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1320 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1321 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1322 unsigned int count
= 0;
1325 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1328 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1329 struct dri2_egl_config
*dri2_conf
;
1331 if (!(dri2_dpy
->formats
& (1u << j
)))
1334 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1335 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1337 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1344 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1345 struct dri2_egl_config
*dri2_conf
;
1346 int alt_dri_image_format
, c
, s
;
1348 /* No match for config. Try if we can blitImage convert to a visual */
1349 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1350 dri2_dpy
->driver_configs
[i
]);
1355 /* Find optimal target visual for blitImage conversion, if any. */
1356 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1357 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1359 if (s
== -1 || !(dri2_dpy
->formats
& (1 << s
)))
1362 /* Visual s works for the Wayland server, and c can be converted into s
1363 * by our client gpu during PRIME blitImage conversion to a linear
1364 * wl_buffer, so add visual c as supported by the client renderer.
1366 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1367 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1368 dri2_wl_visuals
[c
].rgba_masks
);
1370 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1373 if (format_count
[c
] == 1)
1374 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1375 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1376 dri2_wl_visuals
[s
].format_name
);
1381 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1382 if (!format_count
[i
]) {
1383 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1384 dri2_wl_visuals
[i
].format_name
);
1388 return (count
!= 0);
1392 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1395 struct dri2_egl_display
*dri2_dpy
;
1397 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1399 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1402 disp
->DriverData
= (void *) dri2_dpy
;
1403 if (disp
->PlatformDisplay
== NULL
) {
1404 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1405 if (dri2_dpy
->wl_dpy
== NULL
)
1407 dri2_dpy
->own_device
= true;
1409 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1412 dri2_dpy
->wl_modifiers
=
1413 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1414 if (!dri2_dpy
->wl_modifiers
)
1416 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1417 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1421 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1423 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1424 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1427 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1428 dri2_dpy
->wl_queue
);
1430 if (dri2_dpy
->own_device
)
1431 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1433 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1434 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1435 ®istry_listener_drm
, dri2_dpy
);
1436 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1439 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1442 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1445 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1446 &dri2_dpy
->is_different_gpu
);
1447 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1449 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1455 if (dri2_dpy
->is_different_gpu
) {
1456 free(dri2_dpy
->device_name
);
1457 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1458 if (!dri2_dpy
->device_name
) {
1459 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1460 "for requested GPU");
1465 /* we have to do the check now, because loader_get_user_preferred_fd
1466 * will return a render-node when the requested gpu is different
1467 * to the server, but also if the client asks for the same gpu than
1468 * the server by requesting its pci-id */
1469 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1471 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1472 if (dri2_dpy
->driver_name
== NULL
) {
1473 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1477 /* render nodes cannot use Gem names, and thus do not support
1478 * the __DRI_DRI2_LOADER extension */
1479 if (!dri2_dpy
->is_render_node
) {
1480 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1481 if (!dri2_load_driver(disp
)) {
1482 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1486 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1487 if (!dri2_load_driver_dri3(disp
)) {
1488 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1493 if (!dri2_create_screen(disp
))
1496 if (!dri2_setup_extensions(disp
))
1499 dri2_setup_screen(disp
);
1501 dri2_wl_setup_swap_interval(disp
);
1503 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1504 * createImageFromFds support indicates that Prime export/import
1505 * is supported by the driver. Fall back to
1506 * gem names if we don't have Prime support. */
1508 if (dri2_dpy
->image
->base
.version
< 7 ||
1509 dri2_dpy
->image
->createImageFromFds
== NULL
)
1510 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1512 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1513 * The server needs to accept them */
1514 if (dri2_dpy
->is_render_node
&&
1515 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1516 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1520 if (dri2_dpy
->is_different_gpu
&&
1521 (dri2_dpy
->image
->base
.version
< 9 ||
1522 dri2_dpy
->image
->blitImage
== NULL
)) {
1523 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1524 "Image extension in the driver is not "
1525 "compatible. Version 9 or later and blitImage() "
1530 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1531 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1535 dri2_set_WL_bind_wayland_display(drv
, disp
);
1536 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1537 * because the buffer of the EGLImage has likely a tiling mode the server
1538 * gpu won't support. These is no way to check for now. Thus do not support the
1540 if (!dri2_dpy
->is_different_gpu
)
1541 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1543 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1545 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1547 /* Fill vtbl last to prevent accidentally calling virtual function during
1550 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1555 dri2_display_destroy(disp
);
1560 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1562 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1564 assume(visual_idx
!= -1);
1566 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1570 * Taken from weston shared/os-compatibility.c
1573 #ifndef HAVE_MKOSTEMP
1576 set_cloexec_or_close(int fd
)
1583 flags
= fcntl(fd
, F_GETFD
);
1587 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1600 * Taken from weston shared/os-compatibility.c
1604 create_tmpfile_cloexec(char *tmpname
)
1608 #ifdef HAVE_MKOSTEMP
1609 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1613 fd
= mkstemp(tmpname
);
1615 fd
= set_cloexec_or_close(fd
);
1624 * Taken from weston shared/os-compatibility.c
1626 * Create a new, unique, anonymous file of the given size, and
1627 * return the file descriptor for it. The file descriptor is set
1628 * CLOEXEC. The file is immediately suitable for mmap()'ing
1629 * the given size at offset zero.
1631 * The file should not have a permanent backing store like a disk,
1632 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1634 * The file name is deleted from the file system.
1636 * The file is suitable for buffer sharing between processes by
1637 * transmitting the file descriptor over Unix sockets using the
1638 * SCM_RIGHTS methods.
1640 * If the C library implements posix_fallocate(), it is used to
1641 * guarantee that disk space is available for the file at the
1642 * given size. If disk space is insufficient, errno is set to ENOSPC.
1643 * If posix_fallocate() is not supported, program may receive
1644 * SIGBUS on accessing mmap()'ed file contents instead.
1647 os_create_anonymous_file(off_t size
)
1649 static const char templ
[] = "/mesa-shared-XXXXXX";
1655 path
= getenv("XDG_RUNTIME_DIR");
1661 name
= malloc(strlen(path
) + sizeof(templ
));
1666 strcat(name
, templ
);
1668 fd
= create_tmpfile_cloexec(name
);
1675 ret
= ftruncate(fd
, size
);
1686 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1687 int format
, int w
, int h
,
1688 void **data
, int *size
,
1689 struct wl_buffer
**buffer
)
1691 struct dri2_egl_display
*dri2_dpy
=
1692 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1693 struct wl_shm_pool
*pool
;
1694 int fd
, stride
, size_map
;
1697 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1698 size_map
= h
* stride
;
1700 /* Create a shareable buffer */
1701 fd
= os_create_anonymous_file(size_map
);
1705 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1706 if (data_map
== MAP_FAILED
) {
1711 /* Share it in a wl_buffer */
1712 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1713 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1714 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1715 wl_shm_pool_destroy(pool
);
1724 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1726 struct dri2_egl_display
*dri2_dpy
=
1727 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1729 /* we need to do the following operations only once per frame */
1730 if (dri2_surf
->back
)
1733 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1734 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1736 dri2_wl_release_buffers(dri2_surf
);
1738 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1739 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1740 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1741 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1742 dri2_surf
->current
= NULL
;
1745 /* find back buffer */
1747 /* There might be a buffer release already queued that wasn't processed */
1748 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1750 /* try get free buffer already created */
1751 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1752 if (!dri2_surf
->color_buffers
[i
].locked
&&
1753 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1754 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1759 /* else choose any another free location */
1760 if (!dri2_surf
->back
) {
1761 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1762 if (!dri2_surf
->color_buffers
[i
].locked
) {
1763 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1764 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1766 dri2_surf
->base
.Width
,
1767 dri2_surf
->base
.Height
,
1768 &dri2_surf
->back
->data
,
1769 &dri2_surf
->back
->data_size
,
1770 &dri2_surf
->back
->wl_buffer
)) {
1771 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1774 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1775 &wl_buffer_listener
, dri2_surf
);
1781 if (!dri2_surf
->back
) {
1782 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1786 dri2_surf
->back
->locked
= true;
1788 /* If we have an extra unlocked buffer at this point, we had to do triple
1789 * buffering for a while, but now can go back to just double buffering.
1790 * That means we can free any unlocked buffer now. */
1791 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1792 if (!dri2_surf
->color_buffers
[i
].locked
&&
1793 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1794 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1795 munmap(dri2_surf
->color_buffers
[i
].data
,
1796 dri2_surf
->color_buffers
[i
].data_size
);
1797 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1798 dri2_surf
->color_buffers
[i
].data
= NULL
;
1806 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1808 /* if there has been a resize: */
1809 if (!dri2_surf
->current
)
1812 return dri2_surf
->current
->data
;
1816 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1818 assert(dri2_surf
->back
);
1819 return dri2_surf
->back
->data
;
1823 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1825 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1827 while (dri2_surf
->throttle_callback
!= NULL
)
1828 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1829 dri2_surf
->wl_queue
) == -1)
1832 if (dri2_surf
->base
.SwapInterval
> 0) {
1833 dri2_surf
->throttle_callback
=
1834 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1835 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1836 &throttle_listener
, dri2_surf
);
1839 dri2_surf
->current
= dri2_surf
->back
;
1840 dri2_surf
->back
= NULL
;
1842 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1843 dri2_surf
->current
->wl_buffer
,
1844 dri2_surf
->dx
, dri2_surf
->dy
);
1846 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1847 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1848 /* reset resize growing parameters */
1852 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1853 0, 0, INT32_MAX
, INT32_MAX
);
1854 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1856 /* If we're not waiting for a frame callback then we'll at least throttle
1857 * to a sync callback so that we always give a chance for the compositor to
1858 * handle the commit and send a release event before checking for a free
1860 if (dri2_surf
->throttle_callback
== NULL
) {
1861 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1862 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1863 &throttle_listener
, dri2_surf
);
1866 wl_display_flush(dri2_dpy
->wl_dpy
);
1870 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1871 int *x
, int *y
, int *w
, int *h
,
1872 void *loaderPrivate
)
1874 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1876 (void) swrast_update_buffers(dri2_surf
);
1879 *w
= dri2_surf
->base
.Width
;
1880 *h
= dri2_surf
->base
.Height
;
1884 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1885 int x
, int y
, int w
, int h
,
1886 char *data
, void *loaderPrivate
)
1888 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1889 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1890 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1891 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1892 int dst_stride
= copy_width
;
1895 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1897 memset(data
, 0, copy_width
* h
);
1901 assert(data
!= src
);
1902 assert(copy_width
<= src_stride
);
1905 src
+= y
* src_stride
;
1908 if (copy_width
> src_stride
-x_offset
)
1909 copy_width
= src_stride
-x_offset
;
1910 if (h
> dri2_surf
->base
.Height
-y
)
1911 h
= dri2_surf
->base
.Height
-y
;
1914 memcpy(dst
, src
, copy_width
);
1921 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1922 int x
, int y
, int w
, int h
, int stride
,
1923 char *data
, void *loaderPrivate
)
1925 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1926 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1927 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1928 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1931 assert(copy_width
<= stride
);
1933 (void) swrast_update_buffers(dri2_surf
);
1934 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1936 /* partial copy, copy old content */
1937 if (copy_width
< dst_stride
)
1938 dri2_wl_swrast_get_image(draw
, 0, 0,
1939 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1940 dst
, loaderPrivate
);
1943 dst
+= y
* dst_stride
;
1947 /* drivers expect we do these checks (and some rely on it) */
1948 if (copy_width
> dst_stride
-x_offset
)
1949 copy_width
= dst_stride
-x_offset
;
1950 if (h
> dri2_surf
->base
.Height
-y
)
1951 h
= dri2_surf
->base
.Height
-y
;
1954 memcpy(dst
, src
, copy_width
);
1958 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1962 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1963 int x
, int y
, int w
, int h
,
1964 char *data
, void *loaderPrivate
)
1966 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1969 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1970 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1971 stride
, data
, loaderPrivate
);
1975 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1977 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1978 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1980 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1985 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1987 struct dri2_egl_display
*dri2_dpy
= data
;
1988 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1990 if (visual_idx
== -1)
1993 dri2_dpy
->formats
|= (1u << visual_idx
);
1996 static const struct wl_shm_listener shm_listener
= {
1997 .format
= shm_handle_format
2001 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
2002 uint32_t name
, const char *interface
,
2005 struct dri2_egl_display
*dri2_dpy
= data
;
2007 if (strcmp(interface
, "wl_shm") == 0) {
2009 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
2010 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
2014 static const struct wl_registry_listener registry_listener_swrast
= {
2015 .global
= registry_handle_global_swrast
,
2016 .global_remove
= registry_handle_global_remove
2019 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
2020 .authenticate
= NULL
,
2021 .create_window_surface
= dri2_wl_create_window_surface
,
2022 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
2023 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
2024 .destroy_surface
= dri2_wl_destroy_surface
,
2025 .create_image
= dri2_create_image_khr
,
2026 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
2027 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
2028 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
2029 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
2030 .copy_buffers
= dri2_fallback_copy_buffers
,
2031 .query_buffer_age
= dri2_fallback_query_buffer_age
,
2032 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
2033 .get_sync_values
= dri2_fallback_get_sync_values
,
2034 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
2037 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
2038 .base
= { __DRI_SWRAST_LOADER
, 2 },
2040 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
2041 .putImage
= dri2_wl_swrast_put_image
,
2042 .getImage
= dri2_wl_swrast_get_image
,
2043 .putImage2
= dri2_wl_swrast_put_image2
,
2046 static const __DRIextension
*swrast_loader_extensions
[] = {
2047 &swrast_loader_extension
.base
,
2048 &image_lookup_extension
.base
,
2053 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2056 struct dri2_egl_display
*dri2_dpy
;
2058 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2060 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2063 disp
->DriverData
= (void *) dri2_dpy
;
2064 if (disp
->PlatformDisplay
== NULL
) {
2065 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2066 if (dri2_dpy
->wl_dpy
== NULL
)
2068 dri2_dpy
->own_device
= true;
2070 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2073 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
2075 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
2081 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2083 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2084 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2087 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2088 dri2_dpy
->wl_queue
);
2090 if (dri2_dpy
->own_device
)
2091 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2093 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2094 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2095 ®istry_listener_swrast
, dri2_dpy
);
2097 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2100 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
2103 dri2_dpy
->driver_name
= strdup("swrast");
2104 if (!dri2_load_driver_swrast(disp
))
2107 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2109 if (!dri2_create_screen(disp
))
2112 if (!dri2_setup_extensions(disp
))
2115 dri2_setup_screen(disp
);
2117 dri2_wl_setup_swap_interval(disp
);
2119 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2120 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2124 /* Fill vtbl last to prevent accidentally calling virtual function during
2127 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2132 dri2_display_destroy(disp
);
2137 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2139 EGLBoolean initialized
= EGL_FALSE
;
2141 if (!disp
->Options
.ForceSoftware
)
2142 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2145 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2152 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2154 if (dri2_dpy
->wl_drm
)
2155 wl_drm_destroy(dri2_dpy
->wl_drm
);
2156 if (dri2_dpy
->wl_dmabuf
)
2157 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2158 if (dri2_dpy
->wl_shm
)
2159 wl_shm_destroy(dri2_dpy
->wl_shm
);
2160 if (dri2_dpy
->wl_registry
)
2161 wl_registry_destroy(dri2_dpy
->wl_registry
);
2162 if (dri2_dpy
->wl_queue
)
2163 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2164 if (dri2_dpy
->wl_dpy_wrapper
)
2165 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2167 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2168 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2169 free(dri2_dpy
->wl_modifiers
);
2171 if (dri2_dpy
->own_device
)
2172 wl_display_disconnect(dri2_dpy
->wl_dpy
);