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
,
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
, EGL_WINDOW_BIT
, 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
;
150 dri2_surf
->wl_win
= window
;
152 dri2_surf
->wl_win
->private = dri2_surf
;
153 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
155 dri2_surf
->base
.Width
= -1;
156 dri2_surf
->base
.Height
= -1;
158 dri2_surf
->dri_drawable
=
159 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
160 dri2_conf
->dri_double_config
,
162 if (dri2_surf
->dri_drawable
== NULL
) {
163 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
164 goto cleanup_dri_drawable
;
167 return &dri2_surf
->base
;
169 cleanup_dri_drawable
:
170 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
178 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
181 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
182 _EGLConfig
*conf
, void *native_window
,
183 const EGLint
*attrib_list
)
185 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
188 surf
= dri2_wl_create_surface(drv
, disp
, conf
, native_window
, attrib_list
);
191 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
197 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
198 _EGLConfig
*conf
, void *native_window
,
199 const EGLint
*attrib_list
)
201 /* From the EGL_EXT_platform_wayland spec, version 3:
203 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
204 * that belongs to Wayland. Any such call fails and generates
207 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
213 * Called via eglDestroySurface(), drv->API.DestroySurface().
216 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
218 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
219 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
224 if (!_eglPutSurface(surf
))
227 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
229 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
230 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
231 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
232 if (dri2_surf
->color_buffers
[i
].dri_image
)
233 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
234 if (dri2_surf
->color_buffers
[i
].linear_copy
)
235 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
238 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
239 if (dri2_surf
->dri_buffers
[i
] &&
240 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
241 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
242 dri2_surf
->dri_buffers
[i
]);
244 if (dri2_surf
->throttle_callback
)
245 wl_callback_destroy(dri2_surf
->throttle_callback
);
247 dri2_surf
->wl_win
->private = NULL
;
248 dri2_surf
->wl_win
->resize_callback
= NULL
;
256 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
258 struct dri2_egl_display
*dri2_dpy
=
259 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
262 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
263 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
264 !dri2_surf
->color_buffers
[i
].locked
)
265 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
266 if (dri2_surf
->color_buffers
[i
].dri_image
)
267 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
268 if (dri2_surf
->color_buffers
[i
].linear_copy
)
269 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
271 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
272 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
273 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
274 dri2_surf
->color_buffers
[i
].locked
= 0;
277 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
278 if (dri2_surf
->dri_buffers
[i
] &&
279 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
280 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
281 dri2_surf
->dri_buffers
[i
]);
285 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
287 struct dri2_egl_display
*dri2_dpy
=
288 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
290 unsigned int dri_image_format
;
292 /* currently supports three WL DRM formats,
293 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
294 * and WL_DRM_FORMAT_RGB565
296 switch (dri2_surf
->format
) {
297 case WL_DRM_FORMAT_ARGB8888
:
298 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
300 case WL_DRM_FORMAT_XRGB8888
:
301 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
303 case WL_DRM_FORMAT_RGB565
:
304 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
307 /* format is not supported */
311 /* We always want to throttle to some event (either a frame callback or
312 * a sync request) after the commit so that we can be sure the
313 * compositor has had a chance to handle it and send us a release event
314 * before we look for a free buffer */
315 while (dri2_surf
->throttle_callback
!= NULL
)
316 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
317 dri2_dpy
->wl_queue
) == -1)
320 if (dri2_surf
->back
== NULL
) {
321 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
322 /* Get an unlocked buffer, preferrably one with a dri_buffer
323 * already allocated. */
324 if (dri2_surf
->color_buffers
[i
].locked
)
326 if (dri2_surf
->back
== NULL
)
327 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
328 else if (dri2_surf
->back
->dri_image
== NULL
)
329 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
333 if (dri2_surf
->back
== NULL
)
336 if (dri2_dpy
->is_different_gpu
&&
337 dri2_surf
->back
->linear_copy
== NULL
) {
338 dri2_surf
->back
->linear_copy
=
339 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
340 dri2_surf
->base
.Width
,
341 dri2_surf
->base
.Height
,
343 __DRI_IMAGE_USE_SHARE
|
344 __DRI_IMAGE_USE_LINEAR
,
346 if (dri2_surf
->back
->linear_copy
== NULL
)
350 if (dri2_surf
->back
->dri_image
== NULL
) {
351 dri2_surf
->back
->dri_image
=
352 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
353 dri2_surf
->base
.Width
,
354 dri2_surf
->base
.Height
,
356 dri2_dpy
->is_different_gpu
?
357 0 : __DRI_IMAGE_USE_SHARE
,
359 dri2_surf
->back
->age
= 0;
361 if (dri2_surf
->back
->dri_image
== NULL
)
364 dri2_surf
->back
->locked
= 1;
371 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
373 struct dri2_egl_display
*dri2_dpy
=
374 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
378 image
= dri2_surf
->back
->dri_image
;
380 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
381 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
383 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
385 buffer
->pitch
= pitch
;
391 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
392 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
394 struct dri2_egl_display
*dri2_dpy
=
395 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
396 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
399 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
401 dri2_surf
->base
.Width
,
402 dri2_surf
->base
.Height
);
403 dri2_surf
->dri_buffers
[attachment
] = b
;
408 memcpy(buffer
, b
, sizeof *buffer
);
414 update_buffers(struct dri2_egl_surface
*dri2_surf
)
416 struct dri2_egl_display
*dri2_dpy
=
417 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
420 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
421 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
423 dri2_wl_release_buffers(dri2_surf
);
425 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
426 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
427 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
428 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
431 if (get_back_bo(dri2_surf
) < 0) {
432 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
436 /* If we have an extra unlocked buffer at this point, we had to do triple
437 * buffering for a while, but now can go back to just double buffering.
438 * That means we can free any unlocked buffer now. */
439 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
440 if (!dri2_surf
->color_buffers
[i
].locked
&&
441 dri2_surf
->color_buffers
[i
].wl_buffer
) {
442 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
443 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
444 if (dri2_dpy
->is_different_gpu
)
445 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
446 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
447 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
448 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
456 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
457 int *width
, int *height
,
458 unsigned int *attachments
, int count
,
459 int *out_count
, void *loaderPrivate
)
461 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
464 if (update_buffers(dri2_surf
) < 0)
467 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
468 switch (attachments
[i
]) {
469 case __DRI_BUFFER_BACK_LEFT
:
470 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
473 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
474 &dri2_surf
->buffers
[j
]) < 0) {
475 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
486 *width
= dri2_surf
->base
.Width
;
487 *height
= dri2_surf
->base
.Height
;
489 return dri2_surf
->buffers
;
493 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
494 int *width
, int *height
,
495 unsigned int *attachments
, int count
,
496 int *out_count
, void *loaderPrivate
)
498 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
499 unsigned int *attachments_with_format
;
505 switch (dri2_surf
->format
) {
506 case WL_DRM_FORMAT_ARGB8888
:
507 case WL_DRM_FORMAT_XRGB8888
:
510 case WL_DRM_FORMAT_RGB565
:
514 /* format is not supported */
518 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
519 if (!attachments_with_format
) {
524 for (i
= 0; i
< count
; ++i
) {
525 attachments_with_format
[2*i
] = attachments
[i
];
526 attachments_with_format
[2*i
+ 1] = bpp
;
530 dri2_wl_get_buffers_with_format(driDrawable
,
532 attachments_with_format
, count
,
533 out_count
, loaderPrivate
);
535 free(attachments_with_format
);
541 image_get_buffers(__DRIdrawable
*driDrawable
,
545 uint32_t buffer_mask
,
546 struct __DRIimageList
*buffers
)
548 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
550 if (update_buffers(dri2_surf
) < 0)
553 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
554 buffers
->back
= dri2_surf
->back
->dri_image
;
560 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
563 (void) loaderPrivate
;
566 static const __DRIimageLoaderExtension image_loader_extension
= {
567 .base
= { __DRI_IMAGE_LOADER
, 1 },
569 .getBuffers
= image_get_buffers
,
570 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
574 wayland_throttle_callback(void *data
,
575 struct wl_callback
*callback
,
578 struct dri2_egl_surface
*dri2_surf
= data
;
580 dri2_surf
->throttle_callback
= NULL
;
581 wl_callback_destroy(callback
);
584 static const struct wl_callback_listener throttle_listener
= {
585 wayland_throttle_callback
589 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
591 struct dri2_egl_display
*dri2_dpy
=
592 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
594 int fd
, stride
, name
;
596 if (dri2_surf
->current
->wl_buffer
!= NULL
)
599 if (dri2_dpy
->is_different_gpu
) {
600 image
= dri2_surf
->current
->linear_copy
;
602 image
= dri2_surf
->current
->dri_image
;
604 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
605 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
606 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
608 dri2_surf
->current
->wl_buffer
=
609 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
611 dri2_surf
->base
.Width
,
612 dri2_surf
->base
.Height
,
619 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
620 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
622 dri2_surf
->current
->wl_buffer
=
623 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
625 dri2_surf
->base
.Width
,
626 dri2_surf
->base
.Height
,
631 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
633 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
634 &wl_buffer_listener
, dri2_surf
);
638 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
641 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
647 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
648 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
651 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
652 if (dri2_surf
->color_buffers
[i
].age
> 0)
653 dri2_surf
->color_buffers
[i
].age
++;
655 /* Make sure we have a back buffer in case we're swapping without ever
657 if (get_back_bo(dri2_surf
) < 0) {
658 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
662 if (draw
->SwapInterval
> 0) {
663 dri2_surf
->throttle_callback
=
664 wl_surface_frame(dri2_surf
->wl_win
->surface
);
665 wl_callback_add_listener(dri2_surf
->throttle_callback
,
666 &throttle_listener
, dri2_surf
);
667 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
671 dri2_surf
->back
->age
= 1;
672 dri2_surf
->current
= dri2_surf
->back
;
673 dri2_surf
->back
= NULL
;
675 create_wl_buffer(dri2_surf
);
677 wl_surface_attach(dri2_surf
->wl_win
->surface
,
678 dri2_surf
->current
->wl_buffer
,
679 dri2_surf
->dx
, dri2_surf
->dy
);
681 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
682 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
683 /* reset resize growing parameters */
688 wl_surface_damage(dri2_surf
->wl_win
->surface
,
689 0, 0, INT32_MAX
, INT32_MAX
);
691 for (i
= 0; i
< n_rects
; i
++) {
692 const int *rect
= &rects
[i
* 4];
693 wl_surface_damage(dri2_surf
->wl_win
->surface
,
695 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
700 if (dri2_dpy
->is_different_gpu
) {
701 _EGLContext
*ctx
= _eglGetCurrentContext();
702 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
703 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
704 dri2_surf
->current
->linear_copy
,
705 dri2_surf
->current
->dri_image
,
706 0, 0, dri2_surf
->base
.Width
,
707 dri2_surf
->base
.Height
,
708 0, 0, dri2_surf
->base
.Width
,
709 dri2_surf
->base
.Height
, 0);
712 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
713 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
715 wl_surface_commit(dri2_surf
->wl_win
->surface
);
717 /* If we're not waiting for a frame callback then we'll at least throttle
718 * to a sync callback so that we always give a chance for the compositor to
719 * handle the commit and send a release event before checking for a free
721 if (dri2_surf
->throttle_callback
== NULL
) {
722 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
723 wl_callback_add_listener(dri2_surf
->throttle_callback
,
724 &throttle_listener
, dri2_surf
);
725 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
729 wl_display_flush(dri2_dpy
->wl_dpy
);
735 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
736 _EGLDisplay
*disp
, _EGLSurface
*surface
)
738 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
740 if (get_back_bo(dri2_surf
) < 0) {
741 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
745 return dri2_surf
->back
->age
;
749 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
751 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
754 static struct wl_buffer
*
755 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
759 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
760 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
761 __DRIimage
*image
= dri2_img
->dri_image
;
762 struct wl_buffer
*buffer
;
763 int width
, height
, format
, pitch
;
764 enum wl_drm_format wl_format
;
766 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
769 case __DRI_IMAGE_FORMAT_ARGB8888
:
770 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
772 wl_format
= WL_DRM_FORMAT_ARGB8888
;
774 case __DRI_IMAGE_FORMAT_XRGB8888
:
775 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
777 wl_format
= WL_DRM_FORMAT_XRGB8888
;
783 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
784 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
785 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
787 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
790 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
793 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
805 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
808 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
815 /* The buffer object will have been created with our internal event queue
816 * because it is using the wl_drm object as a proxy factory. We want the
817 * buffer to be used by the application so we'll reset it to the display's
818 * default event queue */
820 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
825 _eglError(EGL_BAD_MATCH
, "unsupported image format");
830 is_fd_render_node(int fd
)
834 if (fstat(fd
, &render
))
837 if (!S_ISCHR(render
.st_mode
))
840 if (render
.st_rdev
& 0x80)
846 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
848 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
851 if (dri2_dpy
->is_render_node
) {
852 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
853 "authenticate for render-nodes");
856 dri2_dpy
->authenticated
= 0;
858 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
859 if (roundtrip(dri2_dpy
) < 0)
862 if (!dri2_dpy
->authenticated
)
865 /* reset authenticated */
866 dri2_dpy
->authenticated
= 1;
872 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
874 struct dri2_egl_display
*dri2_dpy
= data
;
877 dri2_dpy
->device_name
= strdup(device
);
878 if (!dri2_dpy
->device_name
)
882 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
883 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
886 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
887 if (dri2_dpy
->fd
!= -1)
888 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
891 if (dri2_dpy
->fd
== -1) {
892 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
893 dri2_dpy
->device_name
, strerror(errno
));
897 if (is_fd_render_node(dri2_dpy
->fd
)) {
898 dri2_dpy
->authenticated
= 1;
900 drmGetMagic(dri2_dpy
->fd
, &magic
);
901 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
906 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
908 struct dri2_egl_display
*dri2_dpy
= data
;
911 case WL_DRM_FORMAT_ARGB8888
:
912 dri2_dpy
->formats
|= HAS_ARGB8888
;
914 case WL_DRM_FORMAT_XRGB8888
:
915 dri2_dpy
->formats
|= HAS_XRGB8888
;
917 case WL_DRM_FORMAT_RGB565
:
918 dri2_dpy
->formats
|= HAS_RGB565
;
924 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
926 struct dri2_egl_display
*dri2_dpy
= data
;
928 dri2_dpy
->capabilities
= value
;
932 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
934 struct dri2_egl_display
*dri2_dpy
= data
;
936 dri2_dpy
->authenticated
= 1;
939 static const struct wl_drm_listener drm_listener
= {
942 drm_handle_authenticated
,
943 drm_handle_capabilities
947 registry_handle_global(void *data
, struct wl_registry
*registry
, uint32_t name
,
948 const char *interface
, uint32_t version
)
950 struct dri2_egl_display
*dri2_dpy
= data
;
954 if (strcmp(interface
, "wl_drm") == 0) {
956 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
957 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
962 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
967 static const struct wl_registry_listener registry_listener
= {
968 registry_handle_global
,
969 registry_handle_global_remove
973 dri2_wl_swap_interval(_EGLDriver
*drv
,
978 if (interval
> surf
->Config
->MaxSwapInterval
)
979 interval
= surf
->Config
->MaxSwapInterval
;
980 else if (interval
< surf
->Config
->MinSwapInterval
)
981 interval
= surf
->Config
->MinSwapInterval
;
983 surf
->SwapInterval
= interval
;
989 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
991 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
993 /* We can't use values greater than 1 on Wayland because we are using the
994 * frame callback to synchronise the frame and the only way we be sure to
995 * get a frame callback is to attach a new buffer. Therefore we can't just
996 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
998 if (dri2_dpy
->config
)
999 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1000 "vblank_mode", &vblank_mode
);
1001 switch (vblank_mode
) {
1002 case DRI_CONF_VBLANK_NEVER
:
1003 dri2_dpy
->min_swap_interval
= 0;
1004 dri2_dpy
->max_swap_interval
= 0;
1005 dri2_dpy
->default_swap_interval
= 0;
1007 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1008 dri2_dpy
->min_swap_interval
= 1;
1009 dri2_dpy
->max_swap_interval
= 1;
1010 dri2_dpy
->default_swap_interval
= 1;
1012 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1013 dri2_dpy
->min_swap_interval
= 0;
1014 dri2_dpy
->max_swap_interval
= 1;
1015 dri2_dpy
->default_swap_interval
= 0;
1018 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1019 dri2_dpy
->min_swap_interval
= 0;
1020 dri2_dpy
->max_swap_interval
= 1;
1021 dri2_dpy
->default_swap_interval
= 1;
1026 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1027 .authenticate
= dri2_wl_authenticate
,
1028 .create_window_surface
= dri2_wl_create_window_surface
,
1029 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1030 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1031 .destroy_surface
= dri2_wl_destroy_surface
,
1032 .create_image
= dri2_create_image_khr
,
1033 .swap_interval
= dri2_wl_swap_interval
,
1034 .swap_buffers
= dri2_wl_swap_buffers
,
1035 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1036 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1037 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1038 .copy_buffers
= dri2_fallback_copy_buffers
,
1039 .query_buffer_age
= dri2_wl_query_buffer_age
,
1040 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1041 .get_sync_values
= dri2_fallback_get_sync_values
,
1045 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1047 struct dri2_egl_display
*dri2_dpy
;
1048 const __DRIconfig
*config
;
1051 static const unsigned int argb_masks
[4] =
1052 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1053 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1054 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1056 loader_set_logger(_eglLog
);
1058 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1060 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1062 disp
->DriverData
= (void *) dri2_dpy
;
1063 if (disp
->PlatformDisplay
== NULL
) {
1064 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1065 if (dri2_dpy
->wl_dpy
== NULL
)
1067 dri2_dpy
->own_device
= 1;
1069 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1072 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1074 if (dri2_dpy
->own_device
)
1075 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1077 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1078 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1079 dri2_dpy
->wl_queue
);
1080 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1081 ®istry_listener
, dri2_dpy
);
1082 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1083 goto cleanup_registry
;
1085 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1088 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1091 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1092 &dri2_dpy
->is_different_gpu
);
1093 if (dri2_dpy
->is_different_gpu
) {
1094 free(dri2_dpy
->device_name
);
1095 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1096 if (!dri2_dpy
->device_name
) {
1097 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1098 "for requested GPU");
1103 /* we have to do the check now, because loader_get_user_preferred_fd
1104 * will return a render-node when the requested gpu is different
1105 * to the server, but also if the client asks for the same gpu than
1106 * the server by requesting its pci-id */
1107 dri2_dpy
->is_render_node
= is_fd_render_node(dri2_dpy
->fd
);
1109 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1110 if (dri2_dpy
->driver_name
== NULL
) {
1111 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1115 if (!dri2_load_driver(disp
))
1116 goto cleanup_driver_name
;
1118 dri2_dpy
->extensions
[0] = &image_loader_extension
.base
;
1119 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1120 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
1122 /* render nodes cannot use Gem names, and thus do not support
1123 * the __DRI_DRI2_LOADER extension */
1124 if (!dri2_dpy
->is_render_node
) {
1125 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1126 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1127 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1128 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1129 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1130 dri2_wl_get_buffers_with_format
;
1131 dri2_dpy
->extensions
[3] = &dri2_dpy
->dri2_loader_extension
.base
;
1132 dri2_dpy
->extensions
[4] = NULL
;
1134 dri2_dpy
->extensions
[3] = NULL
;
1136 dri2_dpy
->swap_available
= EGL_TRUE
;
1138 if (!dri2_create_screen(disp
))
1139 goto cleanup_driver
;
1141 dri2_wl_setup_swap_interval(dri2_dpy
);
1143 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1144 * createImageFromFds support indicates that Prime export/import
1145 * is supported by the driver. Fall back to
1146 * gem names if we don't have Prime support. */
1148 if (dri2_dpy
->image
->base
.version
< 7 ||
1149 dri2_dpy
->image
->createImageFromFds
== NULL
)
1150 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1152 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1153 * The server needs to accept them */
1154 if (dri2_dpy
->is_render_node
&&
1155 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1156 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1157 goto cleanup_screen
;
1160 if (dri2_dpy
->is_different_gpu
&&
1161 (dri2_dpy
->image
->base
.version
< 9 ||
1162 dri2_dpy
->image
->blitImage
== NULL
)) {
1163 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1164 "Image extension in the driver is not "
1165 "compatible. Version 9 or later and blitImage() "
1167 goto cleanup_screen
;
1170 types
= EGL_WINDOW_BIT
;
1171 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1172 config
= dri2_dpy
->driver_configs
[i
];
1173 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1174 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1175 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1176 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1177 if (dri2_dpy
->formats
& HAS_RGB565
)
1178 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1181 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1182 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1183 * because the buffer of the EGLImage has likely a tiling mode the server
1184 * gpu won't support. These is no way to check for now. Thus do not support the
1186 if (!dri2_dpy
->is_different_gpu
) {
1187 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1189 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1190 dri2_fallback_create_wayland_buffer_from_image
;
1192 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1194 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1196 /* we're supporting EGL 1.4 */
1197 disp
->VersionMajor
= 1;
1198 disp
->VersionMinor
= 4;
1200 /* Fill vtbl last to prevent accidentally calling virtual function during
1203 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1208 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1210 dlclose(dri2_dpy
->driver
);
1211 cleanup_driver_name
:
1212 free(dri2_dpy
->driver_name
);
1214 close(dri2_dpy
->fd
);
1216 free(dri2_dpy
->device_name
);
1217 wl_drm_destroy(dri2_dpy
->wl_drm
);
1219 wl_registry_destroy(dri2_dpy
->wl_registry
);
1220 wl_event_queue_destroy(dri2_dpy
->wl_queue
);