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>
42 #include "egl_dri2_fallbacks.h"
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
48 enum wl_drm_format_flags
{
55 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
59 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
64 wl_callback_destroy(callback
);
67 static const struct wl_callback_listener sync_listener
= {
72 roundtrip(struct dri2_egl_display
*dri2_dpy
)
74 struct wl_callback
*callback
;
75 int done
= 0, ret
= 0;
77 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
78 wl_callback_add_listener(callback
, &sync_listener
, &done
);
79 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
80 while (ret
!= -1 && !done
)
81 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
84 wl_callback_destroy(callback
);
90 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
92 struct dri2_egl_surface
*dri2_surf
= data
;
95 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
96 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
99 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
100 wl_buffer_destroy(buffer
);
104 dri2_surf
->color_buffers
[i
].locked
= 0;
107 static const struct wl_buffer_listener wl_buffer_listener
= {
108 .release
= wl_buffer_release
112 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
114 struct dri2_egl_surface
*dri2_surf
= data
;
115 struct dri2_egl_display
*dri2_dpy
=
116 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
118 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
122 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
125 dri2_wl_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
126 _EGLConfig
*conf
, void *native_window
,
127 const EGLint
*attrib_list
)
129 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
130 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
131 struct wl_egl_window
*window
= native_window
;
132 struct dri2_egl_surface
*dri2_surf
;
133 const __DRIconfig
*config
;
137 dri2_surf
= calloc(1, sizeof *dri2_surf
);
139 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
143 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
146 if (conf
->RedSize
== 5)
147 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
148 else if (conf
->AlphaSize
== 0)
149 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
151 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
154 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
158 dri2_surf
->wl_win
= window
;
160 dri2_surf
->wl_win
->private = dri2_surf
;
161 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
163 dri2_surf
->base
.Width
= -1;
164 dri2_surf
->base
.Height
= -1;
166 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
167 dri2_surf
->base
.GLColorspace
);
169 dri2_surf
->dri_drawable
=
170 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
172 if (dri2_surf
->dri_drawable
== NULL
) {
173 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
177 return &dri2_surf
->base
;
186 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
189 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
190 _EGLConfig
*conf
, void *native_window
,
191 const EGLint
*attrib_list
)
193 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
196 surf
= dri2_wl_create_surface(drv
, disp
, conf
, native_window
, attrib_list
);
199 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
205 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
206 _EGLConfig
*conf
, void *native_window
,
207 const EGLint
*attrib_list
)
209 /* From the EGL_EXT_platform_wayland spec, version 3:
211 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
212 * that belongs to Wayland. Any such call fails and generates
215 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
221 * Called via eglDestroySurface(), drv->API.DestroySurface().
224 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
226 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
227 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
232 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
234 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
235 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
236 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
237 if (dri2_surf
->color_buffers
[i
].dri_image
)
238 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
239 if (dri2_surf
->color_buffers
[i
].linear_copy
)
240 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
241 if (dri2_surf
->color_buffers
[i
].data
)
242 munmap(dri2_surf
->color_buffers
[i
].data
,
243 dri2_surf
->color_buffers
[i
].data_size
);
246 if (dri2_dpy
->dri2
) {
247 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
248 if (dri2_surf
->dri_buffers
[i
] &&
249 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
250 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
251 dri2_surf
->dri_buffers
[i
]);
254 if (dri2_surf
->throttle_callback
)
255 wl_callback_destroy(dri2_surf
->throttle_callback
);
257 dri2_surf
->wl_win
->private = NULL
;
258 dri2_surf
->wl_win
->resize_callback
= NULL
;
266 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
268 struct dri2_egl_display
*dri2_dpy
=
269 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
272 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
273 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
274 !dri2_surf
->color_buffers
[i
].locked
)
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
);
284 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
285 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
286 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
287 dri2_surf
->color_buffers
[i
].data
= NULL
;
288 dri2_surf
->color_buffers
[i
].locked
= 0;
291 if (dri2_dpy
->dri2
) {
292 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
293 if (dri2_surf
->dri_buffers
[i
] &&
294 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
295 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
296 dri2_surf
->dri_buffers
[i
]);
301 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
303 struct dri2_egl_display
*dri2_dpy
=
304 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
306 unsigned int dri_image_format
;
308 /* currently supports three WL DRM formats,
309 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
310 * and WL_DRM_FORMAT_RGB565
312 switch (dri2_surf
->format
) {
313 case WL_DRM_FORMAT_ARGB8888
:
314 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
316 case WL_DRM_FORMAT_XRGB8888
:
317 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
319 case WL_DRM_FORMAT_RGB565
:
320 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
323 /* format is not supported */
327 /* We always want to throttle to some event (either a frame callback or
328 * a sync request) after the commit so that we can be sure the
329 * compositor has had a chance to handle it and send us a release event
330 * before we look for a free buffer */
331 while (dri2_surf
->throttle_callback
!= NULL
)
332 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
333 dri2_dpy
->wl_queue
) == -1)
336 if (dri2_surf
->back
== NULL
) {
337 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
338 /* Get an unlocked buffer, preferrably one with a dri_buffer
339 * already allocated. */
340 if (dri2_surf
->color_buffers
[i
].locked
)
342 if (dri2_surf
->back
== NULL
)
343 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
344 else if (dri2_surf
->back
->dri_image
== NULL
)
345 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
349 if (dri2_surf
->back
== NULL
)
352 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
354 if (dri2_dpy
->is_different_gpu
&&
355 dri2_surf
->back
->linear_copy
== NULL
) {
356 dri2_surf
->back
->linear_copy
=
357 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
358 dri2_surf
->base
.Width
,
359 dri2_surf
->base
.Height
,
362 __DRI_IMAGE_USE_LINEAR
,
364 if (dri2_surf
->back
->linear_copy
== NULL
)
368 if (dri2_surf
->back
->dri_image
== NULL
) {
369 dri2_surf
->back
->dri_image
=
370 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
371 dri2_surf
->base
.Width
,
372 dri2_surf
->base
.Height
,
374 dri2_dpy
->is_different_gpu
?
377 dri2_surf
->back
->age
= 0;
379 if (dri2_surf
->back
->dri_image
== NULL
)
382 dri2_surf
->back
->locked
= 1;
389 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
391 struct dri2_egl_display
*dri2_dpy
=
392 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
396 image
= dri2_surf
->back
->dri_image
;
398 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
399 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
401 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
403 buffer
->pitch
= pitch
;
409 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
410 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
412 struct dri2_egl_display
*dri2_dpy
=
413 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
414 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
417 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
419 dri2_surf
->base
.Width
,
420 dri2_surf
->base
.Height
);
421 dri2_surf
->dri_buffers
[attachment
] = b
;
426 memcpy(buffer
, b
, sizeof *buffer
);
432 update_buffers(struct dri2_egl_surface
*dri2_surf
)
434 struct dri2_egl_display
*dri2_dpy
=
435 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
438 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
439 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
441 dri2_wl_release_buffers(dri2_surf
);
443 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
444 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
445 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
446 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
449 if (get_back_bo(dri2_surf
) < 0) {
450 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
454 /* If we have an extra unlocked buffer at this point, we had to do triple
455 * buffering for a while, but now can go back to just double buffering.
456 * That means we can free any unlocked buffer now. */
457 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
458 if (!dri2_surf
->color_buffers
[i
].locked
&&
459 dri2_surf
->color_buffers
[i
].wl_buffer
) {
460 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
461 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
462 if (dri2_dpy
->is_different_gpu
)
463 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
464 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
465 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
466 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
474 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
475 int *width
, int *height
,
476 unsigned int *attachments
, int count
,
477 int *out_count
, void *loaderPrivate
)
479 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
482 if (update_buffers(dri2_surf
) < 0)
485 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
486 switch (attachments
[i
]) {
487 case __DRI_BUFFER_BACK_LEFT
:
488 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
491 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
492 &dri2_surf
->buffers
[j
]) < 0) {
493 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
504 *width
= dri2_surf
->base
.Width
;
505 *height
= dri2_surf
->base
.Height
;
507 return dri2_surf
->buffers
;
511 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
512 int *width
, int *height
,
513 unsigned int *attachments
, int count
,
514 int *out_count
, void *loaderPrivate
)
516 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
517 unsigned int *attachments_with_format
;
523 switch (dri2_surf
->format
) {
524 case WL_DRM_FORMAT_ARGB8888
:
525 case WL_DRM_FORMAT_XRGB8888
:
528 case WL_DRM_FORMAT_RGB565
:
532 /* format is not supported */
536 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
537 if (!attachments_with_format
) {
542 for (i
= 0; i
< count
; ++i
) {
543 attachments_with_format
[2*i
] = attachments
[i
];
544 attachments_with_format
[2*i
+ 1] = bpp
;
548 dri2_wl_get_buffers_with_format(driDrawable
,
550 attachments_with_format
, count
,
551 out_count
, loaderPrivate
);
553 free(attachments_with_format
);
559 image_get_buffers(__DRIdrawable
*driDrawable
,
563 uint32_t buffer_mask
,
564 struct __DRIimageList
*buffers
)
566 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
568 if (update_buffers(dri2_surf
) < 0)
571 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
572 buffers
->back
= dri2_surf
->back
->dri_image
;
578 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
581 (void) loaderPrivate
;
584 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
585 .base
= { __DRI_DRI2_LOADER
, 3 },
587 .getBuffers
= dri2_wl_get_buffers
,
588 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
589 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
592 static const __DRIimageLoaderExtension image_loader_extension
= {
593 .base
= { __DRI_IMAGE_LOADER
, 1 },
595 .getBuffers
= image_get_buffers
,
596 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
600 wayland_throttle_callback(void *data
,
601 struct wl_callback
*callback
,
604 struct dri2_egl_surface
*dri2_surf
= data
;
606 dri2_surf
->throttle_callback
= NULL
;
607 wl_callback_destroy(callback
);
610 static const struct wl_callback_listener throttle_listener
= {
611 .done
= wayland_throttle_callback
615 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
617 struct dri2_egl_display
*dri2_dpy
=
618 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
620 int fd
, stride
, name
;
622 if (dri2_surf
->current
->wl_buffer
!= NULL
)
625 if (dri2_dpy
->is_different_gpu
) {
626 image
= dri2_surf
->current
->linear_copy
;
628 image
= dri2_surf
->current
->dri_image
;
630 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
631 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
632 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
634 dri2_surf
->current
->wl_buffer
=
635 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
637 dri2_surf
->base
.Width
,
638 dri2_surf
->base
.Height
,
645 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
646 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
648 dri2_surf
->current
->wl_buffer
=
649 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
651 dri2_surf
->base
.Width
,
652 dri2_surf
->base
.Height
,
657 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
659 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
660 &wl_buffer_listener
, dri2_surf
);
664 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
668 /* The WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION macro and
669 * wl_proxy_get_version() were both introduced in wayland 1.10.
670 * Instead of bumping our wayland dependency we just make this
671 * function conditional on the required 1.10 features, falling
672 * back to old (correct but suboptimal) behaviour for older
675 #ifdef WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
678 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_win
->surface
)
679 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
682 for (i
= 0; i
< n_rects
; i
++) {
683 const int *rect
= &rects
[i
* 4];
685 wl_surface_damage_buffer(dri2_surf
->wl_win
->surface
,
687 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
695 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
698 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
704 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
705 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
708 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
709 if (dri2_surf
->color_buffers
[i
].age
> 0)
710 dri2_surf
->color_buffers
[i
].age
++;
712 /* Make sure we have a back buffer in case we're swapping without ever
714 if (get_back_bo(dri2_surf
) < 0) {
715 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
719 if (draw
->SwapInterval
> 0) {
720 dri2_surf
->throttle_callback
=
721 wl_surface_frame(dri2_surf
->wl_win
->surface
);
722 wl_callback_add_listener(dri2_surf
->throttle_callback
,
723 &throttle_listener
, dri2_surf
);
724 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
728 dri2_surf
->back
->age
= 1;
729 dri2_surf
->current
= dri2_surf
->back
;
730 dri2_surf
->back
= NULL
;
732 create_wl_buffer(dri2_surf
);
734 wl_surface_attach(dri2_surf
->wl_win
->surface
,
735 dri2_surf
->current
->wl_buffer
,
736 dri2_surf
->dx
, dri2_surf
->dy
);
738 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
739 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
740 /* reset resize growing parameters */
744 /* If the compositor doesn't support damage_buffer, we deliberately
745 * ignore the damage region and post maximum damage, due to
746 * https://bugs.freedesktop.org/78190 */
747 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
748 wl_surface_damage(dri2_surf
->wl_win
->surface
,
749 0, 0, INT32_MAX
, INT32_MAX
);
751 if (dri2_dpy
->is_different_gpu
) {
752 _EGLContext
*ctx
= _eglGetCurrentContext();
753 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
754 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
755 dri2_surf
->current
->linear_copy
,
756 dri2_surf
->current
->dri_image
,
757 0, 0, dri2_surf
->base
.Width
,
758 dri2_surf
->base
.Height
,
759 0, 0, dri2_surf
->base
.Width
,
760 dri2_surf
->base
.Height
, 0);
763 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
764 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
766 wl_surface_commit(dri2_surf
->wl_win
->surface
);
768 /* If we're not waiting for a frame callback then we'll at least throttle
769 * to a sync callback so that we always give a chance for the compositor to
770 * handle the commit and send a release event before checking for a free
772 if (dri2_surf
->throttle_callback
== NULL
) {
773 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
774 wl_callback_add_listener(dri2_surf
->throttle_callback
,
775 &throttle_listener
, dri2_surf
);
776 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
780 wl_display_flush(dri2_dpy
->wl_dpy
);
786 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
787 _EGLDisplay
*disp
, _EGLSurface
*surface
)
789 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
791 if (get_back_bo(dri2_surf
) < 0) {
792 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
796 return dri2_surf
->back
->age
;
800 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
802 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
805 static struct wl_buffer
*
806 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
810 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
811 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
812 __DRIimage
*image
= dri2_img
->dri_image
;
813 struct wl_buffer
*buffer
;
814 int width
, height
, format
, pitch
;
815 enum wl_drm_format wl_format
;
817 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
820 case __DRI_IMAGE_FORMAT_ARGB8888
:
821 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
823 wl_format
= WL_DRM_FORMAT_ARGB8888
;
825 case __DRI_IMAGE_FORMAT_XRGB8888
:
826 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
828 wl_format
= WL_DRM_FORMAT_XRGB8888
;
834 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
835 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
836 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
838 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
841 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
844 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
856 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
859 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
866 /* The buffer object will have been created with our internal event queue
867 * because it is using the wl_drm object as a proxy factory. We want the
868 * buffer to be used by the application so we'll reset it to the display's
869 * default event queue */
871 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
876 _eglError(EGL_BAD_MATCH
, "unsupported image format");
881 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
883 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
886 if (dri2_dpy
->is_render_node
) {
887 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
888 "authenticate for render-nodes");
891 dri2_dpy
->authenticated
= 0;
893 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
894 if (roundtrip(dri2_dpy
) < 0)
897 if (!dri2_dpy
->authenticated
)
900 /* reset authenticated */
901 dri2_dpy
->authenticated
= 1;
907 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
909 struct dri2_egl_display
*dri2_dpy
= data
;
912 dri2_dpy
->device_name
= strdup(device
);
913 if (!dri2_dpy
->device_name
)
916 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
917 if (dri2_dpy
->fd
== -1) {
918 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
919 dri2_dpy
->device_name
, strerror(errno
));
923 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
924 dri2_dpy
->authenticated
= 1;
926 drmGetMagic(dri2_dpy
->fd
, &magic
);
927 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
932 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
934 struct dri2_egl_display
*dri2_dpy
= data
;
937 case WL_DRM_FORMAT_ARGB8888
:
938 dri2_dpy
->formats
|= HAS_ARGB8888
;
940 case WL_DRM_FORMAT_XRGB8888
:
941 dri2_dpy
->formats
|= HAS_XRGB8888
;
943 case WL_DRM_FORMAT_RGB565
:
944 dri2_dpy
->formats
|= HAS_RGB565
;
950 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
952 struct dri2_egl_display
*dri2_dpy
= data
;
954 dri2_dpy
->capabilities
= value
;
958 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
960 struct dri2_egl_display
*dri2_dpy
= data
;
962 dri2_dpy
->authenticated
= 1;
965 static const struct wl_drm_listener drm_listener
= {
966 .device
= drm_handle_device
,
967 .format
= drm_handle_format
,
968 .authenticated
= drm_handle_authenticated
,
969 .capabilities
= drm_handle_capabilities
973 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
974 const char *interface
, uint32_t version
)
976 struct dri2_egl_display
*dri2_dpy
= data
;
980 if (strcmp(interface
, "wl_drm") == 0) {
982 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
983 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
988 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
993 static const struct wl_registry_listener registry_listener_drm
= {
994 .global
= registry_handle_global_drm
,
995 .global_remove
= registry_handle_global_remove
999 dri2_wl_swap_interval(_EGLDriver
*drv
,
1004 if (interval
> surf
->Config
->MaxSwapInterval
)
1005 interval
= surf
->Config
->MaxSwapInterval
;
1006 else if (interval
< surf
->Config
->MinSwapInterval
)
1007 interval
= surf
->Config
->MinSwapInterval
;
1009 surf
->SwapInterval
= interval
;
1015 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1017 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1019 /* We can't use values greater than 1 on Wayland because we are using the
1020 * frame callback to synchronise the frame and the only way we be sure to
1021 * get a frame callback is to attach a new buffer. Therefore we can't just
1022 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1024 if (dri2_dpy
->config
)
1025 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1026 "vblank_mode", &vblank_mode
);
1027 switch (vblank_mode
) {
1028 case DRI_CONF_VBLANK_NEVER
:
1029 dri2_dpy
->min_swap_interval
= 0;
1030 dri2_dpy
->max_swap_interval
= 0;
1031 dri2_dpy
->default_swap_interval
= 0;
1033 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1034 dri2_dpy
->min_swap_interval
= 1;
1035 dri2_dpy
->max_swap_interval
= 1;
1036 dri2_dpy
->default_swap_interval
= 1;
1038 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1039 dri2_dpy
->min_swap_interval
= 0;
1040 dri2_dpy
->max_swap_interval
= 1;
1041 dri2_dpy
->default_swap_interval
= 0;
1044 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1045 dri2_dpy
->min_swap_interval
= 0;
1046 dri2_dpy
->max_swap_interval
= 1;
1047 dri2_dpy
->default_swap_interval
= 1;
1052 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1053 .authenticate
= dri2_wl_authenticate
,
1054 .create_window_surface
= dri2_wl_create_window_surface
,
1055 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1056 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1057 .destroy_surface
= dri2_wl_destroy_surface
,
1058 .create_image
= dri2_create_image_khr
,
1059 .swap_interval
= dri2_wl_swap_interval
,
1060 .swap_buffers
= dri2_wl_swap_buffers
,
1061 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1062 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1063 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1064 .copy_buffers
= dri2_fallback_copy_buffers
,
1065 .query_buffer_age
= dri2_wl_query_buffer_age
,
1066 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1067 .get_sync_values
= dri2_fallback_get_sync_values
,
1068 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1071 static const __DRIextension
*dri2_loader_extensions
[] = {
1072 &dri2_loader_extension
.base
,
1073 &image_lookup_extension
.base
,
1074 &use_invalidate
.base
,
1078 static const __DRIextension
*image_loader_extensions
[] = {
1079 &image_loader_extension
.base
,
1080 &image_lookup_extension
.base
,
1081 &use_invalidate
.base
,
1086 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1088 struct dri2_egl_display
*dri2_dpy
;
1089 const __DRIconfig
*config
;
1092 static const unsigned int argb_masks
[4] =
1093 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1094 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1095 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1097 loader_set_logger(_eglLog
);
1099 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1101 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1103 disp
->DriverData
= (void *) dri2_dpy
;
1104 if (disp
->PlatformDisplay
== NULL
) {
1105 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1106 if (dri2_dpy
->wl_dpy
== NULL
)
1108 dri2_dpy
->own_device
= 1;
1110 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1113 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1115 if (dri2_dpy
->own_device
)
1116 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1118 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1119 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1120 dri2_dpy
->wl_queue
);
1121 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1122 ®istry_listener_drm
, dri2_dpy
);
1123 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1124 goto cleanup_registry
;
1126 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1129 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1132 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1133 &dri2_dpy
->is_different_gpu
);
1134 if (dri2_dpy
->is_different_gpu
) {
1135 free(dri2_dpy
->device_name
);
1136 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1137 if (!dri2_dpy
->device_name
) {
1138 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1139 "for requested GPU");
1144 /* we have to do the check now, because loader_get_user_preferred_fd
1145 * will return a render-node when the requested gpu is different
1146 * to the server, but also if the client asks for the same gpu than
1147 * the server by requesting its pci-id */
1148 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1150 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1151 if (dri2_dpy
->driver_name
== NULL
) {
1152 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1156 if (!dri2_load_driver(disp
))
1157 goto cleanup_driver_name
;
1159 /* render nodes cannot use Gem names, and thus do not support
1160 * the __DRI_DRI2_LOADER extension */
1161 if (!dri2_dpy
->is_render_node
)
1162 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1164 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1166 if (!dri2_create_screen(disp
))
1167 goto cleanup_driver
;
1169 dri2_wl_setup_swap_interval(dri2_dpy
);
1171 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1172 * createImageFromFds support indicates that Prime export/import
1173 * is supported by the driver. Fall back to
1174 * gem names if we don't have Prime support. */
1176 if (dri2_dpy
->image
->base
.version
< 7 ||
1177 dri2_dpy
->image
->createImageFromFds
== NULL
)
1178 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1180 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1181 * The server needs to accept them */
1182 if (dri2_dpy
->is_render_node
&&
1183 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1184 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1185 goto cleanup_screen
;
1188 if (dri2_dpy
->is_different_gpu
&&
1189 (dri2_dpy
->image
->base
.version
< 9 ||
1190 dri2_dpy
->image
->blitImage
== NULL
)) {
1191 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1192 "Image extension in the driver is not "
1193 "compatible. Version 9 or later and blitImage() "
1195 goto cleanup_screen
;
1198 types
= EGL_WINDOW_BIT
;
1199 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1200 config
= dri2_dpy
->driver_configs
[i
];
1201 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1202 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1203 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1204 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1205 if (dri2_dpy
->formats
& HAS_RGB565
)
1206 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1209 dri2_set_WL_bind_wayland_display(drv
, disp
);
1210 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1211 * because the buffer of the EGLImage has likely a tiling mode the server
1212 * gpu won't support. These is no way to check for now. Thus do not support the
1214 if (!dri2_dpy
->is_different_gpu
) {
1215 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1217 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1218 dri2_fallback_create_wayland_buffer_from_image
;
1220 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1222 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1224 /* Fill vtbl last to prevent accidentally calling virtual function during
1227 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1232 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1234 dlclose(dri2_dpy
->driver
);
1235 cleanup_driver_name
:
1236 free(dri2_dpy
->driver_name
);
1238 close(dri2_dpy
->fd
);
1240 free(dri2_dpy
->device_name
);
1241 wl_drm_destroy(dri2_dpy
->wl_drm
);
1243 wl_registry_destroy(dri2_dpy
->wl_registry
);
1244 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1247 disp
->DriverData
= NULL
;
1253 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1255 if (format
== WL_SHM_FORMAT_RGB565
)
1257 else /* ARGB8888 || XRGB8888 */
1262 * Taken from weston shared/os-compatibility.c
1265 #ifndef HAVE_MKOSTEMP
1268 set_cloexec_or_close(int fd
)
1275 flags
= fcntl(fd
, F_GETFD
);
1279 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1292 * Taken from weston shared/os-compatibility.c
1296 create_tmpfile_cloexec(char *tmpname
)
1300 #ifdef HAVE_MKOSTEMP
1301 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1305 fd
= mkstemp(tmpname
);
1307 fd
= set_cloexec_or_close(fd
);
1316 * Taken from weston shared/os-compatibility.c
1318 * Create a new, unique, anonymous file of the given size, and
1319 * return the file descriptor for it. The file descriptor is set
1320 * CLOEXEC. The file is immediately suitable for mmap()'ing
1321 * the given size at offset zero.
1323 * The file should not have a permanent backing store like a disk,
1324 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1326 * The file name is deleted from the file system.
1328 * The file is suitable for buffer sharing between processes by
1329 * transmitting the file descriptor over Unix sockets using the
1330 * SCM_RIGHTS methods.
1332 * If the C library implements posix_fallocate(), it is used to
1333 * guarantee that disk space is available for the file at the
1334 * given size. If disk space is insufficent, errno is set to ENOSPC.
1335 * If posix_fallocate() is not supported, program may receive
1336 * SIGBUS on accessing mmap()'ed file contents instead.
1339 os_create_anonymous_file(off_t size
)
1341 static const char template[] = "/mesa-shared-XXXXXX";
1347 path
= getenv("XDG_RUNTIME_DIR");
1353 name
= malloc(strlen(path
) + sizeof(template));
1358 strcat(name
, template);
1360 fd
= create_tmpfile_cloexec(name
);
1367 ret
= ftruncate(fd
, size
);
1378 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1379 int format
, int w
, int h
,
1380 void **data
, int *size
,
1381 struct wl_buffer
**buffer
)
1383 struct wl_shm_pool
*pool
;
1384 int fd
, stride
, size_map
;
1387 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1388 size_map
= h
* stride
;
1390 /* Create a sharable buffer */
1391 fd
= os_create_anonymous_file(size_map
);
1395 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1396 if (data_map
== MAP_FAILED
) {
1401 /* Share it in a wl_buffer */
1402 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1403 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1404 wl_shm_pool_destroy(pool
);
1413 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1415 struct dri2_egl_display
*dri2_dpy
=
1416 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1419 /* we need to do the following operations only once per frame */
1420 if (dri2_surf
->back
)
1423 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1424 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1426 dri2_wl_release_buffers(dri2_surf
);
1428 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1429 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1430 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1431 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1432 dri2_surf
->current
= NULL
;
1435 /* find back buffer */
1437 /* We always want to throttle to some event (either a frame callback or
1438 * a sync request) after the commit so that we can be sure the
1439 * compositor has had a chance to handle it and send us a release event
1440 * before we look for a free buffer */
1441 while (dri2_surf
->throttle_callback
!= NULL
)
1442 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1443 dri2_dpy
->wl_queue
) == -1)
1446 /* try get free buffer already created */
1447 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1448 if (!dri2_surf
->color_buffers
[i
].locked
&&
1449 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1450 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1455 /* else choose any another free location */
1456 if (!dri2_surf
->back
) {
1457 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1458 if (!dri2_surf
->color_buffers
[i
].locked
) {
1459 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1460 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1462 dri2_surf
->base
.Width
,
1463 dri2_surf
->base
.Height
,
1464 &dri2_surf
->back
->data
,
1465 &dri2_surf
->back
->data_size
,
1466 &dri2_surf
->back
->wl_buffer
)) {
1467 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1470 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1471 dri2_dpy
->wl_queue
);
1472 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1473 &wl_buffer_listener
, dri2_surf
);
1479 if (!dri2_surf
->back
) {
1480 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1484 dri2_surf
->back
->locked
= 1;
1486 /* If we have an extra unlocked buffer at this point, we had to do triple
1487 * buffering for a while, but now can go back to just double buffering.
1488 * That means we can free any unlocked buffer now. */
1489 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1490 if (!dri2_surf
->color_buffers
[i
].locked
&&
1491 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1492 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1493 munmap(dri2_surf
->color_buffers
[i
].data
,
1494 dri2_surf
->color_buffers
[i
].data_size
);
1495 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1496 dri2_surf
->color_buffers
[i
].data
= NULL
;
1504 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1506 /* if there has been a resize: */
1507 if (!dri2_surf
->current
)
1510 return dri2_surf
->current
->data
;
1514 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1516 assert(dri2_surf
->back
);
1517 return dri2_surf
->back
->data
;
1521 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1523 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1525 if (dri2_surf
->base
.SwapInterval
> 0) {
1526 dri2_surf
->throttle_callback
=
1527 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1528 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1529 &throttle_listener
, dri2_surf
);
1530 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1531 dri2_dpy
->wl_queue
);
1534 dri2_surf
->current
= dri2_surf
->back
;
1535 dri2_surf
->back
= NULL
;
1537 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1538 dri2_surf
->current
->wl_buffer
,
1539 dri2_surf
->dx
, dri2_surf
->dy
);
1541 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1542 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1543 /* reset resize growing parameters */
1547 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1548 0, 0, INT32_MAX
, INT32_MAX
);
1549 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1551 /* If we're not waiting for a frame callback then we'll at least throttle
1552 * to a sync callback so that we always give a chance for the compositor to
1553 * handle the commit and send a release event before checking for a free
1555 if (dri2_surf
->throttle_callback
== NULL
) {
1556 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
1557 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1558 &throttle_listener
, dri2_surf
);
1559 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1560 dri2_dpy
->wl_queue
);
1563 wl_display_flush(dri2_dpy
->wl_dpy
);
1567 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1568 int *x
, int *y
, int *w
, int *h
,
1569 void *loaderPrivate
)
1571 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1573 (void) swrast_update_buffers(dri2_surf
);
1576 *w
= dri2_surf
->base
.Width
;
1577 *h
= dri2_surf
->base
.Height
;
1581 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1582 int x
, int y
, int w
, int h
,
1583 char *data
, void *loaderPrivate
)
1585 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1586 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1587 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1588 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1589 int dst_stride
= copy_width
;
1592 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1594 memset(data
, 0, copy_width
* h
);
1598 assert(data
!= src
);
1599 assert(copy_width
<= src_stride
);
1602 src
+= y
* src_stride
;
1605 if (copy_width
> src_stride
-x_offset
)
1606 copy_width
= src_stride
-x_offset
;
1607 if (h
> dri2_surf
->base
.Height
-y
)
1608 h
= dri2_surf
->base
.Height
-y
;
1611 memcpy(dst
, src
, copy_width
);
1618 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1619 int x
, int y
, int w
, int h
, int stride
,
1620 char *data
, void *loaderPrivate
)
1622 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1623 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1624 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1625 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1628 assert(copy_width
<= stride
);
1630 (void) swrast_update_buffers(dri2_surf
);
1631 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1633 /* partial copy, copy old content */
1634 if (copy_width
< dst_stride
)
1635 dri2_wl_swrast_get_image(draw
, 0, 0,
1636 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1637 dst
, loaderPrivate
);
1640 dst
+= y
* dst_stride
;
1644 /* drivers expect we do these checks (and some rely on it) */
1645 if (copy_width
> dst_stride
-x_offset
)
1646 copy_width
= dst_stride
-x_offset
;
1647 if (h
> dri2_surf
->base
.Height
-y
)
1648 h
= dri2_surf
->base
.Height
-y
;
1651 memcpy(dst
, src
, copy_width
);
1655 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1659 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1660 int x
, int y
, int w
, int h
,
1661 char *data
, void *loaderPrivate
)
1663 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1666 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1667 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1668 stride
, data
, loaderPrivate
);
1672 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1674 static _EGLSurface
*
1675 dri2_wl_swrast_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1676 _EGLConfig
*conf
, void *native_window
,
1677 const EGLint
*attrib_list
)
1679 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1680 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1681 struct wl_egl_window
*window
= native_window
;
1682 struct dri2_egl_surface
*dri2_surf
;
1683 const __DRIconfig
*config
;
1687 dri2_surf
= calloc(1, sizeof *dri2_surf
);
1689 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1693 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
1696 if (conf
->RedSize
== 5)
1697 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
1698 else if (conf
->AlphaSize
== 0)
1699 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
1701 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
1703 dri2_surf
->wl_win
= window
;
1705 dri2_surf
->base
.Width
= -1;
1706 dri2_surf
->base
.Height
= -1;
1708 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
1709 dri2_surf
->base
.GLColorspace
);
1711 dri2_surf
->dri_drawable
=
1712 (*dri2_dpy
->swrast
->createNewDrawable
)(dri2_dpy
->dri_screen
,
1714 if (dri2_surf
->dri_drawable
== NULL
) {
1715 _eglError(EGL_BAD_ALLOC
, "swrast->createNewDrawable");
1716 goto cleanup_dri_drawable
;
1719 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
1720 dri2_dpy
->default_swap_interval
);
1722 return &dri2_surf
->base
;
1724 cleanup_dri_drawable
:
1725 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1733 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1735 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1736 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1738 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1743 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1745 struct dri2_egl_display
*dri2_dpy
= data
;
1748 case WL_SHM_FORMAT_ARGB8888
:
1749 dri2_dpy
->formats
|= HAS_ARGB8888
;
1751 case WL_SHM_FORMAT_XRGB8888
:
1752 dri2_dpy
->formats
|= HAS_XRGB8888
;
1754 case WL_SHM_FORMAT_RGB565
:
1755 dri2_dpy
->formats
|= HAS_RGB565
;
1760 static const struct wl_shm_listener shm_listener
= {
1761 .format
= shm_handle_format
1765 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1766 const char *interface
, uint32_t version
)
1768 struct dri2_egl_display
*dri2_dpy
= data
;
1770 if (strcmp(interface
, "wl_shm") == 0) {
1772 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1773 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1777 static const struct wl_registry_listener registry_listener_swrast
= {
1778 .global
= registry_handle_global_swrast
,
1779 .global_remove
= registry_handle_global_remove
1782 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1783 .authenticate
= NULL
,
1784 .create_window_surface
= dri2_wl_swrast_create_window_surface
,
1785 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1786 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1787 .destroy_surface
= dri2_wl_destroy_surface
,
1788 .create_image
= dri2_fallback_create_image_khr
,
1789 .swap_interval
= dri2_wl_swap_interval
,
1790 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1791 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1792 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1793 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1794 .copy_buffers
= dri2_fallback_copy_buffers
,
1795 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1796 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1797 .get_sync_values
= dri2_fallback_get_sync_values
,
1798 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1801 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1802 .base
= { __DRI_SWRAST_LOADER
, 2 },
1804 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1805 .putImage
= dri2_wl_swrast_put_image
,
1806 .getImage
= dri2_wl_swrast_get_image
,
1807 .putImage2
= dri2_wl_swrast_put_image2
,
1810 static const __DRIextension
*swrast_loader_extensions
[] = {
1811 &swrast_loader_extension
.base
,
1816 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1818 struct dri2_egl_display
*dri2_dpy
;
1819 const __DRIconfig
*config
;
1822 static const unsigned int argb_masks
[4] =
1823 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1824 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1825 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1827 loader_set_logger(_eglLog
);
1829 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1831 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1833 disp
->DriverData
= (void *) dri2_dpy
;
1834 if (disp
->PlatformDisplay
== NULL
) {
1835 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1836 if (dri2_dpy
->wl_dpy
== NULL
)
1838 dri2_dpy
->own_device
= 1;
1840 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1843 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1845 if (dri2_dpy
->own_device
)
1846 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1848 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1849 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1850 dri2_dpy
->wl_queue
);
1851 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1852 ®istry_listener_swrast
, dri2_dpy
);
1854 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1855 goto cleanup_registry
;
1857 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1861 dri2_dpy
->driver_name
= strdup("swrast");
1862 if (!dri2_load_driver_swrast(disp
))
1865 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1867 if (!dri2_create_screen(disp
))
1868 goto cleanup_driver
;
1870 dri2_wl_setup_swap_interval(dri2_dpy
);
1872 types
= EGL_WINDOW_BIT
;
1873 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1874 config
= dri2_dpy
->driver_configs
[i
];
1875 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1876 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1877 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1878 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1879 if (dri2_dpy
->formats
& HAS_RGB565
)
1880 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1883 /* Fill vtbl last to prevent accidentally calling virtual function during
1886 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1891 dlclose(dri2_dpy
->driver
);
1893 wl_shm_destroy(dri2_dpy
->wl_shm
);
1895 wl_registry_destroy(dri2_dpy
->wl_registry
);
1896 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1899 disp
->DriverData
= NULL
;
1905 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1907 EGLBoolean initialized
= EGL_TRUE
;
1909 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1912 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1913 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1916 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);