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 (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 roundtrip(struct dri2_egl_display
*dri2_dpy
)
136 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
140 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
142 struct dri2_egl_surface
*dri2_surf
= data
;
145 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
146 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
149 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
150 wl_buffer_destroy(buffer
);
154 dri2_surf
->color_buffers
[i
].locked
= false;
157 static const struct wl_buffer_listener wl_buffer_listener
= {
158 .release
= wl_buffer_release
162 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
164 struct dri2_egl_surface
*dri2_surf
= data
;
165 struct dri2_egl_display
*dri2_dpy
=
166 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
168 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
172 destroy_window_callback(void *data
)
174 struct dri2_egl_surface
*dri2_surf
= data
;
175 dri2_surf
->wl_win
= NULL
;
178 static struct wl_surface
*
179 get_wl_surface_proxy(struct wl_egl_window
*window
)
181 /* Version 3 of wl_egl_window introduced a version field at the same
182 * location where a pointer to wl_surface was stored. Thus, if
183 * window->version is dereferencable, we've been given an older version of
184 * wl_egl_window, and window->version points to wl_surface */
185 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
186 return wl_proxy_create_wrapper((void *)(window
->version
));
188 return wl_proxy_create_wrapper(window
->surface
);
192 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
195 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
196 _EGLConfig
*conf
, void *native_window
,
197 const EGLint
*attrib_list
)
199 __DRIcreateNewDrawableFunc createNewDrawable
;
200 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
201 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
202 struct wl_egl_window
*window
= native_window
;
203 struct dri2_egl_surface
*dri2_surf
;
205 const __DRIconfig
*config
;
207 dri2_surf
= calloc(1, sizeof *dri2_surf
);
209 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
213 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
217 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
218 dri2_surf
->base
.GLColorspace
);
219 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
220 assert(visual_idx
!= -1);
222 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
223 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
225 assert(dri2_dpy
->wl_shm
);
226 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
229 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
230 if (!dri2_surf
->wl_queue
) {
231 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
235 if (dri2_dpy
->wl_drm
) {
236 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
237 if (!dri2_surf
->wl_drm_wrapper
) {
238 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
241 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
242 dri2_surf
->wl_queue
);
245 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
246 if (!dri2_surf
->wl_dpy_wrapper
) {
247 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
250 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
251 dri2_surf
->wl_queue
);
253 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
254 if (!dri2_surf
->wl_surface_wrapper
) {
255 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
256 goto cleanup_dpy_wrapper
;
258 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
259 dri2_surf
->wl_queue
);
261 dri2_surf
->wl_win
= window
;
262 dri2_surf
->wl_win
->private = dri2_surf
;
263 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
265 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
267 if (dri2_dpy
->image_driver
)
268 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
269 else if (dri2_dpy
->dri2
)
270 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
272 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
274 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
276 if (dri2_surf
->dri_drawable
== NULL
) {
277 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
278 goto cleanup_surf_wrapper
;
281 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
283 return &dri2_surf
->base
;
285 cleanup_surf_wrapper
:
286 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
288 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
290 if (dri2_surf
->wl_drm_wrapper
)
291 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
293 wl_event_queue_destroy(dri2_surf
->wl_queue
);
301 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
302 _EGLConfig
*conf
, void *native_window
,
303 const EGLint
*attrib_list
)
305 /* From the EGL_EXT_platform_wayland spec, version 3:
307 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
308 * that belongs to Wayland. Any such call fails and generates
311 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
317 * Called via eglDestroySurface(), drv->API.DestroySurface().
320 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
322 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
323 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
327 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
329 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
330 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
331 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
332 if (dri2_surf
->color_buffers
[i
].dri_image
)
333 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
334 if (dri2_surf
->color_buffers
[i
].linear_copy
)
335 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
336 if (dri2_surf
->color_buffers
[i
].data
)
337 munmap(dri2_surf
->color_buffers
[i
].data
,
338 dri2_surf
->color_buffers
[i
].data_size
);
342 dri2_egl_surface_free_local_buffers(dri2_surf
);
344 if (dri2_surf
->throttle_callback
)
345 wl_callback_destroy(dri2_surf
->throttle_callback
);
347 if (dri2_surf
->wl_win
) {
348 dri2_surf
->wl_win
->private = NULL
;
349 dri2_surf
->wl_win
->resize_callback
= NULL
;
350 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
353 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
354 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
355 if (dri2_surf
->wl_drm_wrapper
)
356 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
357 wl_event_queue_destroy(dri2_surf
->wl_queue
);
359 dri2_fini_surface(surf
);
366 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
368 struct dri2_egl_display
*dri2_dpy
=
369 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
371 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
372 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
373 !dri2_surf
->color_buffers
[i
].locked
)
374 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
375 if (dri2_surf
->color_buffers
[i
].dri_image
)
376 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
377 if (dri2_surf
->color_buffers
[i
].linear_copy
)
378 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
379 if (dri2_surf
->color_buffers
[i
].data
)
380 munmap(dri2_surf
->color_buffers
[i
].data
,
381 dri2_surf
->color_buffers
[i
].data_size
);
383 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
384 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
385 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
386 dri2_surf
->color_buffers
[i
].data
= NULL
;
387 dri2_surf
->color_buffers
[i
].locked
= false;
391 dri2_egl_surface_free_local_buffers(dri2_surf
);
395 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
397 struct dri2_egl_display
*dri2_dpy
=
398 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
400 unsigned int dri_image_format
;
404 /* currently supports five WL DRM formats,
405 * WL_DRM_FORMAT_ARGB2101010, WL_DRM_FORMAT_XRGB2101010,
406 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
407 * and WL_DRM_FORMAT_RGB565
409 switch (dri2_surf
->format
) {
410 case WL_DRM_FORMAT_ARGB2101010
:
411 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
412 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb2101010
);
413 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb2101010
);
415 case WL_DRM_FORMAT_XRGB2101010
:
416 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
417 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
418 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
420 case WL_DRM_FORMAT_ARGB8888
:
421 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
422 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
423 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
425 case WL_DRM_FORMAT_XRGB8888
:
426 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
427 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
428 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
430 case WL_DRM_FORMAT_RGB565
:
431 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
432 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
433 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
436 /* format is not supported */
440 /* There might be a buffer release already queued that wasn't processed */
441 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
443 while (dri2_surf
->back
== NULL
) {
444 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
445 /* Get an unlocked buffer, preferrably one with a dri_buffer
446 * already allocated. */
447 if (dri2_surf
->color_buffers
[i
].locked
)
449 if (dri2_surf
->back
== NULL
)
450 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
451 else if (dri2_surf
->back
->dri_image
== NULL
)
452 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
458 /* If we don't have a buffer, then block on the server to release one for
459 * us, and try again. wl_display_dispatch_queue will process any pending
460 * events, however not all servers flush on issuing a buffer release
461 * event. So, we spam the server with roundtrips as they always cause a
464 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
465 dri2_surf
->wl_queue
) < 0)
469 if (dri2_surf
->back
== NULL
)
472 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
474 if (dri2_dpy
->is_different_gpu
&&
475 dri2_surf
->back
->linear_copy
== NULL
) {
476 /* The LINEAR modifier should be a perfect alias of the LINEAR use
477 * flag; try the new interface first before the old, then fall back. */
478 if (dri2_dpy
->image
->base
.version
>= 15 &&
479 dri2_dpy
->image
->createImageWithModifiers
) {
480 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
482 dri2_surf
->back
->linear_copy
=
483 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
484 dri2_surf
->base
.Width
,
485 dri2_surf
->base
.Height
,
491 dri2_surf
->back
->linear_copy
=
492 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
493 dri2_surf
->base
.Width
,
494 dri2_surf
->base
.Height
,
497 __DRI_IMAGE_USE_LINEAR
,
500 if (dri2_surf
->back
->linear_copy
== NULL
)
504 if (dri2_surf
->back
->dri_image
== NULL
) {
505 /* If our DRIImage implementation does not support
506 * createImageWithModifiers, then fall back to the old createImage,
507 * and hope it allocates an image which is acceptable to the winsys.
509 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
510 dri2_dpy
->image
->createImageWithModifiers
) {
511 dri2_surf
->back
->dri_image
=
512 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
513 dri2_surf
->base
.Width
,
514 dri2_surf
->base
.Height
,
520 dri2_surf
->back
->dri_image
=
521 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
522 dri2_surf
->base
.Width
,
523 dri2_surf
->base
.Height
,
525 dri2_dpy
->is_different_gpu
?
530 dri2_surf
->back
->age
= 0;
532 if (dri2_surf
->back
->dri_image
== NULL
)
535 dri2_surf
->back
->locked
= true;
542 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
544 struct dri2_egl_display
*dri2_dpy
=
545 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
549 image
= dri2_surf
->back
->dri_image
;
551 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
552 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
554 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
556 buffer
->pitch
= pitch
;
562 update_buffers(struct dri2_egl_surface
*dri2_surf
)
564 struct dri2_egl_display
*dri2_dpy
=
565 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
567 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
568 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
570 dri2_wl_release_buffers(dri2_surf
);
572 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
573 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
574 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
575 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
578 if (get_back_bo(dri2_surf
) < 0) {
579 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
583 /* If we have an extra unlocked buffer at this point, we had to do triple
584 * buffering for a while, but now can go back to just double buffering.
585 * That means we can free any unlocked buffer now. */
586 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
587 if (!dri2_surf
->color_buffers
[i
].locked
&&
588 dri2_surf
->color_buffers
[i
].wl_buffer
) {
589 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
590 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
591 if (dri2_dpy
->is_different_gpu
)
592 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
593 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
594 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
595 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
603 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
604 int *width
, int *height
,
605 unsigned int *attachments
, int count
,
606 int *out_count
, void *loaderPrivate
)
608 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
611 if (update_buffers(dri2_surf
) < 0)
614 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
617 switch (attachments
[i
]) {
618 case __DRI_BUFFER_BACK_LEFT
:
619 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
622 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
626 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
629 dri2_surf
->buffers
[j
] = *local
;
638 *width
= dri2_surf
->base
.Width
;
639 *height
= dri2_surf
->base
.Height
;
641 return dri2_surf
->buffers
;
645 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
646 int *width
, int *height
,
647 unsigned int *attachments
, int count
,
648 int *out_count
, void *loaderPrivate
)
650 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
651 unsigned int *attachments_with_format
;
655 switch (dri2_surf
->format
) {
656 case WL_DRM_FORMAT_ARGB2101010
:
657 case WL_DRM_FORMAT_XRGB2101010
:
658 case WL_DRM_FORMAT_ARGB8888
:
659 case WL_DRM_FORMAT_XRGB8888
:
662 case WL_DRM_FORMAT_RGB565
:
666 /* format is not supported */
670 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
671 if (!attachments_with_format
) {
676 for (int i
= 0; i
< count
; ++i
) {
677 attachments_with_format
[2*i
] = attachments
[i
];
678 attachments_with_format
[2*i
+ 1] = bpp
;
682 dri2_wl_get_buffers_with_format(driDrawable
,
684 attachments_with_format
, count
,
685 out_count
, loaderPrivate
);
687 free(attachments_with_format
);
693 image_get_buffers(__DRIdrawable
*driDrawable
,
697 uint32_t buffer_mask
,
698 struct __DRIimageList
*buffers
)
700 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
702 if (update_buffers(dri2_surf
) < 0)
705 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
706 buffers
->back
= dri2_surf
->back
->dri_image
;
712 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
715 (void) loaderPrivate
;
718 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
719 .base
= { __DRI_DRI2_LOADER
, 3 },
721 .getBuffers
= dri2_wl_get_buffers
,
722 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
723 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
726 static const __DRIimageLoaderExtension image_loader_extension
= {
727 .base
= { __DRI_IMAGE_LOADER
, 1 },
729 .getBuffers
= image_get_buffers
,
730 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
734 wayland_throttle_callback(void *data
,
735 struct wl_callback
*callback
,
738 struct dri2_egl_surface
*dri2_surf
= data
;
740 dri2_surf
->throttle_callback
= NULL
;
741 wl_callback_destroy(callback
);
744 static const struct wl_callback_listener throttle_listener
= {
745 .done
= wayland_throttle_callback
749 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
750 __DRIimage
*image
, int *fourcc
)
755 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
760 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
765 switch (dri_format
) {
766 case __DRI_IMAGE_FORMAT_ARGB8888
:
767 *fourcc
= __DRI_IMAGE_FOURCC_ARGB8888
;
769 case __DRI_IMAGE_FORMAT_XRGB8888
:
770 *fourcc
= __DRI_IMAGE_FOURCC_XRGB8888
;
777 static struct wl_buffer
*
778 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
779 struct dri2_egl_surface
*dri2_surf
,
782 struct wl_buffer
*ret
;
784 int width
, height
, fourcc
, num_planes
;
785 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
787 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
788 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
790 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
794 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
799 if (dri2_dpy
->image
->base
.version
>= 15) {
802 query
= dri2_dpy
->image
->queryImage(image
,
803 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
805 query
&= dri2_dpy
->image
->queryImage(image
,
806 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
809 modifier
= (uint64_t) mod_hi
<< 32;
810 modifier
|= (uint64_t) (mod_lo
& 0xffffffff);
814 if (dri2_dpy
->wl_dmabuf
&& modifier
!= DRM_FORMAT_MOD_INVALID
) {
815 struct zwp_linux_buffer_params_v1
*params
;
818 /* We don't need a wrapper for wl_dmabuf objects, because we have to
819 * create the intermediate params object; we can set the queue on this,
820 * and the wl_buffer inherits it race-free. */
821 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
823 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
825 for (i
= 0; i
< num_planes
; i
++) {
833 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
835 zwp_linux_buffer_params_v1_destroy(params
);
839 query
= dri2_dpy
->image
->queryImage(p_image
,
840 __DRI_IMAGE_ATTRIB_FD
,
842 query
&= dri2_dpy
->image
->queryImage(p_image
,
843 __DRI_IMAGE_ATTRIB_STRIDE
,
845 query
&= dri2_dpy
->image
->queryImage(p_image
,
846 __DRI_IMAGE_ATTRIB_OFFSET
,
848 if (image
!= p_image
)
849 dri2_dpy
->image
->destroyImage(p_image
);
854 zwp_linux_buffer_params_v1_destroy(params
);
858 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
859 modifier
>> 32, modifier
& 0xffffffff);
863 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
865 zwp_linux_buffer_params_v1_destroy(params
);
866 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
867 struct wl_drm
*wl_drm
=
868 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
874 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
875 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
876 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
880 struct wl_drm
*wl_drm
=
881 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
887 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
888 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
889 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
896 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
900 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
901 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
904 for (int i
= 0; i
< n_rects
; i
++) {
905 const int *rect
= &rects
[i
* 4];
907 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
909 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
915 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
918 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
924 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
925 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
927 while (dri2_surf
->throttle_callback
!= NULL
)
928 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
929 dri2_surf
->wl_queue
) == -1)
932 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
933 if (dri2_surf
->color_buffers
[i
].age
> 0)
934 dri2_surf
->color_buffers
[i
].age
++;
936 /* Make sure we have a back buffer in case we're swapping without ever
938 if (get_back_bo(dri2_surf
) < 0)
939 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
941 if (draw
->SwapInterval
> 0) {
942 dri2_surf
->throttle_callback
=
943 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
944 wl_callback_add_listener(dri2_surf
->throttle_callback
,
945 &throttle_listener
, dri2_surf
);
948 dri2_surf
->back
->age
= 1;
949 dri2_surf
->current
= dri2_surf
->back
;
950 dri2_surf
->back
= NULL
;
952 if (!dri2_surf
->current
->wl_buffer
) {
955 if (dri2_dpy
->is_different_gpu
)
956 image
= dri2_surf
->current
->linear_copy
;
958 image
= dri2_surf
->current
->dri_image
;
960 dri2_surf
->current
->wl_buffer
=
961 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
963 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
964 &wl_buffer_listener
, dri2_surf
);
967 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
968 dri2_surf
->current
->wl_buffer
,
969 dri2_surf
->dx
, dri2_surf
->dy
);
971 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
972 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
973 /* reset resize growing parameters */
977 /* If the compositor doesn't support damage_buffer, we deliberately
978 * ignore the damage region and post maximum damage, due to
979 * https://bugs.freedesktop.org/78190 */
980 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
981 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
982 0, 0, INT32_MAX
, INT32_MAX
);
984 if (dri2_dpy
->is_different_gpu
) {
985 _EGLContext
*ctx
= _eglGetCurrentContext();
986 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
987 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
988 dri2_surf
->current
->linear_copy
,
989 dri2_surf
->current
->dri_image
,
990 0, 0, dri2_surf
->base
.Width
,
991 dri2_surf
->base
.Height
,
992 0, 0, dri2_surf
->base
.Width
,
993 dri2_surf
->base
.Height
, 0);
996 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
997 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
999 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1001 /* If we're not waiting for a frame callback then we'll at least throttle
1002 * to a sync callback so that we always give a chance for the compositor to
1003 * handle the commit and send a release event before checking for a free
1005 if (dri2_surf
->throttle_callback
== NULL
) {
1006 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1007 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1008 &throttle_listener
, dri2_surf
);
1011 wl_display_flush(dri2_dpy
->wl_dpy
);
1017 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1018 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1020 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1022 if (get_back_bo(dri2_surf
) < 0) {
1023 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1027 return dri2_surf
->back
->age
;
1031 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1033 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1036 static struct wl_buffer
*
1037 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1041 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1042 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1043 __DRIimage
*image
= dri2_img
->dri_image
;
1044 struct wl_buffer
*buffer
;
1047 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1049 case __DRI_IMAGE_FORMAT_ARGB2101010
:
1050 if (!(dri2_dpy
->formats
& HAS_ARGB2101010
))
1053 case __DRI_IMAGE_FORMAT_XRGB2101010
:
1054 if (!(dri2_dpy
->formats
& HAS_XRGB2101010
))
1057 case __DRI_IMAGE_FORMAT_ARGB8888
:
1058 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
1061 case __DRI_IMAGE_FORMAT_XRGB8888
:
1062 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
1069 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1071 /* The buffer object will have been created with our internal event queue
1072 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1073 * buffer to be used by the application so we'll reset it to the display's
1074 * default event queue. This isn't actually racy, as the only event the
1075 * buffer can get is a buffer release, which doesn't happen with an explicit
1078 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1083 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1088 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1090 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1093 if (dri2_dpy
->is_render_node
) {
1094 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1095 "authenticate for render-nodes");
1098 dri2_dpy
->authenticated
= false;
1100 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1101 if (roundtrip(dri2_dpy
) < 0)
1104 if (!dri2_dpy
->authenticated
)
1107 /* reset authenticated */
1108 dri2_dpy
->authenticated
= true;
1114 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1116 struct dri2_egl_display
*dri2_dpy
= data
;
1119 dri2_dpy
->device_name
= strdup(device
);
1120 if (!dri2_dpy
->device_name
)
1123 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1124 if (dri2_dpy
->fd
== -1) {
1125 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1126 dri2_dpy
->device_name
, strerror(errno
));
1130 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1131 dri2_dpy
->authenticated
= true;
1133 drmGetMagic(dri2_dpy
->fd
, &magic
);
1134 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1139 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1141 struct dri2_egl_display
*dri2_dpy
= data
;
1144 case WL_DRM_FORMAT_ARGB2101010
:
1145 dri2_dpy
->formats
|= HAS_ARGB2101010
;
1147 case WL_DRM_FORMAT_XRGB2101010
:
1148 dri2_dpy
->formats
|= HAS_XRGB2101010
;
1150 case WL_DRM_FORMAT_ARGB8888
:
1151 dri2_dpy
->formats
|= HAS_ARGB8888
;
1153 case WL_DRM_FORMAT_XRGB8888
:
1154 dri2_dpy
->formats
|= HAS_XRGB8888
;
1156 case WL_DRM_FORMAT_RGB565
:
1157 dri2_dpy
->formats
|= HAS_RGB565
;
1163 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1165 struct dri2_egl_display
*dri2_dpy
= data
;
1167 dri2_dpy
->capabilities
= value
;
1171 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1173 struct dri2_egl_display
*dri2_dpy
= data
;
1175 dri2_dpy
->authenticated
= true;
1178 static const struct wl_drm_listener drm_listener
= {
1179 .device
= drm_handle_device
,
1180 .format
= drm_handle_format
,
1181 .authenticated
= drm_handle_authenticated
,
1182 .capabilities
= drm_handle_capabilities
1186 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1189 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1193 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1194 uint32_t format
, uint32_t modifier_hi
,
1195 uint32_t modifier_lo
)
1197 struct dri2_egl_display
*dri2_dpy
= data
;
1198 uint64_t *mod
= NULL
;
1200 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1201 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1205 case WL_DRM_FORMAT_ARGB2101010
:
1206 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb2101010
);
1207 dri2_dpy
->formats
|= HAS_ARGB2101010
;
1209 case WL_DRM_FORMAT_XRGB2101010
:
1210 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
1211 dri2_dpy
->formats
|= HAS_XRGB2101010
;
1213 case WL_DRM_FORMAT_ARGB8888
:
1214 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1215 dri2_dpy
->formats
|= HAS_ARGB8888
;
1217 case WL_DRM_FORMAT_XRGB8888
:
1218 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1219 dri2_dpy
->formats
|= HAS_XRGB8888
;
1221 case WL_DRM_FORMAT_RGB565
:
1222 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1223 dri2_dpy
->formats
|= HAS_RGB565
;
1232 *mod
= (uint64_t) modifier_hi
<< 32;
1233 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1236 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1237 .format
= dmabuf_ignore_format
,
1238 .modifier
= dmabuf_handle_modifier
,
1242 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1243 uint32_t name
, const char *interface
,
1246 struct dri2_egl_display
*dri2_dpy
= data
;
1248 if (strcmp(interface
, "wl_drm") == 0) {
1250 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1251 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1252 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1253 dri2_dpy
->wl_dmabuf
=
1254 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1256 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1262 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1267 static const struct wl_registry_listener registry_listener_drm
= {
1268 .global
= registry_handle_global_drm
,
1269 .global_remove
= registry_handle_global_remove
1273 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1275 /* We can't use values greater than 1 on Wayland because we are using the
1276 * frame callback to synchronise the frame and the only way we be sure to
1277 * get a frame callback is to attach a new buffer. Therefore we can't just
1278 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1280 dri2_setup_swap_interval(disp
, 1);
1283 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1284 .authenticate
= dri2_wl_authenticate
,
1285 .create_window_surface
= dri2_wl_create_window_surface
,
1286 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1287 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1288 .destroy_surface
= dri2_wl_destroy_surface
,
1289 .create_image
= dri2_create_image_khr
,
1290 .swap_buffers
= dri2_wl_swap_buffers
,
1291 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1292 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1293 .set_damage_region
= dri2_fallback_set_damage_region
,
1294 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1295 .copy_buffers
= dri2_fallback_copy_buffers
,
1296 .query_buffer_age
= dri2_wl_query_buffer_age
,
1297 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1298 .get_sync_values
= dri2_fallback_get_sync_values
,
1299 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1302 static const __DRIextension
*dri2_loader_extensions
[] = {
1303 &dri2_loader_extension
.base
,
1304 &image_loader_extension
.base
,
1305 &image_lookup_extension
.base
,
1306 &use_invalidate
.base
,
1310 static const __DRIextension
*image_loader_extensions
[] = {
1311 &image_loader_extension
.base
,
1312 &image_lookup_extension
.base
,
1313 &use_invalidate
.base
,
1318 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1320 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1321 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1322 unsigned int count
= 0;
1324 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1325 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1326 struct dri2_egl_config
*dri2_conf
;
1328 if (!(dri2_dpy
->formats
& dri2_wl_visuals
[j
].has_format
))
1331 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1332 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_masks
);
1334 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1341 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1342 if (!format_count
[i
]) {
1343 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1344 dri2_wl_visuals
[i
].format_name
);
1348 return (count
!= 0);
1352 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1354 struct dri2_egl_display
*dri2_dpy
;
1356 loader_set_logger(_eglLog
);
1358 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1360 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1363 disp
->DriverData
= (void *) dri2_dpy
;
1364 if (disp
->PlatformDisplay
== NULL
) {
1365 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1366 if (dri2_dpy
->wl_dpy
== NULL
)
1368 dri2_dpy
->own_device
= true;
1370 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1373 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb2101010
, sizeof(uint64_t), 32) ||
1374 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb2101010
, sizeof(uint64_t), 32) ||
1375 !u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1376 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1377 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1381 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1383 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1384 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1387 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1388 dri2_dpy
->wl_queue
);
1390 if (dri2_dpy
->own_device
)
1391 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1393 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1394 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1395 ®istry_listener_drm
, dri2_dpy
);
1396 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1399 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1402 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1405 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1406 &dri2_dpy
->is_different_gpu
);
1407 if (dri2_dpy
->is_different_gpu
) {
1408 free(dri2_dpy
->device_name
);
1409 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1410 if (!dri2_dpy
->device_name
) {
1411 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1412 "for requested GPU");
1417 /* we have to do the check now, because loader_get_user_preferred_fd
1418 * will return a render-node when the requested gpu is different
1419 * to the server, but also if the client asks for the same gpu than
1420 * the server by requesting its pci-id */
1421 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1423 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1424 if (dri2_dpy
->driver_name
== NULL
) {
1425 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1429 /* render nodes cannot use Gem names, and thus do not support
1430 * the __DRI_DRI2_LOADER extension */
1431 if (!dri2_dpy
->is_render_node
) {
1432 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1433 if (!dri2_load_driver(disp
)) {
1434 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1438 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1439 if (!dri2_load_driver_dri3(disp
)) {
1440 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1445 if (!dri2_create_screen(disp
))
1448 if (!dri2_setup_extensions(disp
))
1451 dri2_setup_screen(disp
);
1453 dri2_wl_setup_swap_interval(disp
);
1455 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1456 * createImageFromFds support indicates that Prime export/import
1457 * is supported by the driver. Fall back to
1458 * gem names if we don't have Prime support. */
1460 if (dri2_dpy
->image
->base
.version
< 7 ||
1461 dri2_dpy
->image
->createImageFromFds
== NULL
)
1462 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1464 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1465 * The server needs to accept them */
1466 if (dri2_dpy
->is_render_node
&&
1467 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1468 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1472 if (dri2_dpy
->is_different_gpu
&&
1473 (dri2_dpy
->image
->base
.version
< 9 ||
1474 dri2_dpy
->image
->blitImage
== NULL
)) {
1475 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1476 "Image extension in the driver is not "
1477 "compatible. Version 9 or later and blitImage() "
1482 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1483 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1487 dri2_set_WL_bind_wayland_display(drv
, disp
);
1488 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1489 * because the buffer of the EGLImage has likely a tiling mode the server
1490 * gpu won't support. These is no way to check for now. Thus do not support the
1492 if (!dri2_dpy
->is_different_gpu
)
1493 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1495 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1497 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1499 /* Fill vtbl last to prevent accidentally calling virtual function during
1502 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1507 dri2_display_destroy(disp
);
1512 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1514 if (format
== WL_SHM_FORMAT_RGB565
)
1516 else /* ARGB8888 || XRGB8888 || ARGB2101010 || XRGB2101010 */
1521 * Taken from weston shared/os-compatibility.c
1524 #ifndef HAVE_MKOSTEMP
1527 set_cloexec_or_close(int fd
)
1534 flags
= fcntl(fd
, F_GETFD
);
1538 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1551 * Taken from weston shared/os-compatibility.c
1555 create_tmpfile_cloexec(char *tmpname
)
1559 #ifdef HAVE_MKOSTEMP
1560 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1564 fd
= mkstemp(tmpname
);
1566 fd
= set_cloexec_or_close(fd
);
1575 * Taken from weston shared/os-compatibility.c
1577 * Create a new, unique, anonymous file of the given size, and
1578 * return the file descriptor for it. The file descriptor is set
1579 * CLOEXEC. The file is immediately suitable for mmap()'ing
1580 * the given size at offset zero.
1582 * The file should not have a permanent backing store like a disk,
1583 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1585 * The file name is deleted from the file system.
1587 * The file is suitable for buffer sharing between processes by
1588 * transmitting the file descriptor over Unix sockets using the
1589 * SCM_RIGHTS methods.
1591 * If the C library implements posix_fallocate(), it is used to
1592 * guarantee that disk space is available for the file at the
1593 * given size. If disk space is insufficent, errno is set to ENOSPC.
1594 * If posix_fallocate() is not supported, program may receive
1595 * SIGBUS on accessing mmap()'ed file contents instead.
1598 os_create_anonymous_file(off_t size
)
1600 static const char templ
[] = "/mesa-shared-XXXXXX";
1606 path
= getenv("XDG_RUNTIME_DIR");
1612 name
= malloc(strlen(path
) + sizeof(templ
));
1617 strcat(name
, templ
);
1619 fd
= create_tmpfile_cloexec(name
);
1626 ret
= ftruncate(fd
, size
);
1637 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1638 int format
, int w
, int h
,
1639 void **data
, int *size
,
1640 struct wl_buffer
**buffer
)
1642 struct dri2_egl_display
*dri2_dpy
=
1643 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1644 struct wl_shm_pool
*pool
;
1645 int fd
, stride
, size_map
;
1648 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1649 size_map
= h
* stride
;
1651 /* Create a sharable buffer */
1652 fd
= os_create_anonymous_file(size_map
);
1656 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1657 if (data_map
== MAP_FAILED
) {
1662 /* Share it in a wl_buffer */
1663 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1664 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1665 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1666 wl_shm_pool_destroy(pool
);
1675 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1677 struct dri2_egl_display
*dri2_dpy
=
1678 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1680 /* we need to do the following operations only once per frame */
1681 if (dri2_surf
->back
)
1684 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1685 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1687 dri2_wl_release_buffers(dri2_surf
);
1689 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1690 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1691 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1692 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1693 dri2_surf
->current
= NULL
;
1696 /* find back buffer */
1698 /* There might be a buffer release already queued that wasn't processed */
1699 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1701 /* try get free buffer already created */
1702 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1703 if (!dri2_surf
->color_buffers
[i
].locked
&&
1704 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1705 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1710 /* else choose any another free location */
1711 if (!dri2_surf
->back
) {
1712 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1713 if (!dri2_surf
->color_buffers
[i
].locked
) {
1714 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1715 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1717 dri2_surf
->base
.Width
,
1718 dri2_surf
->base
.Height
,
1719 &dri2_surf
->back
->data
,
1720 &dri2_surf
->back
->data_size
,
1721 &dri2_surf
->back
->wl_buffer
)) {
1722 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1725 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1726 &wl_buffer_listener
, dri2_surf
);
1732 if (!dri2_surf
->back
) {
1733 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1737 dri2_surf
->back
->locked
= true;
1739 /* If we have an extra unlocked buffer at this point, we had to do triple
1740 * buffering for a while, but now can go back to just double buffering.
1741 * That means we can free any unlocked buffer now. */
1742 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1743 if (!dri2_surf
->color_buffers
[i
].locked
&&
1744 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1745 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1746 munmap(dri2_surf
->color_buffers
[i
].data
,
1747 dri2_surf
->color_buffers
[i
].data_size
);
1748 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1749 dri2_surf
->color_buffers
[i
].data
= NULL
;
1757 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1759 /* if there has been a resize: */
1760 if (!dri2_surf
->current
)
1763 return dri2_surf
->current
->data
;
1767 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1769 assert(dri2_surf
->back
);
1770 return dri2_surf
->back
->data
;
1774 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1776 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1778 while (dri2_surf
->throttle_callback
!= NULL
)
1779 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1780 dri2_surf
->wl_queue
) == -1)
1783 if (dri2_surf
->base
.SwapInterval
> 0) {
1784 dri2_surf
->throttle_callback
=
1785 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1786 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1787 &throttle_listener
, dri2_surf
);
1790 dri2_surf
->current
= dri2_surf
->back
;
1791 dri2_surf
->back
= NULL
;
1793 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1794 dri2_surf
->current
->wl_buffer
,
1795 dri2_surf
->dx
, dri2_surf
->dy
);
1797 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1798 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1799 /* reset resize growing parameters */
1803 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1804 0, 0, INT32_MAX
, INT32_MAX
);
1805 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1807 /* If we're not waiting for a frame callback then we'll at least throttle
1808 * to a sync callback so that we always give a chance for the compositor to
1809 * handle the commit and send a release event before checking for a free
1811 if (dri2_surf
->throttle_callback
== NULL
) {
1812 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1813 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1814 &throttle_listener
, dri2_surf
);
1817 wl_display_flush(dri2_dpy
->wl_dpy
);
1821 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1822 int *x
, int *y
, int *w
, int *h
,
1823 void *loaderPrivate
)
1825 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1827 (void) swrast_update_buffers(dri2_surf
);
1830 *w
= dri2_surf
->base
.Width
;
1831 *h
= dri2_surf
->base
.Height
;
1835 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1836 int x
, int y
, int w
, int h
,
1837 char *data
, void *loaderPrivate
)
1839 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1840 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1841 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1842 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1843 int dst_stride
= copy_width
;
1846 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1848 memset(data
, 0, copy_width
* h
);
1852 assert(data
!= src
);
1853 assert(copy_width
<= src_stride
);
1856 src
+= y
* src_stride
;
1859 if (copy_width
> src_stride
-x_offset
)
1860 copy_width
= src_stride
-x_offset
;
1861 if (h
> dri2_surf
->base
.Height
-y
)
1862 h
= dri2_surf
->base
.Height
-y
;
1865 memcpy(dst
, src
, copy_width
);
1872 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1873 int x
, int y
, int w
, int h
, int stride
,
1874 char *data
, void *loaderPrivate
)
1876 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1877 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1878 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1879 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1882 assert(copy_width
<= stride
);
1884 (void) swrast_update_buffers(dri2_surf
);
1885 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1887 /* partial copy, copy old content */
1888 if (copy_width
< dst_stride
)
1889 dri2_wl_swrast_get_image(draw
, 0, 0,
1890 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1891 dst
, loaderPrivate
);
1894 dst
+= y
* dst_stride
;
1898 /* drivers expect we do these checks (and some rely on it) */
1899 if (copy_width
> dst_stride
-x_offset
)
1900 copy_width
= dst_stride
-x_offset
;
1901 if (h
> dri2_surf
->base
.Height
-y
)
1902 h
= dri2_surf
->base
.Height
-y
;
1905 memcpy(dst
, src
, copy_width
);
1909 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1913 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1914 int x
, int y
, int w
, int h
,
1915 char *data
, void *loaderPrivate
)
1917 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1920 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1921 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1922 stride
, data
, loaderPrivate
);
1926 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1928 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1929 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1931 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1936 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1938 struct dri2_egl_display
*dri2_dpy
= data
;
1941 case WL_SHM_FORMAT_ARGB2101010
:
1942 dri2_dpy
->formats
|= HAS_ARGB2101010
;
1944 case WL_SHM_FORMAT_XRGB2101010
:
1945 dri2_dpy
->formats
|= HAS_XRGB2101010
;
1947 case WL_SHM_FORMAT_ARGB8888
:
1948 dri2_dpy
->formats
|= HAS_ARGB8888
;
1950 case WL_SHM_FORMAT_XRGB8888
:
1951 dri2_dpy
->formats
|= HAS_XRGB8888
;
1953 case WL_SHM_FORMAT_RGB565
:
1954 dri2_dpy
->formats
|= HAS_RGB565
;
1959 static const struct wl_shm_listener shm_listener
= {
1960 .format
= shm_handle_format
1964 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1965 uint32_t name
, const char *interface
,
1968 struct dri2_egl_display
*dri2_dpy
= data
;
1970 if (strcmp(interface
, "wl_shm") == 0) {
1972 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1973 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1977 static const struct wl_registry_listener registry_listener_swrast
= {
1978 .global
= registry_handle_global_swrast
,
1979 .global_remove
= registry_handle_global_remove
1982 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1983 .authenticate
= NULL
,
1984 .create_window_surface
= dri2_wl_create_window_surface
,
1985 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1986 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1987 .destroy_surface
= dri2_wl_destroy_surface
,
1988 .create_image
= dri2_create_image_khr
,
1989 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1990 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1991 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1992 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1993 .copy_buffers
= dri2_fallback_copy_buffers
,
1994 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1995 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1996 .get_sync_values
= dri2_fallback_get_sync_values
,
1997 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
2000 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
2001 .base
= { __DRI_SWRAST_LOADER
, 2 },
2003 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
2004 .putImage
= dri2_wl_swrast_put_image
,
2005 .getImage
= dri2_wl_swrast_get_image
,
2006 .putImage2
= dri2_wl_swrast_put_image2
,
2009 static const __DRIextension
*swrast_loader_extensions
[] = {
2010 &swrast_loader_extension
.base
,
2011 &image_lookup_extension
.base
,
2016 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2018 struct dri2_egl_display
*dri2_dpy
;
2020 loader_set_logger(_eglLog
);
2022 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2024 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2027 disp
->DriverData
= (void *) dri2_dpy
;
2028 if (disp
->PlatformDisplay
== NULL
) {
2029 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2030 if (dri2_dpy
->wl_dpy
== NULL
)
2032 dri2_dpy
->own_device
= true;
2034 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2037 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2039 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2040 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2043 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2044 dri2_dpy
->wl_queue
);
2046 if (dri2_dpy
->own_device
)
2047 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2049 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2050 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2051 ®istry_listener_swrast
, dri2_dpy
);
2053 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2056 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
2059 dri2_dpy
->driver_name
= strdup("swrast");
2060 if (!dri2_load_driver_swrast(disp
))
2063 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2065 if (!dri2_create_screen(disp
))
2068 if (!dri2_setup_extensions(disp
))
2071 dri2_setup_screen(disp
);
2073 dri2_wl_setup_swap_interval(disp
);
2075 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2076 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2080 /* Fill vtbl last to prevent accidentally calling virtual function during
2083 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2088 dri2_display_destroy(disp
);
2093 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2095 EGLBoolean initialized
= EGL_FALSE
;
2097 if (!disp
->Options
.ForceSoftware
)
2098 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2101 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2108 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2110 if (dri2_dpy
->wl_drm
)
2111 wl_drm_destroy(dri2_dpy
->wl_drm
);
2112 if (dri2_dpy
->wl_dmabuf
)
2113 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2114 if (dri2_dpy
->wl_shm
)
2115 wl_shm_destroy(dri2_dpy
->wl_shm
);
2116 if (dri2_dpy
->wl_registry
)
2117 wl_registry_destroy(dri2_dpy
->wl_registry
);
2118 if (dri2_dpy
->wl_queue
)
2119 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2120 if (dri2_dpy
->wl_dpy_wrapper
)
2121 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2122 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb2101010
);
2123 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb2101010
);
2124 u_vector_finish(&dri2_dpy
->wl_modifiers
.argb8888
);
2125 u_vector_finish(&dri2_dpy
->wl_modifiers
.xrgb8888
);
2126 u_vector_finish(&dri2_dpy
->wl_modifiers
.rgb565
);
2127 if (dri2_dpy
->own_device
)
2128 wl_display_disconnect(dri2_dpy
->wl_dpy
);