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_wrapper
);
78 wl_callback_add_listener(callback
, &sync_listener
, &done
);
79 while (ret
!= -1 && !done
)
80 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
83 wl_callback_destroy(callback
);
89 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
91 struct dri2_egl_surface
*dri2_surf
= data
;
94 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
95 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
98 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
99 wl_buffer_destroy(buffer
);
103 dri2_surf
->color_buffers
[i
].locked
= 0;
106 static const struct wl_buffer_listener wl_buffer_listener
= {
107 .release
= wl_buffer_release
111 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
113 struct dri2_egl_surface
*dri2_surf
= data
;
114 struct dri2_egl_display
*dri2_dpy
=
115 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
117 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
121 destroy_window_callback(void *data
)
123 struct dri2_egl_surface
*dri2_surf
= data
;
124 dri2_surf
->wl_win
= NULL
;
128 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
131 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
132 _EGLConfig
*conf
, void *native_window
,
133 const EGLint
*attrib_list
)
135 __DRIcreateNewDrawableFunc createNewDrawable
;
136 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
137 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
138 struct wl_egl_window
*window
= native_window
;
139 struct dri2_egl_surface
*dri2_surf
;
140 const __DRIconfig
*config
;
142 dri2_surf
= calloc(1, sizeof *dri2_surf
);
144 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
148 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
151 if (conf
->RedSize
== 5)
152 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
153 else if (conf
->AlphaSize
== 0)
154 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
156 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
159 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
163 dri2_surf
->wl_win
= window
;
165 dri2_surf
->wl_win
->private = dri2_surf
;
166 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
168 dri2_surf
->base
.Width
= -1;
169 dri2_surf
->base
.Height
= -1;
171 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
172 dri2_surf
->base
.GLColorspace
);
174 if (dri2_dpy
->dri2
) {
175 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
177 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
179 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
182 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
184 if (dri2_surf
->dri_drawable
== NULL
) {
185 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
189 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
190 dri2_dpy
->default_swap_interval
);
192 return &dri2_surf
->base
;
201 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
202 _EGLConfig
*conf
, void *native_window
,
203 const EGLint
*attrib_list
)
205 /* From the EGL_EXT_platform_wayland spec, version 3:
207 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
208 * that belongs to Wayland. Any such call fails and generates
211 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
217 * Called via eglDestroySurface(), drv->API.DestroySurface().
220 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
222 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
223 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
228 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
230 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
231 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
232 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
233 if (dri2_surf
->color_buffers
[i
].dri_image
)
234 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
235 if (dri2_surf
->color_buffers
[i
].linear_copy
)
236 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
237 if (dri2_surf
->color_buffers
[i
].data
)
238 munmap(dri2_surf
->color_buffers
[i
].data
,
239 dri2_surf
->color_buffers
[i
].data_size
);
242 if (dri2_dpy
->dri2
) {
243 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
244 if (dri2_surf
->dri_buffers
[i
] &&
245 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
246 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
247 dri2_surf
->dri_buffers
[i
]);
250 if (dri2_surf
->throttle_callback
)
251 wl_callback_destroy(dri2_surf
->throttle_callback
);
253 if (dri2_surf
->wl_win
) {
254 dri2_surf
->wl_win
->private = NULL
;
255 dri2_surf
->wl_win
->resize_callback
= NULL
;
256 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
265 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
267 struct dri2_egl_display
*dri2_dpy
=
268 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
271 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
272 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
273 !dri2_surf
->color_buffers
[i
].locked
)
274 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
275 if (dri2_surf
->color_buffers
[i
].dri_image
)
276 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
277 if (dri2_surf
->color_buffers
[i
].linear_copy
)
278 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
279 if (dri2_surf
->color_buffers
[i
].data
)
280 munmap(dri2_surf
->color_buffers
[i
].data
,
281 dri2_surf
->color_buffers
[i
].data_size
);
283 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
284 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
285 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
286 dri2_surf
->color_buffers
[i
].data
= NULL
;
287 dri2_surf
->color_buffers
[i
].locked
= 0;
290 if (dri2_dpy
->dri2
) {
291 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
292 if (dri2_surf
->dri_buffers
[i
] &&
293 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
294 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
295 dri2_surf
->dri_buffers
[i
]);
300 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
302 struct dri2_egl_display
*dri2_dpy
=
303 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
305 unsigned int dri_image_format
;
307 /* currently supports three WL DRM formats,
308 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
309 * and WL_DRM_FORMAT_RGB565
311 switch (dri2_surf
->format
) {
312 case WL_DRM_FORMAT_ARGB8888
:
313 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
315 case WL_DRM_FORMAT_XRGB8888
:
316 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
318 case WL_DRM_FORMAT_RGB565
:
319 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
322 /* format is not supported */
326 /* There might be a buffer release already queued that wasn't processed */
327 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
329 if (dri2_surf
->back
== NULL
) {
330 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
331 /* Get an unlocked buffer, preferrably one with a dri_buffer
332 * already allocated. */
333 if (dri2_surf
->color_buffers
[i
].locked
)
335 if (dri2_surf
->back
== NULL
)
336 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
337 else if (dri2_surf
->back
->dri_image
== NULL
)
338 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
342 if (dri2_surf
->back
== NULL
)
345 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
347 if (dri2_dpy
->is_different_gpu
&&
348 dri2_surf
->back
->linear_copy
== NULL
) {
349 dri2_surf
->back
->linear_copy
=
350 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
351 dri2_surf
->base
.Width
,
352 dri2_surf
->base
.Height
,
355 __DRI_IMAGE_USE_LINEAR
,
357 if (dri2_surf
->back
->linear_copy
== NULL
)
361 if (dri2_surf
->back
->dri_image
== NULL
) {
362 dri2_surf
->back
->dri_image
=
363 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
364 dri2_surf
->base
.Width
,
365 dri2_surf
->base
.Height
,
367 dri2_dpy
->is_different_gpu
?
370 dri2_surf
->back
->age
= 0;
372 if (dri2_surf
->back
->dri_image
== NULL
)
375 dri2_surf
->back
->locked
= 1;
382 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
384 struct dri2_egl_display
*dri2_dpy
=
385 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
389 image
= dri2_surf
->back
->dri_image
;
391 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
392 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
394 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
396 buffer
->pitch
= pitch
;
402 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
403 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
405 struct dri2_egl_display
*dri2_dpy
=
406 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
407 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
410 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
412 dri2_surf
->base
.Width
,
413 dri2_surf
->base
.Height
);
414 dri2_surf
->dri_buffers
[attachment
] = b
;
419 memcpy(buffer
, b
, sizeof *buffer
);
425 update_buffers(struct dri2_egl_surface
*dri2_surf
)
427 struct dri2_egl_display
*dri2_dpy
=
428 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
431 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
432 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
434 dri2_wl_release_buffers(dri2_surf
);
436 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
437 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
438 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
439 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
442 if (get_back_bo(dri2_surf
) < 0) {
443 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
447 /* If we have an extra unlocked buffer at this point, we had to do triple
448 * buffering for a while, but now can go back to just double buffering.
449 * That means we can free any unlocked buffer now. */
450 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
451 if (!dri2_surf
->color_buffers
[i
].locked
&&
452 dri2_surf
->color_buffers
[i
].wl_buffer
) {
453 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
454 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
455 if (dri2_dpy
->is_different_gpu
)
456 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
457 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
458 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
459 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
467 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
468 int *width
, int *height
,
469 unsigned int *attachments
, int count
,
470 int *out_count
, void *loaderPrivate
)
472 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
475 if (update_buffers(dri2_surf
) < 0)
478 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
479 switch (attachments
[i
]) {
480 case __DRI_BUFFER_BACK_LEFT
:
481 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
484 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
485 &dri2_surf
->buffers
[j
]) < 0) {
486 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
497 *width
= dri2_surf
->base
.Width
;
498 *height
= dri2_surf
->base
.Height
;
500 return dri2_surf
->buffers
;
504 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
505 int *width
, int *height
,
506 unsigned int *attachments
, int count
,
507 int *out_count
, void *loaderPrivate
)
509 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
510 unsigned int *attachments_with_format
;
516 switch (dri2_surf
->format
) {
517 case WL_DRM_FORMAT_ARGB8888
:
518 case WL_DRM_FORMAT_XRGB8888
:
521 case WL_DRM_FORMAT_RGB565
:
525 /* format is not supported */
529 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
530 if (!attachments_with_format
) {
535 for (i
= 0; i
< count
; ++i
) {
536 attachments_with_format
[2*i
] = attachments
[i
];
537 attachments_with_format
[2*i
+ 1] = bpp
;
541 dri2_wl_get_buffers_with_format(driDrawable
,
543 attachments_with_format
, count
,
544 out_count
, loaderPrivate
);
546 free(attachments_with_format
);
552 image_get_buffers(__DRIdrawable
*driDrawable
,
556 uint32_t buffer_mask
,
557 struct __DRIimageList
*buffers
)
559 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
561 if (update_buffers(dri2_surf
) < 0)
564 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
565 buffers
->back
= dri2_surf
->back
->dri_image
;
571 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
574 (void) loaderPrivate
;
577 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
578 .base
= { __DRI_DRI2_LOADER
, 3 },
580 .getBuffers
= dri2_wl_get_buffers
,
581 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
582 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
585 static const __DRIimageLoaderExtension image_loader_extension
= {
586 .base
= { __DRI_IMAGE_LOADER
, 1 },
588 .getBuffers
= image_get_buffers
,
589 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
593 wayland_throttle_callback(void *data
,
594 struct wl_callback
*callback
,
597 struct dri2_egl_surface
*dri2_surf
= data
;
599 dri2_surf
->throttle_callback
= NULL
;
600 wl_callback_destroy(callback
);
603 static const struct wl_callback_listener throttle_listener
= {
604 .done
= wayland_throttle_callback
608 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
610 struct dri2_egl_display
*dri2_dpy
=
611 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
613 int fd
, stride
, name
;
615 if (dri2_surf
->current
->wl_buffer
!= NULL
)
618 if (dri2_dpy
->is_different_gpu
) {
619 image
= dri2_surf
->current
->linear_copy
;
621 image
= dri2_surf
->current
->dri_image
;
623 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
624 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
625 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
627 dri2_surf
->current
->wl_buffer
=
628 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
630 dri2_surf
->base
.Width
,
631 dri2_surf
->base
.Height
,
638 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
639 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
641 dri2_surf
->current
->wl_buffer
=
642 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
644 dri2_surf
->base
.Width
,
645 dri2_surf
->base
.Height
,
650 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
652 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
653 &wl_buffer_listener
, dri2_surf
);
657 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
661 /* The WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION macro and
662 * wl_proxy_get_version() were both introduced in wayland 1.10.
663 * Instead of bumping our wayland dependency we just make this
664 * function conditional on the required 1.10 features, falling
665 * back to old (correct but suboptimal) behaviour for older
668 #ifdef WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
671 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_win
->surface
)
672 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
675 for (i
= 0; i
< n_rects
; i
++) {
676 const int *rect
= &rects
[i
* 4];
678 wl_surface_damage_buffer(dri2_surf
->wl_win
->surface
,
680 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
688 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
691 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
697 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
698 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
701 while (dri2_surf
->throttle_callback
!= NULL
)
702 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
703 dri2_dpy
->wl_queue
) == -1)
706 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
707 if (dri2_surf
->color_buffers
[i
].age
> 0)
708 dri2_surf
->color_buffers
[i
].age
++;
710 /* Make sure we have a back buffer in case we're swapping without ever
712 if (get_back_bo(dri2_surf
) < 0) {
713 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
717 if (draw
->SwapInterval
> 0) {
718 dri2_surf
->throttle_callback
=
719 wl_surface_frame(dri2_surf
->wl_win
->surface
);
720 wl_callback_add_listener(dri2_surf
->throttle_callback
,
721 &throttle_listener
, dri2_surf
);
722 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
726 dri2_surf
->back
->age
= 1;
727 dri2_surf
->current
= dri2_surf
->back
;
728 dri2_surf
->back
= NULL
;
730 create_wl_buffer(dri2_surf
);
732 wl_surface_attach(dri2_surf
->wl_win
->surface
,
733 dri2_surf
->current
->wl_buffer
,
734 dri2_surf
->dx
, dri2_surf
->dy
);
736 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
737 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
738 /* reset resize growing parameters */
742 /* If the compositor doesn't support damage_buffer, we deliberately
743 * ignore the damage region and post maximum damage, due to
744 * https://bugs.freedesktop.org/78190 */
745 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
746 wl_surface_damage(dri2_surf
->wl_win
->surface
,
747 0, 0, INT32_MAX
, INT32_MAX
);
749 if (dri2_dpy
->is_different_gpu
) {
750 _EGLContext
*ctx
= _eglGetCurrentContext();
751 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
752 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
753 dri2_surf
->current
->linear_copy
,
754 dri2_surf
->current
->dri_image
,
755 0, 0, dri2_surf
->base
.Width
,
756 dri2_surf
->base
.Height
,
757 0, 0, dri2_surf
->base
.Width
,
758 dri2_surf
->base
.Height
, 0);
761 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
762 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
764 wl_surface_commit(dri2_surf
->wl_win
->surface
);
766 /* If we're not waiting for a frame callback then we'll at least throttle
767 * to a sync callback so that we always give a chance for the compositor to
768 * handle the commit and send a release event before checking for a free
770 if (dri2_surf
->throttle_callback
== NULL
) {
771 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
772 wl_callback_add_listener(dri2_surf
->throttle_callback
,
773 &throttle_listener
, dri2_surf
);
776 wl_display_flush(dri2_dpy
->wl_dpy
);
782 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
783 _EGLDisplay
*disp
, _EGLSurface
*surface
)
785 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
787 if (get_back_bo(dri2_surf
) < 0) {
788 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
792 return dri2_surf
->back
->age
;
796 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
798 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
801 static struct wl_buffer
*
802 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
806 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
807 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
808 __DRIimage
*image
= dri2_img
->dri_image
;
809 struct wl_buffer
*buffer
;
810 int width
, height
, format
, pitch
;
811 enum wl_drm_format wl_format
;
813 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
816 case __DRI_IMAGE_FORMAT_ARGB8888
:
817 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
819 wl_format
= WL_DRM_FORMAT_ARGB8888
;
821 case __DRI_IMAGE_FORMAT_XRGB8888
:
822 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
824 wl_format
= WL_DRM_FORMAT_XRGB8888
;
830 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
831 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
832 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
834 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
837 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
840 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
852 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
855 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
862 /* The buffer object will have been created with our internal event queue
863 * because it is using the wl_drm object as a proxy factory. We want the
864 * buffer to be used by the application so we'll reset it to the display's
865 * default event queue */
867 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
872 _eglError(EGL_BAD_MATCH
, "unsupported image format");
877 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
879 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
882 if (dri2_dpy
->is_render_node
) {
883 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
884 "authenticate for render-nodes");
887 dri2_dpy
->authenticated
= 0;
889 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
890 if (roundtrip(dri2_dpy
) < 0)
893 if (!dri2_dpy
->authenticated
)
896 /* reset authenticated */
897 dri2_dpy
->authenticated
= 1;
903 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
905 struct dri2_egl_display
*dri2_dpy
= data
;
908 dri2_dpy
->device_name
= strdup(device
);
909 if (!dri2_dpy
->device_name
)
912 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
913 if (dri2_dpy
->fd
== -1) {
914 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
915 dri2_dpy
->device_name
, strerror(errno
));
919 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
920 dri2_dpy
->authenticated
= 1;
922 drmGetMagic(dri2_dpy
->fd
, &magic
);
923 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
928 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
930 struct dri2_egl_display
*dri2_dpy
= data
;
933 case WL_DRM_FORMAT_ARGB8888
:
934 dri2_dpy
->formats
|= HAS_ARGB8888
;
936 case WL_DRM_FORMAT_XRGB8888
:
937 dri2_dpy
->formats
|= HAS_XRGB8888
;
939 case WL_DRM_FORMAT_RGB565
:
940 dri2_dpy
->formats
|= HAS_RGB565
;
946 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
948 struct dri2_egl_display
*dri2_dpy
= data
;
950 dri2_dpy
->capabilities
= value
;
954 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
956 struct dri2_egl_display
*dri2_dpy
= data
;
958 dri2_dpy
->authenticated
= 1;
961 static const struct wl_drm_listener drm_listener
= {
962 .device
= drm_handle_device
,
963 .format
= drm_handle_format
,
964 .authenticated
= drm_handle_authenticated
,
965 .capabilities
= drm_handle_capabilities
969 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
970 const char *interface
, uint32_t version
)
972 struct dri2_egl_display
*dri2_dpy
= data
;
976 if (strcmp(interface
, "wl_drm") == 0) {
978 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
979 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
984 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
989 static const struct wl_registry_listener registry_listener_drm
= {
990 .global
= registry_handle_global_drm
,
991 .global_remove
= registry_handle_global_remove
995 dri2_wl_swap_interval(_EGLDriver
*drv
,
1000 if (interval
> surf
->Config
->MaxSwapInterval
)
1001 interval
= surf
->Config
->MaxSwapInterval
;
1002 else if (interval
< surf
->Config
->MinSwapInterval
)
1003 interval
= surf
->Config
->MinSwapInterval
;
1005 surf
->SwapInterval
= interval
;
1011 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1013 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1015 /* We can't use values greater than 1 on Wayland because we are using the
1016 * frame callback to synchronise the frame and the only way we be sure to
1017 * get a frame callback is to attach a new buffer. Therefore we can't just
1018 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1020 if (dri2_dpy
->config
)
1021 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1022 "vblank_mode", &vblank_mode
);
1023 switch (vblank_mode
) {
1024 case DRI_CONF_VBLANK_NEVER
:
1025 dri2_dpy
->min_swap_interval
= 0;
1026 dri2_dpy
->max_swap_interval
= 0;
1027 dri2_dpy
->default_swap_interval
= 0;
1029 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1030 dri2_dpy
->min_swap_interval
= 1;
1031 dri2_dpy
->max_swap_interval
= 1;
1032 dri2_dpy
->default_swap_interval
= 1;
1034 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1035 dri2_dpy
->min_swap_interval
= 0;
1036 dri2_dpy
->max_swap_interval
= 1;
1037 dri2_dpy
->default_swap_interval
= 0;
1040 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1041 dri2_dpy
->min_swap_interval
= 0;
1042 dri2_dpy
->max_swap_interval
= 1;
1043 dri2_dpy
->default_swap_interval
= 1;
1048 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1049 .authenticate
= dri2_wl_authenticate
,
1050 .create_window_surface
= dri2_wl_create_window_surface
,
1051 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1052 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1053 .destroy_surface
= dri2_wl_destroy_surface
,
1054 .create_image
= dri2_create_image_khr
,
1055 .swap_interval
= dri2_wl_swap_interval
,
1056 .swap_buffers
= dri2_wl_swap_buffers
,
1057 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1058 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1059 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1060 .copy_buffers
= dri2_fallback_copy_buffers
,
1061 .query_buffer_age
= dri2_wl_query_buffer_age
,
1062 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1063 .get_sync_values
= dri2_fallback_get_sync_values
,
1064 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1067 static const __DRIextension
*dri2_loader_extensions
[] = {
1068 &dri2_loader_extension
.base
,
1069 &image_loader_extension
.base
,
1070 &image_lookup_extension
.base
,
1071 &use_invalidate
.base
,
1075 static const __DRIextension
*image_loader_extensions
[] = {
1076 &image_loader_extension
.base
,
1077 &image_lookup_extension
.base
,
1078 &use_invalidate
.base
,
1083 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1085 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1086 static const struct {
1087 const char *format_name
;
1089 unsigned int rgba_masks
[4];
1091 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1092 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1093 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1095 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1096 unsigned int count
, i
, j
;
1099 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1100 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1101 struct dri2_egl_config
*dri2_conf
;
1103 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1106 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1107 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1115 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1116 if (!format_count
[i
]) {
1117 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1118 visuals
[i
].format_name
);
1122 return (count
!= 0);
1126 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1128 struct dri2_egl_display
*dri2_dpy
;
1130 loader_set_logger(_eglLog
);
1132 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1134 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1136 disp
->DriverData
= (void *) dri2_dpy
;
1137 if (disp
->PlatformDisplay
== NULL
) {
1138 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1139 if (dri2_dpy
->wl_dpy
== NULL
)
1141 dri2_dpy
->own_device
= 1;
1143 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1146 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1148 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1149 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1150 goto cleanup_dpy_wrapper
;
1152 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1153 dri2_dpy
->wl_queue
);
1155 if (dri2_dpy
->own_device
)
1156 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1158 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1159 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1160 ®istry_listener_drm
, dri2_dpy
);
1161 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1162 goto cleanup_registry
;
1164 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1167 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1170 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1171 &dri2_dpy
->is_different_gpu
);
1172 if (dri2_dpy
->is_different_gpu
) {
1173 free(dri2_dpy
->device_name
);
1174 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1175 if (!dri2_dpy
->device_name
) {
1176 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1177 "for requested GPU");
1182 /* we have to do the check now, because loader_get_user_preferred_fd
1183 * will return a render-node when the requested gpu is different
1184 * to the server, but also if the client asks for the same gpu than
1185 * the server by requesting its pci-id */
1186 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1188 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1189 if (dri2_dpy
->driver_name
== NULL
) {
1190 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1194 if (!dri2_load_driver(disp
))
1195 goto cleanup_driver_name
;
1197 /* render nodes cannot use Gem names, and thus do not support
1198 * the __DRI_DRI2_LOADER extension */
1199 if (!dri2_dpy
->is_render_node
)
1200 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1202 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1204 if (!dri2_create_screen(disp
))
1205 goto cleanup_driver
;
1207 dri2_wl_setup_swap_interval(dri2_dpy
);
1209 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1210 * createImageFromFds support indicates that Prime export/import
1211 * is supported by the driver. Fall back to
1212 * gem names if we don't have Prime support. */
1214 if (dri2_dpy
->image
->base
.version
< 7 ||
1215 dri2_dpy
->image
->createImageFromFds
== NULL
)
1216 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1218 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1219 * The server needs to accept them */
1220 if (dri2_dpy
->is_render_node
&&
1221 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1222 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1223 goto cleanup_screen
;
1226 if (dri2_dpy
->is_different_gpu
&&
1227 (dri2_dpy
->image
->base
.version
< 9 ||
1228 dri2_dpy
->image
->blitImage
== NULL
)) {
1229 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1230 "Image extension in the driver is not "
1231 "compatible. Version 9 or later and blitImage() "
1233 goto cleanup_screen
;
1236 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1237 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1238 goto cleanup_screen
;
1241 dri2_set_WL_bind_wayland_display(drv
, disp
);
1242 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1243 * because the buffer of the EGLImage has likely a tiling mode the server
1244 * gpu won't support. These is no way to check for now. Thus do not support the
1246 if (!dri2_dpy
->is_different_gpu
) {
1247 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1249 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1250 dri2_fallback_create_wayland_buffer_from_image
;
1252 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1254 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1256 /* Fill vtbl last to prevent accidentally calling virtual function during
1259 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1264 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1266 dlclose(dri2_dpy
->driver
);
1267 cleanup_driver_name
:
1268 free(dri2_dpy
->driver_name
);
1270 close(dri2_dpy
->fd
);
1272 free(dri2_dpy
->device_name
);
1273 wl_drm_destroy(dri2_dpy
->wl_drm
);
1275 wl_registry_destroy(dri2_dpy
->wl_registry
);
1276 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1277 cleanup_dpy_wrapper
:
1278 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1279 if (disp
->PlatformDisplay
== NULL
)
1280 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1283 disp
->DriverData
= NULL
;
1289 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1291 if (format
== WL_SHM_FORMAT_RGB565
)
1293 else /* ARGB8888 || XRGB8888 */
1298 * Taken from weston shared/os-compatibility.c
1301 #ifndef HAVE_MKOSTEMP
1304 set_cloexec_or_close(int fd
)
1311 flags
= fcntl(fd
, F_GETFD
);
1315 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1328 * Taken from weston shared/os-compatibility.c
1332 create_tmpfile_cloexec(char *tmpname
)
1336 #ifdef HAVE_MKOSTEMP
1337 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1341 fd
= mkstemp(tmpname
);
1343 fd
= set_cloexec_or_close(fd
);
1352 * Taken from weston shared/os-compatibility.c
1354 * Create a new, unique, anonymous file of the given size, and
1355 * return the file descriptor for it. The file descriptor is set
1356 * CLOEXEC. The file is immediately suitable for mmap()'ing
1357 * the given size at offset zero.
1359 * The file should not have a permanent backing store like a disk,
1360 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1362 * The file name is deleted from the file system.
1364 * The file is suitable for buffer sharing between processes by
1365 * transmitting the file descriptor over Unix sockets using the
1366 * SCM_RIGHTS methods.
1368 * If the C library implements posix_fallocate(), it is used to
1369 * guarantee that disk space is available for the file at the
1370 * given size. If disk space is insufficent, errno is set to ENOSPC.
1371 * If posix_fallocate() is not supported, program may receive
1372 * SIGBUS on accessing mmap()'ed file contents instead.
1375 os_create_anonymous_file(off_t size
)
1377 static const char template[] = "/mesa-shared-XXXXXX";
1383 path
= getenv("XDG_RUNTIME_DIR");
1389 name
= malloc(strlen(path
) + sizeof(template));
1394 strcat(name
, template);
1396 fd
= create_tmpfile_cloexec(name
);
1403 ret
= ftruncate(fd
, size
);
1414 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1415 int format
, int w
, int h
,
1416 void **data
, int *size
,
1417 struct wl_buffer
**buffer
)
1419 struct wl_shm_pool
*pool
;
1420 int fd
, stride
, size_map
;
1423 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1424 size_map
= h
* stride
;
1426 /* Create a sharable buffer */
1427 fd
= os_create_anonymous_file(size_map
);
1431 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1432 if (data_map
== MAP_FAILED
) {
1437 /* Share it in a wl_buffer */
1438 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1439 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1440 wl_shm_pool_destroy(pool
);
1449 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1451 struct dri2_egl_display
*dri2_dpy
=
1452 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1455 /* we need to do the following operations only once per frame */
1456 if (dri2_surf
->back
)
1459 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1460 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1462 dri2_wl_release_buffers(dri2_surf
);
1464 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1465 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1466 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1467 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1468 dri2_surf
->current
= NULL
;
1471 /* find back buffer */
1473 /* There might be a buffer release already queued that wasn't processed */
1474 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
1476 /* try get free buffer already created */
1477 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1478 if (!dri2_surf
->color_buffers
[i
].locked
&&
1479 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1480 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1485 /* else choose any another free location */
1486 if (!dri2_surf
->back
) {
1487 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1488 if (!dri2_surf
->color_buffers
[i
].locked
) {
1489 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1490 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1492 dri2_surf
->base
.Width
,
1493 dri2_surf
->base
.Height
,
1494 &dri2_surf
->back
->data
,
1495 &dri2_surf
->back
->data_size
,
1496 &dri2_surf
->back
->wl_buffer
)) {
1497 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1500 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1501 dri2_dpy
->wl_queue
);
1502 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1503 &wl_buffer_listener
, dri2_surf
);
1509 if (!dri2_surf
->back
) {
1510 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1514 dri2_surf
->back
->locked
= 1;
1516 /* If we have an extra unlocked buffer at this point, we had to do triple
1517 * buffering for a while, but now can go back to just double buffering.
1518 * That means we can free any unlocked buffer now. */
1519 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1520 if (!dri2_surf
->color_buffers
[i
].locked
&&
1521 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1522 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1523 munmap(dri2_surf
->color_buffers
[i
].data
,
1524 dri2_surf
->color_buffers
[i
].data_size
);
1525 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1526 dri2_surf
->color_buffers
[i
].data
= NULL
;
1534 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1536 /* if there has been a resize: */
1537 if (!dri2_surf
->current
)
1540 return dri2_surf
->current
->data
;
1544 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1546 assert(dri2_surf
->back
);
1547 return dri2_surf
->back
->data
;
1551 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1553 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1555 while (dri2_surf
->throttle_callback
!= NULL
)
1556 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1557 dri2_dpy
->wl_queue
) == -1)
1560 if (dri2_surf
->base
.SwapInterval
> 0) {
1561 dri2_surf
->throttle_callback
=
1562 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1563 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1564 &throttle_listener
, dri2_surf
);
1565 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1566 dri2_dpy
->wl_queue
);
1569 dri2_surf
->current
= dri2_surf
->back
;
1570 dri2_surf
->back
= NULL
;
1572 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1573 dri2_surf
->current
->wl_buffer
,
1574 dri2_surf
->dx
, dri2_surf
->dy
);
1576 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1577 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1578 /* reset resize growing parameters */
1582 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1583 0, 0, INT32_MAX
, INT32_MAX
);
1584 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1586 /* If we're not waiting for a frame callback then we'll at least throttle
1587 * to a sync callback so that we always give a chance for the compositor to
1588 * handle the commit and send a release event before checking for a free
1590 if (dri2_surf
->throttle_callback
== NULL
) {
1591 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1592 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1593 &throttle_listener
, dri2_surf
);
1596 wl_display_flush(dri2_dpy
->wl_dpy
);
1600 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1601 int *x
, int *y
, int *w
, int *h
,
1602 void *loaderPrivate
)
1604 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1606 (void) swrast_update_buffers(dri2_surf
);
1609 *w
= dri2_surf
->base
.Width
;
1610 *h
= dri2_surf
->base
.Height
;
1614 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1615 int x
, int y
, int w
, int h
,
1616 char *data
, void *loaderPrivate
)
1618 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1619 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1620 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1621 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1622 int dst_stride
= copy_width
;
1625 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1627 memset(data
, 0, copy_width
* h
);
1631 assert(data
!= src
);
1632 assert(copy_width
<= src_stride
);
1635 src
+= y
* src_stride
;
1638 if (copy_width
> src_stride
-x_offset
)
1639 copy_width
= src_stride
-x_offset
;
1640 if (h
> dri2_surf
->base
.Height
-y
)
1641 h
= dri2_surf
->base
.Height
-y
;
1644 memcpy(dst
, src
, copy_width
);
1651 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1652 int x
, int y
, int w
, int h
, int stride
,
1653 char *data
, void *loaderPrivate
)
1655 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1656 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1657 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1658 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1661 assert(copy_width
<= stride
);
1663 (void) swrast_update_buffers(dri2_surf
);
1664 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1666 /* partial copy, copy old content */
1667 if (copy_width
< dst_stride
)
1668 dri2_wl_swrast_get_image(draw
, 0, 0,
1669 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1670 dst
, loaderPrivate
);
1673 dst
+= y
* dst_stride
;
1677 /* drivers expect we do these checks (and some rely on it) */
1678 if (copy_width
> dst_stride
-x_offset
)
1679 copy_width
= dst_stride
-x_offset
;
1680 if (h
> dri2_surf
->base
.Height
-y
)
1681 h
= dri2_surf
->base
.Height
-y
;
1684 memcpy(dst
, src
, copy_width
);
1688 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1692 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1693 int x
, int y
, int w
, int h
,
1694 char *data
, void *loaderPrivate
)
1696 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1699 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1700 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1701 stride
, data
, loaderPrivate
);
1705 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1707 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1708 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1710 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1715 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1717 struct dri2_egl_display
*dri2_dpy
= data
;
1720 case WL_SHM_FORMAT_ARGB8888
:
1721 dri2_dpy
->formats
|= HAS_ARGB8888
;
1723 case WL_SHM_FORMAT_XRGB8888
:
1724 dri2_dpy
->formats
|= HAS_XRGB8888
;
1726 case WL_SHM_FORMAT_RGB565
:
1727 dri2_dpy
->formats
|= HAS_RGB565
;
1732 static const struct wl_shm_listener shm_listener
= {
1733 .format
= shm_handle_format
1737 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1738 const char *interface
, uint32_t version
)
1740 struct dri2_egl_display
*dri2_dpy
= data
;
1742 if (strcmp(interface
, "wl_shm") == 0) {
1744 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1745 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1749 static const struct wl_registry_listener registry_listener_swrast
= {
1750 .global
= registry_handle_global_swrast
,
1751 .global_remove
= registry_handle_global_remove
1754 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1755 .authenticate
= NULL
,
1756 .create_window_surface
= dri2_wl_create_window_surface
,
1757 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1758 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1759 .destroy_surface
= dri2_wl_destroy_surface
,
1760 .create_image
= dri2_fallback_create_image_khr
,
1761 .swap_interval
= dri2_wl_swap_interval
,
1762 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1763 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1764 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1765 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1766 .copy_buffers
= dri2_fallback_copy_buffers
,
1767 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1768 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1769 .get_sync_values
= dri2_fallback_get_sync_values
,
1770 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1773 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1774 .base
= { __DRI_SWRAST_LOADER
, 2 },
1776 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1777 .putImage
= dri2_wl_swrast_put_image
,
1778 .getImage
= dri2_wl_swrast_get_image
,
1779 .putImage2
= dri2_wl_swrast_put_image2
,
1782 static const __DRIextension
*swrast_loader_extensions
[] = {
1783 &swrast_loader_extension
.base
,
1788 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1790 struct dri2_egl_display
*dri2_dpy
;
1792 loader_set_logger(_eglLog
);
1794 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1796 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1798 disp
->DriverData
= (void *) dri2_dpy
;
1799 if (disp
->PlatformDisplay
== NULL
) {
1800 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1801 if (dri2_dpy
->wl_dpy
== NULL
)
1803 dri2_dpy
->own_device
= 1;
1805 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1808 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1810 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1811 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1812 goto cleanup_dpy_wrapper
;
1814 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1815 dri2_dpy
->wl_queue
);
1817 if (dri2_dpy
->own_device
)
1818 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1820 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1821 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1822 ®istry_listener_swrast
, dri2_dpy
);
1824 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1825 goto cleanup_registry
;
1827 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1831 dri2_dpy
->driver_name
= strdup("swrast");
1832 if (!dri2_load_driver_swrast(disp
))
1835 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1837 if (!dri2_create_screen(disp
))
1838 goto cleanup_driver
;
1840 dri2_wl_setup_swap_interval(dri2_dpy
);
1842 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1843 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1844 goto cleanup_screen
;
1847 /* Fill vtbl last to prevent accidentally calling virtual function during
1850 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1855 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1857 dlclose(dri2_dpy
->driver
);
1859 wl_shm_destroy(dri2_dpy
->wl_shm
);
1861 wl_registry_destroy(dri2_dpy
->wl_registry
);
1862 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1863 cleanup_dpy_wrapper
:
1864 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1865 if (disp
->PlatformDisplay
== NULL
)
1866 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1869 disp
->DriverData
= NULL
;
1875 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1877 EGLBoolean initialized
= EGL_TRUE
;
1879 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1882 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1883 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1886 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);