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
);
252 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
254 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
255 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
256 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
257 if (dri2_surf
->color_buffers
[i
].dri_image
)
258 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
259 if (dri2_surf
->color_buffers
[i
].linear_copy
)
260 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
261 if (dri2_surf
->color_buffers
[i
].data
)
262 munmap(dri2_surf
->color_buffers
[i
].data
,
263 dri2_surf
->color_buffers
[i
].data_size
);
266 if (dri2_dpy
->dri2
) {
267 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
268 if (dri2_surf
->dri_buffers
[i
] &&
269 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
270 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
271 dri2_surf
->dri_buffers
[i
]);
274 if (dri2_surf
->throttle_callback
)
275 wl_callback_destroy(dri2_surf
->throttle_callback
);
277 if (dri2_surf
->wl_win
) {
278 dri2_surf
->wl_win
->private = NULL
;
279 dri2_surf
->wl_win
->resize_callback
= NULL
;
280 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
283 if (dri2_surf
->wl_drm_wrapper
)
284 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
285 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
286 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
287 wl_event_queue_destroy(dri2_surf
->wl_queue
);
295 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
297 struct dri2_egl_display
*dri2_dpy
=
298 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
301 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
302 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
303 !dri2_surf
->color_buffers
[i
].locked
)
304 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
305 if (dri2_surf
->color_buffers
[i
].dri_image
)
306 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
307 if (dri2_surf
->color_buffers
[i
].linear_copy
)
308 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
309 if (dri2_surf
->color_buffers
[i
].data
)
310 munmap(dri2_surf
->color_buffers
[i
].data
,
311 dri2_surf
->color_buffers
[i
].data_size
);
313 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
314 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
315 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
316 dri2_surf
->color_buffers
[i
].data
= NULL
;
317 dri2_surf
->color_buffers
[i
].locked
= false;
320 if (dri2_dpy
->dri2
) {
321 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
322 if (dri2_surf
->dri_buffers
[i
] &&
323 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
324 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
325 dri2_surf
->dri_buffers
[i
]);
330 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
332 struct dri2_egl_display
*dri2_dpy
=
333 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
335 unsigned int dri_image_format
;
337 /* currently supports three WL DRM formats,
338 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
339 * and WL_DRM_FORMAT_RGB565
341 switch (dri2_surf
->format
) {
342 case WL_DRM_FORMAT_ARGB8888
:
343 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
345 case WL_DRM_FORMAT_XRGB8888
:
346 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
348 case WL_DRM_FORMAT_RGB565
:
349 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
352 /* format is not supported */
356 /* There might be a buffer release already queued that wasn't processed */
357 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
359 while (dri2_surf
->back
== NULL
) {
360 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
361 /* Get an unlocked buffer, preferrably one with a dri_buffer
362 * already allocated. */
363 if (dri2_surf
->color_buffers
[i
].locked
)
365 if (dri2_surf
->back
== NULL
)
366 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
367 else if (dri2_surf
->back
->dri_image
== NULL
)
368 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
374 /* If we don't have a buffer, then block on the server to release one for
375 * us, and try again. */
376 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
380 if (dri2_surf
->back
== NULL
)
383 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
385 if (dri2_dpy
->is_different_gpu
&&
386 dri2_surf
->back
->linear_copy
== NULL
) {
387 dri2_surf
->back
->linear_copy
=
388 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
389 dri2_surf
->base
.Width
,
390 dri2_surf
->base
.Height
,
393 __DRI_IMAGE_USE_LINEAR
,
395 if (dri2_surf
->back
->linear_copy
== NULL
)
399 if (dri2_surf
->back
->dri_image
== NULL
) {
400 dri2_surf
->back
->dri_image
=
401 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
402 dri2_surf
->base
.Width
,
403 dri2_surf
->base
.Height
,
405 dri2_dpy
->is_different_gpu
?
408 dri2_surf
->back
->age
= 0;
410 if (dri2_surf
->back
->dri_image
== NULL
)
413 dri2_surf
->back
->locked
= true;
420 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
422 struct dri2_egl_display
*dri2_dpy
=
423 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
427 image
= dri2_surf
->back
->dri_image
;
429 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
430 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
432 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
434 buffer
->pitch
= pitch
;
440 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
441 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
443 struct dri2_egl_display
*dri2_dpy
=
444 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
445 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
448 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
450 dri2_surf
->base
.Width
,
451 dri2_surf
->base
.Height
);
452 dri2_surf
->dri_buffers
[attachment
] = b
;
457 memcpy(buffer
, b
, sizeof *buffer
);
463 update_buffers(struct dri2_egl_surface
*dri2_surf
)
465 struct dri2_egl_display
*dri2_dpy
=
466 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
469 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
470 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
472 dri2_wl_release_buffers(dri2_surf
);
474 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
475 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
476 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
477 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
480 if (get_back_bo(dri2_surf
) < 0) {
481 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
485 /* If we have an extra unlocked buffer at this point, we had to do triple
486 * buffering for a while, but now can go back to just double buffering.
487 * That means we can free any unlocked buffer now. */
488 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
489 if (!dri2_surf
->color_buffers
[i
].locked
&&
490 dri2_surf
->color_buffers
[i
].wl_buffer
) {
491 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
492 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
493 if (dri2_dpy
->is_different_gpu
)
494 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
495 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
496 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
497 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
505 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
506 int *width
, int *height
,
507 unsigned int *attachments
, int count
,
508 int *out_count
, void *loaderPrivate
)
510 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
513 if (update_buffers(dri2_surf
) < 0)
516 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
517 switch (attachments
[i
]) {
518 case __DRI_BUFFER_BACK_LEFT
:
519 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
522 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
523 &dri2_surf
->buffers
[j
]) < 0) {
524 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
535 *width
= dri2_surf
->base
.Width
;
536 *height
= dri2_surf
->base
.Height
;
538 return dri2_surf
->buffers
;
542 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
543 int *width
, int *height
,
544 unsigned int *attachments
, int count
,
545 int *out_count
, void *loaderPrivate
)
547 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
548 unsigned int *attachments_with_format
;
554 switch (dri2_surf
->format
) {
555 case WL_DRM_FORMAT_ARGB8888
:
556 case WL_DRM_FORMAT_XRGB8888
:
559 case WL_DRM_FORMAT_RGB565
:
563 /* format is not supported */
567 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
568 if (!attachments_with_format
) {
573 for (i
= 0; i
< count
; ++i
) {
574 attachments_with_format
[2*i
] = attachments
[i
];
575 attachments_with_format
[2*i
+ 1] = bpp
;
579 dri2_wl_get_buffers_with_format(driDrawable
,
581 attachments_with_format
, count
,
582 out_count
, loaderPrivate
);
584 free(attachments_with_format
);
590 image_get_buffers(__DRIdrawable
*driDrawable
,
594 uint32_t buffer_mask
,
595 struct __DRIimageList
*buffers
)
597 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
599 if (update_buffers(dri2_surf
) < 0)
602 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
603 buffers
->back
= dri2_surf
->back
->dri_image
;
609 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
612 (void) loaderPrivate
;
615 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
616 .base
= { __DRI_DRI2_LOADER
, 3 },
618 .getBuffers
= dri2_wl_get_buffers
,
619 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
620 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
623 static const __DRIimageLoaderExtension image_loader_extension
= {
624 .base
= { __DRI_IMAGE_LOADER
, 1 },
626 .getBuffers
= image_get_buffers
,
627 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
631 wayland_throttle_callback(void *data
,
632 struct wl_callback
*callback
,
635 struct dri2_egl_surface
*dri2_surf
= data
;
637 dri2_surf
->throttle_callback
= NULL
;
638 wl_callback_destroy(callback
);
641 static const struct wl_callback_listener throttle_listener
= {
642 .done
= wayland_throttle_callback
646 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
648 struct dri2_egl_display
*dri2_dpy
=
649 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
651 int fd
, stride
, name
;
653 if (dri2_surf
->current
->wl_buffer
!= NULL
)
656 if (dri2_dpy
->is_different_gpu
) {
657 image
= dri2_surf
->current
->linear_copy
;
659 image
= dri2_surf
->current
->dri_image
;
661 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
662 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
663 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
665 dri2_surf
->current
->wl_buffer
=
666 wl_drm_create_prime_buffer(dri2_surf
->wl_drm_wrapper
,
668 dri2_surf
->base
.Width
,
669 dri2_surf
->base
.Height
,
676 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
677 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
679 dri2_surf
->current
->wl_buffer
=
680 wl_drm_create_buffer(dri2_surf
->wl_drm_wrapper
,
682 dri2_surf
->base
.Width
,
683 dri2_surf
->base
.Height
,
688 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
689 &wl_buffer_listener
, dri2_surf
);
693 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
699 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
700 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
703 for (i
= 0; i
< n_rects
; i
++) {
704 const int *rect
= &rects
[i
* 4];
706 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
708 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
714 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
717 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
723 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
724 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
727 while (dri2_surf
->throttle_callback
!= NULL
)
728 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
729 dri2_surf
->wl_queue
) == -1)
732 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
733 if (dri2_surf
->color_buffers
[i
].age
> 0)
734 dri2_surf
->color_buffers
[i
].age
++;
736 /* Make sure we have a back buffer in case we're swapping without ever
738 if (get_back_bo(dri2_surf
) < 0) {
739 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
743 if (draw
->SwapInterval
> 0) {
744 dri2_surf
->throttle_callback
=
745 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
746 wl_callback_add_listener(dri2_surf
->throttle_callback
,
747 &throttle_listener
, dri2_surf
);
750 dri2_surf
->back
->age
= 1;
751 dri2_surf
->current
= dri2_surf
->back
;
752 dri2_surf
->back
= NULL
;
754 create_wl_buffer(dri2_surf
);
756 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
757 dri2_surf
->current
->wl_buffer
,
758 dri2_surf
->dx
, dri2_surf
->dy
);
760 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
761 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
762 /* reset resize growing parameters */
766 /* If the compositor doesn't support damage_buffer, we deliberately
767 * ignore the damage region and post maximum damage, due to
768 * https://bugs.freedesktop.org/78190 */
769 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
770 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
771 0, 0, INT32_MAX
, INT32_MAX
);
773 if (dri2_dpy
->is_different_gpu
) {
774 _EGLContext
*ctx
= _eglGetCurrentContext();
775 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
776 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
777 dri2_surf
->current
->linear_copy
,
778 dri2_surf
->current
->dri_image
,
779 0, 0, dri2_surf
->base
.Width
,
780 dri2_surf
->base
.Height
,
781 0, 0, dri2_surf
->base
.Width
,
782 dri2_surf
->base
.Height
, 0);
785 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
786 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
788 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
790 /* If we're not waiting for a frame callback then we'll at least throttle
791 * to a sync callback so that we always give a chance for the compositor to
792 * handle the commit and send a release event before checking for a free
794 if (dri2_surf
->throttle_callback
== NULL
) {
795 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
796 wl_callback_add_listener(dri2_surf
->throttle_callback
,
797 &throttle_listener
, dri2_surf
);
800 wl_display_flush(dri2_dpy
->wl_dpy
);
806 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
807 _EGLDisplay
*disp
, _EGLSurface
*surface
)
809 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
811 if (get_back_bo(dri2_surf
) < 0) {
812 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
816 return dri2_surf
->back
->age
;
820 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
822 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
825 static struct wl_buffer
*
826 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
830 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
831 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
832 __DRIimage
*image
= dri2_img
->dri_image
;
833 struct wl_buffer
*buffer
;
834 int width
, height
, format
, pitch
;
835 enum wl_drm_format wl_format
;
837 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
840 case __DRI_IMAGE_FORMAT_ARGB8888
:
841 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
843 wl_format
= WL_DRM_FORMAT_ARGB8888
;
845 case __DRI_IMAGE_FORMAT_XRGB8888
:
846 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
848 wl_format
= WL_DRM_FORMAT_XRGB8888
;
854 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
855 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
856 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
858 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
861 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
864 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
876 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
879 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
886 /* The buffer object will have been created with our internal event queue
887 * because it is using the wl_drm object as a proxy factory. We want the
888 * buffer to be used by the application so we'll reset it to the display's
889 * default event queue */
891 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
896 _eglError(EGL_BAD_MATCH
, "unsupported image format");
901 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
903 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
906 if (dri2_dpy
->is_render_node
) {
907 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
908 "authenticate for render-nodes");
911 dri2_dpy
->authenticated
= false;
913 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
914 if (roundtrip(dri2_dpy
) < 0)
917 if (!dri2_dpy
->authenticated
)
920 /* reset authenticated */
921 dri2_dpy
->authenticated
= true;
927 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
929 struct dri2_egl_display
*dri2_dpy
= data
;
932 dri2_dpy
->device_name
= strdup(device
);
933 if (!dri2_dpy
->device_name
)
936 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
937 if (dri2_dpy
->fd
== -1) {
938 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
939 dri2_dpy
->device_name
, strerror(errno
));
943 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
944 dri2_dpy
->authenticated
= true;
946 drmGetMagic(dri2_dpy
->fd
, &magic
);
947 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
952 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
954 struct dri2_egl_display
*dri2_dpy
= data
;
957 case WL_DRM_FORMAT_ARGB8888
:
958 dri2_dpy
->formats
|= HAS_ARGB8888
;
960 case WL_DRM_FORMAT_XRGB8888
:
961 dri2_dpy
->formats
|= HAS_XRGB8888
;
963 case WL_DRM_FORMAT_RGB565
:
964 dri2_dpy
->formats
|= HAS_RGB565
;
970 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
972 struct dri2_egl_display
*dri2_dpy
= data
;
974 dri2_dpy
->capabilities
= value
;
978 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
980 struct dri2_egl_display
*dri2_dpy
= data
;
982 dri2_dpy
->authenticated
= true;
985 static const struct wl_drm_listener drm_listener
= {
986 .device
= drm_handle_device
,
987 .format
= drm_handle_format
,
988 .authenticated
= drm_handle_authenticated
,
989 .capabilities
= drm_handle_capabilities
993 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
994 const char *interface
, uint32_t version
)
996 struct dri2_egl_display
*dri2_dpy
= data
;
1000 if (strcmp(interface
, "wl_drm") == 0) {
1002 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
1003 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1008 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1013 static const struct wl_registry_listener registry_listener_drm
= {
1014 .global
= registry_handle_global_drm
,
1015 .global_remove
= registry_handle_global_remove
1019 dri2_wl_swap_interval(_EGLDriver
*drv
,
1024 if (interval
> surf
->Config
->MaxSwapInterval
)
1025 interval
= surf
->Config
->MaxSwapInterval
;
1026 else if (interval
< surf
->Config
->MinSwapInterval
)
1027 interval
= surf
->Config
->MinSwapInterval
;
1029 surf
->SwapInterval
= interval
;
1035 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1037 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1039 /* We can't use values greater than 1 on Wayland because we are using the
1040 * frame callback to synchronise the frame and the only way we be sure to
1041 * get a frame callback is to attach a new buffer. Therefore we can't just
1042 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1044 if (dri2_dpy
->config
)
1045 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1046 "vblank_mode", &vblank_mode
);
1047 switch (vblank_mode
) {
1048 case DRI_CONF_VBLANK_NEVER
:
1049 dri2_dpy
->min_swap_interval
= 0;
1050 dri2_dpy
->max_swap_interval
= 0;
1051 dri2_dpy
->default_swap_interval
= 0;
1053 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1054 dri2_dpy
->min_swap_interval
= 1;
1055 dri2_dpy
->max_swap_interval
= 1;
1056 dri2_dpy
->default_swap_interval
= 1;
1058 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1059 dri2_dpy
->min_swap_interval
= 0;
1060 dri2_dpy
->max_swap_interval
= 1;
1061 dri2_dpy
->default_swap_interval
= 0;
1064 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1065 dri2_dpy
->min_swap_interval
= 0;
1066 dri2_dpy
->max_swap_interval
= 1;
1067 dri2_dpy
->default_swap_interval
= 1;
1072 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1073 .authenticate
= dri2_wl_authenticate
,
1074 .create_window_surface
= dri2_wl_create_window_surface
,
1075 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1076 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1077 .destroy_surface
= dri2_wl_destroy_surface
,
1078 .create_image
= dri2_create_image_khr
,
1079 .swap_interval
= dri2_wl_swap_interval
,
1080 .swap_buffers
= dri2_wl_swap_buffers
,
1081 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1082 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1083 .set_damage_region
= dri2_fallback_set_damage_region
,
1084 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1085 .copy_buffers
= dri2_fallback_copy_buffers
,
1086 .query_buffer_age
= dri2_wl_query_buffer_age
,
1087 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1088 .get_sync_values
= dri2_fallback_get_sync_values
,
1089 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1092 static const __DRIextension
*dri2_loader_extensions
[] = {
1093 &dri2_loader_extension
.base
,
1094 &image_loader_extension
.base
,
1095 &image_lookup_extension
.base
,
1096 &use_invalidate
.base
,
1100 static const __DRIextension
*image_loader_extensions
[] = {
1101 &image_loader_extension
.base
,
1102 &image_lookup_extension
.base
,
1103 &use_invalidate
.base
,
1108 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1110 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1111 static const struct {
1112 const char *format_name
;
1114 unsigned int rgba_masks
[4];
1116 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1117 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1118 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1120 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1121 unsigned int count
, i
, j
;
1124 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1125 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1126 struct dri2_egl_config
*dri2_conf
;
1128 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1131 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1132 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1140 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1141 if (!format_count
[i
]) {
1142 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1143 visuals
[i
].format_name
);
1147 return (count
!= 0);
1151 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1153 struct dri2_egl_display
*dri2_dpy
;
1155 loader_set_logger(_eglLog
);
1157 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1159 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1162 disp
->DriverData
= (void *) dri2_dpy
;
1163 if (disp
->PlatformDisplay
== NULL
) {
1164 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1165 if (dri2_dpy
->wl_dpy
== NULL
)
1167 dri2_dpy
->own_device
= true;
1169 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1172 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1174 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1175 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1178 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1179 dri2_dpy
->wl_queue
);
1181 if (dri2_dpy
->own_device
)
1182 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1184 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1185 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1186 ®istry_listener_drm
, dri2_dpy
);
1187 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1190 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1193 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1196 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1197 &dri2_dpy
->is_different_gpu
);
1198 if (dri2_dpy
->is_different_gpu
) {
1199 free(dri2_dpy
->device_name
);
1200 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1201 if (!dri2_dpy
->device_name
) {
1202 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1203 "for requested GPU");
1208 /* we have to do the check now, because loader_get_user_preferred_fd
1209 * will return a render-node when the requested gpu is different
1210 * to the server, but also if the client asks for the same gpu than
1211 * the server by requesting its pci-id */
1212 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1214 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1215 if (dri2_dpy
->driver_name
== NULL
) {
1216 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1220 /* render nodes cannot use Gem names, and thus do not support
1221 * the __DRI_DRI2_LOADER extension */
1222 if (!dri2_dpy
->is_render_node
) {
1223 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1224 if (!dri2_load_driver(disp
)) {
1225 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1229 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1230 if (!dri2_load_driver_dri3(disp
)) {
1231 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1236 if (!dri2_create_screen(disp
))
1239 if (!dri2_setup_extensions(disp
))
1242 dri2_setup_screen(disp
);
1244 dri2_wl_setup_swap_interval(dri2_dpy
);
1246 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1247 * createImageFromFds support indicates that Prime export/import
1248 * is supported by the driver. Fall back to
1249 * gem names if we don't have Prime support. */
1251 if (dri2_dpy
->image
->base
.version
< 7 ||
1252 dri2_dpy
->image
->createImageFromFds
== NULL
)
1253 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1255 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1256 * The server needs to accept them */
1257 if (dri2_dpy
->is_render_node
&&
1258 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1259 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1263 if (dri2_dpy
->is_different_gpu
&&
1264 (dri2_dpy
->image
->base
.version
< 9 ||
1265 dri2_dpy
->image
->blitImage
== NULL
)) {
1266 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1267 "Image extension in the driver is not "
1268 "compatible. Version 9 or later and blitImage() "
1273 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1274 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1278 dri2_set_WL_bind_wayland_display(drv
, disp
);
1279 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1280 * because the buffer of the EGLImage has likely a tiling mode the server
1281 * gpu won't support. These is no way to check for now. Thus do not support the
1283 if (!dri2_dpy
->is_different_gpu
)
1284 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1286 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1288 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1290 /* Fill vtbl last to prevent accidentally calling virtual function during
1293 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1298 dri2_display_destroy(disp
);
1303 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1305 if (format
== WL_SHM_FORMAT_RGB565
)
1307 else /* ARGB8888 || XRGB8888 */
1312 * Taken from weston shared/os-compatibility.c
1315 #ifndef HAVE_MKOSTEMP
1318 set_cloexec_or_close(int fd
)
1325 flags
= fcntl(fd
, F_GETFD
);
1329 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1342 * Taken from weston shared/os-compatibility.c
1346 create_tmpfile_cloexec(char *tmpname
)
1350 #ifdef HAVE_MKOSTEMP
1351 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1355 fd
= mkstemp(tmpname
);
1357 fd
= set_cloexec_or_close(fd
);
1366 * Taken from weston shared/os-compatibility.c
1368 * Create a new, unique, anonymous file of the given size, and
1369 * return the file descriptor for it. The file descriptor is set
1370 * CLOEXEC. The file is immediately suitable for mmap()'ing
1371 * the given size at offset zero.
1373 * The file should not have a permanent backing store like a disk,
1374 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1376 * The file name is deleted from the file system.
1378 * The file is suitable for buffer sharing between processes by
1379 * transmitting the file descriptor over Unix sockets using the
1380 * SCM_RIGHTS methods.
1382 * If the C library implements posix_fallocate(), it is used to
1383 * guarantee that disk space is available for the file at the
1384 * given size. If disk space is insufficent, errno is set to ENOSPC.
1385 * If posix_fallocate() is not supported, program may receive
1386 * SIGBUS on accessing mmap()'ed file contents instead.
1389 os_create_anonymous_file(off_t size
)
1391 static const char template[] = "/mesa-shared-XXXXXX";
1397 path
= getenv("XDG_RUNTIME_DIR");
1403 name
= malloc(strlen(path
) + sizeof(template));
1408 strcat(name
, template);
1410 fd
= create_tmpfile_cloexec(name
);
1417 ret
= ftruncate(fd
, size
);
1428 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1429 int format
, int w
, int h
,
1430 void **data
, int *size
,
1431 struct wl_buffer
**buffer
)
1433 struct dri2_egl_display
*dri2_dpy
=
1434 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1435 struct wl_shm_pool
*pool
;
1436 int fd
, stride
, size_map
;
1439 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1440 size_map
= h
* stride
;
1442 /* Create a sharable buffer */
1443 fd
= os_create_anonymous_file(size_map
);
1447 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1448 if (data_map
== MAP_FAILED
) {
1453 /* Share it in a wl_buffer */
1454 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1455 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1456 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1457 wl_shm_pool_destroy(pool
);
1466 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1468 struct dri2_egl_display
*dri2_dpy
=
1469 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1472 /* we need to do the following operations only once per frame */
1473 if (dri2_surf
->back
)
1476 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1477 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1479 dri2_wl_release_buffers(dri2_surf
);
1481 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1482 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1483 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1484 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1485 dri2_surf
->current
= NULL
;
1488 /* find back buffer */
1490 /* There might be a buffer release already queued that wasn't processed */
1491 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1493 /* try get free buffer already created */
1494 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1495 if (!dri2_surf
->color_buffers
[i
].locked
&&
1496 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1497 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1502 /* else choose any another free location */
1503 if (!dri2_surf
->back
) {
1504 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1505 if (!dri2_surf
->color_buffers
[i
].locked
) {
1506 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1507 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1509 dri2_surf
->base
.Width
,
1510 dri2_surf
->base
.Height
,
1511 &dri2_surf
->back
->data
,
1512 &dri2_surf
->back
->data_size
,
1513 &dri2_surf
->back
->wl_buffer
)) {
1514 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1517 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1518 &wl_buffer_listener
, dri2_surf
);
1524 if (!dri2_surf
->back
) {
1525 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1529 dri2_surf
->back
->locked
= true;
1531 /* If we have an extra unlocked buffer at this point, we had to do triple
1532 * buffering for a while, but now can go back to just double buffering.
1533 * That means we can free any unlocked buffer now. */
1534 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1535 if (!dri2_surf
->color_buffers
[i
].locked
&&
1536 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1537 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1538 munmap(dri2_surf
->color_buffers
[i
].data
,
1539 dri2_surf
->color_buffers
[i
].data_size
);
1540 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1541 dri2_surf
->color_buffers
[i
].data
= NULL
;
1549 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1551 /* if there has been a resize: */
1552 if (!dri2_surf
->current
)
1555 return dri2_surf
->current
->data
;
1559 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1561 assert(dri2_surf
->back
);
1562 return dri2_surf
->back
->data
;
1566 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1568 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1570 while (dri2_surf
->throttle_callback
!= NULL
)
1571 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1572 dri2_surf
->wl_queue
) == -1)
1575 if (dri2_surf
->base
.SwapInterval
> 0) {
1576 dri2_surf
->throttle_callback
=
1577 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1578 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1579 &throttle_listener
, dri2_surf
);
1582 dri2_surf
->current
= dri2_surf
->back
;
1583 dri2_surf
->back
= NULL
;
1585 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1586 dri2_surf
->current
->wl_buffer
,
1587 dri2_surf
->dx
, dri2_surf
->dy
);
1589 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1590 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1591 /* reset resize growing parameters */
1595 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1596 0, 0, INT32_MAX
, INT32_MAX
);
1597 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1599 /* If we're not waiting for a frame callback then we'll at least throttle
1600 * to a sync callback so that we always give a chance for the compositor to
1601 * handle the commit and send a release event before checking for a free
1603 if (dri2_surf
->throttle_callback
== NULL
) {
1604 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1605 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1606 &throttle_listener
, dri2_surf
);
1609 wl_display_flush(dri2_dpy
->wl_dpy
);
1613 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1614 int *x
, int *y
, int *w
, int *h
,
1615 void *loaderPrivate
)
1617 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1619 (void) swrast_update_buffers(dri2_surf
);
1622 *w
= dri2_surf
->base
.Width
;
1623 *h
= dri2_surf
->base
.Height
;
1627 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1628 int x
, int y
, int w
, int h
,
1629 char *data
, void *loaderPrivate
)
1631 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1632 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1633 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1634 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1635 int dst_stride
= copy_width
;
1638 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1640 memset(data
, 0, copy_width
* h
);
1644 assert(data
!= src
);
1645 assert(copy_width
<= src_stride
);
1648 src
+= y
* src_stride
;
1651 if (copy_width
> src_stride
-x_offset
)
1652 copy_width
= src_stride
-x_offset
;
1653 if (h
> dri2_surf
->base
.Height
-y
)
1654 h
= dri2_surf
->base
.Height
-y
;
1657 memcpy(dst
, src
, copy_width
);
1664 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1665 int x
, int y
, int w
, int h
, int stride
,
1666 char *data
, void *loaderPrivate
)
1668 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1669 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1670 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1671 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1674 assert(copy_width
<= stride
);
1676 (void) swrast_update_buffers(dri2_surf
);
1677 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1679 /* partial copy, copy old content */
1680 if (copy_width
< dst_stride
)
1681 dri2_wl_swrast_get_image(draw
, 0, 0,
1682 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1683 dst
, loaderPrivate
);
1686 dst
+= y
* dst_stride
;
1690 /* drivers expect we do these checks (and some rely on it) */
1691 if (copy_width
> dst_stride
-x_offset
)
1692 copy_width
= dst_stride
-x_offset
;
1693 if (h
> dri2_surf
->base
.Height
-y
)
1694 h
= dri2_surf
->base
.Height
-y
;
1697 memcpy(dst
, src
, copy_width
);
1701 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1705 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1706 int x
, int y
, int w
, int h
,
1707 char *data
, void *loaderPrivate
)
1709 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1712 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1713 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1714 stride
, data
, loaderPrivate
);
1718 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1720 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1721 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1723 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1728 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1730 struct dri2_egl_display
*dri2_dpy
= data
;
1733 case WL_SHM_FORMAT_ARGB8888
:
1734 dri2_dpy
->formats
|= HAS_ARGB8888
;
1736 case WL_SHM_FORMAT_XRGB8888
:
1737 dri2_dpy
->formats
|= HAS_XRGB8888
;
1739 case WL_SHM_FORMAT_RGB565
:
1740 dri2_dpy
->formats
|= HAS_RGB565
;
1745 static const struct wl_shm_listener shm_listener
= {
1746 .format
= shm_handle_format
1750 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1751 const char *interface
, uint32_t version
)
1753 struct dri2_egl_display
*dri2_dpy
= data
;
1755 if (strcmp(interface
, "wl_shm") == 0) {
1757 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1758 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1762 static const struct wl_registry_listener registry_listener_swrast
= {
1763 .global
= registry_handle_global_swrast
,
1764 .global_remove
= registry_handle_global_remove
1767 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1768 .authenticate
= NULL
,
1769 .create_window_surface
= dri2_wl_create_window_surface
,
1770 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1771 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1772 .destroy_surface
= dri2_wl_destroy_surface
,
1773 .create_image
= dri2_fallback_create_image_khr
,
1774 .swap_interval
= dri2_wl_swap_interval
,
1775 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1776 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1777 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1778 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1779 .copy_buffers
= dri2_fallback_copy_buffers
,
1780 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1781 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1782 .get_sync_values
= dri2_fallback_get_sync_values
,
1783 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1786 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1787 .base
= { __DRI_SWRAST_LOADER
, 2 },
1789 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1790 .putImage
= dri2_wl_swrast_put_image
,
1791 .getImage
= dri2_wl_swrast_get_image
,
1792 .putImage2
= dri2_wl_swrast_put_image2
,
1795 static const __DRIextension
*swrast_loader_extensions
[] = {
1796 &swrast_loader_extension
.base
,
1801 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1803 struct dri2_egl_display
*dri2_dpy
;
1805 loader_set_logger(_eglLog
);
1807 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1809 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1812 disp
->DriverData
= (void *) dri2_dpy
;
1813 if (disp
->PlatformDisplay
== NULL
) {
1814 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1815 if (dri2_dpy
->wl_dpy
== NULL
)
1817 dri2_dpy
->own_device
= true;
1819 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1822 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1824 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1825 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1828 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1829 dri2_dpy
->wl_queue
);
1831 if (dri2_dpy
->own_device
)
1832 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1834 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1835 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1836 ®istry_listener_swrast
, dri2_dpy
);
1838 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1841 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1844 dri2_dpy
->driver_name
= strdup("swrast");
1845 if (!dri2_load_driver_swrast(disp
))
1848 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1850 if (!dri2_create_screen(disp
))
1853 if (!dri2_setup_extensions(disp
))
1856 dri2_setup_screen(disp
);
1858 dri2_wl_setup_swap_interval(dri2_dpy
);
1860 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1861 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1865 /* Fill vtbl last to prevent accidentally calling virtual function during
1868 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1873 dri2_display_destroy(disp
);
1878 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1880 EGLBoolean initialized
= EGL_TRUE
;
1882 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1885 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1886 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1889 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);