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_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_win
= window
;
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");
195 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
196 dri2_surf
->wl_queue
);
198 dri2_surf
->wl_win
->private = dri2_surf
;
199 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
201 dri2_surf
->base
.Width
= -1;
202 dri2_surf
->base
.Height
= -1;
204 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
205 dri2_surf
->base
.GLColorspace
);
208 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
210 if (dri2_dpy
->image_driver
)
211 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
212 else if (dri2_dpy
->dri2
)
213 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
215 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
217 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
219 if (dri2_surf
->dri_drawable
== NULL
) {
220 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
224 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
226 return &dri2_surf
->base
;
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 if (dri2_surf
->wl_drm_wrapper
)
293 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
294 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
295 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_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. */
387 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
391 if (dri2_surf
->back
== NULL
)
394 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
396 if (dri2_dpy
->is_different_gpu
&&
397 dri2_surf
->back
->linear_copy
== NULL
) {
398 /* The LINEAR modifier should be a perfect alias of the LINEAR use
399 * flag; try the new interface first before the old, then fall back. */
400 if (dri2_dpy
->image
->base
.version
>= 15 &&
401 dri2_dpy
->image
->createImageWithModifiers
) {
402 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
404 dri2_surf
->back
->linear_copy
=
405 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
406 dri2_surf
->base
.Width
,
407 dri2_surf
->base
.Height
,
413 dri2_surf
->back
->linear_copy
=
414 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
415 dri2_surf
->base
.Width
,
416 dri2_surf
->base
.Height
,
419 __DRI_IMAGE_USE_LINEAR
,
422 if (dri2_surf
->back
->linear_copy
== NULL
)
426 if (dri2_surf
->back
->dri_image
== NULL
) {
427 /* If our DRIImage implementation does not support
428 * createImageWithModifiers, then fall back to the old createImage,
429 * and hope it allocates an image which is acceptable to the winsys.
431 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
432 dri2_dpy
->image
->createImageWithModifiers
) {
433 dri2_surf
->back
->dri_image
=
434 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
435 dri2_surf
->base
.Width
,
436 dri2_surf
->base
.Height
,
442 dri2_surf
->back
->dri_image
=
443 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
444 dri2_surf
->base
.Width
,
445 dri2_surf
->base
.Height
,
447 dri2_dpy
->is_different_gpu
?
452 dri2_surf
->back
->age
= 0;
454 if (dri2_surf
->back
->dri_image
== NULL
)
457 dri2_surf
->back
->locked
= true;
464 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
466 struct dri2_egl_display
*dri2_dpy
=
467 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
471 image
= dri2_surf
->back
->dri_image
;
473 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
474 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
476 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
478 buffer
->pitch
= pitch
;
484 update_buffers(struct dri2_egl_surface
*dri2_surf
)
486 struct dri2_egl_display
*dri2_dpy
=
487 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
489 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
490 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
492 dri2_wl_release_buffers(dri2_surf
);
494 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
495 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
496 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
497 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
500 if (get_back_bo(dri2_surf
) < 0) {
501 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
505 /* If we have an extra unlocked buffer at this point, we had to do triple
506 * buffering for a while, but now can go back to just double buffering.
507 * That means we can free any unlocked buffer now. */
508 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
509 if (!dri2_surf
->color_buffers
[i
].locked
&&
510 dri2_surf
->color_buffers
[i
].wl_buffer
) {
511 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
512 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
513 if (dri2_dpy
->is_different_gpu
)
514 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
515 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
516 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
517 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
525 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
526 int *width
, int *height
,
527 unsigned int *attachments
, int count
,
528 int *out_count
, void *loaderPrivate
)
530 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
533 if (update_buffers(dri2_surf
) < 0)
536 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
539 switch (attachments
[i
]) {
540 case __DRI_BUFFER_BACK_LEFT
:
541 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
544 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
548 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
551 dri2_surf
->buffers
[j
] = *local
;
560 *width
= dri2_surf
->base
.Width
;
561 *height
= dri2_surf
->base
.Height
;
563 return dri2_surf
->buffers
;
567 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
568 int *width
, int *height
,
569 unsigned int *attachments
, int count
,
570 int *out_count
, void *loaderPrivate
)
572 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
573 unsigned int *attachments_with_format
;
577 switch (dri2_surf
->format
) {
578 case WL_DRM_FORMAT_ARGB8888
:
579 case WL_DRM_FORMAT_XRGB8888
:
582 case WL_DRM_FORMAT_RGB565
:
586 /* format is not supported */
590 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
591 if (!attachments_with_format
) {
596 for (int i
= 0; i
< count
; ++i
) {
597 attachments_with_format
[2*i
] = attachments
[i
];
598 attachments_with_format
[2*i
+ 1] = bpp
;
602 dri2_wl_get_buffers_with_format(driDrawable
,
604 attachments_with_format
, count
,
605 out_count
, loaderPrivate
);
607 free(attachments_with_format
);
613 image_get_buffers(__DRIdrawable
*driDrawable
,
617 uint32_t buffer_mask
,
618 struct __DRIimageList
*buffers
)
620 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
622 if (update_buffers(dri2_surf
) < 0)
625 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
626 buffers
->back
= dri2_surf
->back
->dri_image
;
632 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
635 (void) loaderPrivate
;
638 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
639 .base
= { __DRI_DRI2_LOADER
, 3 },
641 .getBuffers
= dri2_wl_get_buffers
,
642 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
643 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
646 static const __DRIimageLoaderExtension image_loader_extension
= {
647 .base
= { __DRI_IMAGE_LOADER
, 1 },
649 .getBuffers
= image_get_buffers
,
650 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
654 wayland_throttle_callback(void *data
,
655 struct wl_callback
*callback
,
658 struct dri2_egl_surface
*dri2_surf
= data
;
660 dri2_surf
->throttle_callback
= NULL
;
661 wl_callback_destroy(callback
);
664 static const struct wl_callback_listener throttle_listener
= {
665 .done
= wayland_throttle_callback
668 static struct wl_buffer
*
669 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
670 struct dri2_egl_surface
*dri2_surf
,
673 struct wl_buffer
*ret
;
674 int width
, height
, fourcc
, num_planes
;
676 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
677 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
678 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
);
679 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
682 if (dri2_dpy
->wl_dmabuf
&& dri2_dpy
->image
->base
.version
>= 15) {
683 struct zwp_linux_buffer_params_v1
*params
;
687 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
689 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
692 /* We don't need a wrapper for wl_dmabuf objects, because we have to
693 * create the intermediate params object; we can set the queue on this,
694 * and the wl_buffer inherits it race-free. */
695 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
697 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
699 for (i
= 0; i
< num_planes
; i
++) {
701 int stride
, offset
, fd
;
706 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
708 zwp_linux_buffer_params_v1_destroy(params
);
712 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
713 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_STRIDE
,
715 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_OFFSET
,
717 if (image
!= p_image
)
718 dri2_dpy
->image
->destroyImage(p_image
);
720 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
725 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
727 zwp_linux_buffer_params_v1_destroy(params
);
728 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
729 struct wl_drm
*wl_drm
=
730 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
736 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
737 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
738 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
742 struct wl_drm
*wl_drm
=
743 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
749 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
750 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
751 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
758 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
762 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
763 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
766 for (int i
= 0; i
< n_rects
; i
++) {
767 const int *rect
= &rects
[i
* 4];
769 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
771 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
777 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
780 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
786 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
787 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
789 while (dri2_surf
->throttle_callback
!= NULL
)
790 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
791 dri2_surf
->wl_queue
) == -1)
794 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
795 if (dri2_surf
->color_buffers
[i
].age
> 0)
796 dri2_surf
->color_buffers
[i
].age
++;
798 /* Make sure we have a back buffer in case we're swapping without ever
800 if (get_back_bo(dri2_surf
) < 0)
801 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
803 if (draw
->SwapInterval
> 0) {
804 dri2_surf
->throttle_callback
=
805 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
806 wl_callback_add_listener(dri2_surf
->throttle_callback
,
807 &throttle_listener
, dri2_surf
);
810 dri2_surf
->back
->age
= 1;
811 dri2_surf
->current
= dri2_surf
->back
;
812 dri2_surf
->back
= NULL
;
814 if (!dri2_surf
->current
->wl_buffer
) {
817 if (dri2_dpy
->is_different_gpu
)
818 image
= dri2_surf
->current
->linear_copy
;
820 image
= dri2_surf
->current
->dri_image
;
822 dri2_surf
->current
->wl_buffer
=
823 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
825 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
826 &wl_buffer_listener
, dri2_surf
);
829 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
830 dri2_surf
->current
->wl_buffer
,
831 dri2_surf
->dx
, dri2_surf
->dy
);
833 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
834 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
835 /* reset resize growing parameters */
839 /* If the compositor doesn't support damage_buffer, we deliberately
840 * ignore the damage region and post maximum damage, due to
841 * https://bugs.freedesktop.org/78190 */
842 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
843 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
844 0, 0, INT32_MAX
, INT32_MAX
);
846 if (dri2_dpy
->is_different_gpu
) {
847 _EGLContext
*ctx
= _eglGetCurrentContext();
848 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
849 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
850 dri2_surf
->current
->linear_copy
,
851 dri2_surf
->current
->dri_image
,
852 0, 0, dri2_surf
->base
.Width
,
853 dri2_surf
->base
.Height
,
854 0, 0, dri2_surf
->base
.Width
,
855 dri2_surf
->base
.Height
, 0);
858 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
859 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
861 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
863 /* If we're not waiting for a frame callback then we'll at least throttle
864 * to a sync callback so that we always give a chance for the compositor to
865 * handle the commit and send a release event before checking for a free
867 if (dri2_surf
->throttle_callback
== NULL
) {
868 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
869 wl_callback_add_listener(dri2_surf
->throttle_callback
,
870 &throttle_listener
, dri2_surf
);
873 wl_display_flush(dri2_dpy
->wl_dpy
);
879 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
880 _EGLDisplay
*disp
, _EGLSurface
*surface
)
882 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
884 if (get_back_bo(dri2_surf
) < 0) {
885 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
889 return dri2_surf
->back
->age
;
893 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
895 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
898 static struct wl_buffer
*
899 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
903 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
904 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
905 __DRIimage
*image
= dri2_img
->dri_image
;
906 struct wl_buffer
*buffer
;
909 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
911 case __DRI_IMAGE_FORMAT_ARGB8888
:
912 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
915 case __DRI_IMAGE_FORMAT_XRGB8888
:
916 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
923 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
925 /* The buffer object will have been created with our internal event queue
926 * because it is using the wl_drm object as a proxy factory. We want the
927 * buffer to be used by the application so we'll reset it to the display's
928 * default event queue. This isn't actually racy, as the only event the
929 * buffer can get is a buffer release, which doesn't happen with an explicit
932 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
937 _eglError(EGL_BAD_MATCH
, "unsupported image format");
942 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
944 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
947 if (dri2_dpy
->is_render_node
) {
948 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
949 "authenticate for render-nodes");
952 dri2_dpy
->authenticated
= false;
954 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
955 if (roundtrip(dri2_dpy
) < 0)
958 if (!dri2_dpy
->authenticated
)
961 /* reset authenticated */
962 dri2_dpy
->authenticated
= true;
968 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
970 struct dri2_egl_display
*dri2_dpy
= data
;
973 dri2_dpy
->device_name
= strdup(device
);
974 if (!dri2_dpy
->device_name
)
977 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
978 if (dri2_dpy
->fd
== -1) {
979 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
980 dri2_dpy
->device_name
, strerror(errno
));
984 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
985 dri2_dpy
->authenticated
= true;
987 drmGetMagic(dri2_dpy
->fd
, &magic
);
988 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
993 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
995 struct dri2_egl_display
*dri2_dpy
= data
;
998 case WL_DRM_FORMAT_ARGB8888
:
999 dri2_dpy
->formats
|= HAS_ARGB8888
;
1001 case WL_DRM_FORMAT_XRGB8888
:
1002 dri2_dpy
->formats
|= HAS_XRGB8888
;
1004 case WL_DRM_FORMAT_RGB565
:
1005 dri2_dpy
->formats
|= HAS_RGB565
;
1011 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1013 struct dri2_egl_display
*dri2_dpy
= data
;
1015 dri2_dpy
->capabilities
= value
;
1019 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1021 struct dri2_egl_display
*dri2_dpy
= data
;
1023 dri2_dpy
->authenticated
= true;
1026 static const struct wl_drm_listener drm_listener
= {
1027 .device
= drm_handle_device
,
1028 .format
= drm_handle_format
,
1029 .authenticated
= drm_handle_authenticated
,
1030 .capabilities
= drm_handle_capabilities
1034 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1037 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1041 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1042 uint32_t format
, uint32_t modifier_hi
,
1043 uint32_t modifier_lo
)
1045 struct dri2_egl_display
*dri2_dpy
= data
;
1046 uint64_t *mod
= NULL
;
1048 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1049 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1053 case WL_DRM_FORMAT_ARGB8888
:
1054 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1056 case WL_DRM_FORMAT_XRGB8888
:
1057 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1059 case WL_DRM_FORMAT_RGB565
:
1060 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1069 *mod
= (uint64_t) modifier_hi
<< 32;
1070 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1073 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1074 .format
= dmabuf_ignore_format
,
1075 .modifier
= dmabuf_handle_modifier
,
1079 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1080 uint32_t name
, const char *interface
,
1083 struct dri2_egl_display
*dri2_dpy
= data
;
1085 if (strcmp(interface
, "wl_drm") == 0) {
1087 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1088 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1089 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1090 dri2_dpy
->wl_dmabuf
=
1091 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1093 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1099 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1104 static const struct wl_registry_listener registry_listener_drm
= {
1105 .global
= registry_handle_global_drm
,
1106 .global_remove
= registry_handle_global_remove
1110 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1112 /* We can't use values greater than 1 on Wayland because we are using the
1113 * frame callback to synchronise the frame and the only way we be sure to
1114 * get a frame callback is to attach a new buffer. Therefore we can't just
1115 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1117 dri2_setup_swap_interval(disp
, 1);
1120 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1121 .authenticate
= dri2_wl_authenticate
,
1122 .create_window_surface
= dri2_wl_create_window_surface
,
1123 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1124 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1125 .destroy_surface
= dri2_wl_destroy_surface
,
1126 .create_image
= dri2_create_image_khr
,
1127 .swap_buffers
= dri2_wl_swap_buffers
,
1128 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1129 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1130 .set_damage_region
= dri2_fallback_set_damage_region
,
1131 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1132 .copy_buffers
= dri2_fallback_copy_buffers
,
1133 .query_buffer_age
= dri2_wl_query_buffer_age
,
1134 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1135 .get_sync_values
= dri2_fallback_get_sync_values
,
1136 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1139 static const __DRIextension
*dri2_loader_extensions
[] = {
1140 &dri2_loader_extension
.base
,
1141 &image_loader_extension
.base
,
1142 &image_lookup_extension
.base
,
1143 &use_invalidate
.base
,
1147 static const __DRIextension
*image_loader_extensions
[] = {
1148 &image_loader_extension
.base
,
1149 &image_lookup_extension
.base
,
1150 &use_invalidate
.base
,
1155 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1157 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1158 static const struct {
1159 const char *format_name
;
1161 unsigned int rgba_masks
[4];
1163 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1164 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1165 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1167 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1168 unsigned int count
= 0;
1170 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1171 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1172 struct dri2_egl_config
*dri2_conf
;
1174 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1177 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1178 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1180 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1187 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1188 if (!format_count
[i
]) {
1189 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1190 visuals
[i
].format_name
);
1194 return (count
!= 0);
1198 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1200 struct dri2_egl_display
*dri2_dpy
;
1202 loader_set_logger(_eglLog
);
1204 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1206 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1209 disp
->DriverData
= (void *) dri2_dpy
;
1210 if (disp
->PlatformDisplay
== NULL
) {
1211 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1212 if (dri2_dpy
->wl_dpy
== NULL
)
1214 dri2_dpy
->own_device
= true;
1216 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1219 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1220 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1221 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1225 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1227 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1228 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1231 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1232 dri2_dpy
->wl_queue
);
1234 if (dri2_dpy
->own_device
)
1235 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1237 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1238 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1239 ®istry_listener_drm
, dri2_dpy
);
1240 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1243 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1246 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1249 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1250 &dri2_dpy
->is_different_gpu
);
1251 if (dri2_dpy
->is_different_gpu
) {
1252 free(dri2_dpy
->device_name
);
1253 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1254 if (!dri2_dpy
->device_name
) {
1255 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1256 "for requested GPU");
1261 /* we have to do the check now, because loader_get_user_preferred_fd
1262 * will return a render-node when the requested gpu is different
1263 * to the server, but also if the client asks for the same gpu than
1264 * the server by requesting its pci-id */
1265 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1267 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1268 if (dri2_dpy
->driver_name
== NULL
) {
1269 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1273 /* render nodes cannot use Gem names, and thus do not support
1274 * the __DRI_DRI2_LOADER extension */
1275 if (!dri2_dpy
->is_render_node
) {
1276 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1277 if (!dri2_load_driver(disp
)) {
1278 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1282 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1283 if (!dri2_load_driver_dri3(disp
)) {
1284 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1289 if (!dri2_create_screen(disp
))
1292 if (!dri2_setup_extensions(disp
))
1295 dri2_setup_screen(disp
);
1297 dri2_wl_setup_swap_interval(disp
);
1299 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1300 * createImageFromFds support indicates that Prime export/import
1301 * is supported by the driver. Fall back to
1302 * gem names if we don't have Prime support. */
1304 if (dri2_dpy
->image
->base
.version
< 7 ||
1305 dri2_dpy
->image
->createImageFromFds
== NULL
)
1306 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1308 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1309 * The server needs to accept them */
1310 if (dri2_dpy
->is_render_node
&&
1311 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1312 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1316 if (dri2_dpy
->is_different_gpu
&&
1317 (dri2_dpy
->image
->base
.version
< 9 ||
1318 dri2_dpy
->image
->blitImage
== NULL
)) {
1319 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1320 "Image extension in the driver is not "
1321 "compatible. Version 9 or later and blitImage() "
1326 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1327 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1331 dri2_set_WL_bind_wayland_display(drv
, disp
);
1332 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1333 * because the buffer of the EGLImage has likely a tiling mode the server
1334 * gpu won't support. These is no way to check for now. Thus do not support the
1336 if (!dri2_dpy
->is_different_gpu
)
1337 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1339 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1341 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1343 /* Fill vtbl last to prevent accidentally calling virtual function during
1346 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1351 dri2_display_destroy(disp
);
1356 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1358 if (format
== WL_SHM_FORMAT_RGB565
)
1360 else /* ARGB8888 || XRGB8888 */
1365 * Taken from weston shared/os-compatibility.c
1368 #ifndef HAVE_MKOSTEMP
1371 set_cloexec_or_close(int fd
)
1378 flags
= fcntl(fd
, F_GETFD
);
1382 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1395 * Taken from weston shared/os-compatibility.c
1399 create_tmpfile_cloexec(char *tmpname
)
1403 #ifdef HAVE_MKOSTEMP
1404 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1408 fd
= mkstemp(tmpname
);
1410 fd
= set_cloexec_or_close(fd
);
1419 * Taken from weston shared/os-compatibility.c
1421 * Create a new, unique, anonymous file of the given size, and
1422 * return the file descriptor for it. The file descriptor is set
1423 * CLOEXEC. The file is immediately suitable for mmap()'ing
1424 * the given size at offset zero.
1426 * The file should not have a permanent backing store like a disk,
1427 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1429 * The file name is deleted from the file system.
1431 * The file is suitable for buffer sharing between processes by
1432 * transmitting the file descriptor over Unix sockets using the
1433 * SCM_RIGHTS methods.
1435 * If the C library implements posix_fallocate(), it is used to
1436 * guarantee that disk space is available for the file at the
1437 * given size. If disk space is insufficent, errno is set to ENOSPC.
1438 * If posix_fallocate() is not supported, program may receive
1439 * SIGBUS on accessing mmap()'ed file contents instead.
1442 os_create_anonymous_file(off_t size
)
1444 static const char template[] = "/mesa-shared-XXXXXX";
1450 path
= getenv("XDG_RUNTIME_DIR");
1456 name
= malloc(strlen(path
) + sizeof(template));
1461 strcat(name
, template);
1463 fd
= create_tmpfile_cloexec(name
);
1470 ret
= ftruncate(fd
, size
);
1481 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1482 int format
, int w
, int h
,
1483 void **data
, int *size
,
1484 struct wl_buffer
**buffer
)
1486 struct dri2_egl_display
*dri2_dpy
=
1487 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1488 struct wl_shm_pool
*pool
;
1489 int fd
, stride
, size_map
;
1492 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1493 size_map
= h
* stride
;
1495 /* Create a sharable buffer */
1496 fd
= os_create_anonymous_file(size_map
);
1500 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1501 if (data_map
== MAP_FAILED
) {
1506 /* Share it in a wl_buffer */
1507 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1508 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1509 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1510 wl_shm_pool_destroy(pool
);
1519 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1521 struct dri2_egl_display
*dri2_dpy
=
1522 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1524 /* we need to do the following operations only once per frame */
1525 if (dri2_surf
->back
)
1528 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1529 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1531 dri2_wl_release_buffers(dri2_surf
);
1533 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1534 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1535 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1536 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1537 dri2_surf
->current
= NULL
;
1540 /* find back buffer */
1542 /* There might be a buffer release already queued that wasn't processed */
1543 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1545 /* try get free buffer already created */
1546 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1547 if (!dri2_surf
->color_buffers
[i
].locked
&&
1548 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1549 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1554 /* else choose any another free location */
1555 if (!dri2_surf
->back
) {
1556 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1557 if (!dri2_surf
->color_buffers
[i
].locked
) {
1558 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1559 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1561 dri2_surf
->base
.Width
,
1562 dri2_surf
->base
.Height
,
1563 &dri2_surf
->back
->data
,
1564 &dri2_surf
->back
->data_size
,
1565 &dri2_surf
->back
->wl_buffer
)) {
1566 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1569 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1570 &wl_buffer_listener
, dri2_surf
);
1576 if (!dri2_surf
->back
) {
1577 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1581 dri2_surf
->back
->locked
= true;
1583 /* If we have an extra unlocked buffer at this point, we had to do triple
1584 * buffering for a while, but now can go back to just double buffering.
1585 * That means we can free any unlocked buffer now. */
1586 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1587 if (!dri2_surf
->color_buffers
[i
].locked
&&
1588 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1589 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1590 munmap(dri2_surf
->color_buffers
[i
].data
,
1591 dri2_surf
->color_buffers
[i
].data_size
);
1592 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1593 dri2_surf
->color_buffers
[i
].data
= NULL
;
1601 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1603 /* if there has been a resize: */
1604 if (!dri2_surf
->current
)
1607 return dri2_surf
->current
->data
;
1611 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1613 assert(dri2_surf
->back
);
1614 return dri2_surf
->back
->data
;
1618 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1620 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1622 while (dri2_surf
->throttle_callback
!= NULL
)
1623 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1624 dri2_surf
->wl_queue
) == -1)
1627 if (dri2_surf
->base
.SwapInterval
> 0) {
1628 dri2_surf
->throttle_callback
=
1629 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1630 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1631 &throttle_listener
, dri2_surf
);
1634 dri2_surf
->current
= dri2_surf
->back
;
1635 dri2_surf
->back
= NULL
;
1637 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1638 dri2_surf
->current
->wl_buffer
,
1639 dri2_surf
->dx
, dri2_surf
->dy
);
1641 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1642 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1643 /* reset resize growing parameters */
1647 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1648 0, 0, INT32_MAX
, INT32_MAX
);
1649 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1651 /* If we're not waiting for a frame callback then we'll at least throttle
1652 * to a sync callback so that we always give a chance for the compositor to
1653 * handle the commit and send a release event before checking for a free
1655 if (dri2_surf
->throttle_callback
== NULL
) {
1656 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1657 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1658 &throttle_listener
, dri2_surf
);
1661 wl_display_flush(dri2_dpy
->wl_dpy
);
1665 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1666 int *x
, int *y
, int *w
, int *h
,
1667 void *loaderPrivate
)
1669 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1671 (void) swrast_update_buffers(dri2_surf
);
1674 *w
= dri2_surf
->base
.Width
;
1675 *h
= dri2_surf
->base
.Height
;
1679 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1680 int x
, int y
, int w
, int h
,
1681 char *data
, void *loaderPrivate
)
1683 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1684 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1685 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1686 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1687 int dst_stride
= copy_width
;
1690 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1692 memset(data
, 0, copy_width
* h
);
1696 assert(data
!= src
);
1697 assert(copy_width
<= src_stride
);
1700 src
+= y
* src_stride
;
1703 if (copy_width
> src_stride
-x_offset
)
1704 copy_width
= src_stride
-x_offset
;
1705 if (h
> dri2_surf
->base
.Height
-y
)
1706 h
= dri2_surf
->base
.Height
-y
;
1709 memcpy(dst
, src
, copy_width
);
1716 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1717 int x
, int y
, int w
, int h
, int stride
,
1718 char *data
, void *loaderPrivate
)
1720 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1721 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1722 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1723 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1726 assert(copy_width
<= stride
);
1728 (void) swrast_update_buffers(dri2_surf
);
1729 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1731 /* partial copy, copy old content */
1732 if (copy_width
< dst_stride
)
1733 dri2_wl_swrast_get_image(draw
, 0, 0,
1734 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1735 dst
, loaderPrivate
);
1738 dst
+= y
* dst_stride
;
1742 /* drivers expect we do these checks (and some rely on it) */
1743 if (copy_width
> dst_stride
-x_offset
)
1744 copy_width
= dst_stride
-x_offset
;
1745 if (h
> dri2_surf
->base
.Height
-y
)
1746 h
= dri2_surf
->base
.Height
-y
;
1749 memcpy(dst
, src
, copy_width
);
1753 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1757 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1758 int x
, int y
, int w
, int h
,
1759 char *data
, void *loaderPrivate
)
1761 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1764 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1765 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1766 stride
, data
, loaderPrivate
);
1770 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1772 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1773 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1775 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1780 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1782 struct dri2_egl_display
*dri2_dpy
= data
;
1785 case WL_SHM_FORMAT_ARGB8888
:
1786 dri2_dpy
->formats
|= HAS_ARGB8888
;
1788 case WL_SHM_FORMAT_XRGB8888
:
1789 dri2_dpy
->formats
|= HAS_XRGB8888
;
1791 case WL_SHM_FORMAT_RGB565
:
1792 dri2_dpy
->formats
|= HAS_RGB565
;
1797 static const struct wl_shm_listener shm_listener
= {
1798 .format
= shm_handle_format
1802 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1803 uint32_t name
, const char *interface
,
1806 struct dri2_egl_display
*dri2_dpy
= data
;
1808 if (strcmp(interface
, "wl_shm") == 0) {
1810 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1811 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1815 static const struct wl_registry_listener registry_listener_swrast
= {
1816 .global
= registry_handle_global_swrast
,
1817 .global_remove
= registry_handle_global_remove
1820 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1821 .authenticate
= NULL
,
1822 .create_window_surface
= dri2_wl_create_window_surface
,
1823 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1824 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1825 .destroy_surface
= dri2_wl_destroy_surface
,
1826 .create_image
= dri2_create_image_khr
,
1827 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1828 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1829 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1830 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1831 .copy_buffers
= dri2_fallback_copy_buffers
,
1832 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1833 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1834 .get_sync_values
= dri2_fallback_get_sync_values
,
1835 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1838 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1839 .base
= { __DRI_SWRAST_LOADER
, 2 },
1841 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1842 .putImage
= dri2_wl_swrast_put_image
,
1843 .getImage
= dri2_wl_swrast_get_image
,
1844 .putImage2
= dri2_wl_swrast_put_image2
,
1847 static const __DRIextension
*swrast_loader_extensions
[] = {
1848 &swrast_loader_extension
.base
,
1849 &image_lookup_extension
.base
,
1854 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1856 struct dri2_egl_display
*dri2_dpy
;
1858 loader_set_logger(_eglLog
);
1860 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1862 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1865 disp
->DriverData
= (void *) dri2_dpy
;
1866 if (disp
->PlatformDisplay
== NULL
) {
1867 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1868 if (dri2_dpy
->wl_dpy
== NULL
)
1870 dri2_dpy
->own_device
= true;
1872 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1875 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1877 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1878 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1881 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1882 dri2_dpy
->wl_queue
);
1884 if (dri2_dpy
->own_device
)
1885 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1887 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1888 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1889 ®istry_listener_swrast
, dri2_dpy
);
1891 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1894 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1897 dri2_dpy
->driver_name
= strdup("swrast");
1898 if (!dri2_load_driver_swrast(disp
))
1901 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1903 if (!dri2_create_screen(disp
))
1906 if (!dri2_setup_extensions(disp
))
1909 dri2_setup_screen(disp
);
1911 dri2_wl_setup_swap_interval(disp
);
1913 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1914 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1918 /* Fill vtbl last to prevent accidentally calling virtual function during
1921 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1926 dri2_display_destroy(disp
);
1931 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1933 EGLBoolean initialized
= EGL_TRUE
;
1935 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1938 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1939 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1942 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);