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"
47 #include <wayland-client.h>
48 #include "wayland-drm-client-protocol.h"
49 #include "linux-dmabuf-unstable-v1-client-protocol.h"
51 #ifndef DRM_FORMAT_MOD_INVALID
52 #define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
55 #ifndef DRM_FORMAT_MOD_LINEAR
56 #define DRM_FORMAT_MOD_LINEAR 0
59 enum wl_drm_format_flags
{
66 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
70 roundtrip(struct dri2_egl_display
*dri2_dpy
)
72 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
76 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
78 struct dri2_egl_surface
*dri2_surf
= data
;
81 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
82 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
85 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
86 wl_buffer_destroy(buffer
);
90 dri2_surf
->color_buffers
[i
].locked
= false;
93 static const struct wl_buffer_listener wl_buffer_listener
= {
94 .release
= wl_buffer_release
98 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
100 struct dri2_egl_surface
*dri2_surf
= data
;
101 struct dri2_egl_display
*dri2_dpy
=
102 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
104 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
108 destroy_window_callback(void *data
)
110 struct dri2_egl_surface
*dri2_surf
= data
;
111 dri2_surf
->wl_win
= NULL
;
115 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
118 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
119 _EGLConfig
*conf
, void *native_window
,
120 const EGLint
*attrib_list
)
122 __DRIcreateNewDrawableFunc createNewDrawable
;
123 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
124 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
125 struct wl_egl_window
*window
= native_window
;
126 struct dri2_egl_surface
*dri2_surf
;
127 const __DRIconfig
*config
;
129 dri2_surf
= calloc(1, sizeof *dri2_surf
);
131 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
135 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
138 if (dri2_dpy
->wl_drm
) {
139 if (conf
->RedSize
== 5)
140 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
141 else if (conf
->AlphaSize
== 0)
142 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
144 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
146 assert(dri2_dpy
->wl_shm
);
147 if (conf
->RedSize
== 5)
148 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
149 else if (conf
->AlphaSize
== 0)
150 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
152 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
156 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
160 dri2_surf
->wl_win
= window
;
161 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
162 if (!dri2_surf
->wl_queue
) {
163 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
167 if (dri2_dpy
->wl_drm
) {
168 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
169 if (!dri2_surf
->wl_drm_wrapper
) {
170 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
173 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
174 dri2_surf
->wl_queue
);
177 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
178 if (!dri2_surf
->wl_dpy_wrapper
) {
179 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
182 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
183 dri2_surf
->wl_queue
);
185 dri2_surf
->wl_surface_wrapper
= wl_proxy_create_wrapper(window
->surface
);
186 if (!dri2_surf
->wl_surface_wrapper
) {
187 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
190 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
191 dri2_surf
->wl_queue
);
193 dri2_surf
->wl_win
->private = dri2_surf
;
194 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
196 dri2_surf
->base
.Width
= -1;
197 dri2_surf
->base
.Height
= -1;
199 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
200 dri2_surf
->base
.GLColorspace
);
203 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
205 if (dri2_dpy
->image_driver
)
206 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
207 else if (dri2_dpy
->dri2
)
208 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
210 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
212 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
214 if (dri2_surf
->dri_drawable
== NULL
) {
215 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
219 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
220 dri2_dpy
->default_swap_interval
);
222 return &dri2_surf
->base
;
225 if (dri2_surf
->wl_drm_wrapper
)
226 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
228 wl_event_queue_destroy(dri2_surf
->wl_queue
);
236 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
237 _EGLConfig
*conf
, void *native_window
,
238 const EGLint
*attrib_list
)
240 /* From the EGL_EXT_platform_wayland spec, version 3:
242 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
243 * that belongs to Wayland. Any such call fails and generates
246 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
252 * Called via eglDestroySurface(), drv->API.DestroySurface().
255 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
257 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
258 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
262 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
264 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
265 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
266 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
267 if (dri2_surf
->color_buffers
[i
].dri_image
)
268 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
269 if (dri2_surf
->color_buffers
[i
].linear_copy
)
270 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
271 if (dri2_surf
->color_buffers
[i
].data
)
272 munmap(dri2_surf
->color_buffers
[i
].data
,
273 dri2_surf
->color_buffers
[i
].data_size
);
276 if (dri2_dpy
->dri2
) {
277 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
278 if (dri2_surf
->dri_buffers
[i
] &&
279 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
280 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
281 dri2_surf
->dri_buffers
[i
]);
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 if (dri2_surf
->wl_drm_wrapper
)
294 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
295 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
296 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
297 wl_event_queue_destroy(dri2_surf
->wl_queue
);
305 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
307 struct dri2_egl_display
*dri2_dpy
=
308 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
310 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
311 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
312 !dri2_surf
->color_buffers
[i
].locked
)
313 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
314 if (dri2_surf
->color_buffers
[i
].dri_image
)
315 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
316 if (dri2_surf
->color_buffers
[i
].linear_copy
)
317 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
318 if (dri2_surf
->color_buffers
[i
].data
)
319 munmap(dri2_surf
->color_buffers
[i
].data
,
320 dri2_surf
->color_buffers
[i
].data_size
);
322 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
323 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
324 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
325 dri2_surf
->color_buffers
[i
].data
= NULL
;
326 dri2_surf
->color_buffers
[i
].locked
= false;
329 if (dri2_dpy
->dri2
) {
330 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
331 if (dri2_surf
->dri_buffers
[i
] &&
332 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
333 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
334 dri2_surf
->dri_buffers
[i
]);
339 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
341 struct dri2_egl_display
*dri2_dpy
=
342 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
344 unsigned int dri_image_format
;
348 /* currently supports three WL DRM formats,
349 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
350 * and WL_DRM_FORMAT_RGB565
352 switch (dri2_surf
->format
) {
353 case WL_DRM_FORMAT_ARGB8888
:
354 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
355 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
356 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
358 case WL_DRM_FORMAT_XRGB8888
:
359 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
360 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
361 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
363 case WL_DRM_FORMAT_RGB565
:
364 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
365 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
366 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
369 /* format is not supported */
373 /* There might be a buffer release already queued that wasn't processed */
374 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
376 while (dri2_surf
->back
== NULL
) {
377 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
378 /* Get an unlocked buffer, preferrably one with a dri_buffer
379 * already allocated. */
380 if (dri2_surf
->color_buffers
[i
].locked
)
382 if (dri2_surf
->back
== NULL
)
383 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
384 else if (dri2_surf
->back
->dri_image
== NULL
)
385 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
391 /* If we don't have a buffer, then block on the server to release one for
392 * us, and try again. */
393 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
397 if (dri2_surf
->back
== NULL
)
400 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
402 if (dri2_dpy
->is_different_gpu
&&
403 dri2_surf
->back
->linear_copy
== NULL
) {
404 /* The LINEAR modifier should be a perfect alias of the LINEAR use
405 * flag; try the new interface first before the old, then fall back. */
406 if (dri2_dpy
->image
->base
.version
>= 15 &&
407 dri2_dpy
->image
->createImageWithModifiers
) {
408 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
410 dri2_surf
->back
->linear_copy
=
411 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
412 dri2_surf
->base
.Width
,
413 dri2_surf
->base
.Height
,
419 dri2_surf
->back
->linear_copy
=
420 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
421 dri2_surf
->base
.Width
,
422 dri2_surf
->base
.Height
,
425 __DRI_IMAGE_USE_LINEAR
,
428 if (dri2_surf
->back
->linear_copy
== NULL
)
432 if (dri2_surf
->back
->dri_image
== NULL
) {
433 /* If our DRIImage implementation does not support
434 * createImageWithModifiers, then fall back to the old createImage,
435 * and hope it allocates an image which is acceptable to the winsys.
437 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
438 dri2_dpy
->image
->createImageWithModifiers
) {
439 dri2_surf
->back
->dri_image
=
440 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
441 dri2_surf
->base
.Width
,
442 dri2_surf
->base
.Height
,
448 dri2_surf
->back
->dri_image
=
449 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
450 dri2_surf
->base
.Width
,
451 dri2_surf
->base
.Height
,
453 dri2_dpy
->is_different_gpu
?
458 dri2_surf
->back
->age
= 0;
460 if (dri2_surf
->back
->dri_image
== NULL
)
463 dri2_surf
->back
->locked
= true;
470 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
472 struct dri2_egl_display
*dri2_dpy
=
473 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
477 image
= dri2_surf
->back
->dri_image
;
479 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
480 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
482 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
484 buffer
->pitch
= pitch
;
490 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
491 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
493 struct dri2_egl_display
*dri2_dpy
=
494 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
495 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
498 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
500 dri2_surf
->base
.Width
,
501 dri2_surf
->base
.Height
);
502 dri2_surf
->dri_buffers
[attachment
] = b
;
507 memcpy(buffer
, b
, sizeof *buffer
);
513 update_buffers(struct dri2_egl_surface
*dri2_surf
)
515 struct dri2_egl_display
*dri2_dpy
=
516 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
518 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
519 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
521 dri2_wl_release_buffers(dri2_surf
);
523 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
524 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
525 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
526 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
529 if (get_back_bo(dri2_surf
) < 0) {
530 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
534 /* If we have an extra unlocked buffer at this point, we had to do triple
535 * buffering for a while, but now can go back to just double buffering.
536 * That means we can free any unlocked buffer now. */
537 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
538 if (!dri2_surf
->color_buffers
[i
].locked
&&
539 dri2_surf
->color_buffers
[i
].wl_buffer
) {
540 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
541 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
542 if (dri2_dpy
->is_different_gpu
)
543 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
544 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
545 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
546 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
554 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
555 int *width
, int *height
,
556 unsigned int *attachments
, int count
,
557 int *out_count
, void *loaderPrivate
)
559 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
562 if (update_buffers(dri2_surf
) < 0)
565 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
566 switch (attachments
[i
]) {
567 case __DRI_BUFFER_BACK_LEFT
:
568 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
571 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
572 &dri2_surf
->buffers
[j
]) < 0) {
573 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
584 *width
= dri2_surf
->base
.Width
;
585 *height
= dri2_surf
->base
.Height
;
587 return dri2_surf
->buffers
;
591 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
592 int *width
, int *height
,
593 unsigned int *attachments
, int count
,
594 int *out_count
, void *loaderPrivate
)
596 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
597 unsigned int *attachments_with_format
;
601 switch (dri2_surf
->format
) {
602 case WL_DRM_FORMAT_ARGB8888
:
603 case WL_DRM_FORMAT_XRGB8888
:
606 case WL_DRM_FORMAT_RGB565
:
610 /* format is not supported */
614 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
615 if (!attachments_with_format
) {
620 for (int i
= 0; i
< count
; ++i
) {
621 attachments_with_format
[2*i
] = attachments
[i
];
622 attachments_with_format
[2*i
+ 1] = bpp
;
626 dri2_wl_get_buffers_with_format(driDrawable
,
628 attachments_with_format
, count
,
629 out_count
, loaderPrivate
);
631 free(attachments_with_format
);
637 image_get_buffers(__DRIdrawable
*driDrawable
,
641 uint32_t buffer_mask
,
642 struct __DRIimageList
*buffers
)
644 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
646 if (update_buffers(dri2_surf
) < 0)
649 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
650 buffers
->back
= dri2_surf
->back
->dri_image
;
656 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
659 (void) loaderPrivate
;
662 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
663 .base
= { __DRI_DRI2_LOADER
, 3 },
665 .getBuffers
= dri2_wl_get_buffers
,
666 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
667 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
670 static const __DRIimageLoaderExtension image_loader_extension
= {
671 .base
= { __DRI_IMAGE_LOADER
, 1 },
673 .getBuffers
= image_get_buffers
,
674 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
678 wayland_throttle_callback(void *data
,
679 struct wl_callback
*callback
,
682 struct dri2_egl_surface
*dri2_surf
= data
;
684 dri2_surf
->throttle_callback
= NULL
;
685 wl_callback_destroy(callback
);
688 static const struct wl_callback_listener throttle_listener
= {
689 .done
= wayland_throttle_callback
692 static struct wl_buffer
*
693 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
694 struct dri2_egl_surface
*dri2_surf
,
697 struct wl_buffer
*ret
;
698 int width
, height
, fourcc
, num_planes
;
700 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
701 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
702 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
);
703 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
706 if (dri2_dpy
->wl_dmabuf
&& dri2_dpy
->image
->base
.version
>= 15) {
707 struct zwp_linux_buffer_params_v1
*params
;
711 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
713 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
716 /* We don't need a wrapper for wl_dmabuf objects, because we have to
717 * create the intermediate params object; we can set the queue on this,
718 * and the wl_buffer inherits it race-free. */
719 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
721 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
723 for (i
= 0; i
< num_planes
; i
++) {
725 int stride
, offset
, fd
;
730 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
732 zwp_linux_buffer_params_v1_destroy(params
);
736 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
737 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_STRIDE
,
739 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_OFFSET
,
741 if (image
!= p_image
)
742 dri2_dpy
->image
->destroyImage(p_image
);
744 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
749 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
751 zwp_linux_buffer_params_v1_destroy(params
);
752 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
753 struct wl_drm
*wl_drm
=
754 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
760 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
761 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
762 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
766 struct wl_drm
*wl_drm
=
767 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
773 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
774 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
775 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
782 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
786 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
787 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
790 for (int i
= 0; i
< n_rects
; i
++) {
791 const int *rect
= &rects
[i
* 4];
793 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
795 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
801 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
804 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
810 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
811 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
813 while (dri2_surf
->throttle_callback
!= NULL
)
814 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
815 dri2_surf
->wl_queue
) == -1)
818 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
819 if (dri2_surf
->color_buffers
[i
].age
> 0)
820 dri2_surf
->color_buffers
[i
].age
++;
822 /* Make sure we have a back buffer in case we're swapping without ever
824 if (get_back_bo(dri2_surf
) < 0)
825 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
827 if (draw
->SwapInterval
> 0) {
828 dri2_surf
->throttle_callback
=
829 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
830 wl_callback_add_listener(dri2_surf
->throttle_callback
,
831 &throttle_listener
, dri2_surf
);
834 dri2_surf
->back
->age
= 1;
835 dri2_surf
->current
= dri2_surf
->back
;
836 dri2_surf
->back
= NULL
;
838 if (!dri2_surf
->current
->wl_buffer
) {
841 if (dri2_dpy
->is_different_gpu
)
842 image
= dri2_surf
->current
->linear_copy
;
844 image
= dri2_surf
->current
->dri_image
;
846 dri2_surf
->current
->wl_buffer
=
847 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
849 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
850 &wl_buffer_listener
, dri2_surf
);
853 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
854 dri2_surf
->current
->wl_buffer
,
855 dri2_surf
->dx
, dri2_surf
->dy
);
857 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
858 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
859 /* reset resize growing parameters */
863 /* If the compositor doesn't support damage_buffer, we deliberately
864 * ignore the damage region and post maximum damage, due to
865 * https://bugs.freedesktop.org/78190 */
866 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
867 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
868 0, 0, INT32_MAX
, INT32_MAX
);
870 if (dri2_dpy
->is_different_gpu
) {
871 _EGLContext
*ctx
= _eglGetCurrentContext();
872 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
873 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
874 dri2_surf
->current
->linear_copy
,
875 dri2_surf
->current
->dri_image
,
876 0, 0, dri2_surf
->base
.Width
,
877 dri2_surf
->base
.Height
,
878 0, 0, dri2_surf
->base
.Width
,
879 dri2_surf
->base
.Height
, 0);
882 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
883 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
885 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
887 /* If we're not waiting for a frame callback then we'll at least throttle
888 * to a sync callback so that we always give a chance for the compositor to
889 * handle the commit and send a release event before checking for a free
891 if (dri2_surf
->throttle_callback
== NULL
) {
892 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
893 wl_callback_add_listener(dri2_surf
->throttle_callback
,
894 &throttle_listener
, dri2_surf
);
897 wl_display_flush(dri2_dpy
->wl_dpy
);
903 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
904 _EGLDisplay
*disp
, _EGLSurface
*surface
)
906 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
908 if (get_back_bo(dri2_surf
) < 0) {
909 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
913 return dri2_surf
->back
->age
;
917 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
919 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
922 static struct wl_buffer
*
923 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
927 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
928 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
929 __DRIimage
*image
= dri2_img
->dri_image
;
930 struct wl_buffer
*buffer
;
933 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
935 case __DRI_IMAGE_FORMAT_ARGB8888
:
936 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
939 case __DRI_IMAGE_FORMAT_XRGB8888
:
940 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
947 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
949 /* The buffer object will have been created with our internal event queue
950 * because it is using the wl_drm object as a proxy factory. We want the
951 * buffer to be used by the application so we'll reset it to the display's
952 * default event queue. This isn't actually racy, as the only event the
953 * buffer can get is a buffer release, which doesn't happen with an explicit
956 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
961 _eglError(EGL_BAD_MATCH
, "unsupported image format");
966 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
968 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
971 if (dri2_dpy
->is_render_node
) {
972 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
973 "authenticate for render-nodes");
976 dri2_dpy
->authenticated
= false;
978 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
979 if (roundtrip(dri2_dpy
) < 0)
982 if (!dri2_dpy
->authenticated
)
985 /* reset authenticated */
986 dri2_dpy
->authenticated
= true;
992 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
994 struct dri2_egl_display
*dri2_dpy
= data
;
997 dri2_dpy
->device_name
= strdup(device
);
998 if (!dri2_dpy
->device_name
)
1001 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1002 if (dri2_dpy
->fd
== -1) {
1003 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1004 dri2_dpy
->device_name
, strerror(errno
));
1008 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1009 dri2_dpy
->authenticated
= true;
1011 drmGetMagic(dri2_dpy
->fd
, &magic
);
1012 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1017 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1019 struct dri2_egl_display
*dri2_dpy
= data
;
1022 case WL_DRM_FORMAT_ARGB8888
:
1023 dri2_dpy
->formats
|= HAS_ARGB8888
;
1025 case WL_DRM_FORMAT_XRGB8888
:
1026 dri2_dpy
->formats
|= HAS_XRGB8888
;
1028 case WL_DRM_FORMAT_RGB565
:
1029 dri2_dpy
->formats
|= HAS_RGB565
;
1035 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1037 struct dri2_egl_display
*dri2_dpy
= data
;
1039 dri2_dpy
->capabilities
= value
;
1043 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1045 struct dri2_egl_display
*dri2_dpy
= data
;
1047 dri2_dpy
->authenticated
= true;
1050 static const struct wl_drm_listener drm_listener
= {
1051 .device
= drm_handle_device
,
1052 .format
= drm_handle_format
,
1053 .authenticated
= drm_handle_authenticated
,
1054 .capabilities
= drm_handle_capabilities
1058 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1061 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1065 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1066 uint32_t format
, uint32_t modifier_hi
,
1067 uint32_t modifier_lo
)
1069 struct dri2_egl_display
*dri2_dpy
= data
;
1070 uint64_t *mod
= NULL
;
1073 case WL_DRM_FORMAT_ARGB8888
:
1074 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1076 case WL_DRM_FORMAT_XRGB8888
:
1077 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1079 case WL_DRM_FORMAT_RGB565
:
1080 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1089 *mod
= (uint64_t) modifier_hi
<< 32;
1090 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1093 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1094 .format
= dmabuf_ignore_format
,
1095 .modifier
= dmabuf_handle_modifier
,
1099 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1100 uint32_t name
, const char *interface
,
1103 struct dri2_egl_display
*dri2_dpy
= data
;
1105 if (strcmp(interface
, "wl_drm") == 0) {
1107 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1108 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1109 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1110 dri2_dpy
->wl_dmabuf
=
1111 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1113 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1119 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1124 static const struct wl_registry_listener registry_listener_drm
= {
1125 .global
= registry_handle_global_drm
,
1126 .global_remove
= registry_handle_global_remove
1130 dri2_wl_swap_interval(_EGLDriver
*drv
,
1135 if (interval
> surf
->Config
->MaxSwapInterval
)
1136 interval
= surf
->Config
->MaxSwapInterval
;
1137 else if (interval
< surf
->Config
->MinSwapInterval
)
1138 interval
= surf
->Config
->MinSwapInterval
;
1140 surf
->SwapInterval
= interval
;
1146 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1148 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1150 /* We can't use values greater than 1 on Wayland because we are using the
1151 * frame callback to synchronise the frame and the only way we be sure to
1152 * get a frame callback is to attach a new buffer. Therefore we can't just
1153 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1155 if (dri2_dpy
->config
)
1156 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1157 "vblank_mode", &vblank_mode
);
1158 switch (vblank_mode
) {
1159 case DRI_CONF_VBLANK_NEVER
:
1160 dri2_dpy
->min_swap_interval
= 0;
1161 dri2_dpy
->max_swap_interval
= 0;
1162 dri2_dpy
->default_swap_interval
= 0;
1164 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1165 dri2_dpy
->min_swap_interval
= 1;
1166 dri2_dpy
->max_swap_interval
= 1;
1167 dri2_dpy
->default_swap_interval
= 1;
1169 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1170 dri2_dpy
->min_swap_interval
= 0;
1171 dri2_dpy
->max_swap_interval
= 1;
1172 dri2_dpy
->default_swap_interval
= 0;
1175 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1176 dri2_dpy
->min_swap_interval
= 0;
1177 dri2_dpy
->max_swap_interval
= 1;
1178 dri2_dpy
->default_swap_interval
= 1;
1183 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1184 .authenticate
= dri2_wl_authenticate
,
1185 .create_window_surface
= dri2_wl_create_window_surface
,
1186 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1187 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1188 .destroy_surface
= dri2_wl_destroy_surface
,
1189 .create_image
= dri2_create_image_khr
,
1190 .swap_interval
= dri2_wl_swap_interval
,
1191 .swap_buffers
= dri2_wl_swap_buffers
,
1192 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1193 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1194 .set_damage_region
= dri2_fallback_set_damage_region
,
1195 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1196 .copy_buffers
= dri2_fallback_copy_buffers
,
1197 .query_buffer_age
= dri2_wl_query_buffer_age
,
1198 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1199 .get_sync_values
= dri2_fallback_get_sync_values
,
1200 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1203 static const __DRIextension
*dri2_loader_extensions
[] = {
1204 &dri2_loader_extension
.base
,
1205 &image_loader_extension
.base
,
1206 &image_lookup_extension
.base
,
1207 &use_invalidate
.base
,
1211 static const __DRIextension
*image_loader_extensions
[] = {
1212 &image_loader_extension
.base
,
1213 &image_lookup_extension
.base
,
1214 &use_invalidate
.base
,
1219 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1221 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1222 static const struct {
1223 const char *format_name
;
1225 unsigned int rgba_masks
[4];
1227 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1228 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1229 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1231 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1232 unsigned int count
= 0;
1234 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1235 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1236 struct dri2_egl_config
*dri2_conf
;
1238 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1241 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1242 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1244 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1251 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1252 if (!format_count
[i
]) {
1253 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1254 visuals
[i
].format_name
);
1258 return (count
!= 0);
1262 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1264 struct dri2_egl_display
*dri2_dpy
;
1266 loader_set_logger(_eglLog
);
1268 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1270 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1273 disp
->DriverData
= (void *) dri2_dpy
;
1274 if (disp
->PlatformDisplay
== NULL
) {
1275 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1276 if (dri2_dpy
->wl_dpy
== NULL
)
1278 dri2_dpy
->own_device
= true;
1280 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1283 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1284 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1285 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1289 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1291 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1292 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1295 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1296 dri2_dpy
->wl_queue
);
1298 if (dri2_dpy
->own_device
)
1299 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1301 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1302 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1303 ®istry_listener_drm
, dri2_dpy
);
1304 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1307 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1310 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1313 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1314 &dri2_dpy
->is_different_gpu
);
1315 if (dri2_dpy
->is_different_gpu
) {
1316 free(dri2_dpy
->device_name
);
1317 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1318 if (!dri2_dpy
->device_name
) {
1319 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1320 "for requested GPU");
1325 /* we have to do the check now, because loader_get_user_preferred_fd
1326 * will return a render-node when the requested gpu is different
1327 * to the server, but also if the client asks for the same gpu than
1328 * the server by requesting its pci-id */
1329 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1331 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1332 if (dri2_dpy
->driver_name
== NULL
) {
1333 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1337 /* render nodes cannot use Gem names, and thus do not support
1338 * the __DRI_DRI2_LOADER extension */
1339 if (!dri2_dpy
->is_render_node
) {
1340 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1341 if (!dri2_load_driver(disp
)) {
1342 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1346 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1347 if (!dri2_load_driver_dri3(disp
)) {
1348 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1353 if (!dri2_create_screen(disp
))
1356 if (!dri2_setup_extensions(disp
))
1359 dri2_setup_screen(disp
);
1361 dri2_wl_setup_swap_interval(dri2_dpy
);
1363 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1364 * createImageFromFds support indicates that Prime export/import
1365 * is supported by the driver. Fall back to
1366 * gem names if we don't have Prime support. */
1368 if (dri2_dpy
->image
->base
.version
< 7 ||
1369 dri2_dpy
->image
->createImageFromFds
== NULL
)
1370 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1372 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1373 * The server needs to accept them */
1374 if (dri2_dpy
->is_render_node
&&
1375 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1376 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1380 if (dri2_dpy
->is_different_gpu
&&
1381 (dri2_dpy
->image
->base
.version
< 9 ||
1382 dri2_dpy
->image
->blitImage
== NULL
)) {
1383 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1384 "Image extension in the driver is not "
1385 "compatible. Version 9 or later and blitImage() "
1390 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1391 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1395 dri2_set_WL_bind_wayland_display(drv
, disp
);
1396 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1397 * because the buffer of the EGLImage has likely a tiling mode the server
1398 * gpu won't support. These is no way to check for now. Thus do not support the
1400 if (!dri2_dpy
->is_different_gpu
)
1401 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1403 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1405 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1407 /* Fill vtbl last to prevent accidentally calling virtual function during
1410 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1415 dri2_display_destroy(disp
);
1420 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1422 if (format
== WL_SHM_FORMAT_RGB565
)
1424 else /* ARGB8888 || XRGB8888 */
1429 * Taken from weston shared/os-compatibility.c
1432 #ifndef HAVE_MKOSTEMP
1435 set_cloexec_or_close(int fd
)
1442 flags
= fcntl(fd
, F_GETFD
);
1446 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1459 * Taken from weston shared/os-compatibility.c
1463 create_tmpfile_cloexec(char *tmpname
)
1467 #ifdef HAVE_MKOSTEMP
1468 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1472 fd
= mkstemp(tmpname
);
1474 fd
= set_cloexec_or_close(fd
);
1483 * Taken from weston shared/os-compatibility.c
1485 * Create a new, unique, anonymous file of the given size, and
1486 * return the file descriptor for it. The file descriptor is set
1487 * CLOEXEC. The file is immediately suitable for mmap()'ing
1488 * the given size at offset zero.
1490 * The file should not have a permanent backing store like a disk,
1491 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1493 * The file name is deleted from the file system.
1495 * The file is suitable for buffer sharing between processes by
1496 * transmitting the file descriptor over Unix sockets using the
1497 * SCM_RIGHTS methods.
1499 * If the C library implements posix_fallocate(), it is used to
1500 * guarantee that disk space is available for the file at the
1501 * given size. If disk space is insufficent, errno is set to ENOSPC.
1502 * If posix_fallocate() is not supported, program may receive
1503 * SIGBUS on accessing mmap()'ed file contents instead.
1506 os_create_anonymous_file(off_t size
)
1508 static const char template[] = "/mesa-shared-XXXXXX";
1514 path
= getenv("XDG_RUNTIME_DIR");
1520 name
= malloc(strlen(path
) + sizeof(template));
1525 strcat(name
, template);
1527 fd
= create_tmpfile_cloexec(name
);
1534 ret
= ftruncate(fd
, size
);
1545 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1546 int format
, int w
, int h
,
1547 void **data
, int *size
,
1548 struct wl_buffer
**buffer
)
1550 struct dri2_egl_display
*dri2_dpy
=
1551 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1552 struct wl_shm_pool
*pool
;
1553 int fd
, stride
, size_map
;
1556 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1557 size_map
= h
* stride
;
1559 /* Create a sharable buffer */
1560 fd
= os_create_anonymous_file(size_map
);
1564 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1565 if (data_map
== MAP_FAILED
) {
1570 /* Share it in a wl_buffer */
1571 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1572 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1573 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1574 wl_shm_pool_destroy(pool
);
1583 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1585 struct dri2_egl_display
*dri2_dpy
=
1586 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1588 /* we need to do the following operations only once per frame */
1589 if (dri2_surf
->back
)
1592 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1593 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1595 dri2_wl_release_buffers(dri2_surf
);
1597 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1598 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1599 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1600 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1601 dri2_surf
->current
= NULL
;
1604 /* find back buffer */
1606 /* There might be a buffer release already queued that wasn't processed */
1607 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1609 /* try get free buffer already created */
1610 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1611 if (!dri2_surf
->color_buffers
[i
].locked
&&
1612 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1613 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1618 /* else choose any another free location */
1619 if (!dri2_surf
->back
) {
1620 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1621 if (!dri2_surf
->color_buffers
[i
].locked
) {
1622 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1623 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1625 dri2_surf
->base
.Width
,
1626 dri2_surf
->base
.Height
,
1627 &dri2_surf
->back
->data
,
1628 &dri2_surf
->back
->data_size
,
1629 &dri2_surf
->back
->wl_buffer
)) {
1630 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1633 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1634 &wl_buffer_listener
, dri2_surf
);
1640 if (!dri2_surf
->back
) {
1641 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1645 dri2_surf
->back
->locked
= true;
1647 /* If we have an extra unlocked buffer at this point, we had to do triple
1648 * buffering for a while, but now can go back to just double buffering.
1649 * That means we can free any unlocked buffer now. */
1650 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1651 if (!dri2_surf
->color_buffers
[i
].locked
&&
1652 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1653 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1654 munmap(dri2_surf
->color_buffers
[i
].data
,
1655 dri2_surf
->color_buffers
[i
].data_size
);
1656 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1657 dri2_surf
->color_buffers
[i
].data
= NULL
;
1665 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1667 /* if there has been a resize: */
1668 if (!dri2_surf
->current
)
1671 return dri2_surf
->current
->data
;
1675 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1677 assert(dri2_surf
->back
);
1678 return dri2_surf
->back
->data
;
1682 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1684 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1686 while (dri2_surf
->throttle_callback
!= NULL
)
1687 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1688 dri2_surf
->wl_queue
) == -1)
1691 if (dri2_surf
->base
.SwapInterval
> 0) {
1692 dri2_surf
->throttle_callback
=
1693 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1694 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1695 &throttle_listener
, dri2_surf
);
1698 dri2_surf
->current
= dri2_surf
->back
;
1699 dri2_surf
->back
= NULL
;
1701 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1702 dri2_surf
->current
->wl_buffer
,
1703 dri2_surf
->dx
, dri2_surf
->dy
);
1705 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1706 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1707 /* reset resize growing parameters */
1711 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1712 0, 0, INT32_MAX
, INT32_MAX
);
1713 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1715 /* If we're not waiting for a frame callback then we'll at least throttle
1716 * to a sync callback so that we always give a chance for the compositor to
1717 * handle the commit and send a release event before checking for a free
1719 if (dri2_surf
->throttle_callback
== NULL
) {
1720 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1721 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1722 &throttle_listener
, dri2_surf
);
1725 wl_display_flush(dri2_dpy
->wl_dpy
);
1729 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1730 int *x
, int *y
, int *w
, int *h
,
1731 void *loaderPrivate
)
1733 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1735 (void) swrast_update_buffers(dri2_surf
);
1738 *w
= dri2_surf
->base
.Width
;
1739 *h
= dri2_surf
->base
.Height
;
1743 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1744 int x
, int y
, int w
, int h
,
1745 char *data
, void *loaderPrivate
)
1747 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1748 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1749 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1750 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1751 int dst_stride
= copy_width
;
1754 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1756 memset(data
, 0, copy_width
* h
);
1760 assert(data
!= src
);
1761 assert(copy_width
<= src_stride
);
1764 src
+= y
* src_stride
;
1767 if (copy_width
> src_stride
-x_offset
)
1768 copy_width
= src_stride
-x_offset
;
1769 if (h
> dri2_surf
->base
.Height
-y
)
1770 h
= dri2_surf
->base
.Height
-y
;
1773 memcpy(dst
, src
, copy_width
);
1780 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1781 int x
, int y
, int w
, int h
, int stride
,
1782 char *data
, void *loaderPrivate
)
1784 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1785 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1786 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1787 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1790 assert(copy_width
<= stride
);
1792 (void) swrast_update_buffers(dri2_surf
);
1793 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1795 /* partial copy, copy old content */
1796 if (copy_width
< dst_stride
)
1797 dri2_wl_swrast_get_image(draw
, 0, 0,
1798 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1799 dst
, loaderPrivate
);
1802 dst
+= y
* dst_stride
;
1806 /* drivers expect we do these checks (and some rely on it) */
1807 if (copy_width
> dst_stride
-x_offset
)
1808 copy_width
= dst_stride
-x_offset
;
1809 if (h
> dri2_surf
->base
.Height
-y
)
1810 h
= dri2_surf
->base
.Height
-y
;
1813 memcpy(dst
, src
, copy_width
);
1817 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1821 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1822 int x
, int y
, int w
, int h
,
1823 char *data
, void *loaderPrivate
)
1825 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1828 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1829 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1830 stride
, data
, loaderPrivate
);
1834 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1836 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1837 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1839 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1844 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1846 struct dri2_egl_display
*dri2_dpy
= data
;
1849 case WL_SHM_FORMAT_ARGB8888
:
1850 dri2_dpy
->formats
|= HAS_ARGB8888
;
1852 case WL_SHM_FORMAT_XRGB8888
:
1853 dri2_dpy
->formats
|= HAS_XRGB8888
;
1855 case WL_SHM_FORMAT_RGB565
:
1856 dri2_dpy
->formats
|= HAS_RGB565
;
1861 static const struct wl_shm_listener shm_listener
= {
1862 .format
= shm_handle_format
1866 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1867 uint32_t name
, const char *interface
,
1870 struct dri2_egl_display
*dri2_dpy
= data
;
1872 if (strcmp(interface
, "wl_shm") == 0) {
1874 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1875 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1879 static const struct wl_registry_listener registry_listener_swrast
= {
1880 .global
= registry_handle_global_swrast
,
1881 .global_remove
= registry_handle_global_remove
1884 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1885 .authenticate
= NULL
,
1886 .create_window_surface
= dri2_wl_create_window_surface
,
1887 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1888 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1889 .destroy_surface
= dri2_wl_destroy_surface
,
1890 .create_image
= dri2_fallback_create_image_khr
,
1891 .swap_interval
= dri2_wl_swap_interval
,
1892 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1893 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1894 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1895 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1896 .copy_buffers
= dri2_fallback_copy_buffers
,
1897 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1898 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1899 .get_sync_values
= dri2_fallback_get_sync_values
,
1900 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1903 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1904 .base
= { __DRI_SWRAST_LOADER
, 2 },
1906 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1907 .putImage
= dri2_wl_swrast_put_image
,
1908 .getImage
= dri2_wl_swrast_get_image
,
1909 .putImage2
= dri2_wl_swrast_put_image2
,
1912 static const __DRIextension
*swrast_loader_extensions
[] = {
1913 &swrast_loader_extension
.base
,
1918 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1920 struct dri2_egl_display
*dri2_dpy
;
1922 loader_set_logger(_eglLog
);
1924 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1926 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1929 disp
->DriverData
= (void *) dri2_dpy
;
1930 if (disp
->PlatformDisplay
== NULL
) {
1931 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1932 if (dri2_dpy
->wl_dpy
== NULL
)
1934 dri2_dpy
->own_device
= true;
1936 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1939 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1941 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1942 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1945 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1946 dri2_dpy
->wl_queue
);
1948 if (dri2_dpy
->own_device
)
1949 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1951 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1952 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1953 ®istry_listener_swrast
, dri2_dpy
);
1955 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1958 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1961 dri2_dpy
->driver_name
= strdup("swrast");
1962 if (!dri2_load_driver_swrast(disp
))
1965 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1967 if (!dri2_create_screen(disp
))
1970 if (!dri2_setup_extensions(disp
))
1973 dri2_setup_screen(disp
);
1975 dri2_wl_setup_swap_interval(dri2_dpy
);
1977 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1978 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1982 /* Fill vtbl last to prevent accidentally calling virtual function during
1985 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1990 dri2_display_destroy(disp
);
1995 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1997 EGLBoolean initialized
= EGL_TRUE
;
1999 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
2002 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
2003 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2006 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);