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-client.h>
49 #include "wayland-drm-client-protocol.h"
50 #include "linux-dmabuf-unstable-v1-client-protocol.h"
52 #ifndef DRM_FORMAT_MOD_INVALID
53 #define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
56 #ifndef DRM_FORMAT_MOD_LINEAR
57 #define DRM_FORMAT_MOD_LINEAR 0
60 enum wl_drm_format_flags
{
68 static const struct dri2_wl_visual
{
69 const char *format_name
;
70 enum wl_drm_format_flags has_format
;
71 uint32_t wl_drm_format
;
72 uint32_t wl_shm_format
;
75 unsigned int rgba_masks
[4];
76 } dri2_wl_visuals
[] = {
78 "XRGB2101010", HAS_XRGB2101010
,
79 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
80 __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
81 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0x00000000 }
84 "ARGB2101010", HAS_ARGB2101010
,
85 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
86 __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
87 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 }
90 "XRGB8888", HAS_XRGB8888
,
91 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
92 __DRI_IMAGE_FORMAT_XRGB8888
, 32,
93 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }
96 "ARGB8888", HAS_ARGB8888
,
97 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
98 __DRI_IMAGE_FORMAT_ARGB8888
, 32,
99 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }
102 "RGB565", HAS_RGB565
,
103 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
104 __DRI_IMAGE_FORMAT_RGB565
, 16,
105 { 0xf800, 0x07e0, 0x001f, 0x0000 }
110 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
111 const __DRIconfig
*config
)
113 unsigned int red
, green
, blue
, alpha
;
115 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &red
);
116 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &green
);
117 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &blue
);
118 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &alpha
);
120 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
121 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
123 if (red
== wl_visual
->rgba_masks
[0] &&
124 green
== wl_visual
->rgba_masks
[1] &&
125 blue
== wl_visual
->rgba_masks
[2] &&
126 alpha
== wl_visual
->rgba_masks
[3]) {
135 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
137 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
138 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
140 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
148 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
150 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
151 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
159 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
161 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
162 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
170 roundtrip(struct dri2_egl_display
*dri2_dpy
)
172 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
176 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
178 struct dri2_egl_surface
*dri2_surf
= data
;
181 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
182 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
185 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
186 wl_buffer_destroy(buffer
);
190 dri2_surf
->color_buffers
[i
].locked
= false;
193 static const struct wl_buffer_listener wl_buffer_listener
= {
194 .release
= wl_buffer_release
198 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
200 struct dri2_egl_surface
*dri2_surf
= data
;
201 struct dri2_egl_display
*dri2_dpy
=
202 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
204 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
208 destroy_window_callback(void *data
)
210 struct dri2_egl_surface
*dri2_surf
= data
;
211 dri2_surf
->wl_win
= NULL
;
214 static struct wl_surface
*
215 get_wl_surface_proxy(struct wl_egl_window
*window
)
217 /* Version 3 of wl_egl_window introduced a version field at the same
218 * location where a pointer to wl_surface was stored. Thus, if
219 * window->version is dereferencable, we've been given an older version of
220 * wl_egl_window, and window->version points to wl_surface */
221 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
222 return wl_proxy_create_wrapper((void *)(window
->version
));
224 return wl_proxy_create_wrapper(window
->surface
);
228 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
231 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
232 _EGLConfig
*conf
, void *native_window
,
233 const EGLint
*attrib_list
)
235 __DRIcreateNewDrawableFunc createNewDrawable
;
236 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
237 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
238 struct wl_egl_window
*window
= native_window
;
239 struct dri2_egl_surface
*dri2_surf
;
241 const __DRIconfig
*config
;
243 dri2_surf
= calloc(1, sizeof *dri2_surf
);
245 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
249 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
253 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
254 dri2_surf
->base
.GLColorspace
);
255 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
256 assert(visual_idx
!= -1);
258 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
259 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
261 assert(dri2_dpy
->wl_shm
);
262 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
265 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
266 if (!dri2_surf
->wl_queue
) {
267 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
271 if (dri2_dpy
->wl_drm
) {
272 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
273 if (!dri2_surf
->wl_drm_wrapper
) {
274 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
277 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
278 dri2_surf
->wl_queue
);
281 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
282 if (!dri2_surf
->wl_dpy_wrapper
) {
283 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
286 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
287 dri2_surf
->wl_queue
);
289 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
290 if (!dri2_surf
->wl_surface_wrapper
) {
291 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
292 goto cleanup_dpy_wrapper
;
294 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
295 dri2_surf
->wl_queue
);
297 dri2_surf
->wl_win
= window
;
298 dri2_surf
->wl_win
->private = dri2_surf
;
299 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
301 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
303 if (dri2_dpy
->image_driver
)
304 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
305 else if (dri2_dpy
->dri2
)
306 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
308 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
310 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
312 if (dri2_surf
->dri_drawable
== NULL
) {
313 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
314 goto cleanup_surf_wrapper
;
317 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
319 return &dri2_surf
->base
;
321 cleanup_surf_wrapper
:
322 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
324 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
326 if (dri2_surf
->wl_drm_wrapper
)
327 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
329 wl_event_queue_destroy(dri2_surf
->wl_queue
);
337 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
338 _EGLConfig
*conf
, void *native_window
,
339 const EGLint
*attrib_list
)
341 /* From the EGL_EXT_platform_wayland spec, version 3:
343 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
344 * that belongs to Wayland. Any such call fails and generates
347 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
353 * Called via eglDestroySurface(), drv->API.DestroySurface().
356 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
358 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
359 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
363 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
365 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
366 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
367 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
368 if (dri2_surf
->color_buffers
[i
].dri_image
)
369 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
370 if (dri2_surf
->color_buffers
[i
].linear_copy
)
371 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
372 if (dri2_surf
->color_buffers
[i
].data
)
373 munmap(dri2_surf
->color_buffers
[i
].data
,
374 dri2_surf
->color_buffers
[i
].data_size
);
378 dri2_egl_surface_free_local_buffers(dri2_surf
);
380 if (dri2_surf
->throttle_callback
)
381 wl_callback_destroy(dri2_surf
->throttle_callback
);
383 if (dri2_surf
->wl_win
) {
384 dri2_surf
->wl_win
->private = NULL
;
385 dri2_surf
->wl_win
->resize_callback
= NULL
;
386 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
389 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
390 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
391 if (dri2_surf
->wl_drm_wrapper
)
392 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
393 wl_event_queue_destroy(dri2_surf
->wl_queue
);
395 dri2_fini_surface(surf
);
402 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
404 struct dri2_egl_display
*dri2_dpy
=
405 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
407 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
408 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
409 !dri2_surf
->color_buffers
[i
].locked
)
410 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
411 if (dri2_surf
->color_buffers
[i
].dri_image
)
412 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
413 if (dri2_surf
->color_buffers
[i
].linear_copy
)
414 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
415 if (dri2_surf
->color_buffers
[i
].data
)
416 munmap(dri2_surf
->color_buffers
[i
].data
,
417 dri2_surf
->color_buffers
[i
].data_size
);
419 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
420 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
421 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
422 dri2_surf
->color_buffers
[i
].data
= NULL
;
423 dri2_surf
->color_buffers
[i
].locked
= false;
427 dri2_egl_surface_free_local_buffers(dri2_surf
);
431 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
433 struct dri2_egl_display
*dri2_dpy
=
434 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
437 unsigned int dri_image_format
;
441 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
442 assert(visual_idx
!= -1);
443 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
445 /* currently supports five WL DRM formats,
446 * WL_DRM_FORMAT_ARGB2101010, WL_DRM_FORMAT_XRGB2101010,
447 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
448 * and WL_DRM_FORMAT_RGB565
450 switch (dri2_surf
->format
) {
451 case WL_DRM_FORMAT_ARGB2101010
:
452 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb2101010
);
453 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb2101010
);
455 case WL_DRM_FORMAT_XRGB2101010
:
456 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
457 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
459 case WL_DRM_FORMAT_ARGB8888
:
460 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
461 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
463 case WL_DRM_FORMAT_XRGB8888
:
464 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
465 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
467 case WL_DRM_FORMAT_RGB565
:
468 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
469 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
472 /* format is not supported */
476 /* There might be a buffer release already queued that wasn't processed */
477 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
479 while (dri2_surf
->back
== NULL
) {
480 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
481 /* Get an unlocked buffer, preferrably one with a dri_buffer
482 * already allocated. */
483 if (dri2_surf
->color_buffers
[i
].locked
)
485 if (dri2_surf
->back
== NULL
)
486 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
487 else if (dri2_surf
->back
->dri_image
== NULL
)
488 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
494 /* If we don't have a buffer, then block on the server to release one for
495 * us, and try again. wl_display_dispatch_queue will process any pending
496 * events, however not all servers flush on issuing a buffer release
497 * event. So, we spam the server with roundtrips as they always cause a
500 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
501 dri2_surf
->wl_queue
) < 0)
505 if (dri2_surf
->back
== NULL
)
508 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
510 if (dri2_dpy
->is_different_gpu
&&
511 dri2_surf
->back
->linear_copy
== NULL
) {
512 /* The LINEAR modifier should be a perfect alias of the LINEAR use
513 * flag; try the new interface first before the old, then fall back. */
514 if (dri2_dpy
->image
->base
.version
>= 15 &&
515 dri2_dpy
->image
->createImageWithModifiers
) {
516 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
518 dri2_surf
->back
->linear_copy
=
519 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
520 dri2_surf
->base
.Width
,
521 dri2_surf
->base
.Height
,
527 dri2_surf
->back
->linear_copy
=
528 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
529 dri2_surf
->base
.Width
,
530 dri2_surf
->base
.Height
,
533 __DRI_IMAGE_USE_LINEAR
,
536 if (dri2_surf
->back
->linear_copy
== NULL
)
540 if (dri2_surf
->back
->dri_image
== NULL
) {
541 /* If our DRIImage implementation does not support
542 * createImageWithModifiers, then fall back to the old createImage,
543 * and hope it allocates an image which is acceptable to the winsys.
545 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
546 dri2_dpy
->image
->createImageWithModifiers
) {
547 dri2_surf
->back
->dri_image
=
548 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
549 dri2_surf
->base
.Width
,
550 dri2_surf
->base
.Height
,
556 dri2_surf
->back
->dri_image
=
557 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
558 dri2_surf
->base
.Width
,
559 dri2_surf
->base
.Height
,
561 dri2_dpy
->is_different_gpu
?
566 dri2_surf
->back
->age
= 0;
568 if (dri2_surf
->back
->dri_image
== NULL
)
571 dri2_surf
->back
->locked
= true;
578 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
580 struct dri2_egl_display
*dri2_dpy
=
581 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
585 image
= dri2_surf
->back
->dri_image
;
587 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
588 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
590 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
592 buffer
->pitch
= pitch
;
598 update_buffers(struct dri2_egl_surface
*dri2_surf
)
600 struct dri2_egl_display
*dri2_dpy
=
601 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
603 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
604 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
606 dri2_wl_release_buffers(dri2_surf
);
608 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
609 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
610 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
611 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
614 if (get_back_bo(dri2_surf
) < 0) {
615 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
619 /* If we have an extra unlocked buffer at this point, we had to do triple
620 * buffering for a while, but now can go back to just double buffering.
621 * That means we can free any unlocked buffer now. */
622 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
623 if (!dri2_surf
->color_buffers
[i
].locked
&&
624 dri2_surf
->color_buffers
[i
].wl_buffer
) {
625 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
626 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
627 if (dri2_dpy
->is_different_gpu
)
628 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
629 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
630 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
631 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
639 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
640 int *width
, int *height
,
641 unsigned int *attachments
, int count
,
642 int *out_count
, void *loaderPrivate
)
644 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
647 if (update_buffers(dri2_surf
) < 0)
650 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
653 switch (attachments
[i
]) {
654 case __DRI_BUFFER_BACK_LEFT
:
655 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
658 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
662 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
665 dri2_surf
->buffers
[j
] = *local
;
674 *width
= dri2_surf
->base
.Width
;
675 *height
= dri2_surf
->base
.Height
;
677 return dri2_surf
->buffers
;
681 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
682 int *width
, int *height
,
683 unsigned int *attachments
, int count
,
684 int *out_count
, void *loaderPrivate
)
686 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
687 unsigned int *attachments_with_format
;
689 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
691 if (visual_idx
== -1)
694 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
695 if (!attachments_with_format
) {
700 for (int i
= 0; i
< count
; ++i
) {
701 attachments_with_format
[2*i
] = attachments
[i
];
702 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
706 dri2_wl_get_buffers_with_format(driDrawable
,
708 attachments_with_format
, count
,
709 out_count
, loaderPrivate
);
711 free(attachments_with_format
);
717 image_get_buffers(__DRIdrawable
*driDrawable
,
721 uint32_t buffer_mask
,
722 struct __DRIimageList
*buffers
)
724 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
726 if (update_buffers(dri2_surf
) < 0)
729 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
730 buffers
->back
= dri2_surf
->back
->dri_image
;
736 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
739 (void) loaderPrivate
;
742 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
743 .base
= { __DRI_DRI2_LOADER
, 3 },
745 .getBuffers
= dri2_wl_get_buffers
,
746 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
747 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
750 static const __DRIimageLoaderExtension image_loader_extension
= {
751 .base
= { __DRI_IMAGE_LOADER
, 1 },
753 .getBuffers
= image_get_buffers
,
754 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
758 wayland_throttle_callback(void *data
,
759 struct wl_callback
*callback
,
762 struct dri2_egl_surface
*dri2_surf
= data
;
764 dri2_surf
->throttle_callback
= NULL
;
765 wl_callback_destroy(callback
);
768 static const struct wl_callback_listener throttle_listener
= {
769 .done
= wayland_throttle_callback
773 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
774 __DRIimage
*image
, int *fourcc
)
780 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
785 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
790 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
791 if (visual_idx
== -1)
794 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
798 static struct wl_buffer
*
799 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
800 struct dri2_egl_surface
*dri2_surf
,
803 struct wl_buffer
*ret
;
805 int width
, height
, fourcc
, num_planes
;
806 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
808 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
809 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
811 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
815 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
820 if (dri2_dpy
->image
->base
.version
>= 15) {
823 query
= dri2_dpy
->image
->queryImage(image
,
824 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
826 query
&= dri2_dpy
->image
->queryImage(image
,
827 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
830 modifier
= (uint64_t) mod_hi
<< 32;
831 modifier
|= (uint64_t) (mod_lo
& 0xffffffff);
835 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
836 struct zwp_linux_buffer_params_v1
*params
;
839 /* We don't need a wrapper for wl_dmabuf objects, because we have to
840 * create the intermediate params object; we can set the queue on this,
841 * and the wl_buffer inherits it race-free. */
842 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
844 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
846 for (i
= 0; i
< num_planes
; i
++) {
854 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
856 zwp_linux_buffer_params_v1_destroy(params
);
860 query
= dri2_dpy
->image
->queryImage(p_image
,
861 __DRI_IMAGE_ATTRIB_FD
,
863 query
&= dri2_dpy
->image
->queryImage(p_image
,
864 __DRI_IMAGE_ATTRIB_STRIDE
,
866 query
&= dri2_dpy
->image
->queryImage(p_image
,
867 __DRI_IMAGE_ATTRIB_OFFSET
,
869 if (image
!= p_image
)
870 dri2_dpy
->image
->destroyImage(p_image
);
875 zwp_linux_buffer_params_v1_destroy(params
);
879 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
880 modifier
>> 32, modifier
& 0xffffffff);
884 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
886 zwp_linux_buffer_params_v1_destroy(params
);
887 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
888 struct wl_drm
*wl_drm
=
889 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
895 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
896 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
897 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
901 struct wl_drm
*wl_drm
=
902 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
908 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
909 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
910 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
917 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
921 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
922 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
925 for (int i
= 0; i
< n_rects
; i
++) {
926 const int *rect
= &rects
[i
* 4];
928 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
930 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
936 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
939 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
945 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
946 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
948 while (dri2_surf
->throttle_callback
!= NULL
)
949 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
950 dri2_surf
->wl_queue
) == -1)
953 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
954 if (dri2_surf
->color_buffers
[i
].age
> 0)
955 dri2_surf
->color_buffers
[i
].age
++;
957 /* Make sure we have a back buffer in case we're swapping without ever
959 if (get_back_bo(dri2_surf
) < 0)
960 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
962 if (draw
->SwapInterval
> 0) {
963 dri2_surf
->throttle_callback
=
964 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
965 wl_callback_add_listener(dri2_surf
->throttle_callback
,
966 &throttle_listener
, dri2_surf
);
969 dri2_surf
->back
->age
= 1;
970 dri2_surf
->current
= dri2_surf
->back
;
971 dri2_surf
->back
= NULL
;
973 if (!dri2_surf
->current
->wl_buffer
) {
976 if (dri2_dpy
->is_different_gpu
)
977 image
= dri2_surf
->current
->linear_copy
;
979 image
= dri2_surf
->current
->dri_image
;
981 dri2_surf
->current
->wl_buffer
=
982 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
984 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
985 &wl_buffer_listener
, dri2_surf
);
988 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
989 dri2_surf
->current
->wl_buffer
,
990 dri2_surf
->dx
, dri2_surf
->dy
);
992 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
993 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
994 /* reset resize growing parameters */
998 /* If the compositor doesn't support damage_buffer, we deliberately
999 * ignore the damage region and post maximum damage, due to
1000 * https://bugs.freedesktop.org/78190 */
1001 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1002 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1003 0, 0, INT32_MAX
, INT32_MAX
);
1005 if (dri2_dpy
->is_different_gpu
) {
1006 _EGLContext
*ctx
= _eglGetCurrentContext();
1007 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1008 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1009 dri2_surf
->current
->linear_copy
,
1010 dri2_surf
->current
->dri_image
,
1011 0, 0, dri2_surf
->base
.Width
,
1012 dri2_surf
->base
.Height
,
1013 0, 0, dri2_surf
->base
.Width
,
1014 dri2_surf
->base
.Height
, 0);
1017 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1018 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1020 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1022 /* If we're not waiting for a frame callback then we'll at least throttle
1023 * to a sync callback so that we always give a chance for the compositor to
1024 * handle the commit and send a release event before checking for a free
1026 if (dri2_surf
->throttle_callback
== NULL
) {
1027 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1028 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1029 &throttle_listener
, dri2_surf
);
1032 wl_display_flush(dri2_dpy
->wl_dpy
);
1038 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1039 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1041 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1043 if (get_back_bo(dri2_surf
) < 0) {
1044 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1048 return dri2_surf
->back
->age
;
1052 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1054 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1057 static struct wl_buffer
*
1058 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1062 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1063 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1064 __DRIimage
*image
= dri2_img
->dri_image
;
1065 struct wl_buffer
*buffer
;
1066 int format
, visual_idx
;
1068 /* Check the upstream display supports this buffer's format. */
1069 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1070 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1071 if (visual_idx
== -1)
1074 if (!(dri2_dpy
->formats
& dri2_wl_visuals
[visual_idx
].has_format
))
1077 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1079 /* The buffer object will have been created with our internal event queue
1080 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1081 * buffer to be used by the application so we'll reset it to the display's
1082 * default event queue. This isn't actually racy, as the only event the
1083 * buffer can get is a buffer release, which doesn't happen with an explicit
1086 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1091 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1096 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1098 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1101 if (dri2_dpy
->is_render_node
) {
1102 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1103 "authenticate for render-nodes");
1106 dri2_dpy
->authenticated
= false;
1108 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1109 if (roundtrip(dri2_dpy
) < 0)
1112 if (!dri2_dpy
->authenticated
)
1115 /* reset authenticated */
1116 dri2_dpy
->authenticated
= true;
1122 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1124 struct dri2_egl_display
*dri2_dpy
= data
;
1127 dri2_dpy
->device_name
= strdup(device
);
1128 if (!dri2_dpy
->device_name
)
1131 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1132 if (dri2_dpy
->fd
== -1) {
1133 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1134 dri2_dpy
->device_name
, strerror(errno
));
1138 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1139 dri2_dpy
->authenticated
= true;
1141 drmGetMagic(dri2_dpy
->fd
, &magic
);
1142 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1147 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1149 struct dri2_egl_display
*dri2_dpy
= data
;
1150 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1152 if (visual_idx
== -1)
1155 dri2_dpy
->formats
|= dri2_wl_visuals
[visual_idx
].has_format
;
1159 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1161 struct dri2_egl_display
*dri2_dpy
= data
;
1163 dri2_dpy
->capabilities
= value
;
1167 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1169 struct dri2_egl_display
*dri2_dpy
= data
;
1171 dri2_dpy
->authenticated
= true;
1174 static const struct wl_drm_listener drm_listener
= {
1175 .device
= drm_handle_device
,
1176 .format
= drm_handle_format
,
1177 .authenticated
= drm_handle_authenticated
,
1178 .capabilities
= drm_handle_capabilities
1182 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1185 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1189 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1190 uint32_t format
, uint32_t modifier_hi
,
1191 uint32_t modifier_lo
)
1193 struct dri2_egl_display
*dri2_dpy
= data
;
1194 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1195 uint64_t *mod
= NULL
;
1197 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1198 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1202 case WL_DRM_FORMAT_ARGB2101010
:
1203 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb2101010
);
1205 case WL_DRM_FORMAT_XRGB2101010
:
1206 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
1208 case WL_DRM_FORMAT_ARGB8888
:
1209 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1211 case WL_DRM_FORMAT_XRGB8888
:
1212 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1214 case WL_DRM_FORMAT_RGB565
:
1215 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1224 dri2_dpy
->formats
|= dri2_wl_visuals
[visual_idx
].has_format
;
1225 *mod
= (uint64_t) modifier_hi
<< 32;
1226 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1229 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1230 .format
= dmabuf_ignore_format
,
1231 .modifier
= dmabuf_handle_modifier
,
1235 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1236 uint32_t name
, const char *interface
,
1239 struct dri2_egl_display
*dri2_dpy
= data
;
1241 if (strcmp(interface
, "wl_drm") == 0) {
1243 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1244 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1245 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1246 dri2_dpy
->wl_dmabuf
=
1247 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1249 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1255 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1260 static const struct wl_registry_listener registry_listener_drm
= {
1261 .global
= registry_handle_global_drm
,
1262 .global_remove
= registry_handle_global_remove
1266 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1268 /* We can't use values greater than 1 on Wayland because we are using the
1269 * frame callback to synchronise the frame and the only way we be sure to
1270 * get a frame callback is to attach a new buffer. Therefore we can't just
1271 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1273 dri2_setup_swap_interval(disp
, 1);
1276 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1277 .authenticate
= dri2_wl_authenticate
,
1278 .create_window_surface
= dri2_wl_create_window_surface
,
1279 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1280 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1281 .destroy_surface
= dri2_wl_destroy_surface
,
1282 .create_image
= dri2_create_image_khr
,
1283 .swap_buffers
= dri2_wl_swap_buffers
,
1284 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1285 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1286 .set_damage_region
= dri2_fallback_set_damage_region
,
1287 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1288 .copy_buffers
= dri2_fallback_copy_buffers
,
1289 .query_buffer_age
= dri2_wl_query_buffer_age
,
1290 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1291 .get_sync_values
= dri2_fallback_get_sync_values
,
1292 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1295 static const __DRIextension
*dri2_loader_extensions
[] = {
1296 &dri2_loader_extension
.base
,
1297 &image_loader_extension
.base
,
1298 &image_lookup_extension
.base
,
1299 &use_invalidate
.base
,
1303 static const __DRIextension
*image_loader_extensions
[] = {
1304 &image_loader_extension
.base
,
1305 &image_lookup_extension
.base
,
1306 &use_invalidate
.base
,
1311 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1313 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1314 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1315 unsigned int count
= 0;
1317 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1318 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1319 struct dri2_egl_config
*dri2_conf
;
1321 if (!(dri2_dpy
->formats
& dri2_wl_visuals
[j
].has_format
))
1324 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1325 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1327 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1334 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1335 if (!format_count
[i
]) {
1336 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1337 dri2_wl_visuals
[i
].format_name
);
1341 return (count
!= 0);
1345 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1347 struct dri2_egl_display
*dri2_dpy
;
1349 loader_set_logger(_eglLog
);
1351 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1353 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1356 disp
->DriverData
= (void *) dri2_dpy
;
1357 if (disp
->PlatformDisplay
== NULL
) {
1358 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1359 if (dri2_dpy
->wl_dpy
== NULL
)
1361 dri2_dpy
->own_device
= true;
1363 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1366 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb2101010
, sizeof(uint64_t), 32) ||
1367 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb2101010
, sizeof(uint64_t), 32) ||
1368 !u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1369 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1370 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1374 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1376 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1377 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1380 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1381 dri2_dpy
->wl_queue
);
1383 if (dri2_dpy
->own_device
)
1384 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1386 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1387 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1388 ®istry_listener_drm
, dri2_dpy
);
1389 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1392 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1395 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1398 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1399 &dri2_dpy
->is_different_gpu
);
1400 if (dri2_dpy
->is_different_gpu
) {
1401 free(dri2_dpy
->device_name
);
1402 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1403 if (!dri2_dpy
->device_name
) {
1404 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1405 "for requested GPU");
1410 /* we have to do the check now, because loader_get_user_preferred_fd
1411 * will return a render-node when the requested gpu is different
1412 * to the server, but also if the client asks for the same gpu than
1413 * the server by requesting its pci-id */
1414 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1416 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1417 if (dri2_dpy
->driver_name
== NULL
) {
1418 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1422 /* render nodes cannot use Gem names, and thus do not support
1423 * the __DRI_DRI2_LOADER extension */
1424 if (!dri2_dpy
->is_render_node
) {
1425 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1426 if (!dri2_load_driver(disp
)) {
1427 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1431 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1432 if (!dri2_load_driver_dri3(disp
)) {
1433 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1438 if (!dri2_create_screen(disp
))
1441 if (!dri2_setup_extensions(disp
))
1444 dri2_setup_screen(disp
);
1446 dri2_wl_setup_swap_interval(disp
);
1448 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1449 * createImageFromFds support indicates that Prime export/import
1450 * is supported by the driver. Fall back to
1451 * gem names if we don't have Prime support. */
1453 if (dri2_dpy
->image
->base
.version
< 7 ||
1454 dri2_dpy
->image
->createImageFromFds
== NULL
)
1455 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1457 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1458 * The server needs to accept them */
1459 if (dri2_dpy
->is_render_node
&&
1460 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1461 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1465 if (dri2_dpy
->is_different_gpu
&&
1466 (dri2_dpy
->image
->base
.version
< 9 ||
1467 dri2_dpy
->image
->blitImage
== NULL
)) {
1468 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1469 "Image extension in the driver is not "
1470 "compatible. Version 9 or later and blitImage() "
1475 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1476 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1480 dri2_set_WL_bind_wayland_display(drv
, disp
);
1481 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1482 * because the buffer of the EGLImage has likely a tiling mode the server
1483 * gpu won't support. These is no way to check for now. Thus do not support the
1485 if (!dri2_dpy
->is_different_gpu
)
1486 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1488 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1490 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1492 /* Fill vtbl last to prevent accidentally calling virtual function during
1495 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1500 dri2_display_destroy(disp
);
1505 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1507 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1509 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1513 * Taken from weston shared/os-compatibility.c
1516 #ifndef HAVE_MKOSTEMP
1519 set_cloexec_or_close(int fd
)
1526 flags
= fcntl(fd
, F_GETFD
);
1530 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1543 * Taken from weston shared/os-compatibility.c
1547 create_tmpfile_cloexec(char *tmpname
)
1551 #ifdef HAVE_MKOSTEMP
1552 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1556 fd
= mkstemp(tmpname
);
1558 fd
= set_cloexec_or_close(fd
);
1567 * Taken from weston shared/os-compatibility.c
1569 * Create a new, unique, anonymous file of the given size, and
1570 * return the file descriptor for it. The file descriptor is set
1571 * CLOEXEC. The file is immediately suitable for mmap()'ing
1572 * the given size at offset zero.
1574 * The file should not have a permanent backing store like a disk,
1575 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1577 * The file name is deleted from the file system.
1579 * The file is suitable for buffer sharing between processes by
1580 * transmitting the file descriptor over Unix sockets using the
1581 * SCM_RIGHTS methods.
1583 * If the C library implements posix_fallocate(), it is used to
1584 * guarantee that disk space is available for the file at the
1585 * given size. If disk space is insufficent, errno is set to ENOSPC.
1586 * If posix_fallocate() is not supported, program may receive
1587 * SIGBUS on accessing mmap()'ed file contents instead.
1590 os_create_anonymous_file(off_t size
)
1592 static const char templ
[] = "/mesa-shared-XXXXXX";
1598 path
= getenv("XDG_RUNTIME_DIR");
1604 name
= malloc(strlen(path
) + sizeof(templ
));
1609 strcat(name
, templ
);
1611 fd
= create_tmpfile_cloexec(name
);
1618 ret
= ftruncate(fd
, size
);
1629 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1630 int format
, int w
, int h
,
1631 void **data
, int *size
,
1632 struct wl_buffer
**buffer
)
1634 struct dri2_egl_display
*dri2_dpy
=
1635 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1636 struct wl_shm_pool
*pool
;
1637 int fd
, stride
, size_map
;
1640 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1641 size_map
= h
* stride
;
1643 /* Create a sharable buffer */
1644 fd
= os_create_anonymous_file(size_map
);
1648 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1649 if (data_map
== MAP_FAILED
) {
1654 /* Share it in a wl_buffer */
1655 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1656 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1657 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1658 wl_shm_pool_destroy(pool
);
1667 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1669 struct dri2_egl_display
*dri2_dpy
=
1670 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1672 /* we need to do the following operations only once per frame */
1673 if (dri2_surf
->back
)
1676 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1677 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1679 dri2_wl_release_buffers(dri2_surf
);
1681 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1682 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1683 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1684 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1685 dri2_surf
->current
= NULL
;
1688 /* find back buffer */
1690 /* There might be a buffer release already queued that wasn't processed */
1691 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1693 /* try get free buffer already created */
1694 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1695 if (!dri2_surf
->color_buffers
[i
].locked
&&
1696 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1697 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1702 /* else choose any another free location */
1703 if (!dri2_surf
->back
) {
1704 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1705 if (!dri2_surf
->color_buffers
[i
].locked
) {
1706 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1707 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1709 dri2_surf
->base
.Width
,
1710 dri2_surf
->base
.Height
,
1711 &dri2_surf
->back
->data
,
1712 &dri2_surf
->back
->data_size
,
1713 &dri2_surf
->back
->wl_buffer
)) {
1714 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1717 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1718 &wl_buffer_listener
, dri2_surf
);
1724 if (!dri2_surf
->back
) {
1725 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1729 dri2_surf
->back
->locked
= true;
1731 /* If we have an extra unlocked buffer at this point, we had to do triple
1732 * buffering for a while, but now can go back to just double buffering.
1733 * That means we can free any unlocked buffer now. */
1734 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1735 if (!dri2_surf
->color_buffers
[i
].locked
&&
1736 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1737 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1738 munmap(dri2_surf
->color_buffers
[i
].data
,
1739 dri2_surf
->color_buffers
[i
].data_size
);
1740 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1741 dri2_surf
->color_buffers
[i
].data
= NULL
;
1749 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1751 /* if there has been a resize: */
1752 if (!dri2_surf
->current
)
1755 return dri2_surf
->current
->data
;
1759 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1761 assert(dri2_surf
->back
);
1762 return dri2_surf
->back
->data
;
1766 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1768 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1770 while (dri2_surf
->throttle_callback
!= NULL
)
1771 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1772 dri2_surf
->wl_queue
) == -1)
1775 if (dri2_surf
->base
.SwapInterval
> 0) {
1776 dri2_surf
->throttle_callback
=
1777 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1778 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1779 &throttle_listener
, dri2_surf
);
1782 dri2_surf
->current
= dri2_surf
->back
;
1783 dri2_surf
->back
= NULL
;
1785 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1786 dri2_surf
->current
->wl_buffer
,
1787 dri2_surf
->dx
, dri2_surf
->dy
);
1789 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1790 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1791 /* reset resize growing parameters */
1795 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1796 0, 0, INT32_MAX
, INT32_MAX
);
1797 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1799 /* If we're not waiting for a frame callback then we'll at least throttle
1800 * to a sync callback so that we always give a chance for the compositor to
1801 * handle the commit and send a release event before checking for a free
1803 if (dri2_surf
->throttle_callback
== NULL
) {
1804 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1805 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1806 &throttle_listener
, dri2_surf
);
1809 wl_display_flush(dri2_dpy
->wl_dpy
);
1813 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1814 int *x
, int *y
, int *w
, int *h
,
1815 void *loaderPrivate
)
1817 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1819 (void) swrast_update_buffers(dri2_surf
);
1822 *w
= dri2_surf
->base
.Width
;
1823 *h
= dri2_surf
->base
.Height
;
1827 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1828 int x
, int y
, int w
, int h
,
1829 char *data
, void *loaderPrivate
)
1831 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1832 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1833 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1834 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1835 int dst_stride
= copy_width
;
1838 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1840 memset(data
, 0, copy_width
* h
);
1844 assert(data
!= src
);
1845 assert(copy_width
<= src_stride
);
1848 src
+= y
* src_stride
;
1851 if (copy_width
> src_stride
-x_offset
)
1852 copy_width
= src_stride
-x_offset
;
1853 if (h
> dri2_surf
->base
.Height
-y
)
1854 h
= dri2_surf
->base
.Height
-y
;
1857 memcpy(dst
, src
, copy_width
);
1864 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1865 int x
, int y
, int w
, int h
, int stride
,
1866 char *data
, void *loaderPrivate
)
1868 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1869 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1870 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1871 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1874 assert(copy_width
<= stride
);
1876 (void) swrast_update_buffers(dri2_surf
);
1877 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1879 /* partial copy, copy old content */
1880 if (copy_width
< dst_stride
)
1881 dri2_wl_swrast_get_image(draw
, 0, 0,
1882 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1883 dst
, loaderPrivate
);
1886 dst
+= y
* dst_stride
;
1890 /* drivers expect we do these checks (and some rely on it) */
1891 if (copy_width
> dst_stride
-x_offset
)
1892 copy_width
= dst_stride
-x_offset
;
1893 if (h
> dri2_surf
->base
.Height
-y
)
1894 h
= dri2_surf
->base
.Height
-y
;
1897 memcpy(dst
, src
, copy_width
);
1901 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1905 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1906 int x
, int y
, int w
, int h
,
1907 char *data
, void *loaderPrivate
)
1909 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1912 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1913 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1914 stride
, data
, loaderPrivate
);
1918 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1920 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1921 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1923 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1928 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1930 struct dri2_egl_display
*dri2_dpy
= data
;
1931 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1933 if (visual_idx
== -1)
1936 dri2_dpy
->formats
|= dri2_wl_visuals
[visual_idx
].has_format
;
1939 static const struct wl_shm_listener shm_listener
= {
1940 .format
= shm_handle_format
1944 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1945 uint32_t name
, const char *interface
,
1948 struct dri2_egl_display
*dri2_dpy
= data
;
1950 if (strcmp(interface
, "wl_shm") == 0) {
1952 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1953 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1957 static const struct wl_registry_listener registry_listener_swrast
= {
1958 .global
= registry_handle_global_swrast
,
1959 .global_remove
= registry_handle_global_remove
1962 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1963 .authenticate
= NULL
,
1964 .create_window_surface
= dri2_wl_create_window_surface
,
1965 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1966 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1967 .destroy_surface
= dri2_wl_destroy_surface
,
1968 .create_image
= dri2_create_image_khr
,
1969 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1970 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1971 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1972 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1973 .copy_buffers
= dri2_fallback_copy_buffers
,
1974 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1975 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1976 .get_sync_values
= dri2_fallback_get_sync_values
,
1977 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1980 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1981 .base
= { __DRI_SWRAST_LOADER
, 2 },
1983 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1984 .putImage
= dri2_wl_swrast_put_image
,
1985 .getImage
= dri2_wl_swrast_get_image
,
1986 .putImage2
= dri2_wl_swrast_put_image2
,
1989 static const __DRIextension
*swrast_loader_extensions
[] = {
1990 &swrast_loader_extension
.base
,
1991 &image_lookup_extension
.base
,
1996 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1998 struct dri2_egl_display
*dri2_dpy
;
2000 loader_set_logger(_eglLog
);
2002 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2004 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2007 disp
->DriverData
= (void *) dri2_dpy
;
2008 if (disp
->PlatformDisplay
== NULL
) {
2009 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2010 if (dri2_dpy
->wl_dpy
== NULL
)
2012 dri2_dpy
->own_device
= true;
2014 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2017 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2019 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2020 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2023 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2024 dri2_dpy
->wl_queue
);
2026 if (dri2_dpy
->own_device
)
2027 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2029 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2030 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2031 ®istry_listener_swrast
, dri2_dpy
);
2033 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2036 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
2039 dri2_dpy
->driver_name
= strdup("swrast");
2040 if (!dri2_load_driver_swrast(disp
))
2043 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2045 if (!dri2_create_screen(disp
))
2048 if (!dri2_setup_extensions(disp
))
2051 dri2_setup_screen(disp
);
2053 dri2_wl_setup_swap_interval(disp
);
2055 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2056 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2060 /* Fill vtbl last to prevent accidentally calling virtual function during
2063 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2068 dri2_display_destroy(disp
);
2073 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2075 EGLBoolean initialized
= EGL_FALSE
;
2077 if (!disp
->Options
.ForceSoftware
)
2078 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2081 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2088 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2090 if (dri2_dpy
->wl_drm
)
2091 wl_drm_destroy(dri2_dpy
->wl_drm
);
2092 if (dri2_dpy
->wl_dmabuf
)
2093 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2094 if (dri2_dpy
->wl_shm
)
2095 wl_shm_destroy(dri2_dpy
->wl_shm
);
2096 if (dri2_dpy
->wl_registry
)
2097 wl_registry_destroy(dri2_dpy
->wl_registry
);
2098 if (dri2_dpy
->wl_queue
)
2099 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2100 if (dri2_dpy
->wl_dpy_wrapper
)
2101 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2102 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb2101010
);
2103 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
2104 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb8888
);
2105 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb8888
);
2106 u_vector_finish(&dri2_dpy
->wl_modifiers
.rgb565
);
2107 if (dri2_dpy
->own_device
)
2108 wl_display_disconnect(dri2_dpy
->wl_dpy
);