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>
39 #include <drm_fourcc.h>
43 #include "egl_dri2_fallbacks.h"
45 #include "util/u_vector.h"
46 #include "eglglobals.h"
48 #include <wayland-client.h>
49 #include "wayland-drm-client-protocol.h"
50 #include "linux-dmabuf-unstable-v1-client-protocol.h"
52 #ifndef DRM_FORMAT_MOD_INVALID
53 #define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
56 #ifndef DRM_FORMAT_MOD_LINEAR
57 #define DRM_FORMAT_MOD_LINEAR 0
60 enum wl_drm_format_flags
{
68 static const struct dri2_wl_visual
{
69 const char *format_name
;
70 enum wl_drm_format_flags has_format
;
71 uint32_t wl_drm_format
;
72 uint32_t wl_shm_format
;
74 unsigned int rgba_masks
[4];
75 } dri2_wl_visuals
[] = {
77 "XRGB2101010", HAS_XRGB2101010
,
78 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
79 __DRI_IMAGE_FORMAT_XRGB2101010
,
80 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0x00000000 }
83 "ARGB2101010", HAS_ARGB2101010
,
84 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
85 __DRI_IMAGE_FORMAT_ARGB2101010
,
86 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 }
89 "XRGB8888", HAS_XRGB8888
,
90 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
91 __DRI_IMAGE_FORMAT_XRGB8888
,
92 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }
95 "ARGB8888", HAS_ARGB8888
,
96 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
97 __DRI_IMAGE_FORMAT_ARGB8888
,
98 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }
101 "RGB565", HAS_RGB565
,
102 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
103 __DRI_IMAGE_FORMAT_RGB565
,
104 { 0xf800, 0x07e0, 0x001f, 0x0000 }
109 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
110 const __DRIconfig
*config
)
112 unsigned int red
, green
, blue
, alpha
;
114 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &red
);
115 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &green
);
116 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &blue
);
117 dri2_dpy
->core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &alpha
);
119 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
120 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
122 if (red
== wl_visual
->rgba_masks
[0] &&
123 green
== wl_visual
->rgba_masks
[1] &&
124 blue
== wl_visual
->rgba_masks
[2] &&
125 alpha
== wl_visual
->rgba_masks
[3]) {
134 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
136 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
137 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
139 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
147 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
149 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
150 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
158 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
160 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
161 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
169 roundtrip(struct dri2_egl_display
*dri2_dpy
)
171 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
175 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
177 struct dri2_egl_surface
*dri2_surf
= data
;
180 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
181 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
184 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
185 wl_buffer_destroy(buffer
);
189 dri2_surf
->color_buffers
[i
].locked
= false;
192 static const struct wl_buffer_listener wl_buffer_listener
= {
193 .release
= wl_buffer_release
197 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
199 struct dri2_egl_surface
*dri2_surf
= data
;
200 struct dri2_egl_display
*dri2_dpy
=
201 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
203 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
207 destroy_window_callback(void *data
)
209 struct dri2_egl_surface
*dri2_surf
= data
;
210 dri2_surf
->wl_win
= NULL
;
213 static struct wl_surface
*
214 get_wl_surface_proxy(struct wl_egl_window
*window
)
216 /* Version 3 of wl_egl_window introduced a version field at the same
217 * location where a pointer to wl_surface was stored. Thus, if
218 * window->version is dereferencable, we've been given an older version of
219 * wl_egl_window, and window->version points to wl_surface */
220 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
221 return wl_proxy_create_wrapper((void *)(window
->version
));
223 return wl_proxy_create_wrapper(window
->surface
);
227 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
230 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
231 _EGLConfig
*conf
, void *native_window
,
232 const EGLint
*attrib_list
)
234 __DRIcreateNewDrawableFunc createNewDrawable
;
235 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
236 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
237 struct wl_egl_window
*window
= native_window
;
238 struct dri2_egl_surface
*dri2_surf
;
240 const __DRIconfig
*config
;
242 dri2_surf
= calloc(1, sizeof *dri2_surf
);
244 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
248 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
252 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
253 dri2_surf
->base
.GLColorspace
);
254 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
255 assert(visual_idx
!= -1);
257 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
258 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
260 assert(dri2_dpy
->wl_shm
);
261 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
264 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
265 if (!dri2_surf
->wl_queue
) {
266 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
270 if (dri2_dpy
->wl_drm
) {
271 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
272 if (!dri2_surf
->wl_drm_wrapper
) {
273 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
276 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
277 dri2_surf
->wl_queue
);
280 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
281 if (!dri2_surf
->wl_dpy_wrapper
) {
282 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
285 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
286 dri2_surf
->wl_queue
);
288 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
289 if (!dri2_surf
->wl_surface_wrapper
) {
290 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
291 goto cleanup_dpy_wrapper
;
293 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
294 dri2_surf
->wl_queue
);
296 dri2_surf
->wl_win
= window
;
297 dri2_surf
->wl_win
->private = dri2_surf
;
298 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
300 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
302 if (dri2_dpy
->image_driver
)
303 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
304 else if (dri2_dpy
->dri2
)
305 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
307 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
309 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
311 if (dri2_surf
->dri_drawable
== NULL
) {
312 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
313 goto cleanup_surf_wrapper
;
316 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
318 return &dri2_surf
->base
;
320 cleanup_surf_wrapper
:
321 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
323 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
325 if (dri2_surf
->wl_drm_wrapper
)
326 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
328 wl_event_queue_destroy(dri2_surf
->wl_queue
);
336 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
337 _EGLConfig
*conf
, void *native_window
,
338 const EGLint
*attrib_list
)
340 /* From the EGL_EXT_platform_wayland spec, version 3:
342 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
343 * that belongs to Wayland. Any such call fails and generates
346 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
352 * Called via eglDestroySurface(), drv->API.DestroySurface().
355 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
357 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
358 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
362 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
364 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
365 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
366 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
367 if (dri2_surf
->color_buffers
[i
].dri_image
)
368 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
369 if (dri2_surf
->color_buffers
[i
].linear_copy
)
370 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
371 if (dri2_surf
->color_buffers
[i
].data
)
372 munmap(dri2_surf
->color_buffers
[i
].data
,
373 dri2_surf
->color_buffers
[i
].data_size
);
377 dri2_egl_surface_free_local_buffers(dri2_surf
);
379 if (dri2_surf
->throttle_callback
)
380 wl_callback_destroy(dri2_surf
->throttle_callback
);
382 if (dri2_surf
->wl_win
) {
383 dri2_surf
->wl_win
->private = NULL
;
384 dri2_surf
->wl_win
->resize_callback
= NULL
;
385 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
388 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
389 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
390 if (dri2_surf
->wl_drm_wrapper
)
391 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
392 wl_event_queue_destroy(dri2_surf
->wl_queue
);
394 dri2_fini_surface(surf
);
401 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
403 struct dri2_egl_display
*dri2_dpy
=
404 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
406 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
407 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
408 !dri2_surf
->color_buffers
[i
].locked
)
409 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
410 if (dri2_surf
->color_buffers
[i
].dri_image
)
411 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
412 if (dri2_surf
->color_buffers
[i
].linear_copy
)
413 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
414 if (dri2_surf
->color_buffers
[i
].data
)
415 munmap(dri2_surf
->color_buffers
[i
].data
,
416 dri2_surf
->color_buffers
[i
].data_size
);
418 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
419 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
420 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
421 dri2_surf
->color_buffers
[i
].data
= NULL
;
422 dri2_surf
->color_buffers
[i
].locked
= false;
426 dri2_egl_surface_free_local_buffers(dri2_surf
);
430 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
432 struct dri2_egl_display
*dri2_dpy
=
433 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
436 unsigned int dri_image_format
;
440 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
441 assert(visual_idx
!= -1);
442 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
444 /* currently supports five WL DRM formats,
445 * WL_DRM_FORMAT_ARGB2101010, WL_DRM_FORMAT_XRGB2101010,
446 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
447 * and WL_DRM_FORMAT_RGB565
449 switch (dri2_surf
->format
) {
450 case WL_DRM_FORMAT_ARGB2101010
:
451 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb2101010
);
452 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb2101010
);
454 case WL_DRM_FORMAT_XRGB2101010
:
455 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
456 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
458 case WL_DRM_FORMAT_ARGB8888
:
459 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
460 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
462 case WL_DRM_FORMAT_XRGB8888
:
463 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
464 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
466 case WL_DRM_FORMAT_RGB565
:
467 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
468 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
471 /* format is not supported */
475 /* There might be a buffer release already queued that wasn't processed */
476 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
478 while (dri2_surf
->back
== NULL
) {
479 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
480 /* Get an unlocked buffer, preferrably one with a dri_buffer
481 * already allocated. */
482 if (dri2_surf
->color_buffers
[i
].locked
)
484 if (dri2_surf
->back
== NULL
)
485 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
486 else if (dri2_surf
->back
->dri_image
== NULL
)
487 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
493 /* If we don't have a buffer, then block on the server to release one for
494 * us, and try again. wl_display_dispatch_queue will process any pending
495 * events, however not all servers flush on issuing a buffer release
496 * event. So, we spam the server with roundtrips as they always cause a
499 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
500 dri2_surf
->wl_queue
) < 0)
504 if (dri2_surf
->back
== NULL
)
507 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
509 if (dri2_dpy
->is_different_gpu
&&
510 dri2_surf
->back
->linear_copy
== NULL
) {
511 /* The LINEAR modifier should be a perfect alias of the LINEAR use
512 * flag; try the new interface first before the old, then fall back. */
513 if (dri2_dpy
->image
->base
.version
>= 15 &&
514 dri2_dpy
->image
->createImageWithModifiers
) {
515 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
517 dri2_surf
->back
->linear_copy
=
518 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
519 dri2_surf
->base
.Width
,
520 dri2_surf
->base
.Height
,
526 dri2_surf
->back
->linear_copy
=
527 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
528 dri2_surf
->base
.Width
,
529 dri2_surf
->base
.Height
,
532 __DRI_IMAGE_USE_LINEAR
,
535 if (dri2_surf
->back
->linear_copy
== NULL
)
539 if (dri2_surf
->back
->dri_image
== NULL
) {
540 /* If our DRIImage implementation does not support
541 * createImageWithModifiers, then fall back to the old createImage,
542 * and hope it allocates an image which is acceptable to the winsys.
544 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
545 dri2_dpy
->image
->createImageWithModifiers
) {
546 dri2_surf
->back
->dri_image
=
547 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
548 dri2_surf
->base
.Width
,
549 dri2_surf
->base
.Height
,
555 dri2_surf
->back
->dri_image
=
556 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
557 dri2_surf
->base
.Width
,
558 dri2_surf
->base
.Height
,
560 dri2_dpy
->is_different_gpu
?
565 dri2_surf
->back
->age
= 0;
567 if (dri2_surf
->back
->dri_image
== NULL
)
570 dri2_surf
->back
->locked
= true;
577 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
579 struct dri2_egl_display
*dri2_dpy
=
580 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
584 image
= dri2_surf
->back
->dri_image
;
586 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
587 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
589 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
591 buffer
->pitch
= pitch
;
597 update_buffers(struct dri2_egl_surface
*dri2_surf
)
599 struct dri2_egl_display
*dri2_dpy
=
600 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
602 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
603 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
605 dri2_wl_release_buffers(dri2_surf
);
607 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
608 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
609 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
610 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
613 if (get_back_bo(dri2_surf
) < 0) {
614 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
618 /* If we have an extra unlocked buffer at this point, we had to do triple
619 * buffering for a while, but now can go back to just double buffering.
620 * That means we can free any unlocked buffer now. */
621 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
622 if (!dri2_surf
->color_buffers
[i
].locked
&&
623 dri2_surf
->color_buffers
[i
].wl_buffer
) {
624 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
625 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
626 if (dri2_dpy
->is_different_gpu
)
627 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
628 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
629 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
630 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
638 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
639 int *width
, int *height
,
640 unsigned int *attachments
, int count
,
641 int *out_count
, void *loaderPrivate
)
643 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
646 if (update_buffers(dri2_surf
) < 0)
649 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
652 switch (attachments
[i
]) {
653 case __DRI_BUFFER_BACK_LEFT
:
654 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
657 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
661 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
664 dri2_surf
->buffers
[j
] = *local
;
673 *width
= dri2_surf
->base
.Width
;
674 *height
= dri2_surf
->base
.Height
;
676 return dri2_surf
->buffers
;
680 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
681 int *width
, int *height
,
682 unsigned int *attachments
, int count
,
683 int *out_count
, void *loaderPrivate
)
685 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
686 unsigned int *attachments_with_format
;
690 switch (dri2_surf
->format
) {
691 case WL_DRM_FORMAT_ARGB2101010
:
692 case WL_DRM_FORMAT_XRGB2101010
:
693 case WL_DRM_FORMAT_ARGB8888
:
694 case WL_DRM_FORMAT_XRGB8888
:
697 case WL_DRM_FORMAT_RGB565
:
701 /* format is not supported */
705 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
706 if (!attachments_with_format
) {
711 for (int i
= 0; i
< count
; ++i
) {
712 attachments_with_format
[2*i
] = attachments
[i
];
713 attachments_with_format
[2*i
+ 1] = bpp
;
717 dri2_wl_get_buffers_with_format(driDrawable
,
719 attachments_with_format
, count
,
720 out_count
, loaderPrivate
);
722 free(attachments_with_format
);
728 image_get_buffers(__DRIdrawable
*driDrawable
,
732 uint32_t buffer_mask
,
733 struct __DRIimageList
*buffers
)
735 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
737 if (update_buffers(dri2_surf
) < 0)
740 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
741 buffers
->back
= dri2_surf
->back
->dri_image
;
747 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
750 (void) loaderPrivate
;
753 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
754 .base
= { __DRI_DRI2_LOADER
, 3 },
756 .getBuffers
= dri2_wl_get_buffers
,
757 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
758 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
761 static const __DRIimageLoaderExtension image_loader_extension
= {
762 .base
= { __DRI_IMAGE_LOADER
, 1 },
764 .getBuffers
= image_get_buffers
,
765 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
769 wayland_throttle_callback(void *data
,
770 struct wl_callback
*callback
,
773 struct dri2_egl_surface
*dri2_surf
= data
;
775 dri2_surf
->throttle_callback
= NULL
;
776 wl_callback_destroy(callback
);
779 static const struct wl_callback_listener throttle_listener
= {
780 .done
= wayland_throttle_callback
784 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
785 __DRIimage
*image
, int *fourcc
)
791 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
796 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
801 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
802 if (visual_idx
== -1)
805 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
809 static struct wl_buffer
*
810 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
811 struct dri2_egl_surface
*dri2_surf
,
814 struct wl_buffer
*ret
;
816 int width
, height
, fourcc
, num_planes
;
817 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
819 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
820 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
822 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
826 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
831 if (dri2_dpy
->image
->base
.version
>= 15) {
834 query
= dri2_dpy
->image
->queryImage(image
,
835 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
837 query
&= dri2_dpy
->image
->queryImage(image
,
838 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
841 modifier
= (uint64_t) mod_hi
<< 32;
842 modifier
|= (uint64_t) (mod_lo
& 0xffffffff);
846 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
847 struct zwp_linux_buffer_params_v1
*params
;
850 /* We don't need a wrapper for wl_dmabuf objects, because we have to
851 * create the intermediate params object; we can set the queue on this,
852 * and the wl_buffer inherits it race-free. */
853 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
855 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
857 for (i
= 0; i
< num_planes
; i
++) {
865 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
867 zwp_linux_buffer_params_v1_destroy(params
);
871 query
= dri2_dpy
->image
->queryImage(p_image
,
872 __DRI_IMAGE_ATTRIB_FD
,
874 query
&= dri2_dpy
->image
->queryImage(p_image
,
875 __DRI_IMAGE_ATTRIB_STRIDE
,
877 query
&= dri2_dpy
->image
->queryImage(p_image
,
878 __DRI_IMAGE_ATTRIB_OFFSET
,
880 if (image
!= p_image
)
881 dri2_dpy
->image
->destroyImage(p_image
);
886 zwp_linux_buffer_params_v1_destroy(params
);
890 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
891 modifier
>> 32, modifier
& 0xffffffff);
895 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
897 zwp_linux_buffer_params_v1_destroy(params
);
898 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
899 struct wl_drm
*wl_drm
=
900 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
906 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
907 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
908 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
912 struct wl_drm
*wl_drm
=
913 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
919 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
920 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
921 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
928 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
932 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
933 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
936 for (int i
= 0; i
< n_rects
; i
++) {
937 const int *rect
= &rects
[i
* 4];
939 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
941 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
947 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
950 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
956 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
957 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
959 while (dri2_surf
->throttle_callback
!= NULL
)
960 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
961 dri2_surf
->wl_queue
) == -1)
964 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
965 if (dri2_surf
->color_buffers
[i
].age
> 0)
966 dri2_surf
->color_buffers
[i
].age
++;
968 /* Make sure we have a back buffer in case we're swapping without ever
970 if (get_back_bo(dri2_surf
) < 0)
971 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
973 if (draw
->SwapInterval
> 0) {
974 dri2_surf
->throttle_callback
=
975 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
976 wl_callback_add_listener(dri2_surf
->throttle_callback
,
977 &throttle_listener
, dri2_surf
);
980 dri2_surf
->back
->age
= 1;
981 dri2_surf
->current
= dri2_surf
->back
;
982 dri2_surf
->back
= NULL
;
984 if (!dri2_surf
->current
->wl_buffer
) {
987 if (dri2_dpy
->is_different_gpu
)
988 image
= dri2_surf
->current
->linear_copy
;
990 image
= dri2_surf
->current
->dri_image
;
992 dri2_surf
->current
->wl_buffer
=
993 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
995 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
996 &wl_buffer_listener
, dri2_surf
);
999 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1000 dri2_surf
->current
->wl_buffer
,
1001 dri2_surf
->dx
, dri2_surf
->dy
);
1003 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1004 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1005 /* reset resize growing parameters */
1009 /* If the compositor doesn't support damage_buffer, we deliberately
1010 * ignore the damage region and post maximum damage, due to
1011 * https://bugs.freedesktop.org/78190 */
1012 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1013 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1014 0, 0, INT32_MAX
, INT32_MAX
);
1016 if (dri2_dpy
->is_different_gpu
) {
1017 _EGLContext
*ctx
= _eglGetCurrentContext();
1018 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1019 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1020 dri2_surf
->current
->linear_copy
,
1021 dri2_surf
->current
->dri_image
,
1022 0, 0, dri2_surf
->base
.Width
,
1023 dri2_surf
->base
.Height
,
1024 0, 0, dri2_surf
->base
.Width
,
1025 dri2_surf
->base
.Height
, 0);
1028 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1029 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1031 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1033 /* If we're not waiting for a frame callback then we'll at least throttle
1034 * to a sync callback so that we always give a chance for the compositor to
1035 * handle the commit and send a release event before checking for a free
1037 if (dri2_surf
->throttle_callback
== NULL
) {
1038 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1039 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1040 &throttle_listener
, dri2_surf
);
1043 wl_display_flush(dri2_dpy
->wl_dpy
);
1049 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1050 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1052 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1054 if (get_back_bo(dri2_surf
) < 0) {
1055 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1059 return dri2_surf
->back
->age
;
1063 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1065 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1068 static struct wl_buffer
*
1069 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1073 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1074 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1075 __DRIimage
*image
= dri2_img
->dri_image
;
1076 struct wl_buffer
*buffer
;
1077 int format
, visual_idx
;
1079 /* Check the upstream display supports this buffer's format. */
1080 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1081 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1082 if (visual_idx
== -1)
1085 if (!(dri2_dpy
->formats
& dri2_wl_visuals
[visual_idx
].has_format
))
1088 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1090 /* The buffer object will have been created with our internal event queue
1091 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1092 * buffer to be used by the application so we'll reset it to the display's
1093 * default event queue. This isn't actually racy, as the only event the
1094 * buffer can get is a buffer release, which doesn't happen with an explicit
1097 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1102 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1107 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1109 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1112 if (dri2_dpy
->is_render_node
) {
1113 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1114 "authenticate for render-nodes");
1117 dri2_dpy
->authenticated
= false;
1119 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1120 if (roundtrip(dri2_dpy
) < 0)
1123 if (!dri2_dpy
->authenticated
)
1126 /* reset authenticated */
1127 dri2_dpy
->authenticated
= true;
1133 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1135 struct dri2_egl_display
*dri2_dpy
= data
;
1138 dri2_dpy
->device_name
= strdup(device
);
1139 if (!dri2_dpy
->device_name
)
1142 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1143 if (dri2_dpy
->fd
== -1) {
1144 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1145 dri2_dpy
->device_name
, strerror(errno
));
1149 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1150 dri2_dpy
->authenticated
= true;
1152 drmGetMagic(dri2_dpy
->fd
, &magic
);
1153 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1158 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1160 struct dri2_egl_display
*dri2_dpy
= data
;
1161 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1163 if (visual_idx
== -1)
1166 dri2_dpy
->formats
|= dri2_wl_visuals
[visual_idx
].has_format
;
1170 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1172 struct dri2_egl_display
*dri2_dpy
= data
;
1174 dri2_dpy
->capabilities
= value
;
1178 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1180 struct dri2_egl_display
*dri2_dpy
= data
;
1182 dri2_dpy
->authenticated
= true;
1185 static const struct wl_drm_listener drm_listener
= {
1186 .device
= drm_handle_device
,
1187 .format
= drm_handle_format
,
1188 .authenticated
= drm_handle_authenticated
,
1189 .capabilities
= drm_handle_capabilities
1193 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1196 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1200 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1201 uint32_t format
, uint32_t modifier_hi
,
1202 uint32_t modifier_lo
)
1204 struct dri2_egl_display
*dri2_dpy
= data
;
1205 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1206 uint64_t *mod
= NULL
;
1208 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1209 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1213 case WL_DRM_FORMAT_ARGB2101010
:
1214 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb2101010
);
1216 case WL_DRM_FORMAT_XRGB2101010
:
1217 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
1219 case WL_DRM_FORMAT_ARGB8888
:
1220 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1222 case WL_DRM_FORMAT_XRGB8888
:
1223 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1225 case WL_DRM_FORMAT_RGB565
:
1226 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1235 dri2_dpy
->formats
|= dri2_wl_visuals
[visual_idx
].has_format
;
1236 *mod
= (uint64_t) modifier_hi
<< 32;
1237 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1240 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1241 .format
= dmabuf_ignore_format
,
1242 .modifier
= dmabuf_handle_modifier
,
1246 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1247 uint32_t name
, const char *interface
,
1250 struct dri2_egl_display
*dri2_dpy
= data
;
1252 if (strcmp(interface
, "wl_drm") == 0) {
1254 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1255 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1256 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1257 dri2_dpy
->wl_dmabuf
=
1258 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1260 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1266 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1271 static const struct wl_registry_listener registry_listener_drm
= {
1272 .global
= registry_handle_global_drm
,
1273 .global_remove
= registry_handle_global_remove
1277 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1279 /* We can't use values greater than 1 on Wayland because we are using the
1280 * frame callback to synchronise the frame and the only way we be sure to
1281 * get a frame callback is to attach a new buffer. Therefore we can't just
1282 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1284 dri2_setup_swap_interval(disp
, 1);
1287 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1288 .authenticate
= dri2_wl_authenticate
,
1289 .create_window_surface
= dri2_wl_create_window_surface
,
1290 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1291 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1292 .destroy_surface
= dri2_wl_destroy_surface
,
1293 .create_image
= dri2_create_image_khr
,
1294 .swap_buffers
= dri2_wl_swap_buffers
,
1295 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1296 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1297 .set_damage_region
= dri2_fallback_set_damage_region
,
1298 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1299 .copy_buffers
= dri2_fallback_copy_buffers
,
1300 .query_buffer_age
= dri2_wl_query_buffer_age
,
1301 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1302 .get_sync_values
= dri2_fallback_get_sync_values
,
1303 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1306 static const __DRIextension
*dri2_loader_extensions
[] = {
1307 &dri2_loader_extension
.base
,
1308 &image_loader_extension
.base
,
1309 &image_lookup_extension
.base
,
1310 &use_invalidate
.base
,
1314 static const __DRIextension
*image_loader_extensions
[] = {
1315 &image_loader_extension
.base
,
1316 &image_lookup_extension
.base
,
1317 &use_invalidate
.base
,
1322 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1324 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1325 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1326 unsigned int count
= 0;
1328 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1329 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1330 struct dri2_egl_config
*dri2_conf
;
1332 if (!(dri2_dpy
->formats
& dri2_wl_visuals
[j
].has_format
))
1335 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1336 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1338 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1345 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1346 if (!format_count
[i
]) {
1347 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1348 dri2_wl_visuals
[i
].format_name
);
1352 return (count
!= 0);
1356 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1358 struct dri2_egl_display
*dri2_dpy
;
1360 loader_set_logger(_eglLog
);
1362 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1364 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1367 disp
->DriverData
= (void *) dri2_dpy
;
1368 if (disp
->PlatformDisplay
== NULL
) {
1369 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1370 if (dri2_dpy
->wl_dpy
== NULL
)
1372 dri2_dpy
->own_device
= true;
1374 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1377 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb2101010
, sizeof(uint64_t), 32) ||
1378 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb2101010
, sizeof(uint64_t), 32) ||
1379 !u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1380 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1381 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1385 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1387 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1388 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1391 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1392 dri2_dpy
->wl_queue
);
1394 if (dri2_dpy
->own_device
)
1395 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1397 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1398 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1399 ®istry_listener_drm
, dri2_dpy
);
1400 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1403 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1406 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1409 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1410 &dri2_dpy
->is_different_gpu
);
1411 if (dri2_dpy
->is_different_gpu
) {
1412 free(dri2_dpy
->device_name
);
1413 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1414 if (!dri2_dpy
->device_name
) {
1415 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1416 "for requested GPU");
1421 /* we have to do the check now, because loader_get_user_preferred_fd
1422 * will return a render-node when the requested gpu is different
1423 * to the server, but also if the client asks for the same gpu than
1424 * the server by requesting its pci-id */
1425 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1427 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1428 if (dri2_dpy
->driver_name
== NULL
) {
1429 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1433 /* render nodes cannot use Gem names, and thus do not support
1434 * the __DRI_DRI2_LOADER extension */
1435 if (!dri2_dpy
->is_render_node
) {
1436 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1437 if (!dri2_load_driver(disp
)) {
1438 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1442 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1443 if (!dri2_load_driver_dri3(disp
)) {
1444 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1449 if (!dri2_create_screen(disp
))
1452 if (!dri2_setup_extensions(disp
))
1455 dri2_setup_screen(disp
);
1457 dri2_wl_setup_swap_interval(disp
);
1459 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1460 * createImageFromFds support indicates that Prime export/import
1461 * is supported by the driver. Fall back to
1462 * gem names if we don't have Prime support. */
1464 if (dri2_dpy
->image
->base
.version
< 7 ||
1465 dri2_dpy
->image
->createImageFromFds
== NULL
)
1466 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1468 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1469 * The server needs to accept them */
1470 if (dri2_dpy
->is_render_node
&&
1471 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1472 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1476 if (dri2_dpy
->is_different_gpu
&&
1477 (dri2_dpy
->image
->base
.version
< 9 ||
1478 dri2_dpy
->image
->blitImage
== NULL
)) {
1479 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1480 "Image extension in the driver is not "
1481 "compatible. Version 9 or later and blitImage() "
1486 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1487 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1491 dri2_set_WL_bind_wayland_display(drv
, disp
);
1492 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1493 * because the buffer of the EGLImage has likely a tiling mode the server
1494 * gpu won't support. These is no way to check for now. Thus do not support the
1496 if (!dri2_dpy
->is_different_gpu
)
1497 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1499 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1501 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1503 /* Fill vtbl last to prevent accidentally calling virtual function during
1506 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1511 dri2_display_destroy(disp
);
1516 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1518 if (format
== WL_SHM_FORMAT_RGB565
)
1520 else /* ARGB8888 || XRGB8888 || ARGB2101010 || XRGB2101010 */
1525 * Taken from weston shared/os-compatibility.c
1528 #ifndef HAVE_MKOSTEMP
1531 set_cloexec_or_close(int fd
)
1538 flags
= fcntl(fd
, F_GETFD
);
1542 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1555 * Taken from weston shared/os-compatibility.c
1559 create_tmpfile_cloexec(char *tmpname
)
1563 #ifdef HAVE_MKOSTEMP
1564 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1568 fd
= mkstemp(tmpname
);
1570 fd
= set_cloexec_or_close(fd
);
1579 * Taken from weston shared/os-compatibility.c
1581 * Create a new, unique, anonymous file of the given size, and
1582 * return the file descriptor for it. The file descriptor is set
1583 * CLOEXEC. The file is immediately suitable for mmap()'ing
1584 * the given size at offset zero.
1586 * The file should not have a permanent backing store like a disk,
1587 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1589 * The file name is deleted from the file system.
1591 * The file is suitable for buffer sharing between processes by
1592 * transmitting the file descriptor over Unix sockets using the
1593 * SCM_RIGHTS methods.
1595 * If the C library implements posix_fallocate(), it is used to
1596 * guarantee that disk space is available for the file at the
1597 * given size. If disk space is insufficent, errno is set to ENOSPC.
1598 * If posix_fallocate() is not supported, program may receive
1599 * SIGBUS on accessing mmap()'ed file contents instead.
1602 os_create_anonymous_file(off_t size
)
1604 static const char templ
[] = "/mesa-shared-XXXXXX";
1610 path
= getenv("XDG_RUNTIME_DIR");
1616 name
= malloc(strlen(path
) + sizeof(templ
));
1621 strcat(name
, templ
);
1623 fd
= create_tmpfile_cloexec(name
);
1630 ret
= ftruncate(fd
, size
);
1641 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1642 int format
, int w
, int h
,
1643 void **data
, int *size
,
1644 struct wl_buffer
**buffer
)
1646 struct dri2_egl_display
*dri2_dpy
=
1647 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1648 struct wl_shm_pool
*pool
;
1649 int fd
, stride
, size_map
;
1652 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1653 size_map
= h
* stride
;
1655 /* Create a sharable buffer */
1656 fd
= os_create_anonymous_file(size_map
);
1660 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1661 if (data_map
== MAP_FAILED
) {
1666 /* Share it in a wl_buffer */
1667 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1668 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1669 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1670 wl_shm_pool_destroy(pool
);
1679 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1681 struct dri2_egl_display
*dri2_dpy
=
1682 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1684 /* we need to do the following operations only once per frame */
1685 if (dri2_surf
->back
)
1688 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1689 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1691 dri2_wl_release_buffers(dri2_surf
);
1693 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1694 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1695 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1696 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1697 dri2_surf
->current
= NULL
;
1700 /* find back buffer */
1702 /* There might be a buffer release already queued that wasn't processed */
1703 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1705 /* try get free buffer already created */
1706 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1707 if (!dri2_surf
->color_buffers
[i
].locked
&&
1708 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1709 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1714 /* else choose any another free location */
1715 if (!dri2_surf
->back
) {
1716 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1717 if (!dri2_surf
->color_buffers
[i
].locked
) {
1718 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1719 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1721 dri2_surf
->base
.Width
,
1722 dri2_surf
->base
.Height
,
1723 &dri2_surf
->back
->data
,
1724 &dri2_surf
->back
->data_size
,
1725 &dri2_surf
->back
->wl_buffer
)) {
1726 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1729 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1730 &wl_buffer_listener
, dri2_surf
);
1736 if (!dri2_surf
->back
) {
1737 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1741 dri2_surf
->back
->locked
= true;
1743 /* If we have an extra unlocked buffer at this point, we had to do triple
1744 * buffering for a while, but now can go back to just double buffering.
1745 * That means we can free any unlocked buffer now. */
1746 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1747 if (!dri2_surf
->color_buffers
[i
].locked
&&
1748 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1749 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1750 munmap(dri2_surf
->color_buffers
[i
].data
,
1751 dri2_surf
->color_buffers
[i
].data_size
);
1752 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1753 dri2_surf
->color_buffers
[i
].data
= NULL
;
1761 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1763 /* if there has been a resize: */
1764 if (!dri2_surf
->current
)
1767 return dri2_surf
->current
->data
;
1771 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1773 assert(dri2_surf
->back
);
1774 return dri2_surf
->back
->data
;
1778 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1780 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1782 while (dri2_surf
->throttle_callback
!= NULL
)
1783 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1784 dri2_surf
->wl_queue
) == -1)
1787 if (dri2_surf
->base
.SwapInterval
> 0) {
1788 dri2_surf
->throttle_callback
=
1789 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1790 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1791 &throttle_listener
, dri2_surf
);
1794 dri2_surf
->current
= dri2_surf
->back
;
1795 dri2_surf
->back
= NULL
;
1797 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1798 dri2_surf
->current
->wl_buffer
,
1799 dri2_surf
->dx
, dri2_surf
->dy
);
1801 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1802 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1803 /* reset resize growing parameters */
1807 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1808 0, 0, INT32_MAX
, INT32_MAX
);
1809 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1811 /* If we're not waiting for a frame callback then we'll at least throttle
1812 * to a sync callback so that we always give a chance for the compositor to
1813 * handle the commit and send a release event before checking for a free
1815 if (dri2_surf
->throttle_callback
== NULL
) {
1816 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1817 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1818 &throttle_listener
, dri2_surf
);
1821 wl_display_flush(dri2_dpy
->wl_dpy
);
1825 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1826 int *x
, int *y
, int *w
, int *h
,
1827 void *loaderPrivate
)
1829 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1831 (void) swrast_update_buffers(dri2_surf
);
1834 *w
= dri2_surf
->base
.Width
;
1835 *h
= dri2_surf
->base
.Height
;
1839 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1840 int x
, int y
, int w
, int h
,
1841 char *data
, void *loaderPrivate
)
1843 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1844 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1845 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1846 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1847 int dst_stride
= copy_width
;
1850 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1852 memset(data
, 0, copy_width
* h
);
1856 assert(data
!= src
);
1857 assert(copy_width
<= src_stride
);
1860 src
+= y
* src_stride
;
1863 if (copy_width
> src_stride
-x_offset
)
1864 copy_width
= src_stride
-x_offset
;
1865 if (h
> dri2_surf
->base
.Height
-y
)
1866 h
= dri2_surf
->base
.Height
-y
;
1869 memcpy(dst
, src
, copy_width
);
1876 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1877 int x
, int y
, int w
, int h
, int stride
,
1878 char *data
, void *loaderPrivate
)
1880 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1881 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1882 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1883 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1886 assert(copy_width
<= stride
);
1888 (void) swrast_update_buffers(dri2_surf
);
1889 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1891 /* partial copy, copy old content */
1892 if (copy_width
< dst_stride
)
1893 dri2_wl_swrast_get_image(draw
, 0, 0,
1894 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1895 dst
, loaderPrivate
);
1898 dst
+= y
* dst_stride
;
1902 /* drivers expect we do these checks (and some rely on it) */
1903 if (copy_width
> dst_stride
-x_offset
)
1904 copy_width
= dst_stride
-x_offset
;
1905 if (h
> dri2_surf
->base
.Height
-y
)
1906 h
= dri2_surf
->base
.Height
-y
;
1909 memcpy(dst
, src
, copy_width
);
1913 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1917 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1918 int x
, int y
, int w
, int h
,
1919 char *data
, void *loaderPrivate
)
1921 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1924 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1925 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1926 stride
, data
, loaderPrivate
);
1930 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1932 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1933 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1935 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1940 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1942 struct dri2_egl_display
*dri2_dpy
= data
;
1943 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1945 if (visual_idx
== -1)
1948 dri2_dpy
->formats
|= dri2_wl_visuals
[visual_idx
].has_format
;
1951 static const struct wl_shm_listener shm_listener
= {
1952 .format
= shm_handle_format
1956 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1957 uint32_t name
, const char *interface
,
1960 struct dri2_egl_display
*dri2_dpy
= data
;
1962 if (strcmp(interface
, "wl_shm") == 0) {
1964 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1965 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1969 static const struct wl_registry_listener registry_listener_swrast
= {
1970 .global
= registry_handle_global_swrast
,
1971 .global_remove
= registry_handle_global_remove
1974 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1975 .authenticate
= NULL
,
1976 .create_window_surface
= dri2_wl_create_window_surface
,
1977 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1978 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1979 .destroy_surface
= dri2_wl_destroy_surface
,
1980 .create_image
= dri2_create_image_khr
,
1981 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1982 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1983 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1984 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1985 .copy_buffers
= dri2_fallback_copy_buffers
,
1986 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1987 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1988 .get_sync_values
= dri2_fallback_get_sync_values
,
1989 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1992 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1993 .base
= { __DRI_SWRAST_LOADER
, 2 },
1995 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1996 .putImage
= dri2_wl_swrast_put_image
,
1997 .getImage
= dri2_wl_swrast_get_image
,
1998 .putImage2
= dri2_wl_swrast_put_image2
,
2001 static const __DRIextension
*swrast_loader_extensions
[] = {
2002 &swrast_loader_extension
.base
,
2003 &image_lookup_extension
.base
,
2008 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2010 struct dri2_egl_display
*dri2_dpy
;
2012 loader_set_logger(_eglLog
);
2014 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2016 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2019 disp
->DriverData
= (void *) dri2_dpy
;
2020 if (disp
->PlatformDisplay
== NULL
) {
2021 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2022 if (dri2_dpy
->wl_dpy
== NULL
)
2024 dri2_dpy
->own_device
= true;
2026 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2029 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2031 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2032 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2035 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2036 dri2_dpy
->wl_queue
);
2038 if (dri2_dpy
->own_device
)
2039 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2041 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2042 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2043 ®istry_listener_swrast
, dri2_dpy
);
2045 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2048 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
2051 dri2_dpy
->driver_name
= strdup("swrast");
2052 if (!dri2_load_driver_swrast(disp
))
2055 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2057 if (!dri2_create_screen(disp
))
2060 if (!dri2_setup_extensions(disp
))
2063 dri2_setup_screen(disp
);
2065 dri2_wl_setup_swap_interval(disp
);
2067 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2068 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2072 /* Fill vtbl last to prevent accidentally calling virtual function during
2075 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2080 dri2_display_destroy(disp
);
2085 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2087 EGLBoolean initialized
= EGL_FALSE
;
2089 if (!disp
->Options
.ForceSoftware
)
2090 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2093 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2100 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2102 if (dri2_dpy
->wl_drm
)
2103 wl_drm_destroy(dri2_dpy
->wl_drm
);
2104 if (dri2_dpy
->wl_dmabuf
)
2105 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2106 if (dri2_dpy
->wl_shm
)
2107 wl_shm_destroy(dri2_dpy
->wl_shm
);
2108 if (dri2_dpy
->wl_registry
)
2109 wl_registry_destroy(dri2_dpy
->wl_registry
);
2110 if (dri2_dpy
->wl_queue
)
2111 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2112 if (dri2_dpy
->wl_dpy_wrapper
)
2113 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2114 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb2101010
);
2115 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
2116 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb8888
);
2117 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb8888
);
2118 u_vector_finish(&dri2_dpy
->wl_modifiers
.rgb565
);
2119 if (dri2_dpy
->own_device
)
2120 wl_display_disconnect(dri2_dpy
->wl_dpy
);