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 roundtrip(struct dri2_egl_display
*dri2_dpy
)
61 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
65 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
67 struct dri2_egl_surface
*dri2_surf
= data
;
70 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
71 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
74 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
75 wl_buffer_destroy(buffer
);
79 dri2_surf
->color_buffers
[i
].locked
= false;
82 static const struct wl_buffer_listener wl_buffer_listener
= {
83 .release
= wl_buffer_release
87 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
89 struct dri2_egl_surface
*dri2_surf
= data
;
90 struct dri2_egl_display
*dri2_dpy
=
91 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
93 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
97 destroy_window_callback(void *data
)
99 struct dri2_egl_surface
*dri2_surf
= data
;
100 dri2_surf
->wl_win
= NULL
;
104 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
107 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
108 _EGLConfig
*conf
, void *native_window
,
109 const EGLint
*attrib_list
)
111 __DRIcreateNewDrawableFunc createNewDrawable
;
112 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
113 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
114 struct wl_egl_window
*window
= native_window
;
115 struct dri2_egl_surface
*dri2_surf
;
116 const __DRIconfig
*config
;
118 dri2_surf
= calloc(1, sizeof *dri2_surf
);
120 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
124 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
127 if (dri2_dpy
->wl_drm
) {
128 if (conf
->RedSize
== 5)
129 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
130 else if (conf
->AlphaSize
== 0)
131 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
133 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
135 assert(dri2_dpy
->wl_shm
);
136 if (conf
->RedSize
== 5)
137 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
138 else if (conf
->AlphaSize
== 0)
139 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
141 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
145 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
149 dri2_surf
->wl_win
= window
;
150 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
151 if (!dri2_surf
->wl_queue
) {
152 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
156 if (dri2_dpy
->wl_drm
) {
157 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
158 if (!dri2_surf
->wl_drm_wrapper
) {
159 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
162 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
163 dri2_surf
->wl_queue
);
166 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
167 if (!dri2_surf
->wl_dpy_wrapper
) {
168 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
171 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
172 dri2_surf
->wl_queue
);
174 dri2_surf
->wl_surface_wrapper
= wl_proxy_create_wrapper(window
->surface
);
175 if (!dri2_surf
->wl_surface_wrapper
) {
176 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
179 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
180 dri2_surf
->wl_queue
);
182 dri2_surf
->wl_win
->private = dri2_surf
;
183 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
185 dri2_surf
->base
.Width
= -1;
186 dri2_surf
->base
.Height
= -1;
188 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
189 dri2_surf
->base
.GLColorspace
);
192 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
194 if (dri2_dpy
->image_driver
)
195 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
196 else if (dri2_dpy
->dri2
)
197 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
199 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
201 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
203 if (dri2_surf
->dri_drawable
== NULL
) {
204 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
208 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
209 dri2_dpy
->default_swap_interval
);
211 return &dri2_surf
->base
;
214 if (dri2_surf
->wl_drm_wrapper
)
215 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
217 wl_event_queue_destroy(dri2_surf
->wl_queue
);
225 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
226 _EGLConfig
*conf
, void *native_window
,
227 const EGLint
*attrib_list
)
229 /* From the EGL_EXT_platform_wayland spec, version 3:
231 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
232 * that belongs to Wayland. Any such call fails and generates
235 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
241 * Called via eglDestroySurface(), drv->API.DestroySurface().
244 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
246 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
247 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
251 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
253 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
254 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
255 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
256 if (dri2_surf
->color_buffers
[i
].dri_image
)
257 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
258 if (dri2_surf
->color_buffers
[i
].linear_copy
)
259 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
260 if (dri2_surf
->color_buffers
[i
].data
)
261 munmap(dri2_surf
->color_buffers
[i
].data
,
262 dri2_surf
->color_buffers
[i
].data_size
);
265 if (dri2_dpy
->dri2
) {
266 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
267 if (dri2_surf
->dri_buffers
[i
] &&
268 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
269 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
270 dri2_surf
->dri_buffers
[i
]);
273 if (dri2_surf
->throttle_callback
)
274 wl_callback_destroy(dri2_surf
->throttle_callback
);
276 if (dri2_surf
->wl_win
) {
277 dri2_surf
->wl_win
->private = NULL
;
278 dri2_surf
->wl_win
->resize_callback
= NULL
;
279 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
282 if (dri2_surf
->wl_drm_wrapper
)
283 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
284 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
285 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
286 wl_event_queue_destroy(dri2_surf
->wl_queue
);
294 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
296 struct dri2_egl_display
*dri2_dpy
=
297 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
299 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
300 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
301 !dri2_surf
->color_buffers
[i
].locked
)
302 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
303 if (dri2_surf
->color_buffers
[i
].dri_image
)
304 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
305 if (dri2_surf
->color_buffers
[i
].linear_copy
)
306 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
307 if (dri2_surf
->color_buffers
[i
].data
)
308 munmap(dri2_surf
->color_buffers
[i
].data
,
309 dri2_surf
->color_buffers
[i
].data_size
);
311 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
312 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
313 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
314 dri2_surf
->color_buffers
[i
].data
= NULL
;
315 dri2_surf
->color_buffers
[i
].locked
= false;
318 if (dri2_dpy
->dri2
) {
319 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
320 if (dri2_surf
->dri_buffers
[i
] &&
321 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
322 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
323 dri2_surf
->dri_buffers
[i
]);
328 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
330 struct dri2_egl_display
*dri2_dpy
=
331 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
333 unsigned int dri_image_format
;
335 /* currently supports three WL DRM formats,
336 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
337 * and WL_DRM_FORMAT_RGB565
339 switch (dri2_surf
->format
) {
340 case WL_DRM_FORMAT_ARGB8888
:
341 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
343 case WL_DRM_FORMAT_XRGB8888
:
344 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
346 case WL_DRM_FORMAT_RGB565
:
347 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
350 /* format is not supported */
354 /* There might be a buffer release already queued that wasn't processed */
355 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
357 while (dri2_surf
->back
== NULL
) {
358 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
359 /* Get an unlocked buffer, preferrably one with a dri_buffer
360 * already allocated. */
361 if (dri2_surf
->color_buffers
[i
].locked
)
363 if (dri2_surf
->back
== NULL
)
364 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
365 else if (dri2_surf
->back
->dri_image
== NULL
)
366 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
372 /* If we don't have a buffer, then block on the server to release one for
373 * us, and try again. */
374 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
378 if (dri2_surf
->back
== NULL
)
381 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
383 if (dri2_dpy
->is_different_gpu
&&
384 dri2_surf
->back
->linear_copy
== NULL
) {
385 dri2_surf
->back
->linear_copy
=
386 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
387 dri2_surf
->base
.Width
,
388 dri2_surf
->base
.Height
,
391 __DRI_IMAGE_USE_LINEAR
,
393 if (dri2_surf
->back
->linear_copy
== NULL
)
397 if (dri2_surf
->back
->dri_image
== NULL
) {
398 dri2_surf
->back
->dri_image
=
399 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
400 dri2_surf
->base
.Width
,
401 dri2_surf
->base
.Height
,
403 dri2_dpy
->is_different_gpu
?
406 dri2_surf
->back
->age
= 0;
408 if (dri2_surf
->back
->dri_image
== NULL
)
411 dri2_surf
->back
->locked
= true;
418 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
420 struct dri2_egl_display
*dri2_dpy
=
421 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
425 image
= dri2_surf
->back
->dri_image
;
427 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
428 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
430 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
432 buffer
->pitch
= pitch
;
438 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
439 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
441 struct dri2_egl_display
*dri2_dpy
=
442 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
443 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
446 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
448 dri2_surf
->base
.Width
,
449 dri2_surf
->base
.Height
);
450 dri2_surf
->dri_buffers
[attachment
] = b
;
455 memcpy(buffer
, b
, sizeof *buffer
);
461 update_buffers(struct dri2_egl_surface
*dri2_surf
)
463 struct dri2_egl_display
*dri2_dpy
=
464 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
466 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
467 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
469 dri2_wl_release_buffers(dri2_surf
);
471 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
472 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
473 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
474 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
477 if (get_back_bo(dri2_surf
) < 0) {
478 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
482 /* If we have an extra unlocked buffer at this point, we had to do triple
483 * buffering for a while, but now can go back to just double buffering.
484 * That means we can free any unlocked buffer now. */
485 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
486 if (!dri2_surf
->color_buffers
[i
].locked
&&
487 dri2_surf
->color_buffers
[i
].wl_buffer
) {
488 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
489 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
490 if (dri2_dpy
->is_different_gpu
)
491 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
492 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
493 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
494 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
502 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
503 int *width
, int *height
,
504 unsigned int *attachments
, int count
,
505 int *out_count
, void *loaderPrivate
)
507 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
510 if (update_buffers(dri2_surf
) < 0)
513 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
514 switch (attachments
[i
]) {
515 case __DRI_BUFFER_BACK_LEFT
:
516 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
519 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
520 &dri2_surf
->buffers
[j
]) < 0) {
521 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
532 *width
= dri2_surf
->base
.Width
;
533 *height
= dri2_surf
->base
.Height
;
535 return dri2_surf
->buffers
;
539 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
540 int *width
, int *height
,
541 unsigned int *attachments
, int count
,
542 int *out_count
, void *loaderPrivate
)
544 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
545 unsigned int *attachments_with_format
;
549 switch (dri2_surf
->format
) {
550 case WL_DRM_FORMAT_ARGB8888
:
551 case WL_DRM_FORMAT_XRGB8888
:
554 case WL_DRM_FORMAT_RGB565
:
558 /* format is not supported */
562 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
563 if (!attachments_with_format
) {
568 for (int i
= 0; i
< count
; ++i
) {
569 attachments_with_format
[2*i
] = attachments
[i
];
570 attachments_with_format
[2*i
+ 1] = bpp
;
574 dri2_wl_get_buffers_with_format(driDrawable
,
576 attachments_with_format
, count
,
577 out_count
, loaderPrivate
);
579 free(attachments_with_format
);
585 image_get_buffers(__DRIdrawable
*driDrawable
,
589 uint32_t buffer_mask
,
590 struct __DRIimageList
*buffers
)
592 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
594 if (update_buffers(dri2_surf
) < 0)
597 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
598 buffers
->back
= dri2_surf
->back
->dri_image
;
604 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
607 (void) loaderPrivate
;
610 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
611 .base
= { __DRI_DRI2_LOADER
, 3 },
613 .getBuffers
= dri2_wl_get_buffers
,
614 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
615 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
618 static const __DRIimageLoaderExtension image_loader_extension
= {
619 .base
= { __DRI_IMAGE_LOADER
, 1 },
621 .getBuffers
= image_get_buffers
,
622 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
626 wayland_throttle_callback(void *data
,
627 struct wl_callback
*callback
,
630 struct dri2_egl_surface
*dri2_surf
= data
;
632 dri2_surf
->throttle_callback
= NULL
;
633 wl_callback_destroy(callback
);
636 static const struct wl_callback_listener throttle_listener
= {
637 .done
= wayland_throttle_callback
640 static struct wl_buffer
*
641 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
642 struct dri2_egl_surface
*dri2_surf
,
645 struct wl_buffer
*ret
;
646 int width
, height
, fourcc
;
648 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
649 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
650 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
);
652 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
655 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
656 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
657 ret
= wl_drm_create_prime_buffer(dri2_surf
->wl_drm_wrapper
,
658 fd
, width
, height
, fourcc
, 0, stride
,
664 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
665 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
666 ret
= wl_drm_create_buffer(dri2_surf
->wl_drm_wrapper
,
667 name
, width
, height
, stride
, fourcc
);
674 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
678 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
679 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
682 for (int i
= 0; i
< n_rects
; i
++) {
683 const int *rect
= &rects
[i
* 4];
685 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
687 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
693 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
696 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
702 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
703 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
705 while (dri2_surf
->throttle_callback
!= NULL
)
706 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
707 dri2_surf
->wl_queue
) == -1)
710 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
711 if (dri2_surf
->color_buffers
[i
].age
> 0)
712 dri2_surf
->color_buffers
[i
].age
++;
714 /* Make sure we have a back buffer in case we're swapping without ever
716 if (get_back_bo(dri2_surf
) < 0)
717 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
719 if (draw
->SwapInterval
> 0) {
720 dri2_surf
->throttle_callback
=
721 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
722 wl_callback_add_listener(dri2_surf
->throttle_callback
,
723 &throttle_listener
, dri2_surf
);
726 dri2_surf
->back
->age
= 1;
727 dri2_surf
->current
= dri2_surf
->back
;
728 dri2_surf
->back
= NULL
;
730 if (!dri2_surf
->current
->wl_buffer
) {
733 if (dri2_dpy
->is_different_gpu
)
734 image
= dri2_surf
->current
->linear_copy
;
736 image
= dri2_surf
->current
->dri_image
;
738 dri2_surf
->current
->wl_buffer
=
739 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
741 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
742 &wl_buffer_listener
, dri2_surf
);
745 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
746 dri2_surf
->current
->wl_buffer
,
747 dri2_surf
->dx
, dri2_surf
->dy
);
749 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
750 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
751 /* reset resize growing parameters */
755 /* If the compositor doesn't support damage_buffer, we deliberately
756 * ignore the damage region and post maximum damage, due to
757 * https://bugs.freedesktop.org/78190 */
758 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
759 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
760 0, 0, INT32_MAX
, INT32_MAX
);
762 if (dri2_dpy
->is_different_gpu
) {
763 _EGLContext
*ctx
= _eglGetCurrentContext();
764 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
765 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
766 dri2_surf
->current
->linear_copy
,
767 dri2_surf
->current
->dri_image
,
768 0, 0, dri2_surf
->base
.Width
,
769 dri2_surf
->base
.Height
,
770 0, 0, dri2_surf
->base
.Width
,
771 dri2_surf
->base
.Height
, 0);
774 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
775 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
777 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
779 /* If we're not waiting for a frame callback then we'll at least throttle
780 * to a sync callback so that we always give a chance for the compositor to
781 * handle the commit and send a release event before checking for a free
783 if (dri2_surf
->throttle_callback
== NULL
) {
784 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
785 wl_callback_add_listener(dri2_surf
->throttle_callback
,
786 &throttle_listener
, dri2_surf
);
789 wl_display_flush(dri2_dpy
->wl_dpy
);
795 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
796 _EGLDisplay
*disp
, _EGLSurface
*surface
)
798 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
800 if (get_back_bo(dri2_surf
) < 0) {
801 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
805 return dri2_surf
->back
->age
;
809 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
811 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
814 static struct wl_buffer
*
815 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
819 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
820 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
821 __DRIimage
*image
= dri2_img
->dri_image
;
822 struct wl_buffer
*buffer
;
823 int width
, height
, format
, pitch
;
824 enum wl_drm_format wl_format
;
826 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
829 case __DRI_IMAGE_FORMAT_ARGB8888
:
830 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
832 wl_format
= WL_DRM_FORMAT_ARGB8888
;
834 case __DRI_IMAGE_FORMAT_XRGB8888
:
835 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
837 wl_format
= WL_DRM_FORMAT_XRGB8888
;
843 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
844 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
845 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
847 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
850 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
853 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
865 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
868 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
875 /* The buffer object will have been created with our internal event queue
876 * because it is using the wl_drm object as a proxy factory. We want the
877 * buffer to be used by the application so we'll reset it to the display's
878 * default event queue */
880 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
885 _eglError(EGL_BAD_MATCH
, "unsupported image format");
890 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
892 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
895 if (dri2_dpy
->is_render_node
) {
896 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
897 "authenticate for render-nodes");
900 dri2_dpy
->authenticated
= false;
902 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
903 if (roundtrip(dri2_dpy
) < 0)
906 if (!dri2_dpy
->authenticated
)
909 /* reset authenticated */
910 dri2_dpy
->authenticated
= true;
916 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
918 struct dri2_egl_display
*dri2_dpy
= data
;
921 dri2_dpy
->device_name
= strdup(device
);
922 if (!dri2_dpy
->device_name
)
925 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
926 if (dri2_dpy
->fd
== -1) {
927 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
928 dri2_dpy
->device_name
, strerror(errno
));
932 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
933 dri2_dpy
->authenticated
= true;
935 drmGetMagic(dri2_dpy
->fd
, &magic
);
936 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
941 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
943 struct dri2_egl_display
*dri2_dpy
= data
;
946 case WL_DRM_FORMAT_ARGB8888
:
947 dri2_dpy
->formats
|= HAS_ARGB8888
;
949 case WL_DRM_FORMAT_XRGB8888
:
950 dri2_dpy
->formats
|= HAS_XRGB8888
;
952 case WL_DRM_FORMAT_RGB565
:
953 dri2_dpy
->formats
|= HAS_RGB565
;
959 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
961 struct dri2_egl_display
*dri2_dpy
= data
;
963 dri2_dpy
->capabilities
= value
;
967 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
969 struct dri2_egl_display
*dri2_dpy
= data
;
971 dri2_dpy
->authenticated
= true;
974 static const struct wl_drm_listener drm_listener
= {
975 .device
= drm_handle_device
,
976 .format
= drm_handle_format
,
977 .authenticated
= drm_handle_authenticated
,
978 .capabilities
= drm_handle_capabilities
982 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
983 uint32_t name
, const char *interface
,
986 struct dri2_egl_display
*dri2_dpy
= data
;
988 if (strcmp(interface
, "wl_drm") == 0) {
990 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
991 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
996 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1001 static const struct wl_registry_listener registry_listener_drm
= {
1002 .global
= registry_handle_global_drm
,
1003 .global_remove
= registry_handle_global_remove
1007 dri2_wl_swap_interval(_EGLDriver
*drv
,
1012 if (interval
> surf
->Config
->MaxSwapInterval
)
1013 interval
= surf
->Config
->MaxSwapInterval
;
1014 else if (interval
< surf
->Config
->MinSwapInterval
)
1015 interval
= surf
->Config
->MinSwapInterval
;
1017 surf
->SwapInterval
= interval
;
1023 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1025 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1027 /* We can't use values greater than 1 on Wayland because we are using the
1028 * frame callback to synchronise the frame and the only way we be sure to
1029 * get a frame callback is to attach a new buffer. Therefore we can't just
1030 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1032 if (dri2_dpy
->config
)
1033 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1034 "vblank_mode", &vblank_mode
);
1035 switch (vblank_mode
) {
1036 case DRI_CONF_VBLANK_NEVER
:
1037 dri2_dpy
->min_swap_interval
= 0;
1038 dri2_dpy
->max_swap_interval
= 0;
1039 dri2_dpy
->default_swap_interval
= 0;
1041 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1042 dri2_dpy
->min_swap_interval
= 1;
1043 dri2_dpy
->max_swap_interval
= 1;
1044 dri2_dpy
->default_swap_interval
= 1;
1046 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1047 dri2_dpy
->min_swap_interval
= 0;
1048 dri2_dpy
->max_swap_interval
= 1;
1049 dri2_dpy
->default_swap_interval
= 0;
1052 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1053 dri2_dpy
->min_swap_interval
= 0;
1054 dri2_dpy
->max_swap_interval
= 1;
1055 dri2_dpy
->default_swap_interval
= 1;
1060 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1061 .authenticate
= dri2_wl_authenticate
,
1062 .create_window_surface
= dri2_wl_create_window_surface
,
1063 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1064 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1065 .destroy_surface
= dri2_wl_destroy_surface
,
1066 .create_image
= dri2_create_image_khr
,
1067 .swap_interval
= dri2_wl_swap_interval
,
1068 .swap_buffers
= dri2_wl_swap_buffers
,
1069 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1070 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1071 .set_damage_region
= dri2_fallback_set_damage_region
,
1072 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1073 .copy_buffers
= dri2_fallback_copy_buffers
,
1074 .query_buffer_age
= dri2_wl_query_buffer_age
,
1075 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1076 .get_sync_values
= dri2_fallback_get_sync_values
,
1077 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1080 static const __DRIextension
*dri2_loader_extensions
[] = {
1081 &dri2_loader_extension
.base
,
1082 &image_loader_extension
.base
,
1083 &image_lookup_extension
.base
,
1084 &use_invalidate
.base
,
1088 static const __DRIextension
*image_loader_extensions
[] = {
1089 &image_loader_extension
.base
,
1090 &image_lookup_extension
.base
,
1091 &use_invalidate
.base
,
1096 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1098 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1099 static const struct {
1100 const char *format_name
;
1102 unsigned int rgba_masks
[4];
1104 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1105 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1106 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1108 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1109 unsigned int count
= 0;
1111 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1112 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1113 struct dri2_egl_config
*dri2_conf
;
1115 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1118 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1119 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1121 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1128 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1129 if (!format_count
[i
]) {
1130 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1131 visuals
[i
].format_name
);
1135 return (count
!= 0);
1139 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1141 struct dri2_egl_display
*dri2_dpy
;
1143 loader_set_logger(_eglLog
);
1145 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1147 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
= true;
1157 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1160 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1162 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1163 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1166 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1167 dri2_dpy
->wl_queue
);
1169 if (dri2_dpy
->own_device
)
1170 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1172 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1173 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1174 ®istry_listener_drm
, dri2_dpy
);
1175 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1178 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1181 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1184 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1185 &dri2_dpy
->is_different_gpu
);
1186 if (dri2_dpy
->is_different_gpu
) {
1187 free(dri2_dpy
->device_name
);
1188 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1189 if (!dri2_dpy
->device_name
) {
1190 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1191 "for requested GPU");
1196 /* we have to do the check now, because loader_get_user_preferred_fd
1197 * will return a render-node when the requested gpu is different
1198 * to the server, but also if the client asks for the same gpu than
1199 * the server by requesting its pci-id */
1200 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1202 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1203 if (dri2_dpy
->driver_name
== NULL
) {
1204 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get 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
;
1212 if (!dri2_load_driver(disp
)) {
1213 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1217 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1218 if (!dri2_load_driver_dri3(disp
)) {
1219 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1224 if (!dri2_create_screen(disp
))
1227 if (!dri2_setup_extensions(disp
))
1230 dri2_setup_screen(disp
);
1232 dri2_wl_setup_swap_interval(dri2_dpy
);
1234 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1235 * createImageFromFds support indicates that Prime export/import
1236 * is supported by the driver. Fall back to
1237 * gem names if we don't have Prime support. */
1239 if (dri2_dpy
->image
->base
.version
< 7 ||
1240 dri2_dpy
->image
->createImageFromFds
== NULL
)
1241 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1243 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1244 * The server needs to accept them */
1245 if (dri2_dpy
->is_render_node
&&
1246 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1247 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1251 if (dri2_dpy
->is_different_gpu
&&
1252 (dri2_dpy
->image
->base
.version
< 9 ||
1253 dri2_dpy
->image
->blitImage
== NULL
)) {
1254 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1255 "Image extension in the driver is not "
1256 "compatible. Version 9 or later and blitImage() "
1261 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1262 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1266 dri2_set_WL_bind_wayland_display(drv
, disp
);
1267 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1268 * because the buffer of the EGLImage has likely a tiling mode the server
1269 * gpu won't support. These is no way to check for now. Thus do not support the
1271 if (!dri2_dpy
->is_different_gpu
)
1272 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1274 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1276 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1278 /* Fill vtbl last to prevent accidentally calling virtual function during
1281 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1286 dri2_display_destroy(disp
);
1291 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1293 if (format
== WL_SHM_FORMAT_RGB565
)
1295 else /* ARGB8888 || XRGB8888 */
1300 * Taken from weston shared/os-compatibility.c
1303 #ifndef HAVE_MKOSTEMP
1306 set_cloexec_or_close(int fd
)
1313 flags
= fcntl(fd
, F_GETFD
);
1317 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1330 * Taken from weston shared/os-compatibility.c
1334 create_tmpfile_cloexec(char *tmpname
)
1338 #ifdef HAVE_MKOSTEMP
1339 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1343 fd
= mkstemp(tmpname
);
1345 fd
= set_cloexec_or_close(fd
);
1354 * Taken from weston shared/os-compatibility.c
1356 * Create a new, unique, anonymous file of the given size, and
1357 * return the file descriptor for it. The file descriptor is set
1358 * CLOEXEC. The file is immediately suitable for mmap()'ing
1359 * the given size at offset zero.
1361 * The file should not have a permanent backing store like a disk,
1362 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1364 * The file name is deleted from the file system.
1366 * The file is suitable for buffer sharing between processes by
1367 * transmitting the file descriptor over Unix sockets using the
1368 * SCM_RIGHTS methods.
1370 * If the C library implements posix_fallocate(), it is used to
1371 * guarantee that disk space is available for the file at the
1372 * given size. If disk space is insufficent, errno is set to ENOSPC.
1373 * If posix_fallocate() is not supported, program may receive
1374 * SIGBUS on accessing mmap()'ed file contents instead.
1377 os_create_anonymous_file(off_t size
)
1379 static const char template[] = "/mesa-shared-XXXXXX";
1385 path
= getenv("XDG_RUNTIME_DIR");
1391 name
= malloc(strlen(path
) + sizeof(template));
1396 strcat(name
, template);
1398 fd
= create_tmpfile_cloexec(name
);
1405 ret
= ftruncate(fd
, size
);
1416 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1417 int format
, int w
, int h
,
1418 void **data
, int *size
,
1419 struct wl_buffer
**buffer
)
1421 struct dri2_egl_display
*dri2_dpy
=
1422 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1423 struct wl_shm_pool
*pool
;
1424 int fd
, stride
, size_map
;
1427 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1428 size_map
= h
* stride
;
1430 /* Create a sharable buffer */
1431 fd
= os_create_anonymous_file(size_map
);
1435 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1436 if (data_map
== MAP_FAILED
) {
1441 /* Share it in a wl_buffer */
1442 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1443 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1444 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1445 wl_shm_pool_destroy(pool
);
1454 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1456 struct dri2_egl_display
*dri2_dpy
=
1457 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1459 /* we need to do the following operations only once per frame */
1460 if (dri2_surf
->back
)
1463 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1464 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1466 dri2_wl_release_buffers(dri2_surf
);
1468 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1469 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1470 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1471 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1472 dri2_surf
->current
= NULL
;
1475 /* find back buffer */
1477 /* There might be a buffer release already queued that wasn't processed */
1478 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1480 /* try get free buffer already created */
1481 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1482 if (!dri2_surf
->color_buffers
[i
].locked
&&
1483 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1484 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1489 /* else choose any another free location */
1490 if (!dri2_surf
->back
) {
1491 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1492 if (!dri2_surf
->color_buffers
[i
].locked
) {
1493 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1494 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1496 dri2_surf
->base
.Width
,
1497 dri2_surf
->base
.Height
,
1498 &dri2_surf
->back
->data
,
1499 &dri2_surf
->back
->data_size
,
1500 &dri2_surf
->back
->wl_buffer
)) {
1501 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1504 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1505 &wl_buffer_listener
, dri2_surf
);
1511 if (!dri2_surf
->back
) {
1512 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1516 dri2_surf
->back
->locked
= true;
1518 /* If we have an extra unlocked buffer at this point, we had to do triple
1519 * buffering for a while, but now can go back to just double buffering.
1520 * That means we can free any unlocked buffer now. */
1521 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1522 if (!dri2_surf
->color_buffers
[i
].locked
&&
1523 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1524 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1525 munmap(dri2_surf
->color_buffers
[i
].data
,
1526 dri2_surf
->color_buffers
[i
].data_size
);
1527 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1528 dri2_surf
->color_buffers
[i
].data
= NULL
;
1536 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1538 /* if there has been a resize: */
1539 if (!dri2_surf
->current
)
1542 return dri2_surf
->current
->data
;
1546 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1548 assert(dri2_surf
->back
);
1549 return dri2_surf
->back
->data
;
1553 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1555 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1557 while (dri2_surf
->throttle_callback
!= NULL
)
1558 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1559 dri2_surf
->wl_queue
) == -1)
1562 if (dri2_surf
->base
.SwapInterval
> 0) {
1563 dri2_surf
->throttle_callback
=
1564 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1565 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1566 &throttle_listener
, dri2_surf
);
1569 dri2_surf
->current
= dri2_surf
->back
;
1570 dri2_surf
->back
= NULL
;
1572 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1573 dri2_surf
->current
->wl_buffer
,
1574 dri2_surf
->dx
, dri2_surf
->dy
);
1576 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1577 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1578 /* reset resize growing parameters */
1582 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1583 0, 0, INT32_MAX
, INT32_MAX
);
1584 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1586 /* If we're not waiting for a frame callback then we'll at least throttle
1587 * to a sync callback so that we always give a chance for the compositor to
1588 * handle the commit and send a release event before checking for a free
1590 if (dri2_surf
->throttle_callback
== NULL
) {
1591 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1592 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1593 &throttle_listener
, dri2_surf
);
1596 wl_display_flush(dri2_dpy
->wl_dpy
);
1600 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1601 int *x
, int *y
, int *w
, int *h
,
1602 void *loaderPrivate
)
1604 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1606 (void) swrast_update_buffers(dri2_surf
);
1609 *w
= dri2_surf
->base
.Width
;
1610 *h
= dri2_surf
->base
.Height
;
1614 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1615 int x
, int y
, int w
, int h
,
1616 char *data
, void *loaderPrivate
)
1618 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1619 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1620 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1621 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1622 int dst_stride
= copy_width
;
1625 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1627 memset(data
, 0, copy_width
* h
);
1631 assert(data
!= src
);
1632 assert(copy_width
<= src_stride
);
1635 src
+= y
* src_stride
;
1638 if (copy_width
> src_stride
-x_offset
)
1639 copy_width
= src_stride
-x_offset
;
1640 if (h
> dri2_surf
->base
.Height
-y
)
1641 h
= dri2_surf
->base
.Height
-y
;
1644 memcpy(dst
, src
, copy_width
);
1651 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1652 int x
, int y
, int w
, int h
, int stride
,
1653 char *data
, void *loaderPrivate
)
1655 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1656 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1657 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1658 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1661 assert(copy_width
<= stride
);
1663 (void) swrast_update_buffers(dri2_surf
);
1664 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1666 /* partial copy, copy old content */
1667 if (copy_width
< dst_stride
)
1668 dri2_wl_swrast_get_image(draw
, 0, 0,
1669 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1670 dst
, loaderPrivate
);
1673 dst
+= y
* dst_stride
;
1677 /* drivers expect we do these checks (and some rely on it) */
1678 if (copy_width
> dst_stride
-x_offset
)
1679 copy_width
= dst_stride
-x_offset
;
1680 if (h
> dri2_surf
->base
.Height
-y
)
1681 h
= dri2_surf
->base
.Height
-y
;
1684 memcpy(dst
, src
, copy_width
);
1688 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1692 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1693 int x
, int y
, int w
, int h
,
1694 char *data
, void *loaderPrivate
)
1696 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1699 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1700 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1701 stride
, data
, loaderPrivate
);
1705 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1707 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1708 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1710 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1715 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1717 struct dri2_egl_display
*dri2_dpy
= data
;
1720 case WL_SHM_FORMAT_ARGB8888
:
1721 dri2_dpy
->formats
|= HAS_ARGB8888
;
1723 case WL_SHM_FORMAT_XRGB8888
:
1724 dri2_dpy
->formats
|= HAS_XRGB8888
;
1726 case WL_SHM_FORMAT_RGB565
:
1727 dri2_dpy
->formats
|= HAS_RGB565
;
1732 static const struct wl_shm_listener shm_listener
= {
1733 .format
= shm_handle_format
1737 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1738 uint32_t name
, const char *interface
,
1741 struct dri2_egl_display
*dri2_dpy
= data
;
1743 if (strcmp(interface
, "wl_shm") == 0) {
1745 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1746 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1750 static const struct wl_registry_listener registry_listener_swrast
= {
1751 .global
= registry_handle_global_swrast
,
1752 .global_remove
= registry_handle_global_remove
1755 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1756 .authenticate
= NULL
,
1757 .create_window_surface
= dri2_wl_create_window_surface
,
1758 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1759 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1760 .destroy_surface
= dri2_wl_destroy_surface
,
1761 .create_image
= dri2_fallback_create_image_khr
,
1762 .swap_interval
= dri2_wl_swap_interval
,
1763 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1764 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1765 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1766 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1767 .copy_buffers
= dri2_fallback_copy_buffers
,
1768 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1769 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1770 .get_sync_values
= dri2_fallback_get_sync_values
,
1771 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1774 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1775 .base
= { __DRI_SWRAST_LOADER
, 2 },
1777 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1778 .putImage
= dri2_wl_swrast_put_image
,
1779 .getImage
= dri2_wl_swrast_get_image
,
1780 .putImage2
= dri2_wl_swrast_put_image2
,
1783 static const __DRIextension
*swrast_loader_extensions
[] = {
1784 &swrast_loader_extension
.base
,
1789 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1791 struct dri2_egl_display
*dri2_dpy
;
1793 loader_set_logger(_eglLog
);
1795 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1797 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1800 disp
->DriverData
= (void *) dri2_dpy
;
1801 if (disp
->PlatformDisplay
== NULL
) {
1802 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1803 if (dri2_dpy
->wl_dpy
== NULL
)
1805 dri2_dpy
->own_device
= true;
1807 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1810 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1812 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1813 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1816 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1817 dri2_dpy
->wl_queue
);
1819 if (dri2_dpy
->own_device
)
1820 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1822 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1823 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1824 ®istry_listener_swrast
, dri2_dpy
);
1826 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1829 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1832 dri2_dpy
->driver_name
= strdup("swrast");
1833 if (!dri2_load_driver_swrast(disp
))
1836 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1838 if (!dri2_create_screen(disp
))
1841 if (!dri2_setup_extensions(disp
))
1844 dri2_setup_screen(disp
);
1846 dri2_wl_setup_swap_interval(dri2_dpy
);
1848 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1849 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1853 /* Fill vtbl last to prevent accidentally calling virtual function during
1856 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1861 dri2_display_destroy(disp
);
1866 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1868 EGLBoolean initialized
= EGL_TRUE
;
1870 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1873 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1874 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1877 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);