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/debug.h"
46 #include "util/u_vector.h"
47 #include "eglglobals.h"
49 #include <wayland-client.h>
50 #include "wayland-drm-client-protocol.h"
51 #include "linux-dmabuf-unstable-v1-client-protocol.h"
53 #ifndef DRM_FORMAT_MOD_INVALID
54 #define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
57 #ifndef DRM_FORMAT_MOD_LINEAR
58 #define DRM_FORMAT_MOD_LINEAR 0
61 enum wl_drm_format_flags
{
68 roundtrip(struct dri2_egl_display
*dri2_dpy
)
70 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
74 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
76 struct dri2_egl_surface
*dri2_surf
= data
;
79 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
80 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
83 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
84 wl_buffer_destroy(buffer
);
88 dri2_surf
->color_buffers
[i
].locked
= false;
91 static const struct wl_buffer_listener wl_buffer_listener
= {
92 .release
= wl_buffer_release
96 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
98 struct dri2_egl_surface
*dri2_surf
= data
;
99 struct dri2_egl_display
*dri2_dpy
=
100 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
102 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
106 destroy_window_callback(void *data
)
108 struct dri2_egl_surface
*dri2_surf
= data
;
109 dri2_surf
->wl_win
= NULL
;
112 static struct wl_surface
*
113 get_wl_surface_proxy(struct wl_egl_window
*window
)
115 /* Version 3 of wl_egl_window introduced a version field at the same
116 * location where a pointer to wl_surface was stored. Thus, if
117 * window->version is dereferencable, we've been given an older version of
118 * wl_egl_window, and window->version points to wl_surface */
119 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
120 return wl_proxy_create_wrapper((void *)(window
->version
));
122 return wl_proxy_create_wrapper(window
->surface
);
126 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
129 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
130 _EGLConfig
*conf
, void *native_window
,
131 const EGLint
*attrib_list
)
133 __DRIcreateNewDrawableFunc createNewDrawable
;
134 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
135 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
136 struct wl_egl_window
*window
= native_window
;
137 struct dri2_egl_surface
*dri2_surf
;
138 const __DRIconfig
*config
;
140 dri2_surf
= calloc(1, sizeof *dri2_surf
);
142 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
146 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
, false))
149 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
150 if (conf
->RedSize
== 5)
151 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
152 else if (conf
->AlphaSize
== 0)
153 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
155 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
157 assert(dri2_dpy
->wl_shm
);
158 if (conf
->RedSize
== 5)
159 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
160 else if (conf
->AlphaSize
== 0)
161 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
163 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
166 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
167 if (!dri2_surf
->wl_queue
) {
168 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
172 if (dri2_dpy
->wl_drm
) {
173 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
174 if (!dri2_surf
->wl_drm_wrapper
) {
175 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
178 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
179 dri2_surf
->wl_queue
);
182 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
183 if (!dri2_surf
->wl_dpy_wrapper
) {
184 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
187 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
188 dri2_surf
->wl_queue
);
190 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
191 if (!dri2_surf
->wl_surface_wrapper
) {
192 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
193 goto cleanup_dpy_wrapper
;
195 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
196 dri2_surf
->wl_queue
);
198 dri2_surf
->wl_win
= window
;
199 dri2_surf
->wl_win
->private = dri2_surf
;
200 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
202 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
204 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
205 dri2_surf
->base
.GLColorspace
);
207 if (dri2_dpy
->image_driver
)
208 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
209 else if (dri2_dpy
->dri2
)
210 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
212 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
214 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
216 if (dri2_surf
->dri_drawable
== NULL
) {
217 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
218 goto cleanup_surf_wrapper
;
221 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
223 return &dri2_surf
->base
;
225 cleanup_surf_wrapper
:
226 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
228 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
230 if (dri2_surf
->wl_drm_wrapper
)
231 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
233 wl_event_queue_destroy(dri2_surf
->wl_queue
);
241 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
242 _EGLConfig
*conf
, void *native_window
,
243 const EGLint
*attrib_list
)
245 /* From the EGL_EXT_platform_wayland spec, version 3:
247 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
248 * that belongs to Wayland. Any such call fails and generates
251 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
257 * Called via eglDestroySurface(), drv->API.DestroySurface().
260 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
262 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
263 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
267 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
269 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
270 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
271 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
272 if (dri2_surf
->color_buffers
[i
].dri_image
)
273 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
274 if (dri2_surf
->color_buffers
[i
].linear_copy
)
275 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
276 if (dri2_surf
->color_buffers
[i
].data
)
277 munmap(dri2_surf
->color_buffers
[i
].data
,
278 dri2_surf
->color_buffers
[i
].data_size
);
282 dri2_egl_surface_free_local_buffers(dri2_surf
);
284 if (dri2_surf
->throttle_callback
)
285 wl_callback_destroy(dri2_surf
->throttle_callback
);
287 if (dri2_surf
->wl_win
) {
288 dri2_surf
->wl_win
->private = NULL
;
289 dri2_surf
->wl_win
->resize_callback
= NULL
;
290 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
293 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
294 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
295 if (dri2_surf
->wl_drm_wrapper
)
296 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
297 wl_event_queue_destroy(dri2_surf
->wl_queue
);
299 dri2_fini_surface(surf
);
306 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
308 struct dri2_egl_display
*dri2_dpy
=
309 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
311 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
312 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
313 !dri2_surf
->color_buffers
[i
].locked
)
314 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
315 if (dri2_surf
->color_buffers
[i
].dri_image
)
316 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
317 if (dri2_surf
->color_buffers
[i
].linear_copy
)
318 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
319 if (dri2_surf
->color_buffers
[i
].data
)
320 munmap(dri2_surf
->color_buffers
[i
].data
,
321 dri2_surf
->color_buffers
[i
].data_size
);
323 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
324 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
325 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
326 dri2_surf
->color_buffers
[i
].data
= NULL
;
327 dri2_surf
->color_buffers
[i
].locked
= false;
331 dri2_egl_surface_free_local_buffers(dri2_surf
);
335 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
337 struct dri2_egl_display
*dri2_dpy
=
338 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
340 unsigned int dri_image_format
;
344 /* currently supports three WL DRM formats,
345 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
346 * and WL_DRM_FORMAT_RGB565
348 switch (dri2_surf
->format
) {
349 case WL_DRM_FORMAT_ARGB8888
:
350 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
351 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
352 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
354 case WL_DRM_FORMAT_XRGB8888
:
355 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
356 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
357 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
359 case WL_DRM_FORMAT_RGB565
:
360 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
361 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
362 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
365 /* format is not supported */
369 /* There might be a buffer release already queued that wasn't processed */
370 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
372 while (dri2_surf
->back
== NULL
) {
373 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
374 /* Get an unlocked buffer, preferrably one with a dri_buffer
375 * already allocated. */
376 if (dri2_surf
->color_buffers
[i
].locked
)
378 if (dri2_surf
->back
== NULL
)
379 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
380 else if (dri2_surf
->back
->dri_image
== NULL
)
381 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
387 /* If we don't have a buffer, then block on the server to release one for
388 * us, and try again. wl_display_dispatch_queue will process any pending
389 * events, however not all servers flush on issuing a buffer release
390 * event. So, we spam the server with roundtrips as they always cause a
393 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
394 dri2_surf
->wl_queue
) < 0)
398 if (dri2_surf
->back
== NULL
)
401 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
403 if (dri2_dpy
->is_different_gpu
&&
404 dri2_surf
->back
->linear_copy
== NULL
) {
405 /* The LINEAR modifier should be a perfect alias of the LINEAR use
406 * flag; try the new interface first before the old, then fall back. */
407 if (dri2_dpy
->image
->base
.version
>= 15 &&
408 dri2_dpy
->image
->createImageWithModifiers
) {
409 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
411 dri2_surf
->back
->linear_copy
=
412 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
413 dri2_surf
->base
.Width
,
414 dri2_surf
->base
.Height
,
420 dri2_surf
->back
->linear_copy
=
421 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
422 dri2_surf
->base
.Width
,
423 dri2_surf
->base
.Height
,
426 __DRI_IMAGE_USE_LINEAR
,
429 if (dri2_surf
->back
->linear_copy
== NULL
)
433 if (dri2_surf
->back
->dri_image
== NULL
) {
434 /* If our DRIImage implementation does not support
435 * createImageWithModifiers, then fall back to the old createImage,
436 * and hope it allocates an image which is acceptable to the winsys.
438 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
439 dri2_dpy
->image
->createImageWithModifiers
) {
440 dri2_surf
->back
->dri_image
=
441 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
442 dri2_surf
->base
.Width
,
443 dri2_surf
->base
.Height
,
449 dri2_surf
->back
->dri_image
=
450 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
451 dri2_surf
->base
.Width
,
452 dri2_surf
->base
.Height
,
454 dri2_dpy
->is_different_gpu
?
459 dri2_surf
->back
->age
= 0;
461 if (dri2_surf
->back
->dri_image
== NULL
)
464 dri2_surf
->back
->locked
= true;
471 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
473 struct dri2_egl_display
*dri2_dpy
=
474 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
478 image
= dri2_surf
->back
->dri_image
;
480 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
481 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
483 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
485 buffer
->pitch
= pitch
;
491 update_buffers(struct dri2_egl_surface
*dri2_surf
)
493 struct dri2_egl_display
*dri2_dpy
=
494 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
496 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
497 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
499 dri2_wl_release_buffers(dri2_surf
);
501 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
502 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
503 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
504 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
507 if (get_back_bo(dri2_surf
) < 0) {
508 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
512 /* If we have an extra unlocked buffer at this point, we had to do triple
513 * buffering for a while, but now can go back to just double buffering.
514 * That means we can free any unlocked buffer now. */
515 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
516 if (!dri2_surf
->color_buffers
[i
].locked
&&
517 dri2_surf
->color_buffers
[i
].wl_buffer
) {
518 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
519 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
520 if (dri2_dpy
->is_different_gpu
)
521 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
522 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
523 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
524 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
532 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
533 int *width
, int *height
,
534 unsigned int *attachments
, int count
,
535 int *out_count
, void *loaderPrivate
)
537 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
540 if (update_buffers(dri2_surf
) < 0)
543 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
546 switch (attachments
[i
]) {
547 case __DRI_BUFFER_BACK_LEFT
:
548 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
551 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
555 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
558 dri2_surf
->buffers
[j
] = *local
;
567 *width
= dri2_surf
->base
.Width
;
568 *height
= dri2_surf
->base
.Height
;
570 return dri2_surf
->buffers
;
574 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
575 int *width
, int *height
,
576 unsigned int *attachments
, int count
,
577 int *out_count
, void *loaderPrivate
)
579 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
580 unsigned int *attachments_with_format
;
584 switch (dri2_surf
->format
) {
585 case WL_DRM_FORMAT_ARGB8888
:
586 case WL_DRM_FORMAT_XRGB8888
:
589 case WL_DRM_FORMAT_RGB565
:
593 /* format is not supported */
597 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
598 if (!attachments_with_format
) {
603 for (int i
= 0; i
< count
; ++i
) {
604 attachments_with_format
[2*i
] = attachments
[i
];
605 attachments_with_format
[2*i
+ 1] = bpp
;
609 dri2_wl_get_buffers_with_format(driDrawable
,
611 attachments_with_format
, count
,
612 out_count
, loaderPrivate
);
614 free(attachments_with_format
);
620 image_get_buffers(__DRIdrawable
*driDrawable
,
624 uint32_t buffer_mask
,
625 struct __DRIimageList
*buffers
)
627 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
629 if (update_buffers(dri2_surf
) < 0)
632 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
633 buffers
->back
= dri2_surf
->back
->dri_image
;
639 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
642 (void) loaderPrivate
;
645 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
646 .base
= { __DRI_DRI2_LOADER
, 3 },
648 .getBuffers
= dri2_wl_get_buffers
,
649 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
650 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
653 static const __DRIimageLoaderExtension image_loader_extension
= {
654 .base
= { __DRI_IMAGE_LOADER
, 1 },
656 .getBuffers
= image_get_buffers
,
657 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
661 wayland_throttle_callback(void *data
,
662 struct wl_callback
*callback
,
665 struct dri2_egl_surface
*dri2_surf
= data
;
667 dri2_surf
->throttle_callback
= NULL
;
668 wl_callback_destroy(callback
);
671 static const struct wl_callback_listener throttle_listener
= {
672 .done
= wayland_throttle_callback
675 static struct wl_buffer
*
676 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
677 struct dri2_egl_surface
*dri2_surf
,
680 struct wl_buffer
*ret
;
682 int width
, height
, fourcc
, num_planes
;
684 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
685 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
687 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
692 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
697 if (dri2_dpy
->wl_dmabuf
&& dri2_dpy
->image
->base
.version
>= 15) {
698 struct zwp_linux_buffer_params_v1
*params
;
702 query
= dri2_dpy
->image
->queryImage(image
,
703 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
705 query
&= dri2_dpy
->image
->queryImage(image
,
706 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
709 mod_hi
= DRM_FORMAT_MOD_INVALID
>> 32;
710 mod_lo
= DRM_FORMAT_MOD_INVALID
& 0xffffffff;
713 /* We don't need a wrapper for wl_dmabuf objects, because we have to
714 * create the intermediate params object; we can set the queue on this,
715 * and the wl_buffer inherits it race-free. */
716 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
718 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
720 for (i
= 0; i
< num_planes
; i
++) {
728 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
730 zwp_linux_buffer_params_v1_destroy(params
);
734 query
= dri2_dpy
->image
->queryImage(p_image
,
735 __DRI_IMAGE_ATTRIB_FD
,
737 query
&= dri2_dpy
->image
->queryImage(p_image
,
738 __DRI_IMAGE_ATTRIB_STRIDE
,
740 query
&= dri2_dpy
->image
->queryImage(p_image
,
741 __DRI_IMAGE_ATTRIB_OFFSET
,
743 if (image
!= p_image
)
744 dri2_dpy
->image
->destroyImage(p_image
);
749 zwp_linux_buffer_params_v1_destroy(params
);
753 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
758 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
760 zwp_linux_buffer_params_v1_destroy(params
);
761 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
762 struct wl_drm
*wl_drm
=
763 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
769 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
770 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
771 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
775 struct wl_drm
*wl_drm
=
776 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
782 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
783 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
784 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
791 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
795 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
796 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
799 for (int i
= 0; i
< n_rects
; i
++) {
800 const int *rect
= &rects
[i
* 4];
802 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
804 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
810 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
813 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
819 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
820 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
822 while (dri2_surf
->throttle_callback
!= NULL
)
823 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
824 dri2_surf
->wl_queue
) == -1)
827 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
828 if (dri2_surf
->color_buffers
[i
].age
> 0)
829 dri2_surf
->color_buffers
[i
].age
++;
831 /* Make sure we have a back buffer in case we're swapping without ever
833 if (get_back_bo(dri2_surf
) < 0)
834 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
836 if (draw
->SwapInterval
> 0) {
837 dri2_surf
->throttle_callback
=
838 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
839 wl_callback_add_listener(dri2_surf
->throttle_callback
,
840 &throttle_listener
, dri2_surf
);
843 dri2_surf
->back
->age
= 1;
844 dri2_surf
->current
= dri2_surf
->back
;
845 dri2_surf
->back
= NULL
;
847 if (!dri2_surf
->current
->wl_buffer
) {
850 if (dri2_dpy
->is_different_gpu
)
851 image
= dri2_surf
->current
->linear_copy
;
853 image
= dri2_surf
->current
->dri_image
;
855 dri2_surf
->current
->wl_buffer
=
856 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
858 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
859 &wl_buffer_listener
, dri2_surf
);
862 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
863 dri2_surf
->current
->wl_buffer
,
864 dri2_surf
->dx
, dri2_surf
->dy
);
866 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
867 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
868 /* reset resize growing parameters */
872 /* If the compositor doesn't support damage_buffer, we deliberately
873 * ignore the damage region and post maximum damage, due to
874 * https://bugs.freedesktop.org/78190 */
875 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
876 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
877 0, 0, INT32_MAX
, INT32_MAX
);
879 if (dri2_dpy
->is_different_gpu
) {
880 _EGLContext
*ctx
= _eglGetCurrentContext();
881 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
882 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
883 dri2_surf
->current
->linear_copy
,
884 dri2_surf
->current
->dri_image
,
885 0, 0, dri2_surf
->base
.Width
,
886 dri2_surf
->base
.Height
,
887 0, 0, dri2_surf
->base
.Width
,
888 dri2_surf
->base
.Height
, 0);
891 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
892 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
894 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
896 /* If we're not waiting for a frame callback then we'll at least throttle
897 * to a sync callback so that we always give a chance for the compositor to
898 * handle the commit and send a release event before checking for a free
900 if (dri2_surf
->throttle_callback
== NULL
) {
901 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
902 wl_callback_add_listener(dri2_surf
->throttle_callback
,
903 &throttle_listener
, dri2_surf
);
906 wl_display_flush(dri2_dpy
->wl_dpy
);
912 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
913 _EGLDisplay
*disp
, _EGLSurface
*surface
)
915 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
917 if (get_back_bo(dri2_surf
) < 0) {
918 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
922 return dri2_surf
->back
->age
;
926 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
928 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
931 static struct wl_buffer
*
932 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
936 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
937 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
938 __DRIimage
*image
= dri2_img
->dri_image
;
939 struct wl_buffer
*buffer
;
942 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
944 case __DRI_IMAGE_FORMAT_ARGB8888
:
945 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
948 case __DRI_IMAGE_FORMAT_XRGB8888
:
949 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
956 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
958 /* The buffer object will have been created with our internal event queue
959 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
960 * buffer to be used by the application so we'll reset it to the display's
961 * default event queue. This isn't actually racy, as the only event the
962 * buffer can get is a buffer release, which doesn't happen with an explicit
965 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
970 _eglError(EGL_BAD_MATCH
, "unsupported image format");
975 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
977 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
980 if (dri2_dpy
->is_render_node
) {
981 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
982 "authenticate for render-nodes");
985 dri2_dpy
->authenticated
= false;
987 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
988 if (roundtrip(dri2_dpy
) < 0)
991 if (!dri2_dpy
->authenticated
)
994 /* reset authenticated */
995 dri2_dpy
->authenticated
= true;
1001 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1003 struct dri2_egl_display
*dri2_dpy
= data
;
1006 dri2_dpy
->device_name
= strdup(device
);
1007 if (!dri2_dpy
->device_name
)
1010 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1011 if (dri2_dpy
->fd
== -1) {
1012 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1013 dri2_dpy
->device_name
, strerror(errno
));
1017 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1018 dri2_dpy
->authenticated
= true;
1020 drmGetMagic(dri2_dpy
->fd
, &magic
);
1021 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1026 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1028 struct dri2_egl_display
*dri2_dpy
= data
;
1031 case WL_DRM_FORMAT_ARGB8888
:
1032 dri2_dpy
->formats
|= HAS_ARGB8888
;
1034 case WL_DRM_FORMAT_XRGB8888
:
1035 dri2_dpy
->formats
|= HAS_XRGB8888
;
1037 case WL_DRM_FORMAT_RGB565
:
1038 dri2_dpy
->formats
|= HAS_RGB565
;
1044 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1046 struct dri2_egl_display
*dri2_dpy
= data
;
1048 dri2_dpy
->capabilities
= value
;
1052 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1054 struct dri2_egl_display
*dri2_dpy
= data
;
1056 dri2_dpy
->authenticated
= true;
1059 static const struct wl_drm_listener drm_listener
= {
1060 .device
= drm_handle_device
,
1061 .format
= drm_handle_format
,
1062 .authenticated
= drm_handle_authenticated
,
1063 .capabilities
= drm_handle_capabilities
1067 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1070 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1074 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1075 uint32_t format
, uint32_t modifier_hi
,
1076 uint32_t modifier_lo
)
1078 struct dri2_egl_display
*dri2_dpy
= data
;
1079 uint64_t *mod
= NULL
;
1081 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1082 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1086 case WL_DRM_FORMAT_ARGB8888
:
1087 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1088 dri2_dpy
->formats
|= HAS_ARGB8888
;
1090 case WL_DRM_FORMAT_XRGB8888
:
1091 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1092 dri2_dpy
->formats
|= HAS_XRGB8888
;
1094 case WL_DRM_FORMAT_RGB565
:
1095 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1096 dri2_dpy
->formats
|= HAS_RGB565
;
1105 *mod
= (uint64_t) modifier_hi
<< 32;
1106 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1109 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1110 .format
= dmabuf_ignore_format
,
1111 .modifier
= dmabuf_handle_modifier
,
1115 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1116 uint32_t name
, const char *interface
,
1119 struct dri2_egl_display
*dri2_dpy
= data
;
1121 if (strcmp(interface
, "wl_drm") == 0) {
1123 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1124 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1125 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1126 dri2_dpy
->wl_dmabuf
=
1127 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1129 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1135 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1140 static const struct wl_registry_listener registry_listener_drm
= {
1141 .global
= registry_handle_global_drm
,
1142 .global_remove
= registry_handle_global_remove
1146 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1148 /* We can't use values greater than 1 on Wayland because we are using the
1149 * frame callback to synchronise the frame and the only way we be sure to
1150 * get a frame callback is to attach a new buffer. Therefore we can't just
1151 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1153 dri2_setup_swap_interval(disp
, 1);
1156 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1157 .authenticate
= dri2_wl_authenticate
,
1158 .create_window_surface
= dri2_wl_create_window_surface
,
1159 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1160 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1161 .destroy_surface
= dri2_wl_destroy_surface
,
1162 .create_image
= dri2_create_image_khr
,
1163 .swap_buffers
= dri2_wl_swap_buffers
,
1164 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1165 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1166 .set_damage_region
= dri2_fallback_set_damage_region
,
1167 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1168 .copy_buffers
= dri2_fallback_copy_buffers
,
1169 .query_buffer_age
= dri2_wl_query_buffer_age
,
1170 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1171 .get_sync_values
= dri2_fallback_get_sync_values
,
1172 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1175 static const __DRIextension
*dri2_loader_extensions
[] = {
1176 &dri2_loader_extension
.base
,
1177 &image_loader_extension
.base
,
1178 &image_lookup_extension
.base
,
1179 &use_invalidate
.base
,
1183 static const __DRIextension
*image_loader_extensions
[] = {
1184 &image_loader_extension
.base
,
1185 &image_lookup_extension
.base
,
1186 &use_invalidate
.base
,
1191 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1193 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1194 static const struct {
1195 const char *format_name
;
1197 unsigned int rgba_masks
[4];
1199 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1200 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1201 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1203 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1204 unsigned int count
= 0;
1206 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1207 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1208 struct dri2_egl_config
*dri2_conf
;
1210 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1213 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1214 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1216 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1223 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1224 if (!format_count
[i
]) {
1225 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1226 visuals
[i
].format_name
);
1230 return (count
!= 0);
1234 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1236 struct dri2_egl_display
*dri2_dpy
;
1238 loader_set_logger(_eglLog
);
1240 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1242 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1245 disp
->DriverData
= (void *) dri2_dpy
;
1246 if (disp
->PlatformDisplay
== NULL
) {
1247 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1248 if (dri2_dpy
->wl_dpy
== NULL
)
1250 dri2_dpy
->own_device
= true;
1252 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1255 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1256 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1257 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1261 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1263 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1264 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1267 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1268 dri2_dpy
->wl_queue
);
1270 if (dri2_dpy
->own_device
)
1271 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1273 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1274 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1275 ®istry_listener_drm
, dri2_dpy
);
1276 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1279 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1282 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1285 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1286 &dri2_dpy
->is_different_gpu
);
1287 if (dri2_dpy
->is_different_gpu
) {
1288 free(dri2_dpy
->device_name
);
1289 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1290 if (!dri2_dpy
->device_name
) {
1291 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1292 "for requested GPU");
1297 /* we have to do the check now, because loader_get_user_preferred_fd
1298 * will return a render-node when the requested gpu is different
1299 * to the server, but also if the client asks for the same gpu than
1300 * the server by requesting its pci-id */
1301 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1303 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1304 if (dri2_dpy
->driver_name
== NULL
) {
1305 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1309 /* render nodes cannot use Gem names, and thus do not support
1310 * the __DRI_DRI2_LOADER extension */
1311 if (!dri2_dpy
->is_render_node
) {
1312 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1313 if (!dri2_load_driver(disp
)) {
1314 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1318 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1319 if (!dri2_load_driver_dri3(disp
)) {
1320 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1325 if (!dri2_create_screen(disp
))
1328 if (!dri2_setup_extensions(disp
))
1331 dri2_setup_screen(disp
);
1333 dri2_wl_setup_swap_interval(disp
);
1335 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1336 * createImageFromFds support indicates that Prime export/import
1337 * is supported by the driver. Fall back to
1338 * gem names if we don't have Prime support. */
1340 if (dri2_dpy
->image
->base
.version
< 7 ||
1341 dri2_dpy
->image
->createImageFromFds
== NULL
)
1342 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1344 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1345 * The server needs to accept them */
1346 if (dri2_dpy
->is_render_node
&&
1347 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1348 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1352 if (dri2_dpy
->is_different_gpu
&&
1353 (dri2_dpy
->image
->base
.version
< 9 ||
1354 dri2_dpy
->image
->blitImage
== NULL
)) {
1355 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1356 "Image extension in the driver is not "
1357 "compatible. Version 9 or later and blitImage() "
1362 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1363 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1367 dri2_set_WL_bind_wayland_display(drv
, disp
);
1368 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1369 * because the buffer of the EGLImage has likely a tiling mode the server
1370 * gpu won't support. These is no way to check for now. Thus do not support the
1372 if (!dri2_dpy
->is_different_gpu
)
1373 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1375 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1377 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1379 /* Fill vtbl last to prevent accidentally calling virtual function during
1382 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1387 dri2_display_destroy(disp
);
1392 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1394 if (format
== WL_SHM_FORMAT_RGB565
)
1396 else /* ARGB8888 || XRGB8888 */
1401 * Taken from weston shared/os-compatibility.c
1404 #ifndef HAVE_MKOSTEMP
1407 set_cloexec_or_close(int fd
)
1414 flags
= fcntl(fd
, F_GETFD
);
1418 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1431 * Taken from weston shared/os-compatibility.c
1435 create_tmpfile_cloexec(char *tmpname
)
1439 #ifdef HAVE_MKOSTEMP
1440 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1444 fd
= mkstemp(tmpname
);
1446 fd
= set_cloexec_or_close(fd
);
1455 * Taken from weston shared/os-compatibility.c
1457 * Create a new, unique, anonymous file of the given size, and
1458 * return the file descriptor for it. The file descriptor is set
1459 * CLOEXEC. The file is immediately suitable for mmap()'ing
1460 * the given size at offset zero.
1462 * The file should not have a permanent backing store like a disk,
1463 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1465 * The file name is deleted from the file system.
1467 * The file is suitable for buffer sharing between processes by
1468 * transmitting the file descriptor over Unix sockets using the
1469 * SCM_RIGHTS methods.
1471 * If the C library implements posix_fallocate(), it is used to
1472 * guarantee that disk space is available for the file at the
1473 * given size. If disk space is insufficent, errno is set to ENOSPC.
1474 * If posix_fallocate() is not supported, program may receive
1475 * SIGBUS on accessing mmap()'ed file contents instead.
1478 os_create_anonymous_file(off_t size
)
1480 static const char templ
[] = "/mesa-shared-XXXXXX";
1486 path
= getenv("XDG_RUNTIME_DIR");
1492 name
= malloc(strlen(path
) + sizeof(templ
));
1497 strcat(name
, templ
);
1499 fd
= create_tmpfile_cloexec(name
);
1506 ret
= ftruncate(fd
, size
);
1517 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1518 int format
, int w
, int h
,
1519 void **data
, int *size
,
1520 struct wl_buffer
**buffer
)
1522 struct dri2_egl_display
*dri2_dpy
=
1523 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1524 struct wl_shm_pool
*pool
;
1525 int fd
, stride
, size_map
;
1528 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1529 size_map
= h
* stride
;
1531 /* Create a sharable buffer */
1532 fd
= os_create_anonymous_file(size_map
);
1536 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1537 if (data_map
== MAP_FAILED
) {
1542 /* Share it in a wl_buffer */
1543 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1544 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1545 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1546 wl_shm_pool_destroy(pool
);
1555 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1557 struct dri2_egl_display
*dri2_dpy
=
1558 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1560 /* we need to do the following operations only once per frame */
1561 if (dri2_surf
->back
)
1564 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1565 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1567 dri2_wl_release_buffers(dri2_surf
);
1569 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1570 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1571 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1572 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1573 dri2_surf
->current
= NULL
;
1576 /* find back buffer */
1578 /* There might be a buffer release already queued that wasn't processed */
1579 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1581 /* try get free buffer already created */
1582 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1583 if (!dri2_surf
->color_buffers
[i
].locked
&&
1584 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1585 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1590 /* else choose any another free location */
1591 if (!dri2_surf
->back
) {
1592 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1593 if (!dri2_surf
->color_buffers
[i
].locked
) {
1594 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1595 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1597 dri2_surf
->base
.Width
,
1598 dri2_surf
->base
.Height
,
1599 &dri2_surf
->back
->data
,
1600 &dri2_surf
->back
->data_size
,
1601 &dri2_surf
->back
->wl_buffer
)) {
1602 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1605 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1606 &wl_buffer_listener
, dri2_surf
);
1612 if (!dri2_surf
->back
) {
1613 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1617 dri2_surf
->back
->locked
= true;
1619 /* If we have an extra unlocked buffer at this point, we had to do triple
1620 * buffering for a while, but now can go back to just double buffering.
1621 * That means we can free any unlocked buffer now. */
1622 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1623 if (!dri2_surf
->color_buffers
[i
].locked
&&
1624 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1625 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1626 munmap(dri2_surf
->color_buffers
[i
].data
,
1627 dri2_surf
->color_buffers
[i
].data_size
);
1628 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1629 dri2_surf
->color_buffers
[i
].data
= NULL
;
1637 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1639 /* if there has been a resize: */
1640 if (!dri2_surf
->current
)
1643 return dri2_surf
->current
->data
;
1647 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1649 assert(dri2_surf
->back
);
1650 return dri2_surf
->back
->data
;
1654 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1656 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1658 while (dri2_surf
->throttle_callback
!= NULL
)
1659 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1660 dri2_surf
->wl_queue
) == -1)
1663 if (dri2_surf
->base
.SwapInterval
> 0) {
1664 dri2_surf
->throttle_callback
=
1665 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1666 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1667 &throttle_listener
, dri2_surf
);
1670 dri2_surf
->current
= dri2_surf
->back
;
1671 dri2_surf
->back
= NULL
;
1673 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1674 dri2_surf
->current
->wl_buffer
,
1675 dri2_surf
->dx
, dri2_surf
->dy
);
1677 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1678 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1679 /* reset resize growing parameters */
1683 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1684 0, 0, INT32_MAX
, INT32_MAX
);
1685 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1687 /* If we're not waiting for a frame callback then we'll at least throttle
1688 * to a sync callback so that we always give a chance for the compositor to
1689 * handle the commit and send a release event before checking for a free
1691 if (dri2_surf
->throttle_callback
== NULL
) {
1692 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1693 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1694 &throttle_listener
, dri2_surf
);
1697 wl_display_flush(dri2_dpy
->wl_dpy
);
1701 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1702 int *x
, int *y
, int *w
, int *h
,
1703 void *loaderPrivate
)
1705 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1707 (void) swrast_update_buffers(dri2_surf
);
1710 *w
= dri2_surf
->base
.Width
;
1711 *h
= dri2_surf
->base
.Height
;
1715 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1716 int x
, int y
, int w
, int h
,
1717 char *data
, void *loaderPrivate
)
1719 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1720 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1721 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1722 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1723 int dst_stride
= copy_width
;
1726 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1728 memset(data
, 0, copy_width
* h
);
1732 assert(data
!= src
);
1733 assert(copy_width
<= src_stride
);
1736 src
+= y
* src_stride
;
1739 if (copy_width
> src_stride
-x_offset
)
1740 copy_width
= src_stride
-x_offset
;
1741 if (h
> dri2_surf
->base
.Height
-y
)
1742 h
= dri2_surf
->base
.Height
-y
;
1745 memcpy(dst
, src
, copy_width
);
1752 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1753 int x
, int y
, int w
, int h
, int stride
,
1754 char *data
, void *loaderPrivate
)
1756 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1757 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1758 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1759 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1762 assert(copy_width
<= stride
);
1764 (void) swrast_update_buffers(dri2_surf
);
1765 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1767 /* partial copy, copy old content */
1768 if (copy_width
< dst_stride
)
1769 dri2_wl_swrast_get_image(draw
, 0, 0,
1770 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1771 dst
, loaderPrivate
);
1774 dst
+= y
* dst_stride
;
1778 /* drivers expect we do these checks (and some rely on it) */
1779 if (copy_width
> dst_stride
-x_offset
)
1780 copy_width
= dst_stride
-x_offset
;
1781 if (h
> dri2_surf
->base
.Height
-y
)
1782 h
= dri2_surf
->base
.Height
-y
;
1785 memcpy(dst
, src
, copy_width
);
1789 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1793 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1794 int x
, int y
, int w
, int h
,
1795 char *data
, void *loaderPrivate
)
1797 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1800 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1801 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1802 stride
, data
, loaderPrivate
);
1806 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1808 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1809 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1811 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1816 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1818 struct dri2_egl_display
*dri2_dpy
= data
;
1821 case WL_SHM_FORMAT_ARGB8888
:
1822 dri2_dpy
->formats
|= HAS_ARGB8888
;
1824 case WL_SHM_FORMAT_XRGB8888
:
1825 dri2_dpy
->formats
|= HAS_XRGB8888
;
1827 case WL_SHM_FORMAT_RGB565
:
1828 dri2_dpy
->formats
|= HAS_RGB565
;
1833 static const struct wl_shm_listener shm_listener
= {
1834 .format
= shm_handle_format
1838 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1839 uint32_t name
, const char *interface
,
1842 struct dri2_egl_display
*dri2_dpy
= data
;
1844 if (strcmp(interface
, "wl_shm") == 0) {
1846 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1847 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1851 static const struct wl_registry_listener registry_listener_swrast
= {
1852 .global
= registry_handle_global_swrast
,
1853 .global_remove
= registry_handle_global_remove
1856 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1857 .authenticate
= NULL
,
1858 .create_window_surface
= dri2_wl_create_window_surface
,
1859 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1860 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1861 .destroy_surface
= dri2_wl_destroy_surface
,
1862 .create_image
= dri2_create_image_khr
,
1863 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1864 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1865 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1866 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1867 .copy_buffers
= dri2_fallback_copy_buffers
,
1868 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1869 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1870 .get_sync_values
= dri2_fallback_get_sync_values
,
1871 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1874 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1875 .base
= { __DRI_SWRAST_LOADER
, 2 },
1877 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1878 .putImage
= dri2_wl_swrast_put_image
,
1879 .getImage
= dri2_wl_swrast_get_image
,
1880 .putImage2
= dri2_wl_swrast_put_image2
,
1883 static const __DRIextension
*swrast_loader_extensions
[] = {
1884 &swrast_loader_extension
.base
,
1885 &image_lookup_extension
.base
,
1890 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1892 struct dri2_egl_display
*dri2_dpy
;
1894 loader_set_logger(_eglLog
);
1896 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1898 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1901 disp
->DriverData
= (void *) dri2_dpy
;
1902 if (disp
->PlatformDisplay
== NULL
) {
1903 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1904 if (dri2_dpy
->wl_dpy
== NULL
)
1906 dri2_dpy
->own_device
= true;
1908 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1911 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1913 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1914 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1917 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1918 dri2_dpy
->wl_queue
);
1920 if (dri2_dpy
->own_device
)
1921 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1923 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1924 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1925 ®istry_listener_swrast
, dri2_dpy
);
1927 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1930 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1933 dri2_dpy
->driver_name
= strdup("swrast");
1934 if (!dri2_load_driver_swrast(disp
))
1937 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1939 if (!dri2_create_screen(disp
))
1942 if (!dri2_setup_extensions(disp
))
1945 dri2_setup_screen(disp
);
1947 dri2_wl_setup_swap_interval(disp
);
1949 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1950 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1954 /* Fill vtbl last to prevent accidentally calling virtual function during
1957 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1962 dri2_display_destroy(disp
);
1967 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1969 EGLBoolean initialized
= EGL_FALSE
;
1971 if (!env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false))
1972 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
1975 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);