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 if (!_eglPutSurface(surf
))
235 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
237 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
238 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
239 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
240 if (dri2_surf
->color_buffers
[i
].dri_image
)
241 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
242 if (dri2_surf
->color_buffers
[i
].linear_copy
)
243 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
244 if (dri2_surf
->color_buffers
[i
].data
)
245 munmap(dri2_surf
->color_buffers
[i
].data
,
246 dri2_surf
->color_buffers
[i
].data_size
);
249 if (dri2_dpy
->dri2
) {
250 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
251 if (dri2_surf
->dri_buffers
[i
] &&
252 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
253 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
254 dri2_surf
->dri_buffers
[i
]);
257 if (dri2_surf
->throttle_callback
)
258 wl_callback_destroy(dri2_surf
->throttle_callback
);
260 dri2_surf
->wl_win
->private = NULL
;
261 dri2_surf
->wl_win
->resize_callback
= NULL
;
269 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
271 struct dri2_egl_display
*dri2_dpy
=
272 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
275 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
276 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
277 !dri2_surf
->color_buffers
[i
].locked
)
278 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
279 if (dri2_surf
->color_buffers
[i
].dri_image
)
280 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
281 if (dri2_surf
->color_buffers
[i
].linear_copy
)
282 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
283 if (dri2_surf
->color_buffers
[i
].data
)
284 munmap(dri2_surf
->color_buffers
[i
].data
,
285 dri2_surf
->color_buffers
[i
].data_size
);
287 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
288 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
289 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
290 dri2_surf
->color_buffers
[i
].data
= NULL
;
291 dri2_surf
->color_buffers
[i
].locked
= 0;
294 if (dri2_dpy
->dri2
) {
295 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
296 if (dri2_surf
->dri_buffers
[i
] &&
297 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
298 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
299 dri2_surf
->dri_buffers
[i
]);
304 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
306 struct dri2_egl_display
*dri2_dpy
=
307 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
309 unsigned int dri_image_format
;
311 /* currently supports three WL DRM formats,
312 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
313 * and WL_DRM_FORMAT_RGB565
315 switch (dri2_surf
->format
) {
316 case WL_DRM_FORMAT_ARGB8888
:
317 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
319 case WL_DRM_FORMAT_XRGB8888
:
320 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
322 case WL_DRM_FORMAT_RGB565
:
323 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
326 /* format is not supported */
330 /* We always want to throttle to some event (either a frame callback or
331 * a sync request) after the commit so that we can be sure the
332 * compositor has had a chance to handle it and send us a release event
333 * before we look for a free buffer */
334 while (dri2_surf
->throttle_callback
!= NULL
)
335 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
336 dri2_dpy
->wl_queue
) == -1)
339 if (dri2_surf
->back
== NULL
) {
340 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
341 /* Get an unlocked buffer, preferrably one with a dri_buffer
342 * already allocated. */
343 if (dri2_surf
->color_buffers
[i
].locked
)
345 if (dri2_surf
->back
== NULL
)
346 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
347 else if (dri2_surf
->back
->dri_image
== NULL
)
348 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
352 if (dri2_surf
->back
== NULL
)
355 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
357 if (dri2_dpy
->is_different_gpu
&&
358 dri2_surf
->back
->linear_copy
== NULL
) {
359 dri2_surf
->back
->linear_copy
=
360 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
361 dri2_surf
->base
.Width
,
362 dri2_surf
->base
.Height
,
365 __DRI_IMAGE_USE_LINEAR
,
367 if (dri2_surf
->back
->linear_copy
== NULL
)
371 if (dri2_surf
->back
->dri_image
== NULL
) {
372 dri2_surf
->back
->dri_image
=
373 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
374 dri2_surf
->base
.Width
,
375 dri2_surf
->base
.Height
,
377 dri2_dpy
->is_different_gpu
?
380 dri2_surf
->back
->age
= 0;
382 if (dri2_surf
->back
->dri_image
== NULL
)
385 dri2_surf
->back
->locked
= 1;
392 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
394 struct dri2_egl_display
*dri2_dpy
=
395 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
399 image
= dri2_surf
->back
->dri_image
;
401 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
402 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
404 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
406 buffer
->pitch
= pitch
;
412 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
413 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
415 struct dri2_egl_display
*dri2_dpy
=
416 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
417 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
420 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
422 dri2_surf
->base
.Width
,
423 dri2_surf
->base
.Height
);
424 dri2_surf
->dri_buffers
[attachment
] = b
;
429 memcpy(buffer
, b
, sizeof *buffer
);
435 update_buffers(struct dri2_egl_surface
*dri2_surf
)
437 struct dri2_egl_display
*dri2_dpy
=
438 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
441 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
442 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
444 dri2_wl_release_buffers(dri2_surf
);
446 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
447 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
448 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
449 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
452 if (get_back_bo(dri2_surf
) < 0) {
453 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
457 /* If we have an extra unlocked buffer at this point, we had to do triple
458 * buffering for a while, but now can go back to just double buffering.
459 * That means we can free any unlocked buffer now. */
460 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
461 if (!dri2_surf
->color_buffers
[i
].locked
&&
462 dri2_surf
->color_buffers
[i
].wl_buffer
) {
463 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
464 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
465 if (dri2_dpy
->is_different_gpu
)
466 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
467 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
468 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
469 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
477 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
478 int *width
, int *height
,
479 unsigned int *attachments
, int count
,
480 int *out_count
, void *loaderPrivate
)
482 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
485 if (update_buffers(dri2_surf
) < 0)
488 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
489 switch (attachments
[i
]) {
490 case __DRI_BUFFER_BACK_LEFT
:
491 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
494 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
495 &dri2_surf
->buffers
[j
]) < 0) {
496 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
507 *width
= dri2_surf
->base
.Width
;
508 *height
= dri2_surf
->base
.Height
;
510 return dri2_surf
->buffers
;
514 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
515 int *width
, int *height
,
516 unsigned int *attachments
, int count
,
517 int *out_count
, void *loaderPrivate
)
519 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
520 unsigned int *attachments_with_format
;
526 switch (dri2_surf
->format
) {
527 case WL_DRM_FORMAT_ARGB8888
:
528 case WL_DRM_FORMAT_XRGB8888
:
531 case WL_DRM_FORMAT_RGB565
:
535 /* format is not supported */
539 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
540 if (!attachments_with_format
) {
545 for (i
= 0; i
< count
; ++i
) {
546 attachments_with_format
[2*i
] = attachments
[i
];
547 attachments_with_format
[2*i
+ 1] = bpp
;
551 dri2_wl_get_buffers_with_format(driDrawable
,
553 attachments_with_format
, count
,
554 out_count
, loaderPrivate
);
556 free(attachments_with_format
);
562 image_get_buffers(__DRIdrawable
*driDrawable
,
566 uint32_t buffer_mask
,
567 struct __DRIimageList
*buffers
)
569 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
571 if (update_buffers(dri2_surf
) < 0)
574 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
575 buffers
->back
= dri2_surf
->back
->dri_image
;
581 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
584 (void) loaderPrivate
;
587 static const __DRIimageLoaderExtension image_loader_extension
= {
588 .base
= { __DRI_IMAGE_LOADER
, 1 },
590 .getBuffers
= image_get_buffers
,
591 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
595 wayland_throttle_callback(void *data
,
596 struct wl_callback
*callback
,
599 struct dri2_egl_surface
*dri2_surf
= data
;
601 dri2_surf
->throttle_callback
= NULL
;
602 wl_callback_destroy(callback
);
605 static const struct wl_callback_listener throttle_listener
= {
606 .done
= wayland_throttle_callback
610 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
612 struct dri2_egl_display
*dri2_dpy
=
613 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
615 int fd
, stride
, name
;
617 if (dri2_surf
->current
->wl_buffer
!= NULL
)
620 if (dri2_dpy
->is_different_gpu
) {
621 image
= dri2_surf
->current
->linear_copy
;
623 image
= dri2_surf
->current
->dri_image
;
625 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
626 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
627 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
629 dri2_surf
->current
->wl_buffer
=
630 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
632 dri2_surf
->base
.Width
,
633 dri2_surf
->base
.Height
,
640 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
641 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
643 dri2_surf
->current
->wl_buffer
=
644 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
646 dri2_surf
->base
.Width
,
647 dri2_surf
->base
.Height
,
652 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
654 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
655 &wl_buffer_listener
, dri2_surf
);
659 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
663 /* The WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION macro and
664 * wl_proxy_get_version() were both introduced in wayland 1.10.
665 * Instead of bumping our wayland dependency we just make this
666 * function conditional on the required 1.10 features, falling
667 * back to old (correct but suboptimal) behaviour for older
670 #ifdef WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
673 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_win
->surface
)
674 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
677 for (i
= 0; i
< n_rects
; i
++) {
678 const int *rect
= &rects
[i
* 4];
680 wl_surface_damage_buffer(dri2_surf
->wl_win
->surface
,
682 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
690 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
693 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
699 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
700 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
703 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
704 if (dri2_surf
->color_buffers
[i
].age
> 0)
705 dri2_surf
->color_buffers
[i
].age
++;
707 /* Make sure we have a back buffer in case we're swapping without ever
709 if (get_back_bo(dri2_surf
) < 0) {
710 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
714 if (draw
->SwapInterval
> 0) {
715 dri2_surf
->throttle_callback
=
716 wl_surface_frame(dri2_surf
->wl_win
->surface
);
717 wl_callback_add_listener(dri2_surf
->throttle_callback
,
718 &throttle_listener
, dri2_surf
);
719 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
723 dri2_surf
->back
->age
= 1;
724 dri2_surf
->current
= dri2_surf
->back
;
725 dri2_surf
->back
= NULL
;
727 create_wl_buffer(dri2_surf
);
729 wl_surface_attach(dri2_surf
->wl_win
->surface
,
730 dri2_surf
->current
->wl_buffer
,
731 dri2_surf
->dx
, dri2_surf
->dy
);
733 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
734 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
735 /* reset resize growing parameters */
739 /* If the compositor doesn't support damage_buffer, we deliberately
740 * ignore the damage region and post maximum damage, due to
741 * https://bugs.freedesktop.org/78190 */
742 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
743 wl_surface_damage(dri2_surf
->wl_win
->surface
,
744 0, 0, INT32_MAX
, INT32_MAX
);
746 if (dri2_dpy
->is_different_gpu
) {
747 _EGLContext
*ctx
= _eglGetCurrentContext();
748 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
749 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
750 dri2_surf
->current
->linear_copy
,
751 dri2_surf
->current
->dri_image
,
752 0, 0, dri2_surf
->base
.Width
,
753 dri2_surf
->base
.Height
,
754 0, 0, dri2_surf
->base
.Width
,
755 dri2_surf
->base
.Height
, 0);
758 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
759 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
761 wl_surface_commit(dri2_surf
->wl_win
->surface
);
763 /* If we're not waiting for a frame callback then we'll at least throttle
764 * to a sync callback so that we always give a chance for the compositor to
765 * handle the commit and send a release event before checking for a free
767 if (dri2_surf
->throttle_callback
== NULL
) {
768 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
769 wl_callback_add_listener(dri2_surf
->throttle_callback
,
770 &throttle_listener
, dri2_surf
);
771 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
775 wl_display_flush(dri2_dpy
->wl_dpy
);
781 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
782 _EGLDisplay
*disp
, _EGLSurface
*surface
)
784 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
786 if (get_back_bo(dri2_surf
) < 0) {
787 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
791 return dri2_surf
->back
->age
;
795 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
797 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
800 static struct wl_buffer
*
801 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
805 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
806 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
807 __DRIimage
*image
= dri2_img
->dri_image
;
808 struct wl_buffer
*buffer
;
809 int width
, height
, format
, pitch
;
810 enum wl_drm_format wl_format
;
812 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
815 case __DRI_IMAGE_FORMAT_ARGB8888
:
816 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
818 wl_format
= WL_DRM_FORMAT_ARGB8888
;
820 case __DRI_IMAGE_FORMAT_XRGB8888
:
821 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
823 wl_format
= WL_DRM_FORMAT_XRGB8888
;
829 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
830 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
831 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
833 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
836 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
839 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
851 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
854 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
861 /* The buffer object will have been created with our internal event queue
862 * because it is using the wl_drm object as a proxy factory. We want the
863 * buffer to be used by the application so we'll reset it to the display's
864 * default event queue */
866 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
871 _eglError(EGL_BAD_MATCH
, "unsupported image format");
876 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
878 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
881 if (dri2_dpy
->is_render_node
) {
882 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
883 "authenticate for render-nodes");
886 dri2_dpy
->authenticated
= 0;
888 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
889 if (roundtrip(dri2_dpy
) < 0)
892 if (!dri2_dpy
->authenticated
)
895 /* reset authenticated */
896 dri2_dpy
->authenticated
= 1;
902 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
904 struct dri2_egl_display
*dri2_dpy
= data
;
907 dri2_dpy
->device_name
= strdup(device
);
908 if (!dri2_dpy
->device_name
)
911 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
912 if (dri2_dpy
->fd
== -1) {
913 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
914 dri2_dpy
->device_name
, strerror(errno
));
918 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
919 dri2_dpy
->authenticated
= 1;
921 drmGetMagic(dri2_dpy
->fd
, &magic
);
922 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
927 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
929 struct dri2_egl_display
*dri2_dpy
= data
;
932 case WL_DRM_FORMAT_ARGB8888
:
933 dri2_dpy
->formats
|= HAS_ARGB8888
;
935 case WL_DRM_FORMAT_XRGB8888
:
936 dri2_dpy
->formats
|= HAS_XRGB8888
;
938 case WL_DRM_FORMAT_RGB565
:
939 dri2_dpy
->formats
|= HAS_RGB565
;
945 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
947 struct dri2_egl_display
*dri2_dpy
= data
;
949 dri2_dpy
->capabilities
= value
;
953 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
955 struct dri2_egl_display
*dri2_dpy
= data
;
957 dri2_dpy
->authenticated
= 1;
960 static const struct wl_drm_listener drm_listener
= {
961 .device
= drm_handle_device
,
962 .format
= drm_handle_format
,
963 .authenticated
= drm_handle_authenticated
,
964 .capabilities
= drm_handle_capabilities
968 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
969 const char *interface
, uint32_t version
)
971 struct dri2_egl_display
*dri2_dpy
= data
;
975 if (strcmp(interface
, "wl_drm") == 0) {
977 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
978 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
983 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
988 static const struct wl_registry_listener registry_listener_drm
= {
989 .global
= registry_handle_global_drm
,
990 .global_remove
= registry_handle_global_remove
994 dri2_wl_swap_interval(_EGLDriver
*drv
,
999 if (interval
> surf
->Config
->MaxSwapInterval
)
1000 interval
= surf
->Config
->MaxSwapInterval
;
1001 else if (interval
< surf
->Config
->MinSwapInterval
)
1002 interval
= surf
->Config
->MinSwapInterval
;
1004 surf
->SwapInterval
= interval
;
1010 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1012 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1014 /* We can't use values greater than 1 on Wayland because we are using the
1015 * frame callback to synchronise the frame and the only way we be sure to
1016 * get a frame callback is to attach a new buffer. Therefore we can't just
1017 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1019 if (dri2_dpy
->config
)
1020 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1021 "vblank_mode", &vblank_mode
);
1022 switch (vblank_mode
) {
1023 case DRI_CONF_VBLANK_NEVER
:
1024 dri2_dpy
->min_swap_interval
= 0;
1025 dri2_dpy
->max_swap_interval
= 0;
1026 dri2_dpy
->default_swap_interval
= 0;
1028 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1029 dri2_dpy
->min_swap_interval
= 1;
1030 dri2_dpy
->max_swap_interval
= 1;
1031 dri2_dpy
->default_swap_interval
= 1;
1033 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1034 dri2_dpy
->min_swap_interval
= 0;
1035 dri2_dpy
->max_swap_interval
= 1;
1036 dri2_dpy
->default_swap_interval
= 0;
1039 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1040 dri2_dpy
->min_swap_interval
= 0;
1041 dri2_dpy
->max_swap_interval
= 1;
1042 dri2_dpy
->default_swap_interval
= 1;
1047 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1048 .authenticate
= dri2_wl_authenticate
,
1049 .create_window_surface
= dri2_wl_create_window_surface
,
1050 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1051 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1052 .destroy_surface
= dri2_wl_destroy_surface
,
1053 .create_image
= dri2_create_image_khr
,
1054 .swap_interval
= dri2_wl_swap_interval
,
1055 .swap_buffers
= dri2_wl_swap_buffers
,
1056 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1057 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1058 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1059 .copy_buffers
= dri2_fallback_copy_buffers
,
1060 .query_buffer_age
= dri2_wl_query_buffer_age
,
1061 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1062 .get_sync_values
= dri2_fallback_get_sync_values
,
1063 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1067 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1069 struct dri2_egl_display
*dri2_dpy
;
1070 const __DRIconfig
*config
;
1073 static const unsigned int argb_masks
[4] =
1074 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1075 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1076 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1078 loader_set_logger(_eglLog
);
1080 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1082 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1084 disp
->DriverData
= (void *) dri2_dpy
;
1085 if (disp
->PlatformDisplay
== NULL
) {
1086 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1087 if (dri2_dpy
->wl_dpy
== NULL
)
1089 dri2_dpy
->own_device
= 1;
1091 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1094 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1096 if (dri2_dpy
->own_device
)
1097 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1099 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1100 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1101 dri2_dpy
->wl_queue
);
1102 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1103 ®istry_listener_drm
, dri2_dpy
);
1104 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1105 goto cleanup_registry
;
1107 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1110 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1113 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1114 &dri2_dpy
->is_different_gpu
);
1115 if (dri2_dpy
->is_different_gpu
) {
1116 free(dri2_dpy
->device_name
);
1117 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1118 if (!dri2_dpy
->device_name
) {
1119 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1120 "for requested GPU");
1125 /* we have to do the check now, because loader_get_user_preferred_fd
1126 * will return a render-node when the requested gpu is different
1127 * to the server, but also if the client asks for the same gpu than
1128 * the server by requesting its pci-id */
1129 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1131 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1132 if (dri2_dpy
->driver_name
== NULL
) {
1133 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1137 if (!dri2_load_driver(disp
))
1138 goto cleanup_driver_name
;
1140 dri2_dpy
->extensions
[0] = &image_loader_extension
.base
;
1141 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1142 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
1144 /* render nodes cannot use Gem names, and thus do not support
1145 * the __DRI_DRI2_LOADER extension */
1146 if (!dri2_dpy
->is_render_node
) {
1147 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1148 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1149 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1150 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1151 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1152 dri2_wl_get_buffers_with_format
;
1153 dri2_dpy
->extensions
[3] = &dri2_dpy
->dri2_loader_extension
.base
;
1154 dri2_dpy
->extensions
[4] = NULL
;
1156 dri2_dpy
->extensions
[3] = NULL
;
1158 if (!dri2_create_screen(disp
))
1159 goto cleanup_driver
;
1161 dri2_wl_setup_swap_interval(dri2_dpy
);
1163 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1164 * createImageFromFds support indicates that Prime export/import
1165 * is supported by the driver. Fall back to
1166 * gem names if we don't have Prime support. */
1168 if (dri2_dpy
->image
->base
.version
< 7 ||
1169 dri2_dpy
->image
->createImageFromFds
== NULL
)
1170 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1172 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1173 * The server needs to accept them */
1174 if (dri2_dpy
->is_render_node
&&
1175 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1176 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1177 goto cleanup_screen
;
1180 if (dri2_dpy
->is_different_gpu
&&
1181 (dri2_dpy
->image
->base
.version
< 9 ||
1182 dri2_dpy
->image
->blitImage
== NULL
)) {
1183 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1184 "Image extension in the driver is not "
1185 "compatible. Version 9 or later and blitImage() "
1187 goto cleanup_screen
;
1190 types
= EGL_WINDOW_BIT
;
1191 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1192 config
= dri2_dpy
->driver_configs
[i
];
1193 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1194 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1195 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1196 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1197 if (dri2_dpy
->formats
& HAS_RGB565
)
1198 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1201 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1202 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1203 * because the buffer of the EGLImage has likely a tiling mode the server
1204 * gpu won't support. These is no way to check for now. Thus do not support the
1206 if (!dri2_dpy
->is_different_gpu
) {
1207 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1209 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1210 dri2_fallback_create_wayland_buffer_from_image
;
1212 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1214 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1216 /* Fill vtbl last to prevent accidentally calling virtual function during
1219 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1224 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1226 dlclose(dri2_dpy
->driver
);
1227 cleanup_driver_name
:
1228 free(dri2_dpy
->driver_name
);
1230 close(dri2_dpy
->fd
);
1232 free(dri2_dpy
->device_name
);
1233 wl_drm_destroy(dri2_dpy
->wl_drm
);
1235 wl_registry_destroy(dri2_dpy
->wl_registry
);
1236 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1239 disp
->DriverData
= NULL
;
1245 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1247 if (format
== WL_SHM_FORMAT_RGB565
)
1249 else /* ARGB8888 || XRGB8888 */
1254 * Taken from weston shared/os-compatibility.c
1257 #ifndef HAVE_MKOSTEMP
1260 set_cloexec_or_close(int fd
)
1267 flags
= fcntl(fd
, F_GETFD
);
1271 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1284 * Taken from weston shared/os-compatibility.c
1288 create_tmpfile_cloexec(char *tmpname
)
1292 #ifdef HAVE_MKOSTEMP
1293 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1297 fd
= mkstemp(tmpname
);
1299 fd
= set_cloexec_or_close(fd
);
1308 * Taken from weston shared/os-compatibility.c
1310 * Create a new, unique, anonymous file of the given size, and
1311 * return the file descriptor for it. The file descriptor is set
1312 * CLOEXEC. The file is immediately suitable for mmap()'ing
1313 * the given size at offset zero.
1315 * The file should not have a permanent backing store like a disk,
1316 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1318 * The file name is deleted from the file system.
1320 * The file is suitable for buffer sharing between processes by
1321 * transmitting the file descriptor over Unix sockets using the
1322 * SCM_RIGHTS methods.
1324 * If the C library implements posix_fallocate(), it is used to
1325 * guarantee that disk space is available for the file at the
1326 * given size. If disk space is insufficent, errno is set to ENOSPC.
1327 * If posix_fallocate() is not supported, program may receive
1328 * SIGBUS on accessing mmap()'ed file contents instead.
1331 os_create_anonymous_file(off_t size
)
1333 static const char template[] = "/mesa-shared-XXXXXX";
1339 path
= getenv("XDG_RUNTIME_DIR");
1345 name
= malloc(strlen(path
) + sizeof(template));
1350 strcat(name
, template);
1352 fd
= create_tmpfile_cloexec(name
);
1359 ret
= ftruncate(fd
, size
);
1370 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1371 int format
, int w
, int h
,
1372 void **data
, int *size
,
1373 struct wl_buffer
**buffer
)
1375 struct wl_shm_pool
*pool
;
1376 int fd
, stride
, size_map
;
1379 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1380 size_map
= h
* stride
;
1382 /* Create a sharable buffer */
1383 fd
= os_create_anonymous_file(size_map
);
1387 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1388 if (data_map
== MAP_FAILED
) {
1393 /* Share it in a wl_buffer */
1394 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1395 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1396 wl_shm_pool_destroy(pool
);
1405 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1407 struct dri2_egl_display
*dri2_dpy
=
1408 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1411 /* we need to do the following operations only once per frame */
1412 if (dri2_surf
->back
)
1415 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1416 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1418 dri2_wl_release_buffers(dri2_surf
);
1420 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1421 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1422 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1423 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1424 dri2_surf
->current
= NULL
;
1427 /* find back buffer */
1429 /* We always want to throttle to some event (either a frame callback or
1430 * a sync request) after the commit so that we can be sure the
1431 * compositor has had a chance to handle it and send us a release event
1432 * before we look for a free buffer */
1433 while (dri2_surf
->throttle_callback
!= NULL
)
1434 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1435 dri2_dpy
->wl_queue
) == -1)
1438 /* try get free buffer already created */
1439 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1440 if (!dri2_surf
->color_buffers
[i
].locked
&&
1441 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1442 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1447 /* else choose any another free location */
1448 if (!dri2_surf
->back
) {
1449 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1450 if (!dri2_surf
->color_buffers
[i
].locked
) {
1451 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1452 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1454 dri2_surf
->base
.Width
,
1455 dri2_surf
->base
.Height
,
1456 &dri2_surf
->back
->data
,
1457 &dri2_surf
->back
->data_size
,
1458 &dri2_surf
->back
->wl_buffer
)) {
1459 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1462 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1463 dri2_dpy
->wl_queue
);
1464 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1465 &wl_buffer_listener
, dri2_surf
);
1471 if (!dri2_surf
->back
) {
1472 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1476 dri2_surf
->back
->locked
= 1;
1478 /* If we have an extra unlocked buffer at this point, we had to do triple
1479 * buffering for a while, but now can go back to just double buffering.
1480 * That means we can free any unlocked buffer now. */
1481 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1482 if (!dri2_surf
->color_buffers
[i
].locked
&&
1483 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1484 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1485 munmap(dri2_surf
->color_buffers
[i
].data
,
1486 dri2_surf
->color_buffers
[i
].data_size
);
1487 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1488 dri2_surf
->color_buffers
[i
].data
= NULL
;
1496 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1498 /* if there has been a resize: */
1499 if (!dri2_surf
->current
)
1502 return dri2_surf
->current
->data
;
1506 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1508 assert(dri2_surf
->back
);
1509 return dri2_surf
->back
->data
;
1513 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1515 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1517 if (dri2_surf
->base
.SwapInterval
> 0) {
1518 dri2_surf
->throttle_callback
=
1519 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1520 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1521 &throttle_listener
, dri2_surf
);
1522 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1523 dri2_dpy
->wl_queue
);
1526 dri2_surf
->current
= dri2_surf
->back
;
1527 dri2_surf
->back
= NULL
;
1529 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1530 dri2_surf
->current
->wl_buffer
,
1531 dri2_surf
->dx
, dri2_surf
->dy
);
1533 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1534 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1535 /* reset resize growing parameters */
1539 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1540 0, 0, INT32_MAX
, INT32_MAX
);
1541 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1543 /* If we're not waiting for a frame callback then we'll at least throttle
1544 * to a sync callback so that we always give a chance for the compositor to
1545 * handle the commit and send a release event before checking for a free
1547 if (dri2_surf
->throttle_callback
== NULL
) {
1548 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
1549 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1550 &throttle_listener
, dri2_surf
);
1551 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1552 dri2_dpy
->wl_queue
);
1555 wl_display_flush(dri2_dpy
->wl_dpy
);
1559 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1560 int *x
, int *y
, int *w
, int *h
,
1561 void *loaderPrivate
)
1563 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1565 (void) swrast_update_buffers(dri2_surf
);
1568 *w
= dri2_surf
->base
.Width
;
1569 *h
= dri2_surf
->base
.Height
;
1573 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1574 int x
, int y
, int w
, int h
,
1575 char *data
, void *loaderPrivate
)
1577 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1578 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1579 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1580 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1581 int dst_stride
= copy_width
;
1584 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1586 memset(data
, 0, copy_width
* h
);
1590 assert(data
!= src
);
1591 assert(copy_width
<= src_stride
);
1594 src
+= y
* src_stride
;
1597 if (copy_width
> src_stride
-x_offset
)
1598 copy_width
= src_stride
-x_offset
;
1599 if (h
> dri2_surf
->base
.Height
-y
)
1600 h
= dri2_surf
->base
.Height
-y
;
1603 memcpy(dst
, src
, copy_width
);
1610 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1611 int x
, int y
, int w
, int h
, int stride
,
1612 char *data
, void *loaderPrivate
)
1614 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1615 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1616 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1617 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1620 assert(copy_width
<= stride
);
1622 (void) swrast_update_buffers(dri2_surf
);
1623 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1625 /* partial copy, copy old content */
1626 if (copy_width
< dst_stride
)
1627 dri2_wl_swrast_get_image(draw
, 0, 0,
1628 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1629 dst
, loaderPrivate
);
1632 dst
+= y
* dst_stride
;
1636 /* drivers expect we do these checks (and some rely on it) */
1637 if (copy_width
> dst_stride
-x_offset
)
1638 copy_width
= dst_stride
-x_offset
;
1639 if (h
> dri2_surf
->base
.Height
-y
)
1640 h
= dri2_surf
->base
.Height
-y
;
1643 memcpy(dst
, src
, copy_width
);
1647 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1651 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1652 int x
, int y
, int w
, int h
,
1653 char *data
, void *loaderPrivate
)
1655 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1658 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1659 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1660 stride
, data
, loaderPrivate
);
1664 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1666 static _EGLSurface
*
1667 dri2_wl_swrast_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1668 _EGLConfig
*conf
, void *native_window
,
1669 const EGLint
*attrib_list
)
1671 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1672 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1673 struct wl_egl_window
*window
= native_window
;
1674 struct dri2_egl_surface
*dri2_surf
;
1675 const __DRIconfig
*config
;
1679 dri2_surf
= calloc(1, sizeof *dri2_surf
);
1681 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1685 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
1688 if (conf
->RedSize
== 5)
1689 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
1690 else if (conf
->AlphaSize
== 0)
1691 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
1693 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
1695 dri2_surf
->wl_win
= window
;
1697 dri2_surf
->base
.Width
= -1;
1698 dri2_surf
->base
.Height
= -1;
1700 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
1701 dri2_surf
->base
.GLColorspace
);
1703 dri2_surf
->dri_drawable
=
1704 (*dri2_dpy
->swrast
->createNewDrawable
)(dri2_dpy
->dri_screen
,
1706 if (dri2_surf
->dri_drawable
== NULL
) {
1707 _eglError(EGL_BAD_ALLOC
, "swrast->createNewDrawable");
1708 goto cleanup_dri_drawable
;
1711 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
1712 dri2_dpy
->default_swap_interval
);
1714 return &dri2_surf
->base
;
1716 cleanup_dri_drawable
:
1717 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1725 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1727 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1728 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1730 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1735 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1737 struct dri2_egl_display
*dri2_dpy
= data
;
1740 case WL_SHM_FORMAT_ARGB8888
:
1741 dri2_dpy
->formats
|= HAS_ARGB8888
;
1743 case WL_SHM_FORMAT_XRGB8888
:
1744 dri2_dpy
->formats
|= HAS_XRGB8888
;
1746 case WL_SHM_FORMAT_RGB565
:
1747 dri2_dpy
->formats
|= HAS_RGB565
;
1752 static const struct wl_shm_listener shm_listener
= {
1753 .format
= shm_handle_format
1757 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1758 const char *interface
, uint32_t version
)
1760 struct dri2_egl_display
*dri2_dpy
= data
;
1762 if (strcmp(interface
, "wl_shm") == 0) {
1764 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1765 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1769 static const struct wl_registry_listener registry_listener_swrast
= {
1770 .global
= registry_handle_global_swrast
,
1771 .global_remove
= registry_handle_global_remove
1774 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1775 .authenticate
= NULL
,
1776 .create_window_surface
= dri2_wl_swrast_create_window_surface
,
1777 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1778 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1779 .destroy_surface
= dri2_wl_destroy_surface
,
1780 .create_image
= dri2_fallback_create_image_khr
,
1781 .swap_interval
= dri2_wl_swap_interval
,
1782 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1783 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1784 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1785 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1786 .copy_buffers
= dri2_fallback_copy_buffers
,
1787 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1788 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1789 .get_sync_values
= dri2_fallback_get_sync_values
,
1790 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1794 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1796 struct dri2_egl_display
*dri2_dpy
;
1797 const __DRIconfig
*config
;
1800 static const unsigned int argb_masks
[4] =
1801 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1802 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1803 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1805 loader_set_logger(_eglLog
);
1807 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1809 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1811 disp
->DriverData
= (void *) dri2_dpy
;
1812 if (disp
->PlatformDisplay
== NULL
) {
1813 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1814 if (dri2_dpy
->wl_dpy
== NULL
)
1816 dri2_dpy
->own_device
= 1;
1818 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1821 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1823 if (dri2_dpy
->own_device
)
1824 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1826 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1827 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1828 dri2_dpy
->wl_queue
);
1829 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1830 ®istry_listener_swrast
, dri2_dpy
);
1832 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1833 goto cleanup_registry
;
1835 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1839 dri2_dpy
->driver_name
= strdup("swrast");
1840 if (!dri2_load_driver_swrast(disp
))
1843 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
1844 dri2_dpy
->swrast_loader_extension
.base
.version
= 2;
1845 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= dri2_wl_swrast_get_drawable_info
;
1846 dri2_dpy
->swrast_loader_extension
.putImage
= dri2_wl_swrast_put_image
;
1847 dri2_dpy
->swrast_loader_extension
.getImage
= dri2_wl_swrast_get_image
;
1848 dri2_dpy
->swrast_loader_extension
.putImage2
= dri2_wl_swrast_put_image2
;
1850 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
1851 dri2_dpy
->extensions
[1] = NULL
;
1853 if (!dri2_create_screen(disp
))
1854 goto cleanup_driver
;
1856 dri2_wl_setup_swap_interval(dri2_dpy
);
1858 types
= EGL_WINDOW_BIT
;
1859 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1860 config
= dri2_dpy
->driver_configs
[i
];
1861 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1862 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1863 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1864 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1865 if (dri2_dpy
->formats
& HAS_RGB565
)
1866 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1869 /* Fill vtbl last to prevent accidentally calling virtual function during
1872 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1877 dlclose(dri2_dpy
->driver
);
1879 wl_shm_destroy(dri2_dpy
->wl_shm
);
1881 wl_registry_destroy(dri2_dpy
->wl_registry
);
1882 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1885 disp
->DriverData
= NULL
;
1891 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1893 EGLBoolean initialized
= EGL_TRUE
;
1895 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1898 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1899 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1902 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);