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 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
64 wl_callback_destroy(callback
);
67 static const struct wl_callback_listener sync_listener
= {
72 roundtrip(struct dri2_egl_display
*dri2_dpy
)
74 struct wl_callback
*callback
;
75 int done
= 0, ret
= 0;
77 callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
78 wl_callback_add_listener(callback
, &sync_listener
, &done
);
79 wl_proxy_set_queue((struct wl_proxy
*) callback
, dri2_dpy
->wl_queue
);
80 while (ret
!= -1 && !done
)
81 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
84 wl_callback_destroy(callback
);
90 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
92 struct dri2_egl_surface
*dri2_surf
= data
;
95 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
96 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
99 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
100 wl_buffer_destroy(buffer
);
104 dri2_surf
->color_buffers
[i
].locked
= 0;
107 static struct wl_buffer_listener wl_buffer_listener
= {
112 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
114 struct dri2_egl_surface
*dri2_surf
= data
;
115 struct dri2_egl_display
*dri2_dpy
=
116 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
118 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
122 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
125 dri2_wl_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
126 _EGLConfig
*conf
, void *native_window
,
127 const EGLint
*attrib_list
)
129 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
130 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
131 struct wl_egl_window
*window
= native_window
;
132 struct dri2_egl_surface
*dri2_surf
;
136 dri2_surf
= calloc(1, sizeof *dri2_surf
);
138 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
142 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
145 if (conf
->RedSize
== 5)
146 dri2_surf
->format
= WL_DRM_FORMAT_RGB565
;
147 else if (conf
->AlphaSize
== 0)
148 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
150 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
152 dri2_surf
->wl_win
= window
;
154 dri2_surf
->wl_win
->private = dri2_surf
;
155 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
157 dri2_surf
->base
.Width
= -1;
158 dri2_surf
->base
.Height
= -1;
160 dri2_surf
->dri_drawable
=
161 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
162 dri2_conf
->dri_double_config
,
164 if (dri2_surf
->dri_drawable
== NULL
) {
165 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
166 goto cleanup_dri_drawable
;
169 return &dri2_surf
->base
;
171 cleanup_dri_drawable
:
172 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
180 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
183 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
184 _EGLConfig
*conf
, void *native_window
,
185 const EGLint
*attrib_list
)
187 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
190 surf
= dri2_wl_create_surface(drv
, disp
, conf
, native_window
, attrib_list
);
193 dri2_wl_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
199 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
200 _EGLConfig
*conf
, void *native_window
,
201 const EGLint
*attrib_list
)
203 /* From the EGL_EXT_platform_wayland spec, version 3:
205 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
206 * that belongs to Wayland. Any such call fails and generates
209 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
215 * Called via eglDestroySurface(), drv->API.DestroySurface().
218 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
220 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
221 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
226 if (!_eglPutSurface(surf
))
229 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
231 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
232 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
233 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
234 if (dri2_surf
->color_buffers
[i
].dri_image
)
235 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
236 if (dri2_surf
->color_buffers
[i
].linear_copy
)
237 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
238 if (dri2_surf
->color_buffers
[i
].data
)
239 munmap(dri2_surf
->color_buffers
[i
].data
,
240 dri2_surf
->color_buffers
[i
].data_size
);
243 if (dri2_dpy
->dri2
) {
244 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
245 if (dri2_surf
->dri_buffers
[i
] &&
246 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
247 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
248 dri2_surf
->dri_buffers
[i
]);
251 if (dri2_surf
->throttle_callback
)
252 wl_callback_destroy(dri2_surf
->throttle_callback
);
254 dri2_surf
->wl_win
->private = NULL
;
255 dri2_surf
->wl_win
->resize_callback
= NULL
;
263 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
265 struct dri2_egl_display
*dri2_dpy
=
266 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
269 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
270 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
271 !dri2_surf
->color_buffers
[i
].locked
)
272 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
273 if (dri2_surf
->color_buffers
[i
].dri_image
)
274 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
275 if (dri2_surf
->color_buffers
[i
].linear_copy
)
276 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
277 if (dri2_surf
->color_buffers
[i
].data
)
278 munmap(dri2_surf
->color_buffers
[i
].data
,
279 dri2_surf
->color_buffers
[i
].data_size
);
281 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
282 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
283 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
284 dri2_surf
->color_buffers
[i
].data
= NULL
;
285 dri2_surf
->color_buffers
[i
].locked
= 0;
288 if (dri2_dpy
->dri2
) {
289 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
290 if (dri2_surf
->dri_buffers
[i
] &&
291 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
292 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
293 dri2_surf
->dri_buffers
[i
]);
298 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
300 struct dri2_egl_display
*dri2_dpy
=
301 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
303 unsigned int dri_image_format
;
305 /* currently supports three WL DRM formats,
306 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
307 * and WL_DRM_FORMAT_RGB565
309 switch (dri2_surf
->format
) {
310 case WL_DRM_FORMAT_ARGB8888
:
311 dri_image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
313 case WL_DRM_FORMAT_XRGB8888
:
314 dri_image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
316 case WL_DRM_FORMAT_RGB565
:
317 dri_image_format
= __DRI_IMAGE_FORMAT_RGB565
;
320 /* format is not supported */
324 /* We always want to throttle to some event (either a frame callback or
325 * a sync request) after the commit so that we can be sure the
326 * compositor has had a chance to handle it and send us a release event
327 * before we look for a free buffer */
328 while (dri2_surf
->throttle_callback
!= NULL
)
329 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
330 dri2_dpy
->wl_queue
) == -1)
333 if (dri2_surf
->back
== NULL
) {
334 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
335 /* Get an unlocked buffer, preferrably one with a dri_buffer
336 * already allocated. */
337 if (dri2_surf
->color_buffers
[i
].locked
)
339 if (dri2_surf
->back
== NULL
)
340 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
341 else if (dri2_surf
->back
->dri_image
== NULL
)
342 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
346 if (dri2_surf
->back
== NULL
)
349 if (dri2_dpy
->is_different_gpu
&&
350 dri2_surf
->back
->linear_copy
== NULL
) {
351 dri2_surf
->back
->linear_copy
=
352 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
353 dri2_surf
->base
.Width
,
354 dri2_surf
->base
.Height
,
356 __DRI_IMAGE_USE_SHARE
|
357 __DRI_IMAGE_USE_LINEAR
,
359 if (dri2_surf
->back
->linear_copy
== NULL
)
363 if (dri2_surf
->back
->dri_image
== NULL
) {
364 dri2_surf
->back
->dri_image
=
365 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
366 dri2_surf
->base
.Width
,
367 dri2_surf
->base
.Height
,
369 dri2_dpy
->is_different_gpu
?
370 0 : __DRI_IMAGE_USE_SHARE
,
372 dri2_surf
->back
->age
= 0;
374 if (dri2_surf
->back
->dri_image
== NULL
)
377 dri2_surf
->back
->locked
= 1;
384 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
386 struct dri2_egl_display
*dri2_dpy
=
387 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
391 image
= dri2_surf
->back
->dri_image
;
393 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
394 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
396 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
398 buffer
->pitch
= pitch
;
404 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
405 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
407 struct dri2_egl_display
*dri2_dpy
=
408 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
409 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
412 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
414 dri2_surf
->base
.Width
,
415 dri2_surf
->base
.Height
);
416 dri2_surf
->dri_buffers
[attachment
] = b
;
421 memcpy(buffer
, b
, sizeof *buffer
);
427 update_buffers(struct dri2_egl_surface
*dri2_surf
)
429 struct dri2_egl_display
*dri2_dpy
=
430 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
433 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
434 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
436 dri2_wl_release_buffers(dri2_surf
);
438 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
439 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
440 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
441 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
444 if (get_back_bo(dri2_surf
) < 0) {
445 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
449 /* If we have an extra unlocked buffer at this point, we had to do triple
450 * buffering for a while, but now can go back to just double buffering.
451 * That means we can free any unlocked buffer now. */
452 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
453 if (!dri2_surf
->color_buffers
[i
].locked
&&
454 dri2_surf
->color_buffers
[i
].wl_buffer
) {
455 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
456 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
457 if (dri2_dpy
->is_different_gpu
)
458 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
459 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
460 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
461 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
469 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
470 int *width
, int *height
,
471 unsigned int *attachments
, int count
,
472 int *out_count
, void *loaderPrivate
)
474 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
477 if (update_buffers(dri2_surf
) < 0)
480 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
481 switch (attachments
[i
]) {
482 case __DRI_BUFFER_BACK_LEFT
:
483 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
486 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
487 &dri2_surf
->buffers
[j
]) < 0) {
488 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
499 *width
= dri2_surf
->base
.Width
;
500 *height
= dri2_surf
->base
.Height
;
502 return dri2_surf
->buffers
;
506 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
507 int *width
, int *height
,
508 unsigned int *attachments
, int count
,
509 int *out_count
, void *loaderPrivate
)
511 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
512 unsigned int *attachments_with_format
;
518 switch (dri2_surf
->format
) {
519 case WL_DRM_FORMAT_ARGB8888
:
520 case WL_DRM_FORMAT_XRGB8888
:
523 case WL_DRM_FORMAT_RGB565
:
527 /* format is not supported */
531 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
532 if (!attachments_with_format
) {
537 for (i
= 0; i
< count
; ++i
) {
538 attachments_with_format
[2*i
] = attachments
[i
];
539 attachments_with_format
[2*i
+ 1] = bpp
;
543 dri2_wl_get_buffers_with_format(driDrawable
,
545 attachments_with_format
, count
,
546 out_count
, loaderPrivate
);
548 free(attachments_with_format
);
554 image_get_buffers(__DRIdrawable
*driDrawable
,
558 uint32_t buffer_mask
,
559 struct __DRIimageList
*buffers
)
561 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
563 if (update_buffers(dri2_surf
) < 0)
566 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
567 buffers
->back
= dri2_surf
->back
->dri_image
;
573 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
576 (void) loaderPrivate
;
579 static const __DRIimageLoaderExtension image_loader_extension
= {
580 .base
= { __DRI_IMAGE_LOADER
, 1 },
582 .getBuffers
= image_get_buffers
,
583 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
587 wayland_throttle_callback(void *data
,
588 struct wl_callback
*callback
,
591 struct dri2_egl_surface
*dri2_surf
= data
;
593 dri2_surf
->throttle_callback
= NULL
;
594 wl_callback_destroy(callback
);
597 static const struct wl_callback_listener throttle_listener
= {
598 wayland_throttle_callback
602 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
604 struct dri2_egl_display
*dri2_dpy
=
605 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
607 int fd
, stride
, name
;
609 if (dri2_surf
->current
->wl_buffer
!= NULL
)
612 if (dri2_dpy
->is_different_gpu
) {
613 image
= dri2_surf
->current
->linear_copy
;
615 image
= dri2_surf
->current
->dri_image
;
617 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
618 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
619 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
621 dri2_surf
->current
->wl_buffer
=
622 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
624 dri2_surf
->base
.Width
,
625 dri2_surf
->base
.Height
,
632 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
633 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
635 dri2_surf
->current
->wl_buffer
=
636 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
638 dri2_surf
->base
.Width
,
639 dri2_surf
->base
.Height
,
644 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
646 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
647 &wl_buffer_listener
, dri2_surf
);
651 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
654 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
660 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
661 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
664 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
665 if (dri2_surf
->color_buffers
[i
].age
> 0)
666 dri2_surf
->color_buffers
[i
].age
++;
668 /* Make sure we have a back buffer in case we're swapping without ever
670 if (get_back_bo(dri2_surf
) < 0) {
671 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
675 if (draw
->SwapInterval
> 0) {
676 dri2_surf
->throttle_callback
=
677 wl_surface_frame(dri2_surf
->wl_win
->surface
);
678 wl_callback_add_listener(dri2_surf
->throttle_callback
,
679 &throttle_listener
, dri2_surf
);
680 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
684 dri2_surf
->back
->age
= 1;
685 dri2_surf
->current
= dri2_surf
->back
;
686 dri2_surf
->back
= NULL
;
688 create_wl_buffer(dri2_surf
);
690 wl_surface_attach(dri2_surf
->wl_win
->surface
,
691 dri2_surf
->current
->wl_buffer
,
692 dri2_surf
->dx
, dri2_surf
->dy
);
694 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
695 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
696 /* reset resize growing parameters */
701 wl_surface_damage(dri2_surf
->wl_win
->surface
,
702 0, 0, INT32_MAX
, INT32_MAX
);
704 for (i
= 0; i
< n_rects
; i
++) {
705 const int *rect
= &rects
[i
* 4];
706 wl_surface_damage(dri2_surf
->wl_win
->surface
,
708 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
713 if (dri2_dpy
->is_different_gpu
) {
714 _EGLContext
*ctx
= _eglGetCurrentContext();
715 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
716 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
717 dri2_surf
->current
->linear_copy
,
718 dri2_surf
->current
->dri_image
,
719 0, 0, dri2_surf
->base
.Width
,
720 dri2_surf
->base
.Height
,
721 0, 0, dri2_surf
->base
.Width
,
722 dri2_surf
->base
.Height
, 0);
725 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
726 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
728 wl_surface_commit(dri2_surf
->wl_win
->surface
);
730 /* If we're not waiting for a frame callback then we'll at least throttle
731 * to a sync callback so that we always give a chance for the compositor to
732 * handle the commit and send a release event before checking for a free
734 if (dri2_surf
->throttle_callback
== NULL
) {
735 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
736 wl_callback_add_listener(dri2_surf
->throttle_callback
,
737 &throttle_listener
, dri2_surf
);
738 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
742 wl_display_flush(dri2_dpy
->wl_dpy
);
748 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
749 _EGLDisplay
*disp
, _EGLSurface
*surface
)
751 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
753 if (get_back_bo(dri2_surf
) < 0) {
754 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
758 return dri2_surf
->back
->age
;
762 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
764 return dri2_wl_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
767 static struct wl_buffer
*
768 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
772 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
773 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
774 __DRIimage
*image
= dri2_img
->dri_image
;
775 struct wl_buffer
*buffer
;
776 int width
, height
, format
, pitch
;
777 enum wl_drm_format wl_format
;
779 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
782 case __DRI_IMAGE_FORMAT_ARGB8888
:
783 if (!(dri2_dpy
->formats
& HAS_ARGB8888
))
785 wl_format
= WL_DRM_FORMAT_ARGB8888
;
787 case __DRI_IMAGE_FORMAT_XRGB8888
:
788 if (!(dri2_dpy
->formats
& HAS_XRGB8888
))
790 wl_format
= WL_DRM_FORMAT_XRGB8888
;
796 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
797 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
, &height
);
798 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
800 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
803 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
806 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
818 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
821 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
828 /* The buffer object will have been created with our internal event queue
829 * because it is using the wl_drm object as a proxy factory. We want the
830 * buffer to be used by the application so we'll reset it to the display's
831 * default event queue */
833 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
838 _eglError(EGL_BAD_MATCH
, "unsupported image format");
843 is_fd_render_node(int fd
)
847 if (fstat(fd
, &render
))
850 if (!S_ISCHR(render
.st_mode
))
853 if (render
.st_rdev
& 0x80)
859 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
861 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
864 if (dri2_dpy
->is_render_node
) {
865 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
866 "authenticate for render-nodes");
869 dri2_dpy
->authenticated
= 0;
871 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
872 if (roundtrip(dri2_dpy
) < 0)
875 if (!dri2_dpy
->authenticated
)
878 /* reset authenticated */
879 dri2_dpy
->authenticated
= 1;
885 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
887 struct dri2_egl_display
*dri2_dpy
= data
;
890 dri2_dpy
->device_name
= strdup(device
);
891 if (!dri2_dpy
->device_name
)
894 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
895 if (dri2_dpy
->fd
== -1) {
896 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
897 dri2_dpy
->device_name
, strerror(errno
));
901 if (is_fd_render_node(dri2_dpy
->fd
)) {
902 dri2_dpy
->authenticated
= 1;
904 drmGetMagic(dri2_dpy
->fd
, &magic
);
905 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
910 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
912 struct dri2_egl_display
*dri2_dpy
= data
;
915 case WL_DRM_FORMAT_ARGB8888
:
916 dri2_dpy
->formats
|= HAS_ARGB8888
;
918 case WL_DRM_FORMAT_XRGB8888
:
919 dri2_dpy
->formats
|= HAS_XRGB8888
;
921 case WL_DRM_FORMAT_RGB565
:
922 dri2_dpy
->formats
|= HAS_RGB565
;
928 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
930 struct dri2_egl_display
*dri2_dpy
= data
;
932 dri2_dpy
->capabilities
= value
;
936 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
938 struct dri2_egl_display
*dri2_dpy
= data
;
940 dri2_dpy
->authenticated
= 1;
943 static const struct wl_drm_listener drm_listener
= {
946 drm_handle_authenticated
,
947 drm_handle_capabilities
951 registry_handle_global_drm(void *data
, struct wl_registry
*registry
, uint32_t name
,
952 const char *interface
, uint32_t version
)
954 struct dri2_egl_display
*dri2_dpy
= data
;
958 if (strcmp(interface
, "wl_drm") == 0) {
960 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
961 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
966 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
971 static const struct wl_registry_listener registry_listener_drm
= {
972 registry_handle_global_drm
,
973 registry_handle_global_remove
977 dri2_wl_swap_interval(_EGLDriver
*drv
,
982 if (interval
> surf
->Config
->MaxSwapInterval
)
983 interval
= surf
->Config
->MaxSwapInterval
;
984 else if (interval
< surf
->Config
->MinSwapInterval
)
985 interval
= surf
->Config
->MinSwapInterval
;
987 surf
->SwapInterval
= interval
;
993 dri2_wl_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
995 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
997 /* We can't use values greater than 1 on Wayland because we are using the
998 * frame callback to synchronise the frame and the only way we be sure to
999 * get a frame callback is to attach a new buffer. Therefore we can't just
1000 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1002 if (dri2_dpy
->config
)
1003 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1004 "vblank_mode", &vblank_mode
);
1005 switch (vblank_mode
) {
1006 case DRI_CONF_VBLANK_NEVER
:
1007 dri2_dpy
->min_swap_interval
= 0;
1008 dri2_dpy
->max_swap_interval
= 0;
1009 dri2_dpy
->default_swap_interval
= 0;
1011 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1012 dri2_dpy
->min_swap_interval
= 1;
1013 dri2_dpy
->max_swap_interval
= 1;
1014 dri2_dpy
->default_swap_interval
= 1;
1016 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1017 dri2_dpy
->min_swap_interval
= 0;
1018 dri2_dpy
->max_swap_interval
= 1;
1019 dri2_dpy
->default_swap_interval
= 0;
1022 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1023 dri2_dpy
->min_swap_interval
= 0;
1024 dri2_dpy
->max_swap_interval
= 1;
1025 dri2_dpy
->default_swap_interval
= 1;
1030 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1031 .authenticate
= dri2_wl_authenticate
,
1032 .create_window_surface
= dri2_wl_create_window_surface
,
1033 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1034 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1035 .destroy_surface
= dri2_wl_destroy_surface
,
1036 .create_image
= dri2_create_image_khr
,
1037 .swap_interval
= dri2_wl_swap_interval
,
1038 .swap_buffers
= dri2_wl_swap_buffers
,
1039 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1040 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1041 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1042 .copy_buffers
= dri2_fallback_copy_buffers
,
1043 .query_buffer_age
= dri2_wl_query_buffer_age
,
1044 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1045 .get_sync_values
= dri2_fallback_get_sync_values
,
1049 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1051 struct dri2_egl_display
*dri2_dpy
;
1052 const __DRIconfig
*config
;
1055 static const unsigned int argb_masks
[4] =
1056 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1057 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1058 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1060 loader_set_logger(_eglLog
);
1062 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1064 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1066 disp
->DriverData
= (void *) dri2_dpy
;
1067 if (disp
->PlatformDisplay
== NULL
) {
1068 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1069 if (dri2_dpy
->wl_dpy
== NULL
)
1071 dri2_dpy
->own_device
= 1;
1073 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1076 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1078 if (dri2_dpy
->own_device
)
1079 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1081 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1082 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1083 dri2_dpy
->wl_queue
);
1084 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1085 ®istry_listener_drm
, dri2_dpy
);
1086 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1087 goto cleanup_registry
;
1089 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1092 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1095 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1096 &dri2_dpy
->is_different_gpu
);
1097 if (dri2_dpy
->is_different_gpu
) {
1098 free(dri2_dpy
->device_name
);
1099 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1100 if (!dri2_dpy
->device_name
) {
1101 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1102 "for requested GPU");
1107 /* we have to do the check now, because loader_get_user_preferred_fd
1108 * will return a render-node when the requested gpu is different
1109 * to the server, but also if the client asks for the same gpu than
1110 * the server by requesting its pci-id */
1111 dri2_dpy
->is_render_node
= is_fd_render_node(dri2_dpy
->fd
);
1113 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
1114 if (dri2_dpy
->driver_name
== NULL
) {
1115 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1119 if (!dri2_load_driver(disp
))
1120 goto cleanup_driver_name
;
1122 dri2_dpy
->extensions
[0] = &image_loader_extension
.base
;
1123 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1124 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
1126 /* render nodes cannot use Gem names, and thus do not support
1127 * the __DRI_DRI2_LOADER extension */
1128 if (!dri2_dpy
->is_render_node
) {
1129 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1130 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1131 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_wl_get_buffers
;
1132 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_wl_flush_front_buffer
;
1133 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1134 dri2_wl_get_buffers_with_format
;
1135 dri2_dpy
->extensions
[3] = &dri2_dpy
->dri2_loader_extension
.base
;
1136 dri2_dpy
->extensions
[4] = NULL
;
1138 dri2_dpy
->extensions
[3] = NULL
;
1140 dri2_dpy
->swap_available
= EGL_TRUE
;
1142 if (!dri2_create_screen(disp
))
1143 goto cleanup_driver
;
1145 dri2_wl_setup_swap_interval(dri2_dpy
);
1147 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1148 * createImageFromFds support indicates that Prime export/import
1149 * is supported by the driver. Fall back to
1150 * gem names if we don't have Prime support. */
1152 if (dri2_dpy
->image
->base
.version
< 7 ||
1153 dri2_dpy
->image
->createImageFromFds
== NULL
)
1154 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1156 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1157 * The server needs to accept them */
1158 if (dri2_dpy
->is_render_node
&&
1159 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1160 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1161 goto cleanup_screen
;
1164 if (dri2_dpy
->is_different_gpu
&&
1165 (dri2_dpy
->image
->base
.version
< 9 ||
1166 dri2_dpy
->image
->blitImage
== NULL
)) {
1167 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1168 "Image extension in the driver is not "
1169 "compatible. Version 9 or later and blitImage() "
1171 goto cleanup_screen
;
1174 types
= EGL_WINDOW_BIT
;
1175 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1176 config
= dri2_dpy
->driver_configs
[i
];
1177 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1178 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1179 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1180 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1181 if (dri2_dpy
->formats
& HAS_RGB565
)
1182 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1185 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1186 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1187 * because the buffer of the EGLImage has likely a tiling mode the server
1188 * gpu won't support. These is no way to check for now. Thus do not support the
1190 if (!dri2_dpy
->is_different_gpu
) {
1191 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1193 dri2_wl_display_vtbl
.create_wayland_buffer_from_image
=
1194 dri2_fallback_create_wayland_buffer_from_image
;
1196 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1198 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1200 /* Fill vtbl last to prevent accidentally calling virtual function during
1203 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1208 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1210 dlclose(dri2_dpy
->driver
);
1211 cleanup_driver_name
:
1212 free(dri2_dpy
->driver_name
);
1214 close(dri2_dpy
->fd
);
1216 free(dri2_dpy
->device_name
);
1217 wl_drm_destroy(dri2_dpy
->wl_drm
);
1219 wl_registry_destroy(dri2_dpy
->wl_registry
);
1220 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1228 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1230 if (format
== WL_SHM_FORMAT_RGB565
)
1232 else /* ARGB8888 || XRGB8888 */
1237 * Taken from weston shared/os-compatibility.c
1241 set_cloexec_or_close(int fd
)
1248 flags
= fcntl(fd
, F_GETFD
);
1252 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
1263 * Taken from weston shared/os-compatibility.c
1267 create_tmpfile_cloexec(char *tmpname
)
1271 #ifdef HAVE_MKOSTEMP
1272 fd
= mkostemp(tmpname
, O_CLOEXEC
);
1276 fd
= mkstemp(tmpname
);
1278 fd
= set_cloexec_or_close(fd
);
1287 * Taken from weston shared/os-compatibility.c
1289 * Create a new, unique, anonymous file of the given size, and
1290 * return the file descriptor for it. The file descriptor is set
1291 * CLOEXEC. The file is immediately suitable for mmap()'ing
1292 * the given size at offset zero.
1294 * The file should not have a permanent backing store like a disk,
1295 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1297 * The file name is deleted from the file system.
1299 * The file is suitable for buffer sharing between processes by
1300 * transmitting the file descriptor over Unix sockets using the
1301 * SCM_RIGHTS methods.
1303 * If the C library implements posix_fallocate(), it is used to
1304 * guarantee that disk space is available for the file at the
1305 * given size. If disk space is insufficent, errno is set to ENOSPC.
1306 * If posix_fallocate() is not supported, program may receive
1307 * SIGBUS on accessing mmap()'ed file contents instead.
1310 os_create_anonymous_file(off_t size
)
1312 static const char template[] = "/mesa-shared-XXXXXX";
1318 path
= getenv("XDG_RUNTIME_DIR");
1324 name
= malloc(strlen(path
) + sizeof(template));
1329 strcat(name
, template);
1331 fd
= create_tmpfile_cloexec(name
);
1338 ret
= ftruncate(fd
, size
);
1349 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display
*dri2_dpy
,
1350 int format
, int w
, int h
,
1351 void **data
, int *size
,
1352 struct wl_buffer
**buffer
)
1354 struct wl_shm_pool
*pool
;
1355 int fd
, stride
, size_map
;
1358 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1359 size_map
= h
* stride
;
1361 /* Create a sharable buffer */
1362 fd
= os_create_anonymous_file(size_map
);
1366 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1367 if (data_map
== MAP_FAILED
) {
1372 /* Share it in a wl_buffer */
1373 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1374 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1375 wl_shm_pool_destroy(pool
);
1384 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1386 struct dri2_egl_display
*dri2_dpy
=
1387 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1390 /* we need to do the following operations only once per frame */
1391 if (dri2_surf
->back
)
1394 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1395 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1397 dri2_wl_release_buffers(dri2_surf
);
1399 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1400 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1401 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1402 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1403 dri2_surf
->current
= NULL
;
1406 /* find back buffer */
1408 /* We always want to throttle to some event (either a frame callback or
1409 * a sync request) after the commit so that we can be sure the
1410 * compositor has had a chance to handle it and send us a release event
1411 * before we look for a free buffer */
1412 while (dri2_surf
->throttle_callback
!= NULL
)
1413 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1414 dri2_dpy
->wl_queue
) == -1)
1417 /* try get free buffer already created */
1418 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1419 if (!dri2_surf
->color_buffers
[i
].locked
&&
1420 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1421 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1426 /* else choose any another free location */
1427 if (!dri2_surf
->back
) {
1428 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1429 if (!dri2_surf
->color_buffers
[i
].locked
) {
1430 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1431 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy
,
1433 dri2_surf
->base
.Width
,
1434 dri2_surf
->base
.Height
,
1435 &dri2_surf
->back
->data
,
1436 &dri2_surf
->back
->data_size
,
1437 &dri2_surf
->back
->wl_buffer
)) {
1438 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1441 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->back
->wl_buffer
,
1442 dri2_dpy
->wl_queue
);
1443 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1444 &wl_buffer_listener
, dri2_surf
);
1450 if (!dri2_surf
->back
) {
1451 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1455 dri2_surf
->back
->locked
= 1;
1457 /* If we have an extra unlocked buffer at this point, we had to do triple
1458 * buffering for a while, but now can go back to just double buffering.
1459 * That means we can free any unlocked buffer now. */
1460 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1461 if (!dri2_surf
->color_buffers
[i
].locked
&&
1462 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1463 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1464 munmap(dri2_surf
->color_buffers
[i
].data
,
1465 dri2_surf
->color_buffers
[i
].data_size
);
1466 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1467 dri2_surf
->color_buffers
[i
].data
= NULL
;
1475 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1477 /* if there has been a resize: */
1478 if (!dri2_surf
->current
)
1481 return dri2_surf
->current
->data
;
1485 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1487 assert(dri2_surf
->back
);
1488 return dri2_surf
->back
->data
;
1492 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1494 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1496 if (dri2_surf
->base
.SwapInterval
> 0) {
1497 dri2_surf
->throttle_callback
=
1498 wl_surface_frame(dri2_surf
->wl_win
->surface
);
1499 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1500 &throttle_listener
, dri2_surf
);
1501 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1502 dri2_dpy
->wl_queue
);
1505 dri2_surf
->current
= dri2_surf
->back
;
1506 dri2_surf
->back
= NULL
;
1508 wl_surface_attach(dri2_surf
->wl_win
->surface
,
1509 dri2_surf
->current
->wl_buffer
,
1510 dri2_surf
->dx
, dri2_surf
->dy
);
1512 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1513 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1514 /* reset resize growing parameters */
1518 wl_surface_damage(dri2_surf
->wl_win
->surface
,
1519 0, 0, INT32_MAX
, INT32_MAX
);
1520 wl_surface_commit(dri2_surf
->wl_win
->surface
);
1522 /* If we're not waiting for a frame callback then we'll at least throttle
1523 * to a sync callback so that we always give a chance for the compositor to
1524 * handle the commit and send a release event before checking for a free
1526 if (dri2_surf
->throttle_callback
== NULL
) {
1527 dri2_surf
->throttle_callback
= wl_display_sync(dri2_dpy
->wl_dpy
);
1528 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1529 &throttle_listener
, dri2_surf
);
1530 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->throttle_callback
,
1531 dri2_dpy
->wl_queue
);
1534 wl_display_flush(dri2_dpy
->wl_dpy
);
1538 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1539 int *x
, int *y
, int *w
, int *h
,
1540 void *loaderPrivate
)
1542 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1544 (void) swrast_update_buffers(dri2_surf
);
1547 *w
= dri2_surf
->base
.Width
;
1548 *h
= dri2_surf
->base
.Height
;
1552 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1553 int x
, int y
, int w
, int h
,
1554 char *data
, void *loaderPrivate
)
1556 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1557 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1558 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1559 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1560 int dst_stride
= copy_width
;
1563 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1565 memset(data
, 0, copy_width
* h
);
1569 assert(data
!= src
);
1570 assert(copy_width
<= src_stride
);
1573 src
+= y
* src_stride
;
1576 if (copy_width
> src_stride
-x_offset
)
1577 copy_width
= src_stride
-x_offset
;
1578 if (h
> dri2_surf
->base
.Height
-y
)
1579 h
= dri2_surf
->base
.Height
-y
;
1582 memcpy(dst
, src
, copy_width
);
1589 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1590 int x
, int y
, int w
, int h
, int stride
,
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 dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1596 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1599 assert(copy_width
<= stride
);
1601 (void) swrast_update_buffers(dri2_surf
);
1602 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1604 /* partial copy, copy old content */
1605 if (copy_width
< dst_stride
)
1606 dri2_wl_swrast_get_image(draw
, 0, 0,
1607 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1608 dst
, loaderPrivate
);
1611 dst
+= y
* dst_stride
;
1615 /* drivers expect we do these checks (and some rely on it) */
1616 if (copy_width
> dst_stride
-x_offset
)
1617 copy_width
= dst_stride
-x_offset
;
1618 if (h
> dri2_surf
->base
.Height
-y
)
1619 h
= dri2_surf
->base
.Height
-y
;
1622 memcpy(dst
, src
, copy_width
);
1626 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1630 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1631 int x
, int y
, int w
, int h
,
1632 char *data
, void *loaderPrivate
)
1634 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1637 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1638 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1639 stride
, data
, loaderPrivate
);
1643 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1645 static _EGLSurface
*
1646 dri2_wl_swrast_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1647 _EGLConfig
*conf
, void *native_window
,
1648 const EGLint
*attrib_list
)
1650 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1651 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
1652 struct wl_egl_window
*window
= native_window
;
1653 struct dri2_egl_surface
*dri2_surf
;
1657 dri2_surf
= calloc(1, sizeof *dri2_surf
);
1659 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
1663 if (!_eglInitSurface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
1666 if (conf
->RedSize
== 5)
1667 dri2_surf
->format
= WL_SHM_FORMAT_RGB565
;
1668 else if (conf
->AlphaSize
== 0)
1669 dri2_surf
->format
= WL_SHM_FORMAT_XRGB8888
;
1671 dri2_surf
->format
= WL_SHM_FORMAT_ARGB8888
;
1673 dri2_surf
->wl_win
= window
;
1675 dri2_surf
->base
.Width
= -1;
1676 dri2_surf
->base
.Height
= -1;
1678 dri2_surf
->dri_drawable
=
1679 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
1680 dri2_conf
->dri_double_config
,
1682 if (dri2_surf
->dri_drawable
== NULL
) {
1683 _eglError(EGL_BAD_ALLOC
, "swrast->createNewDrawable");
1684 goto cleanup_dri_drawable
;
1687 dri2_wl_swap_interval(drv
, disp
, &dri2_surf
->base
,
1688 dri2_dpy
->default_swap_interval
);
1690 return &dri2_surf
->base
;
1692 cleanup_dri_drawable
:
1693 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
1701 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1703 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1704 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1706 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1711 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1713 struct dri2_egl_display
*dri2_dpy
= data
;
1716 case WL_SHM_FORMAT_ARGB8888
:
1717 dri2_dpy
->formats
|= HAS_ARGB8888
;
1719 case WL_SHM_FORMAT_XRGB8888
:
1720 dri2_dpy
->formats
|= HAS_XRGB8888
;
1722 case WL_SHM_FORMAT_RGB565
:
1723 dri2_dpy
->formats
|= HAS_RGB565
;
1728 static const struct wl_shm_listener shm_listener
= {
1733 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
, uint32_t name
,
1734 const char *interface
, uint32_t version
)
1736 struct dri2_egl_display
*dri2_dpy
= data
;
1738 if (strcmp(interface
, "wl_shm") == 0) {
1740 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1741 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1745 static const struct wl_registry_listener registry_listener_swrast
= {
1746 registry_handle_global_swrast
,
1747 registry_handle_global_remove
1750 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1751 .authenticate
= NULL
,
1752 .create_window_surface
= dri2_wl_swrast_create_window_surface
,
1753 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1754 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1755 .destroy_surface
= dri2_wl_destroy_surface
,
1756 .create_image
= dri2_fallback_create_image_khr
,
1757 .swap_interval
= dri2_wl_swap_interval
,
1758 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
1759 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1760 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1761 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1762 .copy_buffers
= dri2_fallback_copy_buffers
,
1763 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1764 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1765 .get_sync_values
= dri2_fallback_get_sync_values
,
1769 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1771 struct dri2_egl_display
*dri2_dpy
;
1772 const __DRIconfig
*config
;
1775 static const unsigned int argb_masks
[4] =
1776 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1777 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
1778 static const unsigned int rgb565_masks
[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1780 loader_set_logger(_eglLog
);
1782 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1784 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1786 disp
->DriverData
= (void *) dri2_dpy
;
1787 if (disp
->PlatformDisplay
== NULL
) {
1788 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1789 if (dri2_dpy
->wl_dpy
== NULL
)
1791 dri2_dpy
->own_device
= 1;
1793 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1796 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1798 if (dri2_dpy
->own_device
)
1799 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1801 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
1802 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
1803 dri2_dpy
->wl_queue
);
1804 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1805 ®istry_listener_swrast
, dri2_dpy
);
1807 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
1808 goto cleanup_registry
;
1810 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->formats
== 0)
1813 dri2_dpy
->driver_name
= strdup("swrast");
1814 if (!dri2_load_driver_swrast(disp
))
1817 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
1818 dri2_dpy
->swrast_loader_extension
.base
.version
= 2;
1819 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= dri2_wl_swrast_get_drawable_info
;
1820 dri2_dpy
->swrast_loader_extension
.putImage
= dri2_wl_swrast_put_image
;
1821 dri2_dpy
->swrast_loader_extension
.getImage
= dri2_wl_swrast_get_image
;
1822 dri2_dpy
->swrast_loader_extension
.putImage2
= dri2_wl_swrast_put_image2
;
1824 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
1825 dri2_dpy
->extensions
[1] = NULL
;
1827 if (!dri2_create_screen(disp
))
1828 goto cleanup_driver
;
1830 dri2_wl_setup_swap_interval(dri2_dpy
);
1832 types
= EGL_WINDOW_BIT
;
1833 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1834 config
= dri2_dpy
->driver_configs
[i
];
1835 if (dri2_dpy
->formats
& HAS_XRGB8888
)
1836 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb_masks
);
1837 if (dri2_dpy
->formats
& HAS_ARGB8888
)
1838 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, argb_masks
);
1839 if (dri2_dpy
->formats
& HAS_RGB565
)
1840 dri2_add_config(disp
, config
, i
+ 1, types
, NULL
, rgb565_masks
);
1843 /* Fill vtbl last to prevent accidentally calling virtual function during
1846 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
1851 dlclose(dri2_dpy
->driver
);
1853 wl_shm_destroy(dri2_dpy
->wl_shm
);
1855 wl_registry_destroy(dri2_dpy
->wl_registry
);
1856 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
1864 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1866 EGLBoolean initialized
= EGL_TRUE
;
1868 int hw_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1871 if (!dri2_initialize_wayland_drm(drv
, disp
)) {
1872 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
1875 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);