2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
42 #include "egl_dri2_fallbacks.h"
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
48 enum wl_drm_format_flags
{
55 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
59 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
64 wl_callback_destroy(callback
);
67 static const struct wl_callback_listener sync_listener
= {
72 roundtrip(struct dri2_egl_display
*dri2_dpy
)
74 struct wl_callback
*callback
;
75 int done
= 0, ret
= 0;
77 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
78 wl_callback_add_listener(callback
, &sync_listener
, &done
);
79 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
80 while (ret
!= -1 && !done
)
81 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
84 wl_callback_destroy(callback
);
90 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
92 struct dri2_egl_surface
*dri2_surf
= data
;
95 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
96 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
99 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
100 wl_buffer_destroy(buffer
);
104 dri2_surf
->color_buffers
[i
].locked
= 0;
107 static const struct wl_buffer_listener wl_buffer_listener
= {
108 .release
= wl_buffer_release
112 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
114 struct dri2_egl_surface
*dri2_surf
= data
;
115 struct dri2_egl_display
*dri2_dpy
=
116 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
118 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
122 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
125 dri2_wl_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
126 _EGLConfig
*conf
, void *native_window
,
127 const EGLint
*attrib_list
)
129 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
130 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
131 struct wl_egl_window
*window
= native_window
;
132 struct dri2_egl_surface
*dri2_surf
;
133 const __DRIconfig
*config
;
137 dri2_surf
= calloc(1, sizeof *dri2_surf
);
139 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
143 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
146 if (conf
->RedSize
== 5)
147 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
148 else if (conf
->AlphaSize
== 0)
149 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
151 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
154 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
158 dri2_surf
->wl_win
= window
;
160 dri2_surf
->wl_win
->private = dri2_surf
;
161 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
163 dri2_surf
->base
.Width
= -1;
164 dri2_surf
->base
.Height
= -1;
166 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
167 dri2_surf
->base
.GLColorspace
);
169 dri2_surf
->dri_drawable
=
170 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
172 if (dri2_surf
->dri_drawable
== NULL
) {
173 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
177 return &dri2_surf
->base
;
186 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
189 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
190 _EGLConfig
*conf
, void *native_window
,
191 const EGLint
*attrib_list
)
193 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
196 surf
= dri2_wl_create_surface(drv
, disp
, conf
, native_window
, attrib_list
);
199 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
205 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
206 _EGLConfig
*conf
, void *native_window
,
207 const EGLint
*attrib_list
)
209 /* From the EGL_EXT_platform_wayland spec, version 3:
211 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
212 * that belongs to Wayland. Any such call fails and generates
215 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
221 * Called via eglDestroySurface(), drv->API.DestroySurface().
224 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
226 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
227 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
232 if (!_eglPutSurface(surf
))
235 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
237 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
238 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
239 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
240 if (dri2_surf
->color_buffers
[i
].dri_image
)
241 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
242 if (dri2_surf
->color_buffers
[i
].linear_copy
)
243 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
244 if (dri2_surf
->color_buffers
[i
].data
)
245 munmap(dri2_surf
->color_buffers
[i
].data
,
246 dri2_surf
->color_buffers
[i
].data_size
);
249 if (dri2_dpy
->dri2
) {
250 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
251 if (dri2_surf
->dri_buffers
[i
] &&
252 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
253 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
254 dri2_surf
->dri_buffers
[i
]);
257 if (dri2_surf
->throttle_callback
)
258 wl_callback_destroy(dri2_surf
->throttle_callback
);
260 dri2_surf
->wl_win
->private = NULL
;
261 dri2_surf
->wl_win
->resize_callback
= NULL
;
269 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
271 struct dri2_egl_display
*dri2_dpy
=
272 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
275 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
276 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
277 !dri2_surf
->color_buffers
[i
].locked
)
278 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
279 if (dri2_surf
->color_buffers
[i
].dri_image
)
280 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
281 if (dri2_surf
->color_buffers
[i
].linear_copy
)
282 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
283 if (dri2_surf
->color_buffers
[i
].data
)
284 munmap(dri2_surf
->color_buffers
[i
].data
,
285 dri2_surf
->color_buffers
[i
].data_size
);
287 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
288 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
289 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
290 dri2_surf
->color_buffers
[i
].data
= NULL
;
291 dri2_surf
->color_buffers
[i
].locked
= 0;
294 if (dri2_dpy
->dri2
) {
295 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
296 if (dri2_surf
->dri_buffers
[i
] &&
297 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
298 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
299 dri2_surf
->dri_buffers
[i
]);
304 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
306 struct dri2_egl_display
*dri2_dpy
=
307 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
309 unsigned int dri_image_format
;
311 /* currently supports three WL DRM formats,
312 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
313 * and WL_DRM_FORMAT_RGB565
315 switch (dri2_surf
->format
) {
316 case WL_DRM_FORMAT_ARGB8888
:
317 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
319 case WL_DRM_FORMAT_XRGB8888
:
320 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
322 case WL_DRM_FORMAT_RGB565
:
323 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
326 /* format is not supported */
330 /* We always want to throttle to some event (either a frame callback or
331 * a sync request) after the commit so that we can be sure the
332 * compositor has had a chance to handle it and send us a release event
333 * before we look for a free buffer */
334 while (dri2_surf
->throttle_callback
!= NULL
)
335 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
336 dri2_dpy
->wl_queue
) == -1)
339 if (dri2_surf
->back
== NULL
) {
340 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
341 /* Get an unlocked buffer, preferrably one with a dri_buffer
342 * already allocated. */
343 if (dri2_surf
->color_buffers
[i
].locked
)
345 if (dri2_surf
->back
== NULL
)
346 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
347 else if (dri2_surf
->back
->dri_image
== NULL
)
348 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
352 if (dri2_surf
->back
== NULL
)
355 if (dri2_dpy
->is_different_gpu
&&
356 dri2_surf
->back
->linear_copy
== NULL
) {
357 dri2_surf
->back
->linear_copy
=
358 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
359 dri2_surf
->base
.Width
,
360 dri2_surf
->base
.Height
,
362 __DRI_IMAGE_USE_SHARE
|
363 __DRI_IMAGE_USE_LINEAR
,
365 if (dri2_surf
->back
->linear_copy
== NULL
)
369 if (dri2_surf
->back
->dri_image
== NULL
) {
370 dri2_surf
->back
->dri_image
=
371 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
372 dri2_surf
->base
.Width
,
373 dri2_surf
->base
.Height
,
375 dri2_dpy
->is_different_gpu
?
376 0 : __DRI_IMAGE_USE_SHARE
,
378 dri2_surf
->back
->age
= 0;
380 if (dri2_surf
->back
->dri_image
== NULL
)
383 dri2_surf
->back
->locked
= 1;
390 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
392 struct dri2_egl_display
*dri2_dpy
=
393 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
397 image
= dri2_surf
->back
->dri_image
;
399 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
400 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
402 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
404 buffer
->pitch
= pitch
;
410 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
411 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
413 struct dri2_egl_display
*dri2_dpy
=
414 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
415 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
418 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
420 dri2_surf
->base
.Width
,
421 dri2_surf
->base
.Height
);
422 dri2_surf
->dri_buffers
[attachment
] = b
;
427 memcpy(buffer
, b
, sizeof *buffer
);
433 update_buffers(struct dri2_egl_surface
*dri2_surf
)
435 struct dri2_egl_display
*dri2_dpy
=
436 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
439 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
440 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
442 dri2_wl_release_buffers(dri2_surf
);
444 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
445 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
446 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
447 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
450 if (get_back_bo(dri2_surf
) < 0) {
451 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
455 /* If we have an extra unlocked buffer at this point, we had to do triple
456 * buffering for a while, but now can go back to just double buffering.
457 * That means we can free any unlocked buffer now. */
458 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
459 if (!dri2_surf
->color_buffers
[i
].locked
&&
460 dri2_surf
->color_buffers
[i
].wl_buffer
) {
461 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
462 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
463 if (dri2_dpy
->is_different_gpu
)
464 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
465 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
466 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
467 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
475 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
476 int *width
, int *height
,
477 unsigned int *attachments
, int count
,
478 int *out_count
, void *loaderPrivate
)
480 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
483 if (update_buffers(dri2_surf
) < 0)
486 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
487 switch (attachments
[i
]) {
488 case __DRI_BUFFER_BACK_LEFT
:
489 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
492 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
493 &dri2_surf
->buffers
[j
]) < 0) {
494 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
505 *width
= dri2_surf
->base
.Width
;
506 *height
= dri2_surf
->base
.Height
;
508 return dri2_surf
->buffers
;
512 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
513 int *width
, int *height
,
514 unsigned int *attachments
, int count
,
515 int *out_count
, void *loaderPrivate
)
517 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
518 unsigned int *attachments_with_format
;
524 switch (dri2_surf
->format
) {
525 case WL_DRM_FORMAT_ARGB8888
:
526 case WL_DRM_FORMAT_XRGB8888
:
529 case WL_DRM_FORMAT_RGB565
:
533 /* format is not supported */
537 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
538 if (!attachments_with_format
) {
543 for (i
= 0; i
< count
; ++i
) {
544 attachments_with_format
[2*i
] = attachments
[i
];
545 attachments_with_format
[2*i
+ 1] = bpp
;
549 dri2_wl_get_buffers_with_format(driDrawable
,
551 attachments_with_format
, count
,
552 out_count
, loaderPrivate
);
554 free(attachments_with_format
);
560 image_get_buffers(__DRIdrawable
*driDrawable
,
564 uint32_t buffer_mask
,
565 struct __DRIimageList
*buffers
)
567 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
569 if (update_buffers(dri2_surf
) < 0)
572 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
573 buffers
->back
= dri2_surf
->back
->dri_image
;
579 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
582 (void) loaderPrivate
;
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 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
660 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
666 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
667 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
670 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
671 if (dri2_surf
->color_buffers
[i
].age
> 0)
672 dri2_surf
->color_buffers
[i
].age
++;
674 /* Make sure we have a back buffer in case we're swapping without ever
676 if (get_back_bo(dri2_surf
) < 0) {
677 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
681 if (draw
->SwapInterval
> 0) {
682 dri2_surf
->throttle_callback
=
683 wl_surface_frame(dri2_surf
->wl_win
->surface
);
684 wl_callback_add_listener(dri2_surf
->throttle_callback
,
685 &throttle_listener
, dri2_surf
);
686 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
690 dri2_surf
->back
->age
= 1;
691 dri2_surf
->current
= dri2_surf
->back
;
692 dri2_surf
->back
= NULL
;
694 create_wl_buffer(dri2_surf
);
696 wl_surface_attach(dri2_surf
->wl_win
->surface
,
697 dri2_surf
->current
->wl_buffer
,
698 dri2_surf
->dx
, dri2_surf
->dy
);
700 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
701 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
702 /* reset resize growing parameters */
707 wl_surface_damage(dri2_surf
->wl_win
->surface
,
708 0, 0, INT32_MAX
, INT32_MAX
);
710 for (i
= 0; i
< n_rects
; i
++) {
711 const int *rect
= &rects
[i
* 4];
712 wl_surface_damage(dri2_surf
->wl_win
->surface
,
714 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
719 if (dri2_dpy
->is_different_gpu
) {
720 _EGLContext
*ctx
= _eglGetCurrentContext();
721 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
722 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
723 dri2_surf
->current
->linear_copy
,
724 dri2_surf
->current
->dri_image
,
725 0, 0, dri2_surf
->base
.Width
,
726 dri2_surf
->base
.Height
,
727 0, 0, dri2_surf
->base
.Width
,
728 dri2_surf
->base
.Height
, 0);
731 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
732 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
734 wl_surface_commit(dri2_surf
->wl_win
->surface
);
736 /* If we're not waiting for a frame callback then we'll at least throttle
737 * to a sync callback so that we always give a chance for the compositor to
738 * handle the commit and send a release event before checking for a free
740 if (dri2_surf
->throttle_callback
== NULL
) {
741 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
742 wl_callback_add_listener(dri2_surf
->throttle_callback
,
743 &throttle_listener
, dri2_surf
);
744 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
748 wl_display_flush(dri2_dpy
->wl_dpy
);
754 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
755 _EGLDisplay
*disp
, _EGLSurface
*surface
)
757 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
759 if (get_back_bo(dri2_surf
) < 0) {
760 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
764 return dri2_surf
->back
->age
;
768 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
770 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
773 static struct wl_buffer
*
774 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
778 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
779 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
780 __DRIimage
*image
= dri2_img
->dri_image
;
781 struct wl_buffer
*buffer
;
782 int width
, height
, format
, pitch
;
783 enum wl_drm_format wl_format
;
785 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
788 case __DRI_IMAGE_FORMAT_ARGB8888
:
789 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
791 wl_format
= WL_DRM_FORMAT_ARGB8888
;
793 case __DRI_IMAGE_FORMAT_XRGB8888
:
794 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
796 wl_format
= WL_DRM_FORMAT_XRGB8888
;
802 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
803 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
804 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
806 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
809 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
812 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
824 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
827 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
834 /* The buffer object will have been created with our internal event queue
835 * because it is using the wl_drm object as a proxy factory. We want the
836 * buffer to be used by the application so we'll reset it to the display's
837 * default event queue */
839 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
844 _eglError(EGL_BAD_MATCH
, "unsupported image format");
849 is_fd_render_node(int fd
)
853 if (fstat(fd
, &render
))
856 if (!S_ISCHR(render
.st_mode
))
859 if (render
.st_rdev
& 0x80)
865 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
867 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
870 if (dri2_dpy
->is_render_node
) {
871 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
872 "authenticate for render-nodes");
875 dri2_dpy
->authenticated
= 0;
877 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
878 if (roundtrip(dri2_dpy
) < 0)
881 if (!dri2_dpy
->authenticated
)
884 /* reset authenticated */
885 dri2_dpy
->authenticated
= 1;
891 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
893 struct dri2_egl_display
*dri2_dpy
= data
;
896 dri2_dpy
->device_name
= strdup(device
);
897 if (!dri2_dpy
->device_name
)
900 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
901 if (dri2_dpy
->fd
== -1) {
902 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
903 dri2_dpy
->device_name
, strerror(errno
));
907 if (is_fd_render_node(dri2_dpy
->fd
)) {
908 dri2_dpy
->authenticated
= 1;
910 drmGetMagic(dri2_dpy
->fd
, &magic
);
911 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
916 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
918 struct dri2_egl_display
*dri2_dpy
= data
;
921 case WL_DRM_FORMAT_ARGB8888
:
922 dri2_dpy
->formats
|= HAS_ARGB8888
;
924 case WL_DRM_FORMAT_XRGB8888
:
925 dri2_dpy
->formats
|= HAS_XRGB8888
;
927 case WL_DRM_FORMAT_RGB565
:
928 dri2_dpy
->formats
|= HAS_RGB565
;
934 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
936 struct dri2_egl_display
*dri2_dpy
= data
;
938 dri2_dpy
->capabilities
= value
;
942 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
944 struct dri2_egl_display
*dri2_dpy
= data
;
946 dri2_dpy
->authenticated
= 1;
949 static const struct wl_drm_listener drm_listener
= {
950 .device
= drm_handle_device
,
951 .format
= drm_handle_format
,
952 .authenticated
= drm_handle_authenticated
,
953 .capabilities
= drm_handle_capabilities
957 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
958 const char *interface
, uint32_t version
)
960 struct dri2_egl_display
*dri2_dpy
= data
;
964 if (strcmp(interface
, "wl_drm") == 0) {
966 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
967 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
972 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
977 static const struct wl_registry_listener registry_listener_drm
= {
978 .global
= registry_handle_global_drm
,
979 .global_remove
= registry_handle_global_remove
983 dri2_wl_swap_interval(_EGLDriver
*drv
,
988 if (interval
> surf
->Config
->MaxSwapInterval
)
989 interval
= surf
->Config
->MaxSwapInterval
;
990 else if (interval
< surf
->Config
->MinSwapInterval
)
991 interval
= surf
->Config
->MinSwapInterval
;
993 surf
->SwapInterval
= interval
;
999 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1001 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1003 /* We can't use values greater than 1 on Wayland because we are using the
1004 * frame callback to synchronise the frame and the only way we be sure to
1005 * get a frame callback is to attach a new buffer. Therefore we can't just
1006 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1008 if (dri2_dpy
->config
)
1009 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1010 "vblank_mode", &vblank_mode
);
1011 switch (vblank_mode
) {
1012 case DRI_CONF_VBLANK_NEVER
:
1013 dri2_dpy
->min_swap_interval
= 0;
1014 dri2_dpy
->max_swap_interval
= 0;
1015 dri2_dpy
->default_swap_interval
= 0;
1017 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1018 dri2_dpy
->min_swap_interval
= 1;
1019 dri2_dpy
->max_swap_interval
= 1;
1020 dri2_dpy
->default_swap_interval
= 1;
1022 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1023 dri2_dpy
->min_swap_interval
= 0;
1024 dri2_dpy
->max_swap_interval
= 1;
1025 dri2_dpy
->default_swap_interval
= 0;
1028 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1029 dri2_dpy
->min_swap_interval
= 0;
1030 dri2_dpy
->max_swap_interval
= 1;
1031 dri2_dpy
->default_swap_interval
= 1;
1036 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1037 .authenticate
= dri2_wl_authenticate
,
1038 .create_window_surface
= dri2_wl_create_window_surface
,
1039 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1040 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1041 .destroy_surface
= dri2_wl_destroy_surface
,
1042 .create_image
= dri2_create_image_khr
,
1043 .swap_interval
= dri2_wl_swap_interval
,
1044 .swap_buffers
= dri2_wl_swap_buffers
,
1045 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1046 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1047 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1048 .copy_buffers
= dri2_fallback_copy_buffers
,
1049 .query_buffer_age
= dri2_wl_query_buffer_age
,
1050 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1051 .get_sync_values
= dri2_fallback_get_sync_values
,
1055 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1057 struct dri2_egl_display
*dri2_dpy
;
1058 const __DRIconfig
*config
;
1061 static const unsigned int argb_masks
[4] =
1062 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1063 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1064 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1066 loader_set_logger(_eglLog
);
1068 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1070 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1072 disp
->DriverData
= (void *) dri2_dpy
;
1073 if (disp
->PlatformDisplay
== NULL
) {
1074 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1075 if (dri2_dpy
->wl_dpy
== NULL
)
1077 dri2_dpy
->own_device
= 1;
1079 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1082 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1084 if (dri2_dpy
->own_device
)
1085 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1087 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1088 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1089 dri2_dpy
->wl_queue
);
1090 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1091 ®istry_listener_drm
, dri2_dpy
);
1092 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1093 goto cleanup_registry
;
1095 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1098 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1101 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1102 &dri2_dpy
->is_different_gpu
);
1103 if (dri2_dpy
->is_different_gpu
) {
1104 free(dri2_dpy
->device_name
);
1105 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1106 if (!dri2_dpy
->device_name
) {
1107 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1108 "for requested GPU");
1113 /* we have to do the check now, because loader_get_user_preferred_fd
1114 * will return a render-node when the requested gpu is different
1115 * to the server, but also if the client asks for the same gpu than
1116 * the server by requesting its pci-id */
1117 dri2_dpy
->is_render_node
= is_fd_render_node(dri2_dpy
->fd
);
1119 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1120 if (dri2_dpy
->driver_name
== NULL
) {
1121 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1125 if (!dri2_load_driver(disp
))
1126 goto cleanup_driver_name
;
1128 dri2_dpy
->extensions
[0] = &image_loader_extension
.base
;
1129 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1130 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
1132 /* render nodes cannot use Gem names, and thus do not support
1133 * the __DRI_DRI2_LOADER extension */
1134 if (!dri2_dpy
->is_render_node
) {
1135 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1136 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1137 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1138 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1139 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1140 dri2_wl_get_buffers_with_format
;
1141 dri2_dpy
->extensions
[3] = &dri2_dpy
->dri2_loader_extension
.base
;
1142 dri2_dpy
->extensions
[4] = NULL
;
1144 dri2_dpy
->extensions
[3] = NULL
;
1146 dri2_dpy
->swap_available
= EGL_TRUE
;
1148 if (!dri2_create_screen(disp
))
1149 goto cleanup_driver
;
1151 dri2_wl_setup_swap_interval(dri2_dpy
);
1153 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1154 * createImageFromFds support indicates that Prime export/import
1155 * is supported by the driver. Fall back to
1156 * gem names if we don't have Prime support. */
1158 if (dri2_dpy
->image
->base
.version
< 7 ||
1159 dri2_dpy
->image
->createImageFromFds
== NULL
)
1160 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1162 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1163 * The server needs to accept them */
1164 if (dri2_dpy
->is_render_node
&&
1165 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1166 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1167 goto cleanup_screen
;
1170 if (dri2_dpy
->is_different_gpu
&&
1171 (dri2_dpy
->image
->base
.version
< 9 ||
1172 dri2_dpy
->image
->blitImage
== NULL
)) {
1173 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1174 "Image extension in the driver is not "
1175 "compatible. Version 9 or later and blitImage() "
1177 goto cleanup_screen
;
1180 types
= EGL_WINDOW_BIT
;
1181 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1182 config
= dri2_dpy
->driver_configs
[i
];
1183 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1184 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1185 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1186 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1187 if (dri2_dpy
->formats
& HAS_RGB565
)
1188 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1191 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1192 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1193 * because the buffer of the EGLImage has likely a tiling mode the server
1194 * gpu won't support. These is no way to check for now. Thus do not support the
1196 if (!dri2_dpy
->is_different_gpu
) {
1197 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1199 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1200 dri2_fallback_create_wayland_buffer_from_image
;
1202 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1204 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1206 /* Fill vtbl last to prevent accidentally calling virtual function during
1209 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1214 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1216 dlclose(dri2_dpy
->driver
);
1217 cleanup_driver_name
:
1218 free(dri2_dpy
->driver_name
);
1220 close(dri2_dpy
->fd
);
1222 free(dri2_dpy
->device_name
);
1223 wl_drm_destroy(dri2_dpy
->wl_drm
);
1225 wl_registry_destroy(dri2_dpy
->wl_registry
);
1226 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1234 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1236 if (format
== WL_SHM_FORMAT_RGB565
)
1238 else /* ARGB8888 || XRGB8888 */
1243 * Taken from weston shared/os-compatibility.c
1247 set_cloexec_or_close(int fd
)
1254 flags
= fcntl(fd
, F_GETFD
);
1258 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1269 * Taken from weston shared/os-compatibility.c
1273 create_tmpfile_cloexec(char *tmpname
)
1277 #ifdef HAVE_MKOSTEMP
1278 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1282 fd
= mkstemp(tmpname
);
1284 fd
= set_cloexec_or_close(fd
);
1293 * Taken from weston shared/os-compatibility.c
1295 * Create a new, unique, anonymous file of the given size, and
1296 * return the file descriptor for it. The file descriptor is set
1297 * CLOEXEC. The file is immediately suitable for mmap()'ing
1298 * the given size at offset zero.
1300 * The file should not have a permanent backing store like a disk,
1301 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1303 * The file name is deleted from the file system.
1305 * The file is suitable for buffer sharing between processes by
1306 * transmitting the file descriptor over Unix sockets using the
1307 * SCM_RIGHTS methods.
1309 * If the C library implements posix_fallocate(), it is used to
1310 * guarantee that disk space is available for the file at the
1311 * given size. If disk space is insufficent, errno is set to ENOSPC.
1312 * If posix_fallocate() is not supported, program may receive
1313 * SIGBUS on accessing mmap()'ed file contents instead.
1316 os_create_anonymous_file(off_t size
)
1318 static const char template[] = "/mesa-shared-XXXXXX";
1324 path
= getenv("XDG_RUNTIME_DIR");
1330 name
= malloc(strlen(path
) + sizeof(template));
1335 strcat(name
, template);
1337 fd
= create_tmpfile_cloexec(name
);
1344 ret
= ftruncate(fd
, size
);
1355 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1356 int format
, int w
, int h
,
1357 void **data
, int *size
,
1358 struct wl_buffer
**buffer
)
1360 struct wl_shm_pool
*pool
;
1361 int fd
, stride
, size_map
;
1364 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1365 size_map
= h
* stride
;
1367 /* Create a sharable buffer */
1368 fd
= os_create_anonymous_file(size_map
);
1372 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1373 if (data_map
== MAP_FAILED
) {
1378 /* Share it in a wl_buffer */
1379 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1380 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1381 wl_shm_pool_destroy(pool
);
1390 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1392 struct dri2_egl_display
*dri2_dpy
=
1393 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1396 /* we need to do the following operations only once per frame */
1397 if (dri2_surf
->back
)
1400 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1401 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1403 dri2_wl_release_buffers(dri2_surf
);
1405 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1406 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1407 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1408 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1409 dri2_surf
->current
= NULL
;
1412 /* find back buffer */
1414 /* We always want to throttle to some event (either a frame callback or
1415 * a sync request) after the commit so that we can be sure the
1416 * compositor has had a chance to handle it and send us a release event
1417 * before we look for a free buffer */
1418 while (dri2_surf
->throttle_callback
!= NULL
)
1419 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1420 dri2_dpy
->wl_queue
) == -1)
1423 /* try get free buffer already created */
1424 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1425 if (!dri2_surf
->color_buffers
[i
].locked
&&
1426 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1427 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1432 /* else choose any another free location */
1433 if (!dri2_surf
->back
) {
1434 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1435 if (!dri2_surf
->color_buffers
[i
].locked
) {
1436 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1437 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1439 dri2_surf
->base
.Width
,
1440 dri2_surf
->base
.Height
,
1441 &dri2_surf
->back
->data
,
1442 &dri2_surf
->back
->data_size
,
1443 &dri2_surf
->back
->wl_buffer
)) {
1444 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1447 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1448 dri2_dpy
->wl_queue
);
1449 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1450 &wl_buffer_listener
, dri2_surf
);
1456 if (!dri2_surf
->back
) {
1457 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1461 dri2_surf
->back
->locked
= 1;
1463 /* If we have an extra unlocked buffer at this point, we had to do triple
1464 * buffering for a while, but now can go back to just double buffering.
1465 * That means we can free any unlocked buffer now. */
1466 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1467 if (!dri2_surf
->color_buffers
[i
].locked
&&
1468 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1469 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1470 munmap(dri2_surf
->color_buffers
[i
].data
,
1471 dri2_surf
->color_buffers
[i
].data_size
);
1472 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1473 dri2_surf
->color_buffers
[i
].data
= NULL
;
1481 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1483 /* if there has been a resize: */
1484 if (!dri2_surf
->current
)
1487 return dri2_surf
->current
->data
;
1491 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1493 assert(dri2_surf
->back
);
1494 return dri2_surf
->back
->data
;
1498 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1500 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1502 if (dri2_surf
->base
.SwapInterval
> 0) {
1503 dri2_surf
->throttle_callback
=
1504 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1505 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1506 &throttle_listener
, dri2_surf
);
1507 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1508 dri2_dpy
->wl_queue
);
1511 dri2_surf
->current
= dri2_surf
->back
;
1512 dri2_surf
->back
= NULL
;
1514 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1515 dri2_surf
->current
->wl_buffer
,
1516 dri2_surf
->dx
, dri2_surf
->dy
);
1518 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1519 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1520 /* reset resize growing parameters */
1524 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1525 0, 0, INT32_MAX
, INT32_MAX
);
1526 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1528 /* If we're not waiting for a frame callback then we'll at least throttle
1529 * to a sync callback so that we always give a chance for the compositor to
1530 * handle the commit and send a release event before checking for a free
1532 if (dri2_surf
->throttle_callback
== NULL
) {
1533 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
1534 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1535 &throttle_listener
, dri2_surf
);
1536 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1537 dri2_dpy
->wl_queue
);
1540 wl_display_flush(dri2_dpy
->wl_dpy
);
1544 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1545 int *x
, int *y
, int *w
, int *h
,
1546 void *loaderPrivate
)
1548 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1550 (void) swrast_update_buffers(dri2_surf
);
1553 *w
= dri2_surf
->base
.Width
;
1554 *h
= dri2_surf
->base
.Height
;
1558 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1559 int x
, int y
, int w
, int h
,
1560 char *data
, void *loaderPrivate
)
1562 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1563 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1564 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1565 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1566 int dst_stride
= copy_width
;
1569 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1571 memset(data
, 0, copy_width
* h
);
1575 assert(data
!= src
);
1576 assert(copy_width
<= src_stride
);
1579 src
+= y
* src_stride
;
1582 if (copy_width
> src_stride
-x_offset
)
1583 copy_width
= src_stride
-x_offset
;
1584 if (h
> dri2_surf
->base
.Height
-y
)
1585 h
= dri2_surf
->base
.Height
-y
;
1588 memcpy(dst
, src
, copy_width
);
1595 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1596 int x
, int y
, int w
, int h
, int stride
,
1597 char *data
, void *loaderPrivate
)
1599 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1600 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1601 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1602 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1605 assert(copy_width
<= stride
);
1607 (void) swrast_update_buffers(dri2_surf
);
1608 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1610 /* partial copy, copy old content */
1611 if (copy_width
< dst_stride
)
1612 dri2_wl_swrast_get_image(draw
, 0, 0,
1613 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1614 dst
, loaderPrivate
);
1617 dst
+= y
* dst_stride
;
1621 /* drivers expect we do these checks (and some rely on it) */
1622 if (copy_width
> dst_stride
-x_offset
)
1623 copy_width
= dst_stride
-x_offset
;
1624 if (h
> dri2_surf
->base
.Height
-y
)
1625 h
= dri2_surf
->base
.Height
-y
;
1628 memcpy(dst
, src
, copy_width
);
1632 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1636 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1637 int x
, int y
, int w
, int h
,
1638 char *data
, void *loaderPrivate
)
1640 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1643 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1644 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1645 stride
, data
, loaderPrivate
);
1649 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1651 static _EGLSurface
*
1652 dri2_wl_swrast_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1653 _EGLConfig
*conf
, void *native_window
,
1654 const EGLint
*attrib_list
)
1656 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1657 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1658 struct wl_egl_window
*window
= native_window
;
1659 struct dri2_egl_surface
*dri2_surf
;
1663 dri2_surf
= calloc(1, sizeof *dri2_surf
);
1665 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1669 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
1672 if (conf
->RedSize
== 5)
1673 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
1674 else if (conf
->AlphaSize
== 0)
1675 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
1677 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
1679 dri2_surf
->wl_win
= window
;
1681 dri2_surf
->base
.Width
= -1;
1682 dri2_surf
->base
.Height
= -1;
1684 dri2_surf
->dri_drawable
=
1685 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
1686 dri2_conf
->dri_double_config
,
1688 if (dri2_surf
->dri_drawable
== NULL
) {
1689 _eglError(EGL_BAD_ALLOC
, "swrast->createNewDrawable");
1690 goto cleanup_dri_drawable
;
1693 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
1694 dri2_dpy
->default_swap_interval
);
1696 return &dri2_surf
->base
;
1698 cleanup_dri_drawable
:
1699 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1707 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1709 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1710 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1712 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1717 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1719 struct dri2_egl_display
*dri2_dpy
= data
;
1722 case WL_SHM_FORMAT_ARGB8888
:
1723 dri2_dpy
->formats
|= HAS_ARGB8888
;
1725 case WL_SHM_FORMAT_XRGB8888
:
1726 dri2_dpy
->formats
|= HAS_XRGB8888
;
1728 case WL_SHM_FORMAT_RGB565
:
1729 dri2_dpy
->formats
|= HAS_RGB565
;
1734 static const struct wl_shm_listener shm_listener
= {
1735 .format
= shm_handle_format
1739 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1740 const char *interface
, uint32_t version
)
1742 struct dri2_egl_display
*dri2_dpy
= data
;
1744 if (strcmp(interface
, "wl_shm") == 0) {
1746 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1747 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1751 static const struct wl_registry_listener registry_listener_swrast
= {
1752 .global
= registry_handle_global_swrast
,
1753 .global_remove
= registry_handle_global_remove
1756 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1757 .authenticate
= NULL
,
1758 .create_window_surface
= dri2_wl_swrast_create_window_surface
,
1759 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1760 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1761 .destroy_surface
= dri2_wl_destroy_surface
,
1762 .create_image
= dri2_fallback_create_image_khr
,
1763 .swap_interval
= dri2_wl_swap_interval
,
1764 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1765 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1766 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1767 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1768 .copy_buffers
= dri2_fallback_copy_buffers
,
1769 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1770 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1771 .get_sync_values
= dri2_fallback_get_sync_values
,
1775 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1777 struct dri2_egl_display
*dri2_dpy
;
1778 const __DRIconfig
*config
;
1781 static const unsigned int argb_masks
[4] =
1782 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1783 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1784 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1786 loader_set_logger(_eglLog
);
1788 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1790 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1792 disp
->DriverData
= (void *) dri2_dpy
;
1793 if (disp
->PlatformDisplay
== NULL
) {
1794 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1795 if (dri2_dpy
->wl_dpy
== NULL
)
1797 dri2_dpy
->own_device
= 1;
1799 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1802 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1804 if (dri2_dpy
->own_device
)
1805 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1807 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1808 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1809 dri2_dpy
->wl_queue
);
1810 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1811 ®istry_listener_swrast
, dri2_dpy
);
1813 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1814 goto cleanup_registry
;
1816 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1819 dri2_dpy
->driver_name
= strdup("swrast");
1820 if (!dri2_load_driver_swrast(disp
))
1823 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
1824 dri2_dpy
->swrast_loader_extension
.base
.version
= 2;
1825 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= dri2_wl_swrast_get_drawable_info
;
1826 dri2_dpy
->swrast_loader_extension
.putImage
= dri2_wl_swrast_put_image
;
1827 dri2_dpy
->swrast_loader_extension
.getImage
= dri2_wl_swrast_get_image
;
1828 dri2_dpy
->swrast_loader_extension
.putImage2
= dri2_wl_swrast_put_image2
;
1830 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
1831 dri2_dpy
->extensions
[1] = NULL
;
1833 if (!dri2_create_screen(disp
))
1834 goto cleanup_driver
;
1836 dri2_wl_setup_swap_interval(dri2_dpy
);
1838 types
= EGL_WINDOW_BIT
;
1839 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1840 config
= dri2_dpy
->driver_configs
[i
];
1841 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1842 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1843 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1844 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1845 if (dri2_dpy
->formats
& HAS_RGB565
)
1846 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1849 /* Fill vtbl last to prevent accidentally calling virtual function during
1852 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1857 dlclose(dri2_dpy
->driver
);
1859 wl_shm_destroy(dri2_dpy
->wl_shm
);
1861 wl_registry_destroy(dri2_dpy
->wl_registry
);
1862 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1870 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1872 EGLBoolean initialized
= EGL_TRUE
;
1874 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1877 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1878 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1881 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);