2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
42 #include "egl_dri2_fallbacks.h"
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
48 enum wl_drm_format_flags
{
55 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
59 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
64 wl_callback_destroy(callback
);
67 static const struct wl_callback_listener sync_listener
= {
72 roundtrip(struct dri2_egl_display
*dri2_dpy
)
74 struct wl_callback
*callback
;
75 int done
= 0, ret
= 0;
77 callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
78 wl_callback_add_listener(callback
, &sync_listener
, &done
);
79 while (ret
!= -1 && !done
)
80 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
83 wl_callback_destroy(callback
);
89 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
91 struct dri2_egl_surface
*dri2_surf
= data
;
94 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
95 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
98 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
99 wl_buffer_destroy(buffer
);
103 dri2_surf
->color_buffers
[i
].locked
= 0;
106 static const struct wl_buffer_listener wl_buffer_listener
= {
107 .release
= wl_buffer_release
111 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
113 struct dri2_egl_surface
*dri2_surf
= data
;
114 struct dri2_egl_display
*dri2_dpy
=
115 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
117 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
121 destroy_window_callback(void *data
)
123 struct dri2_egl_surface
*dri2_surf
= data
;
124 dri2_surf
->wl_win
= NULL
;
128 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
131 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
132 _EGLConfig
*conf
, void *native_window
,
133 const EGLint
*attrib_list
)
135 __DRIcreateNewDrawableFunc createNewDrawable
;
136 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
137 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
138 struct wl_egl_window
*window
= native_window
;
139 struct dri2_egl_surface
*dri2_surf
;
140 const __DRIconfig
*config
;
142 dri2_surf
= calloc(1, sizeof *dri2_surf
);
144 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
148 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
151 if (conf
->RedSize
== 5)
152 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
153 else if (conf
->AlphaSize
== 0)
154 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
156 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
159 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
163 dri2_surf
->wl_win
= window
;
165 dri2_surf
->wl_win
->private = dri2_surf
;
166 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
168 dri2_surf
->base
.Width
= -1;
169 dri2_surf
->base
.Height
= -1;
171 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
172 dri2_surf
->base
.GLColorspace
);
174 if (dri2_dpy
->dri2
) {
175 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
177 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
179 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
182 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
184 if (dri2_surf
->dri_drawable
== NULL
) {
185 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
189 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
190 dri2_dpy
->default_swap_interval
);
192 return &dri2_surf
->base
;
201 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
202 _EGLConfig
*conf
, void *native_window
,
203 const EGLint
*attrib_list
)
205 /* From the EGL_EXT_platform_wayland spec, version 3:
207 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
208 * that belongs to Wayland. Any such call fails and generates
211 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
217 * Called via eglDestroySurface(), drv->API.DestroySurface().
220 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
222 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
223 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
228 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
230 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
231 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
232 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
233 if (dri2_surf
->color_buffers
[i
].dri_image
)
234 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
235 if (dri2_surf
->color_buffers
[i
].linear_copy
)
236 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
237 if (dri2_surf
->color_buffers
[i
].data
)
238 munmap(dri2_surf
->color_buffers
[i
].data
,
239 dri2_surf
->color_buffers
[i
].data_size
);
242 if (dri2_dpy
->dri2
) {
243 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
244 if (dri2_surf
->dri_buffers
[i
] &&
245 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
246 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
247 dri2_surf
->dri_buffers
[i
]);
250 if (dri2_surf
->throttle_callback
)
251 wl_callback_destroy(dri2_surf
->throttle_callback
);
253 if (dri2_surf
->wl_win
) {
254 dri2_surf
->wl_win
->private = NULL
;
255 dri2_surf
->wl_win
->resize_callback
= NULL
;
256 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
265 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
267 struct dri2_egl_display
*dri2_dpy
=
268 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
271 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
272 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
273 !dri2_surf
->color_buffers
[i
].locked
)
274 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
275 if (dri2_surf
->color_buffers
[i
].dri_image
)
276 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
277 if (dri2_surf
->color_buffers
[i
].linear_copy
)
278 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
279 if (dri2_surf
->color_buffers
[i
].data
)
280 munmap(dri2_surf
->color_buffers
[i
].data
,
281 dri2_surf
->color_buffers
[i
].data_size
);
283 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
284 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
285 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
286 dri2_surf
->color_buffers
[i
].data
= NULL
;
287 dri2_surf
->color_buffers
[i
].locked
= 0;
290 if (dri2_dpy
->dri2
) {
291 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
292 if (dri2_surf
->dri_buffers
[i
] &&
293 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
294 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
295 dri2_surf
->dri_buffers
[i
]);
300 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
302 struct dri2_egl_display
*dri2_dpy
=
303 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
305 unsigned int dri_image_format
;
307 /* currently supports three WL DRM formats,
308 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
309 * and WL_DRM_FORMAT_RGB565
311 switch (dri2_surf
->format
) {
312 case WL_DRM_FORMAT_ARGB8888
:
313 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
315 case WL_DRM_FORMAT_XRGB8888
:
316 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
318 case WL_DRM_FORMAT_RGB565
:
319 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
322 /* format is not supported */
326 /* There might be a buffer release already queued that wasn't processed */
327 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
329 if (dri2_surf
->back
== NULL
) {
330 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
331 /* Get an unlocked buffer, preferrably one with a dri_buffer
332 * already allocated. */
333 if (dri2_surf
->color_buffers
[i
].locked
)
335 if (dri2_surf
->back
== NULL
)
336 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
337 else if (dri2_surf
->back
->dri_image
== NULL
)
338 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
342 if (dri2_surf
->back
== NULL
)
345 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
347 if (dri2_dpy
->is_different_gpu
&&
348 dri2_surf
->back
->linear_copy
== NULL
) {
349 dri2_surf
->back
->linear_copy
=
350 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
351 dri2_surf
->base
.Width
,
352 dri2_surf
->base
.Height
,
355 __DRI_IMAGE_USE_LINEAR
,
357 if (dri2_surf
->back
->linear_copy
== NULL
)
361 if (dri2_surf
->back
->dri_image
== NULL
) {
362 dri2_surf
->back
->dri_image
=
363 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
364 dri2_surf
->base
.Width
,
365 dri2_surf
->base
.Height
,
367 dri2_dpy
->is_different_gpu
?
370 dri2_surf
->back
->age
= 0;
372 if (dri2_surf
->back
->dri_image
== NULL
)
375 dri2_surf
->back
->locked
= 1;
382 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
384 struct dri2_egl_display
*dri2_dpy
=
385 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
389 image
= dri2_surf
->back
->dri_image
;
391 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
392 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
394 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
396 buffer
->pitch
= pitch
;
402 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
403 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
405 struct dri2_egl_display
*dri2_dpy
=
406 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
407 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
410 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
412 dri2_surf
->base
.Width
,
413 dri2_surf
->base
.Height
);
414 dri2_surf
->dri_buffers
[attachment
] = b
;
419 memcpy(buffer
, b
, sizeof *buffer
);
425 update_buffers(struct dri2_egl_surface
*dri2_surf
)
427 struct dri2_egl_display
*dri2_dpy
=
428 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
431 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
432 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
434 dri2_wl_release_buffers(dri2_surf
);
436 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
437 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
438 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
439 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
442 if (get_back_bo(dri2_surf
) < 0) {
443 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
447 /* If we have an extra unlocked buffer at this point, we had to do triple
448 * buffering for a while, but now can go back to just double buffering.
449 * That means we can free any unlocked buffer now. */
450 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
451 if (!dri2_surf
->color_buffers
[i
].locked
&&
452 dri2_surf
->color_buffers
[i
].wl_buffer
) {
453 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
454 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
455 if (dri2_dpy
->is_different_gpu
)
456 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
457 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
458 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
459 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
467 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
468 int *width
, int *height
,
469 unsigned int *attachments
, int count
,
470 int *out_count
, void *loaderPrivate
)
472 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
475 if (update_buffers(dri2_surf
) < 0)
478 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
479 switch (attachments
[i
]) {
480 case __DRI_BUFFER_BACK_LEFT
:
481 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
484 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
485 &dri2_surf
->buffers
[j
]) < 0) {
486 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
497 *width
= dri2_surf
->base
.Width
;
498 *height
= dri2_surf
->base
.Height
;
500 return dri2_surf
->buffers
;
504 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
505 int *width
, int *height
,
506 unsigned int *attachments
, int count
,
507 int *out_count
, void *loaderPrivate
)
509 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
510 unsigned int *attachments_with_format
;
516 switch (dri2_surf
->format
) {
517 case WL_DRM_FORMAT_ARGB8888
:
518 case WL_DRM_FORMAT_XRGB8888
:
521 case WL_DRM_FORMAT_RGB565
:
525 /* format is not supported */
529 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
530 if (!attachments_with_format
) {
535 for (i
= 0; i
< count
; ++i
) {
536 attachments_with_format
[2*i
] = attachments
[i
];
537 attachments_with_format
[2*i
+ 1] = bpp
;
541 dri2_wl_get_buffers_with_format(driDrawable
,
543 attachments_with_format
, count
,
544 out_count
, loaderPrivate
);
546 free(attachments_with_format
);
552 image_get_buffers(__DRIdrawable
*driDrawable
,
556 uint32_t buffer_mask
,
557 struct __DRIimageList
*buffers
)
559 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
561 if (update_buffers(dri2_surf
) < 0)
564 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
565 buffers
->back
= dri2_surf
->back
->dri_image
;
571 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
574 (void) loaderPrivate
;
577 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
578 .base
= { __DRI_DRI2_LOADER
, 3 },
580 .getBuffers
= dri2_wl_get_buffers
,
581 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
582 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
585 static const __DRIimageLoaderExtension image_loader_extension
= {
586 .base
= { __DRI_IMAGE_LOADER
, 1 },
588 .getBuffers
= image_get_buffers
,
589 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
593 wayland_throttle_callback(void *data
,
594 struct wl_callback
*callback
,
597 struct dri2_egl_surface
*dri2_surf
= data
;
599 dri2_surf
->throttle_callback
= NULL
;
600 wl_callback_destroy(callback
);
603 static const struct wl_callback_listener throttle_listener
= {
604 .done
= wayland_throttle_callback
608 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
610 struct dri2_egl_display
*dri2_dpy
=
611 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
613 int fd
, stride
, name
;
615 if (dri2_surf
->current
->wl_buffer
!= NULL
)
618 if (dri2_dpy
->is_different_gpu
) {
619 image
= dri2_surf
->current
->linear_copy
;
621 image
= dri2_surf
->current
->dri_image
;
623 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
624 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
625 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
627 dri2_surf
->current
->wl_buffer
=
628 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
630 dri2_surf
->base
.Width
,
631 dri2_surf
->base
.Height
,
638 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
639 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
641 dri2_surf
->current
->wl_buffer
=
642 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
644 dri2_surf
->base
.Width
,
645 dri2_surf
->base
.Height
,
650 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
652 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
653 &wl_buffer_listener
, dri2_surf
);
657 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
663 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_win
->surface
)
664 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
667 for (i
= 0; i
< n_rects
; i
++) {
668 const int *rect
= &rects
[i
* 4];
670 wl_surface_damage_buffer(dri2_surf
->wl_win
->surface
,
672 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
678 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
681 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
687 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
688 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
691 while (dri2_surf
->throttle_callback
!= NULL
)
692 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
693 dri2_dpy
->wl_queue
) == -1)
696 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
697 if (dri2_surf
->color_buffers
[i
].age
> 0)
698 dri2_surf
->color_buffers
[i
].age
++;
700 /* Make sure we have a back buffer in case we're swapping without ever
702 if (get_back_bo(dri2_surf
) < 0) {
703 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
707 if (draw
->SwapInterval
> 0) {
708 dri2_surf
->throttle_callback
=
709 wl_surface_frame(dri2_surf
->wl_win
->surface
);
710 wl_callback_add_listener(dri2_surf
->throttle_callback
,
711 &throttle_listener
, dri2_surf
);
712 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
716 dri2_surf
->back
->age
= 1;
717 dri2_surf
->current
= dri2_surf
->back
;
718 dri2_surf
->back
= NULL
;
720 create_wl_buffer(dri2_surf
);
722 wl_surface_attach(dri2_surf
->wl_win
->surface
,
723 dri2_surf
->current
->wl_buffer
,
724 dri2_surf
->dx
, dri2_surf
->dy
);
726 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
727 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
728 /* reset resize growing parameters */
732 /* If the compositor doesn't support damage_buffer, we deliberately
733 * ignore the damage region and post maximum damage, due to
734 * https://bugs.freedesktop.org/78190 */
735 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
736 wl_surface_damage(dri2_surf
->wl_win
->surface
,
737 0, 0, INT32_MAX
, INT32_MAX
);
739 if (dri2_dpy
->is_different_gpu
) {
740 _EGLContext
*ctx
= _eglGetCurrentContext();
741 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
742 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
743 dri2_surf
->current
->linear_copy
,
744 dri2_surf
->current
->dri_image
,
745 0, 0, dri2_surf
->base
.Width
,
746 dri2_surf
->base
.Height
,
747 0, 0, dri2_surf
->base
.Width
,
748 dri2_surf
->base
.Height
, 0);
751 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
752 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
754 wl_surface_commit(dri2_surf
->wl_win
->surface
);
756 /* If we're not waiting for a frame callback then we'll at least throttle
757 * to a sync callback so that we always give a chance for the compositor to
758 * handle the commit and send a release event before checking for a free
760 if (dri2_surf
->throttle_callback
== NULL
) {
761 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
762 wl_callback_add_listener(dri2_surf
->throttle_callback
,
763 &throttle_listener
, dri2_surf
);
766 wl_display_flush(dri2_dpy
->wl_dpy
);
772 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
773 _EGLDisplay
*disp
, _EGLSurface
*surface
)
775 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
777 if (get_back_bo(dri2_surf
) < 0) {
778 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
782 return dri2_surf
->back
->age
;
786 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
788 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
791 static struct wl_buffer
*
792 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
796 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
797 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
798 __DRIimage
*image
= dri2_img
->dri_image
;
799 struct wl_buffer
*buffer
;
800 int width
, height
, format
, pitch
;
801 enum wl_drm_format wl_format
;
803 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
806 case __DRI_IMAGE_FORMAT_ARGB8888
:
807 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
809 wl_format
= WL_DRM_FORMAT_ARGB8888
;
811 case __DRI_IMAGE_FORMAT_XRGB8888
:
812 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
814 wl_format
= WL_DRM_FORMAT_XRGB8888
;
820 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
821 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
822 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
824 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
827 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
830 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
842 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
845 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
852 /* The buffer object will have been created with our internal event queue
853 * because it is using the wl_drm object as a proxy factory. We want the
854 * buffer to be used by the application so we'll reset it to the display's
855 * default event queue */
857 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
862 _eglError(EGL_BAD_MATCH
, "unsupported image format");
867 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
869 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
872 if (dri2_dpy
->is_render_node
) {
873 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
874 "authenticate for render-nodes");
877 dri2_dpy
->authenticated
= 0;
879 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
880 if (roundtrip(dri2_dpy
) < 0)
883 if (!dri2_dpy
->authenticated
)
886 /* reset authenticated */
887 dri2_dpy
->authenticated
= 1;
893 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
895 struct dri2_egl_display
*dri2_dpy
= data
;
898 dri2_dpy
->device_name
= strdup(device
);
899 if (!dri2_dpy
->device_name
)
902 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
903 if (dri2_dpy
->fd
== -1) {
904 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
905 dri2_dpy
->device_name
, strerror(errno
));
909 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
910 dri2_dpy
->authenticated
= 1;
912 drmGetMagic(dri2_dpy
->fd
, &magic
);
913 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
918 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
920 struct dri2_egl_display
*dri2_dpy
= data
;
923 case WL_DRM_FORMAT_ARGB8888
:
924 dri2_dpy
->formats
|= HAS_ARGB8888
;
926 case WL_DRM_FORMAT_XRGB8888
:
927 dri2_dpy
->formats
|= HAS_XRGB8888
;
929 case WL_DRM_FORMAT_RGB565
:
930 dri2_dpy
->formats
|= HAS_RGB565
;
936 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
938 struct dri2_egl_display
*dri2_dpy
= data
;
940 dri2_dpy
->capabilities
= value
;
944 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
946 struct dri2_egl_display
*dri2_dpy
= data
;
948 dri2_dpy
->authenticated
= 1;
951 static const struct wl_drm_listener drm_listener
= {
952 .device
= drm_handle_device
,
953 .format
= drm_handle_format
,
954 .authenticated
= drm_handle_authenticated
,
955 .capabilities
= drm_handle_capabilities
959 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
960 const char *interface
, uint32_t version
)
962 struct dri2_egl_display
*dri2_dpy
= data
;
966 if (strcmp(interface
, "wl_drm") == 0) {
968 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
969 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
974 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
979 static const struct wl_registry_listener registry_listener_drm
= {
980 .global
= registry_handle_global_drm
,
981 .global_remove
= registry_handle_global_remove
985 dri2_wl_swap_interval(_EGLDriver
*drv
,
990 if (interval
> surf
->Config
->MaxSwapInterval
)
991 interval
= surf
->Config
->MaxSwapInterval
;
992 else if (interval
< surf
->Config
->MinSwapInterval
)
993 interval
= surf
->Config
->MinSwapInterval
;
995 surf
->SwapInterval
= interval
;
1001 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1003 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1005 /* We can't use values greater than 1 on Wayland because we are using the
1006 * frame callback to synchronise the frame and the only way we be sure to
1007 * get a frame callback is to attach a new buffer. Therefore we can't just
1008 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1010 if (dri2_dpy
->config
)
1011 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1012 "vblank_mode", &vblank_mode
);
1013 switch (vblank_mode
) {
1014 case DRI_CONF_VBLANK_NEVER
:
1015 dri2_dpy
->min_swap_interval
= 0;
1016 dri2_dpy
->max_swap_interval
= 0;
1017 dri2_dpy
->default_swap_interval
= 0;
1019 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1020 dri2_dpy
->min_swap_interval
= 1;
1021 dri2_dpy
->max_swap_interval
= 1;
1022 dri2_dpy
->default_swap_interval
= 1;
1024 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1025 dri2_dpy
->min_swap_interval
= 0;
1026 dri2_dpy
->max_swap_interval
= 1;
1027 dri2_dpy
->default_swap_interval
= 0;
1030 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1031 dri2_dpy
->min_swap_interval
= 0;
1032 dri2_dpy
->max_swap_interval
= 1;
1033 dri2_dpy
->default_swap_interval
= 1;
1038 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1039 .authenticate
= dri2_wl_authenticate
,
1040 .create_window_surface
= dri2_wl_create_window_surface
,
1041 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1042 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1043 .destroy_surface
= dri2_wl_destroy_surface
,
1044 .create_image
= dri2_create_image_khr
,
1045 .swap_interval
= dri2_wl_swap_interval
,
1046 .swap_buffers
= dri2_wl_swap_buffers
,
1047 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1048 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1049 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1050 .copy_buffers
= dri2_fallback_copy_buffers
,
1051 .query_buffer_age
= dri2_wl_query_buffer_age
,
1052 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1053 .get_sync_values
= dri2_fallback_get_sync_values
,
1054 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1057 static const __DRIextension
*dri2_loader_extensions
[] = {
1058 &dri2_loader_extension
.base
,
1059 &image_loader_extension
.base
,
1060 &image_lookup_extension
.base
,
1061 &use_invalidate
.base
,
1065 static const __DRIextension
*image_loader_extensions
[] = {
1066 &image_loader_extension
.base
,
1067 &image_lookup_extension
.base
,
1068 &use_invalidate
.base
,
1073 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1075 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1076 static const struct {
1077 const char *format_name
;
1079 unsigned int rgba_masks
[4];
1081 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1082 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1083 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1085 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1086 unsigned int count
, i
, j
;
1089 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1090 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1091 struct dri2_egl_config
*dri2_conf
;
1093 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1096 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1097 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1105 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1106 if (!format_count
[i
]) {
1107 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1108 visuals
[i
].format_name
);
1112 return (count
!= 0);
1116 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1118 struct dri2_egl_display
*dri2_dpy
;
1120 loader_set_logger(_eglLog
);
1122 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1124 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1126 disp
->DriverData
= (void *) dri2_dpy
;
1127 if (disp
->PlatformDisplay
== NULL
) {
1128 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1129 if (dri2_dpy
->wl_dpy
== NULL
)
1131 dri2_dpy
->own_device
= 1;
1133 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1136 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1138 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1139 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1140 goto cleanup_dpy_wrapper
;
1142 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1143 dri2_dpy
->wl_queue
);
1145 if (dri2_dpy
->own_device
)
1146 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1148 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1149 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1150 ®istry_listener_drm
, dri2_dpy
);
1151 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1152 goto cleanup_registry
;
1154 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1157 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1160 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1161 &dri2_dpy
->is_different_gpu
);
1162 if (dri2_dpy
->is_different_gpu
) {
1163 free(dri2_dpy
->device_name
);
1164 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1165 if (!dri2_dpy
->device_name
) {
1166 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1167 "for requested GPU");
1172 /* we have to do the check now, because loader_get_user_preferred_fd
1173 * will return a render-node when the requested gpu is different
1174 * to the server, but also if the client asks for the same gpu than
1175 * the server by requesting its pci-id */
1176 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1178 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1179 if (dri2_dpy
->driver_name
== NULL
) {
1180 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1184 if (!dri2_load_driver(disp
))
1185 goto cleanup_driver_name
;
1187 /* render nodes cannot use Gem names, and thus do not support
1188 * the __DRI_DRI2_LOADER extension */
1189 if (!dri2_dpy
->is_render_node
)
1190 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1192 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1194 if (!dri2_create_screen(disp
))
1195 goto cleanup_driver
;
1197 dri2_wl_setup_swap_interval(dri2_dpy
);
1199 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1200 * createImageFromFds support indicates that Prime export/import
1201 * is supported by the driver. Fall back to
1202 * gem names if we don't have Prime support. */
1204 if (dri2_dpy
->image
->base
.version
< 7 ||
1205 dri2_dpy
->image
->createImageFromFds
== NULL
)
1206 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1208 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1209 * The server needs to accept them */
1210 if (dri2_dpy
->is_render_node
&&
1211 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1212 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1213 goto cleanup_screen
;
1216 if (dri2_dpy
->is_different_gpu
&&
1217 (dri2_dpy
->image
->base
.version
< 9 ||
1218 dri2_dpy
->image
->blitImage
== NULL
)) {
1219 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1220 "Image extension in the driver is not "
1221 "compatible. Version 9 or later and blitImage() "
1223 goto cleanup_screen
;
1226 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1227 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1228 goto cleanup_screen
;
1231 dri2_set_WL_bind_wayland_display(drv
, disp
);
1232 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1233 * because the buffer of the EGLImage has likely a tiling mode the server
1234 * gpu won't support. These is no way to check for now. Thus do not support the
1236 if (!dri2_dpy
->is_different_gpu
) {
1237 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1239 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1240 dri2_fallback_create_wayland_buffer_from_image
;
1242 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1244 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1246 /* Fill vtbl last to prevent accidentally calling virtual function during
1249 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1254 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1256 dlclose(dri2_dpy
->driver
);
1257 cleanup_driver_name
:
1258 free(dri2_dpy
->driver_name
);
1260 close(dri2_dpy
->fd
);
1262 free(dri2_dpy
->device_name
);
1263 wl_drm_destroy(dri2_dpy
->wl_drm
);
1265 wl_registry_destroy(dri2_dpy
->wl_registry
);
1266 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1267 cleanup_dpy_wrapper
:
1268 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1269 if (disp
->PlatformDisplay
== NULL
)
1270 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1273 disp
->DriverData
= NULL
;
1279 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1281 if (format
== WL_SHM_FORMAT_RGB565
)
1283 else /* ARGB8888 || XRGB8888 */
1288 * Taken from weston shared/os-compatibility.c
1291 #ifndef HAVE_MKOSTEMP
1294 set_cloexec_or_close(int fd
)
1301 flags
= fcntl(fd
, F_GETFD
);
1305 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1318 * Taken from weston shared/os-compatibility.c
1322 create_tmpfile_cloexec(char *tmpname
)
1326 #ifdef HAVE_MKOSTEMP
1327 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1331 fd
= mkstemp(tmpname
);
1333 fd
= set_cloexec_or_close(fd
);
1342 * Taken from weston shared/os-compatibility.c
1344 * Create a new, unique, anonymous file of the given size, and
1345 * return the file descriptor for it. The file descriptor is set
1346 * CLOEXEC. The file is immediately suitable for mmap()'ing
1347 * the given size at offset zero.
1349 * The file should not have a permanent backing store like a disk,
1350 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1352 * The file name is deleted from the file system.
1354 * The file is suitable for buffer sharing between processes by
1355 * transmitting the file descriptor over Unix sockets using the
1356 * SCM_RIGHTS methods.
1358 * If the C library implements posix_fallocate(), it is used to
1359 * guarantee that disk space is available for the file at the
1360 * given size. If disk space is insufficent, errno is set to ENOSPC.
1361 * If posix_fallocate() is not supported, program may receive
1362 * SIGBUS on accessing mmap()'ed file contents instead.
1365 os_create_anonymous_file(off_t size
)
1367 static const char template[] = "/mesa-shared-XXXXXX";
1373 path
= getenv("XDG_RUNTIME_DIR");
1379 name
= malloc(strlen(path
) + sizeof(template));
1384 strcat(name
, template);
1386 fd
= create_tmpfile_cloexec(name
);
1393 ret
= ftruncate(fd
, size
);
1404 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1405 int format
, int w
, int h
,
1406 void **data
, int *size
,
1407 struct wl_buffer
**buffer
)
1409 struct wl_shm_pool
*pool
;
1410 int fd
, stride
, size_map
;
1413 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1414 size_map
= h
* stride
;
1416 /* Create a sharable buffer */
1417 fd
= os_create_anonymous_file(size_map
);
1421 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1422 if (data_map
== MAP_FAILED
) {
1427 /* Share it in a wl_buffer */
1428 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1429 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1430 wl_shm_pool_destroy(pool
);
1439 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1441 struct dri2_egl_display
*dri2_dpy
=
1442 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1445 /* we need to do the following operations only once per frame */
1446 if (dri2_surf
->back
)
1449 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1450 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1452 dri2_wl_release_buffers(dri2_surf
);
1454 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1455 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1456 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1457 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1458 dri2_surf
->current
= NULL
;
1461 /* find back buffer */
1463 /* There might be a buffer release already queued that wasn't processed */
1464 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
1466 /* try get free buffer already created */
1467 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1468 if (!dri2_surf
->color_buffers
[i
].locked
&&
1469 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1470 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1475 /* else choose any another free location */
1476 if (!dri2_surf
->back
) {
1477 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1478 if (!dri2_surf
->color_buffers
[i
].locked
) {
1479 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1480 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1482 dri2_surf
->base
.Width
,
1483 dri2_surf
->base
.Height
,
1484 &dri2_surf
->back
->data
,
1485 &dri2_surf
->back
->data_size
,
1486 &dri2_surf
->back
->wl_buffer
)) {
1487 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1490 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1491 dri2_dpy
->wl_queue
);
1492 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1493 &wl_buffer_listener
, dri2_surf
);
1499 if (!dri2_surf
->back
) {
1500 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1504 dri2_surf
->back
->locked
= 1;
1506 /* If we have an extra unlocked buffer at this point, we had to do triple
1507 * buffering for a while, but now can go back to just double buffering.
1508 * That means we can free any unlocked buffer now. */
1509 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1510 if (!dri2_surf
->color_buffers
[i
].locked
&&
1511 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1512 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1513 munmap(dri2_surf
->color_buffers
[i
].data
,
1514 dri2_surf
->color_buffers
[i
].data_size
);
1515 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1516 dri2_surf
->color_buffers
[i
].data
= NULL
;
1524 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1526 /* if there has been a resize: */
1527 if (!dri2_surf
->current
)
1530 return dri2_surf
->current
->data
;
1534 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1536 assert(dri2_surf
->back
);
1537 return dri2_surf
->back
->data
;
1541 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1543 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1545 while (dri2_surf
->throttle_callback
!= NULL
)
1546 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1547 dri2_dpy
->wl_queue
) == -1)
1550 if (dri2_surf
->base
.SwapInterval
> 0) {
1551 dri2_surf
->throttle_callback
=
1552 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1553 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1554 &throttle_listener
, dri2_surf
);
1555 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1556 dri2_dpy
->wl_queue
);
1559 dri2_surf
->current
= dri2_surf
->back
;
1560 dri2_surf
->back
= NULL
;
1562 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1563 dri2_surf
->current
->wl_buffer
,
1564 dri2_surf
->dx
, dri2_surf
->dy
);
1566 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1567 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1568 /* reset resize growing parameters */
1572 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1573 0, 0, INT32_MAX
, INT32_MAX
);
1574 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1576 /* If we're not waiting for a frame callback then we'll at least throttle
1577 * to a sync callback so that we always give a chance for the compositor to
1578 * handle the commit and send a release event before checking for a free
1580 if (dri2_surf
->throttle_callback
== NULL
) {
1581 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1582 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1583 &throttle_listener
, dri2_surf
);
1586 wl_display_flush(dri2_dpy
->wl_dpy
);
1590 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1591 int *x
, int *y
, int *w
, int *h
,
1592 void *loaderPrivate
)
1594 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1596 (void) swrast_update_buffers(dri2_surf
);
1599 *w
= dri2_surf
->base
.Width
;
1600 *h
= dri2_surf
->base
.Height
;
1604 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1605 int x
, int y
, int w
, int h
,
1606 char *data
, void *loaderPrivate
)
1608 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1609 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1610 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1611 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1612 int dst_stride
= copy_width
;
1615 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1617 memset(data
, 0, copy_width
* h
);
1621 assert(data
!= src
);
1622 assert(copy_width
<= src_stride
);
1625 src
+= y
* src_stride
;
1628 if (copy_width
> src_stride
-x_offset
)
1629 copy_width
= src_stride
-x_offset
;
1630 if (h
> dri2_surf
->base
.Height
-y
)
1631 h
= dri2_surf
->base
.Height
-y
;
1634 memcpy(dst
, src
, copy_width
);
1641 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1642 int x
, int y
, int w
, int h
, int stride
,
1643 char *data
, void *loaderPrivate
)
1645 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1646 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1647 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1648 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1651 assert(copy_width
<= stride
);
1653 (void) swrast_update_buffers(dri2_surf
);
1654 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1656 /* partial copy, copy old content */
1657 if (copy_width
< dst_stride
)
1658 dri2_wl_swrast_get_image(draw
, 0, 0,
1659 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1660 dst
, loaderPrivate
);
1663 dst
+= y
* dst_stride
;
1667 /* drivers expect we do these checks (and some rely on it) */
1668 if (copy_width
> dst_stride
-x_offset
)
1669 copy_width
= dst_stride
-x_offset
;
1670 if (h
> dri2_surf
->base
.Height
-y
)
1671 h
= dri2_surf
->base
.Height
-y
;
1674 memcpy(dst
, src
, copy_width
);
1678 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1682 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1683 int x
, int y
, int w
, int h
,
1684 char *data
, void *loaderPrivate
)
1686 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1689 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1690 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1691 stride
, data
, loaderPrivate
);
1695 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1697 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1698 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1700 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1705 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1707 struct dri2_egl_display
*dri2_dpy
= data
;
1710 case WL_SHM_FORMAT_ARGB8888
:
1711 dri2_dpy
->formats
|= HAS_ARGB8888
;
1713 case WL_SHM_FORMAT_XRGB8888
:
1714 dri2_dpy
->formats
|= HAS_XRGB8888
;
1716 case WL_SHM_FORMAT_RGB565
:
1717 dri2_dpy
->formats
|= HAS_RGB565
;
1722 static const struct wl_shm_listener shm_listener
= {
1723 .format
= shm_handle_format
1727 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1728 const char *interface
, uint32_t version
)
1730 struct dri2_egl_display
*dri2_dpy
= data
;
1732 if (strcmp(interface
, "wl_shm") == 0) {
1734 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1735 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1739 static const struct wl_registry_listener registry_listener_swrast
= {
1740 .global
= registry_handle_global_swrast
,
1741 .global_remove
= registry_handle_global_remove
1744 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1745 .authenticate
= NULL
,
1746 .create_window_surface
= dri2_wl_create_window_surface
,
1747 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1748 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1749 .destroy_surface
= dri2_wl_destroy_surface
,
1750 .create_image
= dri2_fallback_create_image_khr
,
1751 .swap_interval
= dri2_wl_swap_interval
,
1752 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1753 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1754 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1755 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1756 .copy_buffers
= dri2_fallback_copy_buffers
,
1757 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1758 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1759 .get_sync_values
= dri2_fallback_get_sync_values
,
1760 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1763 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1764 .base
= { __DRI_SWRAST_LOADER
, 2 },
1766 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1767 .putImage
= dri2_wl_swrast_put_image
,
1768 .getImage
= dri2_wl_swrast_get_image
,
1769 .putImage2
= dri2_wl_swrast_put_image2
,
1772 static const __DRIextension
*swrast_loader_extensions
[] = {
1773 &swrast_loader_extension
.base
,
1778 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1780 struct dri2_egl_display
*dri2_dpy
;
1782 loader_set_logger(_eglLog
);
1784 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1786 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1788 disp
->DriverData
= (void *) dri2_dpy
;
1789 if (disp
->PlatformDisplay
== NULL
) {
1790 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1791 if (dri2_dpy
->wl_dpy
== NULL
)
1793 dri2_dpy
->own_device
= 1;
1795 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1798 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1800 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1801 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1802 goto cleanup_dpy_wrapper
;
1804 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1805 dri2_dpy
->wl_queue
);
1807 if (dri2_dpy
->own_device
)
1808 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1810 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1811 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1812 ®istry_listener_swrast
, dri2_dpy
);
1814 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1815 goto cleanup_registry
;
1817 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1821 dri2_dpy
->driver_name
= strdup("swrast");
1822 if (!dri2_load_driver_swrast(disp
))
1825 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1827 if (!dri2_create_screen(disp
))
1828 goto cleanup_driver
;
1830 dri2_wl_setup_swap_interval(dri2_dpy
);
1832 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1833 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1834 goto cleanup_screen
;
1837 /* Fill vtbl last to prevent accidentally calling virtual function during
1840 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1845 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1847 dlclose(dri2_dpy
->driver
);
1849 wl_shm_destroy(dri2_dpy
->wl_shm
);
1851 wl_registry_destroy(dri2_dpy
->wl_registry
);
1852 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1853 cleanup_dpy_wrapper
:
1854 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1855 if (disp
->PlatformDisplay
== NULL
)
1856 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1859 disp
->DriverData
= NULL
;
1865 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1867 EGLBoolean initialized
= EGL_TRUE
;
1869 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1872 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1873 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1876 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);