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 <wayland-client.h>
41 #include "wayland-drm-client-protocol.h"
43 enum wl_drm_format_flags
{
50 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
55 wl_callback_destroy(callback
);
58 static const struct wl_callback_listener sync_listener
= {
63 roundtrip(struct dri2_egl_display
*dri2_dpy
)
65 struct wl_callback
*callback
;
66 int done
= 0, ret
= 0;
68 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
69 wl_callback_add_listener(callback
, &sync_listener
, &done
);
70 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
71 while (ret
!= -1 && !done
)
72 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
75 wl_callback_destroy(callback
);
81 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
83 struct dri2_egl_surface
*dri2_surf
= data
;
86 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
87 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
90 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
91 wl_buffer_destroy(buffer
);
95 dri2_surf
->color_buffers
[i
].locked
= 0;
98 static struct wl_buffer_listener wl_buffer_listener
= {
103 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
105 struct dri2_egl_surface
*dri2_surf
= data
;
106 struct dri2_egl_display
*dri2_dpy
=
107 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
109 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
113 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
116 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
117 _EGLConfig
*conf
, EGLNativeWindowType window
,
118 const EGLint
*attrib_list
)
120 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
121 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
122 struct dri2_egl_surface
*dri2_surf
;
126 dri2_surf
= malloc(sizeof *dri2_surf
);
128 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
132 memset(dri2_surf
, 0, sizeof *dri2_surf
);
133 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
136 if (conf
->RedSize
== 5)
137 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
138 else if (conf
->AlphaSize
== 0)
139 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
141 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
145 dri2_surf
->wl_win
= (struct wl_egl_window
*) window
;
147 dri2_surf
->wl_win
->private = dri2_surf
;
148 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
150 dri2_surf
->base
.Width
= -1;
151 dri2_surf
->base
.Height
= -1;
157 dri2_surf
->dri_drawable
=
158 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
159 type
== EGL_WINDOW_BIT
?
160 dri2_conf
->dri_double_config
:
161 dri2_conf
->dri_single_config
,
163 if (dri2_surf
->dri_drawable
== NULL
) {
164 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
165 goto cleanup_dri_drawable
;
168 return &dri2_surf
->base
;
170 cleanup_dri_drawable
:
171 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
179 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
182 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
183 _EGLConfig
*conf
, EGLNativeWindowType window
,
184 const EGLint
*attrib_list
)
186 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
189 surf
= dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
190 window
, attrib_list
);
193 drv
->API
.SwapInterval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
199 * Called via eglDestroySurface(), drv->API.DestroySurface().
202 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
204 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
205 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
210 if (!_eglPutSurface(surf
))
213 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
215 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
216 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
217 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
218 if (dri2_surf
->color_buffers
[i
].dri_image
)
219 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
222 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
223 if (dri2_surf
->dri_buffers
[i
] &&
224 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
225 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
226 dri2_surf
->dri_buffers
[i
]);
228 if (dri2_surf
->throttle_callback
)
229 wl_callback_destroy(dri2_surf
->throttle_callback
);
231 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
232 dri2_surf
->wl_win
->private = NULL
;
233 dri2_surf
->wl_win
->resize_callback
= NULL
;
242 dri2_release_buffers(struct dri2_egl_surface
*dri2_surf
)
244 struct dri2_egl_display
*dri2_dpy
=
245 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
248 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
249 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
250 !dri2_surf
->color_buffers
[i
].locked
)
251 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
252 if (dri2_surf
->color_buffers
[i
].dri_image
)
253 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
255 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
256 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
257 dri2_surf
->color_buffers
[i
].locked
= 0;
260 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
261 if (dri2_surf
->dri_buffers
[i
] &&
262 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
263 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
264 dri2_surf
->dri_buffers
[i
]);
268 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
270 struct dri2_egl_display
*dri2_dpy
=
271 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
274 /* We always want to throttle to some event (either a frame callback or
275 * a sync request) after the commit so that we can be sure the
276 * compositor has had a chance to handle it and send us a release event
277 * before we look for a free buffer */
278 while (dri2_surf
->throttle_callback
!= NULL
)
279 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
280 dri2_dpy
->wl_queue
) == -1)
283 if (dri2_surf
->back
== NULL
) {
284 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
285 /* Get an unlocked buffer, preferrably one with a dri_buffer
286 * already allocated. */
287 if (dri2_surf
->color_buffers
[i
].locked
)
289 if (dri2_surf
->back
== NULL
)
290 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
291 else if (dri2_surf
->back
->dri_image
== NULL
)
292 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
296 if (dri2_surf
->back
== NULL
)
298 if (dri2_surf
->back
->dri_image
== NULL
) {
299 dri2_surf
->back
->dri_image
=
300 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
301 dri2_surf
->base
.Width
,
302 dri2_surf
->base
.Height
,
303 __DRI_IMAGE_FORMAT_ARGB8888
,
304 __DRI_IMAGE_USE_SHARE
,
306 dri2_surf
->back
->age
= 0;
308 if (dri2_surf
->back
->dri_image
== NULL
)
311 dri2_surf
->back
->locked
= 1;
318 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
320 struct dri2_egl_display
*dri2_dpy
=
321 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
325 image
= dri2_surf
->back
->dri_image
;
327 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
328 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
330 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
332 buffer
->pitch
= pitch
;
338 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
339 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
341 struct dri2_egl_display
*dri2_dpy
=
342 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
343 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
346 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
348 dri2_surf
->base
.Width
,
349 dri2_surf
->base
.Height
);
350 dri2_surf
->dri_buffers
[attachment
] = b
;
355 memcpy(buffer
, b
, sizeof *buffer
);
361 update_buffers(struct dri2_egl_surface
*dri2_surf
)
363 struct dri2_egl_display
*dri2_dpy
=
364 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
367 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&&
368 (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
369 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
)) {
371 dri2_release_buffers(dri2_surf
);
373 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
374 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
375 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
376 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
379 if (get_back_bo(dri2_surf
) < 0) {
380 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
384 /* If we have an extra unlocked buffer at this point, we had to do triple
385 * buffering for a while, but now can go back to just double buffering.
386 * That means we can free any unlocked buffer now. */
387 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
388 if (!dri2_surf
->color_buffers
[i
].locked
&&
389 dri2_surf
->color_buffers
[i
].wl_buffer
) {
390 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
391 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
392 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
393 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
401 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
402 int *width
, int *height
,
403 unsigned int *attachments
, int count
,
404 int *out_count
, void *loaderPrivate
)
406 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
409 if (update_buffers(dri2_surf
) < 0)
412 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
413 switch (attachments
[i
]) {
414 case __DRI_BUFFER_BACK_LEFT
:
415 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
418 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
419 &dri2_surf
->buffers
[j
]) < 0) {
420 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
431 *width
= dri2_surf
->base
.Width
;
432 *height
= dri2_surf
->base
.Height
;
434 return dri2_surf
->buffers
;
438 dri2_get_buffers(__DRIdrawable
* driDrawable
,
439 int *width
, int *height
,
440 unsigned int *attachments
, int count
,
441 int *out_count
, void *loaderPrivate
)
443 unsigned int *attachments_with_format
;
445 const unsigned int format
= 32;
448 attachments_with_format
= calloc(count
* 2, sizeof(unsigned int));
449 if (!attachments_with_format
) {
454 for (i
= 0; i
< count
; ++i
) {
455 attachments_with_format
[2*i
] = attachments
[i
];
456 attachments_with_format
[2*i
+ 1] = format
;
460 dri2_get_buffers_with_format(driDrawable
,
462 attachments_with_format
, count
,
463 out_count
, loaderPrivate
);
465 free(attachments_with_format
);
471 image_get_buffers(__DRIdrawable
*driDrawable
,
475 uint32_t buffer_mask
,
476 struct __DRIimageList
*buffers
)
478 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
480 if (update_buffers(dri2_surf
) < 0)
483 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
484 buffers
->back
= dri2_surf
->back
->dri_image
;
490 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
493 (void) loaderPrivate
;
496 static const __DRIimageLoaderExtension image_loader_extension
= {
497 { __DRI_IMAGE_LOADER
, 1 },
499 dri2_flush_front_buffer
503 wayland_throttle_callback(void *data
,
504 struct wl_callback
*callback
,
507 struct dri2_egl_surface
*dri2_surf
= data
;
509 dri2_surf
->throttle_callback
= NULL
;
510 wl_callback_destroy(callback
);
513 static const struct wl_callback_listener throttle_listener
= {
514 wayland_throttle_callback
518 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
520 struct dri2_egl_display
*dri2_dpy
=
521 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
522 int fd
, stride
, name
;
524 if (dri2_surf
->current
->wl_buffer
!= NULL
)
527 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
528 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
529 __DRI_IMAGE_ATTRIB_FD
, &fd
);
530 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
531 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
533 dri2_surf
->current
->wl_buffer
=
534 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
536 dri2_surf
->base
.Width
,
537 dri2_surf
->base
.Height
,
544 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
545 __DRI_IMAGE_ATTRIB_NAME
, &name
);
546 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
547 __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
549 dri2_surf
->current
->wl_buffer
=
550 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
552 dri2_surf
->base
.Width
,
553 dri2_surf
->base
.Height
,
558 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
560 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
561 &wl_buffer_listener
, dri2_surf
);
565 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
568 dri2_swap_buffers_with_damage(_EGLDriver
*drv
,
574 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
575 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
576 struct dri2_egl_context
*dri2_ctx
;
580 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
581 if (dri2_surf
->color_buffers
[i
].age
> 0)
582 dri2_surf
->color_buffers
[i
].age
++;
584 /* Make sure we have a back buffer in case we're swapping without ever
586 if (get_back_bo(dri2_surf
) < 0) {
587 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
591 if (draw
->SwapInterval
> 0) {
592 dri2_surf
->throttle_callback
=
593 wl_surface_frame(dri2_surf
->wl_win
->surface
);
594 wl_callback_add_listener(dri2_surf
->throttle_callback
,
595 &throttle_listener
, dri2_surf
);
596 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
600 dri2_surf
->back
->age
= 1;
601 dri2_surf
->current
= dri2_surf
->back
;
602 dri2_surf
->back
= NULL
;
604 create_wl_buffer(dri2_surf
);
606 wl_surface_attach(dri2_surf
->wl_win
->surface
,
607 dri2_surf
->current
->wl_buffer
,
608 dri2_surf
->dx
, dri2_surf
->dy
);
610 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
611 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
612 /* reset resize growing parameters */
617 wl_surface_damage(dri2_surf
->wl_win
->surface
,
618 0, 0, INT32_MAX
, INT32_MAX
);
620 for (i
= 0; i
< n_rects
; i
++) {
621 const int *rect
= &rects
[i
* 4];
622 wl_surface_damage(dri2_surf
->wl_win
->surface
,
624 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
629 if (dri2_dpy
->flush
->base
.version
>= 4) {
630 ctx
= _eglGetCurrentContext();
631 dri2_ctx
= dri2_egl_context(ctx
);
632 (*dri2_dpy
->flush
->flush_with_flags
)(dri2_ctx
->dri_context
,
633 dri2_surf
->dri_drawable
,
634 __DRI2_FLUSH_DRAWABLE
,
635 __DRI2_THROTTLE_SWAPBUFFER
);
637 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
640 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
642 wl_surface_commit(dri2_surf
->wl_win
->surface
);
644 /* If we're not waiting for a frame callback then we'll at least throttle
645 * to a sync callback so that we always give a chance for the compositor to
646 * handle the commit and send a release event before checking for a free
648 if (dri2_surf
->throttle_callback
== NULL
) {
649 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
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 wl_display_flush(dri2_dpy
->wl_dpy
);
662 dri2_query_buffer_age(_EGLDriver
*drv
,
663 _EGLDisplay
*disp
, _EGLSurface
*surface
)
665 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
667 if (get_back_bo(dri2_surf
) < 0) {
668 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
672 return dri2_surf
->back
->age
;
676 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
678 return dri2_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
681 static struct wl_buffer
*
682 dri2_create_wayland_buffer_from_image_wl(_EGLDriver
*drv
,
686 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
687 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
688 __DRIimage
*image
= dri2_img
->dri_image
;
689 struct wl_buffer
*buffer
;
690 int width
, height
, format
, pitch
;
691 enum wl_drm_format wl_format
;
693 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
696 case __DRI_IMAGE_FORMAT_ARGB8888
:
697 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
699 wl_format
= WL_DRM_FORMAT_ARGB8888
;
701 case __DRI_IMAGE_FORMAT_XRGB8888
:
702 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
704 wl_format
= WL_DRM_FORMAT_XRGB8888
;
710 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
711 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
712 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
714 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
717 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
720 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
732 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
735 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
742 /* The buffer object will have been created with our internal event queue
743 * because it is using the wl_drm object as a proxy factory. We want the
744 * buffer to be used by the application so we'll reset it to the display's
745 * default event queue */
747 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
752 _eglError(EGL_BAD_MATCH
, "unsupported image format");
757 dri2_wayland_authenticate(_EGLDisplay
*disp
, uint32_t id
)
759 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
762 dri2_dpy
->authenticated
= 0;
764 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
765 if (roundtrip(dri2_dpy
) < 0)
768 if (!dri2_dpy
->authenticated
)
771 /* reset authenticated */
772 dri2_dpy
->authenticated
= 1;
778 * Called via eglTerminate(), drv->API.Terminate().
781 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
783 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
785 _eglReleaseDisplayResources(drv
, disp
);
786 _eglCleanupDisplay(disp
);
788 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
790 dlclose(dri2_dpy
->driver
);
791 free(dri2_dpy
->driver_name
);
792 free(dri2_dpy
->device_name
);
793 wl_drm_destroy(dri2_dpy
->wl_drm
);
794 if (dri2_dpy
->own_device
)
795 wl_display_disconnect(dri2_dpy
->wl_dpy
);
797 disp
->DriverData
= NULL
;
803 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
805 struct dri2_egl_display
*dri2_dpy
= data
;
808 dri2_dpy
->device_name
= strdup(device
);
809 if (!dri2_dpy
->device_name
)
813 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
814 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
817 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
818 if (dri2_dpy
->fd
!= -1)
819 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
822 if (dri2_dpy
->fd
== -1) {
823 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
824 dri2_dpy
->device_name
, strerror(errno
));
828 drmGetMagic(dri2_dpy
->fd
, &magic
);
829 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
833 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
835 struct dri2_egl_display
*dri2_dpy
= data
;
838 case WL_DRM_FORMAT_ARGB8888
:
839 dri2_dpy
->formats
|= HAS_ARGB8888
;
841 case WL_DRM_FORMAT_XRGB8888
:
842 dri2_dpy
->formats
|= HAS_XRGB8888
;
844 case WL_DRM_FORMAT_RGB565
:
845 dri2_dpy
->formats
|= HAS_RGB565
;
851 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
853 struct dri2_egl_display
*dri2_dpy
= data
;
855 dri2_dpy
->capabilities
= value
;
859 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
861 struct dri2_egl_display
*dri2_dpy
= data
;
863 dri2_dpy
->authenticated
= 1;
866 static const struct wl_drm_listener drm_listener
= {
869 drm_handle_authenticated
,
870 drm_handle_capabilities
874 registry_handle_global(void *data
, struct wl_registry
*registry
, uint32_t name
,
875 const char *interface
, uint32_t version
)
877 struct dri2_egl_display
*dri2_dpy
= data
;
881 if (strcmp(interface
, "wl_drm") == 0) {
883 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
884 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
889 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
894 static const struct wl_registry_listener registry_listener
= {
895 registry_handle_global
,
896 registry_handle_global_remove
900 dri2_swap_interval(_EGLDriver
*drv
,
905 if (interval
> surf
->Config
->MaxSwapInterval
)
906 interval
= surf
->Config
->MaxSwapInterval
;
907 else if (interval
< surf
->Config
->MinSwapInterval
)
908 interval
= surf
->Config
->MinSwapInterval
;
910 surf
->SwapInterval
= interval
;
916 dri2_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
918 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
920 /* We can't use values greater than 1 on Wayland because we are using the
921 * frame callback to synchronise the frame and the only way we be sure to
922 * get a frame callback is to attach a new buffer. Therefore we can't just
923 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
925 if (dri2_dpy
->config
)
926 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
927 "vblank_mode", &vblank_mode
);
928 switch (vblank_mode
) {
929 case DRI_CONF_VBLANK_NEVER
:
930 dri2_dpy
->min_swap_interval
= 0;
931 dri2_dpy
->max_swap_interval
= 0;
932 dri2_dpy
->default_swap_interval
= 0;
934 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
935 dri2_dpy
->min_swap_interval
= 1;
936 dri2_dpy
->max_swap_interval
= 1;
937 dri2_dpy
->default_swap_interval
= 1;
939 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
940 dri2_dpy
->min_swap_interval
= 0;
941 dri2_dpy
->max_swap_interval
= 1;
942 dri2_dpy
->default_swap_interval
= 0;
945 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
946 dri2_dpy
->min_swap_interval
= 0;
947 dri2_dpy
->max_swap_interval
= 1;
948 dri2_dpy
->default_swap_interval
= 1;
954 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
956 struct dri2_egl_display
*dri2_dpy
;
957 const __DRIconfig
*config
;
960 static const unsigned int argb_masks
[4] =
961 { 0xff0000, 0xff00, 0xff, 0xff000000 };
962 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
963 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
965 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
966 drv
->API
.DestroySurface
= dri2_destroy_surface
;
967 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
968 drv
->API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
969 drv
->API
.SwapInterval
= dri2_swap_interval
;
970 drv
->API
.Terminate
= dri2_terminate
;
971 drv
->API
.QueryBufferAge
= dri2_query_buffer_age
;
973 drv
->API
.CreateWaylandBufferFromImageWL
=
974 dri2_create_wayland_buffer_from_image_wl
;
976 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
978 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
980 disp
->DriverData
= (void *) dri2_dpy
;
981 if (disp
->PlatformDisplay
== NULL
) {
982 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
983 if (dri2_dpy
->wl_dpy
== NULL
)
985 dri2_dpy
->own_device
= 1;
987 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
990 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
992 if (dri2_dpy
->own_device
)
993 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
995 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
996 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
998 wl_registry_add_listener(dri2_dpy
->wl_registry
,
999 ®istry_listener
, dri2_dpy
);
1000 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1003 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1006 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1009 dri2_dpy
->driver_name
= dri2_get_driver_for_fd(dri2_dpy
->fd
);
1010 if (dri2_dpy
->driver_name
== NULL
) {
1011 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1015 if (!dri2_load_driver(disp
))
1016 goto cleanup_driver_name
;
1018 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1019 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1020 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1021 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1022 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1023 dri2_get_buffers_with_format
;
1025 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1026 dri2_dpy
->extensions
[1] = &image_loader_extension
.base
;
1027 dri2_dpy
->extensions
[2] = &image_lookup_extension
.base
;
1028 dri2_dpy
->extensions
[3] = &use_invalidate
.base
;
1029 dri2_dpy
->extensions
[4] = NULL
;
1031 dri2_dpy
->swap_available
= EGL_TRUE
;
1033 if (!dri2_create_screen(disp
))
1034 goto cleanup_driver
;
1036 dri2_setup_swap_interval(dri2_dpy
);
1038 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
1039 * doesn't have createImageFromFds, since we're using the same driver on
1040 * both sides. We don't want crash if that happens anyway, so fall back to
1041 * gem names if we don't have prime support. */
1043 if (dri2_dpy
->image
->base
.version
< 7 ||
1044 dri2_dpy
->image
->createImageFromFds
== NULL
)
1045 dri2_dpy
->capabilities
&= WL_DRM_CAPABILITY_PRIME
;
1047 types
= EGL_WINDOW_BIT
;
1048 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1049 config
= dri2_dpy
->driver_configs
[i
];
1050 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1051 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1052 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1053 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1054 if (dri2_dpy
->formats
& HAS_RGB565
)
1055 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1058 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1059 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1060 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1061 dri2_dpy
->authenticate
= dri2_wayland_authenticate
;
1063 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1065 /* we're supporting EGL 1.4 */
1066 disp
->VersionMajor
= 1;
1067 disp
->VersionMinor
= 4;
1072 dlclose(dri2_dpy
->driver
);
1073 cleanup_driver_name
:
1074 free(dri2_dpy
->driver_name
);
1076 close(dri2_dpy
->fd
);
1078 free(dri2_dpy
->device_name
);
1079 wl_drm_destroy(dri2_dpy
->wl_drm
);