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
= malloc(sizeof *dri2_surf
);
135 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
139 memset(dri2_surf
, 0, sizeof *dri2_surf
);
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
);
245 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
246 if (dri2_surf
->dri_buffers
[i
] &&
247 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
248 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
249 dri2_surf
->dri_buffers
[i
]);
251 if (dri2_surf
->throttle_callback
)
252 wl_callback_destroy(dri2_surf
->throttle_callback
);
254 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
255 dri2_surf
->wl_win
->private = NULL
;
256 dri2_surf
->wl_win
->resize_callback
= NULL
;
265 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
267 struct dri2_egl_display
*dri2_dpy
=
268 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
271 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
272 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
273 !dri2_surf
->color_buffers
[i
].locked
)
274 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
275 if (dri2_surf
->color_buffers
[i
].dri_image
)
276 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
278 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
279 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
280 dri2_surf
->color_buffers
[i
].locked
= 0;
283 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
284 if (dri2_surf
->dri_buffers
[i
] &&
285 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
286 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
287 dri2_surf
->dri_buffers
[i
]);
291 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
293 struct dri2_egl_display
*dri2_dpy
=
294 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
297 /* We always want to throttle to some event (either a frame callback or
298 * a sync request) after the commit so that we can be sure the
299 * compositor has had a chance to handle it and send us a release event
300 * before we look for a free buffer */
301 while (dri2_surf
->throttle_callback
!= NULL
)
302 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
303 dri2_dpy
->wl_queue
) == -1)
306 if (dri2_surf
->back
== NULL
) {
307 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
308 /* Get an unlocked buffer, preferrably one with a dri_buffer
309 * already allocated. */
310 if (dri2_surf
->color_buffers
[i
].locked
)
312 if (dri2_surf
->back
== NULL
)
313 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
314 else if (dri2_surf
->back
->dri_image
== NULL
)
315 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
319 if (dri2_surf
->back
== NULL
)
321 if (dri2_surf
->back
->dri_image
== NULL
) {
322 dri2_surf
->back
->dri_image
=
323 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
324 dri2_surf
->base
.Width
,
325 dri2_surf
->base
.Height
,
326 __DRI_IMAGE_FORMAT_ARGB8888
,
327 __DRI_IMAGE_USE_SHARE
,
329 dri2_surf
->back
->age
= 0;
331 if (dri2_surf
->back
->dri_image
== NULL
)
334 dri2_surf
->back
->locked
= 1;
341 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
343 struct dri2_egl_display
*dri2_dpy
=
344 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
348 image
= dri2_surf
->back
->dri_image
;
350 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
351 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
353 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
355 buffer
->pitch
= pitch
;
361 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
362 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
364 struct dri2_egl_display
*dri2_dpy
=
365 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
366 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
369 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
371 dri2_surf
->base
.Width
,
372 dri2_surf
->base
.Height
);
373 dri2_surf
->dri_buffers
[attachment
] = b
;
378 memcpy(buffer
, b
, sizeof *buffer
);
384 update_buffers(struct dri2_egl_surface
*dri2_surf
)
386 struct dri2_egl_display
*dri2_dpy
=
387 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
390 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&&
391 (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
392 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
)) {
394 dri2_wl_release_buffers(dri2_surf
);
396 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
397 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
398 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
399 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
402 if (get_back_bo(dri2_surf
) < 0) {
403 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
407 /* If we have an extra unlocked buffer at this point, we had to do triple
408 * buffering for a while, but now can go back to just double buffering.
409 * That means we can free any unlocked buffer now. */
410 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
411 if (!dri2_surf
->color_buffers
[i
].locked
&&
412 dri2_surf
->color_buffers
[i
].wl_buffer
) {
413 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
414 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
415 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
416 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
424 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
425 int *width
, int *height
,
426 unsigned int *attachments
, int count
,
427 int *out_count
, void *loaderPrivate
)
429 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
432 if (update_buffers(dri2_surf
) < 0)
435 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
436 switch (attachments
[i
]) {
437 case __DRI_BUFFER_BACK_LEFT
:
438 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
441 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
442 &dri2_surf
->buffers
[j
]) < 0) {
443 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
454 *width
= dri2_surf
->base
.Width
;
455 *height
= dri2_surf
->base
.Height
;
457 return dri2_surf
->buffers
;
461 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
462 int *width
, int *height
,
463 unsigned int *attachments
, int count
,
464 int *out_count
, void *loaderPrivate
)
466 unsigned int *attachments_with_format
;
468 const unsigned int format
= 32;
471 attachments_with_format
= calloc(count
* 2, sizeof(unsigned int));
472 if (!attachments_with_format
) {
477 for (i
= 0; i
< count
; ++i
) {
478 attachments_with_format
[2*i
] = attachments
[i
];
479 attachments_with_format
[2*i
+ 1] = format
;
483 dri2_wl_get_buffers_with_format(driDrawable
,
485 attachments_with_format
, count
,
486 out_count
, loaderPrivate
);
488 free(attachments_with_format
);
494 image_get_buffers(__DRIdrawable
*driDrawable
,
498 uint32_t buffer_mask
,
499 struct __DRIimageList
*buffers
)
501 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
503 if (update_buffers(dri2_surf
) < 0)
506 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
507 buffers
->back
= dri2_surf
->back
->dri_image
;
513 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
516 (void) loaderPrivate
;
519 static const __DRIimageLoaderExtension image_loader_extension
= {
520 .base
= { __DRI_IMAGE_LOADER
, 1 },
522 .getBuffers
= image_get_buffers
,
523 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
527 wayland_throttle_callback(void *data
,
528 struct wl_callback
*callback
,
531 struct dri2_egl_surface
*dri2_surf
= data
;
533 dri2_surf
->throttle_callback
= NULL
;
534 wl_callback_destroy(callback
);
537 static const struct wl_callback_listener throttle_listener
= {
538 wayland_throttle_callback
542 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
544 struct dri2_egl_display
*dri2_dpy
=
545 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
546 int fd
, stride
, name
;
548 if (dri2_surf
->current
->wl_buffer
!= NULL
)
551 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
552 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
553 __DRI_IMAGE_ATTRIB_FD
, &fd
);
554 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
555 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
557 dri2_surf
->current
->wl_buffer
=
558 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
560 dri2_surf
->base
.Width
,
561 dri2_surf
->base
.Height
,
568 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
569 __DRI_IMAGE_ATTRIB_NAME
, &name
);
570 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
571 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
573 dri2_surf
->current
->wl_buffer
=
574 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
576 dri2_surf
->base
.Width
,
577 dri2_surf
->base
.Height
,
582 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
584 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
585 &wl_buffer_listener
, dri2_surf
);
589 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
592 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
598 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
599 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
600 struct dri2_egl_context
*dri2_ctx
;
604 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
605 if (dri2_surf
->color_buffers
[i
].age
> 0)
606 dri2_surf
->color_buffers
[i
].age
++;
608 /* Make sure we have a back buffer in case we're swapping without ever
610 if (get_back_bo(dri2_surf
) < 0) {
611 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
615 if (draw
->SwapInterval
> 0) {
616 dri2_surf
->throttle_callback
=
617 wl_surface_frame(dri2_surf
->wl_win
->surface
);
618 wl_callback_add_listener(dri2_surf
->throttle_callback
,
619 &throttle_listener
, dri2_surf
);
620 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
624 dri2_surf
->back
->age
= 1;
625 dri2_surf
->current
= dri2_surf
->back
;
626 dri2_surf
->back
= NULL
;
628 create_wl_buffer(dri2_surf
);
630 wl_surface_attach(dri2_surf
->wl_win
->surface
,
631 dri2_surf
->current
->wl_buffer
,
632 dri2_surf
->dx
, dri2_surf
->dy
);
634 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
635 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
636 /* reset resize growing parameters */
641 wl_surface_damage(dri2_surf
->wl_win
->surface
,
642 0, 0, INT32_MAX
, INT32_MAX
);
644 for (i
= 0; i
< n_rects
; i
++) {
645 const int *rect
= &rects
[i
* 4];
646 wl_surface_damage(dri2_surf
->wl_win
->surface
,
648 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
653 if (dri2_dpy
->flush
->base
.version
>= 4) {
654 ctx
= _eglGetCurrentContext();
655 dri2_ctx
= dri2_egl_context(ctx
);
656 (*dri2_dpy
->flush
->flush_with_flags
)(dri2_ctx
->dri_context
,
657 dri2_surf
->dri_drawable
,
658 __DRI2_FLUSH_DRAWABLE
,
659 __DRI2_THROTTLE_SWAPBUFFER
);
661 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
664 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
666 wl_surface_commit(dri2_surf
->wl_win
->surface
);
668 /* If we're not waiting for a frame callback then we'll at least throttle
669 * to a sync callback so that we always give a chance for the compositor to
670 * handle the commit and send a release event before checking for a free
672 if (dri2_surf
->throttle_callback
== NULL
) {
673 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
674 wl_callback_add_listener(dri2_surf
->throttle_callback
,
675 &throttle_listener
, dri2_surf
);
676 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
680 wl_display_flush(dri2_dpy
->wl_dpy
);
686 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
687 _EGLDisplay
*disp
, _EGLSurface
*surface
)
689 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
691 if (get_back_bo(dri2_surf
) < 0) {
692 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
696 return dri2_surf
->back
->age
;
700 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
702 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
705 static struct wl_buffer
*
706 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
710 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
711 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
712 __DRIimage
*image
= dri2_img
->dri_image
;
713 struct wl_buffer
*buffer
;
714 int width
, height
, format
, pitch
;
715 enum wl_drm_format wl_format
;
717 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
720 case __DRI_IMAGE_FORMAT_ARGB8888
:
721 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
723 wl_format
= WL_DRM_FORMAT_ARGB8888
;
725 case __DRI_IMAGE_FORMAT_XRGB8888
:
726 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
728 wl_format
= WL_DRM_FORMAT_XRGB8888
;
734 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
735 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
736 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
738 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
741 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
744 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
756 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
759 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
766 /* The buffer object will have been created with our internal event queue
767 * because it is using the wl_drm object as a proxy factory. We want the
768 * buffer to be used by the application so we'll reset it to the display's
769 * default event queue */
771 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
776 _eglError(EGL_BAD_MATCH
, "unsupported image format");
781 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
783 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
786 dri2_dpy
->authenticated
= 0;
788 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
789 if (roundtrip(dri2_dpy
) < 0)
792 if (!dri2_dpy
->authenticated
)
795 /* reset authenticated */
796 dri2_dpy
->authenticated
= 1;
802 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
804 struct dri2_egl_display
*dri2_dpy
= data
;
807 dri2_dpy
->device_name
= strdup(device
);
808 if (!dri2_dpy
->device_name
)
812 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
813 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
816 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
817 if (dri2_dpy
->fd
!= -1)
818 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
821 if (dri2_dpy
->fd
== -1) {
822 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
823 dri2_dpy
->device_name
, strerror(errno
));
827 drmGetMagic(dri2_dpy
->fd
, &magic
);
828 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
832 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
834 struct dri2_egl_display
*dri2_dpy
= data
;
837 case WL_DRM_FORMAT_ARGB8888
:
838 dri2_dpy
->formats
|= HAS_ARGB8888
;
840 case WL_DRM_FORMAT_XRGB8888
:
841 dri2_dpy
->formats
|= HAS_XRGB8888
;
843 case WL_DRM_FORMAT_RGB565
:
844 dri2_dpy
->formats
|= HAS_RGB565
;
850 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
852 struct dri2_egl_display
*dri2_dpy
= data
;
854 dri2_dpy
->capabilities
= value
;
858 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
860 struct dri2_egl_display
*dri2_dpy
= data
;
862 dri2_dpy
->authenticated
= 1;
865 static const struct wl_drm_listener drm_listener
= {
868 drm_handle_authenticated
,
869 drm_handle_capabilities
873 registry_handle_global(void *data
, struct wl_registry
*registry
, uint32_t name
,
874 const char *interface
, uint32_t version
)
876 struct dri2_egl_display
*dri2_dpy
= data
;
880 if (strcmp(interface
, "wl_drm") == 0) {
882 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
883 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
888 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
893 static const struct wl_registry_listener registry_listener
= {
894 registry_handle_global
,
895 registry_handle_global_remove
899 dri2_wl_swap_interval(_EGLDriver
*drv
,
904 if (interval
> surf
->Config
->MaxSwapInterval
)
905 interval
= surf
->Config
->MaxSwapInterval
;
906 else if (interval
< surf
->Config
->MinSwapInterval
)
907 interval
= surf
->Config
->MinSwapInterval
;
909 surf
->SwapInterval
= interval
;
915 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
917 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
919 /* We can't use values greater than 1 on Wayland because we are using the
920 * frame callback to synchronise the frame and the only way we be sure to
921 * get a frame callback is to attach a new buffer. Therefore we can't just
922 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
924 if (dri2_dpy
->config
)
925 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
926 "vblank_mode", &vblank_mode
);
927 switch (vblank_mode
) {
928 case DRI_CONF_VBLANK_NEVER
:
929 dri2_dpy
->min_swap_interval
= 0;
930 dri2_dpy
->max_swap_interval
= 0;
931 dri2_dpy
->default_swap_interval
= 0;
933 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
934 dri2_dpy
->min_swap_interval
= 1;
935 dri2_dpy
->max_swap_interval
= 1;
936 dri2_dpy
->default_swap_interval
= 1;
938 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
939 dri2_dpy
->min_swap_interval
= 0;
940 dri2_dpy
->max_swap_interval
= 1;
941 dri2_dpy
->default_swap_interval
= 0;
944 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
945 dri2_dpy
->min_swap_interval
= 0;
946 dri2_dpy
->max_swap_interval
= 1;
947 dri2_dpy
->default_swap_interval
= 1;
952 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
953 .authenticate
= dri2_wl_authenticate
,
954 .create_window_surface
= dri2_wl_create_window_surface
,
955 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
956 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
957 .destroy_surface
= dri2_wl_destroy_surface
,
958 .create_image
= dri2_create_image_khr
,
959 .swap_interval
= dri2_wl_swap_interval
,
960 .swap_buffers
= dri2_wl_swap_buffers
,
961 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
962 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
963 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
964 .copy_buffers
= dri2_fallback_copy_buffers
,
965 .query_buffer_age
= dri2_wl_query_buffer_age
,
966 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
970 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
972 struct dri2_egl_display
*dri2_dpy
;
973 const __DRIconfig
*config
;
976 static const unsigned int argb_masks
[4] =
977 { 0xff0000, 0xff00, 0xff, 0xff000000 };
978 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
979 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
981 loader_set_logger(_eglLog
);
983 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
985 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
987 disp
->DriverData
= (void *) dri2_dpy
;
988 if (disp
->PlatformDisplay
== NULL
) {
989 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
990 if (dri2_dpy
->wl_dpy
== NULL
)
992 dri2_dpy
->own_device
= 1;
994 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
997 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
999 if (dri2_dpy
->own_device
)
1000 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1002 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1003 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1004 dri2_dpy
->wl_queue
);
1005 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1006 ®istry_listener
, dri2_dpy
);
1007 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1010 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1013 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1016 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1017 if (dri2_dpy
->driver_name
== NULL
) {
1018 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1022 if (!dri2_load_driver(disp
))
1023 goto cleanup_driver_name
;
1025 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1026 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1027 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1028 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1029 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1030 dri2_wl_get_buffers_with_format
;
1032 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1033 dri2_dpy
->extensions
[1] = &image_loader_extension
.base
;
1034 dri2_dpy
->extensions
[2] = &image_lookup_extension
.base
;
1035 dri2_dpy
->extensions
[3] = &use_invalidate
.base
;
1036 dri2_dpy
->extensions
[4] = NULL
;
1038 dri2_dpy
->swap_available
= EGL_TRUE
;
1040 if (!dri2_create_screen(disp
))
1041 goto cleanup_driver
;
1043 dri2_wl_setup_swap_interval(dri2_dpy
);
1045 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
1046 * doesn't have createImageFromFds, since we're using the same driver on
1047 * both sides. We don't want crash if that happens anyway, so fall back to
1048 * gem names if we don't have prime support. */
1050 if (dri2_dpy
->image
->base
.version
< 7 ||
1051 dri2_dpy
->image
->createImageFromFds
== NULL
)
1052 dri2_dpy
->capabilities
&= WL_DRM_CAPABILITY_PRIME
;
1054 types
= EGL_WINDOW_BIT
;
1055 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1056 config
= dri2_dpy
->driver_configs
[i
];
1057 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1058 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1059 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1060 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1061 if (dri2_dpy
->formats
& HAS_RGB565
)
1062 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1065 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1066 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1067 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1069 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1071 /* we're supporting EGL 1.4 */
1072 disp
->VersionMajor
= 1;
1073 disp
->VersionMinor
= 4;
1075 /* Fill vtbl last to prevent accidentally calling virtual function during
1078 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1083 dlclose(dri2_dpy
->driver
);
1084 cleanup_driver_name
:
1085 free(dri2_dpy
->driver_name
);
1087 close(dri2_dpy
->fd
);
1089 free(dri2_dpy
->device_name
);
1090 wl_drm_destroy(dri2_dpy
->wl_drm
);