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 (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
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
);
304 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
306 struct dri2_egl_display
*dri2_dpy
=
307 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
309 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
310 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
311 !dri2_surf
->color_buffers
[i
].locked
)
312 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
313 if (dri2_surf
->color_buffers
[i
].dri_image
)
314 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
315 if (dri2_surf
->color_buffers
[i
].linear_copy
)
316 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
317 if (dri2_surf
->color_buffers
[i
].data
)
318 munmap(dri2_surf
->color_buffers
[i
].data
,
319 dri2_surf
->color_buffers
[i
].data_size
);
321 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
322 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
323 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
324 dri2_surf
->color_buffers
[i
].data
= NULL
;
325 dri2_surf
->color_buffers
[i
].locked
= false;
329 dri2_egl_surface_free_local_buffers(dri2_surf
);
333 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
335 struct dri2_egl_display
*dri2_dpy
=
336 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
338 unsigned int dri_image_format
;
342 /* currently supports three WL DRM formats,
343 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
344 * and WL_DRM_FORMAT_RGB565
346 switch (dri2_surf
->format
) {
347 case WL_DRM_FORMAT_ARGB8888
:
348 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
349 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
350 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
352 case WL_DRM_FORMAT_XRGB8888
:
353 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
354 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
355 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
357 case WL_DRM_FORMAT_RGB565
:
358 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
359 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
360 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
363 /* format is not supported */
367 /* There might be a buffer release already queued that wasn't processed */
368 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
370 while (dri2_surf
->back
== NULL
) {
371 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
372 /* Get an unlocked buffer, preferrably one with a dri_buffer
373 * already allocated. */
374 if (dri2_surf
->color_buffers
[i
].locked
)
376 if (dri2_surf
->back
== NULL
)
377 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
378 else if (dri2_surf
->back
->dri_image
== NULL
)
379 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
385 /* If we don't have a buffer, then block on the server to release one for
386 * us, and try again. wl_display_dispatch_queue will process any pending
387 * events, however not all servers flush on issuing a buffer release
388 * event. So, we spam the server with roundtrips as they always cause a
391 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
392 dri2_surf
->wl_queue
) < 0)
396 if (dri2_surf
->back
== NULL
)
399 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
401 if (dri2_dpy
->is_different_gpu
&&
402 dri2_surf
->back
->linear_copy
== NULL
) {
403 /* The LINEAR modifier should be a perfect alias of the LINEAR use
404 * flag; try the new interface first before the old, then fall back. */
405 if (dri2_dpy
->image
->base
.version
>= 15 &&
406 dri2_dpy
->image
->createImageWithModifiers
) {
407 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
409 dri2_surf
->back
->linear_copy
=
410 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
411 dri2_surf
->base
.Width
,
412 dri2_surf
->base
.Height
,
418 dri2_surf
->back
->linear_copy
=
419 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
420 dri2_surf
->base
.Width
,
421 dri2_surf
->base
.Height
,
424 __DRI_IMAGE_USE_LINEAR
,
427 if (dri2_surf
->back
->linear_copy
== NULL
)
431 if (dri2_surf
->back
->dri_image
== NULL
) {
432 /* If our DRIImage implementation does not support
433 * createImageWithModifiers, then fall back to the old createImage,
434 * and hope it allocates an image which is acceptable to the winsys.
436 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
437 dri2_dpy
->image
->createImageWithModifiers
) {
438 dri2_surf
->back
->dri_image
=
439 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
440 dri2_surf
->base
.Width
,
441 dri2_surf
->base
.Height
,
447 dri2_surf
->back
->dri_image
=
448 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
449 dri2_surf
->base
.Width
,
450 dri2_surf
->base
.Height
,
452 dri2_dpy
->is_different_gpu
?
457 dri2_surf
->back
->age
= 0;
459 if (dri2_surf
->back
->dri_image
== NULL
)
462 dri2_surf
->back
->locked
= true;
469 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
471 struct dri2_egl_display
*dri2_dpy
=
472 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
476 image
= dri2_surf
->back
->dri_image
;
478 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
479 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
481 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
483 buffer
->pitch
= pitch
;
489 update_buffers(struct dri2_egl_surface
*dri2_surf
)
491 struct dri2_egl_display
*dri2_dpy
=
492 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
494 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
495 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
497 dri2_wl_release_buffers(dri2_surf
);
499 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
500 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
501 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
502 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
505 if (get_back_bo(dri2_surf
) < 0) {
506 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
510 /* If we have an extra unlocked buffer at this point, we had to do triple
511 * buffering for a while, but now can go back to just double buffering.
512 * That means we can free any unlocked buffer now. */
513 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
514 if (!dri2_surf
->color_buffers
[i
].locked
&&
515 dri2_surf
->color_buffers
[i
].wl_buffer
) {
516 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
517 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
518 if (dri2_dpy
->is_different_gpu
)
519 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
520 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
521 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
522 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
530 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
531 int *width
, int *height
,
532 unsigned int *attachments
, int count
,
533 int *out_count
, void *loaderPrivate
)
535 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
538 if (update_buffers(dri2_surf
) < 0)
541 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
544 switch (attachments
[i
]) {
545 case __DRI_BUFFER_BACK_LEFT
:
546 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
549 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
553 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
556 dri2_surf
->buffers
[j
] = *local
;
565 *width
= dri2_surf
->base
.Width
;
566 *height
= dri2_surf
->base
.Height
;
568 return dri2_surf
->buffers
;
572 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
573 int *width
, int *height
,
574 unsigned int *attachments
, int count
,
575 int *out_count
, void *loaderPrivate
)
577 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
578 unsigned int *attachments_with_format
;
582 switch (dri2_surf
->format
) {
583 case WL_DRM_FORMAT_ARGB8888
:
584 case WL_DRM_FORMAT_XRGB8888
:
587 case WL_DRM_FORMAT_RGB565
:
591 /* format is not supported */
595 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
596 if (!attachments_with_format
) {
601 for (int i
= 0; i
< count
; ++i
) {
602 attachments_with_format
[2*i
] = attachments
[i
];
603 attachments_with_format
[2*i
+ 1] = bpp
;
607 dri2_wl_get_buffers_with_format(driDrawable
,
609 attachments_with_format
, count
,
610 out_count
, loaderPrivate
);
612 free(attachments_with_format
);
618 image_get_buffers(__DRIdrawable
*driDrawable
,
622 uint32_t buffer_mask
,
623 struct __DRIimageList
*buffers
)
625 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
627 if (update_buffers(dri2_surf
) < 0)
630 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
631 buffers
->back
= dri2_surf
->back
->dri_image
;
637 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
640 (void) loaderPrivate
;
643 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
644 .base
= { __DRI_DRI2_LOADER
, 3 },
646 .getBuffers
= dri2_wl_get_buffers
,
647 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
648 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
651 static const __DRIimageLoaderExtension image_loader_extension
= {
652 .base
= { __DRI_IMAGE_LOADER
, 1 },
654 .getBuffers
= image_get_buffers
,
655 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
659 wayland_throttle_callback(void *data
,
660 struct wl_callback
*callback
,
663 struct dri2_egl_surface
*dri2_surf
= data
;
665 dri2_surf
->throttle_callback
= NULL
;
666 wl_callback_destroy(callback
);
669 static const struct wl_callback_listener throttle_listener
= {
670 .done
= wayland_throttle_callback
673 static struct wl_buffer
*
674 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
675 struct dri2_egl_surface
*dri2_surf
,
678 struct wl_buffer
*ret
;
679 int width
, height
, fourcc
, num_planes
;
681 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
682 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
683 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
);
684 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
687 if (dri2_dpy
->wl_dmabuf
&& dri2_dpy
->image
->base
.version
>= 15) {
688 struct zwp_linux_buffer_params_v1
*params
;
692 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
694 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
697 /* We don't need a wrapper for wl_dmabuf objects, because we have to
698 * create the intermediate params object; we can set the queue on this,
699 * and the wl_buffer inherits it race-free. */
700 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
702 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
704 for (i
= 0; i
< num_planes
; i
++) {
706 int stride
, offset
, fd
;
711 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
713 zwp_linux_buffer_params_v1_destroy(params
);
717 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
718 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_STRIDE
,
720 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_OFFSET
,
722 if (image
!= p_image
)
723 dri2_dpy
->image
->destroyImage(p_image
);
725 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
730 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
732 zwp_linux_buffer_params_v1_destroy(params
);
733 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
734 struct wl_drm
*wl_drm
=
735 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
741 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
742 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
743 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
747 struct wl_drm
*wl_drm
=
748 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
754 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
755 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
756 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
763 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
767 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
768 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
771 for (int i
= 0; i
< n_rects
; i
++) {
772 const int *rect
= &rects
[i
* 4];
774 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
776 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
782 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
785 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
791 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
792 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
794 while (dri2_surf
->throttle_callback
!= NULL
)
795 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
796 dri2_surf
->wl_queue
) == -1)
799 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
800 if (dri2_surf
->color_buffers
[i
].age
> 0)
801 dri2_surf
->color_buffers
[i
].age
++;
803 /* Make sure we have a back buffer in case we're swapping without ever
805 if (get_back_bo(dri2_surf
) < 0)
806 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
808 if (draw
->SwapInterval
> 0) {
809 dri2_surf
->throttle_callback
=
810 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
811 wl_callback_add_listener(dri2_surf
->throttle_callback
,
812 &throttle_listener
, dri2_surf
);
815 dri2_surf
->back
->age
= 1;
816 dri2_surf
->current
= dri2_surf
->back
;
817 dri2_surf
->back
= NULL
;
819 if (!dri2_surf
->current
->wl_buffer
) {
822 if (dri2_dpy
->is_different_gpu
)
823 image
= dri2_surf
->current
->linear_copy
;
825 image
= dri2_surf
->current
->dri_image
;
827 dri2_surf
->current
->wl_buffer
=
828 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
830 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
831 &wl_buffer_listener
, dri2_surf
);
834 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
835 dri2_surf
->current
->wl_buffer
,
836 dri2_surf
->dx
, dri2_surf
->dy
);
838 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
839 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
840 /* reset resize growing parameters */
844 /* If the compositor doesn't support damage_buffer, we deliberately
845 * ignore the damage region and post maximum damage, due to
846 * https://bugs.freedesktop.org/78190 */
847 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
848 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
849 0, 0, INT32_MAX
, INT32_MAX
);
851 if (dri2_dpy
->is_different_gpu
) {
852 _EGLContext
*ctx
= _eglGetCurrentContext();
853 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
854 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
855 dri2_surf
->current
->linear_copy
,
856 dri2_surf
->current
->dri_image
,
857 0, 0, dri2_surf
->base
.Width
,
858 dri2_surf
->base
.Height
,
859 0, 0, dri2_surf
->base
.Width
,
860 dri2_surf
->base
.Height
, 0);
863 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
864 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
866 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
868 /* If we're not waiting for a frame callback then we'll at least throttle
869 * to a sync callback so that we always give a chance for the compositor to
870 * handle the commit and send a release event before checking for a free
872 if (dri2_surf
->throttle_callback
== NULL
) {
873 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
874 wl_callback_add_listener(dri2_surf
->throttle_callback
,
875 &throttle_listener
, dri2_surf
);
878 wl_display_flush(dri2_dpy
->wl_dpy
);
884 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
885 _EGLDisplay
*disp
, _EGLSurface
*surface
)
887 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
889 if (get_back_bo(dri2_surf
) < 0) {
890 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
894 return dri2_surf
->back
->age
;
898 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
900 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
903 static struct wl_buffer
*
904 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
908 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
909 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
910 __DRIimage
*image
= dri2_img
->dri_image
;
911 struct wl_buffer
*buffer
;
914 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
916 case __DRI_IMAGE_FORMAT_ARGB8888
:
917 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
920 case __DRI_IMAGE_FORMAT_XRGB8888
:
921 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
928 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
930 /* The buffer object will have been created with our internal event queue
931 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
932 * buffer to be used by the application so we'll reset it to the display's
933 * default event queue. This isn't actually racy, as the only event the
934 * buffer can get is a buffer release, which doesn't happen with an explicit
937 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
942 _eglError(EGL_BAD_MATCH
, "unsupported image format");
947 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
949 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
952 if (dri2_dpy
->is_render_node
) {
953 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
954 "authenticate for render-nodes");
957 dri2_dpy
->authenticated
= false;
959 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
960 if (roundtrip(dri2_dpy
) < 0)
963 if (!dri2_dpy
->authenticated
)
966 /* reset authenticated */
967 dri2_dpy
->authenticated
= true;
973 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
975 struct dri2_egl_display
*dri2_dpy
= data
;
978 dri2_dpy
->device_name
= strdup(device
);
979 if (!dri2_dpy
->device_name
)
982 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
983 if (dri2_dpy
->fd
== -1) {
984 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
985 dri2_dpy
->device_name
, strerror(errno
));
989 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
990 dri2_dpy
->authenticated
= true;
992 drmGetMagic(dri2_dpy
->fd
, &magic
);
993 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
998 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1000 struct dri2_egl_display
*dri2_dpy
= data
;
1003 case WL_DRM_FORMAT_ARGB8888
:
1004 dri2_dpy
->formats
|= HAS_ARGB8888
;
1006 case WL_DRM_FORMAT_XRGB8888
:
1007 dri2_dpy
->formats
|= HAS_XRGB8888
;
1009 case WL_DRM_FORMAT_RGB565
:
1010 dri2_dpy
->formats
|= HAS_RGB565
;
1016 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1018 struct dri2_egl_display
*dri2_dpy
= data
;
1020 dri2_dpy
->capabilities
= value
;
1024 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1026 struct dri2_egl_display
*dri2_dpy
= data
;
1028 dri2_dpy
->authenticated
= true;
1031 static const struct wl_drm_listener drm_listener
= {
1032 .device
= drm_handle_device
,
1033 .format
= drm_handle_format
,
1034 .authenticated
= drm_handle_authenticated
,
1035 .capabilities
= drm_handle_capabilities
1039 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1042 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1046 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1047 uint32_t format
, uint32_t modifier_hi
,
1048 uint32_t modifier_lo
)
1050 struct dri2_egl_display
*dri2_dpy
= data
;
1051 uint64_t *mod
= NULL
;
1053 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1054 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1058 case WL_DRM_FORMAT_ARGB8888
:
1059 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1061 case WL_DRM_FORMAT_XRGB8888
:
1062 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1064 case WL_DRM_FORMAT_RGB565
:
1065 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1074 *mod
= (uint64_t) modifier_hi
<< 32;
1075 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1078 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1079 .format
= dmabuf_ignore_format
,
1080 .modifier
= dmabuf_handle_modifier
,
1084 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1085 uint32_t name
, const char *interface
,
1088 struct dri2_egl_display
*dri2_dpy
= data
;
1090 if (strcmp(interface
, "wl_drm") == 0) {
1092 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1093 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1094 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1095 dri2_dpy
->wl_dmabuf
=
1096 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1098 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1104 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1109 static const struct wl_registry_listener registry_listener_drm
= {
1110 .global
= registry_handle_global_drm
,
1111 .global_remove
= registry_handle_global_remove
1115 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1117 /* We can't use values greater than 1 on Wayland because we are using the
1118 * frame callback to synchronise the frame and the only way we be sure to
1119 * get a frame callback is to attach a new buffer. Therefore we can't just
1120 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1122 dri2_setup_swap_interval(disp
, 1);
1125 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1126 .authenticate
= dri2_wl_authenticate
,
1127 .create_window_surface
= dri2_wl_create_window_surface
,
1128 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1129 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1130 .destroy_surface
= dri2_wl_destroy_surface
,
1131 .create_image
= dri2_create_image_khr
,
1132 .swap_buffers
= dri2_wl_swap_buffers
,
1133 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1134 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1135 .set_damage_region
= dri2_fallback_set_damage_region
,
1136 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1137 .copy_buffers
= dri2_fallback_copy_buffers
,
1138 .query_buffer_age
= dri2_wl_query_buffer_age
,
1139 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1140 .get_sync_values
= dri2_fallback_get_sync_values
,
1141 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1144 static const __DRIextension
*dri2_loader_extensions
[] = {
1145 &dri2_loader_extension
.base
,
1146 &image_loader_extension
.base
,
1147 &image_lookup_extension
.base
,
1148 &use_invalidate
.base
,
1152 static const __DRIextension
*image_loader_extensions
[] = {
1153 &image_loader_extension
.base
,
1154 &image_lookup_extension
.base
,
1155 &use_invalidate
.base
,
1160 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1162 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1163 static const struct {
1164 const char *format_name
;
1166 unsigned int rgba_masks
[4];
1168 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1169 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1170 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1172 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1173 unsigned int count
= 0;
1175 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1176 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1177 struct dri2_egl_config
*dri2_conf
;
1179 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1182 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1183 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1185 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1192 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1193 if (!format_count
[i
]) {
1194 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1195 visuals
[i
].format_name
);
1199 return (count
!= 0);
1203 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1205 struct dri2_egl_display
*dri2_dpy
;
1207 loader_set_logger(_eglLog
);
1209 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1211 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1214 disp
->DriverData
= (void *) dri2_dpy
;
1215 if (disp
->PlatformDisplay
== NULL
) {
1216 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1217 if (dri2_dpy
->wl_dpy
== NULL
)
1219 dri2_dpy
->own_device
= true;
1221 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1224 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1225 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1226 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1230 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1232 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1233 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1236 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1237 dri2_dpy
->wl_queue
);
1239 if (dri2_dpy
->own_device
)
1240 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1242 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1243 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1244 ®istry_listener_drm
, dri2_dpy
);
1245 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1248 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1251 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1254 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1255 &dri2_dpy
->is_different_gpu
);
1256 if (dri2_dpy
->is_different_gpu
) {
1257 free(dri2_dpy
->device_name
);
1258 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1259 if (!dri2_dpy
->device_name
) {
1260 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1261 "for requested GPU");
1266 /* we have to do the check now, because loader_get_user_preferred_fd
1267 * will return a render-node when the requested gpu is different
1268 * to the server, but also if the client asks for the same gpu than
1269 * the server by requesting its pci-id */
1270 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1272 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1273 if (dri2_dpy
->driver_name
== NULL
) {
1274 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1278 /* render nodes cannot use Gem names, and thus do not support
1279 * the __DRI_DRI2_LOADER extension */
1280 if (!dri2_dpy
->is_render_node
) {
1281 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1282 if (!dri2_load_driver(disp
)) {
1283 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1287 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1288 if (!dri2_load_driver_dri3(disp
)) {
1289 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1294 if (!dri2_create_screen(disp
))
1297 if (!dri2_setup_extensions(disp
))
1300 dri2_setup_screen(disp
);
1302 dri2_wl_setup_swap_interval(disp
);
1304 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1305 * createImageFromFds support indicates that Prime export/import
1306 * is supported by the driver. Fall back to
1307 * gem names if we don't have Prime support. */
1309 if (dri2_dpy
->image
->base
.version
< 7 ||
1310 dri2_dpy
->image
->createImageFromFds
== NULL
)
1311 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1313 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1314 * The server needs to accept them */
1315 if (dri2_dpy
->is_render_node
&&
1316 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1317 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1321 if (dri2_dpy
->is_different_gpu
&&
1322 (dri2_dpy
->image
->base
.version
< 9 ||
1323 dri2_dpy
->image
->blitImage
== NULL
)) {
1324 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1325 "Image extension in the driver is not "
1326 "compatible. Version 9 or later and blitImage() "
1331 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1332 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1336 dri2_set_WL_bind_wayland_display(drv
, disp
);
1337 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1338 * because the buffer of the EGLImage has likely a tiling mode the server
1339 * gpu won't support. These is no way to check for now. Thus do not support the
1341 if (!dri2_dpy
->is_different_gpu
)
1342 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1344 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1346 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1348 /* Fill vtbl last to prevent accidentally calling virtual function during
1351 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1356 dri2_display_destroy(disp
);
1361 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1363 if (format
== WL_SHM_FORMAT_RGB565
)
1365 else /* ARGB8888 || XRGB8888 */
1370 * Taken from weston shared/os-compatibility.c
1373 #ifndef HAVE_MKOSTEMP
1376 set_cloexec_or_close(int fd
)
1383 flags
= fcntl(fd
, F_GETFD
);
1387 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1400 * Taken from weston shared/os-compatibility.c
1404 create_tmpfile_cloexec(char *tmpname
)
1408 #ifdef HAVE_MKOSTEMP
1409 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1413 fd
= mkstemp(tmpname
);
1415 fd
= set_cloexec_or_close(fd
);
1424 * Taken from weston shared/os-compatibility.c
1426 * Create a new, unique, anonymous file of the given size, and
1427 * return the file descriptor for it. The file descriptor is set
1428 * CLOEXEC. The file is immediately suitable for mmap()'ing
1429 * the given size at offset zero.
1431 * The file should not have a permanent backing store like a disk,
1432 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1434 * The file name is deleted from the file system.
1436 * The file is suitable for buffer sharing between processes by
1437 * transmitting the file descriptor over Unix sockets using the
1438 * SCM_RIGHTS methods.
1440 * If the C library implements posix_fallocate(), it is used to
1441 * guarantee that disk space is available for the file at the
1442 * given size. If disk space is insufficent, errno is set to ENOSPC.
1443 * If posix_fallocate() is not supported, program may receive
1444 * SIGBUS on accessing mmap()'ed file contents instead.
1447 os_create_anonymous_file(off_t size
)
1449 static const char template[] = "/mesa-shared-XXXXXX";
1455 path
= getenv("XDG_RUNTIME_DIR");
1461 name
= malloc(strlen(path
) + sizeof(template));
1466 strcat(name
, template);
1468 fd
= create_tmpfile_cloexec(name
);
1475 ret
= ftruncate(fd
, size
);
1486 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1487 int format
, int w
, int h
,
1488 void **data
, int *size
,
1489 struct wl_buffer
**buffer
)
1491 struct dri2_egl_display
*dri2_dpy
=
1492 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1493 struct wl_shm_pool
*pool
;
1494 int fd
, stride
, size_map
;
1497 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1498 size_map
= h
* stride
;
1500 /* Create a sharable buffer */
1501 fd
= os_create_anonymous_file(size_map
);
1505 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1506 if (data_map
== MAP_FAILED
) {
1511 /* Share it in a wl_buffer */
1512 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1513 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1514 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1515 wl_shm_pool_destroy(pool
);
1524 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1526 struct dri2_egl_display
*dri2_dpy
=
1527 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1529 /* we need to do the following operations only once per frame */
1530 if (dri2_surf
->back
)
1533 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1534 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1536 dri2_wl_release_buffers(dri2_surf
);
1538 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1539 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1540 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1541 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1542 dri2_surf
->current
= NULL
;
1545 /* find back buffer */
1547 /* There might be a buffer release already queued that wasn't processed */
1548 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1550 /* try get free buffer already created */
1551 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1552 if (!dri2_surf
->color_buffers
[i
].locked
&&
1553 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1554 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1559 /* else choose any another free location */
1560 if (!dri2_surf
->back
) {
1561 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1562 if (!dri2_surf
->color_buffers
[i
].locked
) {
1563 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1564 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1566 dri2_surf
->base
.Width
,
1567 dri2_surf
->base
.Height
,
1568 &dri2_surf
->back
->data
,
1569 &dri2_surf
->back
->data_size
,
1570 &dri2_surf
->back
->wl_buffer
)) {
1571 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1574 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1575 &wl_buffer_listener
, dri2_surf
);
1581 if (!dri2_surf
->back
) {
1582 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1586 dri2_surf
->back
->locked
= true;
1588 /* If we have an extra unlocked buffer at this point, we had to do triple
1589 * buffering for a while, but now can go back to just double buffering.
1590 * That means we can free any unlocked buffer now. */
1591 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1592 if (!dri2_surf
->color_buffers
[i
].locked
&&
1593 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1594 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1595 munmap(dri2_surf
->color_buffers
[i
].data
,
1596 dri2_surf
->color_buffers
[i
].data_size
);
1597 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1598 dri2_surf
->color_buffers
[i
].data
= NULL
;
1606 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1608 /* if there has been a resize: */
1609 if (!dri2_surf
->current
)
1612 return dri2_surf
->current
->data
;
1616 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1618 assert(dri2_surf
->back
);
1619 return dri2_surf
->back
->data
;
1623 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1625 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1627 while (dri2_surf
->throttle_callback
!= NULL
)
1628 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1629 dri2_surf
->wl_queue
) == -1)
1632 if (dri2_surf
->base
.SwapInterval
> 0) {
1633 dri2_surf
->throttle_callback
=
1634 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1635 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1636 &throttle_listener
, dri2_surf
);
1639 dri2_surf
->current
= dri2_surf
->back
;
1640 dri2_surf
->back
= NULL
;
1642 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1643 dri2_surf
->current
->wl_buffer
,
1644 dri2_surf
->dx
, dri2_surf
->dy
);
1646 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1647 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1648 /* reset resize growing parameters */
1652 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1653 0, 0, INT32_MAX
, INT32_MAX
);
1654 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1656 /* If we're not waiting for a frame callback then we'll at least throttle
1657 * to a sync callback so that we always give a chance for the compositor to
1658 * handle the commit and send a release event before checking for a free
1660 if (dri2_surf
->throttle_callback
== NULL
) {
1661 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1662 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1663 &throttle_listener
, dri2_surf
);
1666 wl_display_flush(dri2_dpy
->wl_dpy
);
1670 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1671 int *x
, int *y
, int *w
, int *h
,
1672 void *loaderPrivate
)
1674 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1676 (void) swrast_update_buffers(dri2_surf
);
1679 *w
= dri2_surf
->base
.Width
;
1680 *h
= dri2_surf
->base
.Height
;
1684 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1685 int x
, int y
, int w
, int h
,
1686 char *data
, void *loaderPrivate
)
1688 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1689 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1690 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1691 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1692 int dst_stride
= copy_width
;
1695 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1697 memset(data
, 0, copy_width
* h
);
1701 assert(data
!= src
);
1702 assert(copy_width
<= src_stride
);
1705 src
+= y
* src_stride
;
1708 if (copy_width
> src_stride
-x_offset
)
1709 copy_width
= src_stride
-x_offset
;
1710 if (h
> dri2_surf
->base
.Height
-y
)
1711 h
= dri2_surf
->base
.Height
-y
;
1714 memcpy(dst
, src
, copy_width
);
1721 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1722 int x
, int y
, int w
, int h
, int stride
,
1723 char *data
, void *loaderPrivate
)
1725 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1726 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1727 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1728 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1731 assert(copy_width
<= stride
);
1733 (void) swrast_update_buffers(dri2_surf
);
1734 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1736 /* partial copy, copy old content */
1737 if (copy_width
< dst_stride
)
1738 dri2_wl_swrast_get_image(draw
, 0, 0,
1739 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1740 dst
, loaderPrivate
);
1743 dst
+= y
* dst_stride
;
1747 /* drivers expect we do these checks (and some rely on it) */
1748 if (copy_width
> dst_stride
-x_offset
)
1749 copy_width
= dst_stride
-x_offset
;
1750 if (h
> dri2_surf
->base
.Height
-y
)
1751 h
= dri2_surf
->base
.Height
-y
;
1754 memcpy(dst
, src
, copy_width
);
1758 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1762 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1763 int x
, int y
, int w
, int h
,
1764 char *data
, void *loaderPrivate
)
1766 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1769 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1770 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1771 stride
, data
, loaderPrivate
);
1775 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1777 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1778 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1780 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1785 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1787 struct dri2_egl_display
*dri2_dpy
= data
;
1790 case WL_SHM_FORMAT_ARGB8888
:
1791 dri2_dpy
->formats
|= HAS_ARGB8888
;
1793 case WL_SHM_FORMAT_XRGB8888
:
1794 dri2_dpy
->formats
|= HAS_XRGB8888
;
1796 case WL_SHM_FORMAT_RGB565
:
1797 dri2_dpy
->formats
|= HAS_RGB565
;
1802 static const struct wl_shm_listener shm_listener
= {
1803 .format
= shm_handle_format
1807 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1808 uint32_t name
, const char *interface
,
1811 struct dri2_egl_display
*dri2_dpy
= data
;
1813 if (strcmp(interface
, "wl_shm") == 0) {
1815 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1816 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1820 static const struct wl_registry_listener registry_listener_swrast
= {
1821 .global
= registry_handle_global_swrast
,
1822 .global_remove
= registry_handle_global_remove
1825 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1826 .authenticate
= NULL
,
1827 .create_window_surface
= dri2_wl_create_window_surface
,
1828 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1829 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1830 .destroy_surface
= dri2_wl_destroy_surface
,
1831 .create_image
= dri2_create_image_khr
,
1832 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1833 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1834 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1835 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1836 .copy_buffers
= dri2_fallback_copy_buffers
,
1837 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1838 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1839 .get_sync_values
= dri2_fallback_get_sync_values
,
1840 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1843 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1844 .base
= { __DRI_SWRAST_LOADER
, 2 },
1846 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1847 .putImage
= dri2_wl_swrast_put_image
,
1848 .getImage
= dri2_wl_swrast_get_image
,
1849 .putImage2
= dri2_wl_swrast_put_image2
,
1852 static const __DRIextension
*swrast_loader_extensions
[] = {
1853 &swrast_loader_extension
.base
,
1854 &image_lookup_extension
.base
,
1859 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1861 struct dri2_egl_display
*dri2_dpy
;
1863 loader_set_logger(_eglLog
);
1865 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1867 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1870 disp
->DriverData
= (void *) dri2_dpy
;
1871 if (disp
->PlatformDisplay
== NULL
) {
1872 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1873 if (dri2_dpy
->wl_dpy
== NULL
)
1875 dri2_dpy
->own_device
= true;
1877 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1880 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1882 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1883 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1886 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1887 dri2_dpy
->wl_queue
);
1889 if (dri2_dpy
->own_device
)
1890 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1892 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1893 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1894 ®istry_listener_swrast
, dri2_dpy
);
1896 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1899 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1902 dri2_dpy
->driver_name
= strdup("swrast");
1903 if (!dri2_load_driver_swrast(disp
))
1906 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1908 if (!dri2_create_screen(disp
))
1911 if (!dri2_setup_extensions(disp
))
1914 dri2_setup_screen(disp
);
1916 dri2_wl_setup_swap_interval(disp
);
1918 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1919 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1923 /* Fill vtbl last to prevent accidentally calling virtual function during
1926 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1931 dri2_display_destroy(disp
);
1936 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1938 EGLBoolean initialized
= EGL_TRUE
;
1940 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1943 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1944 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1947 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);