2 * Copyright © 2011-2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
40 #include "egl_dri2_fallbacks.h"
43 #include <wayland-client.h>
44 #include "wayland-drm-client-protocol.h"
46 enum wl_drm_format_flags
{
53 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
57 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
62 wl_callback_destroy(callback
);
65 static const struct wl_callback_listener sync_listener
= {
70 roundtrip(struct dri2_egl_display
*dri2_dpy
)
72 struct wl_callback
*callback
;
73 int done
= 0, ret
= 0;
75 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
76 wl_callback_add_listener(callback
, &sync_listener
, &done
);
77 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
78 while (ret
!= -1 && !done
)
79 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
82 wl_callback_destroy(callback
);
88 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
90 struct dri2_egl_surface
*dri2_surf
= data
;
93 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
94 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
97 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
98 wl_buffer_destroy(buffer
);
102 dri2_surf
->color_buffers
[i
].locked
= 0;
105 static struct wl_buffer_listener wl_buffer_listener
= {
110 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
112 struct dri2_egl_surface
*dri2_surf
= data
;
113 struct dri2_egl_display
*dri2_dpy
=
114 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
116 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
120 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
123 dri2_wl_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
124 _EGLConfig
*conf
, void *native_window
,
125 const EGLint
*attrib_list
)
127 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
128 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
129 struct wl_egl_window
*window
= native_window
;
130 struct dri2_egl_surface
*dri2_surf
;
134 dri2_surf
= calloc(1, sizeof *dri2_surf
);
136 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
140 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
143 if (conf
->RedSize
== 5)
144 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
145 else if (conf
->AlphaSize
== 0)
146 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
148 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
152 dri2_surf
->wl_win
= window
;
154 dri2_surf
->wl_win
->private = dri2_surf
;
155 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
157 dri2_surf
->base
.Width
= -1;
158 dri2_surf
->base
.Height
= -1;
164 dri2_surf
->dri_drawable
=
165 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
166 type
== EGL_WINDOW_BIT
?
167 dri2_conf
->dri_double_config
:
168 dri2_conf
->dri_single_config
,
170 if (dri2_surf
->dri_drawable
== NULL
) {
171 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
172 goto cleanup_dri_drawable
;
175 return &dri2_surf
->base
;
177 cleanup_dri_drawable
:
178 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
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
, EGL_WINDOW_BIT
, conf
,
197 native_window
, attrib_list
);
200 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
206 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
207 _EGLConfig
*conf
, void *native_window
,
208 const EGLint
*attrib_list
)
210 /* From the EGL_EXT_platform_wayland spec, version 3:
212 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
213 * that belongs to Wayland. Any such call fails and generates
216 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
222 * Called via eglDestroySurface(), drv->API.DestroySurface().
225 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
227 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
228 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
233 if (!_eglPutSurface(surf
))
236 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
238 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
239 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
240 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
241 if (dri2_surf
->color_buffers
[i
].dri_image
)
242 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
243 if (dri2_surf
->color_buffers
[i
].linear_copy
)
244 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
247 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
248 if (dri2_surf
->dri_buffers
[i
] &&
249 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
250 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
251 dri2_surf
->dri_buffers
[i
]);
253 if (dri2_surf
->throttle_callback
)
254 wl_callback_destroy(dri2_surf
->throttle_callback
);
256 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
257 dri2_surf
->wl_win
->private = NULL
;
258 dri2_surf
->wl_win
->resize_callback
= NULL
;
267 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
269 struct dri2_egl_display
*dri2_dpy
=
270 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
273 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
274 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
275 !dri2_surf
->color_buffers
[i
].locked
)
276 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
277 if (dri2_surf
->color_buffers
[i
].dri_image
)
278 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
279 if (dri2_surf
->color_buffers
[i
].linear_copy
)
280 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
282 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
283 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
284 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
285 dri2_surf
->color_buffers
[i
].locked
= 0;
288 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
289 if (dri2_surf
->dri_buffers
[i
] &&
290 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
291 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
292 dri2_surf
->dri_buffers
[i
]);
296 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
298 struct dri2_egl_display
*dri2_dpy
=
299 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
301 unsigned int dri_image_format
;
303 /* currently supports three WL DRM formats,
304 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
305 * and WL_DRM_FORMAT_RGB565
307 switch (dri2_surf
->format
) {
308 case WL_DRM_FORMAT_ARGB8888
:
309 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
311 case WL_DRM_FORMAT_XRGB8888
:
312 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
314 case WL_DRM_FORMAT_RGB565
:
315 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
318 /* format is not supported */
322 /* We always want to throttle to some event (either a frame callback or
323 * a sync request) after the commit so that we can be sure the
324 * compositor has had a chance to handle it and send us a release event
325 * before we look for a free buffer */
326 while (dri2_surf
->throttle_callback
!= NULL
)
327 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
328 dri2_dpy
->wl_queue
) == -1)
331 if (dri2_surf
->back
== NULL
) {
332 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
333 /* Get an unlocked buffer, preferrably one with a dri_buffer
334 * already allocated. */
335 if (dri2_surf
->color_buffers
[i
].locked
)
337 if (dri2_surf
->back
== NULL
)
338 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
339 else if (dri2_surf
->back
->dri_image
== NULL
)
340 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
344 if (dri2_surf
->back
== NULL
)
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
,
354 __DRI_IMAGE_USE_SHARE
|
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
?
368 0 : __DRI_IMAGE_USE_SHARE
,
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
.Type
== EGL_WINDOW_BIT
&&
432 (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
433 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
)) {
435 dri2_wl_release_buffers(dri2_surf
);
437 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
438 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
439 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
440 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
443 if (get_back_bo(dri2_surf
) < 0) {
444 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
448 /* If we have an extra unlocked buffer at this point, we had to do triple
449 * buffering for a while, but now can go back to just double buffering.
450 * That means we can free any unlocked buffer now. */
451 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
452 if (!dri2_surf
->color_buffers
[i
].locked
&&
453 dri2_surf
->color_buffers
[i
].wl_buffer
) {
454 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
455 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
456 if (dri2_dpy
->is_different_gpu
)
457 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
458 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
459 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
460 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
468 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
469 int *width
, int *height
,
470 unsigned int *attachments
, int count
,
471 int *out_count
, void *loaderPrivate
)
473 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
476 if (update_buffers(dri2_surf
) < 0)
479 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
480 switch (attachments
[i
]) {
481 case __DRI_BUFFER_BACK_LEFT
:
482 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
485 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
486 &dri2_surf
->buffers
[j
]) < 0) {
487 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
498 *width
= dri2_surf
->base
.Width
;
499 *height
= dri2_surf
->base
.Height
;
501 return dri2_surf
->buffers
;
505 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
506 int *width
, int *height
,
507 unsigned int *attachments
, int count
,
508 int *out_count
, void *loaderPrivate
)
510 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
511 unsigned int *attachments_with_format
;
517 switch (dri2_surf
->format
) {
518 case WL_DRM_FORMAT_ARGB8888
:
519 case WL_DRM_FORMAT_XRGB8888
:
522 case WL_DRM_FORMAT_RGB565
:
526 /* format is not supported */
530 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
531 if (!attachments_with_format
) {
536 for (i
= 0; i
< count
; ++i
) {
537 attachments_with_format
[2*i
] = attachments
[i
];
538 attachments_with_format
[2*i
+ 1] = bpp
;
542 dri2_wl_get_buffers_with_format(driDrawable
,
544 attachments_with_format
, count
,
545 out_count
, loaderPrivate
);
547 free(attachments_with_format
);
553 image_get_buffers(__DRIdrawable
*driDrawable
,
557 uint32_t buffer_mask
,
558 struct __DRIimageList
*buffers
)
560 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
562 if (update_buffers(dri2_surf
) < 0)
565 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
566 buffers
->back
= dri2_surf
->back
->dri_image
;
572 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
575 (void) loaderPrivate
;
578 static const __DRIimageLoaderExtension image_loader_extension
= {
579 .base
= { __DRI_IMAGE_LOADER
, 1 },
581 .getBuffers
= image_get_buffers
,
582 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
586 wayland_throttle_callback(void *data
,
587 struct wl_callback
*callback
,
590 struct dri2_egl_surface
*dri2_surf
= data
;
592 dri2_surf
->throttle_callback
= NULL
;
593 wl_callback_destroy(callback
);
596 static const struct wl_callback_listener throttle_listener
= {
597 wayland_throttle_callback
601 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
603 struct dri2_egl_display
*dri2_dpy
=
604 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
606 int fd
, stride
, name
;
608 if (dri2_surf
->current
->wl_buffer
!= NULL
)
611 if (dri2_dpy
->is_different_gpu
) {
612 image
= dri2_surf
->current
->linear_copy
;
614 image
= dri2_surf
->current
->dri_image
;
616 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
617 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
618 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
620 dri2_surf
->current
->wl_buffer
=
621 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
623 dri2_surf
->base
.Width
,
624 dri2_surf
->base
.Height
,
631 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
632 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
634 dri2_surf
->current
->wl_buffer
=
635 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
637 dri2_surf
->base
.Width
,
638 dri2_surf
->base
.Height
,
643 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
645 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
646 &wl_buffer_listener
, dri2_surf
);
650 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
653 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
659 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
660 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
663 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
664 if (dri2_surf
->color_buffers
[i
].age
> 0)
665 dri2_surf
->color_buffers
[i
].age
++;
667 /* Make sure we have a back buffer in case we're swapping without ever
669 if (get_back_bo(dri2_surf
) < 0) {
670 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
674 if (draw
->SwapInterval
> 0) {
675 dri2_surf
->throttle_callback
=
676 wl_surface_frame(dri2_surf
->wl_win
->surface
);
677 wl_callback_add_listener(dri2_surf
->throttle_callback
,
678 &throttle_listener
, dri2_surf
);
679 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
683 dri2_surf
->back
->age
= 1;
684 dri2_surf
->current
= dri2_surf
->back
;
685 dri2_surf
->back
= NULL
;
687 create_wl_buffer(dri2_surf
);
689 wl_surface_attach(dri2_surf
->wl_win
->surface
,
690 dri2_surf
->current
->wl_buffer
,
691 dri2_surf
->dx
, dri2_surf
->dy
);
693 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
694 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
695 /* reset resize growing parameters */
700 wl_surface_damage(dri2_surf
->wl_win
->surface
,
701 0, 0, INT32_MAX
, INT32_MAX
);
703 for (i
= 0; i
< n_rects
; i
++) {
704 const int *rect
= &rects
[i
* 4];
705 wl_surface_damage(dri2_surf
->wl_win
->surface
,
707 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
712 if (dri2_dpy
->is_different_gpu
) {
713 _EGLContext
*ctx
= _eglGetCurrentContext();
714 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
715 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
716 dri2_surf
->current
->linear_copy
,
717 dri2_surf
->current
->dri_image
,
718 0, 0, dri2_surf
->base
.Width
,
719 dri2_surf
->base
.Height
,
720 0, 0, dri2_surf
->base
.Width
,
721 dri2_surf
->base
.Height
, 0);
724 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
725 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
727 wl_surface_commit(dri2_surf
->wl_win
->surface
);
729 /* If we're not waiting for a frame callback then we'll at least throttle
730 * to a sync callback so that we always give a chance for the compositor to
731 * handle the commit and send a release event before checking for a free
733 if (dri2_surf
->throttle_callback
== NULL
) {
734 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
735 wl_callback_add_listener(dri2_surf
->throttle_callback
,
736 &throttle_listener
, dri2_surf
);
737 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
741 wl_display_flush(dri2_dpy
->wl_dpy
);
747 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
748 _EGLDisplay
*disp
, _EGLSurface
*surface
)
750 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
752 if (get_back_bo(dri2_surf
) < 0) {
753 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
757 return dri2_surf
->back
->age
;
761 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
763 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
766 static struct wl_buffer
*
767 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
771 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
772 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
773 __DRIimage
*image
= dri2_img
->dri_image
;
774 struct wl_buffer
*buffer
;
775 int width
, height
, format
, pitch
;
776 enum wl_drm_format wl_format
;
778 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
781 case __DRI_IMAGE_FORMAT_ARGB8888
:
782 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
784 wl_format
= WL_DRM_FORMAT_ARGB8888
;
786 case __DRI_IMAGE_FORMAT_XRGB8888
:
787 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
789 wl_format
= WL_DRM_FORMAT_XRGB8888
;
795 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
796 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
797 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
799 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
802 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
805 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
817 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
820 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
827 /* The buffer object will have been created with our internal event queue
828 * because it is using the wl_drm object as a proxy factory. We want the
829 * buffer to be used by the application so we'll reset it to the display's
830 * default event queue */
832 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
837 _eglError(EGL_BAD_MATCH
, "unsupported image format");
842 is_fd_render_node(int fd
)
846 if (fstat(fd
, &render
))
849 if (!S_ISCHR(render
.st_mode
))
852 if (render
.st_rdev
& 0x80)
858 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
860 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
863 if (dri2_dpy
->is_render_node
) {
864 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
865 "authenticate for render-nodes");
868 dri2_dpy
->authenticated
= 0;
870 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
871 if (roundtrip(dri2_dpy
) < 0)
874 if (!dri2_dpy
->authenticated
)
877 /* reset authenticated */
878 dri2_dpy
->authenticated
= 1;
884 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
886 struct dri2_egl_display
*dri2_dpy
= data
;
889 dri2_dpy
->device_name
= strdup(device
);
890 if (!dri2_dpy
->device_name
)
894 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
895 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
898 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
899 if (dri2_dpy
->fd
!= -1)
900 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
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 (is_fd_render_node(dri2_dpy
->fd
)) {
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
= {
954 drm_handle_authenticated
,
955 drm_handle_capabilities
959 registry_handle_global(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
= {
980 registry_handle_global
,
981 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
,
1057 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1059 struct dri2_egl_display
*dri2_dpy
;
1060 const __DRIconfig
*config
;
1063 static const unsigned int argb_masks
[4] =
1064 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1065 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1066 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1068 loader_set_logger(_eglLog
);
1070 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1072 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1074 disp
->DriverData
= (void *) dri2_dpy
;
1075 if (disp
->PlatformDisplay
== NULL
) {
1076 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1077 if (dri2_dpy
->wl_dpy
== NULL
)
1079 dri2_dpy
->own_device
= 1;
1081 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1084 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1086 if (dri2_dpy
->own_device
)
1087 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1089 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1090 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1091 dri2_dpy
->wl_queue
);
1092 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1093 ®istry_listener
, dri2_dpy
);
1094 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1095 goto cleanup_registry
;
1097 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1100 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1103 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1104 &dri2_dpy
->is_different_gpu
);
1105 if (dri2_dpy
->is_different_gpu
) {
1106 free(dri2_dpy
->device_name
);
1107 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1108 if (!dri2_dpy
->device_name
) {
1109 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1110 "for requested GPU");
1115 /* we have to do the check now, because loader_get_user_preferred_fd
1116 * will return a render-node when the requested gpu is different
1117 * to the server, but also if the client asks for the same gpu than
1118 * the server by requesting its pci-id */
1119 dri2_dpy
->is_render_node
= is_fd_render_node(dri2_dpy
->fd
);
1121 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1122 if (dri2_dpy
->driver_name
== NULL
) {
1123 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1127 if (!dri2_load_driver(disp
))
1128 goto cleanup_driver_name
;
1130 dri2_dpy
->extensions
[0] = &image_loader_extension
.base
;
1131 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1132 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
1134 /* render nodes cannot use Gem names, and thus do not support
1135 * the __DRI_DRI2_LOADER extension */
1136 if (!dri2_dpy
->is_render_node
) {
1137 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1138 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1139 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1140 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1141 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1142 dri2_wl_get_buffers_with_format
;
1143 dri2_dpy
->extensions
[3] = &dri2_dpy
->dri2_loader_extension
.base
;
1144 dri2_dpy
->extensions
[4] = NULL
;
1146 dri2_dpy
->extensions
[3] = NULL
;
1148 dri2_dpy
->swap_available
= EGL_TRUE
;
1150 if (!dri2_create_screen(disp
))
1151 goto cleanup_driver
;
1153 dri2_wl_setup_swap_interval(dri2_dpy
);
1155 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1156 * createImageFromFds support indicates that Prime export/import
1157 * is supported by the driver. Fall back to
1158 * gem names if we don't have Prime support. */
1160 if (dri2_dpy
->image
->base
.version
< 7 ||
1161 dri2_dpy
->image
->createImageFromFds
== NULL
)
1162 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1164 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1165 * The server needs to accept them */
1166 if (dri2_dpy
->is_render_node
&&
1167 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1168 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1169 goto cleanup_screen
;
1172 if (dri2_dpy
->is_different_gpu
&&
1173 (dri2_dpy
->image
->base
.version
< 9 ||
1174 dri2_dpy
->image
->blitImage
== NULL
)) {
1175 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1176 "Image extension in the driver is not "
1177 "compatible. Version 9 or later and blitImage() "
1179 goto cleanup_screen
;
1182 types
= EGL_WINDOW_BIT
;
1183 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1184 config
= dri2_dpy
->driver_configs
[i
];
1185 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1186 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1187 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1188 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1189 if (dri2_dpy
->formats
& HAS_RGB565
)
1190 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1193 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1194 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1195 * because the buffer of the EGLImage has likely a tiling mode the server
1196 * gpu won't support. These is no way to check for now. Thus do not support the
1198 if (!dri2_dpy
->is_different_gpu
) {
1199 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1201 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1202 dri2_fallback_create_wayland_buffer_from_image
;
1204 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1206 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1208 /* we're supporting EGL 1.4 */
1209 disp
->VersionMajor
= 1;
1210 disp
->VersionMinor
= 4;
1212 /* Fill vtbl last to prevent accidentally calling virtual function during
1215 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1220 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1222 dlclose(dri2_dpy
->driver
);
1223 cleanup_driver_name
:
1224 free(dri2_dpy
->driver_name
);
1226 close(dri2_dpy
->fd
);
1228 free(dri2_dpy
->device_name
);
1229 wl_drm_destroy(dri2_dpy
->wl_drm
);
1231 wl_registry_destroy(dri2_dpy
->wl_registry
);
1232 wl_event_queue_destroy(dri2_dpy
->wl_queue
);