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_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
132 _EGLConfig
*conf
, void *native_window
,
133 const EGLint
*attrib_list
)
135 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
136 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
137 struct wl_egl_window
*window
= native_window
;
138 struct dri2_egl_surface
*dri2_surf
;
139 const __DRIconfig
*config
;
143 dri2_surf
= calloc(1, sizeof *dri2_surf
);
145 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
149 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
152 if (conf
->RedSize
== 5)
153 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
154 else if (conf
->AlphaSize
== 0)
155 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
157 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
160 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
164 dri2_surf
->wl_win
= window
;
166 dri2_surf
->wl_win
->private = dri2_surf
;
167 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
168 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
170 dri2_surf
->base
.Width
= -1;
171 dri2_surf
->base
.Height
= -1;
173 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
174 dri2_surf
->base
.GLColorspace
);
176 dri2_surf
->dri_drawable
=
177 dri2_dpy
->dri2
->createNewDrawable(dri2_dpy
->dri_screen
, config
,
179 if (dri2_surf
->dri_drawable
== NULL
) {
180 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
184 return &dri2_surf
->base
;
193 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
196 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
197 _EGLConfig
*conf
, void *native_window
,
198 const EGLint
*attrib_list
)
200 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
203 surf
= dri2_wl_create_surface(drv
, disp
, conf
, native_window
, attrib_list
);
206 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
212 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
213 _EGLConfig
*conf
, void *native_window
,
214 const EGLint
*attrib_list
)
216 /* From the EGL_EXT_platform_wayland spec, version 3:
218 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
219 * that belongs to Wayland. Any such call fails and generates
222 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
228 * Called via eglDestroySurface(), drv->API.DestroySurface().
231 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
233 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
234 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
239 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
241 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
242 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
243 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
244 if (dri2_surf
->color_buffers
[i
].dri_image
)
245 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
246 if (dri2_surf
->color_buffers
[i
].linear_copy
)
247 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
248 if (dri2_surf
->color_buffers
[i
].data
)
249 munmap(dri2_surf
->color_buffers
[i
].data
,
250 dri2_surf
->color_buffers
[i
].data_size
);
253 if (dri2_dpy
->dri2
) {
254 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
255 if (dri2_surf
->dri_buffers
[i
] &&
256 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
257 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
258 dri2_surf
->dri_buffers
[i
]);
261 if (dri2_surf
->throttle_callback
)
262 wl_callback_destroy(dri2_surf
->throttle_callback
);
264 if (dri2_surf
->wl_win
) {
265 dri2_surf
->wl_win
->private = NULL
;
266 dri2_surf
->wl_win
->resize_callback
= NULL
;
267 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
276 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
278 struct dri2_egl_display
*dri2_dpy
=
279 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
282 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
283 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
284 !dri2_surf
->color_buffers
[i
].locked
)
285 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
286 if (dri2_surf
->color_buffers
[i
].dri_image
)
287 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
288 if (dri2_surf
->color_buffers
[i
].linear_copy
)
289 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
290 if (dri2_surf
->color_buffers
[i
].data
)
291 munmap(dri2_surf
->color_buffers
[i
].data
,
292 dri2_surf
->color_buffers
[i
].data_size
);
294 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
295 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
296 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
297 dri2_surf
->color_buffers
[i
].data
= NULL
;
298 dri2_surf
->color_buffers
[i
].locked
= 0;
301 if (dri2_dpy
->dri2
) {
302 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
303 if (dri2_surf
->dri_buffers
[i
] &&
304 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
305 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
306 dri2_surf
->dri_buffers
[i
]);
311 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
313 struct dri2_egl_display
*dri2_dpy
=
314 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
316 unsigned int dri_image_format
;
318 /* currently supports three WL DRM formats,
319 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
320 * and WL_DRM_FORMAT_RGB565
322 switch (dri2_surf
->format
) {
323 case WL_DRM_FORMAT_ARGB8888
:
324 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
326 case WL_DRM_FORMAT_XRGB8888
:
327 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
329 case WL_DRM_FORMAT_RGB565
:
330 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
333 /* format is not supported */
337 /* There might be a buffer release already queued that wasn't processed */
338 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
340 if (dri2_surf
->back
== NULL
) {
341 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
342 /* Get an unlocked buffer, preferrably one with a dri_buffer
343 * already allocated. */
344 if (dri2_surf
->color_buffers
[i
].locked
)
346 if (dri2_surf
->back
== NULL
)
347 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
348 else if (dri2_surf
->back
->dri_image
== NULL
)
349 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
353 if (dri2_surf
->back
== NULL
)
356 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
358 if (dri2_dpy
->is_different_gpu
&&
359 dri2_surf
->back
->linear_copy
== NULL
) {
360 dri2_surf
->back
->linear_copy
=
361 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
362 dri2_surf
->base
.Width
,
363 dri2_surf
->base
.Height
,
366 __DRI_IMAGE_USE_LINEAR
,
368 if (dri2_surf
->back
->linear_copy
== NULL
)
372 if (dri2_surf
->back
->dri_image
== NULL
) {
373 dri2_surf
->back
->dri_image
=
374 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
375 dri2_surf
->base
.Width
,
376 dri2_surf
->base
.Height
,
378 dri2_dpy
->is_different_gpu
?
381 dri2_surf
->back
->age
= 0;
383 if (dri2_surf
->back
->dri_image
== NULL
)
386 dri2_surf
->back
->locked
= 1;
393 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
395 struct dri2_egl_display
*dri2_dpy
=
396 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
400 image
= dri2_surf
->back
->dri_image
;
402 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
403 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
405 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
407 buffer
->pitch
= pitch
;
413 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
414 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
416 struct dri2_egl_display
*dri2_dpy
=
417 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
418 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
421 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
423 dri2_surf
->base
.Width
,
424 dri2_surf
->base
.Height
);
425 dri2_surf
->dri_buffers
[attachment
] = b
;
430 memcpy(buffer
, b
, sizeof *buffer
);
436 update_buffers(struct dri2_egl_surface
*dri2_surf
)
438 struct dri2_egl_display
*dri2_dpy
=
439 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
442 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
443 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
445 dri2_wl_release_buffers(dri2_surf
);
447 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
448 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
449 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
450 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
453 if (get_back_bo(dri2_surf
) < 0) {
454 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
458 /* If we have an extra unlocked buffer at this point, we had to do triple
459 * buffering for a while, but now can go back to just double buffering.
460 * That means we can free any unlocked buffer now. */
461 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
462 if (!dri2_surf
->color_buffers
[i
].locked
&&
463 dri2_surf
->color_buffers
[i
].wl_buffer
) {
464 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
465 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
466 if (dri2_dpy
->is_different_gpu
)
467 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
468 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
469 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
470 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
478 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
479 int *width
, int *height
,
480 unsigned int *attachments
, int count
,
481 int *out_count
, void *loaderPrivate
)
483 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
486 if (update_buffers(dri2_surf
) < 0)
489 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
490 switch (attachments
[i
]) {
491 case __DRI_BUFFER_BACK_LEFT
:
492 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
495 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
496 &dri2_surf
->buffers
[j
]) < 0) {
497 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
508 *width
= dri2_surf
->base
.Width
;
509 *height
= dri2_surf
->base
.Height
;
511 return dri2_surf
->buffers
;
515 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
516 int *width
, int *height
,
517 unsigned int *attachments
, int count
,
518 int *out_count
, void *loaderPrivate
)
520 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
521 unsigned int *attachments_with_format
;
527 switch (dri2_surf
->format
) {
528 case WL_DRM_FORMAT_ARGB8888
:
529 case WL_DRM_FORMAT_XRGB8888
:
532 case WL_DRM_FORMAT_RGB565
:
536 /* format is not supported */
540 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
541 if (!attachments_with_format
) {
546 for (i
= 0; i
< count
; ++i
) {
547 attachments_with_format
[2*i
] = attachments
[i
];
548 attachments_with_format
[2*i
+ 1] = bpp
;
552 dri2_wl_get_buffers_with_format(driDrawable
,
554 attachments_with_format
, count
,
555 out_count
, loaderPrivate
);
557 free(attachments_with_format
);
563 image_get_buffers(__DRIdrawable
*driDrawable
,
567 uint32_t buffer_mask
,
568 struct __DRIimageList
*buffers
)
570 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
572 if (update_buffers(dri2_surf
) < 0)
575 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
576 buffers
->back
= dri2_surf
->back
->dri_image
;
582 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
585 (void) loaderPrivate
;
588 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
589 .base
= { __DRI_DRI2_LOADER
, 3 },
591 .getBuffers
= dri2_wl_get_buffers
,
592 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
593 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
596 static const __DRIimageLoaderExtension image_loader_extension
= {
597 .base
= { __DRI_IMAGE_LOADER
, 1 },
599 .getBuffers
= image_get_buffers
,
600 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
604 wayland_throttle_callback(void *data
,
605 struct wl_callback
*callback
,
608 struct dri2_egl_surface
*dri2_surf
= data
;
610 dri2_surf
->throttle_callback
= NULL
;
611 wl_callback_destroy(callback
);
614 static const struct wl_callback_listener throttle_listener
= {
615 .done
= wayland_throttle_callback
619 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
621 struct dri2_egl_display
*dri2_dpy
=
622 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
624 int fd
, stride
, name
;
626 if (dri2_surf
->current
->wl_buffer
!= NULL
)
629 if (dri2_dpy
->is_different_gpu
) {
630 image
= dri2_surf
->current
->linear_copy
;
632 image
= dri2_surf
->current
->dri_image
;
634 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
635 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
636 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
638 dri2_surf
->current
->wl_buffer
=
639 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
641 dri2_surf
->base
.Width
,
642 dri2_surf
->base
.Height
,
649 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
650 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
652 dri2_surf
->current
->wl_buffer
=
653 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
655 dri2_surf
->base
.Width
,
656 dri2_surf
->base
.Height
,
661 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
663 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
664 &wl_buffer_listener
, dri2_surf
);
668 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
672 /* The WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION macro and
673 * wl_proxy_get_version() were both introduced in wayland 1.10.
674 * Instead of bumping our wayland dependency we just make this
675 * function conditional on the required 1.10 features, falling
676 * back to old (correct but suboptimal) behaviour for older
679 #ifdef WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
682 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_win
->surface
)
683 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
686 for (i
= 0; i
< n_rects
; i
++) {
687 const int *rect
= &rects
[i
* 4];
689 wl_surface_damage_buffer(dri2_surf
->wl_win
->surface
,
691 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
699 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
702 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
708 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
709 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
712 while (dri2_surf
->throttle_callback
!= NULL
)
713 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
714 dri2_dpy
->wl_queue
) == -1)
717 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
718 if (dri2_surf
->color_buffers
[i
].age
> 0)
719 dri2_surf
->color_buffers
[i
].age
++;
721 /* Make sure we have a back buffer in case we're swapping without ever
723 if (get_back_bo(dri2_surf
) < 0) {
724 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
728 if (draw
->SwapInterval
> 0) {
729 dri2_surf
->throttle_callback
=
730 wl_surface_frame(dri2_surf
->wl_win
->surface
);
731 wl_callback_add_listener(dri2_surf
->throttle_callback
,
732 &throttle_listener
, dri2_surf
);
733 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
737 dri2_surf
->back
->age
= 1;
738 dri2_surf
->current
= dri2_surf
->back
;
739 dri2_surf
->back
= NULL
;
741 create_wl_buffer(dri2_surf
);
743 wl_surface_attach(dri2_surf
->wl_win
->surface
,
744 dri2_surf
->current
->wl_buffer
,
745 dri2_surf
->dx
, dri2_surf
->dy
);
747 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
748 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
749 /* reset resize growing parameters */
753 /* If the compositor doesn't support damage_buffer, we deliberately
754 * ignore the damage region and post maximum damage, due to
755 * https://bugs.freedesktop.org/78190 */
756 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
757 wl_surface_damage(dri2_surf
->wl_win
->surface
,
758 0, 0, INT32_MAX
, INT32_MAX
);
760 if (dri2_dpy
->is_different_gpu
) {
761 _EGLContext
*ctx
= _eglGetCurrentContext();
762 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
763 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
764 dri2_surf
->current
->linear_copy
,
765 dri2_surf
->current
->dri_image
,
766 0, 0, dri2_surf
->base
.Width
,
767 dri2_surf
->base
.Height
,
768 0, 0, dri2_surf
->base
.Width
,
769 dri2_surf
->base
.Height
, 0);
772 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
773 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
775 wl_surface_commit(dri2_surf
->wl_win
->surface
);
777 /* If we're not waiting for a frame callback then we'll at least throttle
778 * to a sync callback so that we always give a chance for the compositor to
779 * handle the commit and send a release event before checking for a free
781 if (dri2_surf
->throttle_callback
== NULL
) {
782 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
783 wl_callback_add_listener(dri2_surf
->throttle_callback
,
784 &throttle_listener
, dri2_surf
);
787 wl_display_flush(dri2_dpy
->wl_dpy
);
793 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
794 _EGLDisplay
*disp
, _EGLSurface
*surface
)
796 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
798 if (get_back_bo(dri2_surf
) < 0) {
799 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
803 return dri2_surf
->back
->age
;
807 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
809 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
812 static struct wl_buffer
*
813 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
817 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
818 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
819 __DRIimage
*image
= dri2_img
->dri_image
;
820 struct wl_buffer
*buffer
;
821 int width
, height
, format
, pitch
;
822 enum wl_drm_format wl_format
;
824 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
827 case __DRI_IMAGE_FORMAT_ARGB8888
:
828 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
830 wl_format
= WL_DRM_FORMAT_ARGB8888
;
832 case __DRI_IMAGE_FORMAT_XRGB8888
:
833 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
835 wl_format
= WL_DRM_FORMAT_XRGB8888
;
841 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
842 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
843 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
845 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
848 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
851 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
863 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
866 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
873 /* The buffer object will have been created with our internal event queue
874 * because it is using the wl_drm object as a proxy factory. We want the
875 * buffer to be used by the application so we'll reset it to the display's
876 * default event queue */
878 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
883 _eglError(EGL_BAD_MATCH
, "unsupported image format");
888 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
890 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
893 if (dri2_dpy
->is_render_node
) {
894 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
895 "authenticate for render-nodes");
898 dri2_dpy
->authenticated
= 0;
900 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
901 if (roundtrip(dri2_dpy
) < 0)
904 if (!dri2_dpy
->authenticated
)
907 /* reset authenticated */
908 dri2_dpy
->authenticated
= 1;
914 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
916 struct dri2_egl_display
*dri2_dpy
= data
;
919 dri2_dpy
->device_name
= strdup(device
);
920 if (!dri2_dpy
->device_name
)
923 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
924 if (dri2_dpy
->fd
== -1) {
925 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
926 dri2_dpy
->device_name
, strerror(errno
));
930 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
931 dri2_dpy
->authenticated
= 1;
933 drmGetMagic(dri2_dpy
->fd
, &magic
);
934 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
939 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
941 struct dri2_egl_display
*dri2_dpy
= data
;
944 case WL_DRM_FORMAT_ARGB8888
:
945 dri2_dpy
->formats
|= HAS_ARGB8888
;
947 case WL_DRM_FORMAT_XRGB8888
:
948 dri2_dpy
->formats
|= HAS_XRGB8888
;
950 case WL_DRM_FORMAT_RGB565
:
951 dri2_dpy
->formats
|= HAS_RGB565
;
957 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
959 struct dri2_egl_display
*dri2_dpy
= data
;
961 dri2_dpy
->capabilities
= value
;
965 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
967 struct dri2_egl_display
*dri2_dpy
= data
;
969 dri2_dpy
->authenticated
= 1;
972 static const struct wl_drm_listener drm_listener
= {
973 .device
= drm_handle_device
,
974 .format
= drm_handle_format
,
975 .authenticated
= drm_handle_authenticated
,
976 .capabilities
= drm_handle_capabilities
980 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
981 const char *interface
, uint32_t version
)
983 struct dri2_egl_display
*dri2_dpy
= data
;
987 if (strcmp(interface
, "wl_drm") == 0) {
989 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
990 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
995 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1000 static const struct wl_registry_listener registry_listener_drm
= {
1001 .global
= registry_handle_global_drm
,
1002 .global_remove
= registry_handle_global_remove
1006 dri2_wl_swap_interval(_EGLDriver
*drv
,
1011 if (interval
> surf
->Config
->MaxSwapInterval
)
1012 interval
= surf
->Config
->MaxSwapInterval
;
1013 else if (interval
< surf
->Config
->MinSwapInterval
)
1014 interval
= surf
->Config
->MinSwapInterval
;
1016 surf
->SwapInterval
= interval
;
1022 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1024 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1026 /* We can't use values greater than 1 on Wayland because we are using the
1027 * frame callback to synchronise the frame and the only way we be sure to
1028 * get a frame callback is to attach a new buffer. Therefore we can't just
1029 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1031 if (dri2_dpy
->config
)
1032 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1033 "vblank_mode", &vblank_mode
);
1034 switch (vblank_mode
) {
1035 case DRI_CONF_VBLANK_NEVER
:
1036 dri2_dpy
->min_swap_interval
= 0;
1037 dri2_dpy
->max_swap_interval
= 0;
1038 dri2_dpy
->default_swap_interval
= 0;
1040 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1041 dri2_dpy
->min_swap_interval
= 1;
1042 dri2_dpy
->max_swap_interval
= 1;
1043 dri2_dpy
->default_swap_interval
= 1;
1045 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1046 dri2_dpy
->min_swap_interval
= 0;
1047 dri2_dpy
->max_swap_interval
= 1;
1048 dri2_dpy
->default_swap_interval
= 0;
1051 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1052 dri2_dpy
->min_swap_interval
= 0;
1053 dri2_dpy
->max_swap_interval
= 1;
1054 dri2_dpy
->default_swap_interval
= 1;
1059 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1060 .authenticate
= dri2_wl_authenticate
,
1061 .create_window_surface
= dri2_wl_create_window_surface
,
1062 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1063 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1064 .destroy_surface
= dri2_wl_destroy_surface
,
1065 .create_image
= dri2_create_image_khr
,
1066 .swap_interval
= dri2_wl_swap_interval
,
1067 .swap_buffers
= dri2_wl_swap_buffers
,
1068 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1069 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1070 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1071 .copy_buffers
= dri2_fallback_copy_buffers
,
1072 .query_buffer_age
= dri2_wl_query_buffer_age
,
1073 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1074 .get_sync_values
= dri2_fallback_get_sync_values
,
1075 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1078 static const __DRIextension
*dri2_loader_extensions
[] = {
1079 &dri2_loader_extension
.base
,
1080 &image_loader_extension
.base
,
1081 &image_lookup_extension
.base
,
1082 &use_invalidate
.base
,
1086 static const __DRIextension
*image_loader_extensions
[] = {
1087 &image_loader_extension
.base
,
1088 &image_lookup_extension
.base
,
1089 &use_invalidate
.base
,
1094 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1096 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1097 static const struct {
1098 const char *format_name
;
1100 unsigned int rgba_masks
[4];
1102 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1103 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1104 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1106 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1107 unsigned int count
, i
, j
;
1110 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1111 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1112 struct dri2_egl_config
*dri2_conf
;
1114 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1117 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1118 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1126 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1127 if (!format_count
[i
]) {
1128 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1129 visuals
[i
].format_name
);
1133 return (count
!= 0);
1137 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1139 struct dri2_egl_display
*dri2_dpy
;
1141 loader_set_logger(_eglLog
);
1143 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1145 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1147 disp
->DriverData
= (void *) dri2_dpy
;
1148 if (disp
->PlatformDisplay
== NULL
) {
1149 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1150 if (dri2_dpy
->wl_dpy
== NULL
)
1152 dri2_dpy
->own_device
= 1;
1154 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1157 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1159 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1160 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1161 goto cleanup_dpy_wrapper
;
1163 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1164 dri2_dpy
->wl_queue
);
1166 if (dri2_dpy
->own_device
)
1167 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1169 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1170 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1171 ®istry_listener_drm
, dri2_dpy
);
1172 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1173 goto cleanup_registry
;
1175 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1178 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1181 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1182 &dri2_dpy
->is_different_gpu
);
1183 if (dri2_dpy
->is_different_gpu
) {
1184 free(dri2_dpy
->device_name
);
1185 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1186 if (!dri2_dpy
->device_name
) {
1187 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1188 "for requested GPU");
1193 /* we have to do the check now, because loader_get_user_preferred_fd
1194 * will return a render-node when the requested gpu is different
1195 * to the server, but also if the client asks for the same gpu than
1196 * the server by requesting its pci-id */
1197 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1199 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1200 if (dri2_dpy
->driver_name
== NULL
) {
1201 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1205 if (!dri2_load_driver(disp
))
1206 goto cleanup_driver_name
;
1208 /* render nodes cannot use Gem names, and thus do not support
1209 * the __DRI_DRI2_LOADER extension */
1210 if (!dri2_dpy
->is_render_node
)
1211 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1213 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1215 if (!dri2_create_screen(disp
))
1216 goto cleanup_driver
;
1218 dri2_wl_setup_swap_interval(dri2_dpy
);
1220 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1221 * createImageFromFds support indicates that Prime export/import
1222 * is supported by the driver. Fall back to
1223 * gem names if we don't have Prime support. */
1225 if (dri2_dpy
->image
->base
.version
< 7 ||
1226 dri2_dpy
->image
->createImageFromFds
== NULL
)
1227 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1229 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1230 * The server needs to accept them */
1231 if (dri2_dpy
->is_render_node
&&
1232 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1233 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1234 goto cleanup_screen
;
1237 if (dri2_dpy
->is_different_gpu
&&
1238 (dri2_dpy
->image
->base
.version
< 9 ||
1239 dri2_dpy
->image
->blitImage
== NULL
)) {
1240 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1241 "Image extension in the driver is not "
1242 "compatible. Version 9 or later and blitImage() "
1244 goto cleanup_screen
;
1247 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1248 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1249 goto cleanup_screen
;
1252 dri2_set_WL_bind_wayland_display(drv
, disp
);
1253 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1254 * because the buffer of the EGLImage has likely a tiling mode the server
1255 * gpu won't support. These is no way to check for now. Thus do not support the
1257 if (!dri2_dpy
->is_different_gpu
) {
1258 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1260 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1261 dri2_fallback_create_wayland_buffer_from_image
;
1263 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1265 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1267 /* Fill vtbl last to prevent accidentally calling virtual function during
1270 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1275 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1277 dlclose(dri2_dpy
->driver
);
1278 cleanup_driver_name
:
1279 free(dri2_dpy
->driver_name
);
1281 close(dri2_dpy
->fd
);
1283 free(dri2_dpy
->device_name
);
1284 wl_drm_destroy(dri2_dpy
->wl_drm
);
1286 wl_registry_destroy(dri2_dpy
->wl_registry
);
1287 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1288 cleanup_dpy_wrapper
:
1289 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1290 if (disp
->PlatformDisplay
== NULL
)
1291 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1294 disp
->DriverData
= NULL
;
1300 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1302 if (format
== WL_SHM_FORMAT_RGB565
)
1304 else /* ARGB8888 || XRGB8888 */
1309 * Taken from weston shared/os-compatibility.c
1312 #ifndef HAVE_MKOSTEMP
1315 set_cloexec_or_close(int fd
)
1322 flags
= fcntl(fd
, F_GETFD
);
1326 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1339 * Taken from weston shared/os-compatibility.c
1343 create_tmpfile_cloexec(char *tmpname
)
1347 #ifdef HAVE_MKOSTEMP
1348 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1352 fd
= mkstemp(tmpname
);
1354 fd
= set_cloexec_or_close(fd
);
1363 * Taken from weston shared/os-compatibility.c
1365 * Create a new, unique, anonymous file of the given size, and
1366 * return the file descriptor for it. The file descriptor is set
1367 * CLOEXEC. The file is immediately suitable for mmap()'ing
1368 * the given size at offset zero.
1370 * The file should not have a permanent backing store like a disk,
1371 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1373 * The file name is deleted from the file system.
1375 * The file is suitable for buffer sharing between processes by
1376 * transmitting the file descriptor over Unix sockets using the
1377 * SCM_RIGHTS methods.
1379 * If the C library implements posix_fallocate(), it is used to
1380 * guarantee that disk space is available for the file at the
1381 * given size. If disk space is insufficent, errno is set to ENOSPC.
1382 * If posix_fallocate() is not supported, program may receive
1383 * SIGBUS on accessing mmap()'ed file contents instead.
1386 os_create_anonymous_file(off_t size
)
1388 static const char template[] = "/mesa-shared-XXXXXX";
1394 path
= getenv("XDG_RUNTIME_DIR");
1400 name
= malloc(strlen(path
) + sizeof(template));
1405 strcat(name
, template);
1407 fd
= create_tmpfile_cloexec(name
);
1414 ret
= ftruncate(fd
, size
);
1425 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1426 int format
, int w
, int h
,
1427 void **data
, int *size
,
1428 struct wl_buffer
**buffer
)
1430 struct wl_shm_pool
*pool
;
1431 int fd
, stride
, size_map
;
1434 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1435 size_map
= h
* stride
;
1437 /* Create a sharable buffer */
1438 fd
= os_create_anonymous_file(size_map
);
1442 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1443 if (data_map
== MAP_FAILED
) {
1448 /* Share it in a wl_buffer */
1449 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1450 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1451 wl_shm_pool_destroy(pool
);
1460 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1462 struct dri2_egl_display
*dri2_dpy
=
1463 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1466 /* we need to do the following operations only once per frame */
1467 if (dri2_surf
->back
)
1470 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1471 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1473 dri2_wl_release_buffers(dri2_surf
);
1475 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1476 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1477 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1478 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1479 dri2_surf
->current
= NULL
;
1482 /* find back buffer */
1484 /* There might be a buffer release already queued that wasn't processed */
1485 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
1487 /* try get free buffer already created */
1488 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1489 if (!dri2_surf
->color_buffers
[i
].locked
&&
1490 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1491 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1496 /* else choose any another free location */
1497 if (!dri2_surf
->back
) {
1498 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1499 if (!dri2_surf
->color_buffers
[i
].locked
) {
1500 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1501 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1503 dri2_surf
->base
.Width
,
1504 dri2_surf
->base
.Height
,
1505 &dri2_surf
->back
->data
,
1506 &dri2_surf
->back
->data_size
,
1507 &dri2_surf
->back
->wl_buffer
)) {
1508 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1511 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1512 dri2_dpy
->wl_queue
);
1513 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1514 &wl_buffer_listener
, dri2_surf
);
1520 if (!dri2_surf
->back
) {
1521 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1525 dri2_surf
->back
->locked
= 1;
1527 /* If we have an extra unlocked buffer at this point, we had to do triple
1528 * buffering for a while, but now can go back to just double buffering.
1529 * That means we can free any unlocked buffer now. */
1530 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1531 if (!dri2_surf
->color_buffers
[i
].locked
&&
1532 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1533 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1534 munmap(dri2_surf
->color_buffers
[i
].data
,
1535 dri2_surf
->color_buffers
[i
].data_size
);
1536 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1537 dri2_surf
->color_buffers
[i
].data
= NULL
;
1545 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1547 /* if there has been a resize: */
1548 if (!dri2_surf
->current
)
1551 return dri2_surf
->current
->data
;
1555 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1557 assert(dri2_surf
->back
);
1558 return dri2_surf
->back
->data
;
1562 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1564 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1566 while (dri2_surf
->throttle_callback
!= NULL
)
1567 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1568 dri2_dpy
->wl_queue
) == -1)
1571 if (dri2_surf
->base
.SwapInterval
> 0) {
1572 dri2_surf
->throttle_callback
=
1573 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1574 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1575 &throttle_listener
, dri2_surf
);
1576 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1577 dri2_dpy
->wl_queue
);
1580 dri2_surf
->current
= dri2_surf
->back
;
1581 dri2_surf
->back
= NULL
;
1583 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1584 dri2_surf
->current
->wl_buffer
,
1585 dri2_surf
->dx
, dri2_surf
->dy
);
1587 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1588 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1589 /* reset resize growing parameters */
1593 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1594 0, 0, INT32_MAX
, INT32_MAX
);
1595 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1597 /* If we're not waiting for a frame callback then we'll at least throttle
1598 * to a sync callback so that we always give a chance for the compositor to
1599 * handle the commit and send a release event before checking for a free
1601 if (dri2_surf
->throttle_callback
== NULL
) {
1602 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1603 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1604 &throttle_listener
, dri2_surf
);
1607 wl_display_flush(dri2_dpy
->wl_dpy
);
1611 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1612 int *x
, int *y
, int *w
, int *h
,
1613 void *loaderPrivate
)
1615 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1617 (void) swrast_update_buffers(dri2_surf
);
1620 *w
= dri2_surf
->base
.Width
;
1621 *h
= dri2_surf
->base
.Height
;
1625 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1626 int x
, int y
, int w
, int h
,
1627 char *data
, void *loaderPrivate
)
1629 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1630 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1631 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1632 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1633 int dst_stride
= copy_width
;
1636 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1638 memset(data
, 0, copy_width
* h
);
1642 assert(data
!= src
);
1643 assert(copy_width
<= src_stride
);
1646 src
+= y
* src_stride
;
1649 if (copy_width
> src_stride
-x_offset
)
1650 copy_width
= src_stride
-x_offset
;
1651 if (h
> dri2_surf
->base
.Height
-y
)
1652 h
= dri2_surf
->base
.Height
-y
;
1655 memcpy(dst
, src
, copy_width
);
1662 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1663 int x
, int y
, int w
, int h
, int stride
,
1664 char *data
, void *loaderPrivate
)
1666 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1667 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1668 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1669 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1672 assert(copy_width
<= stride
);
1674 (void) swrast_update_buffers(dri2_surf
);
1675 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1677 /* partial copy, copy old content */
1678 if (copy_width
< dst_stride
)
1679 dri2_wl_swrast_get_image(draw
, 0, 0,
1680 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1681 dst
, loaderPrivate
);
1684 dst
+= y
* dst_stride
;
1688 /* drivers expect we do these checks (and some rely on it) */
1689 if (copy_width
> dst_stride
-x_offset
)
1690 copy_width
= dst_stride
-x_offset
;
1691 if (h
> dri2_surf
->base
.Height
-y
)
1692 h
= dri2_surf
->base
.Height
-y
;
1695 memcpy(dst
, src
, copy_width
);
1699 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1703 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1704 int x
, int y
, int w
, int h
,
1705 char *data
, void *loaderPrivate
)
1707 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1710 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1711 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1712 stride
, data
, loaderPrivate
);
1716 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1718 static _EGLSurface
*
1719 dri2_wl_swrast_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1720 _EGLConfig
*conf
, void *native_window
,
1721 const EGLint
*attrib_list
)
1723 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1724 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1725 struct wl_egl_window
*window
= native_window
;
1726 struct dri2_egl_surface
*dri2_surf
;
1727 const __DRIconfig
*config
;
1731 dri2_surf
= calloc(1, sizeof *dri2_surf
);
1733 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1737 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
1740 if (conf
->RedSize
== 5)
1741 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
1742 else if (conf
->AlphaSize
== 0)
1743 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
1745 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
1747 dri2_surf
->wl_win
= window
;
1749 dri2_surf
->base
.Width
= -1;
1750 dri2_surf
->base
.Height
= -1;
1752 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
1753 dri2_surf
->base
.GLColorspace
);
1755 dri2_surf
->dri_drawable
=
1756 dri2_dpy
->swrast
->createNewDrawable(dri2_dpy
->dri_screen
,
1758 if (dri2_surf
->dri_drawable
== NULL
) {
1759 _eglError(EGL_BAD_ALLOC
, "swrast->createNewDrawable");
1763 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
1764 dri2_dpy
->default_swap_interval
);
1766 return &dri2_surf
->base
;
1775 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1777 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1778 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1780 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1785 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1787 struct dri2_egl_display
*dri2_dpy
= data
;
1790 case WL_SHM_FORMAT_ARGB8888
:
1791 dri2_dpy
->formats
|= HAS_ARGB8888
;
1793 case WL_SHM_FORMAT_XRGB8888
:
1794 dri2_dpy
->formats
|= HAS_XRGB8888
;
1796 case WL_SHM_FORMAT_RGB565
:
1797 dri2_dpy
->formats
|= HAS_RGB565
;
1802 static const struct wl_shm_listener shm_listener
= {
1803 .format
= shm_handle_format
1807 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1808 const char *interface
, uint32_t version
)
1810 struct dri2_egl_display
*dri2_dpy
= data
;
1812 if (strcmp(interface
, "wl_shm") == 0) {
1814 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1815 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1819 static const struct wl_registry_listener registry_listener_swrast
= {
1820 .global
= registry_handle_global_swrast
,
1821 .global_remove
= registry_handle_global_remove
1824 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1825 .authenticate
= NULL
,
1826 .create_window_surface
= dri2_wl_swrast_create_window_surface
,
1827 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1828 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1829 .destroy_surface
= dri2_wl_destroy_surface
,
1830 .create_image
= dri2_fallback_create_image_khr
,
1831 .swap_interval
= dri2_wl_swap_interval
,
1832 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1833 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1834 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1835 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1836 .copy_buffers
= dri2_fallback_copy_buffers
,
1837 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1838 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1839 .get_sync_values
= dri2_fallback_get_sync_values
,
1840 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1843 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1844 .base
= { __DRI_SWRAST_LOADER
, 2 },
1846 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1847 .putImage
= dri2_wl_swrast_put_image
,
1848 .getImage
= dri2_wl_swrast_get_image
,
1849 .putImage2
= dri2_wl_swrast_put_image2
,
1852 static const __DRIextension
*swrast_loader_extensions
[] = {
1853 &swrast_loader_extension
.base
,
1858 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1860 struct dri2_egl_display
*dri2_dpy
;
1862 loader_set_logger(_eglLog
);
1864 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1866 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1868 disp
->DriverData
= (void *) dri2_dpy
;
1869 if (disp
->PlatformDisplay
== NULL
) {
1870 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1871 if (dri2_dpy
->wl_dpy
== NULL
)
1873 dri2_dpy
->own_device
= 1;
1875 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1878 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1880 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1881 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1882 goto cleanup_dpy_wrapper
;
1884 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1885 dri2_dpy
->wl_queue
);
1887 if (dri2_dpy
->own_device
)
1888 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1890 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1891 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1892 ®istry_listener_swrast
, dri2_dpy
);
1894 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1895 goto cleanup_registry
;
1897 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1901 dri2_dpy
->driver_name
= strdup("swrast");
1902 if (!dri2_load_driver_swrast(disp
))
1905 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1907 if (!dri2_create_screen(disp
))
1908 goto cleanup_driver
;
1910 dri2_wl_setup_swap_interval(dri2_dpy
);
1912 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1913 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1914 goto cleanup_screen
;
1917 /* Fill vtbl last to prevent accidentally calling virtual function during
1920 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1925 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1927 dlclose(dri2_dpy
->driver
);
1929 wl_shm_destroy(dri2_dpy
->wl_shm
);
1931 wl_registry_destroy(dri2_dpy
->wl_registry
);
1932 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1933 cleanup_dpy_wrapper
:
1934 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1935 if (disp
->PlatformDisplay
== NULL
)
1936 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1939 disp
->DriverData
= NULL
;
1945 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1947 EGLBoolean initialized
= EGL_TRUE
;
1949 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1952 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1953 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1956 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);