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
;
166 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
170 dri2_surf
->wl_win
= window
;
171 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
172 if (!dri2_surf
->wl_queue
) {
173 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
177 if (dri2_dpy
->wl_drm
) {
178 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
179 if (!dri2_surf
->wl_drm_wrapper
) {
180 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
183 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
184 dri2_surf
->wl_queue
);
187 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
188 if (!dri2_surf
->wl_dpy_wrapper
) {
189 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
192 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
193 dri2_surf
->wl_queue
);
195 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
196 if (!dri2_surf
->wl_surface_wrapper
) {
197 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
200 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
201 dri2_surf
->wl_queue
);
203 dri2_surf
->wl_win
->private = dri2_surf
;
204 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
206 dri2_surf
->base
.Width
= -1;
207 dri2_surf
->base
.Height
= -1;
209 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
210 dri2_surf
->base
.GLColorspace
);
213 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
215 if (dri2_dpy
->image_driver
)
216 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
217 else if (dri2_dpy
->dri2
)
218 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
220 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
222 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
224 if (dri2_surf
->dri_drawable
== NULL
) {
225 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
229 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
231 return &dri2_surf
->base
;
234 if (dri2_surf
->wl_drm_wrapper
)
235 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
237 wl_event_queue_destroy(dri2_surf
->wl_queue
);
245 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
246 _EGLConfig
*conf
, void *native_window
,
247 const EGLint
*attrib_list
)
249 /* From the EGL_EXT_platform_wayland spec, version 3:
251 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
252 * that belongs to Wayland. Any such call fails and generates
255 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
261 * Called via eglDestroySurface(), drv->API.DestroySurface().
264 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
266 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
267 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
271 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
273 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
274 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
275 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
276 if (dri2_surf
->color_buffers
[i
].dri_image
)
277 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
278 if (dri2_surf
->color_buffers
[i
].linear_copy
)
279 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
280 if (dri2_surf
->color_buffers
[i
].data
)
281 munmap(dri2_surf
->color_buffers
[i
].data
,
282 dri2_surf
->color_buffers
[i
].data_size
);
285 if (dri2_dpy
->dri2
) {
286 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
287 if (dri2_surf
->dri_buffers
[i
] &&
288 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
289 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
290 dri2_surf
->dri_buffers
[i
]);
293 if (dri2_surf
->throttle_callback
)
294 wl_callback_destroy(dri2_surf
->throttle_callback
);
296 if (dri2_surf
->wl_win
) {
297 dri2_surf
->wl_win
->private = NULL
;
298 dri2_surf
->wl_win
->resize_callback
= NULL
;
299 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
302 if (dri2_surf
->wl_drm_wrapper
)
303 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
304 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
305 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
306 wl_event_queue_destroy(dri2_surf
->wl_queue
);
314 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
316 struct dri2_egl_display
*dri2_dpy
=
317 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
319 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
320 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
321 !dri2_surf
->color_buffers
[i
].locked
)
322 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
323 if (dri2_surf
->color_buffers
[i
].dri_image
)
324 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
325 if (dri2_surf
->color_buffers
[i
].linear_copy
)
326 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
327 if (dri2_surf
->color_buffers
[i
].data
)
328 munmap(dri2_surf
->color_buffers
[i
].data
,
329 dri2_surf
->color_buffers
[i
].data_size
);
331 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
332 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
333 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
334 dri2_surf
->color_buffers
[i
].data
= NULL
;
335 dri2_surf
->color_buffers
[i
].locked
= false;
338 if (dri2_dpy
->dri2
) {
339 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
340 if (dri2_surf
->dri_buffers
[i
] &&
341 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
342 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
343 dri2_surf
->dri_buffers
[i
]);
348 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
350 struct dri2_egl_display
*dri2_dpy
=
351 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
353 unsigned int dri_image_format
;
357 /* currently supports three WL DRM formats,
358 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
359 * and WL_DRM_FORMAT_RGB565
361 switch (dri2_surf
->format
) {
362 case WL_DRM_FORMAT_ARGB8888
:
363 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
364 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
365 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
367 case WL_DRM_FORMAT_XRGB8888
:
368 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
369 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
370 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
372 case WL_DRM_FORMAT_RGB565
:
373 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
374 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
375 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
378 /* format is not supported */
382 /* There might be a buffer release already queued that wasn't processed */
383 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
385 while (dri2_surf
->back
== NULL
) {
386 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
387 /* Get an unlocked buffer, preferrably one with a dri_buffer
388 * already allocated. */
389 if (dri2_surf
->color_buffers
[i
].locked
)
391 if (dri2_surf
->back
== NULL
)
392 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
393 else if (dri2_surf
->back
->dri_image
== NULL
)
394 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
400 /* If we don't have a buffer, then block on the server to release one for
401 * us, and try again. */
402 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
406 if (dri2_surf
->back
== NULL
)
409 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
411 if (dri2_dpy
->is_different_gpu
&&
412 dri2_surf
->back
->linear_copy
== NULL
) {
413 /* The LINEAR modifier should be a perfect alias of the LINEAR use
414 * flag; try the new interface first before the old, then fall back. */
415 if (dri2_dpy
->image
->base
.version
>= 15 &&
416 dri2_dpy
->image
->createImageWithModifiers
) {
417 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
419 dri2_surf
->back
->linear_copy
=
420 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
421 dri2_surf
->base
.Width
,
422 dri2_surf
->base
.Height
,
428 dri2_surf
->back
->linear_copy
=
429 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
430 dri2_surf
->base
.Width
,
431 dri2_surf
->base
.Height
,
434 __DRI_IMAGE_USE_LINEAR
,
437 if (dri2_surf
->back
->linear_copy
== NULL
)
441 if (dri2_surf
->back
->dri_image
== NULL
) {
442 /* If our DRIImage implementation does not support
443 * createImageWithModifiers, then fall back to the old createImage,
444 * and hope it allocates an image which is acceptable to the winsys.
446 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
447 dri2_dpy
->image
->createImageWithModifiers
) {
448 dri2_surf
->back
->dri_image
=
449 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
450 dri2_surf
->base
.Width
,
451 dri2_surf
->base
.Height
,
457 dri2_surf
->back
->dri_image
=
458 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
459 dri2_surf
->base
.Width
,
460 dri2_surf
->base
.Height
,
462 dri2_dpy
->is_different_gpu
?
467 dri2_surf
->back
->age
= 0;
469 if (dri2_surf
->back
->dri_image
== NULL
)
472 dri2_surf
->back
->locked
= true;
479 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
481 struct dri2_egl_display
*dri2_dpy
=
482 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
486 image
= dri2_surf
->back
->dri_image
;
488 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
489 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
491 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
493 buffer
->pitch
= pitch
;
499 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
500 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
502 struct dri2_egl_display
*dri2_dpy
=
503 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
504 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
507 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
509 dri2_surf
->base
.Width
,
510 dri2_surf
->base
.Height
);
511 dri2_surf
->dri_buffers
[attachment
] = b
;
516 memcpy(buffer
, b
, sizeof *buffer
);
522 update_buffers(struct dri2_egl_surface
*dri2_surf
)
524 struct dri2_egl_display
*dri2_dpy
=
525 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
527 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
528 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
530 dri2_wl_release_buffers(dri2_surf
);
532 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
533 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
534 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
535 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
538 if (get_back_bo(dri2_surf
) < 0) {
539 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
543 /* If we have an extra unlocked buffer at this point, we had to do triple
544 * buffering for a while, but now can go back to just double buffering.
545 * That means we can free any unlocked buffer now. */
546 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
547 if (!dri2_surf
->color_buffers
[i
].locked
&&
548 dri2_surf
->color_buffers
[i
].wl_buffer
) {
549 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
550 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
551 if (dri2_dpy
->is_different_gpu
)
552 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
553 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
554 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
555 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
563 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
564 int *width
, int *height
,
565 unsigned int *attachments
, int count
,
566 int *out_count
, void *loaderPrivate
)
568 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
571 if (update_buffers(dri2_surf
) < 0)
574 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
575 switch (attachments
[i
]) {
576 case __DRI_BUFFER_BACK_LEFT
:
577 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
580 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
581 &dri2_surf
->buffers
[j
]) < 0) {
582 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
593 *width
= dri2_surf
->base
.Width
;
594 *height
= dri2_surf
->base
.Height
;
596 return dri2_surf
->buffers
;
600 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
601 int *width
, int *height
,
602 unsigned int *attachments
, int count
,
603 int *out_count
, void *loaderPrivate
)
605 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
606 unsigned int *attachments_with_format
;
610 switch (dri2_surf
->format
) {
611 case WL_DRM_FORMAT_ARGB8888
:
612 case WL_DRM_FORMAT_XRGB8888
:
615 case WL_DRM_FORMAT_RGB565
:
619 /* format is not supported */
623 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
624 if (!attachments_with_format
) {
629 for (int i
= 0; i
< count
; ++i
) {
630 attachments_with_format
[2*i
] = attachments
[i
];
631 attachments_with_format
[2*i
+ 1] = bpp
;
635 dri2_wl_get_buffers_with_format(driDrawable
,
637 attachments_with_format
, count
,
638 out_count
, loaderPrivate
);
640 free(attachments_with_format
);
646 image_get_buffers(__DRIdrawable
*driDrawable
,
650 uint32_t buffer_mask
,
651 struct __DRIimageList
*buffers
)
653 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
655 if (update_buffers(dri2_surf
) < 0)
658 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
659 buffers
->back
= dri2_surf
->back
->dri_image
;
665 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
668 (void) loaderPrivate
;
671 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
672 .base
= { __DRI_DRI2_LOADER
, 3 },
674 .getBuffers
= dri2_wl_get_buffers
,
675 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
676 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
679 static const __DRIimageLoaderExtension image_loader_extension
= {
680 .base
= { __DRI_IMAGE_LOADER
, 1 },
682 .getBuffers
= image_get_buffers
,
683 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
687 wayland_throttle_callback(void *data
,
688 struct wl_callback
*callback
,
691 struct dri2_egl_surface
*dri2_surf
= data
;
693 dri2_surf
->throttle_callback
= NULL
;
694 wl_callback_destroy(callback
);
697 static const struct wl_callback_listener throttle_listener
= {
698 .done
= wayland_throttle_callback
701 static struct wl_buffer
*
702 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
703 struct dri2_egl_surface
*dri2_surf
,
706 struct wl_buffer
*ret
;
707 int width
, height
, fourcc
, num_planes
;
709 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
710 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
711 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
);
712 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
715 if (dri2_dpy
->wl_dmabuf
&& dri2_dpy
->image
->base
.version
>= 15) {
716 struct zwp_linux_buffer_params_v1
*params
;
720 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
722 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
725 /* We don't need a wrapper for wl_dmabuf objects, because we have to
726 * create the intermediate params object; we can set the queue on this,
727 * and the wl_buffer inherits it race-free. */
728 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
730 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
732 for (i
= 0; i
< num_planes
; i
++) {
734 int stride
, offset
, fd
;
739 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
741 zwp_linux_buffer_params_v1_destroy(params
);
745 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
746 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_STRIDE
,
748 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_OFFSET
,
750 if (image
!= p_image
)
751 dri2_dpy
->image
->destroyImage(p_image
);
753 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
758 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
760 zwp_linux_buffer_params_v1_destroy(params
);
761 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
762 struct wl_drm
*wl_drm
=
763 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
769 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
770 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
771 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
775 struct wl_drm
*wl_drm
=
776 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
782 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
783 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
784 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
791 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
795 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
796 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
799 for (int i
= 0; i
< n_rects
; i
++) {
800 const int *rect
= &rects
[i
* 4];
802 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
804 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
810 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
813 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
819 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
820 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
822 while (dri2_surf
->throttle_callback
!= NULL
)
823 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
824 dri2_surf
->wl_queue
) == -1)
827 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
828 if (dri2_surf
->color_buffers
[i
].age
> 0)
829 dri2_surf
->color_buffers
[i
].age
++;
831 /* Make sure we have a back buffer in case we're swapping without ever
833 if (get_back_bo(dri2_surf
) < 0)
834 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
836 if (draw
->SwapInterval
> 0) {
837 dri2_surf
->throttle_callback
=
838 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
839 wl_callback_add_listener(dri2_surf
->throttle_callback
,
840 &throttle_listener
, dri2_surf
);
843 dri2_surf
->back
->age
= 1;
844 dri2_surf
->current
= dri2_surf
->back
;
845 dri2_surf
->back
= NULL
;
847 if (!dri2_surf
->current
->wl_buffer
) {
850 if (dri2_dpy
->is_different_gpu
)
851 image
= dri2_surf
->current
->linear_copy
;
853 image
= dri2_surf
->current
->dri_image
;
855 dri2_surf
->current
->wl_buffer
=
856 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
858 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
859 &wl_buffer_listener
, dri2_surf
);
862 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
863 dri2_surf
->current
->wl_buffer
,
864 dri2_surf
->dx
, dri2_surf
->dy
);
866 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
867 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
868 /* reset resize growing parameters */
872 /* If the compositor doesn't support damage_buffer, we deliberately
873 * ignore the damage region and post maximum damage, due to
874 * https://bugs.freedesktop.org/78190 */
875 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
876 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
877 0, 0, INT32_MAX
, INT32_MAX
);
879 if (dri2_dpy
->is_different_gpu
) {
880 _EGLContext
*ctx
= _eglGetCurrentContext();
881 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
882 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
883 dri2_surf
->current
->linear_copy
,
884 dri2_surf
->current
->dri_image
,
885 0, 0, dri2_surf
->base
.Width
,
886 dri2_surf
->base
.Height
,
887 0, 0, dri2_surf
->base
.Width
,
888 dri2_surf
->base
.Height
, 0);
891 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
892 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
894 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
896 /* If we're not waiting for a frame callback then we'll at least throttle
897 * to a sync callback so that we always give a chance for the compositor to
898 * handle the commit and send a release event before checking for a free
900 if (dri2_surf
->throttle_callback
== NULL
) {
901 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
902 wl_callback_add_listener(dri2_surf
->throttle_callback
,
903 &throttle_listener
, dri2_surf
);
906 wl_display_flush(dri2_dpy
->wl_dpy
);
912 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
913 _EGLDisplay
*disp
, _EGLSurface
*surface
)
915 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
917 if (get_back_bo(dri2_surf
) < 0) {
918 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
922 return dri2_surf
->back
->age
;
926 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
928 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
931 static struct wl_buffer
*
932 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
936 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
937 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
938 __DRIimage
*image
= dri2_img
->dri_image
;
939 struct wl_buffer
*buffer
;
942 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
944 case __DRI_IMAGE_FORMAT_ARGB8888
:
945 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
948 case __DRI_IMAGE_FORMAT_XRGB8888
:
949 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
956 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
958 /* The buffer object will have been created with our internal event queue
959 * because it is using the wl_drm object as a proxy factory. We want the
960 * buffer to be used by the application so we'll reset it to the display's
961 * default event queue. This isn't actually racy, as the only event the
962 * buffer can get is a buffer release, which doesn't happen with an explicit
965 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
970 _eglError(EGL_BAD_MATCH
, "unsupported image format");
975 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
977 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
980 if (dri2_dpy
->is_render_node
) {
981 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
982 "authenticate for render-nodes");
985 dri2_dpy
->authenticated
= false;
987 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
988 if (roundtrip(dri2_dpy
) < 0)
991 if (!dri2_dpy
->authenticated
)
994 /* reset authenticated */
995 dri2_dpy
->authenticated
= true;
1001 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1003 struct dri2_egl_display
*dri2_dpy
= data
;
1006 dri2_dpy
->device_name
= strdup(device
);
1007 if (!dri2_dpy
->device_name
)
1010 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1011 if (dri2_dpy
->fd
== -1) {
1012 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1013 dri2_dpy
->device_name
, strerror(errno
));
1017 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1018 dri2_dpy
->authenticated
= true;
1020 drmGetMagic(dri2_dpy
->fd
, &magic
);
1021 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1026 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1028 struct dri2_egl_display
*dri2_dpy
= data
;
1031 case WL_DRM_FORMAT_ARGB8888
:
1032 dri2_dpy
->formats
|= HAS_ARGB8888
;
1034 case WL_DRM_FORMAT_XRGB8888
:
1035 dri2_dpy
->formats
|= HAS_XRGB8888
;
1037 case WL_DRM_FORMAT_RGB565
:
1038 dri2_dpy
->formats
|= HAS_RGB565
;
1044 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1046 struct dri2_egl_display
*dri2_dpy
= data
;
1048 dri2_dpy
->capabilities
= value
;
1052 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1054 struct dri2_egl_display
*dri2_dpy
= data
;
1056 dri2_dpy
->authenticated
= true;
1059 static const struct wl_drm_listener drm_listener
= {
1060 .device
= drm_handle_device
,
1061 .format
= drm_handle_format
,
1062 .authenticated
= drm_handle_authenticated
,
1063 .capabilities
= drm_handle_capabilities
1067 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1070 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1074 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1075 uint32_t format
, uint32_t modifier_hi
,
1076 uint32_t modifier_lo
)
1078 struct dri2_egl_display
*dri2_dpy
= data
;
1079 uint64_t *mod
= NULL
;
1081 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1082 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1086 case WL_DRM_FORMAT_ARGB8888
:
1087 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1089 case WL_DRM_FORMAT_XRGB8888
:
1090 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1092 case WL_DRM_FORMAT_RGB565
:
1093 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1102 *mod
= (uint64_t) modifier_hi
<< 32;
1103 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1106 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1107 .format
= dmabuf_ignore_format
,
1108 .modifier
= dmabuf_handle_modifier
,
1112 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1113 uint32_t name
, const char *interface
,
1116 struct dri2_egl_display
*dri2_dpy
= data
;
1118 if (strcmp(interface
, "wl_drm") == 0) {
1120 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1121 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1122 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1123 dri2_dpy
->wl_dmabuf
=
1124 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1126 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1132 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1137 static const struct wl_registry_listener registry_listener_drm
= {
1138 .global
= registry_handle_global_drm
,
1139 .global_remove
= registry_handle_global_remove
1143 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1145 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1147 /* We can't use values greater than 1 on Wayland because we are using the
1148 * frame callback to synchronise the frame and the only way we be sure to
1149 * get a frame callback is to attach a new buffer. Therefore we can't just
1150 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1152 if (dri2_dpy
->config
)
1153 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1154 "vblank_mode", &vblank_mode
);
1155 switch (vblank_mode
) {
1156 case DRI_CONF_VBLANK_NEVER
:
1157 dri2_dpy
->min_swap_interval
= 0;
1158 dri2_dpy
->max_swap_interval
= 0;
1159 dri2_dpy
->default_swap_interval
= 0;
1161 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1162 dri2_dpy
->min_swap_interval
= 1;
1163 dri2_dpy
->max_swap_interval
= 1;
1164 dri2_dpy
->default_swap_interval
= 1;
1166 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1167 dri2_dpy
->min_swap_interval
= 0;
1168 dri2_dpy
->max_swap_interval
= 1;
1169 dri2_dpy
->default_swap_interval
= 0;
1172 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1173 dri2_dpy
->min_swap_interval
= 0;
1174 dri2_dpy
->max_swap_interval
= 1;
1175 dri2_dpy
->default_swap_interval
= 1;
1180 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1181 .authenticate
= dri2_wl_authenticate
,
1182 .create_window_surface
= dri2_wl_create_window_surface
,
1183 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1184 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1185 .destroy_surface
= dri2_wl_destroy_surface
,
1186 .create_image
= dri2_create_image_khr
,
1187 .swap_buffers
= dri2_wl_swap_buffers
,
1188 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1189 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1190 .set_damage_region
= dri2_fallback_set_damage_region
,
1191 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1192 .copy_buffers
= dri2_fallback_copy_buffers
,
1193 .query_buffer_age
= dri2_wl_query_buffer_age
,
1194 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1195 .get_sync_values
= dri2_fallback_get_sync_values
,
1196 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1199 static const __DRIextension
*dri2_loader_extensions
[] = {
1200 &dri2_loader_extension
.base
,
1201 &image_loader_extension
.base
,
1202 &image_lookup_extension
.base
,
1203 &use_invalidate
.base
,
1207 static const __DRIextension
*image_loader_extensions
[] = {
1208 &image_loader_extension
.base
,
1209 &image_lookup_extension
.base
,
1210 &use_invalidate
.base
,
1215 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1217 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1218 static const struct {
1219 const char *format_name
;
1221 unsigned int rgba_masks
[4];
1223 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1224 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1225 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1227 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1228 unsigned int count
= 0;
1230 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1231 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1232 struct dri2_egl_config
*dri2_conf
;
1234 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1237 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1238 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1240 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1247 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1248 if (!format_count
[i
]) {
1249 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1250 visuals
[i
].format_name
);
1254 return (count
!= 0);
1258 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1260 struct dri2_egl_display
*dri2_dpy
;
1262 loader_set_logger(_eglLog
);
1264 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1266 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1269 disp
->DriverData
= (void *) dri2_dpy
;
1270 if (disp
->PlatformDisplay
== NULL
) {
1271 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1272 if (dri2_dpy
->wl_dpy
== NULL
)
1274 dri2_dpy
->own_device
= true;
1276 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1279 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1280 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1281 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1285 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1287 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1288 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1291 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1292 dri2_dpy
->wl_queue
);
1294 if (dri2_dpy
->own_device
)
1295 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1297 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1298 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1299 ®istry_listener_drm
, dri2_dpy
);
1300 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1303 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1306 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1309 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1310 &dri2_dpy
->is_different_gpu
);
1311 if (dri2_dpy
->is_different_gpu
) {
1312 free(dri2_dpy
->device_name
);
1313 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1314 if (!dri2_dpy
->device_name
) {
1315 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1316 "for requested GPU");
1321 /* we have to do the check now, because loader_get_user_preferred_fd
1322 * will return a render-node when the requested gpu is different
1323 * to the server, but also if the client asks for the same gpu than
1324 * the server by requesting its pci-id */
1325 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1327 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1328 if (dri2_dpy
->driver_name
== NULL
) {
1329 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1333 /* render nodes cannot use Gem names, and thus do not support
1334 * the __DRI_DRI2_LOADER extension */
1335 if (!dri2_dpy
->is_render_node
) {
1336 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1337 if (!dri2_load_driver(disp
)) {
1338 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1342 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1343 if (!dri2_load_driver_dri3(disp
)) {
1344 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1349 if (!dri2_create_screen(disp
))
1352 if (!dri2_setup_extensions(disp
))
1355 dri2_setup_screen(disp
);
1357 dri2_wl_setup_swap_interval(dri2_dpy
);
1359 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1360 * createImageFromFds support indicates that Prime export/import
1361 * is supported by the driver. Fall back to
1362 * gem names if we don't have Prime support. */
1364 if (dri2_dpy
->image
->base
.version
< 7 ||
1365 dri2_dpy
->image
->createImageFromFds
== NULL
)
1366 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1368 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1369 * The server needs to accept them */
1370 if (dri2_dpy
->is_render_node
&&
1371 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1372 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1376 if (dri2_dpy
->is_different_gpu
&&
1377 (dri2_dpy
->image
->base
.version
< 9 ||
1378 dri2_dpy
->image
->blitImage
== NULL
)) {
1379 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1380 "Image extension in the driver is not "
1381 "compatible. Version 9 or later and blitImage() "
1386 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1387 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1391 dri2_set_WL_bind_wayland_display(drv
, disp
);
1392 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1393 * because the buffer of the EGLImage has likely a tiling mode the server
1394 * gpu won't support. These is no way to check for now. Thus do not support the
1396 if (!dri2_dpy
->is_different_gpu
)
1397 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1399 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1401 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1403 /* Fill vtbl last to prevent accidentally calling virtual function during
1406 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1411 dri2_display_destroy(disp
);
1416 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1418 if (format
== WL_SHM_FORMAT_RGB565
)
1420 else /* ARGB8888 || XRGB8888 */
1425 * Taken from weston shared/os-compatibility.c
1428 #ifndef HAVE_MKOSTEMP
1431 set_cloexec_or_close(int fd
)
1438 flags
= fcntl(fd
, F_GETFD
);
1442 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1455 * Taken from weston shared/os-compatibility.c
1459 create_tmpfile_cloexec(char *tmpname
)
1463 #ifdef HAVE_MKOSTEMP
1464 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1468 fd
= mkstemp(tmpname
);
1470 fd
= set_cloexec_or_close(fd
);
1479 * Taken from weston shared/os-compatibility.c
1481 * Create a new, unique, anonymous file of the given size, and
1482 * return the file descriptor for it. The file descriptor is set
1483 * CLOEXEC. The file is immediately suitable for mmap()'ing
1484 * the given size at offset zero.
1486 * The file should not have a permanent backing store like a disk,
1487 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1489 * The file name is deleted from the file system.
1491 * The file is suitable for buffer sharing between processes by
1492 * transmitting the file descriptor over Unix sockets using the
1493 * SCM_RIGHTS methods.
1495 * If the C library implements posix_fallocate(), it is used to
1496 * guarantee that disk space is available for the file at the
1497 * given size. If disk space is insufficent, errno is set to ENOSPC.
1498 * If posix_fallocate() is not supported, program may receive
1499 * SIGBUS on accessing mmap()'ed file contents instead.
1502 os_create_anonymous_file(off_t size
)
1504 static const char template[] = "/mesa-shared-XXXXXX";
1510 path
= getenv("XDG_RUNTIME_DIR");
1516 name
= malloc(strlen(path
) + sizeof(template));
1521 strcat(name
, template);
1523 fd
= create_tmpfile_cloexec(name
);
1530 ret
= ftruncate(fd
, size
);
1541 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1542 int format
, int w
, int h
,
1543 void **data
, int *size
,
1544 struct wl_buffer
**buffer
)
1546 struct dri2_egl_display
*dri2_dpy
=
1547 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1548 struct wl_shm_pool
*pool
;
1549 int fd
, stride
, size_map
;
1552 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1553 size_map
= h
* stride
;
1555 /* Create a sharable buffer */
1556 fd
= os_create_anonymous_file(size_map
);
1560 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1561 if (data_map
== MAP_FAILED
) {
1566 /* Share it in a wl_buffer */
1567 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1568 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1569 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1570 wl_shm_pool_destroy(pool
);
1579 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1581 struct dri2_egl_display
*dri2_dpy
=
1582 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1584 /* we need to do the following operations only once per frame */
1585 if (dri2_surf
->back
)
1588 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1589 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1591 dri2_wl_release_buffers(dri2_surf
);
1593 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1594 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1595 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1596 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1597 dri2_surf
->current
= NULL
;
1600 /* find back buffer */
1602 /* There might be a buffer release already queued that wasn't processed */
1603 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1605 /* try get free buffer already created */
1606 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1607 if (!dri2_surf
->color_buffers
[i
].locked
&&
1608 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1609 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1614 /* else choose any another free location */
1615 if (!dri2_surf
->back
) {
1616 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1617 if (!dri2_surf
->color_buffers
[i
].locked
) {
1618 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1619 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1621 dri2_surf
->base
.Width
,
1622 dri2_surf
->base
.Height
,
1623 &dri2_surf
->back
->data
,
1624 &dri2_surf
->back
->data_size
,
1625 &dri2_surf
->back
->wl_buffer
)) {
1626 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1629 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1630 &wl_buffer_listener
, dri2_surf
);
1636 if (!dri2_surf
->back
) {
1637 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1641 dri2_surf
->back
->locked
= true;
1643 /* If we have an extra unlocked buffer at this point, we had to do triple
1644 * buffering for a while, but now can go back to just double buffering.
1645 * That means we can free any unlocked buffer now. */
1646 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1647 if (!dri2_surf
->color_buffers
[i
].locked
&&
1648 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1649 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1650 munmap(dri2_surf
->color_buffers
[i
].data
,
1651 dri2_surf
->color_buffers
[i
].data_size
);
1652 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1653 dri2_surf
->color_buffers
[i
].data
= NULL
;
1661 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1663 /* if there has been a resize: */
1664 if (!dri2_surf
->current
)
1667 return dri2_surf
->current
->data
;
1671 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1673 assert(dri2_surf
->back
);
1674 return dri2_surf
->back
->data
;
1678 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1680 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1682 while (dri2_surf
->throttle_callback
!= NULL
)
1683 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1684 dri2_surf
->wl_queue
) == -1)
1687 if (dri2_surf
->base
.SwapInterval
> 0) {
1688 dri2_surf
->throttle_callback
=
1689 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1690 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1691 &throttle_listener
, dri2_surf
);
1694 dri2_surf
->current
= dri2_surf
->back
;
1695 dri2_surf
->back
= NULL
;
1697 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1698 dri2_surf
->current
->wl_buffer
,
1699 dri2_surf
->dx
, dri2_surf
->dy
);
1701 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1702 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1703 /* reset resize growing parameters */
1707 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1708 0, 0, INT32_MAX
, INT32_MAX
);
1709 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1711 /* If we're not waiting for a frame callback then we'll at least throttle
1712 * to a sync callback so that we always give a chance for the compositor to
1713 * handle the commit and send a release event before checking for a free
1715 if (dri2_surf
->throttle_callback
== NULL
) {
1716 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1717 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1718 &throttle_listener
, dri2_surf
);
1721 wl_display_flush(dri2_dpy
->wl_dpy
);
1725 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1726 int *x
, int *y
, int *w
, int *h
,
1727 void *loaderPrivate
)
1729 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1731 (void) swrast_update_buffers(dri2_surf
);
1734 *w
= dri2_surf
->base
.Width
;
1735 *h
= dri2_surf
->base
.Height
;
1739 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1740 int x
, int y
, int w
, int h
,
1741 char *data
, void *loaderPrivate
)
1743 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1744 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1745 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1746 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1747 int dst_stride
= copy_width
;
1750 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1752 memset(data
, 0, copy_width
* h
);
1756 assert(data
!= src
);
1757 assert(copy_width
<= src_stride
);
1760 src
+= y
* src_stride
;
1763 if (copy_width
> src_stride
-x_offset
)
1764 copy_width
= src_stride
-x_offset
;
1765 if (h
> dri2_surf
->base
.Height
-y
)
1766 h
= dri2_surf
->base
.Height
-y
;
1769 memcpy(dst
, src
, copy_width
);
1776 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1777 int x
, int y
, int w
, int h
, int stride
,
1778 char *data
, void *loaderPrivate
)
1780 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1781 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1782 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1783 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1786 assert(copy_width
<= stride
);
1788 (void) swrast_update_buffers(dri2_surf
);
1789 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1791 /* partial copy, copy old content */
1792 if (copy_width
< dst_stride
)
1793 dri2_wl_swrast_get_image(draw
, 0, 0,
1794 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1795 dst
, loaderPrivate
);
1798 dst
+= y
* dst_stride
;
1802 /* drivers expect we do these checks (and some rely on it) */
1803 if (copy_width
> dst_stride
-x_offset
)
1804 copy_width
= dst_stride
-x_offset
;
1805 if (h
> dri2_surf
->base
.Height
-y
)
1806 h
= dri2_surf
->base
.Height
-y
;
1809 memcpy(dst
, src
, copy_width
);
1813 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1817 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1818 int x
, int y
, int w
, int h
,
1819 char *data
, void *loaderPrivate
)
1821 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1824 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1825 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1826 stride
, data
, loaderPrivate
);
1830 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1832 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1833 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1835 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1840 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1842 struct dri2_egl_display
*dri2_dpy
= data
;
1845 case WL_SHM_FORMAT_ARGB8888
:
1846 dri2_dpy
->formats
|= HAS_ARGB8888
;
1848 case WL_SHM_FORMAT_XRGB8888
:
1849 dri2_dpy
->formats
|= HAS_XRGB8888
;
1851 case WL_SHM_FORMAT_RGB565
:
1852 dri2_dpy
->formats
|= HAS_RGB565
;
1857 static const struct wl_shm_listener shm_listener
= {
1858 .format
= shm_handle_format
1862 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1863 uint32_t name
, const char *interface
,
1866 struct dri2_egl_display
*dri2_dpy
= data
;
1868 if (strcmp(interface
, "wl_shm") == 0) {
1870 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1871 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1875 static const struct wl_registry_listener registry_listener_swrast
= {
1876 .global
= registry_handle_global_swrast
,
1877 .global_remove
= registry_handle_global_remove
1880 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1881 .authenticate
= NULL
,
1882 .create_window_surface
= dri2_wl_create_window_surface
,
1883 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1884 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1885 .destroy_surface
= dri2_wl_destroy_surface
,
1886 .create_image
= dri2_create_image_khr
,
1887 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1888 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1889 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1890 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1891 .copy_buffers
= dri2_fallback_copy_buffers
,
1892 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1893 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1894 .get_sync_values
= dri2_fallback_get_sync_values
,
1895 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1898 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1899 .base
= { __DRI_SWRAST_LOADER
, 2 },
1901 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1902 .putImage
= dri2_wl_swrast_put_image
,
1903 .getImage
= dri2_wl_swrast_get_image
,
1904 .putImage2
= dri2_wl_swrast_put_image2
,
1907 static const __DRIextension
*swrast_loader_extensions
[] = {
1908 &swrast_loader_extension
.base
,
1909 &image_lookup_extension
.base
,
1914 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1916 struct dri2_egl_display
*dri2_dpy
;
1918 loader_set_logger(_eglLog
);
1920 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1922 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1925 disp
->DriverData
= (void *) dri2_dpy
;
1926 if (disp
->PlatformDisplay
== NULL
) {
1927 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1928 if (dri2_dpy
->wl_dpy
== NULL
)
1930 dri2_dpy
->own_device
= true;
1932 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1935 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1937 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1938 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1941 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1942 dri2_dpy
->wl_queue
);
1944 if (dri2_dpy
->own_device
)
1945 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1947 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1948 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1949 ®istry_listener_swrast
, dri2_dpy
);
1951 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1954 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1957 dri2_dpy
->driver_name
= strdup("swrast");
1958 if (!dri2_load_driver_swrast(disp
))
1961 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1963 if (!dri2_create_screen(disp
))
1966 if (!dri2_setup_extensions(disp
))
1969 dri2_setup_screen(disp
);
1971 dri2_wl_setup_swap_interval(dri2_dpy
);
1973 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1974 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1978 /* Fill vtbl last to prevent accidentally calling virtual function during
1981 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1986 dri2_display_destroy(disp
);
1991 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1993 EGLBoolean initialized
= EGL_TRUE
;
1995 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1998 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1999 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2002 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);