2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
42 #include "egl_dri2_fallbacks.h"
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
48 enum wl_drm_format_flags
{
55 dri2_wl_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
59 roundtrip(struct dri2_egl_display
*dri2_dpy
)
61 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
65 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
67 struct dri2_egl_surface
*dri2_surf
= data
;
70 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
71 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
74 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
75 wl_buffer_destroy(buffer
);
79 dri2_surf
->color_buffers
[i
].locked
= 0;
82 static const struct wl_buffer_listener wl_buffer_listener
= {
83 .release
= wl_buffer_release
87 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
89 struct dri2_egl_surface
*dri2_surf
= data
;
90 struct dri2_egl_display
*dri2_dpy
=
91 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
93 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
97 destroy_window_callback(void *data
)
99 struct dri2_egl_surface
*dri2_surf
= data
;
100 dri2_surf
->wl_win
= NULL
;
104 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
107 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
108 _EGLConfig
*conf
, void *native_window
,
109 const EGLint
*attrib_list
)
111 __DRIcreateNewDrawableFunc createNewDrawable
;
112 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
113 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
114 struct wl_egl_window
*window
= native_window
;
115 struct dri2_egl_surface
*dri2_surf
;
116 const __DRIconfig
*config
;
118 dri2_surf
= calloc(1, sizeof *dri2_surf
);
120 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
124 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
127 if (dri2_dpy
->wl_drm
) {
128 if (conf
->RedSize
== 5)
129 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
130 else if (conf
->AlphaSize
== 0)
131 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
133 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
135 assert(dri2_dpy
->wl_shm
);
136 if (conf
->RedSize
== 5)
137 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
138 else if (conf
->AlphaSize
== 0)
139 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
141 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
145 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
149 dri2_surf
->wl_win
= window
;
150 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
151 if (!dri2_surf
->wl_queue
) {
152 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
156 if (dri2_dpy
->wl_drm
) {
157 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
158 if (!dri2_surf
->wl_drm_wrapper
) {
159 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
162 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
163 dri2_surf
->wl_queue
);
166 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
167 if (!dri2_surf
->wl_dpy_wrapper
) {
168 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
171 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
172 dri2_surf
->wl_queue
);
174 dri2_surf
->wl_surface_wrapper
= wl_proxy_create_wrapper(window
->surface
);
175 if (!dri2_surf
->wl_surface_wrapper
) {
176 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
179 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
180 dri2_surf
->wl_queue
);
182 dri2_surf
->wl_win
->private = dri2_surf
;
183 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
185 dri2_surf
->base
.Width
= -1;
186 dri2_surf
->base
.Height
= -1;
188 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
189 dri2_surf
->base
.GLColorspace
);
191 if (dri2_dpy
->dri2
) {
192 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
194 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
196 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
199 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
201 if (dri2_surf
->dri_drawable
== NULL
) {
202 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
206 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
207 dri2_dpy
->default_swap_interval
);
209 return &dri2_surf
->base
;
212 if (dri2_surf
->wl_drm_wrapper
)
213 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
215 wl_event_queue_destroy(dri2_surf
->wl_queue
);
223 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
224 _EGLConfig
*conf
, void *native_window
,
225 const EGLint
*attrib_list
)
227 /* From the EGL_EXT_platform_wayland spec, version 3:
229 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
230 * that belongs to Wayland. Any such call fails and generates
233 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
239 * Called via eglDestroySurface(), drv->API.DestroySurface().
242 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
244 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
245 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
250 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
252 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
253 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
254 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
255 if (dri2_surf
->color_buffers
[i
].dri_image
)
256 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
257 if (dri2_surf
->color_buffers
[i
].linear_copy
)
258 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
259 if (dri2_surf
->color_buffers
[i
].data
)
260 munmap(dri2_surf
->color_buffers
[i
].data
,
261 dri2_surf
->color_buffers
[i
].data_size
);
264 if (dri2_dpy
->dri2
) {
265 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
266 if (dri2_surf
->dri_buffers
[i
] &&
267 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
268 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
269 dri2_surf
->dri_buffers
[i
]);
272 if (dri2_surf
->throttle_callback
)
273 wl_callback_destroy(dri2_surf
->throttle_callback
);
275 if (dri2_surf
->wl_win
) {
276 dri2_surf
->wl_win
->private = NULL
;
277 dri2_surf
->wl_win
->resize_callback
= NULL
;
278 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
281 if (dri2_surf
->wl_drm_wrapper
)
282 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
283 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
284 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
285 wl_event_queue_destroy(dri2_surf
->wl_queue
);
293 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
295 struct dri2_egl_display
*dri2_dpy
=
296 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
299 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
300 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
301 !dri2_surf
->color_buffers
[i
].locked
)
302 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
303 if (dri2_surf
->color_buffers
[i
].dri_image
)
304 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
305 if (dri2_surf
->color_buffers
[i
].linear_copy
)
306 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
307 if (dri2_surf
->color_buffers
[i
].data
)
308 munmap(dri2_surf
->color_buffers
[i
].data
,
309 dri2_surf
->color_buffers
[i
].data_size
);
311 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
312 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
313 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
314 dri2_surf
->color_buffers
[i
].data
= NULL
;
315 dri2_surf
->color_buffers
[i
].locked
= 0;
318 if (dri2_dpy
->dri2
) {
319 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
320 if (dri2_surf
->dri_buffers
[i
] &&
321 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
322 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
323 dri2_surf
->dri_buffers
[i
]);
328 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
330 struct dri2_egl_display
*dri2_dpy
=
331 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
333 unsigned int dri_image_format
;
335 /* currently supports three WL DRM formats,
336 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
337 * and WL_DRM_FORMAT_RGB565
339 switch (dri2_surf
->format
) {
340 case WL_DRM_FORMAT_ARGB8888
:
341 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
343 case WL_DRM_FORMAT_XRGB8888
:
344 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
346 case WL_DRM_FORMAT_RGB565
:
347 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
350 /* format is not supported */
354 /* There might be a buffer release already queued that wasn't processed */
355 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
357 while (dri2_surf
->back
== NULL
) {
358 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
359 /* Get an unlocked buffer, preferrably one with a dri_buffer
360 * already allocated. */
361 if (dri2_surf
->color_buffers
[i
].locked
)
363 if (dri2_surf
->back
== NULL
)
364 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
365 else if (dri2_surf
->back
->dri_image
== NULL
)
366 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
372 /* If we don't have a buffer, then block on the server to release one for
373 * us, and try again. */
374 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
) < 0)
378 if (dri2_surf
->back
== NULL
)
381 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
383 if (dri2_dpy
->is_different_gpu
&&
384 dri2_surf
->back
->linear_copy
== NULL
) {
385 dri2_surf
->back
->linear_copy
=
386 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
387 dri2_surf
->base
.Width
,
388 dri2_surf
->base
.Height
,
391 __DRI_IMAGE_USE_LINEAR
,
393 if (dri2_surf
->back
->linear_copy
== NULL
)
397 if (dri2_surf
->back
->dri_image
== NULL
) {
398 dri2_surf
->back
->dri_image
=
399 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
400 dri2_surf
->base
.Width
,
401 dri2_surf
->base
.Height
,
403 dri2_dpy
->is_different_gpu
?
406 dri2_surf
->back
->age
= 0;
408 if (dri2_surf
->back
->dri_image
== NULL
)
411 dri2_surf
->back
->locked
= 1;
418 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
420 struct dri2_egl_display
*dri2_dpy
=
421 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
425 image
= dri2_surf
->back
->dri_image
;
427 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
428 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
430 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
432 buffer
->pitch
= pitch
;
438 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
439 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
441 struct dri2_egl_display
*dri2_dpy
=
442 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
443 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
446 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
448 dri2_surf
->base
.Width
,
449 dri2_surf
->base
.Height
);
450 dri2_surf
->dri_buffers
[attachment
] = b
;
455 memcpy(buffer
, b
, sizeof *buffer
);
461 update_buffers(struct dri2_egl_surface
*dri2_surf
)
463 struct dri2_egl_display
*dri2_dpy
=
464 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
467 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
468 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
470 dri2_wl_release_buffers(dri2_surf
);
472 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
473 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
474 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
475 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
478 if (get_back_bo(dri2_surf
) < 0) {
479 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
483 /* If we have an extra unlocked buffer at this point, we had to do triple
484 * buffering for a while, but now can go back to just double buffering.
485 * That means we can free any unlocked buffer now. */
486 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
487 if (!dri2_surf
->color_buffers
[i
].locked
&&
488 dri2_surf
->color_buffers
[i
].wl_buffer
) {
489 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
490 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
491 if (dri2_dpy
->is_different_gpu
)
492 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
493 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
494 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
495 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
503 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
504 int *width
, int *height
,
505 unsigned int *attachments
, int count
,
506 int *out_count
, void *loaderPrivate
)
508 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
511 if (update_buffers(dri2_surf
) < 0)
514 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
515 switch (attachments
[i
]) {
516 case __DRI_BUFFER_BACK_LEFT
:
517 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
520 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
521 &dri2_surf
->buffers
[j
]) < 0) {
522 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
533 *width
= dri2_surf
->base
.Width
;
534 *height
= dri2_surf
->base
.Height
;
536 return dri2_surf
->buffers
;
540 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
541 int *width
, int *height
,
542 unsigned int *attachments
, int count
,
543 int *out_count
, void *loaderPrivate
)
545 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
546 unsigned int *attachments_with_format
;
552 switch (dri2_surf
->format
) {
553 case WL_DRM_FORMAT_ARGB8888
:
554 case WL_DRM_FORMAT_XRGB8888
:
557 case WL_DRM_FORMAT_RGB565
:
561 /* format is not supported */
565 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
566 if (!attachments_with_format
) {
571 for (i
= 0; i
< count
; ++i
) {
572 attachments_with_format
[2*i
] = attachments
[i
];
573 attachments_with_format
[2*i
+ 1] = bpp
;
577 dri2_wl_get_buffers_with_format(driDrawable
,
579 attachments_with_format
, count
,
580 out_count
, loaderPrivate
);
582 free(attachments_with_format
);
588 image_get_buffers(__DRIdrawable
*driDrawable
,
592 uint32_t buffer_mask
,
593 struct __DRIimageList
*buffers
)
595 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
597 if (update_buffers(dri2_surf
) < 0)
600 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
601 buffers
->back
= dri2_surf
->back
->dri_image
;
607 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
610 (void) loaderPrivate
;
613 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
614 .base
= { __DRI_DRI2_LOADER
, 3 },
616 .getBuffers
= dri2_wl_get_buffers
,
617 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
618 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
621 static const __DRIimageLoaderExtension image_loader_extension
= {
622 .base
= { __DRI_IMAGE_LOADER
, 1 },
624 .getBuffers
= image_get_buffers
,
625 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
629 wayland_throttle_callback(void *data
,
630 struct wl_callback
*callback
,
633 struct dri2_egl_surface
*dri2_surf
= data
;
635 dri2_surf
->throttle_callback
= NULL
;
636 wl_callback_destroy(callback
);
639 static const struct wl_callback_listener throttle_listener
= {
640 .done
= wayland_throttle_callback
644 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
646 struct dri2_egl_display
*dri2_dpy
=
647 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
649 int fd
, stride
, name
;
651 if (dri2_surf
->current
->wl_buffer
!= NULL
)
654 if (dri2_dpy
->is_different_gpu
) {
655 image
= dri2_surf
->current
->linear_copy
;
657 image
= dri2_surf
->current
->dri_image
;
659 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
660 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
661 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
663 dri2_surf
->current
->wl_buffer
=
664 wl_drm_create_prime_buffer(dri2_surf
->wl_drm_wrapper
,
666 dri2_surf
->base
.Width
,
667 dri2_surf
->base
.Height
,
674 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
675 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
677 dri2_surf
->current
->wl_buffer
=
678 wl_drm_create_buffer(dri2_surf
->wl_drm_wrapper
,
680 dri2_surf
->base
.Width
,
681 dri2_surf
->base
.Height
,
686 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
687 &wl_buffer_listener
, dri2_surf
);
691 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
697 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
698 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
701 for (i
= 0; i
< n_rects
; i
++) {
702 const int *rect
= &rects
[i
* 4];
704 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
706 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
712 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
715 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
721 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
722 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
725 while (dri2_surf
->throttle_callback
!= NULL
)
726 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
727 dri2_surf
->wl_queue
) == -1)
730 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
731 if (dri2_surf
->color_buffers
[i
].age
> 0)
732 dri2_surf
->color_buffers
[i
].age
++;
734 /* Make sure we have a back buffer in case we're swapping without ever
736 if (get_back_bo(dri2_surf
) < 0) {
737 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
741 if (draw
->SwapInterval
> 0) {
742 dri2_surf
->throttle_callback
=
743 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
744 wl_callback_add_listener(dri2_surf
->throttle_callback
,
745 &throttle_listener
, dri2_surf
);
748 dri2_surf
->back
->age
= 1;
749 dri2_surf
->current
= dri2_surf
->back
;
750 dri2_surf
->back
= NULL
;
752 create_wl_buffer(dri2_surf
);
754 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
755 dri2_surf
->current
->wl_buffer
,
756 dri2_surf
->dx
, dri2_surf
->dy
);
758 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
759 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
760 /* reset resize growing parameters */
764 /* If the compositor doesn't support damage_buffer, we deliberately
765 * ignore the damage region and post maximum damage, due to
766 * https://bugs.freedesktop.org/78190 */
767 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
768 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
769 0, 0, INT32_MAX
, INT32_MAX
);
771 if (dri2_dpy
->is_different_gpu
) {
772 _EGLContext
*ctx
= _eglGetCurrentContext();
773 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
774 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
775 dri2_surf
->current
->linear_copy
,
776 dri2_surf
->current
->dri_image
,
777 0, 0, dri2_surf
->base
.Width
,
778 dri2_surf
->base
.Height
,
779 0, 0, dri2_surf
->base
.Width
,
780 dri2_surf
->base
.Height
, 0);
783 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
784 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
786 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
788 /* If we're not waiting for a frame callback then we'll at least throttle
789 * to a sync callback so that we always give a chance for the compositor to
790 * handle the commit and send a release event before checking for a free
792 if (dri2_surf
->throttle_callback
== NULL
) {
793 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
794 wl_callback_add_listener(dri2_surf
->throttle_callback
,
795 &throttle_listener
, dri2_surf
);
798 wl_display_flush(dri2_dpy
->wl_dpy
);
804 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
805 _EGLDisplay
*disp
, _EGLSurface
*surface
)
807 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
809 if (get_back_bo(dri2_surf
) < 0) {
810 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
814 return dri2_surf
->back
->age
;
818 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
820 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
823 static struct wl_buffer
*
824 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
828 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
829 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
830 __DRIimage
*image
= dri2_img
->dri_image
;
831 struct wl_buffer
*buffer
;
832 int width
, height
, format
, pitch
;
833 enum wl_drm_format wl_format
;
835 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
838 case __DRI_IMAGE_FORMAT_ARGB8888
:
839 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
841 wl_format
= WL_DRM_FORMAT_ARGB8888
;
843 case __DRI_IMAGE_FORMAT_XRGB8888
:
844 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
846 wl_format
= WL_DRM_FORMAT_XRGB8888
;
852 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
853 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
854 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
856 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
859 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
862 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
874 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
877 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
884 /* The buffer object will have been created with our internal event queue
885 * because it is using the wl_drm object as a proxy factory. We want the
886 * buffer to be used by the application so we'll reset it to the display's
887 * default event queue */
889 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
894 _eglError(EGL_BAD_MATCH
, "unsupported image format");
899 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
901 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
904 if (dri2_dpy
->is_render_node
) {
905 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
906 "authenticate for render-nodes");
909 dri2_dpy
->authenticated
= 0;
911 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
912 if (roundtrip(dri2_dpy
) < 0)
915 if (!dri2_dpy
->authenticated
)
918 /* reset authenticated */
919 dri2_dpy
->authenticated
= 1;
925 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
927 struct dri2_egl_display
*dri2_dpy
= data
;
930 dri2_dpy
->device_name
= strdup(device
);
931 if (!dri2_dpy
->device_name
)
934 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
935 if (dri2_dpy
->fd
== -1) {
936 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
937 dri2_dpy
->device_name
, strerror(errno
));
941 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
942 dri2_dpy
->authenticated
= 1;
944 drmGetMagic(dri2_dpy
->fd
, &magic
);
945 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
950 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
952 struct dri2_egl_display
*dri2_dpy
= data
;
955 case WL_DRM_FORMAT_ARGB8888
:
956 dri2_dpy
->formats
|= HAS_ARGB8888
;
958 case WL_DRM_FORMAT_XRGB8888
:
959 dri2_dpy
->formats
|= HAS_XRGB8888
;
961 case WL_DRM_FORMAT_RGB565
:
962 dri2_dpy
->formats
|= HAS_RGB565
;
968 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
970 struct dri2_egl_display
*dri2_dpy
= data
;
972 dri2_dpy
->capabilities
= value
;
976 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
978 struct dri2_egl_display
*dri2_dpy
= data
;
980 dri2_dpy
->authenticated
= 1;
983 static const struct wl_drm_listener drm_listener
= {
984 .device
= drm_handle_device
,
985 .format
= drm_handle_format
,
986 .authenticated
= drm_handle_authenticated
,
987 .capabilities
= drm_handle_capabilities
991 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
992 const char *interface
, uint32_t version
)
994 struct dri2_egl_display
*dri2_dpy
= data
;
998 if (strcmp(interface
, "wl_drm") == 0) {
1000 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
1001 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1006 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1011 static const struct wl_registry_listener registry_listener_drm
= {
1012 .global
= registry_handle_global_drm
,
1013 .global_remove
= registry_handle_global_remove
1017 dri2_wl_swap_interval(_EGLDriver
*drv
,
1022 if (interval
> surf
->Config
->MaxSwapInterval
)
1023 interval
= surf
->Config
->MaxSwapInterval
;
1024 else if (interval
< surf
->Config
->MinSwapInterval
)
1025 interval
= surf
->Config
->MinSwapInterval
;
1027 surf
->SwapInterval
= interval
;
1033 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1035 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1037 /* We can't use values greater than 1 on Wayland because we are using the
1038 * frame callback to synchronise the frame and the only way we be sure to
1039 * get a frame callback is to attach a new buffer. Therefore we can't just
1040 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1042 if (dri2_dpy
->config
)
1043 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1044 "vblank_mode", &vblank_mode
);
1045 switch (vblank_mode
) {
1046 case DRI_CONF_VBLANK_NEVER
:
1047 dri2_dpy
->min_swap_interval
= 0;
1048 dri2_dpy
->max_swap_interval
= 0;
1049 dri2_dpy
->default_swap_interval
= 0;
1051 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1052 dri2_dpy
->min_swap_interval
= 1;
1053 dri2_dpy
->max_swap_interval
= 1;
1054 dri2_dpy
->default_swap_interval
= 1;
1056 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1057 dri2_dpy
->min_swap_interval
= 0;
1058 dri2_dpy
->max_swap_interval
= 1;
1059 dri2_dpy
->default_swap_interval
= 0;
1062 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1063 dri2_dpy
->min_swap_interval
= 0;
1064 dri2_dpy
->max_swap_interval
= 1;
1065 dri2_dpy
->default_swap_interval
= 1;
1070 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1071 .authenticate
= dri2_wl_authenticate
,
1072 .create_window_surface
= dri2_wl_create_window_surface
,
1073 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1074 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1075 .destroy_surface
= dri2_wl_destroy_surface
,
1076 .create_image
= dri2_create_image_khr
,
1077 .swap_interval
= dri2_wl_swap_interval
,
1078 .swap_buffers
= dri2_wl_swap_buffers
,
1079 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1080 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1081 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1082 .copy_buffers
= dri2_fallback_copy_buffers
,
1083 .query_buffer_age
= dri2_wl_query_buffer_age
,
1084 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1085 .get_sync_values
= dri2_fallback_get_sync_values
,
1086 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1089 static const __DRIextension
*dri2_loader_extensions
[] = {
1090 &dri2_loader_extension
.base
,
1091 &image_loader_extension
.base
,
1092 &image_lookup_extension
.base
,
1093 &use_invalidate
.base
,
1097 static const __DRIextension
*image_loader_extensions
[] = {
1098 &image_loader_extension
.base
,
1099 &image_lookup_extension
.base
,
1100 &use_invalidate
.base
,
1105 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1107 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1108 static const struct {
1109 const char *format_name
;
1111 unsigned int rgba_masks
[4];
1113 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1114 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1115 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1117 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1118 unsigned int count
, i
, j
;
1121 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1122 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1123 struct dri2_egl_config
*dri2_conf
;
1125 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1128 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1129 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1137 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1138 if (!format_count
[i
]) {
1139 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1140 visuals
[i
].format_name
);
1144 return (count
!= 0);
1148 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1150 struct dri2_egl_display
*dri2_dpy
;
1152 loader_set_logger(_eglLog
);
1154 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1156 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1159 disp
->DriverData
= (void *) dri2_dpy
;
1160 if (disp
->PlatformDisplay
== NULL
) {
1161 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1162 if (dri2_dpy
->wl_dpy
== NULL
)
1164 dri2_dpy
->own_device
= 1;
1166 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1169 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1171 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1172 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1175 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1176 dri2_dpy
->wl_queue
);
1178 if (dri2_dpy
->own_device
)
1179 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1181 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1182 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1183 ®istry_listener_drm
, dri2_dpy
);
1184 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1187 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1190 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1193 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1194 &dri2_dpy
->is_different_gpu
);
1195 if (dri2_dpy
->is_different_gpu
) {
1196 free(dri2_dpy
->device_name
);
1197 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1198 if (!dri2_dpy
->device_name
) {
1199 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1200 "for requested GPU");
1205 /* we have to do the check now, because loader_get_user_preferred_fd
1206 * will return a render-node when the requested gpu is different
1207 * to the server, but also if the client asks for the same gpu than
1208 * the server by requesting its pci-id */
1209 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1211 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1212 if (dri2_dpy
->driver_name
== NULL
) {
1213 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1217 if (!dri2_load_driver(disp
))
1220 /* render nodes cannot use Gem names, and thus do not support
1221 * the __DRI_DRI2_LOADER extension */
1222 if (!dri2_dpy
->is_render_node
)
1223 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1225 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1227 if (!dri2_create_screen(disp
))
1230 if (!dri2_setup_extensions(disp
))
1233 dri2_setup_screen(disp
);
1235 dri2_wl_setup_swap_interval(dri2_dpy
);
1237 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1238 * createImageFromFds support indicates that Prime export/import
1239 * is supported by the driver. Fall back to
1240 * gem names if we don't have Prime support. */
1242 if (dri2_dpy
->image
->base
.version
< 7 ||
1243 dri2_dpy
->image
->createImageFromFds
== NULL
)
1244 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1246 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1247 * The server needs to accept them */
1248 if (dri2_dpy
->is_render_node
&&
1249 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1250 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1254 if (dri2_dpy
->is_different_gpu
&&
1255 (dri2_dpy
->image
->base
.version
< 9 ||
1256 dri2_dpy
->image
->blitImage
== NULL
)) {
1257 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1258 "Image extension in the driver is not "
1259 "compatible. Version 9 or later and blitImage() "
1264 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1265 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1269 dri2_set_WL_bind_wayland_display(drv
, disp
);
1270 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1271 * because the buffer of the EGLImage has likely a tiling mode the server
1272 * gpu won't support. These is no way to check for now. Thus do not support the
1274 if (!dri2_dpy
->is_different_gpu
)
1275 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1277 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1279 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1281 /* Fill vtbl last to prevent accidentally calling virtual function during
1284 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1289 dri2_display_destroy(disp
);
1294 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1296 if (format
== WL_SHM_FORMAT_RGB565
)
1298 else /* ARGB8888 || XRGB8888 */
1303 * Taken from weston shared/os-compatibility.c
1306 #ifndef HAVE_MKOSTEMP
1309 set_cloexec_or_close(int fd
)
1316 flags
= fcntl(fd
, F_GETFD
);
1320 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1333 * Taken from weston shared/os-compatibility.c
1337 create_tmpfile_cloexec(char *tmpname
)
1341 #ifdef HAVE_MKOSTEMP
1342 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1346 fd
= mkstemp(tmpname
);
1348 fd
= set_cloexec_or_close(fd
);
1357 * Taken from weston shared/os-compatibility.c
1359 * Create a new, unique, anonymous file of the given size, and
1360 * return the file descriptor for it. The file descriptor is set
1361 * CLOEXEC. The file is immediately suitable for mmap()'ing
1362 * the given size at offset zero.
1364 * The file should not have a permanent backing store like a disk,
1365 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1367 * The file name is deleted from the file system.
1369 * The file is suitable for buffer sharing between processes by
1370 * transmitting the file descriptor over Unix sockets using the
1371 * SCM_RIGHTS methods.
1373 * If the C library implements posix_fallocate(), it is used to
1374 * guarantee that disk space is available for the file at the
1375 * given size. If disk space is insufficent, errno is set to ENOSPC.
1376 * If posix_fallocate() is not supported, program may receive
1377 * SIGBUS on accessing mmap()'ed file contents instead.
1380 os_create_anonymous_file(off_t size
)
1382 static const char template[] = "/mesa-shared-XXXXXX";
1388 path
= getenv("XDG_RUNTIME_DIR");
1394 name
= malloc(strlen(path
) + sizeof(template));
1399 strcat(name
, template);
1401 fd
= create_tmpfile_cloexec(name
);
1408 ret
= ftruncate(fd
, size
);
1419 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1420 int format
, int w
, int h
,
1421 void **data
, int *size
,
1422 struct wl_buffer
**buffer
)
1424 struct dri2_egl_display
*dri2_dpy
=
1425 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1426 struct wl_shm_pool
*pool
;
1427 int fd
, stride
, size_map
;
1430 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1431 size_map
= h
* stride
;
1433 /* Create a sharable buffer */
1434 fd
= os_create_anonymous_file(size_map
);
1438 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1439 if (data_map
== MAP_FAILED
) {
1444 /* Share it in a wl_buffer */
1445 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1446 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1447 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1448 wl_shm_pool_destroy(pool
);
1457 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1459 struct dri2_egl_display
*dri2_dpy
=
1460 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1463 /* we need to do the following operations only once per frame */
1464 if (dri2_surf
->back
)
1467 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1468 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1470 dri2_wl_release_buffers(dri2_surf
);
1472 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1473 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1474 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1475 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1476 dri2_surf
->current
= NULL
;
1479 /* find back buffer */
1481 /* There might be a buffer release already queued that wasn't processed */
1482 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1484 /* try get free buffer already created */
1485 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1486 if (!dri2_surf
->color_buffers
[i
].locked
&&
1487 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1488 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1493 /* else choose any another free location */
1494 if (!dri2_surf
->back
) {
1495 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1496 if (!dri2_surf
->color_buffers
[i
].locked
) {
1497 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1498 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1500 dri2_surf
->base
.Width
,
1501 dri2_surf
->base
.Height
,
1502 &dri2_surf
->back
->data
,
1503 &dri2_surf
->back
->data_size
,
1504 &dri2_surf
->back
->wl_buffer
)) {
1505 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1508 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1509 &wl_buffer_listener
, dri2_surf
);
1515 if (!dri2_surf
->back
) {
1516 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1520 dri2_surf
->back
->locked
= 1;
1522 /* If we have an extra unlocked buffer at this point, we had to do triple
1523 * buffering for a while, but now can go back to just double buffering.
1524 * That means we can free any unlocked buffer now. */
1525 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1526 if (!dri2_surf
->color_buffers
[i
].locked
&&
1527 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1528 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1529 munmap(dri2_surf
->color_buffers
[i
].data
,
1530 dri2_surf
->color_buffers
[i
].data_size
);
1531 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1532 dri2_surf
->color_buffers
[i
].data
= NULL
;
1540 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1542 /* if there has been a resize: */
1543 if (!dri2_surf
->current
)
1546 return dri2_surf
->current
->data
;
1550 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1552 assert(dri2_surf
->back
);
1553 return dri2_surf
->back
->data
;
1557 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1559 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1561 while (dri2_surf
->throttle_callback
!= NULL
)
1562 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1563 dri2_surf
->wl_queue
) == -1)
1566 if (dri2_surf
->base
.SwapInterval
> 0) {
1567 dri2_surf
->throttle_callback
=
1568 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1569 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1570 &throttle_listener
, dri2_surf
);
1573 dri2_surf
->current
= dri2_surf
->back
;
1574 dri2_surf
->back
= NULL
;
1576 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1577 dri2_surf
->current
->wl_buffer
,
1578 dri2_surf
->dx
, dri2_surf
->dy
);
1580 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1581 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1582 /* reset resize growing parameters */
1586 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1587 0, 0, INT32_MAX
, INT32_MAX
);
1588 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1590 /* If we're not waiting for a frame callback then we'll at least throttle
1591 * to a sync callback so that we always give a chance for the compositor to
1592 * handle the commit and send a release event before checking for a free
1594 if (dri2_surf
->throttle_callback
== NULL
) {
1595 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1596 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1597 &throttle_listener
, dri2_surf
);
1600 wl_display_flush(dri2_dpy
->wl_dpy
);
1604 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1605 int *x
, int *y
, int *w
, int *h
,
1606 void *loaderPrivate
)
1608 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1610 (void) swrast_update_buffers(dri2_surf
);
1613 *w
= dri2_surf
->base
.Width
;
1614 *h
= dri2_surf
->base
.Height
;
1618 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1619 int x
, int y
, int w
, int h
,
1620 char *data
, void *loaderPrivate
)
1622 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1623 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1624 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1625 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1626 int dst_stride
= copy_width
;
1629 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1631 memset(data
, 0, copy_width
* h
);
1635 assert(data
!= src
);
1636 assert(copy_width
<= src_stride
);
1639 src
+= y
* src_stride
;
1642 if (copy_width
> src_stride
-x_offset
)
1643 copy_width
= src_stride
-x_offset
;
1644 if (h
> dri2_surf
->base
.Height
-y
)
1645 h
= dri2_surf
->base
.Height
-y
;
1648 memcpy(dst
, src
, copy_width
);
1655 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1656 int x
, int y
, int w
, int h
, int stride
,
1657 char *data
, void *loaderPrivate
)
1659 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1660 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1661 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1662 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1665 assert(copy_width
<= stride
);
1667 (void) swrast_update_buffers(dri2_surf
);
1668 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1670 /* partial copy, copy old content */
1671 if (copy_width
< dst_stride
)
1672 dri2_wl_swrast_get_image(draw
, 0, 0,
1673 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1674 dst
, loaderPrivate
);
1677 dst
+= y
* dst_stride
;
1681 /* drivers expect we do these checks (and some rely on it) */
1682 if (copy_width
> dst_stride
-x_offset
)
1683 copy_width
= dst_stride
-x_offset
;
1684 if (h
> dri2_surf
->base
.Height
-y
)
1685 h
= dri2_surf
->base
.Height
-y
;
1688 memcpy(dst
, src
, copy_width
);
1692 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1696 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1697 int x
, int y
, int w
, int h
,
1698 char *data
, void *loaderPrivate
)
1700 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1703 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1704 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1705 stride
, data
, loaderPrivate
);
1709 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1711 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1712 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1714 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1719 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1721 struct dri2_egl_display
*dri2_dpy
= data
;
1724 case WL_SHM_FORMAT_ARGB8888
:
1725 dri2_dpy
->formats
|= HAS_ARGB8888
;
1727 case WL_SHM_FORMAT_XRGB8888
:
1728 dri2_dpy
->formats
|= HAS_XRGB8888
;
1730 case WL_SHM_FORMAT_RGB565
:
1731 dri2_dpy
->formats
|= HAS_RGB565
;
1736 static const struct wl_shm_listener shm_listener
= {
1737 .format
= shm_handle_format
1741 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1742 const char *interface
, uint32_t version
)
1744 struct dri2_egl_display
*dri2_dpy
= data
;
1746 if (strcmp(interface
, "wl_shm") == 0) {
1748 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1749 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1753 static const struct wl_registry_listener registry_listener_swrast
= {
1754 .global
= registry_handle_global_swrast
,
1755 .global_remove
= registry_handle_global_remove
1758 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1759 .authenticate
= NULL
,
1760 .create_window_surface
= dri2_wl_create_window_surface
,
1761 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1762 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1763 .destroy_surface
= dri2_wl_destroy_surface
,
1764 .create_image
= dri2_fallback_create_image_khr
,
1765 .swap_interval
= dri2_wl_swap_interval
,
1766 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1767 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1768 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1769 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1770 .copy_buffers
= dri2_fallback_copy_buffers
,
1771 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1772 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1773 .get_sync_values
= dri2_fallback_get_sync_values
,
1774 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1777 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1778 .base
= { __DRI_SWRAST_LOADER
, 2 },
1780 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1781 .putImage
= dri2_wl_swrast_put_image
,
1782 .getImage
= dri2_wl_swrast_get_image
,
1783 .putImage2
= dri2_wl_swrast_put_image2
,
1786 static const __DRIextension
*swrast_loader_extensions
[] = {
1787 &swrast_loader_extension
.base
,
1792 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1794 struct dri2_egl_display
*dri2_dpy
;
1796 loader_set_logger(_eglLog
);
1798 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1800 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1803 disp
->DriverData
= (void *) dri2_dpy
;
1804 if (disp
->PlatformDisplay
== NULL
) {
1805 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1806 if (dri2_dpy
->wl_dpy
== NULL
)
1808 dri2_dpy
->own_device
= 1;
1810 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1813 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1815 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1816 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1819 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1820 dri2_dpy
->wl_queue
);
1822 if (dri2_dpy
->own_device
)
1823 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1825 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1826 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1827 ®istry_listener_swrast
, dri2_dpy
);
1829 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1832 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1835 dri2_dpy
->driver_name
= strdup("swrast");
1836 if (!dri2_load_driver_swrast(disp
))
1839 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1841 if (!dri2_create_screen(disp
))
1844 if (!dri2_setup_extensions(disp
))
1847 dri2_setup_screen(disp
);
1849 dri2_wl_setup_swap_interval(dri2_dpy
);
1851 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1852 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1856 /* Fill vtbl last to prevent accidentally calling virtual function during
1859 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1864 dri2_display_destroy(disp
);
1869 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1871 EGLBoolean initialized
= EGL_TRUE
;
1873 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1876 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1877 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1880 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);