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
);
296 /* We always want to throttle to some event (either a frame callback or
297 * a sync request) after the commit so that we can be sure the
298 * compositor has had a chance to handle it and send us a release event
299 * before we look for a free buffer */
300 while (dri2_surf
->throttle_callback
!= NULL
)
301 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
302 dri2_dpy
->wl_queue
) == -1)
305 if (dri2_surf
->back
== NULL
) {
306 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
307 /* Get an unlocked buffer, preferrably one with a dri_buffer
308 * already allocated. */
309 if (dri2_surf
->color_buffers
[i
].locked
)
311 if (dri2_surf
->back
== NULL
)
312 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
313 else if (dri2_surf
->back
->dri_image
== NULL
)
314 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
318 if (dri2_surf
->back
== NULL
)
320 if (dri2_surf
->back
->dri_image
== NULL
) {
321 dri2_surf
->back
->dri_image
=
322 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
323 dri2_surf
->base
.Width
,
324 dri2_surf
->base
.Height
,
325 __DRI_IMAGE_FORMAT_ARGB8888
,
326 __DRI_IMAGE_USE_SHARE
,
328 dri2_surf
->back
->age
= 0;
330 if (dri2_surf
->back
->dri_image
== NULL
)
333 dri2_surf
->back
->locked
= 1;
340 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
342 struct dri2_egl_display
*dri2_dpy
=
343 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
347 image
= dri2_surf
->back
->dri_image
;
349 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
350 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
352 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
354 buffer
->pitch
= pitch
;
360 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
361 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
363 struct dri2_egl_display
*dri2_dpy
=
364 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
365 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
368 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
370 dri2_surf
->base
.Width
,
371 dri2_surf
->base
.Height
);
372 dri2_surf
->dri_buffers
[attachment
] = b
;
377 memcpy(buffer
, b
, sizeof *buffer
);
383 update_buffers(struct dri2_egl_surface
*dri2_surf
)
385 struct dri2_egl_display
*dri2_dpy
=
386 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
389 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&&
390 (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
391 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
)) {
393 dri2_wl_release_buffers(dri2_surf
);
395 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
396 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
397 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
398 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
401 if (get_back_bo(dri2_surf
) < 0) {
402 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
406 /* If we have an extra unlocked buffer at this point, we had to do triple
407 * buffering for a while, but now can go back to just double buffering.
408 * That means we can free any unlocked buffer now. */
409 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
410 if (!dri2_surf
->color_buffers
[i
].locked
&&
411 dri2_surf
->color_buffers
[i
].wl_buffer
) {
412 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
413 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
414 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
415 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
423 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
424 int *width
, int *height
,
425 unsigned int *attachments
, int count
,
426 int *out_count
, void *loaderPrivate
)
428 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
431 if (update_buffers(dri2_surf
) < 0)
434 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
435 switch (attachments
[i
]) {
436 case __DRI_BUFFER_BACK_LEFT
:
437 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
440 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
441 &dri2_surf
->buffers
[j
]) < 0) {
442 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
453 *width
= dri2_surf
->base
.Width
;
454 *height
= dri2_surf
->base
.Height
;
456 return dri2_surf
->buffers
;
460 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
461 int *width
, int *height
,
462 unsigned int *attachments
, int count
,
463 int *out_count
, void *loaderPrivate
)
465 unsigned int *attachments_with_format
;
467 const unsigned int format
= 32;
470 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
471 if (!attachments_with_format
) {
476 for (i
= 0; i
< count
; ++i
) {
477 attachments_with_format
[2*i
] = attachments
[i
];
478 attachments_with_format
[2*i
+ 1] = format
;
482 dri2_wl_get_buffers_with_format(driDrawable
,
484 attachments_with_format
, count
,
485 out_count
, loaderPrivate
);
487 free(attachments_with_format
);
493 image_get_buffers(__DRIdrawable
*driDrawable
,
497 uint32_t buffer_mask
,
498 struct __DRIimageList
*buffers
)
500 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
502 if (update_buffers(dri2_surf
) < 0)
505 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
506 buffers
->back
= dri2_surf
->back
->dri_image
;
512 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
515 (void) loaderPrivate
;
518 static const __DRIimageLoaderExtension image_loader_extension
= {
519 .base
= { __DRI_IMAGE_LOADER
, 1 },
521 .getBuffers
= image_get_buffers
,
522 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
526 wayland_throttle_callback(void *data
,
527 struct wl_callback
*callback
,
530 struct dri2_egl_surface
*dri2_surf
= data
;
532 dri2_surf
->throttle_callback
= NULL
;
533 wl_callback_destroy(callback
);
536 static const struct wl_callback_listener throttle_listener
= {
537 wayland_throttle_callback
541 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
543 struct dri2_egl_display
*dri2_dpy
=
544 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
545 int fd
, stride
, name
;
547 if (dri2_surf
->current
->wl_buffer
!= NULL
)
550 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
551 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
552 __DRI_IMAGE_ATTRIB_FD
, &fd
);
553 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
554 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
556 dri2_surf
->current
->wl_buffer
=
557 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
559 dri2_surf
->base
.Width
,
560 dri2_surf
->base
.Height
,
567 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
568 __DRI_IMAGE_ATTRIB_NAME
, &name
);
569 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
570 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
572 dri2_surf
->current
->wl_buffer
=
573 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
575 dri2_surf
->base
.Width
,
576 dri2_surf
->base
.Height
,
581 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
583 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
584 &wl_buffer_listener
, dri2_surf
);
588 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
591 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
597 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
598 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
599 struct dri2_egl_context
*dri2_ctx
;
603 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
604 if (dri2_surf
->color_buffers
[i
].age
> 0)
605 dri2_surf
->color_buffers
[i
].age
++;
607 /* Make sure we have a back buffer in case we're swapping without ever
609 if (get_back_bo(dri2_surf
) < 0) {
610 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
614 if (draw
->SwapInterval
> 0) {
615 dri2_surf
->throttle_callback
=
616 wl_surface_frame(dri2_surf
->wl_win
->surface
);
617 wl_callback_add_listener(dri2_surf
->throttle_callback
,
618 &throttle_listener
, dri2_surf
);
619 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
623 dri2_surf
->back
->age
= 1;
624 dri2_surf
->current
= dri2_surf
->back
;
625 dri2_surf
->back
= NULL
;
627 create_wl_buffer(dri2_surf
);
629 wl_surface_attach(dri2_surf
->wl_win
->surface
,
630 dri2_surf
->current
->wl_buffer
,
631 dri2_surf
->dx
, dri2_surf
->dy
);
633 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
634 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
635 /* reset resize growing parameters */
640 wl_surface_damage(dri2_surf
->wl_win
->surface
,
641 0, 0, INT32_MAX
, INT32_MAX
);
643 for (i
= 0; i
< n_rects
; i
++) {
644 const int *rect
= &rects
[i
* 4];
645 wl_surface_damage(dri2_surf
->wl_win
->surface
,
647 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
652 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
653 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
655 wl_surface_commit(dri2_surf
->wl_win
->surface
);
657 /* If we're not waiting for a frame callback then we'll at least throttle
658 * to a sync callback so that we always give a chance for the compositor to
659 * handle the commit and send a release event before checking for a free
661 if (dri2_surf
->throttle_callback
== NULL
) {
662 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
663 wl_callback_add_listener(dri2_surf
->throttle_callback
,
664 &throttle_listener
, dri2_surf
);
665 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
669 wl_display_flush(dri2_dpy
->wl_dpy
);
675 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
676 _EGLDisplay
*disp
, _EGLSurface
*surface
)
678 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
680 if (get_back_bo(dri2_surf
) < 0) {
681 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
685 return dri2_surf
->back
->age
;
689 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
691 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
694 static struct wl_buffer
*
695 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
699 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
700 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
701 __DRIimage
*image
= dri2_img
->dri_image
;
702 struct wl_buffer
*buffer
;
703 int width
, height
, format
, pitch
;
704 enum wl_drm_format wl_format
;
706 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
709 case __DRI_IMAGE_FORMAT_ARGB8888
:
710 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
712 wl_format
= WL_DRM_FORMAT_ARGB8888
;
714 case __DRI_IMAGE_FORMAT_XRGB8888
:
715 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
717 wl_format
= WL_DRM_FORMAT_XRGB8888
;
723 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
724 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
725 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
727 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
730 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
733 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
745 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
748 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
755 /* The buffer object will have been created with our internal event queue
756 * because it is using the wl_drm object as a proxy factory. We want the
757 * buffer to be used by the application so we'll reset it to the display's
758 * default event queue */
760 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
765 _eglError(EGL_BAD_MATCH
, "unsupported image format");
770 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
772 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
775 dri2_dpy
->authenticated
= 0;
777 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
778 if (roundtrip(dri2_dpy
) < 0)
781 if (!dri2_dpy
->authenticated
)
784 /* reset authenticated */
785 dri2_dpy
->authenticated
= 1;
791 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
793 struct dri2_egl_display
*dri2_dpy
= data
;
796 dri2_dpy
->device_name
= strdup(device
);
797 if (!dri2_dpy
->device_name
)
801 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
802 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
805 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
806 if (dri2_dpy
->fd
!= -1)
807 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
810 if (dri2_dpy
->fd
== -1) {
811 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
812 dri2_dpy
->device_name
, strerror(errno
));
816 drmGetMagic(dri2_dpy
->fd
, &magic
);
817 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
821 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
823 struct dri2_egl_display
*dri2_dpy
= data
;
826 case WL_DRM_FORMAT_ARGB8888
:
827 dri2_dpy
->formats
|= HAS_ARGB8888
;
829 case WL_DRM_FORMAT_XRGB8888
:
830 dri2_dpy
->formats
|= HAS_XRGB8888
;
832 case WL_DRM_FORMAT_RGB565
:
833 dri2_dpy
->formats
|= HAS_RGB565
;
839 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
841 struct dri2_egl_display
*dri2_dpy
= data
;
843 dri2_dpy
->capabilities
= value
;
847 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
849 struct dri2_egl_display
*dri2_dpy
= data
;
851 dri2_dpy
->authenticated
= 1;
854 static const struct wl_drm_listener drm_listener
= {
857 drm_handle_authenticated
,
858 drm_handle_capabilities
862 registry_handle_global(void *data
, struct wl_registry
*registry
, uint32_t name
,
863 const char *interface
, uint32_t version
)
865 struct dri2_egl_display
*dri2_dpy
= data
;
869 if (strcmp(interface
, "wl_drm") == 0) {
871 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
872 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
877 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
882 static const struct wl_registry_listener registry_listener
= {
883 registry_handle_global
,
884 registry_handle_global_remove
888 dri2_wl_swap_interval(_EGLDriver
*drv
,
893 if (interval
> surf
->Config
->MaxSwapInterval
)
894 interval
= surf
->Config
->MaxSwapInterval
;
895 else if (interval
< surf
->Config
->MinSwapInterval
)
896 interval
= surf
->Config
->MinSwapInterval
;
898 surf
->SwapInterval
= interval
;
904 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
906 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
908 /* We can't use values greater than 1 on Wayland because we are using the
909 * frame callback to synchronise the frame and the only way we be sure to
910 * get a frame callback is to attach a new buffer. Therefore we can't just
911 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
913 if (dri2_dpy
->config
)
914 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
915 "vblank_mode", &vblank_mode
);
916 switch (vblank_mode
) {
917 case DRI_CONF_VBLANK_NEVER
:
918 dri2_dpy
->min_swap_interval
= 0;
919 dri2_dpy
->max_swap_interval
= 0;
920 dri2_dpy
->default_swap_interval
= 0;
922 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
923 dri2_dpy
->min_swap_interval
= 1;
924 dri2_dpy
->max_swap_interval
= 1;
925 dri2_dpy
->default_swap_interval
= 1;
927 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
928 dri2_dpy
->min_swap_interval
= 0;
929 dri2_dpy
->max_swap_interval
= 1;
930 dri2_dpy
->default_swap_interval
= 0;
933 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
934 dri2_dpy
->min_swap_interval
= 0;
935 dri2_dpy
->max_swap_interval
= 1;
936 dri2_dpy
->default_swap_interval
= 1;
941 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
942 .authenticate
= dri2_wl_authenticate
,
943 .create_window_surface
= dri2_wl_create_window_surface
,
944 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
945 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
946 .destroy_surface
= dri2_wl_destroy_surface
,
947 .create_image
= dri2_create_image_khr
,
948 .swap_interval
= dri2_wl_swap_interval
,
949 .swap_buffers
= dri2_wl_swap_buffers
,
950 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
951 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
952 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
953 .copy_buffers
= dri2_fallback_copy_buffers
,
954 .query_buffer_age
= dri2_wl_query_buffer_age
,
955 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
956 .get_sync_values
= dri2_fallback_get_sync_values
,
960 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
962 struct dri2_egl_display
*dri2_dpy
;
963 const __DRIconfig
*config
;
966 static const unsigned int argb_masks
[4] =
967 { 0xff0000, 0xff00, 0xff, 0xff000000 };
968 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
969 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
971 loader_set_logger(_eglLog
);
973 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
975 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
977 disp
->DriverData
= (void *) dri2_dpy
;
978 if (disp
->PlatformDisplay
== NULL
) {
979 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
980 if (dri2_dpy
->wl_dpy
== NULL
)
982 dri2_dpy
->own_device
= 1;
984 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
987 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
989 if (dri2_dpy
->own_device
)
990 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
992 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
993 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
995 wl_registry_add_listener(dri2_dpy
->wl_registry
,
996 ®istry_listener
, dri2_dpy
);
997 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1000 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1003 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1006 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1007 if (dri2_dpy
->driver_name
== NULL
) {
1008 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1012 if (!dri2_load_driver(disp
))
1013 goto cleanup_driver_name
;
1015 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1016 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1017 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1018 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1019 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1020 dri2_wl_get_buffers_with_format
;
1022 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1023 dri2_dpy
->extensions
[1] = &image_loader_extension
.base
;
1024 dri2_dpy
->extensions
[2] = &image_lookup_extension
.base
;
1025 dri2_dpy
->extensions
[3] = &use_invalidate
.base
;
1026 dri2_dpy
->extensions
[4] = NULL
;
1028 dri2_dpy
->swap_available
= EGL_TRUE
;
1030 if (!dri2_create_screen(disp
))
1031 goto cleanup_driver
;
1033 dri2_wl_setup_swap_interval(dri2_dpy
);
1035 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
1036 * doesn't have createImageFromFds, since we're using the same driver on
1037 * both sides. We don't want crash if that happens anyway, so fall back to
1038 * gem names if we don't have prime support. */
1040 if (dri2_dpy
->image
->base
.version
< 7 ||
1041 dri2_dpy
->image
->createImageFromFds
== NULL
)
1042 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1044 types
= EGL_WINDOW_BIT
;
1045 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1046 config
= dri2_dpy
->driver_configs
[i
];
1047 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1048 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1049 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1050 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1051 if (dri2_dpy
->formats
& HAS_RGB565
)
1052 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1055 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1056 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1057 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1059 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1061 /* we're supporting EGL 1.4 */
1062 disp
->VersionMajor
= 1;
1063 disp
->VersionMinor
= 4;
1065 /* Fill vtbl last to prevent accidentally calling virtual function during
1068 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1073 dlclose(dri2_dpy
->driver
);
1074 cleanup_driver_name
:
1075 free(dri2_dpy
->driver_name
);
1077 close(dri2_dpy
->fd
);
1079 free(dri2_dpy
->device_name
);
1080 wl_drm_destroy(dri2_dpy
->wl_drm
);