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 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
71 roundtrip(struct dri2_egl_display
*dri2_dpy
)
73 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
77 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
79 struct dri2_egl_surface
*dri2_surf
= data
;
82 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
83 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
86 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
87 wl_buffer_destroy(buffer
);
91 dri2_surf
->color_buffers
[i
].locked
= false;
94 static const struct wl_buffer_listener wl_buffer_listener
= {
95 .release
= wl_buffer_release
99 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
101 struct dri2_egl_surface
*dri2_surf
= data
;
102 struct dri2_egl_display
*dri2_dpy
=
103 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
105 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
109 destroy_window_callback(void *data
)
111 struct dri2_egl_surface
*dri2_surf
= data
;
112 dri2_surf
->wl_win
= NULL
;
115 static struct wl_surface
*
116 get_wl_surface_proxy(struct wl_egl_window
*window
)
118 /* Version 3 of wl_egl_window introduced a version field at the same
119 * location where a pointer to wl_surface was stored. Thus, if
120 * window->version is dereferencable, we've been given an older version of
121 * wl_egl_window, and window->version points to wl_surface */
122 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
123 return wl_proxy_create_wrapper((void *)(window
->version
));
125 return wl_proxy_create_wrapper(window
->surface
);
129 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
132 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
133 _EGLConfig
*conf
, void *native_window
,
134 const EGLint
*attrib_list
)
136 __DRIcreateNewDrawableFunc createNewDrawable
;
137 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
138 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
139 struct wl_egl_window
*window
= native_window
;
140 struct dri2_egl_surface
*dri2_surf
;
141 const __DRIconfig
*config
;
143 dri2_surf
= calloc(1, sizeof *dri2_surf
);
145 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
149 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
152 if (dri2_dpy
->wl_drm
) {
153 if (conf
->RedSize
== 5)
154 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
155 else if (conf
->AlphaSize
== 0)
156 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
158 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
160 assert(dri2_dpy
->wl_shm
);
161 if (conf
->RedSize
== 5)
162 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
163 else if (conf
->AlphaSize
== 0)
164 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
166 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
170 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
174 dri2_surf
->wl_win
= window
;
175 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
176 if (!dri2_surf
->wl_queue
) {
177 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
181 if (dri2_dpy
->wl_drm
) {
182 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
183 if (!dri2_surf
->wl_drm_wrapper
) {
184 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
187 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
188 dri2_surf
->wl_queue
);
191 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
192 if (!dri2_surf
->wl_dpy_wrapper
) {
193 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
196 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
197 dri2_surf
->wl_queue
);
199 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
200 if (!dri2_surf
->wl_surface_wrapper
) {
201 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
204 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
205 dri2_surf
->wl_queue
);
207 dri2_surf
->wl_win
->private = dri2_surf
;
208 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
210 dri2_surf
->base
.Width
= -1;
211 dri2_surf
->base
.Height
= -1;
213 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
214 dri2_surf
->base
.GLColorspace
);
217 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
219 if (dri2_dpy
->image_driver
)
220 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
221 else if (dri2_dpy
->dri2
)
222 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
224 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
226 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
228 if (dri2_surf
->dri_drawable
== NULL
) {
229 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
233 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
234 dri2_dpy
->default_swap_interval
);
236 return &dri2_surf
->base
;
239 if (dri2_surf
->wl_drm_wrapper
)
240 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
242 wl_event_queue_destroy(dri2_surf
->wl_queue
);
250 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
251 _EGLConfig
*conf
, void *native_window
,
252 const EGLint
*attrib_list
)
254 /* From the EGL_EXT_platform_wayland spec, version 3:
256 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
257 * that belongs to Wayland. Any such call fails and generates
260 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
266 * Called via eglDestroySurface(), drv->API.DestroySurface().
269 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
271 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
272 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
276 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
278 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
279 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
280 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
281 if (dri2_surf
->color_buffers
[i
].dri_image
)
282 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
283 if (dri2_surf
->color_buffers
[i
].linear_copy
)
284 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
285 if (dri2_surf
->color_buffers
[i
].data
)
286 munmap(dri2_surf
->color_buffers
[i
].data
,
287 dri2_surf
->color_buffers
[i
].data_size
);
290 if (dri2_dpy
->dri2
) {
291 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
292 if (dri2_surf
->dri_buffers
[i
] &&
293 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
294 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
295 dri2_surf
->dri_buffers
[i
]);
298 if (dri2_surf
->throttle_callback
)
299 wl_callback_destroy(dri2_surf
->throttle_callback
);
301 if (dri2_surf
->wl_win
) {
302 dri2_surf
->wl_win
->private = NULL
;
303 dri2_surf
->wl_win
->resize_callback
= NULL
;
304 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
307 if (dri2_surf
->wl_drm_wrapper
)
308 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
309 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
310 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
311 wl_event_queue_destroy(dri2_surf
->wl_queue
);
319 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
321 struct dri2_egl_display
*dri2_dpy
=
322 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
324 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
325 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
326 !dri2_surf
->color_buffers
[i
].locked
)
327 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
328 if (dri2_surf
->color_buffers
[i
].dri_image
)
329 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
330 if (dri2_surf
->color_buffers
[i
].linear_copy
)
331 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
332 if (dri2_surf
->color_buffers
[i
].data
)
333 munmap(dri2_surf
->color_buffers
[i
].data
,
334 dri2_surf
->color_buffers
[i
].data_size
);
336 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
337 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
338 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
339 dri2_surf
->color_buffers
[i
].data
= NULL
;
340 dri2_surf
->color_buffers
[i
].locked
= false;
343 if (dri2_dpy
->dri2
) {
344 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
345 if (dri2_surf
->dri_buffers
[i
] &&
346 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
347 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
348 dri2_surf
->dri_buffers
[i
]);
353 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
355 struct dri2_egl_display
*dri2_dpy
=
356 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
358 unsigned int dri_image_format
;
362 /* currently supports three WL DRM formats,
363 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
364 * and WL_DRM_FORMAT_RGB565
366 switch (dri2_surf
->format
) {
367 case WL_DRM_FORMAT_ARGB8888
:
368 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
369 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
370 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
372 case WL_DRM_FORMAT_XRGB8888
:
373 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
374 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
375 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
377 case WL_DRM_FORMAT_RGB565
:
378 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
379 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
380 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
383 /* format is not supported */
387 /* There might be a buffer release already queued that wasn't processed */
388 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
390 while (dri2_surf
->back
== NULL
) {
391 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
392 /* Get an unlocked buffer, preferrably one with a dri_buffer
393 * already allocated. */
394 if (dri2_surf
->color_buffers
[i
].locked
)
396 if (dri2_surf
->back
== NULL
)
397 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
398 else if (dri2_surf
->back
->dri_image
== NULL
)
399 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
405 /* If we don't have a buffer, then block on the server to release one for
406 * us, and try again. */
407 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
411 if (dri2_surf
->back
== NULL
)
414 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
416 if (dri2_dpy
->is_different_gpu
&&
417 dri2_surf
->back
->linear_copy
== NULL
) {
418 /* The LINEAR modifier should be a perfect alias of the LINEAR use
419 * flag; try the new interface first before the old, then fall back. */
420 if (dri2_dpy
->image
->base
.version
>= 15 &&
421 dri2_dpy
->image
->createImageWithModifiers
) {
422 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
424 dri2_surf
->back
->linear_copy
=
425 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
426 dri2_surf
->base
.Width
,
427 dri2_surf
->base
.Height
,
433 dri2_surf
->back
->linear_copy
=
434 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
435 dri2_surf
->base
.Width
,
436 dri2_surf
->base
.Height
,
439 __DRI_IMAGE_USE_LINEAR
,
442 if (dri2_surf
->back
->linear_copy
== NULL
)
446 if (dri2_surf
->back
->dri_image
== NULL
) {
447 /* If our DRIImage implementation does not support
448 * createImageWithModifiers, then fall back to the old createImage,
449 * and hope it allocates an image which is acceptable to the winsys.
451 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
452 dri2_dpy
->image
->createImageWithModifiers
) {
453 dri2_surf
->back
->dri_image
=
454 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
455 dri2_surf
->base
.Width
,
456 dri2_surf
->base
.Height
,
462 dri2_surf
->back
->dri_image
=
463 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
464 dri2_surf
->base
.Width
,
465 dri2_surf
->base
.Height
,
467 dri2_dpy
->is_different_gpu
?
472 dri2_surf
->back
->age
= 0;
474 if (dri2_surf
->back
->dri_image
== NULL
)
477 dri2_surf
->back
->locked
= true;
484 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
486 struct dri2_egl_display
*dri2_dpy
=
487 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
491 image
= dri2_surf
->back
->dri_image
;
493 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
494 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
496 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
498 buffer
->pitch
= pitch
;
504 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
505 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
507 struct dri2_egl_display
*dri2_dpy
=
508 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
509 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
512 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
514 dri2_surf
->base
.Width
,
515 dri2_surf
->base
.Height
);
516 dri2_surf
->dri_buffers
[attachment
] = b
;
521 memcpy(buffer
, b
, sizeof *buffer
);
527 update_buffers(struct dri2_egl_surface
*dri2_surf
)
529 struct dri2_egl_display
*dri2_dpy
=
530 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
532 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
533 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
535 dri2_wl_release_buffers(dri2_surf
);
537 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
538 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
539 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
540 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
543 if (get_back_bo(dri2_surf
) < 0) {
544 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
548 /* If we have an extra unlocked buffer at this point, we had to do triple
549 * buffering for a while, but now can go back to just double buffering.
550 * That means we can free any unlocked buffer now. */
551 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
552 if (!dri2_surf
->color_buffers
[i
].locked
&&
553 dri2_surf
->color_buffers
[i
].wl_buffer
) {
554 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
555 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
556 if (dri2_dpy
->is_different_gpu
)
557 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
558 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
559 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
560 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
568 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
569 int *width
, int *height
,
570 unsigned int *attachments
, int count
,
571 int *out_count
, void *loaderPrivate
)
573 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
576 if (update_buffers(dri2_surf
) < 0)
579 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
580 switch (attachments
[i
]) {
581 case __DRI_BUFFER_BACK_LEFT
:
582 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
585 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
586 &dri2_surf
->buffers
[j
]) < 0) {
587 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
598 *width
= dri2_surf
->base
.Width
;
599 *height
= dri2_surf
->base
.Height
;
601 return dri2_surf
->buffers
;
605 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
606 int *width
, int *height
,
607 unsigned int *attachments
, int count
,
608 int *out_count
, void *loaderPrivate
)
610 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
611 unsigned int *attachments_with_format
;
615 switch (dri2_surf
->format
) {
616 case WL_DRM_FORMAT_ARGB8888
:
617 case WL_DRM_FORMAT_XRGB8888
:
620 case WL_DRM_FORMAT_RGB565
:
624 /* format is not supported */
628 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
629 if (!attachments_with_format
) {
634 for (int i
= 0; i
< count
; ++i
) {
635 attachments_with_format
[2*i
] = attachments
[i
];
636 attachments_with_format
[2*i
+ 1] = bpp
;
640 dri2_wl_get_buffers_with_format(driDrawable
,
642 attachments_with_format
, count
,
643 out_count
, loaderPrivate
);
645 free(attachments_with_format
);
651 image_get_buffers(__DRIdrawable
*driDrawable
,
655 uint32_t buffer_mask
,
656 struct __DRIimageList
*buffers
)
658 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
660 if (update_buffers(dri2_surf
) < 0)
663 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
664 buffers
->back
= dri2_surf
->back
->dri_image
;
670 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
673 (void) loaderPrivate
;
676 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
677 .base
= { __DRI_DRI2_LOADER
, 3 },
679 .getBuffers
= dri2_wl_get_buffers
,
680 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
681 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
684 static const __DRIimageLoaderExtension image_loader_extension
= {
685 .base
= { __DRI_IMAGE_LOADER
, 1 },
687 .getBuffers
= image_get_buffers
,
688 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
692 wayland_throttle_callback(void *data
,
693 struct wl_callback
*callback
,
696 struct dri2_egl_surface
*dri2_surf
= data
;
698 dri2_surf
->throttle_callback
= NULL
;
699 wl_callback_destroy(callback
);
702 static const struct wl_callback_listener throttle_listener
= {
703 .done
= wayland_throttle_callback
706 static struct wl_buffer
*
707 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
708 struct dri2_egl_surface
*dri2_surf
,
711 struct wl_buffer
*ret
;
712 int width
, height
, fourcc
, num_planes
;
714 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
715 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
716 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
);
717 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
720 if (dri2_dpy
->wl_dmabuf
&& dri2_dpy
->image
->base
.version
>= 15) {
721 struct zwp_linux_buffer_params_v1
*params
;
725 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
727 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
730 /* We don't need a wrapper for wl_dmabuf objects, because we have to
731 * create the intermediate params object; we can set the queue on this,
732 * and the wl_buffer inherits it race-free. */
733 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
735 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
737 for (i
= 0; i
< num_planes
; i
++) {
739 int stride
, offset
, fd
;
744 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
746 zwp_linux_buffer_params_v1_destroy(params
);
750 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
751 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_STRIDE
,
753 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_OFFSET
,
755 if (image
!= p_image
)
756 dri2_dpy
->image
->destroyImage(p_image
);
758 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
763 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
765 zwp_linux_buffer_params_v1_destroy(params
);
766 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
767 struct wl_drm
*wl_drm
=
768 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
774 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
775 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
776 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
780 struct wl_drm
*wl_drm
=
781 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
787 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
788 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
789 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
796 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
800 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
801 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
804 for (int i
= 0; i
< n_rects
; i
++) {
805 const int *rect
= &rects
[i
* 4];
807 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
809 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
815 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
818 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
824 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
825 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
827 while (dri2_surf
->throttle_callback
!= NULL
)
828 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
829 dri2_surf
->wl_queue
) == -1)
832 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
833 if (dri2_surf
->color_buffers
[i
].age
> 0)
834 dri2_surf
->color_buffers
[i
].age
++;
836 /* Make sure we have a back buffer in case we're swapping without ever
838 if (get_back_bo(dri2_surf
) < 0)
839 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
841 if (draw
->SwapInterval
> 0) {
842 dri2_surf
->throttle_callback
=
843 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
844 wl_callback_add_listener(dri2_surf
->throttle_callback
,
845 &throttle_listener
, dri2_surf
);
848 dri2_surf
->back
->age
= 1;
849 dri2_surf
->current
= dri2_surf
->back
;
850 dri2_surf
->back
= NULL
;
852 if (!dri2_surf
->current
->wl_buffer
) {
855 if (dri2_dpy
->is_different_gpu
)
856 image
= dri2_surf
->current
->linear_copy
;
858 image
= dri2_surf
->current
->dri_image
;
860 dri2_surf
->current
->wl_buffer
=
861 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
863 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
864 &wl_buffer_listener
, dri2_surf
);
867 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
868 dri2_surf
->current
->wl_buffer
,
869 dri2_surf
->dx
, dri2_surf
->dy
);
871 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
872 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
873 /* reset resize growing parameters */
877 /* If the compositor doesn't support damage_buffer, we deliberately
878 * ignore the damage region and post maximum damage, due to
879 * https://bugs.freedesktop.org/78190 */
880 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
881 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
882 0, 0, INT32_MAX
, INT32_MAX
);
884 if (dri2_dpy
->is_different_gpu
) {
885 _EGLContext
*ctx
= _eglGetCurrentContext();
886 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
887 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
888 dri2_surf
->current
->linear_copy
,
889 dri2_surf
->current
->dri_image
,
890 0, 0, dri2_surf
->base
.Width
,
891 dri2_surf
->base
.Height
,
892 0, 0, dri2_surf
->base
.Width
,
893 dri2_surf
->base
.Height
, 0);
896 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
897 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
899 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
901 /* If we're not waiting for a frame callback then we'll at least throttle
902 * to a sync callback so that we always give a chance for the compositor to
903 * handle the commit and send a release event before checking for a free
905 if (dri2_surf
->throttle_callback
== NULL
) {
906 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
907 wl_callback_add_listener(dri2_surf
->throttle_callback
,
908 &throttle_listener
, dri2_surf
);
911 wl_display_flush(dri2_dpy
->wl_dpy
);
917 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
918 _EGLDisplay
*disp
, _EGLSurface
*surface
)
920 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
922 if (get_back_bo(dri2_surf
) < 0) {
923 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
927 return dri2_surf
->back
->age
;
931 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
933 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
936 static struct wl_buffer
*
937 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
941 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
942 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
943 __DRIimage
*image
= dri2_img
->dri_image
;
944 struct wl_buffer
*buffer
;
947 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
949 case __DRI_IMAGE_FORMAT_ARGB8888
:
950 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
953 case __DRI_IMAGE_FORMAT_XRGB8888
:
954 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
961 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
963 /* The buffer object will have been created with our internal event queue
964 * because it is using the wl_drm object as a proxy factory. We want the
965 * buffer to be used by the application so we'll reset it to the display's
966 * default event queue. This isn't actually racy, as the only event the
967 * buffer can get is a buffer release, which doesn't happen with an explicit
970 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
975 _eglError(EGL_BAD_MATCH
, "unsupported image format");
980 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
982 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
985 if (dri2_dpy
->is_render_node
) {
986 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
987 "authenticate for render-nodes");
990 dri2_dpy
->authenticated
= false;
992 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
993 if (roundtrip(dri2_dpy
) < 0)
996 if (!dri2_dpy
->authenticated
)
999 /* reset authenticated */
1000 dri2_dpy
->authenticated
= true;
1006 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1008 struct dri2_egl_display
*dri2_dpy
= data
;
1011 dri2_dpy
->device_name
= strdup(device
);
1012 if (!dri2_dpy
->device_name
)
1015 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1016 if (dri2_dpy
->fd
== -1) {
1017 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1018 dri2_dpy
->device_name
, strerror(errno
));
1022 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1023 dri2_dpy
->authenticated
= true;
1025 drmGetMagic(dri2_dpy
->fd
, &magic
);
1026 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1031 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1033 struct dri2_egl_display
*dri2_dpy
= data
;
1036 case WL_DRM_FORMAT_ARGB8888
:
1037 dri2_dpy
->formats
|= HAS_ARGB8888
;
1039 case WL_DRM_FORMAT_XRGB8888
:
1040 dri2_dpy
->formats
|= HAS_XRGB8888
;
1042 case WL_DRM_FORMAT_RGB565
:
1043 dri2_dpy
->formats
|= HAS_RGB565
;
1049 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1051 struct dri2_egl_display
*dri2_dpy
= data
;
1053 dri2_dpy
->capabilities
= value
;
1057 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1059 struct dri2_egl_display
*dri2_dpy
= data
;
1061 dri2_dpy
->authenticated
= true;
1064 static const struct wl_drm_listener drm_listener
= {
1065 .device
= drm_handle_device
,
1066 .format
= drm_handle_format
,
1067 .authenticated
= drm_handle_authenticated
,
1068 .capabilities
= drm_handle_capabilities
1072 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1075 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1079 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1080 uint32_t format
, uint32_t modifier_hi
,
1081 uint32_t modifier_lo
)
1083 struct dri2_egl_display
*dri2_dpy
= data
;
1084 uint64_t *mod
= NULL
;
1087 case WL_DRM_FORMAT_ARGB8888
:
1088 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1090 case WL_DRM_FORMAT_XRGB8888
:
1091 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1093 case WL_DRM_FORMAT_RGB565
:
1094 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1103 *mod
= (uint64_t) modifier_hi
<< 32;
1104 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1107 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1108 .format
= dmabuf_ignore_format
,
1109 .modifier
= dmabuf_handle_modifier
,
1113 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1114 uint32_t name
, const char *interface
,
1117 struct dri2_egl_display
*dri2_dpy
= data
;
1119 if (strcmp(interface
, "wl_drm") == 0) {
1121 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1122 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1123 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1124 dri2_dpy
->wl_dmabuf
=
1125 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1127 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1133 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1138 static const struct wl_registry_listener registry_listener_drm
= {
1139 .global
= registry_handle_global_drm
,
1140 .global_remove
= registry_handle_global_remove
1144 dri2_wl_swap_interval(_EGLDriver
*drv
,
1149 if (interval
> surf
->Config
->MaxSwapInterval
)
1150 interval
= surf
->Config
->MaxSwapInterval
;
1151 else if (interval
< surf
->Config
->MinSwapInterval
)
1152 interval
= surf
->Config
->MinSwapInterval
;
1154 surf
->SwapInterval
= interval
;
1160 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1162 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1164 /* We can't use values greater than 1 on Wayland because we are using the
1165 * frame callback to synchronise the frame and the only way we be sure to
1166 * get a frame callback is to attach a new buffer. Therefore we can't just
1167 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1169 if (dri2_dpy
->config
)
1170 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1171 "vblank_mode", &vblank_mode
);
1172 switch (vblank_mode
) {
1173 case DRI_CONF_VBLANK_NEVER
:
1174 dri2_dpy
->min_swap_interval
= 0;
1175 dri2_dpy
->max_swap_interval
= 0;
1176 dri2_dpy
->default_swap_interval
= 0;
1178 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1179 dri2_dpy
->min_swap_interval
= 1;
1180 dri2_dpy
->max_swap_interval
= 1;
1181 dri2_dpy
->default_swap_interval
= 1;
1183 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1184 dri2_dpy
->min_swap_interval
= 0;
1185 dri2_dpy
->max_swap_interval
= 1;
1186 dri2_dpy
->default_swap_interval
= 0;
1189 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1190 dri2_dpy
->min_swap_interval
= 0;
1191 dri2_dpy
->max_swap_interval
= 1;
1192 dri2_dpy
->default_swap_interval
= 1;
1197 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1198 .authenticate
= dri2_wl_authenticate
,
1199 .create_window_surface
= dri2_wl_create_window_surface
,
1200 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1201 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1202 .destroy_surface
= dri2_wl_destroy_surface
,
1203 .create_image
= dri2_create_image_khr
,
1204 .swap_interval
= dri2_wl_swap_interval
,
1205 .swap_buffers
= dri2_wl_swap_buffers
,
1206 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1207 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1208 .set_damage_region
= dri2_fallback_set_damage_region
,
1209 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1210 .copy_buffers
= dri2_fallback_copy_buffers
,
1211 .query_buffer_age
= dri2_wl_query_buffer_age
,
1212 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1213 .get_sync_values
= dri2_fallback_get_sync_values
,
1214 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1217 static const __DRIextension
*dri2_loader_extensions
[] = {
1218 &dri2_loader_extension
.base
,
1219 &image_loader_extension
.base
,
1220 &image_lookup_extension
.base
,
1221 &use_invalidate
.base
,
1225 static const __DRIextension
*image_loader_extensions
[] = {
1226 &image_loader_extension
.base
,
1227 &image_lookup_extension
.base
,
1228 &use_invalidate
.base
,
1233 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1235 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1236 static const struct {
1237 const char *format_name
;
1239 unsigned int rgba_masks
[4];
1241 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1242 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1243 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1245 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1246 unsigned int count
= 0;
1248 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1249 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1250 struct dri2_egl_config
*dri2_conf
;
1252 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1255 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1256 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1258 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1265 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1266 if (!format_count
[i
]) {
1267 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1268 visuals
[i
].format_name
);
1272 return (count
!= 0);
1276 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1278 struct dri2_egl_display
*dri2_dpy
;
1280 loader_set_logger(_eglLog
);
1282 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1284 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1287 disp
->DriverData
= (void *) dri2_dpy
;
1288 if (disp
->PlatformDisplay
== NULL
) {
1289 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1290 if (dri2_dpy
->wl_dpy
== NULL
)
1292 dri2_dpy
->own_device
= true;
1294 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1297 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1298 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1299 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1303 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1305 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1306 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1309 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1310 dri2_dpy
->wl_queue
);
1312 if (dri2_dpy
->own_device
)
1313 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1315 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1316 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1317 ®istry_listener_drm
, dri2_dpy
);
1318 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1321 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1324 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1327 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1328 &dri2_dpy
->is_different_gpu
);
1329 if (dri2_dpy
->is_different_gpu
) {
1330 free(dri2_dpy
->device_name
);
1331 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1332 if (!dri2_dpy
->device_name
) {
1333 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1334 "for requested GPU");
1339 /* we have to do the check now, because loader_get_user_preferred_fd
1340 * will return a render-node when the requested gpu is different
1341 * to the server, but also if the client asks for the same gpu than
1342 * the server by requesting its pci-id */
1343 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1345 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1346 if (dri2_dpy
->driver_name
== NULL
) {
1347 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1351 /* render nodes cannot use Gem names, and thus do not support
1352 * the __DRI_DRI2_LOADER extension */
1353 if (!dri2_dpy
->is_render_node
) {
1354 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1355 if (!dri2_load_driver(disp
)) {
1356 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1360 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1361 if (!dri2_load_driver_dri3(disp
)) {
1362 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1367 if (!dri2_create_screen(disp
))
1370 if (!dri2_setup_extensions(disp
))
1373 dri2_setup_screen(disp
);
1375 dri2_wl_setup_swap_interval(dri2_dpy
);
1377 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1378 * createImageFromFds support indicates that Prime export/import
1379 * is supported by the driver. Fall back to
1380 * gem names if we don't have Prime support. */
1382 if (dri2_dpy
->image
->base
.version
< 7 ||
1383 dri2_dpy
->image
->createImageFromFds
== NULL
)
1384 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1386 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1387 * The server needs to accept them */
1388 if (dri2_dpy
->is_render_node
&&
1389 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1390 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1394 if (dri2_dpy
->is_different_gpu
&&
1395 (dri2_dpy
->image
->base
.version
< 9 ||
1396 dri2_dpy
->image
->blitImage
== NULL
)) {
1397 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1398 "Image extension in the driver is not "
1399 "compatible. Version 9 or later and blitImage() "
1404 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1405 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1409 dri2_set_WL_bind_wayland_display(drv
, disp
);
1410 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1411 * because the buffer of the EGLImage has likely a tiling mode the server
1412 * gpu won't support. These is no way to check for now. Thus do not support the
1414 if (!dri2_dpy
->is_different_gpu
)
1415 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1417 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1419 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1421 /* Fill vtbl last to prevent accidentally calling virtual function during
1424 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1429 dri2_display_destroy(disp
);
1434 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1436 if (format
== WL_SHM_FORMAT_RGB565
)
1438 else /* ARGB8888 || XRGB8888 */
1443 * Taken from weston shared/os-compatibility.c
1446 #ifndef HAVE_MKOSTEMP
1449 set_cloexec_or_close(int fd
)
1456 flags
= fcntl(fd
, F_GETFD
);
1460 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1473 * Taken from weston shared/os-compatibility.c
1477 create_tmpfile_cloexec(char *tmpname
)
1481 #ifdef HAVE_MKOSTEMP
1482 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1486 fd
= mkstemp(tmpname
);
1488 fd
= set_cloexec_or_close(fd
);
1497 * Taken from weston shared/os-compatibility.c
1499 * Create a new, unique, anonymous file of the given size, and
1500 * return the file descriptor for it. The file descriptor is set
1501 * CLOEXEC. The file is immediately suitable for mmap()'ing
1502 * the given size at offset zero.
1504 * The file should not have a permanent backing store like a disk,
1505 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1507 * The file name is deleted from the file system.
1509 * The file is suitable for buffer sharing between processes by
1510 * transmitting the file descriptor over Unix sockets using the
1511 * SCM_RIGHTS methods.
1513 * If the C library implements posix_fallocate(), it is used to
1514 * guarantee that disk space is available for the file at the
1515 * given size. If disk space is insufficent, errno is set to ENOSPC.
1516 * If posix_fallocate() is not supported, program may receive
1517 * SIGBUS on accessing mmap()'ed file contents instead.
1520 os_create_anonymous_file(off_t size
)
1522 static const char template[] = "/mesa-shared-XXXXXX";
1528 path
= getenv("XDG_RUNTIME_DIR");
1534 name
= malloc(strlen(path
) + sizeof(template));
1539 strcat(name
, template);
1541 fd
= create_tmpfile_cloexec(name
);
1548 ret
= ftruncate(fd
, size
);
1559 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1560 int format
, int w
, int h
,
1561 void **data
, int *size
,
1562 struct wl_buffer
**buffer
)
1564 struct dri2_egl_display
*dri2_dpy
=
1565 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1566 struct wl_shm_pool
*pool
;
1567 int fd
, stride
, size_map
;
1570 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1571 size_map
= h
* stride
;
1573 /* Create a sharable buffer */
1574 fd
= os_create_anonymous_file(size_map
);
1578 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1579 if (data_map
== MAP_FAILED
) {
1584 /* Share it in a wl_buffer */
1585 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1586 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1587 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1588 wl_shm_pool_destroy(pool
);
1597 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1599 struct dri2_egl_display
*dri2_dpy
=
1600 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1602 /* we need to do the following operations only once per frame */
1603 if (dri2_surf
->back
)
1606 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1607 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1609 dri2_wl_release_buffers(dri2_surf
);
1611 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1612 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1613 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1614 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1615 dri2_surf
->current
= NULL
;
1618 /* find back buffer */
1620 /* There might be a buffer release already queued that wasn't processed */
1621 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1623 /* try get free buffer already created */
1624 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1625 if (!dri2_surf
->color_buffers
[i
].locked
&&
1626 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1627 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1632 /* else choose any another free location */
1633 if (!dri2_surf
->back
) {
1634 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1635 if (!dri2_surf
->color_buffers
[i
].locked
) {
1636 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1637 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1639 dri2_surf
->base
.Width
,
1640 dri2_surf
->base
.Height
,
1641 &dri2_surf
->back
->data
,
1642 &dri2_surf
->back
->data_size
,
1643 &dri2_surf
->back
->wl_buffer
)) {
1644 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1647 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1648 &wl_buffer_listener
, dri2_surf
);
1654 if (!dri2_surf
->back
) {
1655 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1659 dri2_surf
->back
->locked
= true;
1661 /* If we have an extra unlocked buffer at this point, we had to do triple
1662 * buffering for a while, but now can go back to just double buffering.
1663 * That means we can free any unlocked buffer now. */
1664 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1665 if (!dri2_surf
->color_buffers
[i
].locked
&&
1666 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1667 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1668 munmap(dri2_surf
->color_buffers
[i
].data
,
1669 dri2_surf
->color_buffers
[i
].data_size
);
1670 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1671 dri2_surf
->color_buffers
[i
].data
= NULL
;
1679 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1681 /* if there has been a resize: */
1682 if (!dri2_surf
->current
)
1685 return dri2_surf
->current
->data
;
1689 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1691 assert(dri2_surf
->back
);
1692 return dri2_surf
->back
->data
;
1696 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1698 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1700 while (dri2_surf
->throttle_callback
!= NULL
)
1701 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1702 dri2_surf
->wl_queue
) == -1)
1705 if (dri2_surf
->base
.SwapInterval
> 0) {
1706 dri2_surf
->throttle_callback
=
1707 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1708 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1709 &throttle_listener
, dri2_surf
);
1712 dri2_surf
->current
= dri2_surf
->back
;
1713 dri2_surf
->back
= NULL
;
1715 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1716 dri2_surf
->current
->wl_buffer
,
1717 dri2_surf
->dx
, dri2_surf
->dy
);
1719 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1720 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1721 /* reset resize growing parameters */
1725 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1726 0, 0, INT32_MAX
, INT32_MAX
);
1727 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1729 /* If we're not waiting for a frame callback then we'll at least throttle
1730 * to a sync callback so that we always give a chance for the compositor to
1731 * handle the commit and send a release event before checking for a free
1733 if (dri2_surf
->throttle_callback
== NULL
) {
1734 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1735 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1736 &throttle_listener
, dri2_surf
);
1739 wl_display_flush(dri2_dpy
->wl_dpy
);
1743 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1744 int *x
, int *y
, int *w
, int *h
,
1745 void *loaderPrivate
)
1747 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1749 (void) swrast_update_buffers(dri2_surf
);
1752 *w
= dri2_surf
->base
.Width
;
1753 *h
= dri2_surf
->base
.Height
;
1757 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1758 int x
, int y
, int w
, int h
,
1759 char *data
, void *loaderPrivate
)
1761 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1762 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1763 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1764 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1765 int dst_stride
= copy_width
;
1768 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1770 memset(data
, 0, copy_width
* h
);
1774 assert(data
!= src
);
1775 assert(copy_width
<= src_stride
);
1778 src
+= y
* src_stride
;
1781 if (copy_width
> src_stride
-x_offset
)
1782 copy_width
= src_stride
-x_offset
;
1783 if (h
> dri2_surf
->base
.Height
-y
)
1784 h
= dri2_surf
->base
.Height
-y
;
1787 memcpy(dst
, src
, copy_width
);
1794 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1795 int x
, int y
, int w
, int h
, int stride
,
1796 char *data
, void *loaderPrivate
)
1798 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1799 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1800 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1801 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1804 assert(copy_width
<= stride
);
1806 (void) swrast_update_buffers(dri2_surf
);
1807 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1809 /* partial copy, copy old content */
1810 if (copy_width
< dst_stride
)
1811 dri2_wl_swrast_get_image(draw
, 0, 0,
1812 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1813 dst
, loaderPrivate
);
1816 dst
+= y
* dst_stride
;
1820 /* drivers expect we do these checks (and some rely on it) */
1821 if (copy_width
> dst_stride
-x_offset
)
1822 copy_width
= dst_stride
-x_offset
;
1823 if (h
> dri2_surf
->base
.Height
-y
)
1824 h
= dri2_surf
->base
.Height
-y
;
1827 memcpy(dst
, src
, copy_width
);
1831 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1835 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1836 int x
, int y
, int w
, int h
,
1837 char *data
, void *loaderPrivate
)
1839 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1842 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1843 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1844 stride
, data
, loaderPrivate
);
1848 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1850 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1851 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1853 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1858 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1860 struct dri2_egl_display
*dri2_dpy
= data
;
1863 case WL_SHM_FORMAT_ARGB8888
:
1864 dri2_dpy
->formats
|= HAS_ARGB8888
;
1866 case WL_SHM_FORMAT_XRGB8888
:
1867 dri2_dpy
->formats
|= HAS_XRGB8888
;
1869 case WL_SHM_FORMAT_RGB565
:
1870 dri2_dpy
->formats
|= HAS_RGB565
;
1875 static const struct wl_shm_listener shm_listener
= {
1876 .format
= shm_handle_format
1880 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1881 uint32_t name
, const char *interface
,
1884 struct dri2_egl_display
*dri2_dpy
= data
;
1886 if (strcmp(interface
, "wl_shm") == 0) {
1888 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1889 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1893 static const struct wl_registry_listener registry_listener_swrast
= {
1894 .global
= registry_handle_global_swrast
,
1895 .global_remove
= registry_handle_global_remove
1898 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1899 .authenticate
= NULL
,
1900 .create_window_surface
= dri2_wl_create_window_surface
,
1901 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1902 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1903 .destroy_surface
= dri2_wl_destroy_surface
,
1904 .create_image
= dri2_fallback_create_image_khr
,
1905 .swap_interval
= dri2_wl_swap_interval
,
1906 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1907 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1908 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1909 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1910 .copy_buffers
= dri2_fallback_copy_buffers
,
1911 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1912 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1913 .get_sync_values
= dri2_fallback_get_sync_values
,
1914 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1917 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1918 .base
= { __DRI_SWRAST_LOADER
, 2 },
1920 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1921 .putImage
= dri2_wl_swrast_put_image
,
1922 .getImage
= dri2_wl_swrast_get_image
,
1923 .putImage2
= dri2_wl_swrast_put_image2
,
1926 static const __DRIextension
*swrast_loader_extensions
[] = {
1927 &swrast_loader_extension
.base
,
1932 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1934 struct dri2_egl_display
*dri2_dpy
;
1936 loader_set_logger(_eglLog
);
1938 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1940 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1943 disp
->DriverData
= (void *) dri2_dpy
;
1944 if (disp
->PlatformDisplay
== NULL
) {
1945 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1946 if (dri2_dpy
->wl_dpy
== NULL
)
1948 dri2_dpy
->own_device
= true;
1950 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1953 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1955 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1956 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1959 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1960 dri2_dpy
->wl_queue
);
1962 if (dri2_dpy
->own_device
)
1963 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1965 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1966 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1967 ®istry_listener_swrast
, dri2_dpy
);
1969 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1972 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1975 dri2_dpy
->driver_name
= strdup("swrast");
1976 if (!dri2_load_driver_swrast(disp
))
1979 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1981 if (!dri2_create_screen(disp
))
1984 if (!dri2_setup_extensions(disp
))
1987 dri2_setup_screen(disp
);
1989 dri2_wl_setup_swap_interval(dri2_dpy
);
1991 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1992 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1996 /* Fill vtbl last to prevent accidentally calling virtual function during
1999 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2004 dri2_display_destroy(disp
);
2009 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2011 EGLBoolean initialized
= EGL_TRUE
;
2013 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
2016 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
2017 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2020 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);