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
->dri2
) {
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 if (conf
->RedSize
== 5)
136 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
137 else if (conf
->AlphaSize
== 0)
138 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
140 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
144 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
148 dri2_surf
->wl_win
= window
;
150 dri2_surf
->wl_win
->private = dri2_surf
;
151 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
153 dri2_surf
->base
.Width
= -1;
154 dri2_surf
->base
.Height
= -1;
156 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
157 dri2_surf
->base
.GLColorspace
);
159 if (dri2_dpy
->dri2
) {
160 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
162 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
164 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
167 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
169 if (dri2_surf
->dri_drawable
== NULL
) {
170 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
174 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
175 dri2_dpy
->default_swap_interval
);
177 return &dri2_surf
->base
;
186 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
187 _EGLConfig
*conf
, void *native_window
,
188 const EGLint
*attrib_list
)
190 /* From the EGL_EXT_platform_wayland spec, version 3:
192 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
193 * that belongs to Wayland. Any such call fails and generates
196 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
202 * Called via eglDestroySurface(), drv->API.DestroySurface().
205 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
207 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
208 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
213 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
215 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
216 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
217 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
218 if (dri2_surf
->color_buffers
[i
].dri_image
)
219 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
220 if (dri2_surf
->color_buffers
[i
].linear_copy
)
221 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
222 if (dri2_surf
->color_buffers
[i
].data
)
223 munmap(dri2_surf
->color_buffers
[i
].data
,
224 dri2_surf
->color_buffers
[i
].data_size
);
227 if (dri2_dpy
->dri2
) {
228 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
229 if (dri2_surf
->dri_buffers
[i
] &&
230 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
231 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
232 dri2_surf
->dri_buffers
[i
]);
235 if (dri2_surf
->throttle_callback
)
236 wl_callback_destroy(dri2_surf
->throttle_callback
);
238 if (dri2_surf
->wl_win
) {
239 dri2_surf
->wl_win
->private = NULL
;
240 dri2_surf
->wl_win
->resize_callback
= NULL
;
241 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
250 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
252 struct dri2_egl_display
*dri2_dpy
=
253 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
256 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
257 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
258 !dri2_surf
->color_buffers
[i
].locked
)
259 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
260 if (dri2_surf
->color_buffers
[i
].dri_image
)
261 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
262 if (dri2_surf
->color_buffers
[i
].linear_copy
)
263 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
264 if (dri2_surf
->color_buffers
[i
].data
)
265 munmap(dri2_surf
->color_buffers
[i
].data
,
266 dri2_surf
->color_buffers
[i
].data_size
);
268 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
269 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
270 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
271 dri2_surf
->color_buffers
[i
].data
= NULL
;
272 dri2_surf
->color_buffers
[i
].locked
= 0;
275 if (dri2_dpy
->dri2
) {
276 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
277 if (dri2_surf
->dri_buffers
[i
] &&
278 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
279 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
280 dri2_surf
->dri_buffers
[i
]);
285 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
287 struct dri2_egl_display
*dri2_dpy
=
288 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
290 unsigned int dri_image_format
;
292 /* currently supports three WL DRM formats,
293 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
294 * and WL_DRM_FORMAT_RGB565
296 switch (dri2_surf
->format
) {
297 case WL_DRM_FORMAT_ARGB8888
:
298 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
300 case WL_DRM_FORMAT_XRGB8888
:
301 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
303 case WL_DRM_FORMAT_RGB565
:
304 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
307 /* format is not supported */
311 /* There might be a buffer release already queued that wasn't processed */
312 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
314 if (dri2_surf
->back
== NULL
) {
315 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
316 /* Get an unlocked buffer, preferrably one with a dri_buffer
317 * already allocated. */
318 if (dri2_surf
->color_buffers
[i
].locked
)
320 if (dri2_surf
->back
== NULL
)
321 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
322 else if (dri2_surf
->back
->dri_image
== NULL
)
323 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
327 if (dri2_surf
->back
== NULL
)
330 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
332 if (dri2_dpy
->is_different_gpu
&&
333 dri2_surf
->back
->linear_copy
== NULL
) {
334 dri2_surf
->back
->linear_copy
=
335 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
336 dri2_surf
->base
.Width
,
337 dri2_surf
->base
.Height
,
340 __DRI_IMAGE_USE_LINEAR
,
342 if (dri2_surf
->back
->linear_copy
== NULL
)
346 if (dri2_surf
->back
->dri_image
== NULL
) {
347 dri2_surf
->back
->dri_image
=
348 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
349 dri2_surf
->base
.Width
,
350 dri2_surf
->base
.Height
,
352 dri2_dpy
->is_different_gpu
?
355 dri2_surf
->back
->age
= 0;
357 if (dri2_surf
->back
->dri_image
== NULL
)
360 dri2_surf
->back
->locked
= 1;
367 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
369 struct dri2_egl_display
*dri2_dpy
=
370 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
374 image
= dri2_surf
->back
->dri_image
;
376 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
377 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
379 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
381 buffer
->pitch
= pitch
;
387 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
388 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
390 struct dri2_egl_display
*dri2_dpy
=
391 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
392 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
395 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
397 dri2_surf
->base
.Width
,
398 dri2_surf
->base
.Height
);
399 dri2_surf
->dri_buffers
[attachment
] = b
;
404 memcpy(buffer
, b
, sizeof *buffer
);
410 update_buffers(struct dri2_egl_surface
*dri2_surf
)
412 struct dri2_egl_display
*dri2_dpy
=
413 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
416 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
417 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
419 dri2_wl_release_buffers(dri2_surf
);
421 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
422 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
423 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
424 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
427 if (get_back_bo(dri2_surf
) < 0) {
428 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
432 /* If we have an extra unlocked buffer at this point, we had to do triple
433 * buffering for a while, but now can go back to just double buffering.
434 * That means we can free any unlocked buffer now. */
435 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
436 if (!dri2_surf
->color_buffers
[i
].locked
&&
437 dri2_surf
->color_buffers
[i
].wl_buffer
) {
438 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
439 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
440 if (dri2_dpy
->is_different_gpu
)
441 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
442 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
443 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
444 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
452 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
453 int *width
, int *height
,
454 unsigned int *attachments
, int count
,
455 int *out_count
, void *loaderPrivate
)
457 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
460 if (update_buffers(dri2_surf
) < 0)
463 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
464 switch (attachments
[i
]) {
465 case __DRI_BUFFER_BACK_LEFT
:
466 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
469 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
470 &dri2_surf
->buffers
[j
]) < 0) {
471 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
482 *width
= dri2_surf
->base
.Width
;
483 *height
= dri2_surf
->base
.Height
;
485 return dri2_surf
->buffers
;
489 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
490 int *width
, int *height
,
491 unsigned int *attachments
, int count
,
492 int *out_count
, void *loaderPrivate
)
494 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
495 unsigned int *attachments_with_format
;
501 switch (dri2_surf
->format
) {
502 case WL_DRM_FORMAT_ARGB8888
:
503 case WL_DRM_FORMAT_XRGB8888
:
506 case WL_DRM_FORMAT_RGB565
:
510 /* format is not supported */
514 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
515 if (!attachments_with_format
) {
520 for (i
= 0; i
< count
; ++i
) {
521 attachments_with_format
[2*i
] = attachments
[i
];
522 attachments_with_format
[2*i
+ 1] = bpp
;
526 dri2_wl_get_buffers_with_format(driDrawable
,
528 attachments_with_format
, count
,
529 out_count
, loaderPrivate
);
531 free(attachments_with_format
);
537 image_get_buffers(__DRIdrawable
*driDrawable
,
541 uint32_t buffer_mask
,
542 struct __DRIimageList
*buffers
)
544 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
546 if (update_buffers(dri2_surf
) < 0)
549 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
550 buffers
->back
= dri2_surf
->back
->dri_image
;
556 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
559 (void) loaderPrivate
;
562 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
563 .base
= { __DRI_DRI2_LOADER
, 3 },
565 .getBuffers
= dri2_wl_get_buffers
,
566 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
567 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
570 static const __DRIimageLoaderExtension image_loader_extension
= {
571 .base
= { __DRI_IMAGE_LOADER
, 1 },
573 .getBuffers
= image_get_buffers
,
574 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
578 wayland_throttle_callback(void *data
,
579 struct wl_callback
*callback
,
582 struct dri2_egl_surface
*dri2_surf
= data
;
584 dri2_surf
->throttle_callback
= NULL
;
585 wl_callback_destroy(callback
);
588 static const struct wl_callback_listener throttle_listener
= {
589 .done
= wayland_throttle_callback
593 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
595 struct dri2_egl_display
*dri2_dpy
=
596 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
598 int fd
, stride
, name
;
600 if (dri2_surf
->current
->wl_buffer
!= NULL
)
603 if (dri2_dpy
->is_different_gpu
) {
604 image
= dri2_surf
->current
->linear_copy
;
606 image
= dri2_surf
->current
->dri_image
;
608 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
609 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
610 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
612 dri2_surf
->current
->wl_buffer
=
613 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
615 dri2_surf
->base
.Width
,
616 dri2_surf
->base
.Height
,
623 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
624 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
626 dri2_surf
->current
->wl_buffer
=
627 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
629 dri2_surf
->base
.Width
,
630 dri2_surf
->base
.Height
,
635 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
637 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
638 &wl_buffer_listener
, dri2_surf
);
642 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
648 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_win
->surface
)
649 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
652 for (i
= 0; i
< n_rects
; i
++) {
653 const int *rect
= &rects
[i
* 4];
655 wl_surface_damage_buffer(dri2_surf
->wl_win
->surface
,
657 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
663 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
666 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
672 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
673 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
676 while (dri2_surf
->throttle_callback
!= NULL
)
677 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
678 dri2_dpy
->wl_queue
) == -1)
681 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
682 if (dri2_surf
->color_buffers
[i
].age
> 0)
683 dri2_surf
->color_buffers
[i
].age
++;
685 /* Make sure we have a back buffer in case we're swapping without ever
687 if (get_back_bo(dri2_surf
) < 0) {
688 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
692 if (draw
->SwapInterval
> 0) {
693 dri2_surf
->throttle_callback
=
694 wl_surface_frame(dri2_surf
->wl_win
->surface
);
695 wl_callback_add_listener(dri2_surf
->throttle_callback
,
696 &throttle_listener
, dri2_surf
);
697 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
701 dri2_surf
->back
->age
= 1;
702 dri2_surf
->current
= dri2_surf
->back
;
703 dri2_surf
->back
= NULL
;
705 create_wl_buffer(dri2_surf
);
707 wl_surface_attach(dri2_surf
->wl_win
->surface
,
708 dri2_surf
->current
->wl_buffer
,
709 dri2_surf
->dx
, dri2_surf
->dy
);
711 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
712 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
713 /* reset resize growing parameters */
717 /* If the compositor doesn't support damage_buffer, we deliberately
718 * ignore the damage region and post maximum damage, due to
719 * https://bugs.freedesktop.org/78190 */
720 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
721 wl_surface_damage(dri2_surf
->wl_win
->surface
,
722 0, 0, INT32_MAX
, INT32_MAX
);
724 if (dri2_dpy
->is_different_gpu
) {
725 _EGLContext
*ctx
= _eglGetCurrentContext();
726 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
727 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
728 dri2_surf
->current
->linear_copy
,
729 dri2_surf
->current
->dri_image
,
730 0, 0, dri2_surf
->base
.Width
,
731 dri2_surf
->base
.Height
,
732 0, 0, dri2_surf
->base
.Width
,
733 dri2_surf
->base
.Height
, 0);
736 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
737 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
739 wl_surface_commit(dri2_surf
->wl_win
->surface
);
741 /* If we're not waiting for a frame callback then we'll at least throttle
742 * to a sync callback so that we always give a chance for the compositor to
743 * handle the commit and send a release event before checking for a free
745 if (dri2_surf
->throttle_callback
== NULL
) {
746 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
747 wl_callback_add_listener(dri2_surf
->throttle_callback
,
748 &throttle_listener
, dri2_surf
);
751 wl_display_flush(dri2_dpy
->wl_dpy
);
757 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
758 _EGLDisplay
*disp
, _EGLSurface
*surface
)
760 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
762 if (get_back_bo(dri2_surf
) < 0) {
763 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
767 return dri2_surf
->back
->age
;
771 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
773 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
776 static struct wl_buffer
*
777 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
781 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
782 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
783 __DRIimage
*image
= dri2_img
->dri_image
;
784 struct wl_buffer
*buffer
;
785 int width
, height
, format
, pitch
;
786 enum wl_drm_format wl_format
;
788 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
791 case __DRI_IMAGE_FORMAT_ARGB8888
:
792 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
794 wl_format
= WL_DRM_FORMAT_ARGB8888
;
796 case __DRI_IMAGE_FORMAT_XRGB8888
:
797 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
799 wl_format
= WL_DRM_FORMAT_XRGB8888
;
805 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
806 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
807 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
809 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
812 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
815 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
827 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
830 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
837 /* The buffer object will have been created with our internal event queue
838 * because it is using the wl_drm object as a proxy factory. We want the
839 * buffer to be used by the application so we'll reset it to the display's
840 * default event queue */
842 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
847 _eglError(EGL_BAD_MATCH
, "unsupported image format");
852 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
854 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
857 if (dri2_dpy
->is_render_node
) {
858 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
859 "authenticate for render-nodes");
862 dri2_dpy
->authenticated
= 0;
864 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
865 if (roundtrip(dri2_dpy
) < 0)
868 if (!dri2_dpy
->authenticated
)
871 /* reset authenticated */
872 dri2_dpy
->authenticated
= 1;
878 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
880 struct dri2_egl_display
*dri2_dpy
= data
;
883 dri2_dpy
->device_name
= strdup(device
);
884 if (!dri2_dpy
->device_name
)
887 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
888 if (dri2_dpy
->fd
== -1) {
889 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
890 dri2_dpy
->device_name
, strerror(errno
));
894 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
895 dri2_dpy
->authenticated
= 1;
897 drmGetMagic(dri2_dpy
->fd
, &magic
);
898 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
903 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
905 struct dri2_egl_display
*dri2_dpy
= data
;
908 case WL_DRM_FORMAT_ARGB8888
:
909 dri2_dpy
->formats
|= HAS_ARGB8888
;
911 case WL_DRM_FORMAT_XRGB8888
:
912 dri2_dpy
->formats
|= HAS_XRGB8888
;
914 case WL_DRM_FORMAT_RGB565
:
915 dri2_dpy
->formats
|= HAS_RGB565
;
921 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
923 struct dri2_egl_display
*dri2_dpy
= data
;
925 dri2_dpy
->capabilities
= value
;
929 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
931 struct dri2_egl_display
*dri2_dpy
= data
;
933 dri2_dpy
->authenticated
= 1;
936 static const struct wl_drm_listener drm_listener
= {
937 .device
= drm_handle_device
,
938 .format
= drm_handle_format
,
939 .authenticated
= drm_handle_authenticated
,
940 .capabilities
= drm_handle_capabilities
944 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
945 const char *interface
, uint32_t version
)
947 struct dri2_egl_display
*dri2_dpy
= data
;
951 if (strcmp(interface
, "wl_drm") == 0) {
953 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
954 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
959 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
964 static const struct wl_registry_listener registry_listener_drm
= {
965 .global
= registry_handle_global_drm
,
966 .global_remove
= registry_handle_global_remove
970 dri2_wl_swap_interval(_EGLDriver
*drv
,
975 if (interval
> surf
->Config
->MaxSwapInterval
)
976 interval
= surf
->Config
->MaxSwapInterval
;
977 else if (interval
< surf
->Config
->MinSwapInterval
)
978 interval
= surf
->Config
->MinSwapInterval
;
980 surf
->SwapInterval
= interval
;
986 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
988 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
990 /* We can't use values greater than 1 on Wayland because we are using the
991 * frame callback to synchronise the frame and the only way we be sure to
992 * get a frame callback is to attach a new buffer. Therefore we can't just
993 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
995 if (dri2_dpy
->config
)
996 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
997 "vblank_mode", &vblank_mode
);
998 switch (vblank_mode
) {
999 case DRI_CONF_VBLANK_NEVER
:
1000 dri2_dpy
->min_swap_interval
= 0;
1001 dri2_dpy
->max_swap_interval
= 0;
1002 dri2_dpy
->default_swap_interval
= 0;
1004 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1005 dri2_dpy
->min_swap_interval
= 1;
1006 dri2_dpy
->max_swap_interval
= 1;
1007 dri2_dpy
->default_swap_interval
= 1;
1009 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1010 dri2_dpy
->min_swap_interval
= 0;
1011 dri2_dpy
->max_swap_interval
= 1;
1012 dri2_dpy
->default_swap_interval
= 0;
1015 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1016 dri2_dpy
->min_swap_interval
= 0;
1017 dri2_dpy
->max_swap_interval
= 1;
1018 dri2_dpy
->default_swap_interval
= 1;
1023 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1024 .authenticate
= dri2_wl_authenticate
,
1025 .create_window_surface
= dri2_wl_create_window_surface
,
1026 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1027 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1028 .destroy_surface
= dri2_wl_destroy_surface
,
1029 .create_image
= dri2_create_image_khr
,
1030 .swap_interval
= dri2_wl_swap_interval
,
1031 .swap_buffers
= dri2_wl_swap_buffers
,
1032 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1033 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1034 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1035 .copy_buffers
= dri2_fallback_copy_buffers
,
1036 .query_buffer_age
= dri2_wl_query_buffer_age
,
1037 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1038 .get_sync_values
= dri2_fallback_get_sync_values
,
1039 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1042 static const __DRIextension
*dri2_loader_extensions
[] = {
1043 &dri2_loader_extension
.base
,
1044 &image_loader_extension
.base
,
1045 &image_lookup_extension
.base
,
1046 &use_invalidate
.base
,
1050 static const __DRIextension
*image_loader_extensions
[] = {
1051 &image_loader_extension
.base
,
1052 &image_lookup_extension
.base
,
1053 &use_invalidate
.base
,
1058 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1060 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1061 static const struct {
1062 const char *format_name
;
1064 unsigned int rgba_masks
[4];
1066 { "XRGB8888", HAS_XRGB8888
, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1067 { "ARGB8888", HAS_ARGB8888
, { 0xff0000, 0xff00, 0x00ff, 0 } },
1068 { "RGB565", HAS_RGB565
, { 0x00f800, 0x07e0, 0x001f, 0 } },
1070 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1071 unsigned int count
, i
, j
;
1074 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1075 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1076 struct dri2_egl_config
*dri2_conf
;
1078 if (!(dri2_dpy
->formats
& visuals
[j
].has_format
))
1081 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1082 count
+ 1, EGL_WINDOW_BIT
, NULL
, visuals
[j
].rgba_masks
);
1090 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1091 if (!format_count
[i
]) {
1092 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1093 visuals
[i
].format_name
);
1097 return (count
!= 0);
1101 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1103 struct dri2_egl_display
*dri2_dpy
;
1105 loader_set_logger(_eglLog
);
1107 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1109 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1111 disp
->DriverData
= (void *) dri2_dpy
;
1112 if (disp
->PlatformDisplay
== NULL
) {
1113 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1114 if (dri2_dpy
->wl_dpy
== NULL
)
1116 dri2_dpy
->own_device
= 1;
1118 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1121 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1123 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1124 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1125 goto cleanup_dpy_wrapper
;
1127 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1128 dri2_dpy
->wl_queue
);
1130 if (dri2_dpy
->own_device
)
1131 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1133 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1134 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1135 ®istry_listener_drm
, dri2_dpy
);
1136 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1137 goto cleanup_registry
;
1139 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1142 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1145 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1146 &dri2_dpy
->is_different_gpu
);
1147 if (dri2_dpy
->is_different_gpu
) {
1148 free(dri2_dpy
->device_name
);
1149 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1150 if (!dri2_dpy
->device_name
) {
1151 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1152 "for requested GPU");
1157 /* we have to do the check now, because loader_get_user_preferred_fd
1158 * will return a render-node when the requested gpu is different
1159 * to the server, but also if the client asks for the same gpu than
1160 * the server by requesting its pci-id */
1161 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1163 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1164 if (dri2_dpy
->driver_name
== NULL
) {
1165 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1169 if (!dri2_load_driver(disp
))
1170 goto cleanup_driver_name
;
1172 /* render nodes cannot use Gem names, and thus do not support
1173 * the __DRI_DRI2_LOADER extension */
1174 if (!dri2_dpy
->is_render_node
)
1175 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1177 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1179 if (!dri2_create_screen(disp
))
1180 goto cleanup_driver
;
1182 dri2_wl_setup_swap_interval(dri2_dpy
);
1184 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1185 * createImageFromFds support indicates that Prime export/import
1186 * is supported by the driver. Fall back to
1187 * gem names if we don't have Prime support. */
1189 if (dri2_dpy
->image
->base
.version
< 7 ||
1190 dri2_dpy
->image
->createImageFromFds
== NULL
)
1191 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1193 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1194 * The server needs to accept them */
1195 if (dri2_dpy
->is_render_node
&&
1196 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1197 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1198 goto cleanup_screen
;
1201 if (dri2_dpy
->is_different_gpu
&&
1202 (dri2_dpy
->image
->base
.version
< 9 ||
1203 dri2_dpy
->image
->blitImage
== NULL
)) {
1204 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1205 "Image extension in the driver is not "
1206 "compatible. Version 9 or later and blitImage() "
1208 goto cleanup_screen
;
1211 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1212 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1213 goto cleanup_screen
;
1216 dri2_set_WL_bind_wayland_display(drv
, disp
);
1217 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1218 * because the buffer of the EGLImage has likely a tiling mode the server
1219 * gpu won't support. These is no way to check for now. Thus do not support the
1221 if (!dri2_dpy
->is_different_gpu
) {
1222 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1224 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1225 dri2_fallback_create_wayland_buffer_from_image
;
1227 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1229 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1231 /* Fill vtbl last to prevent accidentally calling virtual function during
1234 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1239 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1241 dlclose(dri2_dpy
->driver
);
1242 cleanup_driver_name
:
1243 free(dri2_dpy
->driver_name
);
1245 close(dri2_dpy
->fd
);
1247 free(dri2_dpy
->device_name
);
1248 wl_drm_destroy(dri2_dpy
->wl_drm
);
1250 wl_registry_destroy(dri2_dpy
->wl_registry
);
1251 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1252 cleanup_dpy_wrapper
:
1253 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1254 if (disp
->PlatformDisplay
== NULL
)
1255 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1258 disp
->DriverData
= NULL
;
1264 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1266 if (format
== WL_SHM_FORMAT_RGB565
)
1268 else /* ARGB8888 || XRGB8888 */
1273 * Taken from weston shared/os-compatibility.c
1276 #ifndef HAVE_MKOSTEMP
1279 set_cloexec_or_close(int fd
)
1286 flags
= fcntl(fd
, F_GETFD
);
1290 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1303 * Taken from weston shared/os-compatibility.c
1307 create_tmpfile_cloexec(char *tmpname
)
1311 #ifdef HAVE_MKOSTEMP
1312 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1316 fd
= mkstemp(tmpname
);
1318 fd
= set_cloexec_or_close(fd
);
1327 * Taken from weston shared/os-compatibility.c
1329 * Create a new, unique, anonymous file of the given size, and
1330 * return the file descriptor for it. The file descriptor is set
1331 * CLOEXEC. The file is immediately suitable for mmap()'ing
1332 * the given size at offset zero.
1334 * The file should not have a permanent backing store like a disk,
1335 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1337 * The file name is deleted from the file system.
1339 * The file is suitable for buffer sharing between processes by
1340 * transmitting the file descriptor over Unix sockets using the
1341 * SCM_RIGHTS methods.
1343 * If the C library implements posix_fallocate(), it is used to
1344 * guarantee that disk space is available for the file at the
1345 * given size. If disk space is insufficent, errno is set to ENOSPC.
1346 * If posix_fallocate() is not supported, program may receive
1347 * SIGBUS on accessing mmap()'ed file contents instead.
1350 os_create_anonymous_file(off_t size
)
1352 static const char template[] = "/mesa-shared-XXXXXX";
1358 path
= getenv("XDG_RUNTIME_DIR");
1364 name
= malloc(strlen(path
) + sizeof(template));
1369 strcat(name
, template);
1371 fd
= create_tmpfile_cloexec(name
);
1378 ret
= ftruncate(fd
, size
);
1389 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1390 int format
, int w
, int h
,
1391 void **data
, int *size
,
1392 struct wl_buffer
**buffer
)
1394 struct wl_shm_pool
*pool
;
1395 int fd
, stride
, size_map
;
1398 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1399 size_map
= h
* stride
;
1401 /* Create a sharable buffer */
1402 fd
= os_create_anonymous_file(size_map
);
1406 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1407 if (data_map
== MAP_FAILED
) {
1412 /* Share it in a wl_buffer */
1413 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1414 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1415 wl_shm_pool_destroy(pool
);
1424 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1426 struct dri2_egl_display
*dri2_dpy
=
1427 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1430 /* we need to do the following operations only once per frame */
1431 if (dri2_surf
->back
)
1434 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1435 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1437 dri2_wl_release_buffers(dri2_surf
);
1439 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1440 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1441 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1442 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1443 dri2_surf
->current
= NULL
;
1446 /* find back buffer */
1448 /* There might be a buffer release already queued that wasn't processed */
1449 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
1451 /* try get free buffer already created */
1452 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1453 if (!dri2_surf
->color_buffers
[i
].locked
&&
1454 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1455 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1460 /* else choose any another free location */
1461 if (!dri2_surf
->back
) {
1462 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1463 if (!dri2_surf
->color_buffers
[i
].locked
) {
1464 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1465 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1467 dri2_surf
->base
.Width
,
1468 dri2_surf
->base
.Height
,
1469 &dri2_surf
->back
->data
,
1470 &dri2_surf
->back
->data_size
,
1471 &dri2_surf
->back
->wl_buffer
)) {
1472 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1475 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1476 dri2_dpy
->wl_queue
);
1477 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1478 &wl_buffer_listener
, dri2_surf
);
1484 if (!dri2_surf
->back
) {
1485 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1489 dri2_surf
->back
->locked
= 1;
1491 /* If we have an extra unlocked buffer at this point, we had to do triple
1492 * buffering for a while, but now can go back to just double buffering.
1493 * That means we can free any unlocked buffer now. */
1494 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1495 if (!dri2_surf
->color_buffers
[i
].locked
&&
1496 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1497 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1498 munmap(dri2_surf
->color_buffers
[i
].data
,
1499 dri2_surf
->color_buffers
[i
].data_size
);
1500 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1501 dri2_surf
->color_buffers
[i
].data
= NULL
;
1509 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1511 /* if there has been a resize: */
1512 if (!dri2_surf
->current
)
1515 return dri2_surf
->current
->data
;
1519 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1521 assert(dri2_surf
->back
);
1522 return dri2_surf
->back
->data
;
1526 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1528 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1530 while (dri2_surf
->throttle_callback
!= NULL
)
1531 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1532 dri2_dpy
->wl_queue
) == -1)
1535 if (dri2_surf
->base
.SwapInterval
> 0) {
1536 dri2_surf
->throttle_callback
=
1537 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1538 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1539 &throttle_listener
, dri2_surf
);
1540 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1541 dri2_dpy
->wl_queue
);
1544 dri2_surf
->current
= dri2_surf
->back
;
1545 dri2_surf
->back
= NULL
;
1547 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1548 dri2_surf
->current
->wl_buffer
,
1549 dri2_surf
->dx
, dri2_surf
->dy
);
1551 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1552 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1553 /* reset resize growing parameters */
1557 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1558 0, 0, INT32_MAX
, INT32_MAX
);
1559 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1561 /* If we're not waiting for a frame callback then we'll at least throttle
1562 * to a sync callback so that we always give a chance for the compositor to
1563 * handle the commit and send a release event before checking for a free
1565 if (dri2_surf
->throttle_callback
== NULL
) {
1566 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy_wrapper
);
1567 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1568 &throttle_listener
, dri2_surf
);
1571 wl_display_flush(dri2_dpy
->wl_dpy
);
1575 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1576 int *x
, int *y
, int *w
, int *h
,
1577 void *loaderPrivate
)
1579 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1581 (void) swrast_update_buffers(dri2_surf
);
1584 *w
= dri2_surf
->base
.Width
;
1585 *h
= dri2_surf
->base
.Height
;
1589 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1590 int x
, int y
, int w
, int h
,
1591 char *data
, void *loaderPrivate
)
1593 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1594 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1595 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1596 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1597 int dst_stride
= copy_width
;
1600 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1602 memset(data
, 0, copy_width
* h
);
1606 assert(data
!= src
);
1607 assert(copy_width
<= src_stride
);
1610 src
+= y
* src_stride
;
1613 if (copy_width
> src_stride
-x_offset
)
1614 copy_width
= src_stride
-x_offset
;
1615 if (h
> dri2_surf
->base
.Height
-y
)
1616 h
= dri2_surf
->base
.Height
-y
;
1619 memcpy(dst
, src
, copy_width
);
1626 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1627 int x
, int y
, int w
, int h
, int stride
,
1628 char *data
, void *loaderPrivate
)
1630 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1631 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1632 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1633 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1636 assert(copy_width
<= stride
);
1638 (void) swrast_update_buffers(dri2_surf
);
1639 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1641 /* partial copy, copy old content */
1642 if (copy_width
< dst_stride
)
1643 dri2_wl_swrast_get_image(draw
, 0, 0,
1644 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1645 dst
, loaderPrivate
);
1648 dst
+= y
* dst_stride
;
1652 /* drivers expect we do these checks (and some rely on it) */
1653 if (copy_width
> dst_stride
-x_offset
)
1654 copy_width
= dst_stride
-x_offset
;
1655 if (h
> dri2_surf
->base
.Height
-y
)
1656 h
= dri2_surf
->base
.Height
-y
;
1659 memcpy(dst
, src
, copy_width
);
1663 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1667 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1668 int x
, int y
, int w
, int h
,
1669 char *data
, void *loaderPrivate
)
1671 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1674 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1675 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1676 stride
, data
, loaderPrivate
);
1680 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1682 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1683 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1685 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1690 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1692 struct dri2_egl_display
*dri2_dpy
= data
;
1695 case WL_SHM_FORMAT_ARGB8888
:
1696 dri2_dpy
->formats
|= HAS_ARGB8888
;
1698 case WL_SHM_FORMAT_XRGB8888
:
1699 dri2_dpy
->formats
|= HAS_XRGB8888
;
1701 case WL_SHM_FORMAT_RGB565
:
1702 dri2_dpy
->formats
|= HAS_RGB565
;
1707 static const struct wl_shm_listener shm_listener
= {
1708 .format
= shm_handle_format
1712 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1713 const char *interface
, uint32_t version
)
1715 struct dri2_egl_display
*dri2_dpy
= data
;
1717 if (strcmp(interface
, "wl_shm") == 0) {
1719 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1720 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1724 static const struct wl_registry_listener registry_listener_swrast
= {
1725 .global
= registry_handle_global_swrast
,
1726 .global_remove
= registry_handle_global_remove
1729 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1730 .authenticate
= NULL
,
1731 .create_window_surface
= dri2_wl_create_window_surface
,
1732 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1733 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1734 .destroy_surface
= dri2_wl_destroy_surface
,
1735 .create_image
= dri2_fallback_create_image_khr
,
1736 .swap_interval
= dri2_wl_swap_interval
,
1737 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1738 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1739 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1740 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1741 .copy_buffers
= dri2_fallback_copy_buffers
,
1742 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1743 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1744 .get_sync_values
= dri2_fallback_get_sync_values
,
1745 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1748 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1749 .base
= { __DRI_SWRAST_LOADER
, 2 },
1751 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
1752 .putImage
= dri2_wl_swrast_put_image
,
1753 .getImage
= dri2_wl_swrast_get_image
,
1754 .putImage2
= dri2_wl_swrast_put_image2
,
1757 static const __DRIextension
*swrast_loader_extensions
[] = {
1758 &swrast_loader_extension
.base
,
1763 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1765 struct dri2_egl_display
*dri2_dpy
;
1767 loader_set_logger(_eglLog
);
1769 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1771 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1773 disp
->DriverData
= (void *) dri2_dpy
;
1774 if (disp
->PlatformDisplay
== NULL
) {
1775 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1776 if (dri2_dpy
->wl_dpy
== NULL
)
1778 dri2_dpy
->own_device
= 1;
1780 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1783 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1785 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1786 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1787 goto cleanup_dpy_wrapper
;
1789 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1790 dri2_dpy
->wl_queue
);
1792 if (dri2_dpy
->own_device
)
1793 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1795 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1796 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1797 ®istry_listener_swrast
, dri2_dpy
);
1799 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1800 goto cleanup_registry
;
1802 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1806 dri2_dpy
->driver_name
= strdup("swrast");
1807 if (!dri2_load_driver_swrast(disp
))
1810 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1812 if (!dri2_create_screen(disp
))
1813 goto cleanup_driver
;
1815 dri2_wl_setup_swap_interval(dri2_dpy
);
1817 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1818 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1819 goto cleanup_screen
;
1822 /* Fill vtbl last to prevent accidentally calling virtual function during
1825 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1830 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1832 dlclose(dri2_dpy
->driver
);
1834 wl_shm_destroy(dri2_dpy
->wl_shm
);
1836 wl_registry_destroy(dri2_dpy
->wl_registry
);
1837 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
1838 cleanup_dpy_wrapper
:
1839 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1840 if (disp
->PlatformDisplay
== NULL
)
1841 wl_display_disconnect(dri2_dpy
->wl_dpy
);
1844 disp
->DriverData
= NULL
;
1850 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1852 EGLBoolean initialized
= EGL_TRUE
;
1854 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1857 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1858 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1861 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);