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
61 * The index of entries in this table is used as a bitmask in
62 * dri2_dpy->formats, which tracks the formats supported by our server.
64 static const struct dri2_wl_visual
{
65 const char *format_name
;
66 uint32_t wl_drm_format
;
67 uint32_t wl_shm_format
;
70 unsigned int rgba_masks
[4];
71 } dri2_wl_visuals
[] = {
74 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
75 __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
76 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0x00000000 }
80 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
81 __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
82 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 }
86 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
87 __DRI_IMAGE_FORMAT_XRGB8888
, 32,
88 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }
92 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
93 __DRI_IMAGE_FORMAT_ARGB8888
, 32,
94 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }
98 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
99 __DRI_IMAGE_FORMAT_RGB565
, 16,
100 { 0xf800, 0x07e0, 0x001f, 0x0000 }
105 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
106 const __DRIconfig
*config
)
108 unsigned int red
, green
, blue
, alpha
;
110 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &red
);
111 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &green
);
112 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &blue
);
113 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &alpha
);
115 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
116 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
118 if (red
== wl_visual
->rgba_masks
[0] &&
119 green
== wl_visual
->rgba_masks
[1] &&
120 blue
== wl_visual
->rgba_masks
[2] &&
121 alpha
== wl_visual
->rgba_masks
[3]) {
130 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
132 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
133 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
135 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
143 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
145 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
146 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
154 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
156 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
157 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
165 roundtrip(struct dri2_egl_display
*dri2_dpy
)
167 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
171 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
173 struct dri2_egl_surface
*dri2_surf
= data
;
176 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
177 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
180 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
181 wl_buffer_destroy(buffer
);
185 dri2_surf
->color_buffers
[i
].locked
= false;
188 static const struct wl_buffer_listener wl_buffer_listener
= {
189 .release
= wl_buffer_release
193 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
195 struct dri2_egl_surface
*dri2_surf
= data
;
196 struct dri2_egl_display
*dri2_dpy
=
197 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
199 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
203 destroy_window_callback(void *data
)
205 struct dri2_egl_surface
*dri2_surf
= data
;
206 dri2_surf
->wl_win
= NULL
;
209 static struct wl_surface
*
210 get_wl_surface_proxy(struct wl_egl_window
*window
)
212 /* Version 3 of wl_egl_window introduced a version field at the same
213 * location where a pointer to wl_surface was stored. Thus, if
214 * window->version is dereferencable, we've been given an older version of
215 * wl_egl_window, and window->version points to wl_surface */
216 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
217 return wl_proxy_create_wrapper((void *)(window
->version
));
219 return wl_proxy_create_wrapper(window
->surface
);
223 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
226 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
227 _EGLConfig
*conf
, void *native_window
,
228 const EGLint
*attrib_list
)
230 __DRIcreateNewDrawableFunc createNewDrawable
;
231 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
232 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
233 struct wl_egl_window
*window
= native_window
;
234 struct dri2_egl_surface
*dri2_surf
;
236 const __DRIconfig
*config
;
238 dri2_surf
= calloc(1, sizeof *dri2_surf
);
240 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
244 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
248 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
249 dri2_surf
->base
.GLColorspace
);
250 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
251 assert(visual_idx
!= -1);
253 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
254 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
256 assert(dri2_dpy
->wl_shm
);
257 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
260 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
261 if (!dri2_surf
->wl_queue
) {
262 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
266 if (dri2_dpy
->wl_drm
) {
267 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
268 if (!dri2_surf
->wl_drm_wrapper
) {
269 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
272 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
273 dri2_surf
->wl_queue
);
276 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
277 if (!dri2_surf
->wl_dpy_wrapper
) {
278 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
281 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
282 dri2_surf
->wl_queue
);
284 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
285 if (!dri2_surf
->wl_surface_wrapper
) {
286 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
287 goto cleanup_dpy_wrapper
;
289 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
290 dri2_surf
->wl_queue
);
292 dri2_surf
->wl_win
= window
;
293 dri2_surf
->wl_win
->private = dri2_surf
;
294 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
296 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
298 if (dri2_dpy
->image_driver
)
299 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
300 else if (dri2_dpy
->dri2
)
301 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
303 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
305 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
307 if (dri2_surf
->dri_drawable
== NULL
) {
308 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
309 goto cleanup_surf_wrapper
;
312 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
314 return &dri2_surf
->base
;
316 cleanup_surf_wrapper
:
317 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
319 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
321 if (dri2_surf
->wl_drm_wrapper
)
322 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
324 wl_event_queue_destroy(dri2_surf
->wl_queue
);
332 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
333 _EGLConfig
*conf
, void *native_window
,
334 const EGLint
*attrib_list
)
336 /* From the EGL_EXT_platform_wayland spec, version 3:
338 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
339 * that belongs to Wayland. Any such call fails and generates
342 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
348 * Called via eglDestroySurface(), drv->API.DestroySurface().
351 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
353 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
354 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
358 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
360 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
361 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
362 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
363 if (dri2_surf
->color_buffers
[i
].dri_image
)
364 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
365 if (dri2_surf
->color_buffers
[i
].linear_copy
)
366 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
367 if (dri2_surf
->color_buffers
[i
].data
)
368 munmap(dri2_surf
->color_buffers
[i
].data
,
369 dri2_surf
->color_buffers
[i
].data_size
);
373 dri2_egl_surface_free_local_buffers(dri2_surf
);
375 if (dri2_surf
->throttle_callback
)
376 wl_callback_destroy(dri2_surf
->throttle_callback
);
378 if (dri2_surf
->wl_win
) {
379 dri2_surf
->wl_win
->private = NULL
;
380 dri2_surf
->wl_win
->resize_callback
= NULL
;
381 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
384 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
385 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
386 if (dri2_surf
->wl_drm_wrapper
)
387 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
388 wl_event_queue_destroy(dri2_surf
->wl_queue
);
390 dri2_fini_surface(surf
);
397 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
399 struct dri2_egl_display
*dri2_dpy
=
400 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
402 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
403 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
404 !dri2_surf
->color_buffers
[i
].locked
)
405 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
406 if (dri2_surf
->color_buffers
[i
].dri_image
)
407 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
408 if (dri2_surf
->color_buffers
[i
].linear_copy
)
409 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
410 if (dri2_surf
->color_buffers
[i
].data
)
411 munmap(dri2_surf
->color_buffers
[i
].data
,
412 dri2_surf
->color_buffers
[i
].data_size
);
414 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
415 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
416 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
417 dri2_surf
->color_buffers
[i
].data
= NULL
;
418 dri2_surf
->color_buffers
[i
].locked
= false;
422 dri2_egl_surface_free_local_buffers(dri2_surf
);
426 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
428 struct dri2_egl_display
*dri2_dpy
=
429 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
432 unsigned int dri_image_format
;
436 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
437 assert(visual_idx
!= -1);
438 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
440 /* currently supports five WL DRM formats,
441 * WL_DRM_FORMAT_ARGB2101010, WL_DRM_FORMAT_XRGB2101010,
442 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
443 * and WL_DRM_FORMAT_RGB565
445 switch (dri2_surf
->format
) {
446 case WL_DRM_FORMAT_ARGB2101010
:
447 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb2101010
);
448 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb2101010
);
450 case WL_DRM_FORMAT_XRGB2101010
:
451 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
452 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
454 case WL_DRM_FORMAT_ARGB8888
:
455 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
456 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
458 case WL_DRM_FORMAT_XRGB8888
:
459 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
460 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
462 case WL_DRM_FORMAT_RGB565
:
463 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
464 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
467 /* format is not supported */
471 /* There might be a buffer release already queued that wasn't processed */
472 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
474 while (dri2_surf
->back
== NULL
) {
475 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
476 /* Get an unlocked buffer, preferrably one with a dri_buffer
477 * already allocated. */
478 if (dri2_surf
->color_buffers
[i
].locked
)
480 if (dri2_surf
->back
== NULL
)
481 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
482 else if (dri2_surf
->back
->dri_image
== NULL
)
483 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
489 /* If we don't have a buffer, then block on the server to release one for
490 * us, and try again. wl_display_dispatch_queue will process any pending
491 * events, however not all servers flush on issuing a buffer release
492 * event. So, we spam the server with roundtrips as they always cause a
495 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
496 dri2_surf
->wl_queue
) < 0)
500 if (dri2_surf
->back
== NULL
)
503 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
505 if (dri2_dpy
->is_different_gpu
&&
506 dri2_surf
->back
->linear_copy
== NULL
) {
507 /* The LINEAR modifier should be a perfect alias of the LINEAR use
508 * flag; try the new interface first before the old, then fall back. */
509 if (dri2_dpy
->image
->base
.version
>= 15 &&
510 dri2_dpy
->image
->createImageWithModifiers
) {
511 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
513 dri2_surf
->back
->linear_copy
=
514 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
515 dri2_surf
->base
.Width
,
516 dri2_surf
->base
.Height
,
522 dri2_surf
->back
->linear_copy
=
523 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
524 dri2_surf
->base
.Width
,
525 dri2_surf
->base
.Height
,
528 __DRI_IMAGE_USE_LINEAR
,
531 if (dri2_surf
->back
->linear_copy
== NULL
)
535 if (dri2_surf
->back
->dri_image
== NULL
) {
536 /* If our DRIImage implementation does not support
537 * createImageWithModifiers, then fall back to the old createImage,
538 * and hope it allocates an image which is acceptable to the winsys.
540 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
541 dri2_dpy
->image
->createImageWithModifiers
) {
542 dri2_surf
->back
->dri_image
=
543 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
544 dri2_surf
->base
.Width
,
545 dri2_surf
->base
.Height
,
551 dri2_surf
->back
->dri_image
=
552 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
553 dri2_surf
->base
.Width
,
554 dri2_surf
->base
.Height
,
556 dri2_dpy
->is_different_gpu
?
561 dri2_surf
->back
->age
= 0;
563 if (dri2_surf
->back
->dri_image
== NULL
)
566 dri2_surf
->back
->locked
= true;
573 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
575 struct dri2_egl_display
*dri2_dpy
=
576 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
580 image
= dri2_surf
->back
->dri_image
;
582 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
583 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
585 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
587 buffer
->pitch
= pitch
;
593 update_buffers(struct dri2_egl_surface
*dri2_surf
)
595 struct dri2_egl_display
*dri2_dpy
=
596 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
598 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
599 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
601 dri2_wl_release_buffers(dri2_surf
);
603 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
604 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
605 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
606 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
609 if (get_back_bo(dri2_surf
) < 0) {
610 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
614 /* If we have an extra unlocked buffer at this point, we had to do triple
615 * buffering for a while, but now can go back to just double buffering.
616 * That means we can free any unlocked buffer now. */
617 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
618 if (!dri2_surf
->color_buffers
[i
].locked
&&
619 dri2_surf
->color_buffers
[i
].wl_buffer
) {
620 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
621 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
622 if (dri2_dpy
->is_different_gpu
)
623 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
624 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
625 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
626 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
634 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
635 int *width
, int *height
,
636 unsigned int *attachments
, int count
,
637 int *out_count
, void *loaderPrivate
)
639 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
642 if (update_buffers(dri2_surf
) < 0)
645 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
648 switch (attachments
[i
]) {
649 case __DRI_BUFFER_BACK_LEFT
:
650 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
653 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
657 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
660 dri2_surf
->buffers
[j
] = *local
;
669 *width
= dri2_surf
->base
.Width
;
670 *height
= dri2_surf
->base
.Height
;
672 return dri2_surf
->buffers
;
676 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
677 int *width
, int *height
,
678 unsigned int *attachments
, int count
,
679 int *out_count
, void *loaderPrivate
)
681 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
682 unsigned int *attachments_with_format
;
684 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
686 if (visual_idx
== -1)
689 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
690 if (!attachments_with_format
) {
695 for (int i
= 0; i
< count
; ++i
) {
696 attachments_with_format
[2*i
] = attachments
[i
];
697 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
701 dri2_wl_get_buffers_with_format(driDrawable
,
703 attachments_with_format
, count
,
704 out_count
, loaderPrivate
);
706 free(attachments_with_format
);
712 image_get_buffers(__DRIdrawable
*driDrawable
,
716 uint32_t buffer_mask
,
717 struct __DRIimageList
*buffers
)
719 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
721 if (update_buffers(dri2_surf
) < 0)
724 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
725 buffers
->back
= dri2_surf
->back
->dri_image
;
731 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
734 (void) loaderPrivate
;
737 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
738 .base
= { __DRI_DRI2_LOADER
, 3 },
740 .getBuffers
= dri2_wl_get_buffers
,
741 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
742 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
745 static const __DRIimageLoaderExtension image_loader_extension
= {
746 .base
= { __DRI_IMAGE_LOADER
, 1 },
748 .getBuffers
= image_get_buffers
,
749 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
753 wayland_throttle_callback(void *data
,
754 struct wl_callback
*callback
,
757 struct dri2_egl_surface
*dri2_surf
= data
;
759 dri2_surf
->throttle_callback
= NULL
;
760 wl_callback_destroy(callback
);
763 static const struct wl_callback_listener throttle_listener
= {
764 .done
= wayland_throttle_callback
768 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
769 __DRIimage
*image
, int *fourcc
)
775 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
780 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
785 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
786 if (visual_idx
== -1)
789 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
793 static struct wl_buffer
*
794 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
795 struct dri2_egl_surface
*dri2_surf
,
798 struct wl_buffer
*ret
;
800 int width
, height
, fourcc
, num_planes
;
801 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
803 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
804 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
806 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
810 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
815 if (dri2_dpy
->image
->base
.version
>= 15) {
818 query
= dri2_dpy
->image
->queryImage(image
,
819 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
821 query
&= dri2_dpy
->image
->queryImage(image
,
822 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
825 modifier
= (uint64_t) mod_hi
<< 32;
826 modifier
|= (uint64_t) (mod_lo
& 0xffffffff);
830 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
831 struct zwp_linux_buffer_params_v1
*params
;
834 /* We don't need a wrapper for wl_dmabuf objects, because we have to
835 * create the intermediate params object; we can set the queue on this,
836 * and the wl_buffer inherits it race-free. */
837 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
839 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
841 for (i
= 0; i
< num_planes
; i
++) {
849 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
851 zwp_linux_buffer_params_v1_destroy(params
);
855 query
= dri2_dpy
->image
->queryImage(p_image
,
856 __DRI_IMAGE_ATTRIB_FD
,
858 query
&= dri2_dpy
->image
->queryImage(p_image
,
859 __DRI_IMAGE_ATTRIB_STRIDE
,
861 query
&= dri2_dpy
->image
->queryImage(p_image
,
862 __DRI_IMAGE_ATTRIB_OFFSET
,
864 if (image
!= p_image
)
865 dri2_dpy
->image
->destroyImage(p_image
);
870 zwp_linux_buffer_params_v1_destroy(params
);
874 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
875 modifier
>> 32, modifier
& 0xffffffff);
879 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
881 zwp_linux_buffer_params_v1_destroy(params
);
882 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
883 struct wl_drm
*wl_drm
=
884 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
890 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
891 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
892 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
896 struct wl_drm
*wl_drm
=
897 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
903 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
904 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
905 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
912 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
916 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
917 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
920 for (int i
= 0; i
< n_rects
; i
++) {
921 const int *rect
= &rects
[i
* 4];
923 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
925 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
931 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
934 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
940 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
941 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
943 while (dri2_surf
->throttle_callback
!= NULL
)
944 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
945 dri2_surf
->wl_queue
) == -1)
948 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
949 if (dri2_surf
->color_buffers
[i
].age
> 0)
950 dri2_surf
->color_buffers
[i
].age
++;
952 /* Make sure we have a back buffer in case we're swapping without ever
954 if (get_back_bo(dri2_surf
) < 0)
955 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
957 if (draw
->SwapInterval
> 0) {
958 dri2_surf
->throttle_callback
=
959 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
960 wl_callback_add_listener(dri2_surf
->throttle_callback
,
961 &throttle_listener
, dri2_surf
);
964 dri2_surf
->back
->age
= 1;
965 dri2_surf
->current
= dri2_surf
->back
;
966 dri2_surf
->back
= NULL
;
968 if (!dri2_surf
->current
->wl_buffer
) {
971 if (dri2_dpy
->is_different_gpu
)
972 image
= dri2_surf
->current
->linear_copy
;
974 image
= dri2_surf
->current
->dri_image
;
976 dri2_surf
->current
->wl_buffer
=
977 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
979 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
980 &wl_buffer_listener
, dri2_surf
);
983 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
984 dri2_surf
->current
->wl_buffer
,
985 dri2_surf
->dx
, dri2_surf
->dy
);
987 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
988 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
989 /* reset resize growing parameters */
993 /* If the compositor doesn't support damage_buffer, we deliberately
994 * ignore the damage region and post maximum damage, due to
995 * https://bugs.freedesktop.org/78190 */
996 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
997 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
998 0, 0, INT32_MAX
, INT32_MAX
);
1000 if (dri2_dpy
->is_different_gpu
) {
1001 _EGLContext
*ctx
= _eglGetCurrentContext();
1002 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1003 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1004 dri2_surf
->current
->linear_copy
,
1005 dri2_surf
->current
->dri_image
,
1006 0, 0, dri2_surf
->base
.Width
,
1007 dri2_surf
->base
.Height
,
1008 0, 0, dri2_surf
->base
.Width
,
1009 dri2_surf
->base
.Height
, 0);
1012 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1013 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1015 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1017 /* If we're not waiting for a frame callback then we'll at least throttle
1018 * to a sync callback so that we always give a chance for the compositor to
1019 * handle the commit and send a release event before checking for a free
1021 if (dri2_surf
->throttle_callback
== NULL
) {
1022 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1023 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1024 &throttle_listener
, dri2_surf
);
1027 wl_display_flush(dri2_dpy
->wl_dpy
);
1033 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1034 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1036 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1038 if (get_back_bo(dri2_surf
) < 0) {
1039 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1043 return dri2_surf
->back
->age
;
1047 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1049 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1052 static struct wl_buffer
*
1053 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1057 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1058 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1059 __DRIimage
*image
= dri2_img
->dri_image
;
1060 struct wl_buffer
*buffer
;
1061 int format
, visual_idx
;
1063 /* Check the upstream display supports this buffer's format. */
1064 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1065 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1066 if (visual_idx
== -1)
1069 if (!(dri2_dpy
->formats
& (1 << visual_idx
)))
1072 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1074 /* The buffer object will have been created with our internal event queue
1075 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1076 * buffer to be used by the application so we'll reset it to the display's
1077 * default event queue. This isn't actually racy, as the only event the
1078 * buffer can get is a buffer release, which doesn't happen with an explicit
1081 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1086 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1091 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1093 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1096 if (dri2_dpy
->is_render_node
) {
1097 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1098 "authenticate for render-nodes");
1101 dri2_dpy
->authenticated
= false;
1103 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1104 if (roundtrip(dri2_dpy
) < 0)
1107 if (!dri2_dpy
->authenticated
)
1110 /* reset authenticated */
1111 dri2_dpy
->authenticated
= true;
1117 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1119 struct dri2_egl_display
*dri2_dpy
= data
;
1122 dri2_dpy
->device_name
= strdup(device
);
1123 if (!dri2_dpy
->device_name
)
1126 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1127 if (dri2_dpy
->fd
== -1) {
1128 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1129 dri2_dpy
->device_name
, strerror(errno
));
1133 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1134 dri2_dpy
->authenticated
= true;
1136 drmGetMagic(dri2_dpy
->fd
, &magic
);
1137 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1142 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1144 struct dri2_egl_display
*dri2_dpy
= data
;
1145 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1147 if (visual_idx
== -1)
1150 dri2_dpy
->formats
|= (1 << visual_idx
);
1154 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1156 struct dri2_egl_display
*dri2_dpy
= data
;
1158 dri2_dpy
->capabilities
= value
;
1162 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1164 struct dri2_egl_display
*dri2_dpy
= data
;
1166 dri2_dpy
->authenticated
= true;
1169 static const struct wl_drm_listener drm_listener
= {
1170 .device
= drm_handle_device
,
1171 .format
= drm_handle_format
,
1172 .authenticated
= drm_handle_authenticated
,
1173 .capabilities
= drm_handle_capabilities
1177 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1180 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1184 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1185 uint32_t format
, uint32_t modifier_hi
,
1186 uint32_t modifier_lo
)
1188 struct dri2_egl_display
*dri2_dpy
= data
;
1189 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1190 uint64_t *mod
= NULL
;
1192 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1193 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1197 case WL_DRM_FORMAT_ARGB2101010
:
1198 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb2101010
);
1200 case WL_DRM_FORMAT_XRGB2101010
:
1201 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
1203 case WL_DRM_FORMAT_ARGB8888
:
1204 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1206 case WL_DRM_FORMAT_XRGB8888
:
1207 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1209 case WL_DRM_FORMAT_RGB565
:
1210 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1219 dri2_dpy
->formats
|= (1 << visual_idx
);
1220 *mod
= (uint64_t) modifier_hi
<< 32;
1221 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1224 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1225 .format
= dmabuf_ignore_format
,
1226 .modifier
= dmabuf_handle_modifier
,
1230 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1231 uint32_t name
, const char *interface
,
1234 struct dri2_egl_display
*dri2_dpy
= data
;
1236 if (strcmp(interface
, "wl_drm") == 0) {
1238 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1239 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1240 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1241 dri2_dpy
->wl_dmabuf
=
1242 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1244 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1250 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1255 static const struct wl_registry_listener registry_listener_drm
= {
1256 .global
= registry_handle_global_drm
,
1257 .global_remove
= registry_handle_global_remove
1261 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1263 /* We can't use values greater than 1 on Wayland because we are using the
1264 * frame callback to synchronise the frame and the only way we be sure to
1265 * get a frame callback is to attach a new buffer. Therefore we can't just
1266 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1268 dri2_setup_swap_interval(disp
, 1);
1271 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1272 .authenticate
= dri2_wl_authenticate
,
1273 .create_window_surface
= dri2_wl_create_window_surface
,
1274 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1275 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1276 .destroy_surface
= dri2_wl_destroy_surface
,
1277 .create_image
= dri2_create_image_khr
,
1278 .swap_buffers
= dri2_wl_swap_buffers
,
1279 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1280 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1281 .set_damage_region
= dri2_fallback_set_damage_region
,
1282 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1283 .copy_buffers
= dri2_fallback_copy_buffers
,
1284 .query_buffer_age
= dri2_wl_query_buffer_age
,
1285 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1286 .get_sync_values
= dri2_fallback_get_sync_values
,
1287 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1290 static const __DRIextension
*dri2_loader_extensions
[] = {
1291 &dri2_loader_extension
.base
,
1292 &image_loader_extension
.base
,
1293 &image_lookup_extension
.base
,
1294 &use_invalidate
.base
,
1298 static const __DRIextension
*image_loader_extensions
[] = {
1299 &image_loader_extension
.base
,
1300 &image_lookup_extension
.base
,
1301 &use_invalidate
.base
,
1306 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1308 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1309 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1310 unsigned int count
= 0;
1312 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1313 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1314 struct dri2_egl_config
*dri2_conf
;
1316 if (!(dri2_dpy
->formats
& (1 << j
)))
1319 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1320 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1322 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1329 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1330 if (!format_count
[i
]) {
1331 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1332 dri2_wl_visuals
[i
].format_name
);
1336 return (count
!= 0);
1340 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1342 struct dri2_egl_display
*dri2_dpy
;
1344 loader_set_logger(_eglLog
);
1346 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1348 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1351 disp
->DriverData
= (void *) dri2_dpy
;
1352 if (disp
->PlatformDisplay
== NULL
) {
1353 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1354 if (dri2_dpy
->wl_dpy
== NULL
)
1356 dri2_dpy
->own_device
= true;
1358 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1361 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb2101010
, sizeof(uint64_t), 32) ||
1362 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb2101010
, sizeof(uint64_t), 32) ||
1363 !u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1364 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1365 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1369 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1371 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1372 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1375 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1376 dri2_dpy
->wl_queue
);
1378 if (dri2_dpy
->own_device
)
1379 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1381 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1382 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1383 ®istry_listener_drm
, dri2_dpy
);
1384 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1387 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1390 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1393 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1394 &dri2_dpy
->is_different_gpu
);
1395 if (dri2_dpy
->is_different_gpu
) {
1396 free(dri2_dpy
->device_name
);
1397 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1398 if (!dri2_dpy
->device_name
) {
1399 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1400 "for requested GPU");
1405 /* we have to do the check now, because loader_get_user_preferred_fd
1406 * will return a render-node when the requested gpu is different
1407 * to the server, but also if the client asks for the same gpu than
1408 * the server by requesting its pci-id */
1409 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1411 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1412 if (dri2_dpy
->driver_name
== NULL
) {
1413 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1417 /* render nodes cannot use Gem names, and thus do not support
1418 * the __DRI_DRI2_LOADER extension */
1419 if (!dri2_dpy
->is_render_node
) {
1420 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1421 if (!dri2_load_driver(disp
)) {
1422 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1426 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1427 if (!dri2_load_driver_dri3(disp
)) {
1428 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1433 if (!dri2_create_screen(disp
))
1436 if (!dri2_setup_extensions(disp
))
1439 dri2_setup_screen(disp
);
1441 dri2_wl_setup_swap_interval(disp
);
1443 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1444 * createImageFromFds support indicates that Prime export/import
1445 * is supported by the driver. Fall back to
1446 * gem names if we don't have Prime support. */
1448 if (dri2_dpy
->image
->base
.version
< 7 ||
1449 dri2_dpy
->image
->createImageFromFds
== NULL
)
1450 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1452 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1453 * The server needs to accept them */
1454 if (dri2_dpy
->is_render_node
&&
1455 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1456 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1460 if (dri2_dpy
->is_different_gpu
&&
1461 (dri2_dpy
->image
->base
.version
< 9 ||
1462 dri2_dpy
->image
->blitImage
== NULL
)) {
1463 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1464 "Image extension in the driver is not "
1465 "compatible. Version 9 or later and blitImage() "
1470 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1471 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1475 dri2_set_WL_bind_wayland_display(drv
, disp
);
1476 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1477 * because the buffer of the EGLImage has likely a tiling mode the server
1478 * gpu won't support. These is no way to check for now. Thus do not support the
1480 if (!dri2_dpy
->is_different_gpu
)
1481 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1483 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1485 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1487 /* Fill vtbl last to prevent accidentally calling virtual function during
1490 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1495 dri2_display_destroy(disp
);
1500 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1502 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1504 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1508 * Taken from weston shared/os-compatibility.c
1511 #ifndef HAVE_MKOSTEMP
1514 set_cloexec_or_close(int fd
)
1521 flags
= fcntl(fd
, F_GETFD
);
1525 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1538 * Taken from weston shared/os-compatibility.c
1542 create_tmpfile_cloexec(char *tmpname
)
1546 #ifdef HAVE_MKOSTEMP
1547 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1551 fd
= mkstemp(tmpname
);
1553 fd
= set_cloexec_or_close(fd
);
1562 * Taken from weston shared/os-compatibility.c
1564 * Create a new, unique, anonymous file of the given size, and
1565 * return the file descriptor for it. The file descriptor is set
1566 * CLOEXEC. The file is immediately suitable for mmap()'ing
1567 * the given size at offset zero.
1569 * The file should not have a permanent backing store like a disk,
1570 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1572 * The file name is deleted from the file system.
1574 * The file is suitable for buffer sharing between processes by
1575 * transmitting the file descriptor over Unix sockets using the
1576 * SCM_RIGHTS methods.
1578 * If the C library implements posix_fallocate(), it is used to
1579 * guarantee that disk space is available for the file at the
1580 * given size. If disk space is insufficent, errno is set to ENOSPC.
1581 * If posix_fallocate() is not supported, program may receive
1582 * SIGBUS on accessing mmap()'ed file contents instead.
1585 os_create_anonymous_file(off_t size
)
1587 static const char templ
[] = "/mesa-shared-XXXXXX";
1593 path
= getenv("XDG_RUNTIME_DIR");
1599 name
= malloc(strlen(path
) + sizeof(templ
));
1604 strcat(name
, templ
);
1606 fd
= create_tmpfile_cloexec(name
);
1613 ret
= ftruncate(fd
, size
);
1624 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1625 int format
, int w
, int h
,
1626 void **data
, int *size
,
1627 struct wl_buffer
**buffer
)
1629 struct dri2_egl_display
*dri2_dpy
=
1630 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1631 struct wl_shm_pool
*pool
;
1632 int fd
, stride
, size_map
;
1635 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1636 size_map
= h
* stride
;
1638 /* Create a sharable buffer */
1639 fd
= os_create_anonymous_file(size_map
);
1643 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1644 if (data_map
== MAP_FAILED
) {
1649 /* Share it in a wl_buffer */
1650 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1651 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1652 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1653 wl_shm_pool_destroy(pool
);
1662 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1664 struct dri2_egl_display
*dri2_dpy
=
1665 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1667 /* we need to do the following operations only once per frame */
1668 if (dri2_surf
->back
)
1671 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1672 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1674 dri2_wl_release_buffers(dri2_surf
);
1676 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1677 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1678 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1679 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1680 dri2_surf
->current
= NULL
;
1683 /* find back buffer */
1685 /* There might be a buffer release already queued that wasn't processed */
1686 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1688 /* try get free buffer already created */
1689 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1690 if (!dri2_surf
->color_buffers
[i
].locked
&&
1691 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1692 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1697 /* else choose any another free location */
1698 if (!dri2_surf
->back
) {
1699 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1700 if (!dri2_surf
->color_buffers
[i
].locked
) {
1701 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1702 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1704 dri2_surf
->base
.Width
,
1705 dri2_surf
->base
.Height
,
1706 &dri2_surf
->back
->data
,
1707 &dri2_surf
->back
->data_size
,
1708 &dri2_surf
->back
->wl_buffer
)) {
1709 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1712 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1713 &wl_buffer_listener
, dri2_surf
);
1719 if (!dri2_surf
->back
) {
1720 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1724 dri2_surf
->back
->locked
= true;
1726 /* If we have an extra unlocked buffer at this point, we had to do triple
1727 * buffering for a while, but now can go back to just double buffering.
1728 * That means we can free any unlocked buffer now. */
1729 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1730 if (!dri2_surf
->color_buffers
[i
].locked
&&
1731 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1732 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1733 munmap(dri2_surf
->color_buffers
[i
].data
,
1734 dri2_surf
->color_buffers
[i
].data_size
);
1735 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1736 dri2_surf
->color_buffers
[i
].data
= NULL
;
1744 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1746 /* if there has been a resize: */
1747 if (!dri2_surf
->current
)
1750 return dri2_surf
->current
->data
;
1754 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1756 assert(dri2_surf
->back
);
1757 return dri2_surf
->back
->data
;
1761 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1763 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1765 while (dri2_surf
->throttle_callback
!= NULL
)
1766 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1767 dri2_surf
->wl_queue
) == -1)
1770 if (dri2_surf
->base
.SwapInterval
> 0) {
1771 dri2_surf
->throttle_callback
=
1772 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1773 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1774 &throttle_listener
, dri2_surf
);
1777 dri2_surf
->current
= dri2_surf
->back
;
1778 dri2_surf
->back
= NULL
;
1780 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1781 dri2_surf
->current
->wl_buffer
,
1782 dri2_surf
->dx
, dri2_surf
->dy
);
1784 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1785 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1786 /* reset resize growing parameters */
1790 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1791 0, 0, INT32_MAX
, INT32_MAX
);
1792 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1794 /* If we're not waiting for a frame callback then we'll at least throttle
1795 * to a sync callback so that we always give a chance for the compositor to
1796 * handle the commit and send a release event before checking for a free
1798 if (dri2_surf
->throttle_callback
== NULL
) {
1799 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1800 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1801 &throttle_listener
, dri2_surf
);
1804 wl_display_flush(dri2_dpy
->wl_dpy
);
1808 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1809 int *x
, int *y
, int *w
, int *h
,
1810 void *loaderPrivate
)
1812 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1814 (void) swrast_update_buffers(dri2_surf
);
1817 *w
= dri2_surf
->base
.Width
;
1818 *h
= dri2_surf
->base
.Height
;
1822 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1823 int x
, int y
, int w
, int h
,
1824 char *data
, void *loaderPrivate
)
1826 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1827 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1828 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1829 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1830 int dst_stride
= copy_width
;
1833 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1835 memset(data
, 0, copy_width
* h
);
1839 assert(data
!= src
);
1840 assert(copy_width
<= src_stride
);
1843 src
+= y
* src_stride
;
1846 if (copy_width
> src_stride
-x_offset
)
1847 copy_width
= src_stride
-x_offset
;
1848 if (h
> dri2_surf
->base
.Height
-y
)
1849 h
= dri2_surf
->base
.Height
-y
;
1852 memcpy(dst
, src
, copy_width
);
1859 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1860 int x
, int y
, int w
, int h
, int stride
,
1861 char *data
, void *loaderPrivate
)
1863 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1864 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1865 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1866 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1869 assert(copy_width
<= stride
);
1871 (void) swrast_update_buffers(dri2_surf
);
1872 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1874 /* partial copy, copy old content */
1875 if (copy_width
< dst_stride
)
1876 dri2_wl_swrast_get_image(draw
, 0, 0,
1877 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1878 dst
, loaderPrivate
);
1881 dst
+= y
* dst_stride
;
1885 /* drivers expect we do these checks (and some rely on it) */
1886 if (copy_width
> dst_stride
-x_offset
)
1887 copy_width
= dst_stride
-x_offset
;
1888 if (h
> dri2_surf
->base
.Height
-y
)
1889 h
= dri2_surf
->base
.Height
-y
;
1892 memcpy(dst
, src
, copy_width
);
1896 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1900 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1901 int x
, int y
, int w
, int h
,
1902 char *data
, void *loaderPrivate
)
1904 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1907 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1908 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1909 stride
, data
, loaderPrivate
);
1913 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1915 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1916 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1918 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1923 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1925 struct dri2_egl_display
*dri2_dpy
= data
;
1926 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1928 if (visual_idx
== -1)
1931 dri2_dpy
->formats
|= (1 << visual_idx
);
1934 static const struct wl_shm_listener shm_listener
= {
1935 .format
= shm_handle_format
1939 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1940 uint32_t name
, const char *interface
,
1943 struct dri2_egl_display
*dri2_dpy
= data
;
1945 if (strcmp(interface
, "wl_shm") == 0) {
1947 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1948 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1952 static const struct wl_registry_listener registry_listener_swrast
= {
1953 .global
= registry_handle_global_swrast
,
1954 .global_remove
= registry_handle_global_remove
1957 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1958 .authenticate
= NULL
,
1959 .create_window_surface
= dri2_wl_create_window_surface
,
1960 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1961 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1962 .destroy_surface
= dri2_wl_destroy_surface
,
1963 .create_image
= dri2_create_image_khr
,
1964 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1965 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1966 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1967 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1968 .copy_buffers
= dri2_fallback_copy_buffers
,
1969 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1970 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1971 .get_sync_values
= dri2_fallback_get_sync_values
,
1972 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1975 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1976 .base
= { __DRI_SWRAST_LOADER
, 2 },
1978 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1979 .putImage
= dri2_wl_swrast_put_image
,
1980 .getImage
= dri2_wl_swrast_get_image
,
1981 .putImage2
= dri2_wl_swrast_put_image2
,
1984 static const __DRIextension
*swrast_loader_extensions
[] = {
1985 &swrast_loader_extension
.base
,
1986 &image_lookup_extension
.base
,
1991 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1993 struct dri2_egl_display
*dri2_dpy
;
1995 loader_set_logger(_eglLog
);
1997 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1999 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2002 disp
->DriverData
= (void *) dri2_dpy
;
2003 if (disp
->PlatformDisplay
== NULL
) {
2004 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2005 if (dri2_dpy
->wl_dpy
== NULL
)
2007 dri2_dpy
->own_device
= true;
2009 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2012 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2014 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2015 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2018 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2019 dri2_dpy
->wl_queue
);
2021 if (dri2_dpy
->own_device
)
2022 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2024 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2025 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2026 ®istry_listener_swrast
, dri2_dpy
);
2028 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2031 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
2034 dri2_dpy
->driver_name
= strdup("swrast");
2035 if (!dri2_load_driver_swrast(disp
))
2038 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2040 if (!dri2_create_screen(disp
))
2043 if (!dri2_setup_extensions(disp
))
2046 dri2_setup_screen(disp
);
2048 dri2_wl_setup_swap_interval(disp
);
2050 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2051 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2055 /* Fill vtbl last to prevent accidentally calling virtual function during
2058 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2063 dri2_display_destroy(disp
);
2068 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2070 EGLBoolean initialized
= EGL_FALSE
;
2072 if (!disp
->Options
.ForceSoftware
)
2073 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2076 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2083 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2085 if (dri2_dpy
->wl_drm
)
2086 wl_drm_destroy(dri2_dpy
->wl_drm
);
2087 if (dri2_dpy
->wl_dmabuf
)
2088 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2089 if (dri2_dpy
->wl_shm
)
2090 wl_shm_destroy(dri2_dpy
->wl_shm
);
2091 if (dri2_dpy
->wl_registry
)
2092 wl_registry_destroy(dri2_dpy
->wl_registry
);
2093 if (dri2_dpy
->wl_queue
)
2094 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2095 if (dri2_dpy
->wl_dpy_wrapper
)
2096 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2097 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb2101010
);
2098 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
2099 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb8888
);
2100 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb8888
);
2101 u_vector_finish(&dri2_dpy
->wl_modifiers
.rgb565
);
2102 if (dri2_dpy
->own_device
)
2103 wl_display_disconnect(dri2_dpy
->wl_dpy
);