2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
42 #include "egl_dri2_fallbacks.h"
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
48 enum wl_drm_format_flags {
55 dri2_wl_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
59 roundtrip(struct dri2_egl_display *dri2_dpy)
61 return wl_display_roundtrip_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
65 wl_buffer_release(void *data, struct wl_buffer *buffer)
67 struct dri2_egl_surface *dri2_surf = data;
70 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
71 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
74 if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
75 wl_buffer_destroy(buffer);
79 dri2_surf->color_buffers[i].locked = 0;
82 static const struct wl_buffer_listener wl_buffer_listener = {
83 .release = wl_buffer_release
87 resize_callback(struct wl_egl_window *wl_win, void *data)
89 struct dri2_egl_surface *dri2_surf = data;
90 struct dri2_egl_display *dri2_dpy =
91 dri2_egl_display(dri2_surf->base.Resource.Display);
93 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
97 destroy_window_callback(void *data)
99 struct dri2_egl_surface *dri2_surf = data;
100 dri2_surf->wl_win = NULL;
104 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
107 dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
108 _EGLConfig *conf, void *native_window,
109 const EGLint *attrib_list)
111 __DRIcreateNewDrawableFunc createNewDrawable;
112 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
113 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
114 struct wl_egl_window *window = native_window;
115 struct dri2_egl_surface *dri2_surf;
116 const __DRIconfig *config;
118 dri2_surf = calloc(1, sizeof *dri2_surf);
120 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
124 if (!_eglInitSurface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list))
127 if (dri2_dpy->wl_drm) {
128 if (conf->RedSize == 5)
129 dri2_surf->format = WL_DRM_FORMAT_RGB565;
130 else if (conf->AlphaSize == 0)
131 dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
133 dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
135 assert(dri2_dpy->wl_shm);
136 if (conf->RedSize == 5)
137 dri2_surf->format = WL_SHM_FORMAT_RGB565;
138 else if (conf->AlphaSize == 0)
139 dri2_surf->format = WL_SHM_FORMAT_XRGB8888;
141 dri2_surf->format = WL_SHM_FORMAT_ARGB8888;
145 _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
149 dri2_surf->wl_win = window;
150 dri2_surf->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
151 if (!dri2_surf->wl_queue) {
152 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
156 if (dri2_dpy->wl_drm) {
157 dri2_surf->wl_drm_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_drm);
158 if (!dri2_surf->wl_drm_wrapper) {
159 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
162 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_drm_wrapper,
163 dri2_surf->wl_queue);
166 dri2_surf->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
167 if (!dri2_surf->wl_dpy_wrapper) {
168 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
171 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_dpy_wrapper,
172 dri2_surf->wl_queue);
174 dri2_surf->wl_surface_wrapper = wl_proxy_create_wrapper(window->surface);
175 if (!dri2_surf->wl_surface_wrapper) {
176 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
177 goto cleanup_dpy_wrapper;
179 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_surface_wrapper,
180 dri2_surf->wl_queue);
182 dri2_surf->wl_win->private = dri2_surf;
183 dri2_surf->wl_win->destroy_window_callback = destroy_window_callback;
185 dri2_surf->base.Width = -1;
186 dri2_surf->base.Height = -1;
188 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
189 dri2_surf->base.GLColorspace);
191 if (dri2_dpy->dri2) {
192 dri2_surf->wl_win->resize_callback = resize_callback;
194 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
196 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
199 dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
201 if (dri2_surf->dri_drawable == NULL) {
202 _eglError(EGL_BAD_ALLOC, "createNewDrawable");
203 goto cleanup_surf_wrapper;
206 dri2_wl_swap_interval(drv, disp, &dri2_surf->base,
207 dri2_dpy->default_swap_interval);
209 return &dri2_surf->base;
211 cleanup_surf_wrapper:
212 wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
214 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
216 if (dri2_surf->wl_drm_wrapper)
217 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
219 wl_event_queue_destroy(dri2_surf->wl_queue);
227 dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
228 _EGLConfig *conf, void *native_window,
229 const EGLint *attrib_list)
231 /* From the EGL_EXT_platform_wayland spec, version 3:
233 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
234 * that belongs to Wayland. Any such call fails and generates
237 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on "
243 * Called via eglDestroySurface(), drv->API.DestroySurface().
246 dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
248 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
249 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
254 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
256 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
257 if (dri2_surf->color_buffers[i].wl_buffer)
258 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
259 if (dri2_surf->color_buffers[i].dri_image)
260 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
261 if (dri2_surf->color_buffers[i].linear_copy)
262 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
263 if (dri2_surf->color_buffers[i].data)
264 munmap(dri2_surf->color_buffers[i].data,
265 dri2_surf->color_buffers[i].data_size);
268 if (dri2_dpy->dri2) {
269 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
270 if (dri2_surf->dri_buffers[i] &&
271 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
272 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
273 dri2_surf->dri_buffers[i]);
276 if (dri2_surf->throttle_callback)
277 wl_callback_destroy(dri2_surf->throttle_callback);
279 if (dri2_surf->wl_win) {
280 dri2_surf->wl_win->private = NULL;
281 dri2_surf->wl_win->resize_callback = NULL;
282 dri2_surf->wl_win->destroy_window_callback = NULL;
285 if (dri2_surf->wl_drm_wrapper)
286 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
287 wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
288 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
289 wl_event_queue_destroy(dri2_surf->wl_queue);
297 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
299 struct dri2_egl_display *dri2_dpy =
300 dri2_egl_display(dri2_surf->base.Resource.Display);
303 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
304 if (dri2_surf->color_buffers[i].wl_buffer &&
305 !dri2_surf->color_buffers[i].locked)
306 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
307 if (dri2_surf->color_buffers[i].dri_image)
308 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
309 if (dri2_surf->color_buffers[i].linear_copy)
310 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
311 if (dri2_surf->color_buffers[i].data)
312 munmap(dri2_surf->color_buffers[i].data,
313 dri2_surf->color_buffers[i].data_size);
315 dri2_surf->color_buffers[i].wl_buffer = NULL;
316 dri2_surf->color_buffers[i].dri_image = NULL;
317 dri2_surf->color_buffers[i].linear_copy = NULL;
318 dri2_surf->color_buffers[i].data = NULL;
319 dri2_surf->color_buffers[i].locked = 0;
322 if (dri2_dpy->dri2) {
323 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
324 if (dri2_surf->dri_buffers[i] &&
325 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
326 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
327 dri2_surf->dri_buffers[i]);
332 get_back_bo(struct dri2_egl_surface *dri2_surf)
334 struct dri2_egl_display *dri2_dpy =
335 dri2_egl_display(dri2_surf->base.Resource.Display);
337 unsigned int dri_image_format;
339 /* currently supports three WL DRM formats,
340 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
341 * and WL_DRM_FORMAT_RGB565
343 switch (dri2_surf->format) {
344 case WL_DRM_FORMAT_ARGB8888:
345 dri_image_format = __DRI_IMAGE_FORMAT_ARGB8888;
347 case WL_DRM_FORMAT_XRGB8888:
348 dri_image_format = __DRI_IMAGE_FORMAT_XRGB8888;
350 case WL_DRM_FORMAT_RGB565:
351 dri_image_format = __DRI_IMAGE_FORMAT_RGB565;
354 /* format is not supported */
358 /* There might be a buffer release already queued that wasn't processed */
359 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
361 while (dri2_surf->back == NULL) {
362 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
363 /* Get an unlocked buffer, preferrably one with a dri_buffer
364 * already allocated. */
365 if (dri2_surf->color_buffers[i].locked)
367 if (dri2_surf->back == NULL)
368 dri2_surf->back = &dri2_surf->color_buffers[i];
369 else if (dri2_surf->back->dri_image == NULL)
370 dri2_surf->back = &dri2_surf->color_buffers[i];
376 /* If we don't have a buffer, then block on the server to release one for
377 * us, and try again. wl_display_dispatch_queue will process any pending
378 * events, however not all servers flush on issuing a buffer release
379 * event. So, we spam the server with roundtrips as they always cause a
382 if (wl_display_roundtrip_queue(dri2_dpy->wl_dpy,
383 dri2_surf->wl_queue) < 0)
387 if (dri2_surf->back == NULL)
390 use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER;
392 if (dri2_dpy->is_different_gpu &&
393 dri2_surf->back->linear_copy == NULL) {
394 dri2_surf->back->linear_copy =
395 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
396 dri2_surf->base.Width,
397 dri2_surf->base.Height,
400 __DRI_IMAGE_USE_LINEAR,
402 if (dri2_surf->back->linear_copy == NULL)
406 if (dri2_surf->back->dri_image == NULL) {
407 dri2_surf->back->dri_image =
408 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
409 dri2_surf->base.Width,
410 dri2_surf->base.Height,
412 dri2_dpy->is_different_gpu ?
415 dri2_surf->back->age = 0;
417 if (dri2_surf->back->dri_image == NULL)
420 dri2_surf->back->locked = 1;
427 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
429 struct dri2_egl_display *dri2_dpy =
430 dri2_egl_display(dri2_surf->base.Resource.Display);
434 image = dri2_surf->back->dri_image;
436 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
437 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
439 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
441 buffer->pitch = pitch;
447 get_aux_bo(struct dri2_egl_surface *dri2_surf,
448 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
450 struct dri2_egl_display *dri2_dpy =
451 dri2_egl_display(dri2_surf->base.Resource.Display);
452 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
455 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
457 dri2_surf->base.Width,
458 dri2_surf->base.Height);
459 dri2_surf->dri_buffers[attachment] = b;
464 memcpy(buffer, b, sizeof *buffer);
470 update_buffers(struct dri2_egl_surface *dri2_surf)
472 struct dri2_egl_display *dri2_dpy =
473 dri2_egl_display(dri2_surf->base.Resource.Display);
476 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
477 dri2_surf->base.Height != dri2_surf->wl_win->height) {
479 dri2_wl_release_buffers(dri2_surf);
481 dri2_surf->base.Width = dri2_surf->wl_win->width;
482 dri2_surf->base.Height = dri2_surf->wl_win->height;
483 dri2_surf->dx = dri2_surf->wl_win->dx;
484 dri2_surf->dy = dri2_surf->wl_win->dy;
487 if (get_back_bo(dri2_surf) < 0) {
488 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
492 /* If we have an extra unlocked buffer at this point, we had to do triple
493 * buffering for a while, but now can go back to just double buffering.
494 * That means we can free any unlocked buffer now. */
495 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
496 if (!dri2_surf->color_buffers[i].locked &&
497 dri2_surf->color_buffers[i].wl_buffer) {
498 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
499 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
500 if (dri2_dpy->is_different_gpu)
501 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
502 dri2_surf->color_buffers[i].wl_buffer = NULL;
503 dri2_surf->color_buffers[i].dri_image = NULL;
504 dri2_surf->color_buffers[i].linear_copy = NULL;
512 dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
513 int *width, int *height,
514 unsigned int *attachments, int count,
515 int *out_count, void *loaderPrivate)
517 struct dri2_egl_surface *dri2_surf = loaderPrivate;
520 if (update_buffers(dri2_surf) < 0)
523 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
524 switch (attachments[i]) {
525 case __DRI_BUFFER_BACK_LEFT:
526 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
529 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
530 &dri2_surf->buffers[j]) < 0) {
531 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
542 *width = dri2_surf->base.Width;
543 *height = dri2_surf->base.Height;
545 return dri2_surf->buffers;
549 dri2_wl_get_buffers(__DRIdrawable * driDrawable,
550 int *width, int *height,
551 unsigned int *attachments, int count,
552 int *out_count, void *loaderPrivate)
554 struct dri2_egl_surface *dri2_surf = loaderPrivate;
555 unsigned int *attachments_with_format;
561 switch (dri2_surf->format) {
562 case WL_DRM_FORMAT_ARGB8888:
563 case WL_DRM_FORMAT_XRGB8888:
566 case WL_DRM_FORMAT_RGB565:
570 /* format is not supported */
574 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
575 if (!attachments_with_format) {
580 for (i = 0; i < count; ++i) {
581 attachments_with_format[2*i] = attachments[i];
582 attachments_with_format[2*i + 1] = bpp;
586 dri2_wl_get_buffers_with_format(driDrawable,
588 attachments_with_format, count,
589 out_count, loaderPrivate);
591 free(attachments_with_format);
597 image_get_buffers(__DRIdrawable *driDrawable,
601 uint32_t buffer_mask,
602 struct __DRIimageList *buffers)
604 struct dri2_egl_surface *dri2_surf = loaderPrivate;
606 if (update_buffers(dri2_surf) < 0)
609 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
610 buffers->back = dri2_surf->back->dri_image;
616 dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
619 (void) loaderPrivate;
622 static const __DRIdri2LoaderExtension dri2_loader_extension = {
623 .base = { __DRI_DRI2_LOADER, 3 },
625 .getBuffers = dri2_wl_get_buffers,
626 .flushFrontBuffer = dri2_wl_flush_front_buffer,
627 .getBuffersWithFormat = dri2_wl_get_buffers_with_format,
630 static const __DRIimageLoaderExtension image_loader_extension = {
631 .base = { __DRI_IMAGE_LOADER, 1 },
633 .getBuffers = image_get_buffers,
634 .flushFrontBuffer = dri2_wl_flush_front_buffer,
638 wayland_throttle_callback(void *data,
639 struct wl_callback *callback,
642 struct dri2_egl_surface *dri2_surf = data;
644 dri2_surf->throttle_callback = NULL;
645 wl_callback_destroy(callback);
648 static const struct wl_callback_listener throttle_listener = {
649 .done = wayland_throttle_callback
653 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
655 struct dri2_egl_display *dri2_dpy =
656 dri2_egl_display(dri2_surf->base.Resource.Display);
658 int fd, stride, name;
660 if (dri2_surf->current->wl_buffer != NULL)
663 if (dri2_dpy->is_different_gpu) {
664 image = dri2_surf->current->linear_copy;
666 image = dri2_surf->current->dri_image;
668 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
669 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
670 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
672 dri2_surf->current->wl_buffer =
673 wl_drm_create_prime_buffer(dri2_surf->wl_drm_wrapper,
675 dri2_surf->base.Width,
676 dri2_surf->base.Height,
683 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
684 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
686 dri2_surf->current->wl_buffer =
687 wl_drm_create_buffer(dri2_surf->wl_drm_wrapper,
689 dri2_surf->base.Width,
690 dri2_surf->base.Height,
695 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
696 &wl_buffer_listener, dri2_surf);
700 try_damage_buffer(struct dri2_egl_surface *dri2_surf,
706 if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_surface_wrapper)
707 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
710 for (i = 0; i < n_rects; i++) {
711 const int *rect = &rects[i * 4];
713 wl_surface_damage_buffer(dri2_surf->wl_surface_wrapper,
715 dri2_surf->base.Height - rect[1] - rect[3],
721 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
724 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
730 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
731 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
734 while (dri2_surf->throttle_callback != NULL)
735 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
736 dri2_surf->wl_queue) == -1)
739 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
740 if (dri2_surf->color_buffers[i].age > 0)
741 dri2_surf->color_buffers[i].age++;
743 /* Make sure we have a back buffer in case we're swapping without ever
745 if (get_back_bo(dri2_surf) < 0) {
746 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
750 if (draw->SwapInterval > 0) {
751 dri2_surf->throttle_callback =
752 wl_surface_frame(dri2_surf->wl_surface_wrapper);
753 wl_callback_add_listener(dri2_surf->throttle_callback,
754 &throttle_listener, dri2_surf);
757 dri2_surf->back->age = 1;
758 dri2_surf->current = dri2_surf->back;
759 dri2_surf->back = NULL;
761 create_wl_buffer(dri2_surf);
763 wl_surface_attach(dri2_surf->wl_surface_wrapper,
764 dri2_surf->current->wl_buffer,
765 dri2_surf->dx, dri2_surf->dy);
767 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
768 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
769 /* reset resize growing parameters */
773 /* If the compositor doesn't support damage_buffer, we deliberately
774 * ignore the damage region and post maximum damage, due to
775 * https://bugs.freedesktop.org/78190 */
776 if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
777 wl_surface_damage(dri2_surf->wl_surface_wrapper,
778 0, 0, INT32_MAX, INT32_MAX);
780 if (dri2_dpy->is_different_gpu) {
781 _EGLContext *ctx = _eglGetCurrentContext();
782 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
783 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
784 dri2_surf->current->linear_copy,
785 dri2_surf->current->dri_image,
786 0, 0, dri2_surf->base.Width,
787 dri2_surf->base.Height,
788 0, 0, dri2_surf->base.Width,
789 dri2_surf->base.Height, 0);
792 dri2_flush_drawable_for_swapbuffers(disp, draw);
793 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
795 wl_surface_commit(dri2_surf->wl_surface_wrapper);
797 /* If we're not waiting for a frame callback then we'll at least throttle
798 * to a sync callback so that we always give a chance for the compositor to
799 * handle the commit and send a release event before checking for a free
801 if (dri2_surf->throttle_callback == NULL) {
802 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
803 wl_callback_add_listener(dri2_surf->throttle_callback,
804 &throttle_listener, dri2_surf);
807 wl_display_flush(dri2_dpy->wl_dpy);
813 dri2_wl_query_buffer_age(_EGLDriver *drv,
814 _EGLDisplay *disp, _EGLSurface *surface)
816 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
818 if (get_back_bo(dri2_surf) < 0) {
819 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
823 return dri2_surf->back->age;
827 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
829 return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
832 static struct wl_buffer *
833 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
837 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
838 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
839 __DRIimage *image = dri2_img->dri_image;
840 struct wl_buffer *buffer;
841 int width, height, format, pitch;
842 enum wl_drm_format wl_format;
844 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
847 case __DRI_IMAGE_FORMAT_ARGB8888:
848 if (!(dri2_dpy->formats & HAS_ARGB8888))
850 wl_format = WL_DRM_FORMAT_ARGB8888;
852 case __DRI_IMAGE_FORMAT_XRGB8888:
853 if (!(dri2_dpy->formats & HAS_XRGB8888))
855 wl_format = WL_DRM_FORMAT_XRGB8888;
861 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
862 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
863 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
865 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
868 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
871 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
883 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
886 wl_drm_create_buffer(dri2_dpy->wl_drm,
893 /* The buffer object will have been created with our internal event queue
894 * because it is using the wl_drm object as a proxy factory. We want the
895 * buffer to be used by the application so we'll reset it to the display's
896 * default event queue */
898 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
903 _eglError(EGL_BAD_MATCH, "unsupported image format");
908 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
910 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
913 if (dri2_dpy->is_render_node) {
914 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
915 "authenticate for render-nodes");
918 dri2_dpy->authenticated = 0;
920 wl_drm_authenticate(dri2_dpy->wl_drm, id);
921 if (roundtrip(dri2_dpy) < 0)
924 if (!dri2_dpy->authenticated)
927 /* reset authenticated */
928 dri2_dpy->authenticated = 1;
934 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
936 struct dri2_egl_display *dri2_dpy = data;
939 dri2_dpy->device_name = strdup(device);
940 if (!dri2_dpy->device_name)
943 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
944 if (dri2_dpy->fd == -1) {
945 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
946 dri2_dpy->device_name, strerror(errno));
950 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
951 dri2_dpy->authenticated = 1;
953 drmGetMagic(dri2_dpy->fd, &magic);
954 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
959 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
961 struct dri2_egl_display *dri2_dpy = data;
964 case WL_DRM_FORMAT_ARGB8888:
965 dri2_dpy->formats |= HAS_ARGB8888;
967 case WL_DRM_FORMAT_XRGB8888:
968 dri2_dpy->formats |= HAS_XRGB8888;
970 case WL_DRM_FORMAT_RGB565:
971 dri2_dpy->formats |= HAS_RGB565;
977 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
979 struct dri2_egl_display *dri2_dpy = data;
981 dri2_dpy->capabilities = value;
985 drm_handle_authenticated(void *data, struct wl_drm *drm)
987 struct dri2_egl_display *dri2_dpy = data;
989 dri2_dpy->authenticated = 1;
992 static const struct wl_drm_listener drm_listener = {
993 .device = drm_handle_device,
994 .format = drm_handle_format,
995 .authenticated = drm_handle_authenticated,
996 .capabilities = drm_handle_capabilities
1000 registry_handle_global_drm(void *data, struct wl_registry *registry, uint32_t name,
1001 const char *interface, uint32_t version)
1003 struct dri2_egl_display *dri2_dpy = data;
1007 if (strcmp(interface, "wl_drm") == 0) {
1009 wl_registry_bind(registry, name, &wl_drm_interface, version);
1010 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
1015 registry_handle_global_remove(void *data, struct wl_registry *registry,
1020 static const struct wl_registry_listener registry_listener_drm = {
1021 .global = registry_handle_global_drm,
1022 .global_remove = registry_handle_global_remove
1026 dri2_wl_swap_interval(_EGLDriver *drv,
1031 if (interval > surf->Config->MaxSwapInterval)
1032 interval = surf->Config->MaxSwapInterval;
1033 else if (interval < surf->Config->MinSwapInterval)
1034 interval = surf->Config->MinSwapInterval;
1036 surf->SwapInterval = interval;
1042 dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
1044 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1046 /* We can't use values greater than 1 on Wayland because we are using the
1047 * frame callback to synchronise the frame and the only way we be sure to
1048 * get a frame callback is to attach a new buffer. Therefore we can't just
1049 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1051 if (dri2_dpy->config)
1052 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1053 "vblank_mode", &vblank_mode);
1054 switch (vblank_mode) {
1055 case DRI_CONF_VBLANK_NEVER:
1056 dri2_dpy->min_swap_interval = 0;
1057 dri2_dpy->max_swap_interval = 0;
1058 dri2_dpy->default_swap_interval = 0;
1060 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1061 dri2_dpy->min_swap_interval = 1;
1062 dri2_dpy->max_swap_interval = 1;
1063 dri2_dpy->default_swap_interval = 1;
1065 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1066 dri2_dpy->min_swap_interval = 0;
1067 dri2_dpy->max_swap_interval = 1;
1068 dri2_dpy->default_swap_interval = 0;
1071 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1072 dri2_dpy->min_swap_interval = 0;
1073 dri2_dpy->max_swap_interval = 1;
1074 dri2_dpy->default_swap_interval = 1;
1079 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1080 .authenticate = dri2_wl_authenticate,
1081 .create_window_surface = dri2_wl_create_window_surface,
1082 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1083 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1084 .destroy_surface = dri2_wl_destroy_surface,
1085 .create_image = dri2_create_image_khr,
1086 .swap_interval = dri2_wl_swap_interval,
1087 .swap_buffers = dri2_wl_swap_buffers,
1088 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1089 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1090 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1091 .copy_buffers = dri2_fallback_copy_buffers,
1092 .query_buffer_age = dri2_wl_query_buffer_age,
1093 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1094 .get_sync_values = dri2_fallback_get_sync_values,
1095 .get_dri_drawable = dri2_surface_get_dri_drawable,
1098 static const __DRIextension *dri2_loader_extensions[] = {
1099 &dri2_loader_extension.base,
1100 &image_loader_extension.base,
1101 &image_lookup_extension.base,
1102 &use_invalidate.base,
1106 static const __DRIextension *image_loader_extensions[] = {
1107 &image_loader_extension.base,
1108 &image_lookup_extension.base,
1109 &use_invalidate.base,
1114 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1116 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1117 static const struct {
1118 const char *format_name;
1120 unsigned int rgba_masks[4];
1122 { "XRGB8888", HAS_XRGB8888, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1123 { "ARGB8888", HAS_ARGB8888, { 0xff0000, 0xff00, 0x00ff, 0 } },
1124 { "RGB565", HAS_RGB565, { 0x00f800, 0x07e0, 0x001f, 0 } },
1126 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1127 unsigned int count, i, j;
1130 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1131 for (j = 0; j < ARRAY_SIZE(visuals); j++) {
1132 struct dri2_egl_config *dri2_conf;
1134 if (!(dri2_dpy->formats & visuals[j].has_format))
1137 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1138 count + 1, EGL_WINDOW_BIT, NULL, visuals[j].rgba_masks);
1140 if (dri2_conf->base.ConfigID == count + 1)
1147 for (i = 0; i < ARRAY_SIZE(format_count); i++) {
1148 if (!format_count[i]) {
1149 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1150 visuals[i].format_name);
1154 return (count != 0);
1158 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1160 struct dri2_egl_display *dri2_dpy;
1162 loader_set_logger(_eglLog);
1164 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1166 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1168 disp->DriverData = (void *) dri2_dpy;
1169 if (disp->PlatformDisplay == NULL) {
1170 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1171 if (dri2_dpy->wl_dpy == NULL)
1173 dri2_dpy->own_device = 1;
1175 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1178 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1180 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1181 if (dri2_dpy->wl_dpy_wrapper == NULL)
1182 goto cleanup_dpy_wrapper;
1184 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1185 dri2_dpy->wl_queue);
1187 if (dri2_dpy->own_device)
1188 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1190 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1191 wl_registry_add_listener(dri2_dpy->wl_registry,
1192 ®istry_listener_drm, dri2_dpy);
1193 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1194 goto cleanup_registry;
1196 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1199 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1202 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1203 &dri2_dpy->is_different_gpu);
1204 if (dri2_dpy->is_different_gpu) {
1205 free(dri2_dpy->device_name);
1206 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1207 if (!dri2_dpy->device_name) {
1208 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1209 "for requested GPU");
1214 /* we have to do the check now, because loader_get_user_preferred_fd
1215 * will return a render-node when the requested gpu is different
1216 * to the server, but also if the client asks for the same gpu than
1217 * the server by requesting its pci-id */
1218 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1220 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1221 if (dri2_dpy->driver_name == NULL) {
1222 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1226 if (!dri2_load_driver(disp))
1227 goto cleanup_driver_name;
1229 /* render nodes cannot use Gem names, and thus do not support
1230 * the __DRI_DRI2_LOADER extension */
1231 if (!dri2_dpy->is_render_node)
1232 dri2_dpy->loader_extensions = dri2_loader_extensions;
1234 dri2_dpy->loader_extensions = image_loader_extensions;
1236 if (!dri2_create_screen(disp))
1237 goto cleanup_driver;
1239 dri2_wl_setup_swap_interval(dri2_dpy);
1241 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1242 * createImageFromFds support indicates that Prime export/import
1243 * is supported by the driver. Fall back to
1244 * gem names if we don't have Prime support. */
1246 if (dri2_dpy->image->base.version < 7 ||
1247 dri2_dpy->image->createImageFromFds == NULL)
1248 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1250 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1251 * The server needs to accept them */
1252 if (dri2_dpy->is_render_node &&
1253 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1254 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1255 goto cleanup_screen;
1258 if (dri2_dpy->is_different_gpu &&
1259 (dri2_dpy->image->base.version < 9 ||
1260 dri2_dpy->image->blitImage == NULL)) {
1261 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1262 "Image extension in the driver is not "
1263 "compatible. Version 9 or later and blitImage() "
1265 goto cleanup_screen;
1268 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1269 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1270 goto cleanup_screen;
1273 dri2_set_WL_bind_wayland_display(drv, disp);
1274 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1275 * because the buffer of the EGLImage has likely a tiling mode the server
1276 * gpu won't support. These is no way to check for now. Thus do not support the
1278 if (!dri2_dpy->is_different_gpu) {
1279 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1281 dri2_wl_display_vtbl.create_wayland_buffer_from_image =
1282 dri2_fallback_create_wayland_buffer_from_image;
1284 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1286 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1288 /* Fill vtbl last to prevent accidentally calling virtual function during
1291 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1296 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1298 dlclose(dri2_dpy->driver);
1299 cleanup_driver_name:
1300 free(dri2_dpy->driver_name);
1302 close(dri2_dpy->fd);
1304 free(dri2_dpy->device_name);
1305 wl_drm_destroy(dri2_dpy->wl_drm);
1307 wl_registry_destroy(dri2_dpy->wl_registry);
1308 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
1309 cleanup_dpy_wrapper:
1310 wl_event_queue_destroy(dri2_dpy->wl_queue);
1311 if (disp->PlatformDisplay == NULL)
1312 wl_display_disconnect(dri2_dpy->wl_dpy);
1315 disp->DriverData = NULL;
1321 dri2_wl_swrast_get_stride_for_format(int format, int w)
1323 if (format == WL_SHM_FORMAT_RGB565)
1325 else /* ARGB8888 || XRGB8888 */
1330 * Taken from weston shared/os-compatibility.c
1333 #ifndef HAVE_MKOSTEMP
1336 set_cloexec_or_close(int fd)
1343 flags = fcntl(fd, F_GETFD);
1347 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1360 * Taken from weston shared/os-compatibility.c
1364 create_tmpfile_cloexec(char *tmpname)
1368 #ifdef HAVE_MKOSTEMP
1369 fd = mkostemp(tmpname, O_CLOEXEC);
1373 fd = mkstemp(tmpname);
1375 fd = set_cloexec_or_close(fd);
1384 * Taken from weston shared/os-compatibility.c
1386 * Create a new, unique, anonymous file of the given size, and
1387 * return the file descriptor for it. The file descriptor is set
1388 * CLOEXEC. The file is immediately suitable for mmap()'ing
1389 * the given size at offset zero.
1391 * The file should not have a permanent backing store like a disk,
1392 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1394 * The file name is deleted from the file system.
1396 * The file is suitable for buffer sharing between processes by
1397 * transmitting the file descriptor over Unix sockets using the
1398 * SCM_RIGHTS methods.
1400 * If the C library implements posix_fallocate(), it is used to
1401 * guarantee that disk space is available for the file at the
1402 * given size. If disk space is insufficent, errno is set to ENOSPC.
1403 * If posix_fallocate() is not supported, program may receive
1404 * SIGBUS on accessing mmap()'ed file contents instead.
1407 os_create_anonymous_file(off_t size)
1409 static const char template[] = "/mesa-shared-XXXXXX";
1415 path = getenv("XDG_RUNTIME_DIR");
1421 name = malloc(strlen(path) + sizeof(template));
1426 strcat(name, template);
1428 fd = create_tmpfile_cloexec(name);
1435 ret = ftruncate(fd, size);
1446 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1447 int format, int w, int h,
1448 void **data, int *size,
1449 struct wl_buffer **buffer)
1451 struct dri2_egl_display *dri2_dpy =
1452 dri2_egl_display(dri2_surf->base.Resource.Display);
1453 struct wl_shm_pool *pool;
1454 int fd, stride, size_map;
1457 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1458 size_map = h * stride;
1460 /* Create a sharable buffer */
1461 fd = os_create_anonymous_file(size_map);
1465 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1466 if (data_map == MAP_FAILED) {
1471 /* Share it in a wl_buffer */
1472 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1473 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1474 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1475 wl_shm_pool_destroy(pool);
1484 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1486 struct dri2_egl_display *dri2_dpy =
1487 dri2_egl_display(dri2_surf->base.Resource.Display);
1490 /* we need to do the following operations only once per frame */
1491 if (dri2_surf->back)
1494 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1495 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1497 dri2_wl_release_buffers(dri2_surf);
1499 dri2_surf->base.Width = dri2_surf->wl_win->width;
1500 dri2_surf->base.Height = dri2_surf->wl_win->height;
1501 dri2_surf->dx = dri2_surf->wl_win->dx;
1502 dri2_surf->dy = dri2_surf->wl_win->dy;
1503 dri2_surf->current = NULL;
1506 /* find back buffer */
1508 /* There might be a buffer release already queued that wasn't processed */
1509 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1511 /* try get free buffer already created */
1512 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1513 if (!dri2_surf->color_buffers[i].locked &&
1514 dri2_surf->color_buffers[i].wl_buffer) {
1515 dri2_surf->back = &dri2_surf->color_buffers[i];
1520 /* else choose any another free location */
1521 if (!dri2_surf->back) {
1522 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1523 if (!dri2_surf->color_buffers[i].locked) {
1524 dri2_surf->back = &dri2_surf->color_buffers[i];
1525 if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1527 dri2_surf->base.Width,
1528 dri2_surf->base.Height,
1529 &dri2_surf->back->data,
1530 &dri2_surf->back->data_size,
1531 &dri2_surf->back->wl_buffer)) {
1532 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1535 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1536 &wl_buffer_listener, dri2_surf);
1542 if (!dri2_surf->back) {
1543 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1547 dri2_surf->back->locked = 1;
1549 /* If we have an extra unlocked buffer at this point, we had to do triple
1550 * buffering for a while, but now can go back to just double buffering.
1551 * That means we can free any unlocked buffer now. */
1552 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1553 if (!dri2_surf->color_buffers[i].locked &&
1554 dri2_surf->color_buffers[i].wl_buffer) {
1555 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1556 munmap(dri2_surf->color_buffers[i].data,
1557 dri2_surf->color_buffers[i].data_size);
1558 dri2_surf->color_buffers[i].wl_buffer = NULL;
1559 dri2_surf->color_buffers[i].data = NULL;
1567 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1569 /* if there has been a resize: */
1570 if (!dri2_surf->current)
1573 return dri2_surf->current->data;
1577 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1579 assert(dri2_surf->back);
1580 return dri2_surf->back->data;
1584 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1586 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1588 while (dri2_surf->throttle_callback != NULL)
1589 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1590 dri2_surf->wl_queue) == -1)
1593 if (dri2_surf->base.SwapInterval > 0) {
1594 dri2_surf->throttle_callback =
1595 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1596 wl_callback_add_listener(dri2_surf->throttle_callback,
1597 &throttle_listener, dri2_surf);
1600 dri2_surf->current = dri2_surf->back;
1601 dri2_surf->back = NULL;
1603 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1604 dri2_surf->current->wl_buffer,
1605 dri2_surf->dx, dri2_surf->dy);
1607 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1608 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1609 /* reset resize growing parameters */
1613 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1614 0, 0, INT32_MAX, INT32_MAX);
1615 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1617 /* If we're not waiting for a frame callback then we'll at least throttle
1618 * to a sync callback so that we always give a chance for the compositor to
1619 * handle the commit and send a release event before checking for a free
1621 if (dri2_surf->throttle_callback == NULL) {
1622 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy_wrapper);
1623 wl_callback_add_listener(dri2_surf->throttle_callback,
1624 &throttle_listener, dri2_surf);
1627 wl_display_flush(dri2_dpy->wl_dpy);
1631 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1632 int *x, int *y, int *w, int *h,
1633 void *loaderPrivate)
1635 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1637 (void) swrast_update_buffers(dri2_surf);
1640 *w = dri2_surf->base.Width;
1641 *h = dri2_surf->base.Height;
1645 dri2_wl_swrast_get_image(__DRIdrawable * read,
1646 int x, int y, int w, int h,
1647 char *data, void *loaderPrivate)
1649 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1650 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1651 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1652 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1653 int dst_stride = copy_width;
1656 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1658 memset(data, 0, copy_width * h);
1662 assert(data != src);
1663 assert(copy_width <= src_stride);
1666 src += y * src_stride;
1669 if (copy_width > src_stride-x_offset)
1670 copy_width = src_stride-x_offset;
1671 if (h > dri2_surf->base.Height-y)
1672 h = dri2_surf->base.Height-y;
1675 memcpy(dst, src, copy_width);
1682 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1683 int x, int y, int w, int h, int stride,
1684 char *data, void *loaderPrivate)
1686 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1687 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1688 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1689 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1692 assert(copy_width <= stride);
1694 (void) swrast_update_buffers(dri2_surf);
1695 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1697 /* partial copy, copy old content */
1698 if (copy_width < dst_stride)
1699 dri2_wl_swrast_get_image(draw, 0, 0,
1700 dri2_surf->base.Width, dri2_surf->base.Height,
1701 dst, loaderPrivate);
1704 dst += y * dst_stride;
1708 /* drivers expect we do these checks (and some rely on it) */
1709 if (copy_width > dst_stride-x_offset)
1710 copy_width = dst_stride-x_offset;
1711 if (h > dri2_surf->base.Height-y)
1712 h = dri2_surf->base.Height-y;
1715 memcpy(dst, src, copy_width);
1719 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1723 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1724 int x, int y, int w, int h,
1725 char *data, void *loaderPrivate)
1727 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1730 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1731 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1732 stride, data, loaderPrivate);
1736 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1738 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1739 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1741 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1746 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1748 struct dri2_egl_display *dri2_dpy = data;
1751 case WL_SHM_FORMAT_ARGB8888:
1752 dri2_dpy->formats |= HAS_ARGB8888;
1754 case WL_SHM_FORMAT_XRGB8888:
1755 dri2_dpy->formats |= HAS_XRGB8888;
1757 case WL_SHM_FORMAT_RGB565:
1758 dri2_dpy->formats |= HAS_RGB565;
1763 static const struct wl_shm_listener shm_listener = {
1764 .format = shm_handle_format
1768 registry_handle_global_swrast(void *data, struct wl_registry *registry, uint32_t name,
1769 const char *interface, uint32_t version)
1771 struct dri2_egl_display *dri2_dpy = data;
1773 if (strcmp(interface, "wl_shm") == 0) {
1775 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1776 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1780 static const struct wl_registry_listener registry_listener_swrast = {
1781 .global = registry_handle_global_swrast,
1782 .global_remove = registry_handle_global_remove
1785 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1786 .authenticate = NULL,
1787 .create_window_surface = dri2_wl_create_window_surface,
1788 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1789 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1790 .destroy_surface = dri2_wl_destroy_surface,
1791 .create_image = dri2_fallback_create_image_khr,
1792 .swap_interval = dri2_wl_swap_interval,
1793 .swap_buffers = dri2_wl_swrast_swap_buffers,
1794 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1795 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1796 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1797 .copy_buffers = dri2_fallback_copy_buffers,
1798 .query_buffer_age = dri2_fallback_query_buffer_age,
1799 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1800 .get_sync_values = dri2_fallback_get_sync_values,
1801 .get_dri_drawable = dri2_surface_get_dri_drawable,
1804 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1805 .base = { __DRI_SWRAST_LOADER, 2 },
1807 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
1808 .putImage = dri2_wl_swrast_put_image,
1809 .getImage = dri2_wl_swrast_get_image,
1810 .putImage2 = dri2_wl_swrast_put_image2,
1813 static const __DRIextension *swrast_loader_extensions[] = {
1814 &swrast_loader_extension.base,
1819 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1821 struct dri2_egl_display *dri2_dpy;
1823 loader_set_logger(_eglLog);
1825 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1827 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1829 disp->DriverData = (void *) dri2_dpy;
1830 if (disp->PlatformDisplay == NULL) {
1831 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1832 if (dri2_dpy->wl_dpy == NULL)
1834 dri2_dpy->own_device = 1;
1836 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1839 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1841 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1842 if (dri2_dpy->wl_dpy_wrapper == NULL)
1843 goto cleanup_dpy_wrapper;
1845 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1846 dri2_dpy->wl_queue);
1848 if (dri2_dpy->own_device)
1849 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1851 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1852 wl_registry_add_listener(dri2_dpy->wl_registry,
1853 ®istry_listener_swrast, dri2_dpy);
1855 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
1856 goto cleanup_registry;
1858 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
1862 dri2_dpy->driver_name = strdup("swrast");
1863 if (!dri2_load_driver_swrast(disp))
1866 dri2_dpy->loader_extensions = swrast_loader_extensions;
1868 if (!dri2_create_screen(disp))
1869 goto cleanup_driver;
1871 dri2_wl_setup_swap_interval(dri2_dpy);
1873 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1874 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1875 goto cleanup_screen;
1878 /* Fill vtbl last to prevent accidentally calling virtual function during
1881 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
1886 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1888 dlclose(dri2_dpy->driver);
1890 wl_shm_destroy(dri2_dpy->wl_shm);
1892 wl_registry_destroy(dri2_dpy->wl_registry);
1893 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
1894 cleanup_dpy_wrapper:
1895 wl_event_queue_destroy(dri2_dpy->wl_queue);
1896 if (disp->PlatformDisplay == NULL)
1897 wl_display_disconnect(dri2_dpy->wl_dpy);
1900 disp->DriverData = NULL;
1906 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
1908 EGLBoolean initialized = EGL_TRUE;
1910 int hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1913 if (!dri2_initialize_wayland_drm(drv, disp)) {
1914 initialized = dri2_initialize_wayland_swrast(drv, disp);
1917 initialized = dri2_initialize_wayland_swrast(drv, disp);