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
);
1060 dri2_dpy
->formats
|= HAS_ARGB8888
;
1062 case WL_DRM_FORMAT_XRGB8888
:
1063 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1064 dri2_dpy
->formats
|= HAS_XRGB8888
;
1066 case WL_DRM_FORMAT_RGB565
:
1067 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1068 dri2_dpy
->formats
|= HAS_RGB565
;
1077 *mod
= (uint64_t) modifier_hi
<< 32;
1078 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1081 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1082 .format
= dmabuf_ignore_format
,
1083 .modifier
= dmabuf_handle_modifier
,
1087 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1088 uint32_t name
, const char *interface
,
1091 struct dri2_egl_display
*dri2_dpy
= data
;
1093 if (strcmp(interface
, "wl_drm") == 0) {
1095 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1096 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1097 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1098 dri2_dpy
->wl_dmabuf
=
1099 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1101 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1107 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1112 static const struct wl_registry_listener registry_listener_drm
= {
1113 .global
= registry_handle_global_drm
,
1114 .global_remove
= registry_handle_global_remove
1118 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1120 /* We can't use values greater than 1 on Wayland because we are using the
1121 * frame callback to synchronise the frame and the only way we be sure to
1122 * get a frame callback is to attach a new buffer. Therefore we can't just
1123 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1125 dri2_setup_swap_interval(disp
, 1);
1128 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1129 .authenticate
= dri2_wl_authenticate
,
1130 .create_window_surface
= dri2_wl_create_window_surface
,
1131 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1132 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1133 .destroy_surface
= dri2_wl_destroy_surface
,
1134 .create_image
= dri2_create_image_khr
,
1135 .swap_buffers
= dri2_wl_swap_buffers
,
1136 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1137 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1138 .set_damage_region
= dri2_fallback_set_damage_region
,
1139 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1140 .copy_buffers
= dri2_fallback_copy_buffers
,
1141 .query_buffer_age
= dri2_wl_query_buffer_age
,
1142 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1143 .get_sync_values
= dri2_fallback_get_sync_values
,
1144 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1147 static const __DRIextension
*dri2_loader_extensions
[] = {
1148 &dri2_loader_extension
.base
,
1149 &image_loader_extension
.base
,
1150 &image_lookup_extension
.base
,
1151 &use_invalidate
.base
,
1155 static const __DRIextension
*image_loader_extensions
[] = {
1156 &image_loader_extension
.base
,
1157 &image_lookup_extension
.base
,
1158 &use_invalidate
.base
,
1163 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1165 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1166 static const struct {
1167 const char *format_name
;
1169 unsigned int rgba_masks
[4];
1171 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1172 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1173 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1175 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1176 unsigned int count
= 0;
1178 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1179 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1180 struct dri2_egl_config
*dri2_conf
;
1182 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1185 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1186 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1188 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1195 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1196 if (!format_count
[i
]) {
1197 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1198 visuals
[i
].format_name
);
1202 return (count
!= 0);
1206 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1208 struct dri2_egl_display
*dri2_dpy
;
1210 loader_set_logger(_eglLog
);
1212 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1214 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1217 disp
->DriverData
= (void *) dri2_dpy
;
1218 if (disp
->PlatformDisplay
== NULL
) {
1219 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1220 if (dri2_dpy
->wl_dpy
== NULL
)
1222 dri2_dpy
->own_device
= true;
1224 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1227 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1228 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1229 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1233 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1235 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1236 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1239 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1240 dri2_dpy
->wl_queue
);
1242 if (dri2_dpy
->own_device
)
1243 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1245 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1246 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1247 ®istry_listener_drm
, dri2_dpy
);
1248 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1251 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1254 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1257 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1258 &dri2_dpy
->is_different_gpu
);
1259 if (dri2_dpy
->is_different_gpu
) {
1260 free(dri2_dpy
->device_name
);
1261 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1262 if (!dri2_dpy
->device_name
) {
1263 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1264 "for requested GPU");
1269 /* we have to do the check now, because loader_get_user_preferred_fd
1270 * will return a render-node when the requested gpu is different
1271 * to the server, but also if the client asks for the same gpu than
1272 * the server by requesting its pci-id */
1273 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1275 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1276 if (dri2_dpy
->driver_name
== NULL
) {
1277 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1281 /* render nodes cannot use Gem names, and thus do not support
1282 * the __DRI_DRI2_LOADER extension */
1283 if (!dri2_dpy
->is_render_node
) {
1284 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1285 if (!dri2_load_driver(disp
)) {
1286 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1290 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1291 if (!dri2_load_driver_dri3(disp
)) {
1292 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1297 if (!dri2_create_screen(disp
))
1300 if (!dri2_setup_extensions(disp
))
1303 dri2_setup_screen(disp
);
1305 dri2_wl_setup_swap_interval(disp
);
1307 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1308 * createImageFromFds support indicates that Prime export/import
1309 * is supported by the driver. Fall back to
1310 * gem names if we don't have Prime support. */
1312 if (dri2_dpy
->image
->base
.version
< 7 ||
1313 dri2_dpy
->image
->createImageFromFds
== NULL
)
1314 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1316 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1317 * The server needs to accept them */
1318 if (dri2_dpy
->is_render_node
&&
1319 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1320 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1324 if (dri2_dpy
->is_different_gpu
&&
1325 (dri2_dpy
->image
->base
.version
< 9 ||
1326 dri2_dpy
->image
->blitImage
== NULL
)) {
1327 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1328 "Image extension in the driver is not "
1329 "compatible. Version 9 or later and blitImage() "
1334 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1335 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1339 dri2_set_WL_bind_wayland_display(drv
, disp
);
1340 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1341 * because the buffer of the EGLImage has likely a tiling mode the server
1342 * gpu won't support. These is no way to check for now. Thus do not support the
1344 if (!dri2_dpy
->is_different_gpu
)
1345 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1347 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1349 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1351 /* Fill vtbl last to prevent accidentally calling virtual function during
1354 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1359 dri2_display_destroy(disp
);
1364 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1366 if (format
== WL_SHM_FORMAT_RGB565
)
1368 else /* ARGB8888 || XRGB8888 */
1373 * Taken from weston shared/os-compatibility.c
1376 #ifndef HAVE_MKOSTEMP
1379 set_cloexec_or_close(int fd
)
1386 flags
= fcntl(fd
, F_GETFD
);
1390 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1403 * Taken from weston shared/os-compatibility.c
1407 create_tmpfile_cloexec(char *tmpname
)
1411 #ifdef HAVE_MKOSTEMP
1412 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1416 fd
= mkstemp(tmpname
);
1418 fd
= set_cloexec_or_close(fd
);
1427 * Taken from weston shared/os-compatibility.c
1429 * Create a new, unique, anonymous file of the given size, and
1430 * return the file descriptor for it. The file descriptor is set
1431 * CLOEXEC. The file is immediately suitable for mmap()'ing
1432 * the given size at offset zero.
1434 * The file should not have a permanent backing store like a disk,
1435 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1437 * The file name is deleted from the file system.
1439 * The file is suitable for buffer sharing between processes by
1440 * transmitting the file descriptor over Unix sockets using the
1441 * SCM_RIGHTS methods.
1443 * If the C library implements posix_fallocate(), it is used to
1444 * guarantee that disk space is available for the file at the
1445 * given size. If disk space is insufficent, errno is set to ENOSPC.
1446 * If posix_fallocate() is not supported, program may receive
1447 * SIGBUS on accessing mmap()'ed file contents instead.
1450 os_create_anonymous_file(off_t size
)
1452 static const char template[] = "/mesa-shared-XXXXXX";
1458 path
= getenv("XDG_RUNTIME_DIR");
1464 name
= malloc(strlen(path
) + sizeof(template));
1469 strcat(name
, template);
1471 fd
= create_tmpfile_cloexec(name
);
1478 ret
= ftruncate(fd
, size
);
1489 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1490 int format
, int w
, int h
,
1491 void **data
, int *size
,
1492 struct wl_buffer
**buffer
)
1494 struct dri2_egl_display
*dri2_dpy
=
1495 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1496 struct wl_shm_pool
*pool
;
1497 int fd
, stride
, size_map
;
1500 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1501 size_map
= h
* stride
;
1503 /* Create a sharable buffer */
1504 fd
= os_create_anonymous_file(size_map
);
1508 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1509 if (data_map
== MAP_FAILED
) {
1514 /* Share it in a wl_buffer */
1515 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1516 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1517 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1518 wl_shm_pool_destroy(pool
);
1527 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1529 struct dri2_egl_display
*dri2_dpy
=
1530 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1532 /* we need to do the following operations only once per frame */
1533 if (dri2_surf
->back
)
1536 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1537 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1539 dri2_wl_release_buffers(dri2_surf
);
1541 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1542 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1543 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1544 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1545 dri2_surf
->current
= NULL
;
1548 /* find back buffer */
1550 /* There might be a buffer release already queued that wasn't processed */
1551 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1553 /* try get free buffer already created */
1554 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1555 if (!dri2_surf
->color_buffers
[i
].locked
&&
1556 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1557 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1562 /* else choose any another free location */
1563 if (!dri2_surf
->back
) {
1564 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1565 if (!dri2_surf
->color_buffers
[i
].locked
) {
1566 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1567 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1569 dri2_surf
->base
.Width
,
1570 dri2_surf
->base
.Height
,
1571 &dri2_surf
->back
->data
,
1572 &dri2_surf
->back
->data_size
,
1573 &dri2_surf
->back
->wl_buffer
)) {
1574 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1577 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1578 &wl_buffer_listener
, dri2_surf
);
1584 if (!dri2_surf
->back
) {
1585 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1589 dri2_surf
->back
->locked
= true;
1591 /* If we have an extra unlocked buffer at this point, we had to do triple
1592 * buffering for a while, but now can go back to just double buffering.
1593 * That means we can free any unlocked buffer now. */
1594 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1595 if (!dri2_surf
->color_buffers
[i
].locked
&&
1596 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1597 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1598 munmap(dri2_surf
->color_buffers
[i
].data
,
1599 dri2_surf
->color_buffers
[i
].data_size
);
1600 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1601 dri2_surf
->color_buffers
[i
].data
= NULL
;
1609 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1611 /* if there has been a resize: */
1612 if (!dri2_surf
->current
)
1615 return dri2_surf
->current
->data
;
1619 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1621 assert(dri2_surf
->back
);
1622 return dri2_surf
->back
->data
;
1626 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1628 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1630 while (dri2_surf
->throttle_callback
!= NULL
)
1631 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1632 dri2_surf
->wl_queue
) == -1)
1635 if (dri2_surf
->base
.SwapInterval
> 0) {
1636 dri2_surf
->throttle_callback
=
1637 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1638 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1639 &throttle_listener
, dri2_surf
);
1642 dri2_surf
->current
= dri2_surf
->back
;
1643 dri2_surf
->back
= NULL
;
1645 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1646 dri2_surf
->current
->wl_buffer
,
1647 dri2_surf
->dx
, dri2_surf
->dy
);
1649 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1650 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1651 /* reset resize growing parameters */
1655 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1656 0, 0, INT32_MAX
, INT32_MAX
);
1657 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1659 /* If we're not waiting for a frame callback then we'll at least throttle
1660 * to a sync callback so that we always give a chance for the compositor to
1661 * handle the commit and send a release event before checking for a free
1663 if (dri2_surf
->throttle_callback
== NULL
) {
1664 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1665 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1666 &throttle_listener
, dri2_surf
);
1669 wl_display_flush(dri2_dpy
->wl_dpy
);
1673 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1674 int *x
, int *y
, int *w
, int *h
,
1675 void *loaderPrivate
)
1677 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1679 (void) swrast_update_buffers(dri2_surf
);
1682 *w
= dri2_surf
->base
.Width
;
1683 *h
= dri2_surf
->base
.Height
;
1687 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1688 int x
, int y
, int w
, int h
,
1689 char *data
, void *loaderPrivate
)
1691 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1692 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1693 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1694 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1695 int dst_stride
= copy_width
;
1698 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1700 memset(data
, 0, copy_width
* h
);
1704 assert(data
!= src
);
1705 assert(copy_width
<= src_stride
);
1708 src
+= y
* src_stride
;
1711 if (copy_width
> src_stride
-x_offset
)
1712 copy_width
= src_stride
-x_offset
;
1713 if (h
> dri2_surf
->base
.Height
-y
)
1714 h
= dri2_surf
->base
.Height
-y
;
1717 memcpy(dst
, src
, copy_width
);
1724 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1725 int x
, int y
, int w
, int h
, int stride
,
1726 char *data
, void *loaderPrivate
)
1728 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1729 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1730 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1731 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1734 assert(copy_width
<= stride
);
1736 (void) swrast_update_buffers(dri2_surf
);
1737 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1739 /* partial copy, copy old content */
1740 if (copy_width
< dst_stride
)
1741 dri2_wl_swrast_get_image(draw
, 0, 0,
1742 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1743 dst
, loaderPrivate
);
1746 dst
+= y
* dst_stride
;
1750 /* drivers expect we do these checks (and some rely on it) */
1751 if (copy_width
> dst_stride
-x_offset
)
1752 copy_width
= dst_stride
-x_offset
;
1753 if (h
> dri2_surf
->base
.Height
-y
)
1754 h
= dri2_surf
->base
.Height
-y
;
1757 memcpy(dst
, src
, copy_width
);
1761 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1765 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1766 int x
, int y
, int w
, int h
,
1767 char *data
, void *loaderPrivate
)
1769 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1772 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1773 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1774 stride
, data
, loaderPrivate
);
1778 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1780 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1781 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1783 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1788 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1790 struct dri2_egl_display
*dri2_dpy
= data
;
1793 case WL_SHM_FORMAT_ARGB8888
:
1794 dri2_dpy
->formats
|= HAS_ARGB8888
;
1796 case WL_SHM_FORMAT_XRGB8888
:
1797 dri2_dpy
->formats
|= HAS_XRGB8888
;
1799 case WL_SHM_FORMAT_RGB565
:
1800 dri2_dpy
->formats
|= HAS_RGB565
;
1805 static const struct wl_shm_listener shm_listener
= {
1806 .format
= shm_handle_format
1810 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1811 uint32_t name
, const char *interface
,
1814 struct dri2_egl_display
*dri2_dpy
= data
;
1816 if (strcmp(interface
, "wl_shm") == 0) {
1818 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1819 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1823 static const struct wl_registry_listener registry_listener_swrast
= {
1824 .global
= registry_handle_global_swrast
,
1825 .global_remove
= registry_handle_global_remove
1828 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1829 .authenticate
= NULL
,
1830 .create_window_surface
= dri2_wl_create_window_surface
,
1831 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1832 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1833 .destroy_surface
= dri2_wl_destroy_surface
,
1834 .create_image
= dri2_create_image_khr
,
1835 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1836 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1837 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1838 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1839 .copy_buffers
= dri2_fallback_copy_buffers
,
1840 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1841 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1842 .get_sync_values
= dri2_fallback_get_sync_values
,
1843 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1846 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1847 .base
= { __DRI_SWRAST_LOADER
, 2 },
1849 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1850 .putImage
= dri2_wl_swrast_put_image
,
1851 .getImage
= dri2_wl_swrast_get_image
,
1852 .putImage2
= dri2_wl_swrast_put_image2
,
1855 static const __DRIextension
*swrast_loader_extensions
[] = {
1856 &swrast_loader_extension
.base
,
1857 &image_lookup_extension
.base
,
1862 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1864 struct dri2_egl_display
*dri2_dpy
;
1866 loader_set_logger(_eglLog
);
1868 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1870 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1873 disp
->DriverData
= (void *) dri2_dpy
;
1874 if (disp
->PlatformDisplay
== NULL
) {
1875 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1876 if (dri2_dpy
->wl_dpy
== NULL
)
1878 dri2_dpy
->own_device
= true;
1880 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1883 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1885 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1886 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1889 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1890 dri2_dpy
->wl_queue
);
1892 if (dri2_dpy
->own_device
)
1893 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1895 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1896 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1897 ®istry_listener_swrast
, dri2_dpy
);
1899 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1902 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1905 dri2_dpy
->driver_name
= strdup("swrast");
1906 if (!dri2_load_driver_swrast(disp
))
1909 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1911 if (!dri2_create_screen(disp
))
1914 if (!dri2_setup_extensions(disp
))
1917 dri2_setup_screen(disp
);
1919 dri2_wl_setup_swap_interval(disp
);
1921 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1922 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1926 /* Fill vtbl last to prevent accidentally calling virtual function during
1929 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1934 dri2_display_destroy(disp
);
1939 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1941 EGLBoolean initialized
= EGL_TRUE
;
1943 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1946 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1947 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1950 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);