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
{
67 roundtrip(struct dri2_egl_display
*dri2_dpy
)
69 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
73 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
75 struct dri2_egl_surface
*dri2_surf
= data
;
78 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
79 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
82 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
83 wl_buffer_destroy(buffer
);
87 dri2_surf
->color_buffers
[i
].locked
= false;
90 static const struct wl_buffer_listener wl_buffer_listener
= {
91 .release
= wl_buffer_release
95 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
97 struct dri2_egl_surface
*dri2_surf
= data
;
98 struct dri2_egl_display
*dri2_dpy
=
99 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
101 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
105 destroy_window_callback(void *data
)
107 struct dri2_egl_surface
*dri2_surf
= data
;
108 dri2_surf
->wl_win
= NULL
;
111 static struct wl_surface
*
112 get_wl_surface_proxy(struct wl_egl_window
*window
)
114 /* Version 3 of wl_egl_window introduced a version field at the same
115 * location where a pointer to wl_surface was stored. Thus, if
116 * window->version is dereferencable, we've been given an older version of
117 * wl_egl_window, and window->version points to wl_surface */
118 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
119 return wl_proxy_create_wrapper((void *)(window
->version
));
121 return wl_proxy_create_wrapper(window
->surface
);
125 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
128 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
129 _EGLConfig
*conf
, void *native_window
,
130 const EGLint
*attrib_list
)
132 __DRIcreateNewDrawableFunc createNewDrawable
;
133 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
134 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
135 struct wl_egl_window
*window
= native_window
;
136 struct dri2_egl_surface
*dri2_surf
;
137 const __DRIconfig
*config
;
139 dri2_surf
= calloc(1, sizeof *dri2_surf
);
141 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
145 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
148 if (dri2_dpy
->wl_drm
) {
149 if (conf
->RedSize
== 5)
150 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
151 else if (conf
->AlphaSize
== 0)
152 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
154 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
156 assert(dri2_dpy
->wl_shm
);
157 if (conf
->RedSize
== 5)
158 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
159 else if (conf
->AlphaSize
== 0)
160 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
162 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
165 dri2_surf
->wl_win
= window
;
166 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
167 if (!dri2_surf
->wl_queue
) {
168 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
172 if (dri2_dpy
->wl_drm
) {
173 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
174 if (!dri2_surf
->wl_drm_wrapper
) {
175 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
178 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
179 dri2_surf
->wl_queue
);
182 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
183 if (!dri2_surf
->wl_dpy_wrapper
) {
184 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
187 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
188 dri2_surf
->wl_queue
);
190 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
191 if (!dri2_surf
->wl_surface_wrapper
) {
192 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
195 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
196 dri2_surf
->wl_queue
);
198 dri2_surf
->wl_win
->private = dri2_surf
;
199 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
201 dri2_surf
->base
.Width
= -1;
202 dri2_surf
->base
.Height
= -1;
204 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
205 dri2_surf
->base
.GLColorspace
);
208 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
210 if (dri2_dpy
->image_driver
)
211 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
212 else if (dri2_dpy
->dri2
)
213 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
215 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
217 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
219 if (dri2_surf
->dri_drawable
== NULL
) {
220 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
224 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
226 return &dri2_surf
->base
;
229 if (dri2_surf
->wl_drm_wrapper
)
230 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
232 wl_event_queue_destroy(dri2_surf
->wl_queue
);
240 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
241 _EGLConfig
*conf
, void *native_window
,
242 const EGLint
*attrib_list
)
244 /* From the EGL_EXT_platform_wayland spec, version 3:
246 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
247 * that belongs to Wayland. Any such call fails and generates
250 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
256 * Called via eglDestroySurface(), drv->API.DestroySurface().
259 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
261 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
262 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
266 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
268 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
269 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
270 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
271 if (dri2_surf
->color_buffers
[i
].dri_image
)
272 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
273 if (dri2_surf
->color_buffers
[i
].linear_copy
)
274 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
275 if (dri2_surf
->color_buffers
[i
].data
)
276 munmap(dri2_surf
->color_buffers
[i
].data
,
277 dri2_surf
->color_buffers
[i
].data_size
);
280 if (dri2_dpy
->dri2
) {
281 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
282 if (dri2_surf
->dri_buffers
[i
] &&
283 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
284 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
285 dri2_surf
->dri_buffers
[i
]);
288 if (dri2_surf
->throttle_callback
)
289 wl_callback_destroy(dri2_surf
->throttle_callback
);
291 if (dri2_surf
->wl_win
) {
292 dri2_surf
->wl_win
->private = NULL
;
293 dri2_surf
->wl_win
->resize_callback
= NULL
;
294 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
297 if (dri2_surf
->wl_drm_wrapper
)
298 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
299 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
300 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
301 wl_event_queue_destroy(dri2_surf
->wl_queue
);
309 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
311 struct dri2_egl_display
*dri2_dpy
=
312 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
314 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
315 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
316 !dri2_surf
->color_buffers
[i
].locked
)
317 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
318 if (dri2_surf
->color_buffers
[i
].dri_image
)
319 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
320 if (dri2_surf
->color_buffers
[i
].linear_copy
)
321 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
322 if (dri2_surf
->color_buffers
[i
].data
)
323 munmap(dri2_surf
->color_buffers
[i
].data
,
324 dri2_surf
->color_buffers
[i
].data_size
);
326 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
327 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
328 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
329 dri2_surf
->color_buffers
[i
].data
= NULL
;
330 dri2_surf
->color_buffers
[i
].locked
= false;
333 if (dri2_dpy
->dri2
) {
334 for (int i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
335 if (dri2_surf
->dri_buffers
[i
] &&
336 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
337 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
338 dri2_surf
->dri_buffers
[i
]);
343 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
345 struct dri2_egl_display
*dri2_dpy
=
346 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
348 unsigned int dri_image_format
;
352 /* currently supports three WL DRM formats,
353 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
354 * and WL_DRM_FORMAT_RGB565
356 switch (dri2_surf
->format
) {
357 case WL_DRM_FORMAT_ARGB8888
:
358 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
359 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.argb8888
);
360 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.argb8888
);
362 case WL_DRM_FORMAT_XRGB8888
:
363 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
364 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.xrgb8888
);
365 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.xrgb8888
);
367 case WL_DRM_FORMAT_RGB565
:
368 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
369 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
.rgb565
);
370 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
.rgb565
);
373 /* format is not supported */
377 /* There might be a buffer release already queued that wasn't processed */
378 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
380 while (dri2_surf
->back
== NULL
) {
381 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
382 /* Get an unlocked buffer, preferrably one with a dri_buffer
383 * already allocated. */
384 if (dri2_surf
->color_buffers
[i
].locked
)
386 if (dri2_surf
->back
== NULL
)
387 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
388 else if (dri2_surf
->back
->dri_image
== NULL
)
389 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
395 /* If we don't have a buffer, then block on the server to release one for
396 * us, and try again. */
397 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
401 if (dri2_surf
->back
== NULL
)
404 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
406 if (dri2_dpy
->is_different_gpu
&&
407 dri2_surf
->back
->linear_copy
== NULL
) {
408 /* The LINEAR modifier should be a perfect alias of the LINEAR use
409 * flag; try the new interface first before the old, then fall back. */
410 if (dri2_dpy
->image
->base
.version
>= 15 &&
411 dri2_dpy
->image
->createImageWithModifiers
) {
412 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
414 dri2_surf
->back
->linear_copy
=
415 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
416 dri2_surf
->base
.Width
,
417 dri2_surf
->base
.Height
,
423 dri2_surf
->back
->linear_copy
=
424 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
425 dri2_surf
->base
.Width
,
426 dri2_surf
->base
.Height
,
429 __DRI_IMAGE_USE_LINEAR
,
432 if (dri2_surf
->back
->linear_copy
== NULL
)
436 if (dri2_surf
->back
->dri_image
== NULL
) {
437 /* If our DRIImage implementation does not support
438 * createImageWithModifiers, then fall back to the old createImage,
439 * and hope it allocates an image which is acceptable to the winsys.
441 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
442 dri2_dpy
->image
->createImageWithModifiers
) {
443 dri2_surf
->back
->dri_image
=
444 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
445 dri2_surf
->base
.Width
,
446 dri2_surf
->base
.Height
,
452 dri2_surf
->back
->dri_image
=
453 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
454 dri2_surf
->base
.Width
,
455 dri2_surf
->base
.Height
,
457 dri2_dpy
->is_different_gpu
?
462 dri2_surf
->back
->age
= 0;
464 if (dri2_surf
->back
->dri_image
== NULL
)
467 dri2_surf
->back
->locked
= true;
474 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
476 struct dri2_egl_display
*dri2_dpy
=
477 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
481 image
= dri2_surf
->back
->dri_image
;
483 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
484 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
486 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
488 buffer
->pitch
= pitch
;
494 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
495 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
497 struct dri2_egl_display
*dri2_dpy
=
498 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
499 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
502 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
504 dri2_surf
->base
.Width
,
505 dri2_surf
->base
.Height
);
506 dri2_surf
->dri_buffers
[attachment
] = b
;
511 memcpy(buffer
, b
, sizeof *buffer
);
517 update_buffers(struct dri2_egl_surface
*dri2_surf
)
519 struct dri2_egl_display
*dri2_dpy
=
520 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
522 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
523 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
525 dri2_wl_release_buffers(dri2_surf
);
527 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
528 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
529 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
530 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
533 if (get_back_bo(dri2_surf
) < 0) {
534 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
538 /* If we have an extra unlocked buffer at this point, we had to do triple
539 * buffering for a while, but now can go back to just double buffering.
540 * That means we can free any unlocked buffer now. */
541 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
542 if (!dri2_surf
->color_buffers
[i
].locked
&&
543 dri2_surf
->color_buffers
[i
].wl_buffer
) {
544 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
545 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
546 if (dri2_dpy
->is_different_gpu
)
547 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
548 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
549 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
550 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
558 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
559 int *width
, int *height
,
560 unsigned int *attachments
, int count
,
561 int *out_count
, void *loaderPrivate
)
563 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
566 if (update_buffers(dri2_surf
) < 0)
569 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
570 switch (attachments
[i
]) {
571 case __DRI_BUFFER_BACK_LEFT
:
572 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
575 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
576 &dri2_surf
->buffers
[j
]) < 0) {
577 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
588 *width
= dri2_surf
->base
.Width
;
589 *height
= dri2_surf
->base
.Height
;
591 return dri2_surf
->buffers
;
595 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
596 int *width
, int *height
,
597 unsigned int *attachments
, int count
,
598 int *out_count
, void *loaderPrivate
)
600 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
601 unsigned int *attachments_with_format
;
605 switch (dri2_surf
->format
) {
606 case WL_DRM_FORMAT_ARGB8888
:
607 case WL_DRM_FORMAT_XRGB8888
:
610 case WL_DRM_FORMAT_RGB565
:
614 /* format is not supported */
618 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
619 if (!attachments_with_format
) {
624 for (int i
= 0; i
< count
; ++i
) {
625 attachments_with_format
[2*i
] = attachments
[i
];
626 attachments_with_format
[2*i
+ 1] = bpp
;
630 dri2_wl_get_buffers_with_format(driDrawable
,
632 attachments_with_format
, count
,
633 out_count
, loaderPrivate
);
635 free(attachments_with_format
);
641 image_get_buffers(__DRIdrawable
*driDrawable
,
645 uint32_t buffer_mask
,
646 struct __DRIimageList
*buffers
)
648 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
650 if (update_buffers(dri2_surf
) < 0)
653 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
654 buffers
->back
= dri2_surf
->back
->dri_image
;
660 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
663 (void) loaderPrivate
;
666 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
667 .base
= { __DRI_DRI2_LOADER
, 3 },
669 .getBuffers
= dri2_wl_get_buffers
,
670 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
671 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
674 static const __DRIimageLoaderExtension image_loader_extension
= {
675 .base
= { __DRI_IMAGE_LOADER
, 1 },
677 .getBuffers
= image_get_buffers
,
678 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
682 wayland_throttle_callback(void *data
,
683 struct wl_callback
*callback
,
686 struct dri2_egl_surface
*dri2_surf
= data
;
688 dri2_surf
->throttle_callback
= NULL
;
689 wl_callback_destroy(callback
);
692 static const struct wl_callback_listener throttle_listener
= {
693 .done
= wayland_throttle_callback
696 static struct wl_buffer
*
697 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
698 struct dri2_egl_surface
*dri2_surf
,
701 struct wl_buffer
*ret
;
702 int width
, height
, fourcc
, num_planes
;
704 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
705 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
706 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
);
707 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
710 if (dri2_dpy
->wl_dmabuf
&& dri2_dpy
->image
->base
.version
>= 15) {
711 struct zwp_linux_buffer_params_v1
*params
;
715 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
717 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
720 /* We don't need a wrapper for wl_dmabuf objects, because we have to
721 * create the intermediate params object; we can set the queue on this,
722 * and the wl_buffer inherits it race-free. */
723 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
725 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
727 for (i
= 0; i
< num_planes
; i
++) {
729 int stride
, offset
, fd
;
734 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
736 zwp_linux_buffer_params_v1_destroy(params
);
740 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
741 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_STRIDE
,
743 dri2_dpy
->image
->queryImage(p_image
, __DRI_IMAGE_ATTRIB_OFFSET
,
745 if (image
!= p_image
)
746 dri2_dpy
->image
->destroyImage(p_image
);
748 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
753 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
755 zwp_linux_buffer_params_v1_destroy(params
);
756 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
757 struct wl_drm
*wl_drm
=
758 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
764 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
765 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
766 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
770 struct wl_drm
*wl_drm
=
771 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
777 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
778 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
779 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
786 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
790 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
791 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
794 for (int i
= 0; i
< n_rects
; i
++) {
795 const int *rect
= &rects
[i
* 4];
797 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
799 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
805 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
808 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
814 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
815 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
817 while (dri2_surf
->throttle_callback
!= NULL
)
818 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
819 dri2_surf
->wl_queue
) == -1)
822 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
823 if (dri2_surf
->color_buffers
[i
].age
> 0)
824 dri2_surf
->color_buffers
[i
].age
++;
826 /* Make sure we have a back buffer in case we're swapping without ever
828 if (get_back_bo(dri2_surf
) < 0)
829 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
831 if (draw
->SwapInterval
> 0) {
832 dri2_surf
->throttle_callback
=
833 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
834 wl_callback_add_listener(dri2_surf
->throttle_callback
,
835 &throttle_listener
, dri2_surf
);
838 dri2_surf
->back
->age
= 1;
839 dri2_surf
->current
= dri2_surf
->back
;
840 dri2_surf
->back
= NULL
;
842 if (!dri2_surf
->current
->wl_buffer
) {
845 if (dri2_dpy
->is_different_gpu
)
846 image
= dri2_surf
->current
->linear_copy
;
848 image
= dri2_surf
->current
->dri_image
;
850 dri2_surf
->current
->wl_buffer
=
851 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
853 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
854 &wl_buffer_listener
, dri2_surf
);
857 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
858 dri2_surf
->current
->wl_buffer
,
859 dri2_surf
->dx
, dri2_surf
->dy
);
861 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
862 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
863 /* reset resize growing parameters */
867 /* If the compositor doesn't support damage_buffer, we deliberately
868 * ignore the damage region and post maximum damage, due to
869 * https://bugs.freedesktop.org/78190 */
870 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
871 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
872 0, 0, INT32_MAX
, INT32_MAX
);
874 if (dri2_dpy
->is_different_gpu
) {
875 _EGLContext
*ctx
= _eglGetCurrentContext();
876 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
877 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
878 dri2_surf
->current
->linear_copy
,
879 dri2_surf
->current
->dri_image
,
880 0, 0, dri2_surf
->base
.Width
,
881 dri2_surf
->base
.Height
,
882 0, 0, dri2_surf
->base
.Width
,
883 dri2_surf
->base
.Height
, 0);
886 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
887 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
889 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
891 /* If we're not waiting for a frame callback then we'll at least throttle
892 * to a sync callback so that we always give a chance for the compositor to
893 * handle the commit and send a release event before checking for a free
895 if (dri2_surf
->throttle_callback
== NULL
) {
896 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
897 wl_callback_add_listener(dri2_surf
->throttle_callback
,
898 &throttle_listener
, dri2_surf
);
901 wl_display_flush(dri2_dpy
->wl_dpy
);
907 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
908 _EGLDisplay
*disp
, _EGLSurface
*surface
)
910 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
912 if (get_back_bo(dri2_surf
) < 0) {
913 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
917 return dri2_surf
->back
->age
;
921 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
923 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
926 static struct wl_buffer
*
927 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
931 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
932 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
933 __DRIimage
*image
= dri2_img
->dri_image
;
934 struct wl_buffer
*buffer
;
937 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
939 case __DRI_IMAGE_FORMAT_ARGB8888
:
940 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
943 case __DRI_IMAGE_FORMAT_XRGB8888
:
944 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
951 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
953 /* The buffer object will have been created with our internal event queue
954 * because it is using the wl_drm object as a proxy factory. We want the
955 * buffer to be used by the application so we'll reset it to the display's
956 * default event queue. This isn't actually racy, as the only event the
957 * buffer can get is a buffer release, which doesn't happen with an explicit
960 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
965 _eglError(EGL_BAD_MATCH
, "unsupported image format");
970 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
972 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
975 if (dri2_dpy
->is_render_node
) {
976 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
977 "authenticate for render-nodes");
980 dri2_dpy
->authenticated
= false;
982 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
983 if (roundtrip(dri2_dpy
) < 0)
986 if (!dri2_dpy
->authenticated
)
989 /* reset authenticated */
990 dri2_dpy
->authenticated
= true;
996 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
998 struct dri2_egl_display
*dri2_dpy
= data
;
1001 dri2_dpy
->device_name
= strdup(device
);
1002 if (!dri2_dpy
->device_name
)
1005 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1006 if (dri2_dpy
->fd
== -1) {
1007 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1008 dri2_dpy
->device_name
, strerror(errno
));
1012 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1013 dri2_dpy
->authenticated
= true;
1015 drmGetMagic(dri2_dpy
->fd
, &magic
);
1016 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1021 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1023 struct dri2_egl_display
*dri2_dpy
= data
;
1026 case WL_DRM_FORMAT_ARGB8888
:
1027 dri2_dpy
->formats
|= HAS_ARGB8888
;
1029 case WL_DRM_FORMAT_XRGB8888
:
1030 dri2_dpy
->formats
|= HAS_XRGB8888
;
1032 case WL_DRM_FORMAT_RGB565
:
1033 dri2_dpy
->formats
|= HAS_RGB565
;
1039 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1041 struct dri2_egl_display
*dri2_dpy
= data
;
1043 dri2_dpy
->capabilities
= value
;
1047 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1049 struct dri2_egl_display
*dri2_dpy
= data
;
1051 dri2_dpy
->authenticated
= true;
1054 static const struct wl_drm_listener drm_listener
= {
1055 .device
= drm_handle_device
,
1056 .format
= drm_handle_format
,
1057 .authenticated
= drm_handle_authenticated
,
1058 .capabilities
= drm_handle_capabilities
1062 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1065 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1069 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1070 uint32_t format
, uint32_t modifier_hi
,
1071 uint32_t modifier_lo
)
1073 struct dri2_egl_display
*dri2_dpy
= data
;
1074 uint64_t *mod
= NULL
;
1076 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
1077 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
1081 case WL_DRM_FORMAT_ARGB8888
:
1082 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.argb8888
);
1084 case WL_DRM_FORMAT_XRGB8888
:
1085 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.xrgb8888
);
1087 case WL_DRM_FORMAT_RGB565
:
1088 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
.rgb565
);
1097 *mod
= (uint64_t) modifier_hi
<< 32;
1098 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
1101 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1102 .format
= dmabuf_ignore_format
,
1103 .modifier
= dmabuf_handle_modifier
,
1107 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1108 uint32_t name
, const char *interface
,
1111 struct dri2_egl_display
*dri2_dpy
= data
;
1113 if (strcmp(interface
, "wl_drm") == 0) {
1115 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1116 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1117 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1118 dri2_dpy
->wl_dmabuf
=
1119 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1121 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1127 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1132 static const struct wl_registry_listener registry_listener_drm
= {
1133 .global
= registry_handle_global_drm
,
1134 .global_remove
= registry_handle_global_remove
1138 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1140 /* We can't use values greater than 1 on Wayland because we are using the
1141 * frame callback to synchronise the frame and the only way we be sure to
1142 * get a frame callback is to attach a new buffer. Therefore we can't just
1143 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1145 dri2_setup_swap_interval(disp
, 1);
1148 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1149 .authenticate
= dri2_wl_authenticate
,
1150 .create_window_surface
= dri2_wl_create_window_surface
,
1151 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1152 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1153 .destroy_surface
= dri2_wl_destroy_surface
,
1154 .create_image
= dri2_create_image_khr
,
1155 .swap_buffers
= dri2_wl_swap_buffers
,
1156 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1157 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1158 .set_damage_region
= dri2_fallback_set_damage_region
,
1159 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1160 .copy_buffers
= dri2_fallback_copy_buffers
,
1161 .query_buffer_age
= dri2_wl_query_buffer_age
,
1162 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1163 .get_sync_values
= dri2_fallback_get_sync_values
,
1164 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1167 static const __DRIextension
*dri2_loader_extensions
[] = {
1168 &dri2_loader_extension
.base
,
1169 &image_loader_extension
.base
,
1170 &image_lookup_extension
.base
,
1171 &use_invalidate
.base
,
1175 static const __DRIextension
*image_loader_extensions
[] = {
1176 &image_loader_extension
.base
,
1177 &image_lookup_extension
.base
,
1178 &use_invalidate
.base
,
1183 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1185 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1186 static const struct {
1187 const char *format_name
;
1189 unsigned int rgba_masks
[4];
1191 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1192 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1193 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1195 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1196 unsigned int count
= 0;
1198 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1199 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1200 struct dri2_egl_config
*dri2_conf
;
1202 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1205 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1206 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1208 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1215 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1216 if (!format_count
[i
]) {
1217 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1218 visuals
[i
].format_name
);
1222 return (count
!= 0);
1226 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1228 struct dri2_egl_display
*dri2_dpy
;
1230 loader_set_logger(_eglLog
);
1232 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1234 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1237 disp
->DriverData
= (void *) dri2_dpy
;
1238 if (disp
->PlatformDisplay
== NULL
) {
1239 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1240 if (dri2_dpy
->wl_dpy
== NULL
)
1242 dri2_dpy
->own_device
= true;
1244 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1247 if (!u_vector_init(&dri2_dpy
->wl_modifiers
.xrgb8888
, sizeof(uint64_t), 32) ||
1248 !u_vector_init(&dri2_dpy
->wl_modifiers
.argb8888
, sizeof(uint64_t), 32) ||
1249 !u_vector_init(&dri2_dpy
->wl_modifiers
.rgb565
, sizeof(uint64_t), 32)) {
1253 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1255 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1256 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1259 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1260 dri2_dpy
->wl_queue
);
1262 if (dri2_dpy
->own_device
)
1263 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1265 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1266 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1267 ®istry_listener_drm
, dri2_dpy
);
1268 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1271 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1274 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1277 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1278 &dri2_dpy
->is_different_gpu
);
1279 if (dri2_dpy
->is_different_gpu
) {
1280 free(dri2_dpy
->device_name
);
1281 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1282 if (!dri2_dpy
->device_name
) {
1283 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1284 "for requested GPU");
1289 /* we have to do the check now, because loader_get_user_preferred_fd
1290 * will return a render-node when the requested gpu is different
1291 * to the server, but also if the client asks for the same gpu than
1292 * the server by requesting its pci-id */
1293 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1295 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1296 if (dri2_dpy
->driver_name
== NULL
) {
1297 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1301 /* render nodes cannot use Gem names, and thus do not support
1302 * the __DRI_DRI2_LOADER extension */
1303 if (!dri2_dpy
->is_render_node
) {
1304 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1305 if (!dri2_load_driver(disp
)) {
1306 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1310 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1311 if (!dri2_load_driver_dri3(disp
)) {
1312 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1317 if (!dri2_create_screen(disp
))
1320 if (!dri2_setup_extensions(disp
))
1323 dri2_setup_screen(disp
);
1325 dri2_wl_setup_swap_interval(disp
);
1327 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1328 * createImageFromFds support indicates that Prime export/import
1329 * is supported by the driver. Fall back to
1330 * gem names if we don't have Prime support. */
1332 if (dri2_dpy
->image
->base
.version
< 7 ||
1333 dri2_dpy
->image
->createImageFromFds
== NULL
)
1334 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1336 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1337 * The server needs to accept them */
1338 if (dri2_dpy
->is_render_node
&&
1339 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1340 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1344 if (dri2_dpy
->is_different_gpu
&&
1345 (dri2_dpy
->image
->base
.version
< 9 ||
1346 dri2_dpy
->image
->blitImage
== NULL
)) {
1347 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1348 "Image extension in the driver is not "
1349 "compatible. Version 9 or later and blitImage() "
1354 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1355 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1359 dri2_set_WL_bind_wayland_display(drv
, disp
);
1360 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1361 * because the buffer of the EGLImage has likely a tiling mode the server
1362 * gpu won't support. These is no way to check for now. Thus do not support the
1364 if (!dri2_dpy
->is_different_gpu
)
1365 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1367 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1369 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1371 /* Fill vtbl last to prevent accidentally calling virtual function during
1374 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1379 dri2_display_destroy(disp
);
1384 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1386 if (format
== WL_SHM_FORMAT_RGB565
)
1388 else /* ARGB8888 || XRGB8888 */
1393 * Taken from weston shared/os-compatibility.c
1396 #ifndef HAVE_MKOSTEMP
1399 set_cloexec_or_close(int fd
)
1406 flags
= fcntl(fd
, F_GETFD
);
1410 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1423 * Taken from weston shared/os-compatibility.c
1427 create_tmpfile_cloexec(char *tmpname
)
1431 #ifdef HAVE_MKOSTEMP
1432 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1436 fd
= mkstemp(tmpname
);
1438 fd
= set_cloexec_or_close(fd
);
1447 * Taken from weston shared/os-compatibility.c
1449 * Create a new, unique, anonymous file of the given size, and
1450 * return the file descriptor for it. The file descriptor is set
1451 * CLOEXEC. The file is immediately suitable for mmap()'ing
1452 * the given size at offset zero.
1454 * The file should not have a permanent backing store like a disk,
1455 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1457 * The file name is deleted from the file system.
1459 * The file is suitable for buffer sharing between processes by
1460 * transmitting the file descriptor over Unix sockets using the
1461 * SCM_RIGHTS methods.
1463 * If the C library implements posix_fallocate(), it is used to
1464 * guarantee that disk space is available for the file at the
1465 * given size. If disk space is insufficent, errno is set to ENOSPC.
1466 * If posix_fallocate() is not supported, program may receive
1467 * SIGBUS on accessing mmap()'ed file contents instead.
1470 os_create_anonymous_file(off_t size
)
1472 static const char template[] = "/mesa-shared-XXXXXX";
1478 path
= getenv("XDG_RUNTIME_DIR");
1484 name
= malloc(strlen(path
) + sizeof(template));
1489 strcat(name
, template);
1491 fd
= create_tmpfile_cloexec(name
);
1498 ret
= ftruncate(fd
, size
);
1509 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1510 int format
, int w
, int h
,
1511 void **data
, int *size
,
1512 struct wl_buffer
**buffer
)
1514 struct dri2_egl_display
*dri2_dpy
=
1515 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1516 struct wl_shm_pool
*pool
;
1517 int fd
, stride
, size_map
;
1520 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1521 size_map
= h
* stride
;
1523 /* Create a sharable buffer */
1524 fd
= os_create_anonymous_file(size_map
);
1528 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1529 if (data_map
== MAP_FAILED
) {
1534 /* Share it in a wl_buffer */
1535 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1536 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1537 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1538 wl_shm_pool_destroy(pool
);
1547 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1549 struct dri2_egl_display
*dri2_dpy
=
1550 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1552 /* we need to do the following operations only once per frame */
1553 if (dri2_surf
->back
)
1556 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1557 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1559 dri2_wl_release_buffers(dri2_surf
);
1561 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1562 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1563 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1564 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1565 dri2_surf
->current
= NULL
;
1568 /* find back buffer */
1570 /* There might be a buffer release already queued that wasn't processed */
1571 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1573 /* try get free buffer already created */
1574 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1575 if (!dri2_surf
->color_buffers
[i
].locked
&&
1576 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1577 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1582 /* else choose any another free location */
1583 if (!dri2_surf
->back
) {
1584 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1585 if (!dri2_surf
->color_buffers
[i
].locked
) {
1586 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1587 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1589 dri2_surf
->base
.Width
,
1590 dri2_surf
->base
.Height
,
1591 &dri2_surf
->back
->data
,
1592 &dri2_surf
->back
->data_size
,
1593 &dri2_surf
->back
->wl_buffer
)) {
1594 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1597 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1598 &wl_buffer_listener
, dri2_surf
);
1604 if (!dri2_surf
->back
) {
1605 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1609 dri2_surf
->back
->locked
= true;
1611 /* If we have an extra unlocked buffer at this point, we had to do triple
1612 * buffering for a while, but now can go back to just double buffering.
1613 * That means we can free any unlocked buffer now. */
1614 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1615 if (!dri2_surf
->color_buffers
[i
].locked
&&
1616 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1617 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1618 munmap(dri2_surf
->color_buffers
[i
].data
,
1619 dri2_surf
->color_buffers
[i
].data_size
);
1620 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1621 dri2_surf
->color_buffers
[i
].data
= NULL
;
1629 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1631 /* if there has been a resize: */
1632 if (!dri2_surf
->current
)
1635 return dri2_surf
->current
->data
;
1639 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1641 assert(dri2_surf
->back
);
1642 return dri2_surf
->back
->data
;
1646 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1648 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1650 while (dri2_surf
->throttle_callback
!= NULL
)
1651 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1652 dri2_surf
->wl_queue
) == -1)
1655 if (dri2_surf
->base
.SwapInterval
> 0) {
1656 dri2_surf
->throttle_callback
=
1657 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1658 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1659 &throttle_listener
, dri2_surf
);
1662 dri2_surf
->current
= dri2_surf
->back
;
1663 dri2_surf
->back
= NULL
;
1665 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1666 dri2_surf
->current
->wl_buffer
,
1667 dri2_surf
->dx
, dri2_surf
->dy
);
1669 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1670 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1671 /* reset resize growing parameters */
1675 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1676 0, 0, INT32_MAX
, INT32_MAX
);
1677 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1679 /* If we're not waiting for a frame callback then we'll at least throttle
1680 * to a sync callback so that we always give a chance for the compositor to
1681 * handle the commit and send a release event before checking for a free
1683 if (dri2_surf
->throttle_callback
== NULL
) {
1684 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1685 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1686 &throttle_listener
, dri2_surf
);
1689 wl_display_flush(dri2_dpy
->wl_dpy
);
1693 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1694 int *x
, int *y
, int *w
, int *h
,
1695 void *loaderPrivate
)
1697 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1699 (void) swrast_update_buffers(dri2_surf
);
1702 *w
= dri2_surf
->base
.Width
;
1703 *h
= dri2_surf
->base
.Height
;
1707 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1708 int x
, int y
, int w
, int h
,
1709 char *data
, void *loaderPrivate
)
1711 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1712 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1713 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1714 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1715 int dst_stride
= copy_width
;
1718 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1720 memset(data
, 0, copy_width
* h
);
1724 assert(data
!= src
);
1725 assert(copy_width
<= src_stride
);
1728 src
+= y
* src_stride
;
1731 if (copy_width
> src_stride
-x_offset
)
1732 copy_width
= src_stride
-x_offset
;
1733 if (h
> dri2_surf
->base
.Height
-y
)
1734 h
= dri2_surf
->base
.Height
-y
;
1737 memcpy(dst
, src
, copy_width
);
1744 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1745 int x
, int y
, int w
, int h
, int stride
,
1746 char *data
, void *loaderPrivate
)
1748 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1749 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1750 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1751 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1754 assert(copy_width
<= stride
);
1756 (void) swrast_update_buffers(dri2_surf
);
1757 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1759 /* partial copy, copy old content */
1760 if (copy_width
< dst_stride
)
1761 dri2_wl_swrast_get_image(draw
, 0, 0,
1762 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1763 dst
, loaderPrivate
);
1766 dst
+= y
* dst_stride
;
1770 /* drivers expect we do these checks (and some rely on it) */
1771 if (copy_width
> dst_stride
-x_offset
)
1772 copy_width
= dst_stride
-x_offset
;
1773 if (h
> dri2_surf
->base
.Height
-y
)
1774 h
= dri2_surf
->base
.Height
-y
;
1777 memcpy(dst
, src
, copy_width
);
1781 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1785 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1786 int x
, int y
, int w
, int h
,
1787 char *data
, void *loaderPrivate
)
1789 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1792 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1793 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1794 stride
, data
, loaderPrivate
);
1798 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1800 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1801 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1803 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1808 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1810 struct dri2_egl_display
*dri2_dpy
= data
;
1813 case WL_SHM_FORMAT_ARGB8888
:
1814 dri2_dpy
->formats
|= HAS_ARGB8888
;
1816 case WL_SHM_FORMAT_XRGB8888
:
1817 dri2_dpy
->formats
|= HAS_XRGB8888
;
1819 case WL_SHM_FORMAT_RGB565
:
1820 dri2_dpy
->formats
|= HAS_RGB565
;
1825 static const struct wl_shm_listener shm_listener
= {
1826 .format
= shm_handle_format
1830 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1831 uint32_t name
, const char *interface
,
1834 struct dri2_egl_display
*dri2_dpy
= data
;
1836 if (strcmp(interface
, "wl_shm") == 0) {
1838 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1839 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1843 static const struct wl_registry_listener registry_listener_swrast
= {
1844 .global
= registry_handle_global_swrast
,
1845 .global_remove
= registry_handle_global_remove
1848 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1849 .authenticate
= NULL
,
1850 .create_window_surface
= dri2_wl_create_window_surface
,
1851 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1852 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1853 .destroy_surface
= dri2_wl_destroy_surface
,
1854 .create_image
= dri2_create_image_khr
,
1855 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1856 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1857 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1858 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1859 .copy_buffers
= dri2_fallback_copy_buffers
,
1860 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1861 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1862 .get_sync_values
= dri2_fallback_get_sync_values
,
1863 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1866 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1867 .base
= { __DRI_SWRAST_LOADER
, 2 },
1869 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1870 .putImage
= dri2_wl_swrast_put_image
,
1871 .getImage
= dri2_wl_swrast_get_image
,
1872 .putImage2
= dri2_wl_swrast_put_image2
,
1875 static const __DRIextension
*swrast_loader_extensions
[] = {
1876 &swrast_loader_extension
.base
,
1877 &image_lookup_extension
.base
,
1882 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1884 struct dri2_egl_display
*dri2_dpy
;
1886 loader_set_logger(_eglLog
);
1888 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1890 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1893 disp
->DriverData
= (void *) dri2_dpy
;
1894 if (disp
->PlatformDisplay
== NULL
) {
1895 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1896 if (dri2_dpy
->wl_dpy
== NULL
)
1898 dri2_dpy
->own_device
= true;
1900 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1903 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1905 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1906 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1909 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1910 dri2_dpy
->wl_queue
);
1912 if (dri2_dpy
->own_device
)
1913 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1915 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1916 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1917 ®istry_listener_swrast
, dri2_dpy
);
1919 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1922 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1925 dri2_dpy
->driver_name
= strdup("swrast");
1926 if (!dri2_load_driver_swrast(disp
))
1929 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1931 if (!dri2_create_screen(disp
))
1934 if (!dri2_setup_extensions(disp
))
1937 dri2_setup_screen(disp
);
1939 dri2_wl_setup_swap_interval(disp
);
1941 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1942 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1946 /* Fill vtbl last to prevent accidentally calling virtual function during
1949 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1954 dri2_display_destroy(disp
);
1959 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1961 EGLBoolean initialized
= EGL_TRUE
;
1963 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1966 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1967 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1970 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);