2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
42 #include "egl_dri2_fallbacks.h"
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
48 enum wl_drm_format_flags
{
55 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
59 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
64 wl_callback_destroy(callback
);
67 static const struct wl_callback_listener sync_listener
= {
72 roundtrip(struct dri2_egl_display
*dri2_dpy
)
74 struct wl_callback
*callback
;
75 int done
= 0, ret
= 0;
77 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
78 wl_callback_add_listener(callback
, &sync_listener
, &done
);
79 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
80 while (ret
!= -1 && !done
)
81 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
84 wl_callback_destroy(callback
);
90 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
92 struct dri2_egl_surface
*dri2_surf
= data
;
95 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
96 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
99 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
100 wl_buffer_destroy(buffer
);
104 dri2_surf
->color_buffers
[i
].locked
= 0;
107 static const struct wl_buffer_listener wl_buffer_listener
= {
108 .release
= wl_buffer_release
112 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
114 struct dri2_egl_surface
*dri2_surf
= data
;
115 struct dri2_egl_display
*dri2_dpy
=
116 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
118 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
122 destroy_window_callback(void *data
)
124 struct dri2_egl_surface
*dri2_surf
= data
;
125 dri2_surf
->wl_win
= NULL
;
129 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
132 dri2_wl_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
133 _EGLConfig
*conf
, void *native_window
,
134 const EGLint
*attrib_list
)
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
;
144 dri2_surf
= calloc(1, sizeof *dri2_surf
);
146 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
150 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
153 if (conf
->RedSize
== 5)
154 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
155 else if (conf
->AlphaSize
== 0)
156 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
158 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
161 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
165 dri2_surf
->wl_win
= window
;
167 dri2_surf
->wl_win
->private = dri2_surf
;
168 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
169 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
171 dri2_surf
->base
.Width
= -1;
172 dri2_surf
->base
.Height
= -1;
174 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
175 dri2_surf
->base
.GLColorspace
);
177 dri2_surf
->dri_drawable
=
178 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
180 if (dri2_surf
->dri_drawable
== NULL
) {
181 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
185 return &dri2_surf
->base
;
194 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
197 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
198 _EGLConfig
*conf
, void *native_window
,
199 const EGLint
*attrib_list
)
201 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
204 surf
= dri2_wl_create_surface(drv
, disp
, conf
, native_window
, attrib_list
);
207 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
213 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
214 _EGLConfig
*conf
, void *native_window
,
215 const EGLint
*attrib_list
)
217 /* From the EGL_EXT_platform_wayland spec, version 3:
219 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
220 * that belongs to Wayland. Any such call fails and generates
223 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
229 * Called via eglDestroySurface(), drv->API.DestroySurface().
232 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
234 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
235 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
240 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
242 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
243 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
244 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
245 if (dri2_surf
->color_buffers
[i
].dri_image
)
246 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
247 if (dri2_surf
->color_buffers
[i
].linear_copy
)
248 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
249 if (dri2_surf
->color_buffers
[i
].data
)
250 munmap(dri2_surf
->color_buffers
[i
].data
,
251 dri2_surf
->color_buffers
[i
].data_size
);
254 if (dri2_dpy
->dri2
) {
255 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
256 if (dri2_surf
->dri_buffers
[i
] &&
257 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
258 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
259 dri2_surf
->dri_buffers
[i
]);
262 if (dri2_surf
->throttle_callback
)
263 wl_callback_destroy(dri2_surf
->throttle_callback
);
265 if (dri2_surf
->wl_win
) {
266 dri2_surf
->wl_win
->private = NULL
;
267 dri2_surf
->wl_win
->resize_callback
= NULL
;
268 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
277 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
279 struct dri2_egl_display
*dri2_dpy
=
280 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
283 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
284 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
285 !dri2_surf
->color_buffers
[i
].locked
)
286 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
287 if (dri2_surf
->color_buffers
[i
].dri_image
)
288 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
289 if (dri2_surf
->color_buffers
[i
].linear_copy
)
290 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
291 if (dri2_surf
->color_buffers
[i
].data
)
292 munmap(dri2_surf
->color_buffers
[i
].data
,
293 dri2_surf
->color_buffers
[i
].data_size
);
295 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
296 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
297 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
298 dri2_surf
->color_buffers
[i
].data
= NULL
;
299 dri2_surf
->color_buffers
[i
].locked
= 0;
302 if (dri2_dpy
->dri2
) {
303 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
304 if (dri2_surf
->dri_buffers
[i
] &&
305 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
306 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
307 dri2_surf
->dri_buffers
[i
]);
312 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
314 struct dri2_egl_display
*dri2_dpy
=
315 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
317 unsigned int dri_image_format
;
319 /* currently supports three WL DRM formats,
320 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
321 * and WL_DRM_FORMAT_RGB565
323 switch (dri2_surf
->format
) {
324 case WL_DRM_FORMAT_ARGB8888
:
325 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
327 case WL_DRM_FORMAT_XRGB8888
:
328 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
330 case WL_DRM_FORMAT_RGB565
:
331 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
334 /* format is not supported */
338 /* There might be a buffer release already queued that wasn't processed */
339 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
341 if (dri2_surf
->back
== NULL
) {
342 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
343 /* Get an unlocked buffer, preferrably one with a dri_buffer
344 * already allocated. */
345 if (dri2_surf
->color_buffers
[i
].locked
)
347 if (dri2_surf
->back
== NULL
)
348 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
349 else if (dri2_surf
->back
->dri_image
== NULL
)
350 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
354 if (dri2_surf
->back
== NULL
)
357 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
359 if (dri2_dpy
->is_different_gpu
&&
360 dri2_surf
->back
->linear_copy
== NULL
) {
361 dri2_surf
->back
->linear_copy
=
362 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
363 dri2_surf
->base
.Width
,
364 dri2_surf
->base
.Height
,
367 __DRI_IMAGE_USE_LINEAR
,
369 if (dri2_surf
->back
->linear_copy
== NULL
)
373 if (dri2_surf
->back
->dri_image
== NULL
) {
374 dri2_surf
->back
->dri_image
=
375 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
376 dri2_surf
->base
.Width
,
377 dri2_surf
->base
.Height
,
379 dri2_dpy
->is_different_gpu
?
382 dri2_surf
->back
->age
= 0;
384 if (dri2_surf
->back
->dri_image
== NULL
)
387 dri2_surf
->back
->locked
= 1;
394 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
396 struct dri2_egl_display
*dri2_dpy
=
397 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
401 image
= dri2_surf
->back
->dri_image
;
403 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
404 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
406 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
408 buffer
->pitch
= pitch
;
414 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
415 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
417 struct dri2_egl_display
*dri2_dpy
=
418 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
419 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
422 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
424 dri2_surf
->base
.Width
,
425 dri2_surf
->base
.Height
);
426 dri2_surf
->dri_buffers
[attachment
] = b
;
431 memcpy(buffer
, b
, sizeof *buffer
);
437 update_buffers(struct dri2_egl_surface
*dri2_surf
)
439 struct dri2_egl_display
*dri2_dpy
=
440 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
443 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
444 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
446 dri2_wl_release_buffers(dri2_surf
);
448 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
449 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
450 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
451 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
454 if (get_back_bo(dri2_surf
) < 0) {
455 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
459 /* If we have an extra unlocked buffer at this point, we had to do triple
460 * buffering for a while, but now can go back to just double buffering.
461 * That means we can free any unlocked buffer now. */
462 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
463 if (!dri2_surf
->color_buffers
[i
].locked
&&
464 dri2_surf
->color_buffers
[i
].wl_buffer
) {
465 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
466 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
467 if (dri2_dpy
->is_different_gpu
)
468 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
469 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
470 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
471 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
479 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
480 int *width
, int *height
,
481 unsigned int *attachments
, int count
,
482 int *out_count
, void *loaderPrivate
)
484 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
487 if (update_buffers(dri2_surf
) < 0)
490 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
491 switch (attachments
[i
]) {
492 case __DRI_BUFFER_BACK_LEFT
:
493 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
496 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
497 &dri2_surf
->buffers
[j
]) < 0) {
498 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
509 *width
= dri2_surf
->base
.Width
;
510 *height
= dri2_surf
->base
.Height
;
512 return dri2_surf
->buffers
;
516 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
517 int *width
, int *height
,
518 unsigned int *attachments
, int count
,
519 int *out_count
, void *loaderPrivate
)
521 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
522 unsigned int *attachments_with_format
;
528 switch (dri2_surf
->format
) {
529 case WL_DRM_FORMAT_ARGB8888
:
530 case WL_DRM_FORMAT_XRGB8888
:
533 case WL_DRM_FORMAT_RGB565
:
537 /* format is not supported */
541 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
542 if (!attachments_with_format
) {
547 for (i
= 0; i
< count
; ++i
) {
548 attachments_with_format
[2*i
] = attachments
[i
];
549 attachments_with_format
[2*i
+ 1] = bpp
;
553 dri2_wl_get_buffers_with_format(driDrawable
,
555 attachments_with_format
, count
,
556 out_count
, loaderPrivate
);
558 free(attachments_with_format
);
564 image_get_buffers(__DRIdrawable
*driDrawable
,
568 uint32_t buffer_mask
,
569 struct __DRIimageList
*buffers
)
571 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
573 if (update_buffers(dri2_surf
) < 0)
576 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
577 buffers
->back
= dri2_surf
->back
->dri_image
;
583 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
586 (void) loaderPrivate
;
589 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
590 .base
= { __DRI_DRI2_LOADER
, 3 },
592 .getBuffers
= dri2_wl_get_buffers
,
593 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
594 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
597 static const __DRIimageLoaderExtension image_loader_extension
= {
598 .base
= { __DRI_IMAGE_LOADER
, 1 },
600 .getBuffers
= image_get_buffers
,
601 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
605 wayland_throttle_callback(void *data
,
606 struct wl_callback
*callback
,
609 struct dri2_egl_surface
*dri2_surf
= data
;
611 dri2_surf
->throttle_callback
= NULL
;
612 wl_callback_destroy(callback
);
615 static const struct wl_callback_listener throttle_listener
= {
616 .done
= wayland_throttle_callback
620 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
622 struct dri2_egl_display
*dri2_dpy
=
623 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
625 int fd
, stride
, name
;
627 if (dri2_surf
->current
->wl_buffer
!= NULL
)
630 if (dri2_dpy
->is_different_gpu
) {
631 image
= dri2_surf
->current
->linear_copy
;
633 image
= dri2_surf
->current
->dri_image
;
635 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
636 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
637 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
639 dri2_surf
->current
->wl_buffer
=
640 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
642 dri2_surf
->base
.Width
,
643 dri2_surf
->base
.Height
,
650 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
651 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
653 dri2_surf
->current
->wl_buffer
=
654 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
656 dri2_surf
->base
.Width
,
657 dri2_surf
->base
.Height
,
662 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
664 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
665 &wl_buffer_listener
, dri2_surf
);
669 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
673 /* The WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION macro and
674 * wl_proxy_get_version() were both introduced in wayland 1.10.
675 * Instead of bumping our wayland dependency we just make this
676 * function conditional on the required 1.10 features, falling
677 * back to old (correct but suboptimal) behaviour for older
680 #ifdef WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
683 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_win
->surface
)
684 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
687 for (i
= 0; i
< n_rects
; i
++) {
688 const int *rect
= &rects
[i
* 4];
690 wl_surface_damage_buffer(dri2_surf
->wl_win
->surface
,
692 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
700 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
703 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
709 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
710 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
713 while (dri2_surf
->throttle_callback
!= NULL
)
714 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
715 dri2_dpy
->wl_queue
) == -1)
718 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
719 if (dri2_surf
->color_buffers
[i
].age
> 0)
720 dri2_surf
->color_buffers
[i
].age
++;
722 /* Make sure we have a back buffer in case we're swapping without ever
724 if (get_back_bo(dri2_surf
) < 0) {
725 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
729 if (draw
->SwapInterval
> 0) {
730 dri2_surf
->throttle_callback
=
731 wl_surface_frame(dri2_surf
->wl_win
->surface
);
732 wl_callback_add_listener(dri2_surf
->throttle_callback
,
733 &throttle_listener
, dri2_surf
);
734 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
738 dri2_surf
->back
->age
= 1;
739 dri2_surf
->current
= dri2_surf
->back
;
740 dri2_surf
->back
= NULL
;
742 create_wl_buffer(dri2_surf
);
744 wl_surface_attach(dri2_surf
->wl_win
->surface
,
745 dri2_surf
->current
->wl_buffer
,
746 dri2_surf
->dx
, dri2_surf
->dy
);
748 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
749 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
750 /* reset resize growing parameters */
754 /* If the compositor doesn't support damage_buffer, we deliberately
755 * ignore the damage region and post maximum damage, due to
756 * https://bugs.freedesktop.org/78190 */
757 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
758 wl_surface_damage(dri2_surf
->wl_win
->surface
,
759 0, 0, INT32_MAX
, INT32_MAX
);
761 if (dri2_dpy
->is_different_gpu
) {
762 _EGLContext
*ctx
= _eglGetCurrentContext();
763 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
764 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
765 dri2_surf
->current
->linear_copy
,
766 dri2_surf
->current
->dri_image
,
767 0, 0, dri2_surf
->base
.Width
,
768 dri2_surf
->base
.Height
,
769 0, 0, dri2_surf
->base
.Width
,
770 dri2_surf
->base
.Height
, 0);
773 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
774 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
776 wl_surface_commit(dri2_surf
->wl_win
->surface
);
778 /* If we're not waiting for a frame callback then we'll at least throttle
779 * to a sync callback so that we always give a chance for the compositor to
780 * handle the commit and send a release event before checking for a free
782 if (dri2_surf
->throttle_callback
== NULL
) {
783 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
784 wl_callback_add_listener(dri2_surf
->throttle_callback
,
785 &throttle_listener
, dri2_surf
);
786 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
790 wl_display_flush(dri2_dpy
->wl_dpy
);
796 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
797 _EGLDisplay
*disp
, _EGLSurface
*surface
)
799 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
801 if (get_back_bo(dri2_surf
) < 0) {
802 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
806 return dri2_surf
->back
->age
;
810 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
812 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
815 static struct wl_buffer
*
816 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
820 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
821 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
822 __DRIimage
*image
= dri2_img
->dri_image
;
823 struct wl_buffer
*buffer
;
824 int width
, height
, format
, pitch
;
825 enum wl_drm_format wl_format
;
827 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
830 case __DRI_IMAGE_FORMAT_ARGB8888
:
831 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
833 wl_format
= WL_DRM_FORMAT_ARGB8888
;
835 case __DRI_IMAGE_FORMAT_XRGB8888
:
836 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
838 wl_format
= WL_DRM_FORMAT_XRGB8888
;
844 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
845 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
846 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
848 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
851 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
854 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
866 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
869 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
876 /* The buffer object will have been created with our internal event queue
877 * because it is using the wl_drm object as a proxy factory. We want the
878 * buffer to be used by the application so we'll reset it to the display's
879 * default event queue */
881 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
886 _eglError(EGL_BAD_MATCH
, "unsupported image format");
891 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
893 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
896 if (dri2_dpy
->is_render_node
) {
897 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
898 "authenticate for render-nodes");
901 dri2_dpy
->authenticated
= 0;
903 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
904 if (roundtrip(dri2_dpy
) < 0)
907 if (!dri2_dpy
->authenticated
)
910 /* reset authenticated */
911 dri2_dpy
->authenticated
= 1;
917 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
919 struct dri2_egl_display
*dri2_dpy
= data
;
922 dri2_dpy
->device_name
= strdup(device
);
923 if (!dri2_dpy
->device_name
)
926 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
927 if (dri2_dpy
->fd
== -1) {
928 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
929 dri2_dpy
->device_name
, strerror(errno
));
933 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
934 dri2_dpy
->authenticated
= 1;
936 drmGetMagic(dri2_dpy
->fd
, &magic
);
937 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
942 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
944 struct dri2_egl_display
*dri2_dpy
= data
;
947 case WL_DRM_FORMAT_ARGB8888
:
948 dri2_dpy
->formats
|= HAS_ARGB8888
;
950 case WL_DRM_FORMAT_XRGB8888
:
951 dri2_dpy
->formats
|= HAS_XRGB8888
;
953 case WL_DRM_FORMAT_RGB565
:
954 dri2_dpy
->formats
|= HAS_RGB565
;
960 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
962 struct dri2_egl_display
*dri2_dpy
= data
;
964 dri2_dpy
->capabilities
= value
;
968 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
970 struct dri2_egl_display
*dri2_dpy
= data
;
972 dri2_dpy
->authenticated
= 1;
975 static const struct wl_drm_listener drm_listener
= {
976 .device
= drm_handle_device
,
977 .format
= drm_handle_format
,
978 .authenticated
= drm_handle_authenticated
,
979 .capabilities
= drm_handle_capabilities
983 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
984 const char *interface
, uint32_t version
)
986 struct dri2_egl_display
*dri2_dpy
= data
;
990 if (strcmp(interface
, "wl_drm") == 0) {
992 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
993 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
998 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1003 static const struct wl_registry_listener registry_listener_drm
= {
1004 .global
= registry_handle_global_drm
,
1005 .global_remove
= registry_handle_global_remove
1009 dri2_wl_swap_interval(_EGLDriver
*drv
,
1014 if (interval
> surf
->Config
->MaxSwapInterval
)
1015 interval
= surf
->Config
->MaxSwapInterval
;
1016 else if (interval
< surf
->Config
->MinSwapInterval
)
1017 interval
= surf
->Config
->MinSwapInterval
;
1019 surf
->SwapInterval
= interval
;
1025 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1027 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1029 /* We can't use values greater than 1 on Wayland because we are using the
1030 * frame callback to synchronise the frame and the only way we be sure to
1031 * get a frame callback is to attach a new buffer. Therefore we can't just
1032 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1034 if (dri2_dpy
->config
)
1035 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1036 "vblank_mode", &vblank_mode
);
1037 switch (vblank_mode
) {
1038 case DRI_CONF_VBLANK_NEVER
:
1039 dri2_dpy
->min_swap_interval
= 0;
1040 dri2_dpy
->max_swap_interval
= 0;
1041 dri2_dpy
->default_swap_interval
= 0;
1043 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1044 dri2_dpy
->min_swap_interval
= 1;
1045 dri2_dpy
->max_swap_interval
= 1;
1046 dri2_dpy
->default_swap_interval
= 1;
1048 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1049 dri2_dpy
->min_swap_interval
= 0;
1050 dri2_dpy
->max_swap_interval
= 1;
1051 dri2_dpy
->default_swap_interval
= 0;
1054 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1055 dri2_dpy
->min_swap_interval
= 0;
1056 dri2_dpy
->max_swap_interval
= 1;
1057 dri2_dpy
->default_swap_interval
= 1;
1062 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1063 .authenticate
= dri2_wl_authenticate
,
1064 .create_window_surface
= dri2_wl_create_window_surface
,
1065 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1066 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1067 .destroy_surface
= dri2_wl_destroy_surface
,
1068 .create_image
= dri2_create_image_khr
,
1069 .swap_interval
= dri2_wl_swap_interval
,
1070 .swap_buffers
= dri2_wl_swap_buffers
,
1071 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1072 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1073 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1074 .copy_buffers
= dri2_fallback_copy_buffers
,
1075 .query_buffer_age
= dri2_wl_query_buffer_age
,
1076 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1077 .get_sync_values
= dri2_fallback_get_sync_values
,
1078 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1081 static const __DRIextension
*dri2_loader_extensions
[] = {
1082 &dri2_loader_extension
.base
,
1083 &image_loader_extension
.base
,
1084 &image_lookup_extension
.base
,
1085 &use_invalidate
.base
,
1089 static const __DRIextension
*image_loader_extensions
[] = {
1090 &image_loader_extension
.base
,
1091 &image_lookup_extension
.base
,
1092 &use_invalidate
.base
,
1097 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1099 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1100 static const struct {
1101 const char *format_name
;
1103 unsigned int rgba_masks
[4];
1105 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1106 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1107 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1109 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1110 unsigned int count
, i
, j
;
1113 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1114 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1115 struct dri2_egl_config
*dri2_conf
;
1117 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1120 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1121 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1129 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1130 if (!format_count
[i
]) {
1131 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1132 visuals
[i
].format_name
);
1136 return (count
!= 0);
1140 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1142 struct dri2_egl_display
*dri2_dpy
;
1144 loader_set_logger(_eglLog
);
1146 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1148 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1150 disp
->DriverData
= (void *) dri2_dpy
;
1151 if (disp
->PlatformDisplay
== NULL
) {
1152 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1153 if (dri2_dpy
->wl_dpy
== NULL
)
1155 dri2_dpy
->own_device
= 1;
1157 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1160 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1162 if (dri2_dpy
->own_device
)
1163 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1165 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1166 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1167 dri2_dpy
->wl_queue
);
1168 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1169 ®istry_listener_drm
, dri2_dpy
);
1170 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1171 goto cleanup_registry
;
1173 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1176 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1179 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1180 &dri2_dpy
->is_different_gpu
);
1181 if (dri2_dpy
->is_different_gpu
) {
1182 free(dri2_dpy
->device_name
);
1183 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1184 if (!dri2_dpy
->device_name
) {
1185 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1186 "for requested GPU");
1191 /* we have to do the check now, because loader_get_user_preferred_fd
1192 * will return a render-node when the requested gpu is different
1193 * to the server, but also if the client asks for the same gpu than
1194 * the server by requesting its pci-id */
1195 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1197 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1198 if (dri2_dpy
->driver_name
== NULL
) {
1199 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1203 if (!dri2_load_driver(disp
))
1204 goto cleanup_driver_name
;
1206 /* render nodes cannot use Gem names, and thus do not support
1207 * the __DRI_DRI2_LOADER extension */
1208 if (!dri2_dpy
->is_render_node
)
1209 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1211 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1213 if (!dri2_create_screen(disp
))
1214 goto cleanup_driver
;
1216 dri2_wl_setup_swap_interval(dri2_dpy
);
1218 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1219 * createImageFromFds support indicates that Prime export/import
1220 * is supported by the driver. Fall back to
1221 * gem names if we don't have Prime support. */
1223 if (dri2_dpy
->image
->base
.version
< 7 ||
1224 dri2_dpy
->image
->createImageFromFds
== NULL
)
1225 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1227 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1228 * The server needs to accept them */
1229 if (dri2_dpy
->is_render_node
&&
1230 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1231 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1232 goto cleanup_screen
;
1235 if (dri2_dpy
->is_different_gpu
&&
1236 (dri2_dpy
->image
->base
.version
< 9 ||
1237 dri2_dpy
->image
->blitImage
== NULL
)) {
1238 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1239 "Image extension in the driver is not "
1240 "compatible. Version 9 or later and blitImage() "
1242 goto cleanup_screen
;
1245 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1246 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1247 goto cleanup_screen
;
1250 dri2_set_WL_bind_wayland_display(drv
, disp
);
1251 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1252 * because the buffer of the EGLImage has likely a tiling mode the server
1253 * gpu won't support. These is no way to check for now. Thus do not support the
1255 if (!dri2_dpy
->is_different_gpu
) {
1256 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1258 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1259 dri2_fallback_create_wayland_buffer_from_image
;
1261 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1263 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1265 /* Fill vtbl last to prevent accidentally calling virtual function during
1268 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1273 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1275 dlclose(dri2_dpy
->driver
);
1276 cleanup_driver_name
:
1277 free(dri2_dpy
->driver_name
);
1279 close(dri2_dpy
->fd
);
1281 free(dri2_dpy
->device_name
);
1282 wl_drm_destroy(dri2_dpy
->wl_drm
);
1284 wl_registry_destroy(dri2_dpy
->wl_registry
);
1285 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1286 if (disp
->PlatformDisplay
== NULL
)
1287 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1290 disp
->DriverData
= NULL
;
1296 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1298 if (format
== WL_SHM_FORMAT_RGB565
)
1300 else /* ARGB8888 || XRGB8888 */
1305 * Taken from weston shared/os-compatibility.c
1308 #ifndef HAVE_MKOSTEMP
1311 set_cloexec_or_close(int fd
)
1318 flags
= fcntl(fd
, F_GETFD
);
1322 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1335 * Taken from weston shared/os-compatibility.c
1339 create_tmpfile_cloexec(char *tmpname
)
1343 #ifdef HAVE_MKOSTEMP
1344 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1348 fd
= mkstemp(tmpname
);
1350 fd
= set_cloexec_or_close(fd
);
1359 * Taken from weston shared/os-compatibility.c
1361 * Create a new, unique, anonymous file of the given size, and
1362 * return the file descriptor for it. The file descriptor is set
1363 * CLOEXEC. The file is immediately suitable for mmap()'ing
1364 * the given size at offset zero.
1366 * The file should not have a permanent backing store like a disk,
1367 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1369 * The file name is deleted from the file system.
1371 * The file is suitable for buffer sharing between processes by
1372 * transmitting the file descriptor over Unix sockets using the
1373 * SCM_RIGHTS methods.
1375 * If the C library implements posix_fallocate(), it is used to
1376 * guarantee that disk space is available for the file at the
1377 * given size. If disk space is insufficent, errno is set to ENOSPC.
1378 * If posix_fallocate() is not supported, program may receive
1379 * SIGBUS on accessing mmap()'ed file contents instead.
1382 os_create_anonymous_file(off_t size
)
1384 static const char template[] = "/mesa-shared-XXXXXX";
1390 path
= getenv("XDG_RUNTIME_DIR");
1396 name
= malloc(strlen(path
) + sizeof(template));
1401 strcat(name
, template);
1403 fd
= create_tmpfile_cloexec(name
);
1410 ret
= ftruncate(fd
, size
);
1421 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1422 int format
, int w
, int h
,
1423 void **data
, int *size
,
1424 struct wl_buffer
**buffer
)
1426 struct wl_shm_pool
*pool
;
1427 int fd
, stride
, size_map
;
1430 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1431 size_map
= h
* stride
;
1433 /* Create a sharable buffer */
1434 fd
= os_create_anonymous_file(size_map
);
1438 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1439 if (data_map
== MAP_FAILED
) {
1444 /* Share it in a wl_buffer */
1445 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1446 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1447 wl_shm_pool_destroy(pool
);
1456 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1458 struct dri2_egl_display
*dri2_dpy
=
1459 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1462 /* we need to do the following operations only once per frame */
1463 if (dri2_surf
->back
)
1466 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1467 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1469 dri2_wl_release_buffers(dri2_surf
);
1471 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1472 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1473 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1474 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1475 dri2_surf
->current
= NULL
;
1478 /* find back buffer */
1480 /* There might be a buffer release already queued that wasn't processed */
1481 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
1483 /* try get free buffer already created */
1484 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1485 if (!dri2_surf
->color_buffers
[i
].locked
&&
1486 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1487 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1492 /* else choose any another free location */
1493 if (!dri2_surf
->back
) {
1494 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1495 if (!dri2_surf
->color_buffers
[i
].locked
) {
1496 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1497 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1499 dri2_surf
->base
.Width
,
1500 dri2_surf
->base
.Height
,
1501 &dri2_surf
->back
->data
,
1502 &dri2_surf
->back
->data_size
,
1503 &dri2_surf
->back
->wl_buffer
)) {
1504 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1507 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1508 dri2_dpy
->wl_queue
);
1509 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1510 &wl_buffer_listener
, dri2_surf
);
1516 if (!dri2_surf
->back
) {
1517 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1521 dri2_surf
->back
->locked
= 1;
1523 /* If we have an extra unlocked buffer at this point, we had to do triple
1524 * buffering for a while, but now can go back to just double buffering.
1525 * That means we can free any unlocked buffer now. */
1526 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1527 if (!dri2_surf
->color_buffers
[i
].locked
&&
1528 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1529 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1530 munmap(dri2_surf
->color_buffers
[i
].data
,
1531 dri2_surf
->color_buffers
[i
].data_size
);
1532 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1533 dri2_surf
->color_buffers
[i
].data
= NULL
;
1541 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1543 /* if there has been a resize: */
1544 if (!dri2_surf
->current
)
1547 return dri2_surf
->current
->data
;
1551 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1553 assert(dri2_surf
->back
);
1554 return dri2_surf
->back
->data
;
1558 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1560 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1562 while (dri2_surf
->throttle_callback
!= NULL
)
1563 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1564 dri2_dpy
->wl_queue
) == -1)
1567 if (dri2_surf
->base
.SwapInterval
> 0) {
1568 dri2_surf
->throttle_callback
=
1569 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1570 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1571 &throttle_listener
, dri2_surf
);
1572 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1573 dri2_dpy
->wl_queue
);
1576 dri2_surf
->current
= dri2_surf
->back
;
1577 dri2_surf
->back
= NULL
;
1579 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1580 dri2_surf
->current
->wl_buffer
,
1581 dri2_surf
->dx
, dri2_surf
->dy
);
1583 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1584 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1585 /* reset resize growing parameters */
1589 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1590 0, 0, INT32_MAX
, INT32_MAX
);
1591 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1593 /* If we're not waiting for a frame callback then we'll at least throttle
1594 * to a sync callback so that we always give a chance for the compositor to
1595 * handle the commit and send a release event before checking for a free
1597 if (dri2_surf
->throttle_callback
== NULL
) {
1598 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
1599 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1600 &throttle_listener
, dri2_surf
);
1601 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1602 dri2_dpy
->wl_queue
);
1605 wl_display_flush(dri2_dpy
->wl_dpy
);
1609 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1610 int *x
, int *y
, int *w
, int *h
,
1611 void *loaderPrivate
)
1613 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1615 (void) swrast_update_buffers(dri2_surf
);
1618 *w
= dri2_surf
->base
.Width
;
1619 *h
= dri2_surf
->base
.Height
;
1623 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1624 int x
, int y
, int w
, int h
,
1625 char *data
, void *loaderPrivate
)
1627 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1628 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1629 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1630 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1631 int dst_stride
= copy_width
;
1634 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1636 memset(data
, 0, copy_width
* h
);
1640 assert(data
!= src
);
1641 assert(copy_width
<= src_stride
);
1644 src
+= y
* src_stride
;
1647 if (copy_width
> src_stride
-x_offset
)
1648 copy_width
= src_stride
-x_offset
;
1649 if (h
> dri2_surf
->base
.Height
-y
)
1650 h
= dri2_surf
->base
.Height
-y
;
1653 memcpy(dst
, src
, copy_width
);
1660 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1661 int x
, int y
, int w
, int h
, int stride
,
1662 char *data
, void *loaderPrivate
)
1664 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1665 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1666 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1667 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1670 assert(copy_width
<= stride
);
1672 (void) swrast_update_buffers(dri2_surf
);
1673 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1675 /* partial copy, copy old content */
1676 if (copy_width
< dst_stride
)
1677 dri2_wl_swrast_get_image(draw
, 0, 0,
1678 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1679 dst
, loaderPrivate
);
1682 dst
+= y
* dst_stride
;
1686 /* drivers expect we do these checks (and some rely on it) */
1687 if (copy_width
> dst_stride
-x_offset
)
1688 copy_width
= dst_stride
-x_offset
;
1689 if (h
> dri2_surf
->base
.Height
-y
)
1690 h
= dri2_surf
->base
.Height
-y
;
1693 memcpy(dst
, src
, copy_width
);
1697 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1701 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1702 int x
, int y
, int w
, int h
,
1703 char *data
, void *loaderPrivate
)
1705 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1708 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1709 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1710 stride
, data
, loaderPrivate
);
1714 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1716 static _EGLSurface
*
1717 dri2_wl_swrast_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1718 _EGLConfig
*conf
, void *native_window
,
1719 const EGLint
*attrib_list
)
1721 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1722 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1723 struct wl_egl_window
*window
= native_window
;
1724 struct dri2_egl_surface
*dri2_surf
;
1725 const __DRIconfig
*config
;
1729 dri2_surf
= calloc(1, sizeof *dri2_surf
);
1731 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1735 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
1738 if (conf
->RedSize
== 5)
1739 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
1740 else if (conf
->AlphaSize
== 0)
1741 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
1743 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
1745 dri2_surf
->wl_win
= window
;
1747 dri2_surf
->base
.Width
= -1;
1748 dri2_surf
->base
.Height
= -1;
1750 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
1751 dri2_surf
->base
.GLColorspace
);
1753 dri2_surf
->dri_drawable
=
1754 (*dri2_dpy
->swrast
->createNewDrawable
)(dri2_dpy
->dri_screen
,
1756 if (dri2_surf
->dri_drawable
== NULL
) {
1757 _eglError(EGL_BAD_ALLOC
, "swrast->createNewDrawable");
1761 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
1762 dri2_dpy
->default_swap_interval
);
1764 return &dri2_surf
->base
;
1773 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1775 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1776 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1778 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1783 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1785 struct dri2_egl_display
*dri2_dpy
= data
;
1788 case WL_SHM_FORMAT_ARGB8888
:
1789 dri2_dpy
->formats
|= HAS_ARGB8888
;
1791 case WL_SHM_FORMAT_XRGB8888
:
1792 dri2_dpy
->formats
|= HAS_XRGB8888
;
1794 case WL_SHM_FORMAT_RGB565
:
1795 dri2_dpy
->formats
|= HAS_RGB565
;
1800 static const struct wl_shm_listener shm_listener
= {
1801 .format
= shm_handle_format
1805 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1806 const char *interface
, uint32_t version
)
1808 struct dri2_egl_display
*dri2_dpy
= data
;
1810 if (strcmp(interface
, "wl_shm") == 0) {
1812 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1813 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1817 static const struct wl_registry_listener registry_listener_swrast
= {
1818 .global
= registry_handle_global_swrast
,
1819 .global_remove
= registry_handle_global_remove
1822 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1823 .authenticate
= NULL
,
1824 .create_window_surface
= dri2_wl_swrast_create_window_surface
,
1825 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1826 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1827 .destroy_surface
= dri2_wl_destroy_surface
,
1828 .create_image
= dri2_fallback_create_image_khr
,
1829 .swap_interval
= dri2_wl_swap_interval
,
1830 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1831 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1832 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1833 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1834 .copy_buffers
= dri2_fallback_copy_buffers
,
1835 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1836 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1837 .get_sync_values
= dri2_fallback_get_sync_values
,
1838 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1841 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1842 .base
= { __DRI_SWRAST_LOADER
, 2 },
1844 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1845 .putImage
= dri2_wl_swrast_put_image
,
1846 .getImage
= dri2_wl_swrast_get_image
,
1847 .putImage2
= dri2_wl_swrast_put_image2
,
1850 static const __DRIextension
*swrast_loader_extensions
[] = {
1851 &swrast_loader_extension
.base
,
1856 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1858 struct dri2_egl_display
*dri2_dpy
;
1860 loader_set_logger(_eglLog
);
1862 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1864 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1866 disp
->DriverData
= (void *) dri2_dpy
;
1867 if (disp
->PlatformDisplay
== NULL
) {
1868 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1869 if (dri2_dpy
->wl_dpy
== NULL
)
1871 dri2_dpy
->own_device
= 1;
1873 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1876 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1878 if (dri2_dpy
->own_device
)
1879 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1881 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1882 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1883 dri2_dpy
->wl_queue
);
1884 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1885 ®istry_listener_swrast
, dri2_dpy
);
1887 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1888 goto cleanup_registry
;
1890 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1894 dri2_dpy
->driver_name
= strdup("swrast");
1895 if (!dri2_load_driver_swrast(disp
))
1898 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1900 if (!dri2_create_screen(disp
))
1901 goto cleanup_driver
;
1903 dri2_wl_setup_swap_interval(dri2_dpy
);
1905 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1906 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1907 goto cleanup_screen
;
1910 /* Fill vtbl last to prevent accidentally calling virtual function during
1913 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1918 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1920 dlclose(dri2_dpy
->driver
);
1922 wl_shm_destroy(dri2_dpy
->wl_shm
);
1924 wl_registry_destroy(dri2_dpy
->wl_registry
);
1925 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1926 if (disp
->PlatformDisplay
== NULL
)
1927 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1930 disp
->DriverData
= NULL
;
1936 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1938 EGLBoolean initialized
= EGL_TRUE
;
1940 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1943 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1944 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1947 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);