2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
42 #include "egl_dri2_fallbacks.h"
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
48 enum wl_drm_format_flags
{
55 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
59 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
64 wl_callback_destroy(callback
);
67 static const struct wl_callback_listener sync_listener
= {
72 roundtrip(struct dri2_egl_display
*dri2_dpy
)
74 struct wl_callback
*callback
;
75 int done
= 0, ret
= 0;
77 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
78 wl_callback_add_listener(callback
, &sync_listener
, &done
);
79 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
80 while (ret
!= -1 && !done
)
81 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
84 wl_callback_destroy(callback
);
90 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
92 struct dri2_egl_surface
*dri2_surf
= data
;
95 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
96 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
99 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
100 wl_buffer_destroy(buffer
);
104 dri2_surf
->color_buffers
[i
].locked
= 0;
107 static struct wl_buffer_listener wl_buffer_listener
= {
112 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
114 struct dri2_egl_surface
*dri2_surf
= data
;
115 struct dri2_egl_display
*dri2_dpy
=
116 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
118 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
122 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
125 dri2_wl_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
126 _EGLConfig
*conf
, void *native_window
,
127 const EGLint
*attrib_list
)
129 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
130 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
131 struct wl_egl_window
*window
= native_window
;
132 struct dri2_egl_surface
*dri2_surf
;
136 dri2_surf
= calloc(1, sizeof *dri2_surf
);
138 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
142 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
145 if (conf
->RedSize
== 5)
146 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
147 else if (conf
->AlphaSize
== 0)
148 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
150 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
153 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
157 dri2_surf
->wl_win
= window
;
159 dri2_surf
->wl_win
->private = dri2_surf
;
160 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
162 dri2_surf
->base
.Width
= -1;
163 dri2_surf
->base
.Height
= -1;
165 dri2_surf
->dri_drawable
=
166 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
167 dri2_conf
->dri_double_config
,
169 if (dri2_surf
->dri_drawable
== NULL
) {
170 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
174 return &dri2_surf
->base
;
183 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
186 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
187 _EGLConfig
*conf
, void *native_window
,
188 const EGLint
*attrib_list
)
190 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
193 surf
= dri2_wl_create_surface(drv
, disp
, conf
, native_window
, attrib_list
);
196 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
202 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
203 _EGLConfig
*conf
, void *native_window
,
204 const EGLint
*attrib_list
)
206 /* From the EGL_EXT_platform_wayland spec, version 3:
208 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
209 * that belongs to Wayland. Any such call fails and generates
212 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
218 * Called via eglDestroySurface(), drv->API.DestroySurface().
221 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
223 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
224 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
229 if (!_eglPutSurface(surf
))
232 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
234 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
235 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
236 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
237 if (dri2_surf
->color_buffers
[i
].dri_image
)
238 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
239 if (dri2_surf
->color_buffers
[i
].linear_copy
)
240 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
241 if (dri2_surf
->color_buffers
[i
].data
)
242 munmap(dri2_surf
->color_buffers
[i
].data
,
243 dri2_surf
->color_buffers
[i
].data_size
);
246 if (dri2_dpy
->dri2
) {
247 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
248 if (dri2_surf
->dri_buffers
[i
] &&
249 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
250 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
251 dri2_surf
->dri_buffers
[i
]);
254 if (dri2_surf
->throttle_callback
)
255 wl_callback_destroy(dri2_surf
->throttle_callback
);
257 dri2_surf
->wl_win
->private = NULL
;
258 dri2_surf
->wl_win
->resize_callback
= NULL
;
266 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
268 struct dri2_egl_display
*dri2_dpy
=
269 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
272 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
273 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
274 !dri2_surf
->color_buffers
[i
].locked
)
275 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
276 if (dri2_surf
->color_buffers
[i
].dri_image
)
277 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
278 if (dri2_surf
->color_buffers
[i
].linear_copy
)
279 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
280 if (dri2_surf
->color_buffers
[i
].data
)
281 munmap(dri2_surf
->color_buffers
[i
].data
,
282 dri2_surf
->color_buffers
[i
].data_size
);
284 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
285 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
286 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
287 dri2_surf
->color_buffers
[i
].data
= NULL
;
288 dri2_surf
->color_buffers
[i
].locked
= 0;
291 if (dri2_dpy
->dri2
) {
292 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
293 if (dri2_surf
->dri_buffers
[i
] &&
294 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
295 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
296 dri2_surf
->dri_buffers
[i
]);
301 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
303 struct dri2_egl_display
*dri2_dpy
=
304 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
306 unsigned int dri_image_format
;
308 /* currently supports three WL DRM formats,
309 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
310 * and WL_DRM_FORMAT_RGB565
312 switch (dri2_surf
->format
) {
313 case WL_DRM_FORMAT_ARGB8888
:
314 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
316 case WL_DRM_FORMAT_XRGB8888
:
317 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
319 case WL_DRM_FORMAT_RGB565
:
320 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
323 /* format is not supported */
327 /* We always want to throttle to some event (either a frame callback or
328 * a sync request) after the commit so that we can be sure the
329 * compositor has had a chance to handle it and send us a release event
330 * before we look for a free buffer */
331 while (dri2_surf
->throttle_callback
!= NULL
)
332 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
333 dri2_dpy
->wl_queue
) == -1)
336 if (dri2_surf
->back
== NULL
) {
337 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
338 /* Get an unlocked buffer, preferrably one with a dri_buffer
339 * already allocated. */
340 if (dri2_surf
->color_buffers
[i
].locked
)
342 if (dri2_surf
->back
== NULL
)
343 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
344 else if (dri2_surf
->back
->dri_image
== NULL
)
345 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
349 if (dri2_surf
->back
== NULL
)
352 if (dri2_dpy
->is_different_gpu
&&
353 dri2_surf
->back
->linear_copy
== NULL
) {
354 dri2_surf
->back
->linear_copy
=
355 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
356 dri2_surf
->base
.Width
,
357 dri2_surf
->base
.Height
,
359 __DRI_IMAGE_USE_SHARE
|
360 __DRI_IMAGE_USE_LINEAR
,
362 if (dri2_surf
->back
->linear_copy
== NULL
)
366 if (dri2_surf
->back
->dri_image
== NULL
) {
367 dri2_surf
->back
->dri_image
=
368 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
369 dri2_surf
->base
.Width
,
370 dri2_surf
->base
.Height
,
372 dri2_dpy
->is_different_gpu
?
373 0 : __DRI_IMAGE_USE_SHARE
,
375 dri2_surf
->back
->age
= 0;
377 if (dri2_surf
->back
->dri_image
== NULL
)
380 dri2_surf
->back
->locked
= 1;
387 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
389 struct dri2_egl_display
*dri2_dpy
=
390 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
394 image
= dri2_surf
->back
->dri_image
;
396 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
397 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
399 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
401 buffer
->pitch
= pitch
;
407 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
408 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
410 struct dri2_egl_display
*dri2_dpy
=
411 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
412 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
415 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
417 dri2_surf
->base
.Width
,
418 dri2_surf
->base
.Height
);
419 dri2_surf
->dri_buffers
[attachment
] = b
;
424 memcpy(buffer
, b
, sizeof *buffer
);
430 update_buffers(struct dri2_egl_surface
*dri2_surf
)
432 struct dri2_egl_display
*dri2_dpy
=
433 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
436 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
437 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
439 dri2_wl_release_buffers(dri2_surf
);
441 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
442 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
443 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
444 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
447 if (get_back_bo(dri2_surf
) < 0) {
448 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
452 /* If we have an extra unlocked buffer at this point, we had to do triple
453 * buffering for a while, but now can go back to just double buffering.
454 * That means we can free any unlocked buffer now. */
455 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
456 if (!dri2_surf
->color_buffers
[i
].locked
&&
457 dri2_surf
->color_buffers
[i
].wl_buffer
) {
458 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
459 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
460 if (dri2_dpy
->is_different_gpu
)
461 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
462 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
463 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
464 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
472 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
473 int *width
, int *height
,
474 unsigned int *attachments
, int count
,
475 int *out_count
, void *loaderPrivate
)
477 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
480 if (update_buffers(dri2_surf
) < 0)
483 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
484 switch (attachments
[i
]) {
485 case __DRI_BUFFER_BACK_LEFT
:
486 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
489 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
490 &dri2_surf
->buffers
[j
]) < 0) {
491 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
502 *width
= dri2_surf
->base
.Width
;
503 *height
= dri2_surf
->base
.Height
;
505 return dri2_surf
->buffers
;
509 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
510 int *width
, int *height
,
511 unsigned int *attachments
, int count
,
512 int *out_count
, void *loaderPrivate
)
514 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
515 unsigned int *attachments_with_format
;
521 switch (dri2_surf
->format
) {
522 case WL_DRM_FORMAT_ARGB8888
:
523 case WL_DRM_FORMAT_XRGB8888
:
526 case WL_DRM_FORMAT_RGB565
:
530 /* format is not supported */
534 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
535 if (!attachments_with_format
) {
540 for (i
= 0; i
< count
; ++i
) {
541 attachments_with_format
[2*i
] = attachments
[i
];
542 attachments_with_format
[2*i
+ 1] = bpp
;
546 dri2_wl_get_buffers_with_format(driDrawable
,
548 attachments_with_format
, count
,
549 out_count
, loaderPrivate
);
551 free(attachments_with_format
);
557 image_get_buffers(__DRIdrawable
*driDrawable
,
561 uint32_t buffer_mask
,
562 struct __DRIimageList
*buffers
)
564 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
566 if (update_buffers(dri2_surf
) < 0)
569 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
570 buffers
->back
= dri2_surf
->back
->dri_image
;
576 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
579 (void) loaderPrivate
;
582 static const __DRIimageLoaderExtension image_loader_extension
= {
583 .base
= { __DRI_IMAGE_LOADER
, 1 },
585 .getBuffers
= image_get_buffers
,
586 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
590 wayland_throttle_callback(void *data
,
591 struct wl_callback
*callback
,
594 struct dri2_egl_surface
*dri2_surf
= data
;
596 dri2_surf
->throttle_callback
= NULL
;
597 wl_callback_destroy(callback
);
600 static const struct wl_callback_listener throttle_listener
= {
601 wayland_throttle_callback
605 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
607 struct dri2_egl_display
*dri2_dpy
=
608 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
610 int fd
, stride
, name
;
612 if (dri2_surf
->current
->wl_buffer
!= NULL
)
615 if (dri2_dpy
->is_different_gpu
) {
616 image
= dri2_surf
->current
->linear_copy
;
618 image
= dri2_surf
->current
->dri_image
;
620 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
621 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
622 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
624 dri2_surf
->current
->wl_buffer
=
625 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
627 dri2_surf
->base
.Width
,
628 dri2_surf
->base
.Height
,
635 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
636 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
638 dri2_surf
->current
->wl_buffer
=
639 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
641 dri2_surf
->base
.Width
,
642 dri2_surf
->base
.Height
,
647 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
649 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
650 &wl_buffer_listener
, dri2_surf
);
654 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
657 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
663 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
664 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
667 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
668 if (dri2_surf
->color_buffers
[i
].age
> 0)
669 dri2_surf
->color_buffers
[i
].age
++;
671 /* Make sure we have a back buffer in case we're swapping without ever
673 if (get_back_bo(dri2_surf
) < 0) {
674 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
678 if (draw
->SwapInterval
> 0) {
679 dri2_surf
->throttle_callback
=
680 wl_surface_frame(dri2_surf
->wl_win
->surface
);
681 wl_callback_add_listener(dri2_surf
->throttle_callback
,
682 &throttle_listener
, dri2_surf
);
683 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
687 dri2_surf
->back
->age
= 1;
688 dri2_surf
->current
= dri2_surf
->back
;
689 dri2_surf
->back
= NULL
;
691 create_wl_buffer(dri2_surf
);
693 wl_surface_attach(dri2_surf
->wl_win
->surface
,
694 dri2_surf
->current
->wl_buffer
,
695 dri2_surf
->dx
, dri2_surf
->dy
);
697 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
698 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
699 /* reset resize growing parameters */
704 wl_surface_damage(dri2_surf
->wl_win
->surface
,
705 0, 0, INT32_MAX
, INT32_MAX
);
707 for (i
= 0; i
< n_rects
; i
++) {
708 const int *rect
= &rects
[i
* 4];
709 wl_surface_damage(dri2_surf
->wl_win
->surface
,
711 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
716 if (dri2_dpy
->is_different_gpu
) {
717 _EGLContext
*ctx
= _eglGetCurrentContext();
718 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
719 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
720 dri2_surf
->current
->linear_copy
,
721 dri2_surf
->current
->dri_image
,
722 0, 0, dri2_surf
->base
.Width
,
723 dri2_surf
->base
.Height
,
724 0, 0, dri2_surf
->base
.Width
,
725 dri2_surf
->base
.Height
, 0);
728 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
729 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
731 wl_surface_commit(dri2_surf
->wl_win
->surface
);
733 /* If we're not waiting for a frame callback then we'll at least throttle
734 * to a sync callback so that we always give a chance for the compositor to
735 * handle the commit and send a release event before checking for a free
737 if (dri2_surf
->throttle_callback
== NULL
) {
738 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
739 wl_callback_add_listener(dri2_surf
->throttle_callback
,
740 &throttle_listener
, dri2_surf
);
741 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
745 wl_display_flush(dri2_dpy
->wl_dpy
);
751 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
752 _EGLDisplay
*disp
, _EGLSurface
*surface
)
754 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
756 if (get_back_bo(dri2_surf
) < 0) {
757 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
761 return dri2_surf
->back
->age
;
765 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
767 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
770 static struct wl_buffer
*
771 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
775 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
776 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
777 __DRIimage
*image
= dri2_img
->dri_image
;
778 struct wl_buffer
*buffer
;
779 int width
, height
, format
, pitch
;
780 enum wl_drm_format wl_format
;
782 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
785 case __DRI_IMAGE_FORMAT_ARGB8888
:
786 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
788 wl_format
= WL_DRM_FORMAT_ARGB8888
;
790 case __DRI_IMAGE_FORMAT_XRGB8888
:
791 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
793 wl_format
= WL_DRM_FORMAT_XRGB8888
;
799 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
800 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
801 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
803 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
806 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
809 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
821 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
824 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
831 /* The buffer object will have been created with our internal event queue
832 * because it is using the wl_drm object as a proxy factory. We want the
833 * buffer to be used by the application so we'll reset it to the display's
834 * default event queue */
836 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
841 _eglError(EGL_BAD_MATCH
, "unsupported image format");
846 is_fd_render_node(int fd
)
850 if (fstat(fd
, &render
))
853 if (!S_ISCHR(render
.st_mode
))
856 if (render
.st_rdev
& 0x80)
862 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
864 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
867 if (dri2_dpy
->is_render_node
) {
868 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
869 "authenticate for render-nodes");
872 dri2_dpy
->authenticated
= 0;
874 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
875 if (roundtrip(dri2_dpy
) < 0)
878 if (!dri2_dpy
->authenticated
)
881 /* reset authenticated */
882 dri2_dpy
->authenticated
= 1;
888 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
890 struct dri2_egl_display
*dri2_dpy
= data
;
893 dri2_dpy
->device_name
= strdup(device
);
894 if (!dri2_dpy
->device_name
)
897 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
898 if (dri2_dpy
->fd
== -1) {
899 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
900 dri2_dpy
->device_name
, strerror(errno
));
904 if (is_fd_render_node(dri2_dpy
->fd
)) {
905 dri2_dpy
->authenticated
= 1;
907 drmGetMagic(dri2_dpy
->fd
, &magic
);
908 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
913 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
915 struct dri2_egl_display
*dri2_dpy
= data
;
918 case WL_DRM_FORMAT_ARGB8888
:
919 dri2_dpy
->formats
|= HAS_ARGB8888
;
921 case WL_DRM_FORMAT_XRGB8888
:
922 dri2_dpy
->formats
|= HAS_XRGB8888
;
924 case WL_DRM_FORMAT_RGB565
:
925 dri2_dpy
->formats
|= HAS_RGB565
;
931 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
933 struct dri2_egl_display
*dri2_dpy
= data
;
935 dri2_dpy
->capabilities
= value
;
939 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
941 struct dri2_egl_display
*dri2_dpy
= data
;
943 dri2_dpy
->authenticated
= 1;
946 static const struct wl_drm_listener drm_listener
= {
949 drm_handle_authenticated
,
950 drm_handle_capabilities
954 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
955 const char *interface
, uint32_t version
)
957 struct dri2_egl_display
*dri2_dpy
= data
;
961 if (strcmp(interface
, "wl_drm") == 0) {
963 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
964 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
969 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
974 static const struct wl_registry_listener registry_listener_drm
= {
975 registry_handle_global_drm
,
976 registry_handle_global_remove
980 dri2_wl_swap_interval(_EGLDriver
*drv
,
985 if (interval
> surf
->Config
->MaxSwapInterval
)
986 interval
= surf
->Config
->MaxSwapInterval
;
987 else if (interval
< surf
->Config
->MinSwapInterval
)
988 interval
= surf
->Config
->MinSwapInterval
;
990 surf
->SwapInterval
= interval
;
996 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
998 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1000 /* We can't use values greater than 1 on Wayland because we are using the
1001 * frame callback to synchronise the frame and the only way we be sure to
1002 * get a frame callback is to attach a new buffer. Therefore we can't just
1003 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1005 if (dri2_dpy
->config
)
1006 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1007 "vblank_mode", &vblank_mode
);
1008 switch (vblank_mode
) {
1009 case DRI_CONF_VBLANK_NEVER
:
1010 dri2_dpy
->min_swap_interval
= 0;
1011 dri2_dpy
->max_swap_interval
= 0;
1012 dri2_dpy
->default_swap_interval
= 0;
1014 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1015 dri2_dpy
->min_swap_interval
= 1;
1016 dri2_dpy
->max_swap_interval
= 1;
1017 dri2_dpy
->default_swap_interval
= 1;
1019 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1020 dri2_dpy
->min_swap_interval
= 0;
1021 dri2_dpy
->max_swap_interval
= 1;
1022 dri2_dpy
->default_swap_interval
= 0;
1025 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1026 dri2_dpy
->min_swap_interval
= 0;
1027 dri2_dpy
->max_swap_interval
= 1;
1028 dri2_dpy
->default_swap_interval
= 1;
1033 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1034 .authenticate
= dri2_wl_authenticate
,
1035 .create_window_surface
= dri2_wl_create_window_surface
,
1036 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1037 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1038 .destroy_surface
= dri2_wl_destroy_surface
,
1039 .create_image
= dri2_create_image_khr
,
1040 .swap_interval
= dri2_wl_swap_interval
,
1041 .swap_buffers
= dri2_wl_swap_buffers
,
1042 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1043 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1044 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1045 .copy_buffers
= dri2_fallback_copy_buffers
,
1046 .query_buffer_age
= dri2_wl_query_buffer_age
,
1047 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1048 .get_sync_values
= dri2_fallback_get_sync_values
,
1052 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1054 struct dri2_egl_display
*dri2_dpy
;
1055 const __DRIconfig
*config
;
1058 static const unsigned int argb_masks
[4] =
1059 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1060 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1061 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1063 loader_set_logger(_eglLog
);
1065 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1067 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1069 disp
->DriverData
= (void *) dri2_dpy
;
1070 if (disp
->PlatformDisplay
== NULL
) {
1071 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1072 if (dri2_dpy
->wl_dpy
== NULL
)
1074 dri2_dpy
->own_device
= 1;
1076 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1079 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1081 if (dri2_dpy
->own_device
)
1082 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1084 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1085 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1086 dri2_dpy
->wl_queue
);
1087 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1088 ®istry_listener_drm
, dri2_dpy
);
1089 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1090 goto cleanup_registry
;
1092 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1095 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1098 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1099 &dri2_dpy
->is_different_gpu
);
1100 if (dri2_dpy
->is_different_gpu
) {
1101 free(dri2_dpy
->device_name
);
1102 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1103 if (!dri2_dpy
->device_name
) {
1104 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1105 "for requested GPU");
1110 /* we have to do the check now, because loader_get_user_preferred_fd
1111 * will return a render-node when the requested gpu is different
1112 * to the server, but also if the client asks for the same gpu than
1113 * the server by requesting its pci-id */
1114 dri2_dpy
->is_render_node
= is_fd_render_node(dri2_dpy
->fd
);
1116 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1117 if (dri2_dpy
->driver_name
== NULL
) {
1118 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1122 if (!dri2_load_driver(disp
))
1123 goto cleanup_driver_name
;
1125 dri2_dpy
->extensions
[0] = &image_loader_extension
.base
;
1126 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1127 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
1129 /* render nodes cannot use Gem names, and thus do not support
1130 * the __DRI_DRI2_LOADER extension */
1131 if (!dri2_dpy
->is_render_node
) {
1132 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1133 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1134 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1135 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1136 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1137 dri2_wl_get_buffers_with_format
;
1138 dri2_dpy
->extensions
[3] = &dri2_dpy
->dri2_loader_extension
.base
;
1139 dri2_dpy
->extensions
[4] = NULL
;
1141 dri2_dpy
->extensions
[3] = NULL
;
1143 dri2_dpy
->swap_available
= EGL_TRUE
;
1145 if (!dri2_create_screen(disp
))
1146 goto cleanup_driver
;
1148 dri2_wl_setup_swap_interval(dri2_dpy
);
1150 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1151 * createImageFromFds support indicates that Prime export/import
1152 * is supported by the driver. Fall back to
1153 * gem names if we don't have Prime support. */
1155 if (dri2_dpy
->image
->base
.version
< 7 ||
1156 dri2_dpy
->image
->createImageFromFds
== NULL
)
1157 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1159 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1160 * The server needs to accept them */
1161 if (dri2_dpy
->is_render_node
&&
1162 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1163 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1164 goto cleanup_screen
;
1167 if (dri2_dpy
->is_different_gpu
&&
1168 (dri2_dpy
->image
->base
.version
< 9 ||
1169 dri2_dpy
->image
->blitImage
== NULL
)) {
1170 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1171 "Image extension in the driver is not "
1172 "compatible. Version 9 or later and blitImage() "
1174 goto cleanup_screen
;
1177 types
= EGL_WINDOW_BIT
;
1178 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1179 config
= dri2_dpy
->driver_configs
[i
];
1180 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1181 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1182 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1183 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1184 if (dri2_dpy
->formats
& HAS_RGB565
)
1185 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1188 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1189 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1190 * because the buffer of the EGLImage has likely a tiling mode the server
1191 * gpu won't support. These is no way to check for now. Thus do not support the
1193 if (!dri2_dpy
->is_different_gpu
) {
1194 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1196 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1197 dri2_fallback_create_wayland_buffer_from_image
;
1199 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1201 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1203 /* Fill vtbl last to prevent accidentally calling virtual function during
1206 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1211 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1213 dlclose(dri2_dpy
->driver
);
1214 cleanup_driver_name
:
1215 free(dri2_dpy
->driver_name
);
1217 close(dri2_dpy
->fd
);
1219 free(dri2_dpy
->device_name
);
1220 wl_drm_destroy(dri2_dpy
->wl_drm
);
1222 wl_registry_destroy(dri2_dpy
->wl_registry
);
1223 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1231 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1233 if (format
== WL_SHM_FORMAT_RGB565
)
1235 else /* ARGB8888 || XRGB8888 */
1240 * Taken from weston shared/os-compatibility.c
1244 set_cloexec_or_close(int fd
)
1251 flags
= fcntl(fd
, F_GETFD
);
1255 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1266 * Taken from weston shared/os-compatibility.c
1270 create_tmpfile_cloexec(char *tmpname
)
1274 #ifdef HAVE_MKOSTEMP
1275 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1279 fd
= mkstemp(tmpname
);
1281 fd
= set_cloexec_or_close(fd
);
1290 * Taken from weston shared/os-compatibility.c
1292 * Create a new, unique, anonymous file of the given size, and
1293 * return the file descriptor for it. The file descriptor is set
1294 * CLOEXEC. The file is immediately suitable for mmap()'ing
1295 * the given size at offset zero.
1297 * The file should not have a permanent backing store like a disk,
1298 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1300 * The file name is deleted from the file system.
1302 * The file is suitable for buffer sharing between processes by
1303 * transmitting the file descriptor over Unix sockets using the
1304 * SCM_RIGHTS methods.
1306 * If the C library implements posix_fallocate(), it is used to
1307 * guarantee that disk space is available for the file at the
1308 * given size. If disk space is insufficent, errno is set to ENOSPC.
1309 * If posix_fallocate() is not supported, program may receive
1310 * SIGBUS on accessing mmap()'ed file contents instead.
1313 os_create_anonymous_file(off_t size
)
1315 static const char template[] = "/mesa-shared-XXXXXX";
1321 path
= getenv("XDG_RUNTIME_DIR");
1327 name
= malloc(strlen(path
) + sizeof(template));
1332 strcat(name
, template);
1334 fd
= create_tmpfile_cloexec(name
);
1341 ret
= ftruncate(fd
, size
);
1352 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1353 int format
, int w
, int h
,
1354 void **data
, int *size
,
1355 struct wl_buffer
**buffer
)
1357 struct wl_shm_pool
*pool
;
1358 int fd
, stride
, size_map
;
1361 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1362 size_map
= h
* stride
;
1364 /* Create a sharable buffer */
1365 fd
= os_create_anonymous_file(size_map
);
1369 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1370 if (data_map
== MAP_FAILED
) {
1375 /* Share it in a wl_buffer */
1376 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1377 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1378 wl_shm_pool_destroy(pool
);
1387 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1389 struct dri2_egl_display
*dri2_dpy
=
1390 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1393 /* we need to do the following operations only once per frame */
1394 if (dri2_surf
->back
)
1397 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1398 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1400 dri2_wl_release_buffers(dri2_surf
);
1402 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1403 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1404 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1405 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1406 dri2_surf
->current
= NULL
;
1409 /* find back buffer */
1411 /* We always want to throttle to some event (either a frame callback or
1412 * a sync request) after the commit so that we can be sure the
1413 * compositor has had a chance to handle it and send us a release event
1414 * before we look for a free buffer */
1415 while (dri2_surf
->throttle_callback
!= NULL
)
1416 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1417 dri2_dpy
->wl_queue
) == -1)
1420 /* try get free buffer already created */
1421 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1422 if (!dri2_surf
->color_buffers
[i
].locked
&&
1423 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1424 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1429 /* else choose any another free location */
1430 if (!dri2_surf
->back
) {
1431 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1432 if (!dri2_surf
->color_buffers
[i
].locked
) {
1433 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1434 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1436 dri2_surf
->base
.Width
,
1437 dri2_surf
->base
.Height
,
1438 &dri2_surf
->back
->data
,
1439 &dri2_surf
->back
->data_size
,
1440 &dri2_surf
->back
->wl_buffer
)) {
1441 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1444 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1445 dri2_dpy
->wl_queue
);
1446 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1447 &wl_buffer_listener
, dri2_surf
);
1453 if (!dri2_surf
->back
) {
1454 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1458 dri2_surf
->back
->locked
= 1;
1460 /* If we have an extra unlocked buffer at this point, we had to do triple
1461 * buffering for a while, but now can go back to just double buffering.
1462 * That means we can free any unlocked buffer now. */
1463 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1464 if (!dri2_surf
->color_buffers
[i
].locked
&&
1465 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1466 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1467 munmap(dri2_surf
->color_buffers
[i
].data
,
1468 dri2_surf
->color_buffers
[i
].data_size
);
1469 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1470 dri2_surf
->color_buffers
[i
].data
= NULL
;
1478 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1480 /* if there has been a resize: */
1481 if (!dri2_surf
->current
)
1484 return dri2_surf
->current
->data
;
1488 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1490 assert(dri2_surf
->back
);
1491 return dri2_surf
->back
->data
;
1495 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1497 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1499 if (dri2_surf
->base
.SwapInterval
> 0) {
1500 dri2_surf
->throttle_callback
=
1501 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1502 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1503 &throttle_listener
, dri2_surf
);
1504 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1505 dri2_dpy
->wl_queue
);
1508 dri2_surf
->current
= dri2_surf
->back
;
1509 dri2_surf
->back
= NULL
;
1511 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1512 dri2_surf
->current
->wl_buffer
,
1513 dri2_surf
->dx
, dri2_surf
->dy
);
1515 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1516 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1517 /* reset resize growing parameters */
1521 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1522 0, 0, INT32_MAX
, INT32_MAX
);
1523 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1525 /* If we're not waiting for a frame callback then we'll at least throttle
1526 * to a sync callback so that we always give a chance for the compositor to
1527 * handle the commit and send a release event before checking for a free
1529 if (dri2_surf
->throttle_callback
== NULL
) {
1530 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
1531 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1532 &throttle_listener
, dri2_surf
);
1533 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1534 dri2_dpy
->wl_queue
);
1537 wl_display_flush(dri2_dpy
->wl_dpy
);
1541 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1542 int *x
, int *y
, int *w
, int *h
,
1543 void *loaderPrivate
)
1545 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1547 (void) swrast_update_buffers(dri2_surf
);
1550 *w
= dri2_surf
->base
.Width
;
1551 *h
= dri2_surf
->base
.Height
;
1555 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1556 int x
, int y
, int w
, int h
,
1557 char *data
, void *loaderPrivate
)
1559 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1560 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1561 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1562 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1563 int dst_stride
= copy_width
;
1566 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1568 memset(data
, 0, copy_width
* h
);
1572 assert(data
!= src
);
1573 assert(copy_width
<= src_stride
);
1576 src
+= y
* src_stride
;
1579 if (copy_width
> src_stride
-x_offset
)
1580 copy_width
= src_stride
-x_offset
;
1581 if (h
> dri2_surf
->base
.Height
-y
)
1582 h
= dri2_surf
->base
.Height
-y
;
1585 memcpy(dst
, src
, copy_width
);
1592 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1593 int x
, int y
, int w
, int h
, int stride
,
1594 char *data
, void *loaderPrivate
)
1596 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1597 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1598 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1599 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1602 assert(copy_width
<= stride
);
1604 (void) swrast_update_buffers(dri2_surf
);
1605 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1607 /* partial copy, copy old content */
1608 if (copy_width
< dst_stride
)
1609 dri2_wl_swrast_get_image(draw
, 0, 0,
1610 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1611 dst
, loaderPrivate
);
1614 dst
+= y
* dst_stride
;
1618 /* drivers expect we do these checks (and some rely on it) */
1619 if (copy_width
> dst_stride
-x_offset
)
1620 copy_width
= dst_stride
-x_offset
;
1621 if (h
> dri2_surf
->base
.Height
-y
)
1622 h
= dri2_surf
->base
.Height
-y
;
1625 memcpy(dst
, src
, copy_width
);
1629 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1633 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1634 int x
, int y
, int w
, int h
,
1635 char *data
, void *loaderPrivate
)
1637 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1640 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1641 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1642 stride
, data
, loaderPrivate
);
1646 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1648 static _EGLSurface
*
1649 dri2_wl_swrast_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1650 _EGLConfig
*conf
, void *native_window
,
1651 const EGLint
*attrib_list
)
1653 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1654 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1655 struct wl_egl_window
*window
= native_window
;
1656 struct dri2_egl_surface
*dri2_surf
;
1660 dri2_surf
= calloc(1, sizeof *dri2_surf
);
1662 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1666 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
1669 if (conf
->RedSize
== 5)
1670 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
1671 else if (conf
->AlphaSize
== 0)
1672 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
1674 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
1676 dri2_surf
->wl_win
= window
;
1678 dri2_surf
->base
.Width
= -1;
1679 dri2_surf
->base
.Height
= -1;
1681 dri2_surf
->dri_drawable
=
1682 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
1683 dri2_conf
->dri_double_config
,
1685 if (dri2_surf
->dri_drawable
== NULL
) {
1686 _eglError(EGL_BAD_ALLOC
, "swrast->createNewDrawable");
1687 goto cleanup_dri_drawable
;
1690 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
1691 dri2_dpy
->default_swap_interval
);
1693 return &dri2_surf
->base
;
1695 cleanup_dri_drawable
:
1696 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1704 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1706 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1707 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1709 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1714 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1716 struct dri2_egl_display
*dri2_dpy
= data
;
1719 case WL_SHM_FORMAT_ARGB8888
:
1720 dri2_dpy
->formats
|= HAS_ARGB8888
;
1722 case WL_SHM_FORMAT_XRGB8888
:
1723 dri2_dpy
->formats
|= HAS_XRGB8888
;
1725 case WL_SHM_FORMAT_RGB565
:
1726 dri2_dpy
->formats
|= HAS_RGB565
;
1731 static const struct wl_shm_listener shm_listener
= {
1736 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1737 const char *interface
, uint32_t version
)
1739 struct dri2_egl_display
*dri2_dpy
= data
;
1741 if (strcmp(interface
, "wl_shm") == 0) {
1743 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1744 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1748 static const struct wl_registry_listener registry_listener_swrast
= {
1749 registry_handle_global_swrast
,
1750 registry_handle_global_remove
1753 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1754 .authenticate
= NULL
,
1755 .create_window_surface
= dri2_wl_swrast_create_window_surface
,
1756 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1757 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1758 .destroy_surface
= dri2_wl_destroy_surface
,
1759 .create_image
= dri2_fallback_create_image_khr
,
1760 .swap_interval
= dri2_wl_swap_interval
,
1761 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1762 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1763 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1764 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1765 .copy_buffers
= dri2_fallback_copy_buffers
,
1766 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1767 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1768 .get_sync_values
= dri2_fallback_get_sync_values
,
1772 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1774 struct dri2_egl_display
*dri2_dpy
;
1775 const __DRIconfig
*config
;
1778 static const unsigned int argb_masks
[4] =
1779 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1780 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1781 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1783 loader_set_logger(_eglLog
);
1785 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1787 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1789 disp
->DriverData
= (void *) dri2_dpy
;
1790 if (disp
->PlatformDisplay
== NULL
) {
1791 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1792 if (dri2_dpy
->wl_dpy
== NULL
)
1794 dri2_dpy
->own_device
= 1;
1796 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1799 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1801 if (dri2_dpy
->own_device
)
1802 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1804 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1805 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1806 dri2_dpy
->wl_queue
);
1807 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1808 ®istry_listener_swrast
, dri2_dpy
);
1810 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1811 goto cleanup_registry
;
1813 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1816 dri2_dpy
->driver_name
= strdup("swrast");
1817 if (!dri2_load_driver_swrast(disp
))
1820 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
1821 dri2_dpy
->swrast_loader_extension
.base
.version
= 2;
1822 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= dri2_wl_swrast_get_drawable_info
;
1823 dri2_dpy
->swrast_loader_extension
.putImage
= dri2_wl_swrast_put_image
;
1824 dri2_dpy
->swrast_loader_extension
.getImage
= dri2_wl_swrast_get_image
;
1825 dri2_dpy
->swrast_loader_extension
.putImage2
= dri2_wl_swrast_put_image2
;
1827 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
1828 dri2_dpy
->extensions
[1] = NULL
;
1830 if (!dri2_create_screen(disp
))
1831 goto cleanup_driver
;
1833 dri2_wl_setup_swap_interval(dri2_dpy
);
1835 types
= EGL_WINDOW_BIT
;
1836 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1837 config
= dri2_dpy
->driver_configs
[i
];
1838 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1839 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1840 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1841 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1842 if (dri2_dpy
->formats
& HAS_RGB565
)
1843 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1846 /* Fill vtbl last to prevent accidentally calling virtual function during
1849 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1854 dlclose(dri2_dpy
->driver
);
1856 wl_shm_destroy(dri2_dpy
->wl_shm
);
1858 wl_registry_destroy(dri2_dpy
->wl_registry
);
1859 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1867 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1869 EGLBoolean initialized
= EGL_TRUE
;
1871 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1874 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1875 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1878 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);