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>
39 #include "egl_dri2_fallbacks.h"
42 #include <wayland-client.h>
43 #include "wayland-drm-client-protocol.h"
45 enum wl_drm_format_flags
{
52 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
56 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
61 wl_callback_destroy(callback
);
64 static const struct wl_callback_listener sync_listener
= {
69 roundtrip(struct dri2_egl_display
*dri2_dpy
)
71 struct wl_callback
*callback
;
72 int done
= 0, ret
= 0;
74 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
75 wl_callback_add_listener(callback
, &sync_listener
, &done
);
76 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
77 while (ret
!= -1 && !done
)
78 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
81 wl_callback_destroy(callback
);
87 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
89 struct dri2_egl_surface
*dri2_surf
= data
;
92 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
93 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
96 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
97 wl_buffer_destroy(buffer
);
101 dri2_surf
->color_buffers
[i
].locked
= 0;
104 static struct wl_buffer_listener wl_buffer_listener
= {
109 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
111 struct dri2_egl_surface
*dri2_surf
= data
;
112 struct dri2_egl_display
*dri2_dpy
=
113 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
115 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
119 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
122 dri2_wl_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
123 _EGLConfig
*conf
, void *native_window
,
124 const EGLint
*attrib_list
)
126 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
127 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
128 struct wl_egl_window
*window
= native_window
;
129 struct dri2_egl_surface
*dri2_surf
;
133 dri2_surf
= calloc(1, sizeof *dri2_surf
);
135 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
139 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
142 if (conf
->RedSize
== 5)
143 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
144 else if (conf
->AlphaSize
== 0)
145 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
147 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
151 dri2_surf
->wl_win
= window
;
153 dri2_surf
->wl_win
->private = dri2_surf
;
154 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
156 dri2_surf
->base
.Width
= -1;
157 dri2_surf
->base
.Height
= -1;
163 dri2_surf
->dri_drawable
=
164 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
165 type
== EGL_WINDOW_BIT
?
166 dri2_conf
->dri_double_config
:
167 dri2_conf
->dri_single_config
,
169 if (dri2_surf
->dri_drawable
== NULL
) {
170 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
171 goto cleanup_dri_drawable
;
174 return &dri2_surf
->base
;
176 cleanup_dri_drawable
:
177 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
185 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
188 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
189 _EGLConfig
*conf
, void *native_window
,
190 const EGLint
*attrib_list
)
192 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
195 surf
= dri2_wl_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
196 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
);
244 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
245 if (dri2_surf
->dri_buffers
[i
] &&
246 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
247 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
248 dri2_surf
->dri_buffers
[i
]);
250 if (dri2_surf
->throttle_callback
)
251 wl_callback_destroy(dri2_surf
->throttle_callback
);
253 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
254 dri2_surf
->wl_win
->private = NULL
;
255 dri2_surf
->wl_win
->resize_callback
= NULL
;
264 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
266 struct dri2_egl_display
*dri2_dpy
=
267 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
270 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
271 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
272 !dri2_surf
->color_buffers
[i
].locked
)
273 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
274 if (dri2_surf
->color_buffers
[i
].dri_image
)
275 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
277 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
278 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
279 dri2_surf
->color_buffers
[i
].locked
= 0;
282 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
283 if (dri2_surf
->dri_buffers
[i
] &&
284 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
285 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
286 dri2_surf
->dri_buffers
[i
]);
290 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
292 struct dri2_egl_display
*dri2_dpy
=
293 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
295 unsigned int dri_image_format
;
297 /* currently supports three WL DRM formats,
298 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
299 * and WL_DRM_FORMAT_RGB565
301 switch (dri2_surf
->format
) {
302 case WL_DRM_FORMAT_ARGB8888
:
303 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
305 case WL_DRM_FORMAT_XRGB8888
:
306 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
308 case WL_DRM_FORMAT_RGB565
:
309 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
312 /* format is not supported */
316 /* We always want to throttle to some event (either a frame callback or
317 * a sync request) after the commit so that we can be sure the
318 * compositor has had a chance to handle it and send us a release event
319 * before we look for a free buffer */
320 while (dri2_surf
->throttle_callback
!= NULL
)
321 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
322 dri2_dpy
->wl_queue
) == -1)
325 if (dri2_surf
->back
== NULL
) {
326 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
327 /* Get an unlocked buffer, preferrably one with a dri_buffer
328 * already allocated. */
329 if (dri2_surf
->color_buffers
[i
].locked
)
331 if (dri2_surf
->back
== NULL
)
332 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
333 else if (dri2_surf
->back
->dri_image
== NULL
)
334 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
338 if (dri2_surf
->back
== NULL
)
340 if (dri2_surf
->back
->dri_image
== NULL
) {
341 dri2_surf
->back
->dri_image
=
342 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
343 dri2_surf
->base
.Width
,
344 dri2_surf
->base
.Height
,
346 __DRI_IMAGE_USE_SHARE
,
348 dri2_surf
->back
->age
= 0;
350 if (dri2_surf
->back
->dri_image
== NULL
)
353 dri2_surf
->back
->locked
= 1;
360 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
362 struct dri2_egl_display
*dri2_dpy
=
363 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
367 image
= dri2_surf
->back
->dri_image
;
369 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
370 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
372 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
374 buffer
->pitch
= pitch
;
380 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
381 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
383 struct dri2_egl_display
*dri2_dpy
=
384 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
385 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
388 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
390 dri2_surf
->base
.Width
,
391 dri2_surf
->base
.Height
);
392 dri2_surf
->dri_buffers
[attachment
] = b
;
397 memcpy(buffer
, b
, sizeof *buffer
);
403 update_buffers(struct dri2_egl_surface
*dri2_surf
)
405 struct dri2_egl_display
*dri2_dpy
=
406 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
409 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&&
410 (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
411 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
)) {
413 dri2_wl_release_buffers(dri2_surf
);
415 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
416 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
417 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
418 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
421 if (get_back_bo(dri2_surf
) < 0) {
422 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
426 /* If we have an extra unlocked buffer at this point, we had to do triple
427 * buffering for a while, but now can go back to just double buffering.
428 * That means we can free any unlocked buffer now. */
429 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
430 if (!dri2_surf
->color_buffers
[i
].locked
&&
431 dri2_surf
->color_buffers
[i
].wl_buffer
) {
432 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
433 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
434 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
435 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
443 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
444 int *width
, int *height
,
445 unsigned int *attachments
, int count
,
446 int *out_count
, void *loaderPrivate
)
448 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
451 if (update_buffers(dri2_surf
) < 0)
454 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
455 switch (attachments
[i
]) {
456 case __DRI_BUFFER_BACK_LEFT
:
457 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
460 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
461 &dri2_surf
->buffers
[j
]) < 0) {
462 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
473 *width
= dri2_surf
->base
.Width
;
474 *height
= dri2_surf
->base
.Height
;
476 return dri2_surf
->buffers
;
480 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
481 int *width
, int *height
,
482 unsigned int *attachments
, int count
,
483 int *out_count
, void *loaderPrivate
)
485 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
486 unsigned int *attachments_with_format
;
492 switch (dri2_surf
->format
) {
493 case WL_DRM_FORMAT_ARGB8888
:
494 case WL_DRM_FORMAT_XRGB8888
:
497 case WL_DRM_FORMAT_RGB565
:
501 /* format is not supported */
505 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
506 if (!attachments_with_format
) {
511 for (i
= 0; i
< count
; ++i
) {
512 attachments_with_format
[2*i
] = attachments
[i
];
513 attachments_with_format
[2*i
+ 1] = bpp
;
517 dri2_wl_get_buffers_with_format(driDrawable
,
519 attachments_with_format
, count
,
520 out_count
, loaderPrivate
);
522 free(attachments_with_format
);
528 image_get_buffers(__DRIdrawable
*driDrawable
,
532 uint32_t buffer_mask
,
533 struct __DRIimageList
*buffers
)
535 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
537 if (update_buffers(dri2_surf
) < 0)
540 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
541 buffers
->back
= dri2_surf
->back
->dri_image
;
547 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
550 (void) loaderPrivate
;
553 static const __DRIimageLoaderExtension image_loader_extension
= {
554 .base
= { __DRI_IMAGE_LOADER
, 1 },
556 .getBuffers
= image_get_buffers
,
557 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
561 wayland_throttle_callback(void *data
,
562 struct wl_callback
*callback
,
565 struct dri2_egl_surface
*dri2_surf
= data
;
567 dri2_surf
->throttle_callback
= NULL
;
568 wl_callback_destroy(callback
);
571 static const struct wl_callback_listener throttle_listener
= {
572 wayland_throttle_callback
576 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
578 struct dri2_egl_display
*dri2_dpy
=
579 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
580 int fd
, stride
, name
;
582 if (dri2_surf
->current
->wl_buffer
!= NULL
)
585 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
586 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
587 __DRI_IMAGE_ATTRIB_FD
, &fd
);
588 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
589 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
591 dri2_surf
->current
->wl_buffer
=
592 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
594 dri2_surf
->base
.Width
,
595 dri2_surf
->base
.Height
,
602 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
603 __DRI_IMAGE_ATTRIB_NAME
, &name
);
604 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
605 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
607 dri2_surf
->current
->wl_buffer
=
608 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
610 dri2_surf
->base
.Width
,
611 dri2_surf
->base
.Height
,
616 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
618 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
619 &wl_buffer_listener
, dri2_surf
);
623 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
626 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
632 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
633 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
636 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
637 if (dri2_surf
->color_buffers
[i
].age
> 0)
638 dri2_surf
->color_buffers
[i
].age
++;
640 /* Make sure we have a back buffer in case we're swapping without ever
642 if (get_back_bo(dri2_surf
) < 0) {
643 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
647 if (draw
->SwapInterval
> 0) {
648 dri2_surf
->throttle_callback
=
649 wl_surface_frame(dri2_surf
->wl_win
->surface
);
650 wl_callback_add_listener(dri2_surf
->throttle_callback
,
651 &throttle_listener
, dri2_surf
);
652 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
656 dri2_surf
->back
->age
= 1;
657 dri2_surf
->current
= dri2_surf
->back
;
658 dri2_surf
->back
= NULL
;
660 create_wl_buffer(dri2_surf
);
662 wl_surface_attach(dri2_surf
->wl_win
->surface
,
663 dri2_surf
->current
->wl_buffer
,
664 dri2_surf
->dx
, dri2_surf
->dy
);
666 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
667 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
668 /* reset resize growing parameters */
673 wl_surface_damage(dri2_surf
->wl_win
->surface
,
674 0, 0, INT32_MAX
, INT32_MAX
);
676 for (i
= 0; i
< n_rects
; i
++) {
677 const int *rect
= &rects
[i
* 4];
678 wl_surface_damage(dri2_surf
->wl_win
->surface
,
680 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
685 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
686 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
688 wl_surface_commit(dri2_surf
->wl_win
->surface
);
690 /* If we're not waiting for a frame callback then we'll at least throttle
691 * to a sync callback so that we always give a chance for the compositor to
692 * handle the commit and send a release event before checking for a free
694 if (dri2_surf
->throttle_callback
== NULL
) {
695 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
696 wl_callback_add_listener(dri2_surf
->throttle_callback
,
697 &throttle_listener
, dri2_surf
);
698 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
702 wl_display_flush(dri2_dpy
->wl_dpy
);
708 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
709 _EGLDisplay
*disp
, _EGLSurface
*surface
)
711 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
713 if (get_back_bo(dri2_surf
) < 0) {
714 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
718 return dri2_surf
->back
->age
;
722 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
724 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
727 static struct wl_buffer
*
728 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
732 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
733 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
734 __DRIimage
*image
= dri2_img
->dri_image
;
735 struct wl_buffer
*buffer
;
736 int width
, height
, format
, pitch
;
737 enum wl_drm_format wl_format
;
739 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
742 case __DRI_IMAGE_FORMAT_ARGB8888
:
743 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
745 wl_format
= WL_DRM_FORMAT_ARGB8888
;
747 case __DRI_IMAGE_FORMAT_XRGB8888
:
748 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
750 wl_format
= WL_DRM_FORMAT_XRGB8888
;
756 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
757 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
758 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
760 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
763 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
766 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
778 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
781 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
788 /* The buffer object will have been created with our internal event queue
789 * because it is using the wl_drm object as a proxy factory. We want the
790 * buffer to be used by the application so we'll reset it to the display's
791 * default event queue */
793 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
798 _eglError(EGL_BAD_MATCH
, "unsupported image format");
803 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
805 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
808 dri2_dpy
->authenticated
= 0;
810 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
811 if (roundtrip(dri2_dpy
) < 0)
814 if (!dri2_dpy
->authenticated
)
817 /* reset authenticated */
818 dri2_dpy
->authenticated
= 1;
824 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
826 struct dri2_egl_display
*dri2_dpy
= data
;
829 dri2_dpy
->device_name
= strdup(device
);
830 if (!dri2_dpy
->device_name
)
834 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
835 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
838 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
839 if (dri2_dpy
->fd
!= -1)
840 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
843 if (dri2_dpy
->fd
== -1) {
844 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
845 dri2_dpy
->device_name
, strerror(errno
));
849 drmGetMagic(dri2_dpy
->fd
, &magic
);
850 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
854 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
856 struct dri2_egl_display
*dri2_dpy
= data
;
859 case WL_DRM_FORMAT_ARGB8888
:
860 dri2_dpy
->formats
|= HAS_ARGB8888
;
862 case WL_DRM_FORMAT_XRGB8888
:
863 dri2_dpy
->formats
|= HAS_XRGB8888
;
865 case WL_DRM_FORMAT_RGB565
:
866 dri2_dpy
->formats
|= HAS_RGB565
;
872 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
874 struct dri2_egl_display
*dri2_dpy
= data
;
876 dri2_dpy
->capabilities
= value
;
880 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
882 struct dri2_egl_display
*dri2_dpy
= data
;
884 dri2_dpy
->authenticated
= 1;
887 static const struct wl_drm_listener drm_listener
= {
890 drm_handle_authenticated
,
891 drm_handle_capabilities
895 registry_handle_global(void *data
, struct wl_registry
*registry
, uint32_t name
,
896 const char *interface
, uint32_t version
)
898 struct dri2_egl_display
*dri2_dpy
= data
;
902 if (strcmp(interface
, "wl_drm") == 0) {
904 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
905 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
910 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
915 static const struct wl_registry_listener registry_listener
= {
916 registry_handle_global
,
917 registry_handle_global_remove
921 dri2_wl_swap_interval(_EGLDriver
*drv
,
926 if (interval
> surf
->Config
->MaxSwapInterval
)
927 interval
= surf
->Config
->MaxSwapInterval
;
928 else if (interval
< surf
->Config
->MinSwapInterval
)
929 interval
= surf
->Config
->MinSwapInterval
;
931 surf
->SwapInterval
= interval
;
937 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
939 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
941 /* We can't use values greater than 1 on Wayland because we are using the
942 * frame callback to synchronise the frame and the only way we be sure to
943 * get a frame callback is to attach a new buffer. Therefore we can't just
944 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
946 if (dri2_dpy
->config
)
947 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
948 "vblank_mode", &vblank_mode
);
949 switch (vblank_mode
) {
950 case DRI_CONF_VBLANK_NEVER
:
951 dri2_dpy
->min_swap_interval
= 0;
952 dri2_dpy
->max_swap_interval
= 0;
953 dri2_dpy
->default_swap_interval
= 0;
955 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
956 dri2_dpy
->min_swap_interval
= 1;
957 dri2_dpy
->max_swap_interval
= 1;
958 dri2_dpy
->default_swap_interval
= 1;
960 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
961 dri2_dpy
->min_swap_interval
= 0;
962 dri2_dpy
->max_swap_interval
= 1;
963 dri2_dpy
->default_swap_interval
= 0;
966 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
967 dri2_dpy
->min_swap_interval
= 0;
968 dri2_dpy
->max_swap_interval
= 1;
969 dri2_dpy
->default_swap_interval
= 1;
974 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
975 .authenticate
= dri2_wl_authenticate
,
976 .create_window_surface
= dri2_wl_create_window_surface
,
977 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
978 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
979 .destroy_surface
= dri2_wl_destroy_surface
,
980 .create_image
= dri2_create_image_khr
,
981 .swap_interval
= dri2_wl_swap_interval
,
982 .swap_buffers
= dri2_wl_swap_buffers
,
983 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
984 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
985 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
986 .copy_buffers
= dri2_fallback_copy_buffers
,
987 .query_buffer_age
= dri2_wl_query_buffer_age
,
988 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
989 .get_sync_values
= dri2_fallback_get_sync_values
,
993 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
995 struct dri2_egl_display
*dri2_dpy
;
996 const __DRIconfig
*config
;
999 static const unsigned int argb_masks
[4] =
1000 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1001 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1002 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1004 loader_set_logger(_eglLog
);
1006 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1008 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1010 disp
->DriverData
= (void *) dri2_dpy
;
1011 if (disp
->PlatformDisplay
== NULL
) {
1012 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1013 if (dri2_dpy
->wl_dpy
== NULL
)
1015 dri2_dpy
->own_device
= 1;
1017 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1020 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1022 if (dri2_dpy
->own_device
)
1023 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1025 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1026 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1027 dri2_dpy
->wl_queue
);
1028 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1029 ®istry_listener
, dri2_dpy
);
1030 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1033 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1036 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1039 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1040 if (dri2_dpy
->driver_name
== NULL
) {
1041 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1045 if (!dri2_load_driver(disp
))
1046 goto cleanup_driver_name
;
1048 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1049 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1050 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1051 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1052 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1053 dri2_wl_get_buffers_with_format
;
1055 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1056 dri2_dpy
->extensions
[1] = &image_loader_extension
.base
;
1057 dri2_dpy
->extensions
[2] = &image_lookup_extension
.base
;
1058 dri2_dpy
->extensions
[3] = &use_invalidate
.base
;
1059 dri2_dpy
->extensions
[4] = NULL
;
1061 dri2_dpy
->swap_available
= EGL_TRUE
;
1063 if (!dri2_create_screen(disp
))
1064 goto cleanup_driver
;
1066 dri2_wl_setup_swap_interval(dri2_dpy
);
1068 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
1069 * doesn't have createImageFromFds, since we're using the same driver on
1070 * both sides. We don't want crash if that happens anyway, so fall back to
1071 * gem names if we don't have prime support. */
1073 if (dri2_dpy
->image
->base
.version
< 7 ||
1074 dri2_dpy
->image
->createImageFromFds
== NULL
)
1075 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1077 types
= EGL_WINDOW_BIT
;
1078 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1079 config
= dri2_dpy
->driver_configs
[i
];
1080 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1081 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1082 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1083 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1084 if (dri2_dpy
->formats
& HAS_RGB565
)
1085 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1088 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1089 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1090 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1092 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1094 /* we're supporting EGL 1.4 */
1095 disp
->VersionMajor
= 1;
1096 disp
->VersionMinor
= 4;
1098 /* Fill vtbl last to prevent accidentally calling virtual function during
1101 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1106 dlclose(dri2_dpy
->driver
);
1107 cleanup_driver_name
:
1108 free(dri2_dpy
->driver_name
);
1110 close(dri2_dpy
->fd
);
1112 free(dri2_dpy
->device_name
);
1113 wl_drm_destroy(dri2_dpy
->wl_drm
);