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
{
67 roundtrip(struct dri2_egl_display
*dri2_dpy
)
69 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
73 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
75 struct dri2_egl_surface
*dri2_surf
= data
;
78 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
79 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
82 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
83 wl_buffer_destroy(buffer
);
87 dri2_surf
->color_buffers
[i
].locked
= false;
90 static const struct wl_buffer_listener wl_buffer_listener
= {
91 .release
= wl_buffer_release
95 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
97 struct dri2_egl_surface
*dri2_surf
= data
;
98 struct dri2_egl_display
*dri2_dpy
=
99 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
101 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
105 destroy_window_callback(void *data
)
107 struct dri2_egl_surface
*dri2_surf
= data
;
108 dri2_surf
->wl_win
= NULL
;
111 static struct wl_surface
*
112 get_wl_surface_proxy(struct wl_egl_window
*window
)
114 /* Version 3 of wl_egl_window introduced a version field at the same
115 * location where a pointer to wl_surface was stored. Thus, if
116 * window->version is dereferencable, we've been given an older version of
117 * wl_egl_window, and window->version points to wl_surface */
118 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
119 return wl_proxy_create_wrapper((void *)(window
->version
));
121 return wl_proxy_create_wrapper(window
->surface
);
125 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
128 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
129 _EGLConfig
*conf
, void *native_window
,
130 const EGLint
*attrib_list
)
132 __DRIcreateNewDrawableFunc createNewDrawable
;
133 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
134 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
135 struct wl_egl_window
*window
= native_window
;
136 struct dri2_egl_surface
*dri2_surf
;
137 const __DRIconfig
*config
;
139 dri2_surf
= calloc(1, sizeof *dri2_surf
);
141 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
145 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
, false))
148 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
149 if (conf
->RedSize
== 5)
150 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
151 else if (conf
->AlphaSize
== 0)
152 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
154 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
156 assert(dri2_dpy
->wl_shm
);
157 if (conf
->RedSize
== 5)
158 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
159 else if (conf
->AlphaSize
== 0)
160 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
162 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
165 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
166 if (!dri2_surf
->wl_queue
) {
167 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
171 if (dri2_dpy
->wl_drm
) {
172 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
173 if (!dri2_surf
->wl_drm_wrapper
) {
174 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
177 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
178 dri2_surf
->wl_queue
);
181 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
182 if (!dri2_surf
->wl_dpy_wrapper
) {
183 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
186 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
187 dri2_surf
->wl_queue
);
189 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
190 if (!dri2_surf
->wl_surface_wrapper
) {
191 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
192 goto cleanup_dpy_wrapper
;
194 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
195 dri2_surf
->wl_queue
);
197 dri2_surf
->wl_win
= window
;
198 dri2_surf
->wl_win
->private = dri2_surf
;
199 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
201 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
203 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
204 dri2_surf
->base
.GLColorspace
);
206 if (dri2_dpy
->image_driver
)
207 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
208 else if (dri2_dpy
->dri2
)
209 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
211 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
213 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
215 if (dri2_surf
->dri_drawable
== NULL
) {
216 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
217 goto cleanup_surf_wrapper
;
220 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
222 return &dri2_surf
->base
;
224 cleanup_surf_wrapper
:
225 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
227 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
229 if (dri2_surf
->wl_drm_wrapper
)
230 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
232 wl_event_queue_destroy(dri2_surf
->wl_queue
);
240 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
241 _EGLConfig
*conf
, void *native_window
,
242 const EGLint
*attrib_list
)
244 /* From the EGL_EXT_platform_wayland spec, version 3:
246 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
247 * that belongs to Wayland. Any such call fails and generates
250 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
256 * Called via eglDestroySurface(), drv->API.DestroySurface().
259 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
261 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
262 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
266 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
268 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
269 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
270 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
271 if (dri2_surf
->color_buffers
[i
].dri_image
)
272 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
273 if (dri2_surf
->color_buffers
[i
].linear_copy
)
274 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
275 if (dri2_surf
->color_buffers
[i
].data
)
276 munmap(dri2_surf
->color_buffers
[i
].data
,
277 dri2_surf
->color_buffers
[i
].data_size
);
281 dri2_egl_surface_free_local_buffers(dri2_surf
);
283 if (dri2_surf
->throttle_callback
)
284 wl_callback_destroy(dri2_surf
->throttle_callback
);
286 if (dri2_surf
->wl_win
) {
287 dri2_surf
->wl_win
->private = NULL
;
288 dri2_surf
->wl_win
->resize_callback
= NULL
;
289 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
292 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
293 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
294 if (dri2_surf
->wl_drm_wrapper
)
295 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
296 wl_event_queue_destroy(dri2_surf
->wl_queue
);
298 dri2_fini_surface(surf
);
305 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
307 struct dri2_egl_display
*dri2_dpy
=
308 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
310 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
311 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
312 !dri2_surf
->color_buffers
[i
].locked
)
313 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
314 if (dri2_surf
->color_buffers
[i
].dri_image
)
315 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
316 if (dri2_surf
->color_buffers
[i
].linear_copy
)
317 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
318 if (dri2_surf
->color_buffers
[i
].data
)
319 munmap(dri2_surf
->color_buffers
[i
].data
,
320 dri2_surf
->color_buffers
[i
].data_size
);
322 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
323 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
324 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
325 dri2_surf
->color_buffers
[i
].data
= NULL
;
326 dri2_surf
->color_buffers
[i
].locked
= false;
330 dri2_egl_surface_free_local_buffers(dri2_surf
);
334 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
336 struct dri2_egl_display
*dri2_dpy
=
337 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
339 unsigned int dri_image_format
;
343 /* currently supports three WL DRM formats,
344 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
345 * and WL_DRM_FORMAT_RGB565
347 switch (dri2_surf
->format
) {
348 case WL_DRM_FORMAT_ARGB8888
:
349 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
350 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
351 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
353 case WL_DRM_FORMAT_XRGB8888
:
354 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
355 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
356 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
358 case WL_DRM_FORMAT_RGB565
:
359 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
360 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
361 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
364 /* format is not supported */
368 /* There might be a buffer release already queued that wasn't processed */
369 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
371 while (dri2_surf
->back
== NULL
) {
372 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
373 /* Get an unlocked buffer, preferrably one with a dri_buffer
374 * already allocated. */
375 if (dri2_surf
->color_buffers
[i
].locked
)
377 if (dri2_surf
->back
== NULL
)
378 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
379 else if (dri2_surf
->back
->dri_image
== NULL
)
380 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
386 /* If we don't have a buffer, then block on the server to release one for
387 * us, and try again. wl_display_dispatch_queue will process any pending
388 * events, however not all servers flush on issuing a buffer release
389 * event. So, we spam the server with roundtrips as they always cause a
392 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
393 dri2_surf
->wl_queue
) < 0)
397 if (dri2_surf
->back
== NULL
)
400 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
402 if (dri2_dpy
->is_different_gpu
&&
403 dri2_surf
->back
->linear_copy
== NULL
) {
404 /* The LINEAR modifier should be a perfect alias of the LINEAR use
405 * flag; try the new interface first before the old, then fall back. */
406 if (dri2_dpy
->image
->base
.version
>= 15 &&
407 dri2_dpy
->image
->createImageWithModifiers
) {
408 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
410 dri2_surf
->back
->linear_copy
=
411 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
412 dri2_surf
->base
.Width
,
413 dri2_surf
->base
.Height
,
419 dri2_surf
->back
->linear_copy
=
420 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
421 dri2_surf
->base
.Width
,
422 dri2_surf
->base
.Height
,
425 __DRI_IMAGE_USE_LINEAR
,
428 if (dri2_surf
->back
->linear_copy
== NULL
)
432 if (dri2_surf
->back
->dri_image
== NULL
) {
433 /* If our DRIImage implementation does not support
434 * createImageWithModifiers, then fall back to the old createImage,
435 * and hope it allocates an image which is acceptable to the winsys.
437 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
438 dri2_dpy
->image
->createImageWithModifiers
) {
439 dri2_surf
->back
->dri_image
=
440 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
441 dri2_surf
->base
.Width
,
442 dri2_surf
->base
.Height
,
448 dri2_surf
->back
->dri_image
=
449 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
450 dri2_surf
->base
.Width
,
451 dri2_surf
->base
.Height
,
453 dri2_dpy
->is_different_gpu
?
458 dri2_surf
->back
->age
= 0;
460 if (dri2_surf
->back
->dri_image
== NULL
)
463 dri2_surf
->back
->locked
= true;
470 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
472 struct dri2_egl_display
*dri2_dpy
=
473 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
477 image
= dri2_surf
->back
->dri_image
;
479 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
480 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
482 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
484 buffer
->pitch
= pitch
;
490 update_buffers(struct dri2_egl_surface
*dri2_surf
)
492 struct dri2_egl_display
*dri2_dpy
=
493 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
495 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
496 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
498 dri2_wl_release_buffers(dri2_surf
);
500 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
501 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
502 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
503 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
506 if (get_back_bo(dri2_surf
) < 0) {
507 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
511 /* If we have an extra unlocked buffer at this point, we had to do triple
512 * buffering for a while, but now can go back to just double buffering.
513 * That means we can free any unlocked buffer now. */
514 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
515 if (!dri2_surf
->color_buffers
[i
].locked
&&
516 dri2_surf
->color_buffers
[i
].wl_buffer
) {
517 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
518 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
519 if (dri2_dpy
->is_different_gpu
)
520 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
521 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
522 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
523 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
531 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
532 int *width
, int *height
,
533 unsigned int *attachments
, int count
,
534 int *out_count
, void *loaderPrivate
)
536 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
539 if (update_buffers(dri2_surf
) < 0)
542 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
545 switch (attachments
[i
]) {
546 case __DRI_BUFFER_BACK_LEFT
:
547 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
550 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
554 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
557 dri2_surf
->buffers
[j
] = *local
;
566 *width
= dri2_surf
->base
.Width
;
567 *height
= dri2_surf
->base
.Height
;
569 return dri2_surf
->buffers
;
573 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
574 int *width
, int *height
,
575 unsigned int *attachments
, int count
,
576 int *out_count
, void *loaderPrivate
)
578 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
579 unsigned int *attachments_with_format
;
583 switch (dri2_surf
->format
) {
584 case WL_DRM_FORMAT_ARGB8888
:
585 case WL_DRM_FORMAT_XRGB8888
:
588 case WL_DRM_FORMAT_RGB565
:
592 /* format is not supported */
596 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
597 if (!attachments_with_format
) {
602 for (int i
= 0; i
< count
; ++i
) {
603 attachments_with_format
[2*i
] = attachments
[i
];
604 attachments_with_format
[2*i
+ 1] = bpp
;
608 dri2_wl_get_buffers_with_format(driDrawable
,
610 attachments_with_format
, count
,
611 out_count
, loaderPrivate
);
613 free(attachments_with_format
);
619 image_get_buffers(__DRIdrawable
*driDrawable
,
623 uint32_t buffer_mask
,
624 struct __DRIimageList
*buffers
)
626 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
628 if (update_buffers(dri2_surf
) < 0)
631 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
632 buffers
->back
= dri2_surf
->back
->dri_image
;
638 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
641 (void) loaderPrivate
;
644 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
645 .base
= { __DRI_DRI2_LOADER
, 3 },
647 .getBuffers
= dri2_wl_get_buffers
,
648 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
649 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
652 static const __DRIimageLoaderExtension image_loader_extension
= {
653 .base
= { __DRI_IMAGE_LOADER
, 1 },
655 .getBuffers
= image_get_buffers
,
656 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
660 wayland_throttle_callback(void *data
,
661 struct wl_callback
*callback
,
664 struct dri2_egl_surface
*dri2_surf
= data
;
666 dri2_surf
->throttle_callback
= NULL
;
667 wl_callback_destroy(callback
);
670 static const struct wl_callback_listener throttle_listener
= {
671 .done
= wayland_throttle_callback
675 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
676 __DRIimage
*image
, int *fourcc
)
681 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
686 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
691 switch (dri_format
) {
692 case __DRI_IMAGE_FORMAT_ARGB8888
:
693 *fourcc
= __DRI_IMAGE_FOURCC_ARGB8888
;
695 case __DRI_IMAGE_FORMAT_XRGB8888
:
696 *fourcc
= __DRI_IMAGE_FOURCC_XRGB8888
;
703 static struct wl_buffer
*
704 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
705 struct dri2_egl_surface
*dri2_surf
,
708 struct wl_buffer
*ret
;
710 int width
, height
, fourcc
, num_planes
;
711 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
713 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
714 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
716 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
720 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
725 if (dri2_dpy
->image
->base
.version
>= 15) {
728 query
= dri2_dpy
->image
->queryImage(image
,
729 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
731 query
&= dri2_dpy
->image
->queryImage(image
,
732 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
735 modifier
= (uint64_t) mod_hi
<< 32;
736 modifier
|= (uint64_t) (mod_lo
& 0xffffffff);
740 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
741 struct zwp_linux_buffer_params_v1
*params
;
744 /* We don't need a wrapper for wl_dmabuf objects, because we have to
745 * create the intermediate params object; we can set the queue on this,
746 * and the wl_buffer inherits it race-free. */
747 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
749 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
751 for (i
= 0; i
< num_planes
; i
++) {
759 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
761 zwp_linux_buffer_params_v1_destroy(params
);
765 query
= dri2_dpy
->image
->queryImage(p_image
,
766 __DRI_IMAGE_ATTRIB_FD
,
768 query
&= dri2_dpy
->image
->queryImage(p_image
,
769 __DRI_IMAGE_ATTRIB_STRIDE
,
771 query
&= dri2_dpy
->image
->queryImage(p_image
,
772 __DRI_IMAGE_ATTRIB_OFFSET
,
774 if (image
!= p_image
)
775 dri2_dpy
->image
->destroyImage(p_image
);
780 zwp_linux_buffer_params_v1_destroy(params
);
784 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
785 modifier
>> 32, modifier
& 0xffffffff);
789 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
791 zwp_linux_buffer_params_v1_destroy(params
);
792 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
793 struct wl_drm
*wl_drm
=
794 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
800 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
801 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
802 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
806 struct wl_drm
*wl_drm
=
807 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
813 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
814 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
815 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
822 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
826 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
827 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
830 for (int i
= 0; i
< n_rects
; i
++) {
831 const int *rect
= &rects
[i
* 4];
833 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
835 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
841 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
844 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
850 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
851 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
853 while (dri2_surf
->throttle_callback
!= NULL
)
854 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
855 dri2_surf
->wl_queue
) == -1)
858 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
859 if (dri2_surf
->color_buffers
[i
].age
> 0)
860 dri2_surf
->color_buffers
[i
].age
++;
862 /* Make sure we have a back buffer in case we're swapping without ever
864 if (get_back_bo(dri2_surf
) < 0)
865 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
867 if (draw
->SwapInterval
> 0) {
868 dri2_surf
->throttle_callback
=
869 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
870 wl_callback_add_listener(dri2_surf
->throttle_callback
,
871 &throttle_listener
, dri2_surf
);
874 dri2_surf
->back
->age
= 1;
875 dri2_surf
->current
= dri2_surf
->back
;
876 dri2_surf
->back
= NULL
;
878 if (!dri2_surf
->current
->wl_buffer
) {
881 if (dri2_dpy
->is_different_gpu
)
882 image
= dri2_surf
->current
->linear_copy
;
884 image
= dri2_surf
->current
->dri_image
;
886 dri2_surf
->current
->wl_buffer
=
887 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
889 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
890 &wl_buffer_listener
, dri2_surf
);
893 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
894 dri2_surf
->current
->wl_buffer
,
895 dri2_surf
->dx
, dri2_surf
->dy
);
897 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
898 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
899 /* reset resize growing parameters */
903 /* If the compositor doesn't support damage_buffer, we deliberately
904 * ignore the damage region and post maximum damage, due to
905 * https://bugs.freedesktop.org/78190 */
906 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
907 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
908 0, 0, INT32_MAX
, INT32_MAX
);
910 if (dri2_dpy
->is_different_gpu
) {
911 _EGLContext
*ctx
= _eglGetCurrentContext();
912 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
913 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
914 dri2_surf
->current
->linear_copy
,
915 dri2_surf
->current
->dri_image
,
916 0, 0, dri2_surf
->base
.Width
,
917 dri2_surf
->base
.Height
,
918 0, 0, dri2_surf
->base
.Width
,
919 dri2_surf
->base
.Height
, 0);
922 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
923 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
925 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
927 /* If we're not waiting for a frame callback then we'll at least throttle
928 * to a sync callback so that we always give a chance for the compositor to
929 * handle the commit and send a release event before checking for a free
931 if (dri2_surf
->throttle_callback
== NULL
) {
932 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
933 wl_callback_add_listener(dri2_surf
->throttle_callback
,
934 &throttle_listener
, dri2_surf
);
937 wl_display_flush(dri2_dpy
->wl_dpy
);
943 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
944 _EGLDisplay
*disp
, _EGLSurface
*surface
)
946 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
948 if (get_back_bo(dri2_surf
) < 0) {
949 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
953 return dri2_surf
->back
->age
;
957 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
959 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
962 static struct wl_buffer
*
963 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
967 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
968 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
969 __DRIimage
*image
= dri2_img
->dri_image
;
970 struct wl_buffer
*buffer
;
973 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
975 case __DRI_IMAGE_FORMAT_ARGB8888
:
976 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
979 case __DRI_IMAGE_FORMAT_XRGB8888
:
980 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
987 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
989 /* The buffer object will have been created with our internal event queue
990 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
991 * buffer to be used by the application so we'll reset it to the display's
992 * default event queue. This isn't actually racy, as the only event the
993 * buffer can get is a buffer release, which doesn't happen with an explicit
996 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1001 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1006 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1008 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1011 if (dri2_dpy
->is_render_node
) {
1012 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1013 "authenticate for render-nodes");
1016 dri2_dpy
->authenticated
= false;
1018 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1019 if (roundtrip(dri2_dpy
) < 0)
1022 if (!dri2_dpy
->authenticated
)
1025 /* reset authenticated */
1026 dri2_dpy
->authenticated
= true;
1032 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1034 struct dri2_egl_display
*dri2_dpy
= data
;
1037 dri2_dpy
->device_name
= strdup(device
);
1038 if (!dri2_dpy
->device_name
)
1041 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1042 if (dri2_dpy
->fd
== -1) {
1043 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1044 dri2_dpy
->device_name
, strerror(errno
));
1048 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1049 dri2_dpy
->authenticated
= true;
1051 drmGetMagic(dri2_dpy
->fd
, &magic
);
1052 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1057 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1059 struct dri2_egl_display
*dri2_dpy
= data
;
1062 case WL_DRM_FORMAT_ARGB8888
:
1063 dri2_dpy
->formats
|= HAS_ARGB8888
;
1065 case WL_DRM_FORMAT_XRGB8888
:
1066 dri2_dpy
->formats
|= HAS_XRGB8888
;
1068 case WL_DRM_FORMAT_RGB565
:
1069 dri2_dpy
->formats
|= HAS_RGB565
;
1075 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1077 struct dri2_egl_display
*dri2_dpy
= data
;
1079 dri2_dpy
->capabilities
= value
;
1083 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1085 struct dri2_egl_display
*dri2_dpy
= data
;
1087 dri2_dpy
->authenticated
= true;
1090 static const struct wl_drm_listener drm_listener
= {
1091 .device
= drm_handle_device
,
1092 .format
= drm_handle_format
,
1093 .authenticated
= drm_handle_authenticated
,
1094 .capabilities
= drm_handle_capabilities
1098 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1101 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1105 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1106 uint32_t format
, uint32_t modifier_hi
,
1107 uint32_t modifier_lo
)
1109 struct dri2_egl_display
*dri2_dpy
= data
;
1110 uint64_t *mod
= NULL
;
1112 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1113 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1117 case WL_DRM_FORMAT_ARGB8888
:
1118 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1119 dri2_dpy
->formats
|= HAS_ARGB8888
;
1121 case WL_DRM_FORMAT_XRGB8888
:
1122 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1123 dri2_dpy
->formats
|= HAS_XRGB8888
;
1125 case WL_DRM_FORMAT_RGB565
:
1126 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1127 dri2_dpy
->formats
|= HAS_RGB565
;
1136 *mod
= (uint64_t) modifier_hi
<< 32;
1137 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1140 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1141 .format
= dmabuf_ignore_format
,
1142 .modifier
= dmabuf_handle_modifier
,
1146 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1147 uint32_t name
, const char *interface
,
1150 struct dri2_egl_display
*dri2_dpy
= data
;
1152 if (strcmp(interface
, "wl_drm") == 0) {
1154 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1155 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1156 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1157 dri2_dpy
->wl_dmabuf
=
1158 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1160 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1166 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1171 static const struct wl_registry_listener registry_listener_drm
= {
1172 .global
= registry_handle_global_drm
,
1173 .global_remove
= registry_handle_global_remove
1177 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1179 /* We can't use values greater than 1 on Wayland because we are using the
1180 * frame callback to synchronise the frame and the only way we be sure to
1181 * get a frame callback is to attach a new buffer. Therefore we can't just
1182 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1184 dri2_setup_swap_interval(disp
, 1);
1187 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1188 .authenticate
= dri2_wl_authenticate
,
1189 .create_window_surface
= dri2_wl_create_window_surface
,
1190 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1191 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1192 .destroy_surface
= dri2_wl_destroy_surface
,
1193 .create_image
= dri2_create_image_khr
,
1194 .swap_buffers
= dri2_wl_swap_buffers
,
1195 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1196 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1197 .set_damage_region
= dri2_fallback_set_damage_region
,
1198 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1199 .copy_buffers
= dri2_fallback_copy_buffers
,
1200 .query_buffer_age
= dri2_wl_query_buffer_age
,
1201 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1202 .get_sync_values
= dri2_fallback_get_sync_values
,
1203 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1206 static const __DRIextension
*dri2_loader_extensions
[] = {
1207 &dri2_loader_extension
.base
,
1208 &image_loader_extension
.base
,
1209 &image_lookup_extension
.base
,
1210 &use_invalidate
.base
,
1214 static const __DRIextension
*image_loader_extensions
[] = {
1215 &image_loader_extension
.base
,
1216 &image_lookup_extension
.base
,
1217 &use_invalidate
.base
,
1222 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1224 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1225 static const struct {
1226 const char *format_name
;
1228 unsigned int rgba_masks
[4];
1230 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1231 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1232 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1234 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1235 unsigned int count
= 0;
1237 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1238 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1239 struct dri2_egl_config
*dri2_conf
;
1241 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1244 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1245 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1247 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1254 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1255 if (!format_count
[i
]) {
1256 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1257 visuals
[i
].format_name
);
1261 return (count
!= 0);
1265 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1267 struct dri2_egl_display
*dri2_dpy
;
1269 loader_set_logger(_eglLog
);
1271 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1273 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1276 disp
->DriverData
= (void *) dri2_dpy
;
1277 if (disp
->PlatformDisplay
== NULL
) {
1278 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1279 if (dri2_dpy
->wl_dpy
== NULL
)
1281 dri2_dpy
->own_device
= true;
1283 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1286 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1287 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1288 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1292 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1294 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1295 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1298 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1299 dri2_dpy
->wl_queue
);
1301 if (dri2_dpy
->own_device
)
1302 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1304 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1305 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1306 ®istry_listener_drm
, dri2_dpy
);
1307 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1310 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1313 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1316 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1317 &dri2_dpy
->is_different_gpu
);
1318 if (dri2_dpy
->is_different_gpu
) {
1319 free(dri2_dpy
->device_name
);
1320 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1321 if (!dri2_dpy
->device_name
) {
1322 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1323 "for requested GPU");
1328 /* we have to do the check now, because loader_get_user_preferred_fd
1329 * will return a render-node when the requested gpu is different
1330 * to the server, but also if the client asks for the same gpu than
1331 * the server by requesting its pci-id */
1332 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1334 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1335 if (dri2_dpy
->driver_name
== NULL
) {
1336 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1340 /* render nodes cannot use Gem names, and thus do not support
1341 * the __DRI_DRI2_LOADER extension */
1342 if (!dri2_dpy
->is_render_node
) {
1343 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1344 if (!dri2_load_driver(disp
)) {
1345 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1349 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1350 if (!dri2_load_driver_dri3(disp
)) {
1351 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1356 if (!dri2_create_screen(disp
))
1359 if (!dri2_setup_extensions(disp
))
1362 dri2_setup_screen(disp
);
1364 dri2_wl_setup_swap_interval(disp
);
1366 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1367 * createImageFromFds support indicates that Prime export/import
1368 * is supported by the driver. Fall back to
1369 * gem names if we don't have Prime support. */
1371 if (dri2_dpy
->image
->base
.version
< 7 ||
1372 dri2_dpy
->image
->createImageFromFds
== NULL
)
1373 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1375 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1376 * The server needs to accept them */
1377 if (dri2_dpy
->is_render_node
&&
1378 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1379 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1383 if (dri2_dpy
->is_different_gpu
&&
1384 (dri2_dpy
->image
->base
.version
< 9 ||
1385 dri2_dpy
->image
->blitImage
== NULL
)) {
1386 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1387 "Image extension in the driver is not "
1388 "compatible. Version 9 or later and blitImage() "
1393 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1394 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1398 dri2_set_WL_bind_wayland_display(drv
, disp
);
1399 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1400 * because the buffer of the EGLImage has likely a tiling mode the server
1401 * gpu won't support. These is no way to check for now. Thus do not support the
1403 if (!dri2_dpy
->is_different_gpu
)
1404 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1406 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1408 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1410 /* Fill vtbl last to prevent accidentally calling virtual function during
1413 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1418 dri2_display_destroy(disp
);
1423 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1425 if (format
== WL_SHM_FORMAT_RGB565
)
1427 else /* ARGB8888 || XRGB8888 */
1432 * Taken from weston shared/os-compatibility.c
1435 #ifndef HAVE_MKOSTEMP
1438 set_cloexec_or_close(int fd
)
1445 flags
= fcntl(fd
, F_GETFD
);
1449 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1462 * Taken from weston shared/os-compatibility.c
1466 create_tmpfile_cloexec(char *tmpname
)
1470 #ifdef HAVE_MKOSTEMP
1471 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1475 fd
= mkstemp(tmpname
);
1477 fd
= set_cloexec_or_close(fd
);
1486 * Taken from weston shared/os-compatibility.c
1488 * Create a new, unique, anonymous file of the given size, and
1489 * return the file descriptor for it. The file descriptor is set
1490 * CLOEXEC. The file is immediately suitable for mmap()'ing
1491 * the given size at offset zero.
1493 * The file should not have a permanent backing store like a disk,
1494 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1496 * The file name is deleted from the file system.
1498 * The file is suitable for buffer sharing between processes by
1499 * transmitting the file descriptor over Unix sockets using the
1500 * SCM_RIGHTS methods.
1502 * If the C library implements posix_fallocate(), it is used to
1503 * guarantee that disk space is available for the file at the
1504 * given size. If disk space is insufficent, errno is set to ENOSPC.
1505 * If posix_fallocate() is not supported, program may receive
1506 * SIGBUS on accessing mmap()'ed file contents instead.
1509 os_create_anonymous_file(off_t size
)
1511 static const char templ
[] = "/mesa-shared-XXXXXX";
1517 path
= getenv("XDG_RUNTIME_DIR");
1523 name
= malloc(strlen(path
) + sizeof(templ
));
1528 strcat(name
, templ
);
1530 fd
= create_tmpfile_cloexec(name
);
1537 ret
= ftruncate(fd
, size
);
1548 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1549 int format
, int w
, int h
,
1550 void **data
, int *size
,
1551 struct wl_buffer
**buffer
)
1553 struct dri2_egl_display
*dri2_dpy
=
1554 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1555 struct wl_shm_pool
*pool
;
1556 int fd
, stride
, size_map
;
1559 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1560 size_map
= h
* stride
;
1562 /* Create a sharable buffer */
1563 fd
= os_create_anonymous_file(size_map
);
1567 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1568 if (data_map
== MAP_FAILED
) {
1573 /* Share it in a wl_buffer */
1574 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1575 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1576 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1577 wl_shm_pool_destroy(pool
);
1586 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1588 struct dri2_egl_display
*dri2_dpy
=
1589 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1591 /* we need to do the following operations only once per frame */
1592 if (dri2_surf
->back
)
1595 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1596 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1598 dri2_wl_release_buffers(dri2_surf
);
1600 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1601 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1602 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1603 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1604 dri2_surf
->current
= NULL
;
1607 /* find back buffer */
1609 /* There might be a buffer release already queued that wasn't processed */
1610 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1612 /* try get free buffer already created */
1613 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1614 if (!dri2_surf
->color_buffers
[i
].locked
&&
1615 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1616 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1621 /* else choose any another free location */
1622 if (!dri2_surf
->back
) {
1623 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1624 if (!dri2_surf
->color_buffers
[i
].locked
) {
1625 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1626 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1628 dri2_surf
->base
.Width
,
1629 dri2_surf
->base
.Height
,
1630 &dri2_surf
->back
->data
,
1631 &dri2_surf
->back
->data_size
,
1632 &dri2_surf
->back
->wl_buffer
)) {
1633 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1636 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1637 &wl_buffer_listener
, dri2_surf
);
1643 if (!dri2_surf
->back
) {
1644 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1648 dri2_surf
->back
->locked
= true;
1650 /* If we have an extra unlocked buffer at this point, we had to do triple
1651 * buffering for a while, but now can go back to just double buffering.
1652 * That means we can free any unlocked buffer now. */
1653 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1654 if (!dri2_surf
->color_buffers
[i
].locked
&&
1655 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1656 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1657 munmap(dri2_surf
->color_buffers
[i
].data
,
1658 dri2_surf
->color_buffers
[i
].data_size
);
1659 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1660 dri2_surf
->color_buffers
[i
].data
= NULL
;
1668 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1670 /* if there has been a resize: */
1671 if (!dri2_surf
->current
)
1674 return dri2_surf
->current
->data
;
1678 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1680 assert(dri2_surf
->back
);
1681 return dri2_surf
->back
->data
;
1685 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1687 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1689 while (dri2_surf
->throttle_callback
!= NULL
)
1690 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1691 dri2_surf
->wl_queue
) == -1)
1694 if (dri2_surf
->base
.SwapInterval
> 0) {
1695 dri2_surf
->throttle_callback
=
1696 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1697 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1698 &throttle_listener
, dri2_surf
);
1701 dri2_surf
->current
= dri2_surf
->back
;
1702 dri2_surf
->back
= NULL
;
1704 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1705 dri2_surf
->current
->wl_buffer
,
1706 dri2_surf
->dx
, dri2_surf
->dy
);
1708 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1709 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1710 /* reset resize growing parameters */
1714 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1715 0, 0, INT32_MAX
, INT32_MAX
);
1716 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1718 /* If we're not waiting for a frame callback then we'll at least throttle
1719 * to a sync callback so that we always give a chance for the compositor to
1720 * handle the commit and send a release event before checking for a free
1722 if (dri2_surf
->throttle_callback
== NULL
) {
1723 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1724 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1725 &throttle_listener
, dri2_surf
);
1728 wl_display_flush(dri2_dpy
->wl_dpy
);
1732 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1733 int *x
, int *y
, int *w
, int *h
,
1734 void *loaderPrivate
)
1736 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1738 (void) swrast_update_buffers(dri2_surf
);
1741 *w
= dri2_surf
->base
.Width
;
1742 *h
= dri2_surf
->base
.Height
;
1746 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1747 int x
, int y
, int w
, int h
,
1748 char *data
, void *loaderPrivate
)
1750 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1751 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1752 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1753 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1754 int dst_stride
= copy_width
;
1757 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1759 memset(data
, 0, copy_width
* h
);
1763 assert(data
!= src
);
1764 assert(copy_width
<= src_stride
);
1767 src
+= y
* src_stride
;
1770 if (copy_width
> src_stride
-x_offset
)
1771 copy_width
= src_stride
-x_offset
;
1772 if (h
> dri2_surf
->base
.Height
-y
)
1773 h
= dri2_surf
->base
.Height
-y
;
1776 memcpy(dst
, src
, copy_width
);
1783 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1784 int x
, int y
, int w
, int h
, int stride
,
1785 char *data
, void *loaderPrivate
)
1787 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1788 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1789 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1790 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1793 assert(copy_width
<= stride
);
1795 (void) swrast_update_buffers(dri2_surf
);
1796 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1798 /* partial copy, copy old content */
1799 if (copy_width
< dst_stride
)
1800 dri2_wl_swrast_get_image(draw
, 0, 0,
1801 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1802 dst
, loaderPrivate
);
1805 dst
+= y
* dst_stride
;
1809 /* drivers expect we do these checks (and some rely on it) */
1810 if (copy_width
> dst_stride
-x_offset
)
1811 copy_width
= dst_stride
-x_offset
;
1812 if (h
> dri2_surf
->base
.Height
-y
)
1813 h
= dri2_surf
->base
.Height
-y
;
1816 memcpy(dst
, src
, copy_width
);
1820 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1824 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1825 int x
, int y
, int w
, int h
,
1826 char *data
, void *loaderPrivate
)
1828 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1831 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1832 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1833 stride
, data
, loaderPrivate
);
1837 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1839 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1840 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1842 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1847 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1849 struct dri2_egl_display
*dri2_dpy
= data
;
1852 case WL_SHM_FORMAT_ARGB8888
:
1853 dri2_dpy
->formats
|= HAS_ARGB8888
;
1855 case WL_SHM_FORMAT_XRGB8888
:
1856 dri2_dpy
->formats
|= HAS_XRGB8888
;
1858 case WL_SHM_FORMAT_RGB565
:
1859 dri2_dpy
->formats
|= HAS_RGB565
;
1864 static const struct wl_shm_listener shm_listener
= {
1865 .format
= shm_handle_format
1869 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1870 uint32_t name
, const char *interface
,
1873 struct dri2_egl_display
*dri2_dpy
= data
;
1875 if (strcmp(interface
, "wl_shm") == 0) {
1877 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1878 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1882 static const struct wl_registry_listener registry_listener_swrast
= {
1883 .global
= registry_handle_global_swrast
,
1884 .global_remove
= registry_handle_global_remove
1887 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1888 .authenticate
= NULL
,
1889 .create_window_surface
= dri2_wl_create_window_surface
,
1890 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1891 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1892 .destroy_surface
= dri2_wl_destroy_surface
,
1893 .create_image
= dri2_create_image_khr
,
1894 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1895 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1896 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1897 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1898 .copy_buffers
= dri2_fallback_copy_buffers
,
1899 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1900 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1901 .get_sync_values
= dri2_fallback_get_sync_values
,
1902 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1905 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1906 .base
= { __DRI_SWRAST_LOADER
, 2 },
1908 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1909 .putImage
= dri2_wl_swrast_put_image
,
1910 .getImage
= dri2_wl_swrast_get_image
,
1911 .putImage2
= dri2_wl_swrast_put_image2
,
1914 static const __DRIextension
*swrast_loader_extensions
[] = {
1915 &swrast_loader_extension
.base
,
1916 &image_lookup_extension
.base
,
1921 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1923 struct dri2_egl_display
*dri2_dpy
;
1925 loader_set_logger(_eglLog
);
1927 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1929 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1932 disp
->DriverData
= (void *) dri2_dpy
;
1933 if (disp
->PlatformDisplay
== NULL
) {
1934 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1935 if (dri2_dpy
->wl_dpy
== NULL
)
1937 dri2_dpy
->own_device
= true;
1939 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1942 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1944 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1945 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1948 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1949 dri2_dpy
->wl_queue
);
1951 if (dri2_dpy
->own_device
)
1952 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1954 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1955 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1956 ®istry_listener_swrast
, dri2_dpy
);
1958 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1961 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1964 dri2_dpy
->driver_name
= strdup("swrast");
1965 if (!dri2_load_driver_swrast(disp
))
1968 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1970 if (!dri2_create_screen(disp
))
1973 if (!dri2_setup_extensions(disp
))
1976 dri2_setup_screen(disp
);
1978 dri2_wl_setup_swap_interval(disp
);
1980 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1981 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1985 /* Fill vtbl last to prevent accidentally calling virtual function during
1988 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1993 dri2_display_destroy(disp
);
1998 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2000 EGLBoolean initialized
= EGL_FALSE
;
2002 if (!disp
->Options
.ForceSoftware
)
2003 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2006 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2013 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2015 if (dri2_dpy
->wl_drm
)
2016 wl_drm_destroy(dri2_dpy
->wl_drm
);
2017 if (dri2_dpy
->wl_dmabuf
)
2018 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2019 if (dri2_dpy
->wl_shm
)
2020 wl_shm_destroy(dri2_dpy
->wl_shm
);
2021 if (dri2_dpy
->wl_registry
)
2022 wl_registry_destroy(dri2_dpy
->wl_registry
);
2023 if (dri2_dpy
->wl_queue
)
2024 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2025 if (dri2_dpy
->wl_dpy_wrapper
)
2026 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2027 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb8888
);
2028 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb8888
);
2029 u_vector_finish(&dri2_dpy
->wl_modifiers
.rgb565
);
2030 if (dri2_dpy
->own_device
)
2031 wl_display_disconnect(dri2_dpy
->wl_dpy
);