egl/wayland: Use visual map for config->format lookup
[mesa.git] / src / egl / drivers / dri2 / platform_wayland.c
1 /*
2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
4 *
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:
11 *
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
14 * Software.
15 *
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.
24 *
25 * Authors:
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
28 */
29
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <dlfcn.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <xf86drm.h>
39 #include <drm_fourcc.h>
40 #include <sys/mman.h>
41
42 #include "egl_dri2.h"
43 #include "egl_dri2_fallbacks.h"
44 #include "loader.h"
45 #include "util/u_vector.h"
46 #include "eglglobals.h"
47
48 #include <wayland-client.h>
49 #include "wayland-drm-client-protocol.h"
50 #include "linux-dmabuf-unstable-v1-client-protocol.h"
51
52 #ifndef DRM_FORMAT_MOD_INVALID
53 #define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
54 #endif
55
56 #ifndef DRM_FORMAT_MOD_LINEAR
57 #define DRM_FORMAT_MOD_LINEAR 0
58 #endif
59
60 enum wl_drm_format_flags {
61 HAS_ARGB8888 = 1,
62 HAS_XRGB8888 = 2,
63 HAS_RGB565 = 4,
64 HAS_ARGB2101010 = 8,
65 HAS_XRGB2101010 = 16,
66 };
67
68 static const struct dri2_wl_visual {
69 const char *format_name;
70 enum wl_drm_format_flags has_format;
71 uint32_t wl_drm_format;
72 uint32_t wl_shm_format;
73 int dri_image_format;
74 unsigned int rgba_masks[4];
75 } dri2_wl_visuals[] = {
76 {
77 "XRGB2101010", HAS_XRGB2101010,
78 WL_DRM_FORMAT_XRGB2101010, WL_SHM_FORMAT_XRGB2101010,
79 __DRI_IMAGE_FORMAT_XRGB2101010,
80 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0x00000000 }
81 },
82 {
83 "ARGB2101010", HAS_ARGB2101010,
84 WL_DRM_FORMAT_ARGB2101010, WL_SHM_FORMAT_ARGB2101010,
85 __DRI_IMAGE_FORMAT_ARGB2101010,
86 { 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 }
87 },
88 {
89 "XRGB8888", HAS_XRGB8888,
90 WL_DRM_FORMAT_XRGB8888, WL_SHM_FORMAT_XRGB8888,
91 __DRI_IMAGE_FORMAT_XRGB8888,
92 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }
93 },
94 {
95 "ARGB8888", HAS_ARGB8888,
96 WL_DRM_FORMAT_ARGB8888, WL_SHM_FORMAT_ARGB8888,
97 __DRI_IMAGE_FORMAT_ARGB8888,
98 { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }
99 },
100 {
101 "RGB565", HAS_RGB565,
102 WL_DRM_FORMAT_RGB565, WL_SHM_FORMAT_RGB565,
103 __DRI_IMAGE_FORMAT_RGB565,
104 { 0xf800, 0x07e0, 0x001f, 0x0000 }
105 },
106 };
107
108 static int
109 dri2_wl_visual_idx_from_config(struct dri2_egl_display *dri2_dpy,
110 const __DRIconfig *config)
111 {
112 unsigned int red, green, blue, alpha;
113
114 dri2_dpy->core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &red);
115 dri2_dpy->core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &green);
116 dri2_dpy->core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &blue);
117 dri2_dpy->core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &alpha);
118
119 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
120 const struct dri2_wl_visual *wl_visual = &dri2_wl_visuals[i];
121
122 if (red == wl_visual->rgba_masks[0] &&
123 green == wl_visual->rgba_masks[1] &&
124 blue == wl_visual->rgba_masks[2] &&
125 alpha == wl_visual->rgba_masks[3]) {
126 return i;
127 }
128 }
129
130 return -1;
131 }
132
133 static int
134 roundtrip(struct dri2_egl_display *dri2_dpy)
135 {
136 return wl_display_roundtrip_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
137 }
138
139 static void
140 wl_buffer_release(void *data, struct wl_buffer *buffer)
141 {
142 struct dri2_egl_surface *dri2_surf = data;
143 int i;
144
145 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
146 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
147 break;
148
149 if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
150 wl_buffer_destroy(buffer);
151 return;
152 }
153
154 dri2_surf->color_buffers[i].locked = false;
155 }
156
157 static const struct wl_buffer_listener wl_buffer_listener = {
158 .release = wl_buffer_release
159 };
160
161 static void
162 resize_callback(struct wl_egl_window *wl_win, void *data)
163 {
164 struct dri2_egl_surface *dri2_surf = data;
165 struct dri2_egl_display *dri2_dpy =
166 dri2_egl_display(dri2_surf->base.Resource.Display);
167
168 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
169 }
170
171 static void
172 destroy_window_callback(void *data)
173 {
174 struct dri2_egl_surface *dri2_surf = data;
175 dri2_surf->wl_win = NULL;
176 }
177
178 static struct wl_surface *
179 get_wl_surface_proxy(struct wl_egl_window *window)
180 {
181 /* Version 3 of wl_egl_window introduced a version field at the same
182 * location where a pointer to wl_surface was stored. Thus, if
183 * window->version is dereferencable, we've been given an older version of
184 * wl_egl_window, and window->version points to wl_surface */
185 if (_eglPointerIsDereferencable((void *)(window->version))) {
186 return wl_proxy_create_wrapper((void *)(window->version));
187 }
188 return wl_proxy_create_wrapper(window->surface);
189 }
190
191 /**
192 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
193 */
194 static _EGLSurface *
195 dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
196 _EGLConfig *conf, void *native_window,
197 const EGLint *attrib_list)
198 {
199 __DRIcreateNewDrawableFunc createNewDrawable;
200 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
201 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
202 struct wl_egl_window *window = native_window;
203 struct dri2_egl_surface *dri2_surf;
204 int visual_idx;
205 const __DRIconfig *config;
206
207 dri2_surf = calloc(1, sizeof *dri2_surf);
208 if (!dri2_surf) {
209 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
210 return NULL;
211 }
212
213 if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf,
214 attrib_list, false))
215 goto cleanup_surf;
216
217 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
218 dri2_surf->base.GLColorspace);
219 visual_idx = dri2_wl_visual_idx_from_config(dri2_dpy, config);
220 assert(visual_idx != -1);
221
222 if (dri2_dpy->wl_dmabuf || dri2_dpy->wl_drm) {
223 dri2_surf->format = dri2_wl_visuals[visual_idx].wl_drm_format;
224 } else {
225 assert(dri2_dpy->wl_shm);
226 dri2_surf->format = dri2_wl_visuals[visual_idx].wl_shm_format;
227 }
228
229 dri2_surf->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
230 if (!dri2_surf->wl_queue) {
231 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
232 goto cleanup_surf;
233 }
234
235 if (dri2_dpy->wl_drm) {
236 dri2_surf->wl_drm_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_drm);
237 if (!dri2_surf->wl_drm_wrapper) {
238 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
239 goto cleanup_queue;
240 }
241 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_drm_wrapper,
242 dri2_surf->wl_queue);
243 }
244
245 dri2_surf->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
246 if (!dri2_surf->wl_dpy_wrapper) {
247 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
248 goto cleanup_drm;
249 }
250 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_dpy_wrapper,
251 dri2_surf->wl_queue);
252
253 dri2_surf->wl_surface_wrapper = get_wl_surface_proxy(window);
254 if (!dri2_surf->wl_surface_wrapper) {
255 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
256 goto cleanup_dpy_wrapper;
257 }
258 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_surface_wrapper,
259 dri2_surf->wl_queue);
260
261 dri2_surf->wl_win = window;
262 dri2_surf->wl_win->private = dri2_surf;
263 dri2_surf->wl_win->destroy_window_callback = destroy_window_callback;
264 if (dri2_dpy->flush)
265 dri2_surf->wl_win->resize_callback = resize_callback;
266
267 if (dri2_dpy->image_driver)
268 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
269 else if (dri2_dpy->dri2)
270 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
271 else
272 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
273
274 dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
275 dri2_surf);
276 if (dri2_surf->dri_drawable == NULL) {
277 _eglError(EGL_BAD_ALLOC, "createNewDrawable");
278 goto cleanup_surf_wrapper;
279 }
280
281 dri2_surf->base.SwapInterval = dri2_dpy->default_swap_interval;
282
283 return &dri2_surf->base;
284
285 cleanup_surf_wrapper:
286 wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
287 cleanup_dpy_wrapper:
288 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
289 cleanup_drm:
290 if (dri2_surf->wl_drm_wrapper)
291 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
292 cleanup_queue:
293 wl_event_queue_destroy(dri2_surf->wl_queue);
294 cleanup_surf:
295 free(dri2_surf);
296
297 return NULL;
298 }
299
300 static _EGLSurface *
301 dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
302 _EGLConfig *conf, void *native_window,
303 const EGLint *attrib_list)
304 {
305 /* From the EGL_EXT_platform_wayland spec, version 3:
306 *
307 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
308 * that belongs to Wayland. Any such call fails and generates
309 * EGL_BAD_PARAMETER.
310 */
311 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on "
312 "Wayland");
313 return NULL;
314 }
315
316 /**
317 * Called via eglDestroySurface(), drv->API.DestroySurface().
318 */
319 static EGLBoolean
320 dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
321 {
322 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
323 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
324
325 (void) drv;
326
327 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
328
329 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
330 if (dri2_surf->color_buffers[i].wl_buffer)
331 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
332 if (dri2_surf->color_buffers[i].dri_image)
333 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
334 if (dri2_surf->color_buffers[i].linear_copy)
335 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
336 if (dri2_surf->color_buffers[i].data)
337 munmap(dri2_surf->color_buffers[i].data,
338 dri2_surf->color_buffers[i].data_size);
339 }
340
341 if (dri2_dpy->dri2)
342 dri2_egl_surface_free_local_buffers(dri2_surf);
343
344 if (dri2_surf->throttle_callback)
345 wl_callback_destroy(dri2_surf->throttle_callback);
346
347 if (dri2_surf->wl_win) {
348 dri2_surf->wl_win->private = NULL;
349 dri2_surf->wl_win->resize_callback = NULL;
350 dri2_surf->wl_win->destroy_window_callback = NULL;
351 }
352
353 wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
354 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
355 if (dri2_surf->wl_drm_wrapper)
356 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
357 wl_event_queue_destroy(dri2_surf->wl_queue);
358
359 dri2_fini_surface(surf);
360 free(surf);
361
362 return EGL_TRUE;
363 }
364
365 static void
366 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
367 {
368 struct dri2_egl_display *dri2_dpy =
369 dri2_egl_display(dri2_surf->base.Resource.Display);
370
371 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
372 if (dri2_surf->color_buffers[i].wl_buffer &&
373 !dri2_surf->color_buffers[i].locked)
374 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
375 if (dri2_surf->color_buffers[i].dri_image)
376 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
377 if (dri2_surf->color_buffers[i].linear_copy)
378 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
379 if (dri2_surf->color_buffers[i].data)
380 munmap(dri2_surf->color_buffers[i].data,
381 dri2_surf->color_buffers[i].data_size);
382
383 dri2_surf->color_buffers[i].wl_buffer = NULL;
384 dri2_surf->color_buffers[i].dri_image = NULL;
385 dri2_surf->color_buffers[i].linear_copy = NULL;
386 dri2_surf->color_buffers[i].data = NULL;
387 dri2_surf->color_buffers[i].locked = false;
388 }
389
390 if (dri2_dpy->dri2)
391 dri2_egl_surface_free_local_buffers(dri2_surf);
392 }
393
394 static int
395 get_back_bo(struct dri2_egl_surface *dri2_surf)
396 {
397 struct dri2_egl_display *dri2_dpy =
398 dri2_egl_display(dri2_surf->base.Resource.Display);
399 int use_flags;
400 unsigned int dri_image_format;
401 uint64_t *modifiers;
402 int num_modifiers;
403
404 /* currently supports five WL DRM formats,
405 * WL_DRM_FORMAT_ARGB2101010, WL_DRM_FORMAT_XRGB2101010,
406 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
407 * and WL_DRM_FORMAT_RGB565
408 */
409 switch (dri2_surf->format) {
410 case WL_DRM_FORMAT_ARGB2101010:
411 dri_image_format = __DRI_IMAGE_FORMAT_ARGB2101010;
412 modifiers = u_vector_tail(&dri2_dpy->wl_modifiers.argb2101010);
413 num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers.argb2101010);
414 break;
415 case WL_DRM_FORMAT_XRGB2101010:
416 dri_image_format = __DRI_IMAGE_FORMAT_XRGB2101010;
417 modifiers = u_vector_tail(&dri2_dpy->wl_modifiers.xrgb2101010);
418 num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers.xrgb2101010);
419 break;
420 case WL_DRM_FORMAT_ARGB8888:
421 dri_image_format = __DRI_IMAGE_FORMAT_ARGB8888;
422 modifiers = u_vector_tail(&dri2_dpy->wl_modifiers.argb8888);
423 num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers.argb8888);
424 break;
425 case WL_DRM_FORMAT_XRGB8888:
426 dri_image_format = __DRI_IMAGE_FORMAT_XRGB8888;
427 modifiers = u_vector_tail(&dri2_dpy->wl_modifiers.xrgb8888);
428 num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers.xrgb8888);
429 break;
430 case WL_DRM_FORMAT_RGB565:
431 dri_image_format = __DRI_IMAGE_FORMAT_RGB565;
432 modifiers = u_vector_tail(&dri2_dpy->wl_modifiers.rgb565);
433 num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers.rgb565);
434 break;
435 default:
436 /* format is not supported */
437 return -1;
438 }
439
440 /* There might be a buffer release already queued that wasn't processed */
441 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
442
443 while (dri2_surf->back == NULL) {
444 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
445 /* Get an unlocked buffer, preferrably one with a dri_buffer
446 * already allocated. */
447 if (dri2_surf->color_buffers[i].locked)
448 continue;
449 if (dri2_surf->back == NULL)
450 dri2_surf->back = &dri2_surf->color_buffers[i];
451 else if (dri2_surf->back->dri_image == NULL)
452 dri2_surf->back = &dri2_surf->color_buffers[i];
453 }
454
455 if (dri2_surf->back)
456 break;
457
458 /* If we don't have a buffer, then block on the server to release one for
459 * us, and try again. wl_display_dispatch_queue will process any pending
460 * events, however not all servers flush on issuing a buffer release
461 * event. So, we spam the server with roundtrips as they always cause a
462 * client flush.
463 */
464 if (wl_display_roundtrip_queue(dri2_dpy->wl_dpy,
465 dri2_surf->wl_queue) < 0)
466 return -1;
467 }
468
469 if (dri2_surf->back == NULL)
470 return -1;
471
472 use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER;
473
474 if (dri2_dpy->is_different_gpu &&
475 dri2_surf->back->linear_copy == NULL) {
476 /* The LINEAR modifier should be a perfect alias of the LINEAR use
477 * flag; try the new interface first before the old, then fall back. */
478 if (dri2_dpy->image->base.version >= 15 &&
479 dri2_dpy->image->createImageWithModifiers) {
480 uint64_t linear_mod = DRM_FORMAT_MOD_LINEAR;
481
482 dri2_surf->back->linear_copy =
483 dri2_dpy->image->createImageWithModifiers(dri2_dpy->dri_screen,
484 dri2_surf->base.Width,
485 dri2_surf->base.Height,
486 dri_image_format,
487 &linear_mod,
488 1,
489 NULL);
490 } else {
491 dri2_surf->back->linear_copy =
492 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
493 dri2_surf->base.Width,
494 dri2_surf->base.Height,
495 dri_image_format,
496 use_flags |
497 __DRI_IMAGE_USE_LINEAR,
498 NULL);
499 }
500 if (dri2_surf->back->linear_copy == NULL)
501 return -1;
502 }
503
504 if (dri2_surf->back->dri_image == NULL) {
505 /* If our DRIImage implementation does not support
506 * createImageWithModifiers, then fall back to the old createImage,
507 * and hope it allocates an image which is acceptable to the winsys.
508 */
509 if (num_modifiers && dri2_dpy->image->base.version >= 15 &&
510 dri2_dpy->image->createImageWithModifiers) {
511 dri2_surf->back->dri_image =
512 dri2_dpy->image->createImageWithModifiers(dri2_dpy->dri_screen,
513 dri2_surf->base.Width,
514 dri2_surf->base.Height,
515 dri_image_format,
516 modifiers,
517 num_modifiers,
518 NULL);
519 } else {
520 dri2_surf->back->dri_image =
521 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
522 dri2_surf->base.Width,
523 dri2_surf->base.Height,
524 dri_image_format,
525 dri2_dpy->is_different_gpu ?
526 0 : use_flags,
527 NULL);
528 }
529
530 dri2_surf->back->age = 0;
531 }
532 if (dri2_surf->back->dri_image == NULL)
533 return -1;
534
535 dri2_surf->back->locked = true;
536
537 return 0;
538 }
539
540
541 static void
542 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
543 {
544 struct dri2_egl_display *dri2_dpy =
545 dri2_egl_display(dri2_surf->base.Resource.Display);
546 __DRIimage *image;
547 int name, pitch;
548
549 image = dri2_surf->back->dri_image;
550
551 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
552 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
553
554 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
555 buffer->name = name;
556 buffer->pitch = pitch;
557 buffer->cpp = 4;
558 buffer->flags = 0;
559 }
560
561 static int
562 update_buffers(struct dri2_egl_surface *dri2_surf)
563 {
564 struct dri2_egl_display *dri2_dpy =
565 dri2_egl_display(dri2_surf->base.Resource.Display);
566
567 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
568 dri2_surf->base.Height != dri2_surf->wl_win->height) {
569
570 dri2_wl_release_buffers(dri2_surf);
571
572 dri2_surf->base.Width = dri2_surf->wl_win->width;
573 dri2_surf->base.Height = dri2_surf->wl_win->height;
574 dri2_surf->dx = dri2_surf->wl_win->dx;
575 dri2_surf->dy = dri2_surf->wl_win->dy;
576 }
577
578 if (get_back_bo(dri2_surf) < 0) {
579 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
580 return -1;
581 }
582
583 /* If we have an extra unlocked buffer at this point, we had to do triple
584 * buffering for a while, but now can go back to just double buffering.
585 * That means we can free any unlocked buffer now. */
586 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
587 if (!dri2_surf->color_buffers[i].locked &&
588 dri2_surf->color_buffers[i].wl_buffer) {
589 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
590 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
591 if (dri2_dpy->is_different_gpu)
592 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
593 dri2_surf->color_buffers[i].wl_buffer = NULL;
594 dri2_surf->color_buffers[i].dri_image = NULL;
595 dri2_surf->color_buffers[i].linear_copy = NULL;
596 }
597 }
598
599 return 0;
600 }
601
602 static __DRIbuffer *
603 dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
604 int *width, int *height,
605 unsigned int *attachments, int count,
606 int *out_count, void *loaderPrivate)
607 {
608 struct dri2_egl_surface *dri2_surf = loaderPrivate;
609 int i, j;
610
611 if (update_buffers(dri2_surf) < 0)
612 return NULL;
613
614 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
615 __DRIbuffer *local;
616
617 switch (attachments[i]) {
618 case __DRI_BUFFER_BACK_LEFT:
619 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
620 break;
621 default:
622 local = dri2_egl_surface_alloc_local_buffer(dri2_surf, attachments[i],
623 attachments[i + 1]);
624
625 if (!local) {
626 _eglError(EGL_BAD_ALLOC, "failed to allocate local buffer");
627 return NULL;
628 }
629 dri2_surf->buffers[j] = *local;
630 break;
631 }
632 }
633
634 *out_count = j;
635 if (j == 0)
636 return NULL;
637
638 *width = dri2_surf->base.Width;
639 *height = dri2_surf->base.Height;
640
641 return dri2_surf->buffers;
642 }
643
644 static __DRIbuffer *
645 dri2_wl_get_buffers(__DRIdrawable * driDrawable,
646 int *width, int *height,
647 unsigned int *attachments, int count,
648 int *out_count, void *loaderPrivate)
649 {
650 struct dri2_egl_surface *dri2_surf = loaderPrivate;
651 unsigned int *attachments_with_format;
652 __DRIbuffer *buffer;
653 unsigned int bpp;
654
655 switch (dri2_surf->format) {
656 case WL_DRM_FORMAT_ARGB2101010:
657 case WL_DRM_FORMAT_XRGB2101010:
658 case WL_DRM_FORMAT_ARGB8888:
659 case WL_DRM_FORMAT_XRGB8888:
660 bpp = 32;
661 break;
662 case WL_DRM_FORMAT_RGB565:
663 bpp = 16;
664 break;
665 default:
666 /* format is not supported */
667 return NULL;
668 }
669
670 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
671 if (!attachments_with_format) {
672 *out_count = 0;
673 return NULL;
674 }
675
676 for (int i = 0; i < count; ++i) {
677 attachments_with_format[2*i] = attachments[i];
678 attachments_with_format[2*i + 1] = bpp;
679 }
680
681 buffer =
682 dri2_wl_get_buffers_with_format(driDrawable,
683 width, height,
684 attachments_with_format, count,
685 out_count, loaderPrivate);
686
687 free(attachments_with_format);
688
689 return buffer;
690 }
691
692 static int
693 image_get_buffers(__DRIdrawable *driDrawable,
694 unsigned int format,
695 uint32_t *stamp,
696 void *loaderPrivate,
697 uint32_t buffer_mask,
698 struct __DRIimageList *buffers)
699 {
700 struct dri2_egl_surface *dri2_surf = loaderPrivate;
701
702 if (update_buffers(dri2_surf) < 0)
703 return 0;
704
705 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
706 buffers->back = dri2_surf->back->dri_image;
707
708 return 1;
709 }
710
711 static void
712 dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
713 {
714 (void) driDrawable;
715 (void) loaderPrivate;
716 }
717
718 static const __DRIdri2LoaderExtension dri2_loader_extension = {
719 .base = { __DRI_DRI2_LOADER, 3 },
720
721 .getBuffers = dri2_wl_get_buffers,
722 .flushFrontBuffer = dri2_wl_flush_front_buffer,
723 .getBuffersWithFormat = dri2_wl_get_buffers_with_format,
724 };
725
726 static const __DRIimageLoaderExtension image_loader_extension = {
727 .base = { __DRI_IMAGE_LOADER, 1 },
728
729 .getBuffers = image_get_buffers,
730 .flushFrontBuffer = dri2_wl_flush_front_buffer,
731 };
732
733 static void
734 wayland_throttle_callback(void *data,
735 struct wl_callback *callback,
736 uint32_t time)
737 {
738 struct dri2_egl_surface *dri2_surf = data;
739
740 dri2_surf->throttle_callback = NULL;
741 wl_callback_destroy(callback);
742 }
743
744 static const struct wl_callback_listener throttle_listener = {
745 .done = wayland_throttle_callback
746 };
747
748 static EGLBoolean
749 get_fourcc(struct dri2_egl_display *dri2_dpy,
750 __DRIimage *image, int *fourcc)
751 {
752 EGLBoolean query;
753 int dri_format;
754
755 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FOURCC,
756 fourcc);
757 if (query)
758 return true;
759
760 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT,
761 &dri_format);
762 if (!query)
763 return false;
764
765 switch (dri_format) {
766 case __DRI_IMAGE_FORMAT_ARGB8888:
767 *fourcc = __DRI_IMAGE_FOURCC_ARGB8888;
768 return true;
769 case __DRI_IMAGE_FORMAT_XRGB8888:
770 *fourcc = __DRI_IMAGE_FOURCC_XRGB8888;
771 return true;
772 default:
773 return false;
774 }
775 }
776
777 static struct wl_buffer *
778 create_wl_buffer(struct dri2_egl_display *dri2_dpy,
779 struct dri2_egl_surface *dri2_surf,
780 __DRIimage *image)
781 {
782 struct wl_buffer *ret;
783 EGLBoolean query;
784 int width, height, fourcc, num_planes;
785 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
786
787 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
788 query &= dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT,
789 &height);
790 query &= get_fourcc(dri2_dpy, image, &fourcc);
791 if (!query)
792 return NULL;
793
794 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES,
795 &num_planes);
796 if (!query)
797 num_planes = 1;
798
799 if (dri2_dpy->image->base.version >= 15) {
800 int mod_hi, mod_lo;
801
802 query = dri2_dpy->image->queryImage(image,
803 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
804 &mod_hi);
805 query &= dri2_dpy->image->queryImage(image,
806 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
807 &mod_lo);
808 if (query) {
809 modifier = (uint64_t) mod_hi << 32;
810 modifier |= (uint64_t) (mod_lo & 0xffffffff);
811 }
812 }
813
814 if (dri2_dpy->wl_dmabuf && modifier != DRM_FORMAT_MOD_INVALID) {
815 struct zwp_linux_buffer_params_v1 *params;
816 int i;
817
818 /* We don't need a wrapper for wl_dmabuf objects, because we have to
819 * create the intermediate params object; we can set the queue on this,
820 * and the wl_buffer inherits it race-free. */
821 params = zwp_linux_dmabuf_v1_create_params(dri2_dpy->wl_dmabuf);
822 if (dri2_surf)
823 wl_proxy_set_queue((struct wl_proxy *) params, dri2_surf->wl_queue);
824
825 for (i = 0; i < num_planes; i++) {
826 __DRIimage *p_image;
827 int stride, offset;
828 int fd = -1;
829
830 if (i == 0)
831 p_image = image;
832 else
833 p_image = dri2_dpy->image->fromPlanar(image, i, NULL);
834 if (!p_image) {
835 zwp_linux_buffer_params_v1_destroy(params);
836 return NULL;
837 }
838
839 query = dri2_dpy->image->queryImage(p_image,
840 __DRI_IMAGE_ATTRIB_FD,
841 &fd);
842 query &= dri2_dpy->image->queryImage(p_image,
843 __DRI_IMAGE_ATTRIB_STRIDE,
844 &stride);
845 query &= dri2_dpy->image->queryImage(p_image,
846 __DRI_IMAGE_ATTRIB_OFFSET,
847 &offset);
848 if (image != p_image)
849 dri2_dpy->image->destroyImage(p_image);
850
851 if (!query) {
852 if (fd >= 0)
853 close(fd);
854 zwp_linux_buffer_params_v1_destroy(params);
855 return NULL;
856 }
857
858 zwp_linux_buffer_params_v1_add(params, fd, i, offset, stride,
859 modifier >> 32, modifier & 0xffffffff);
860 close(fd);
861 }
862
863 ret = zwp_linux_buffer_params_v1_create_immed(params, width, height,
864 fourcc, 0);
865 zwp_linux_buffer_params_v1_destroy(params);
866 } else if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
867 struct wl_drm *wl_drm =
868 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
869 int fd, stride;
870
871 if (num_planes > 1)
872 return NULL;
873
874 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
875 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
876 ret = wl_drm_create_prime_buffer(wl_drm, fd, width, height, fourcc, 0,
877 stride, 0, 0, 0, 0);
878 close(fd);
879 } else {
880 struct wl_drm *wl_drm =
881 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
882 int name, stride;
883
884 if (num_planes > 1)
885 return NULL;
886
887 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
888 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
889 ret = wl_drm_create_buffer(wl_drm, name, width, height, stride, fourcc);
890 }
891
892 return ret;
893 }
894
895 static EGLBoolean
896 try_damage_buffer(struct dri2_egl_surface *dri2_surf,
897 const EGLint *rects,
898 EGLint n_rects)
899 {
900 if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_surface_wrapper)
901 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
902 return EGL_FALSE;
903
904 for (int i = 0; i < n_rects; i++) {
905 const int *rect = &rects[i * 4];
906
907 wl_surface_damage_buffer(dri2_surf->wl_surface_wrapper,
908 rect[0],
909 dri2_surf->base.Height - rect[1] - rect[3],
910 rect[2], rect[3]);
911 }
912 return EGL_TRUE;
913 }
914 /**
915 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
916 */
917 static EGLBoolean
918 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
919 _EGLDisplay *disp,
920 _EGLSurface *draw,
921 const EGLint *rects,
922 EGLint n_rects)
923 {
924 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
925 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
926
927 while (dri2_surf->throttle_callback != NULL)
928 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
929 dri2_surf->wl_queue) == -1)
930 return -1;
931
932 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
933 if (dri2_surf->color_buffers[i].age > 0)
934 dri2_surf->color_buffers[i].age++;
935
936 /* Make sure we have a back buffer in case we're swapping without ever
937 * rendering. */
938 if (get_back_bo(dri2_surf) < 0)
939 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
940
941 if (draw->SwapInterval > 0) {
942 dri2_surf->throttle_callback =
943 wl_surface_frame(dri2_surf->wl_surface_wrapper);
944 wl_callback_add_listener(dri2_surf->throttle_callback,
945 &throttle_listener, dri2_surf);
946 }
947
948 dri2_surf->back->age = 1;
949 dri2_surf->current = dri2_surf->back;
950 dri2_surf->back = NULL;
951
952 if (!dri2_surf->current->wl_buffer) {
953 __DRIimage *image;
954
955 if (dri2_dpy->is_different_gpu)
956 image = dri2_surf->current->linear_copy;
957 else
958 image = dri2_surf->current->dri_image;
959
960 dri2_surf->current->wl_buffer =
961 create_wl_buffer(dri2_dpy, dri2_surf, image);
962
963 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
964 &wl_buffer_listener, dri2_surf);
965 }
966
967 wl_surface_attach(dri2_surf->wl_surface_wrapper,
968 dri2_surf->current->wl_buffer,
969 dri2_surf->dx, dri2_surf->dy);
970
971 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
972 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
973 /* reset resize growing parameters */
974 dri2_surf->dx = 0;
975 dri2_surf->dy = 0;
976
977 /* If the compositor doesn't support damage_buffer, we deliberately
978 * ignore the damage region and post maximum damage, due to
979 * https://bugs.freedesktop.org/78190 */
980 if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
981 wl_surface_damage(dri2_surf->wl_surface_wrapper,
982 0, 0, INT32_MAX, INT32_MAX);
983
984 if (dri2_dpy->is_different_gpu) {
985 _EGLContext *ctx = _eglGetCurrentContext();
986 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
987 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
988 dri2_surf->current->linear_copy,
989 dri2_surf->current->dri_image,
990 0, 0, dri2_surf->base.Width,
991 dri2_surf->base.Height,
992 0, 0, dri2_surf->base.Width,
993 dri2_surf->base.Height, 0);
994 }
995
996 dri2_flush_drawable_for_swapbuffers(disp, draw);
997 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
998
999 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1000
1001 /* If we're not waiting for a frame callback then we'll at least throttle
1002 * to a sync callback so that we always give a chance for the compositor to
1003 * handle the commit and send a release event before checking for a free
1004 * buffer */
1005 if (dri2_surf->throttle_callback == NULL) {
1006 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1007 wl_callback_add_listener(dri2_surf->throttle_callback,
1008 &throttle_listener, dri2_surf);
1009 }
1010
1011 wl_display_flush(dri2_dpy->wl_dpy);
1012
1013 return EGL_TRUE;
1014 }
1015
1016 static EGLint
1017 dri2_wl_query_buffer_age(_EGLDriver *drv,
1018 _EGLDisplay *disp, _EGLSurface *surface)
1019 {
1020 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1021
1022 if (get_back_bo(dri2_surf) < 0) {
1023 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
1024 return -1;
1025 }
1026
1027 return dri2_surf->back->age;
1028 }
1029
1030 static EGLBoolean
1031 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1032 {
1033 return dri2_wl_swap_buffers_with_damage(drv, disp, draw, NULL, 0);
1034 }
1035
1036 static struct wl_buffer *
1037 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
1038 _EGLDisplay *disp,
1039 _EGLImage *img)
1040 {
1041 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1042 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1043 __DRIimage *image = dri2_img->dri_image;
1044 struct wl_buffer *buffer;
1045 int format;
1046
1047 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
1048 switch (format) {
1049 case __DRI_IMAGE_FORMAT_ARGB2101010:
1050 if (!(dri2_dpy->formats & HAS_ARGB2101010))
1051 goto bad_format;
1052 break;
1053 case __DRI_IMAGE_FORMAT_XRGB2101010:
1054 if (!(dri2_dpy->formats & HAS_XRGB2101010))
1055 goto bad_format;
1056 break;
1057 case __DRI_IMAGE_FORMAT_ARGB8888:
1058 if (!(dri2_dpy->formats & HAS_ARGB8888))
1059 goto bad_format;
1060 break;
1061 case __DRI_IMAGE_FORMAT_XRGB8888:
1062 if (!(dri2_dpy->formats & HAS_XRGB8888))
1063 goto bad_format;
1064 break;
1065 default:
1066 goto bad_format;
1067 }
1068
1069 buffer = create_wl_buffer(dri2_dpy, NULL, image);
1070
1071 /* The buffer object will have been created with our internal event queue
1072 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1073 * buffer to be used by the application so we'll reset it to the display's
1074 * default event queue. This isn't actually racy, as the only event the
1075 * buffer can get is a buffer release, which doesn't happen with an explicit
1076 * attach. */
1077 if (buffer)
1078 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
1079
1080 return buffer;
1081
1082 bad_format:
1083 _eglError(EGL_BAD_MATCH, "unsupported image format");
1084 return NULL;
1085 }
1086
1087 static int
1088 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
1089 {
1090 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1091 int ret = 0;
1092
1093 if (dri2_dpy->is_render_node) {
1094 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
1095 "authenticate for render-nodes");
1096 return 0;
1097 }
1098 dri2_dpy->authenticated = false;
1099
1100 wl_drm_authenticate(dri2_dpy->wl_drm, id);
1101 if (roundtrip(dri2_dpy) < 0)
1102 ret = -1;
1103
1104 if (!dri2_dpy->authenticated)
1105 ret = -1;
1106
1107 /* reset authenticated */
1108 dri2_dpy->authenticated = true;
1109
1110 return ret;
1111 }
1112
1113 static void
1114 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1115 {
1116 struct dri2_egl_display *dri2_dpy = data;
1117 drm_magic_t magic;
1118
1119 dri2_dpy->device_name = strdup(device);
1120 if (!dri2_dpy->device_name)
1121 return;
1122
1123 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
1124 if (dri2_dpy->fd == -1) {
1125 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
1126 dri2_dpy->device_name, strerror(errno));
1127 return;
1128 }
1129
1130 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
1131 dri2_dpy->authenticated = true;
1132 } else {
1133 drmGetMagic(dri2_dpy->fd, &magic);
1134 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
1135 }
1136 }
1137
1138 static void
1139 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
1140 {
1141 struct dri2_egl_display *dri2_dpy = data;
1142
1143 switch (format) {
1144 case WL_DRM_FORMAT_ARGB2101010:
1145 dri2_dpy->formats |= HAS_ARGB2101010;
1146 break;
1147 case WL_DRM_FORMAT_XRGB2101010:
1148 dri2_dpy->formats |= HAS_XRGB2101010;
1149 break;
1150 case WL_DRM_FORMAT_ARGB8888:
1151 dri2_dpy->formats |= HAS_ARGB8888;
1152 break;
1153 case WL_DRM_FORMAT_XRGB8888:
1154 dri2_dpy->formats |= HAS_XRGB8888;
1155 break;
1156 case WL_DRM_FORMAT_RGB565:
1157 dri2_dpy->formats |= HAS_RGB565;
1158 break;
1159 }
1160 }
1161
1162 static void
1163 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
1164 {
1165 struct dri2_egl_display *dri2_dpy = data;
1166
1167 dri2_dpy->capabilities = value;
1168 }
1169
1170 static void
1171 drm_handle_authenticated(void *data, struct wl_drm *drm)
1172 {
1173 struct dri2_egl_display *dri2_dpy = data;
1174
1175 dri2_dpy->authenticated = true;
1176 }
1177
1178 static const struct wl_drm_listener drm_listener = {
1179 .device = drm_handle_device,
1180 .format = drm_handle_format,
1181 .authenticated = drm_handle_authenticated,
1182 .capabilities = drm_handle_capabilities
1183 };
1184
1185 static void
1186 dmabuf_ignore_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1187 uint32_t format)
1188 {
1189 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1190 }
1191
1192 static void
1193 dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1194 uint32_t format, uint32_t modifier_hi,
1195 uint32_t modifier_lo)
1196 {
1197 struct dri2_egl_display *dri2_dpy = data;
1198 uint64_t *mod = NULL;
1199
1200 if (modifier_hi == (DRM_FORMAT_MOD_INVALID >> 32) &&
1201 modifier_lo == (DRM_FORMAT_MOD_INVALID & 0xffffffff))
1202 return;
1203
1204 switch (format) {
1205 case WL_DRM_FORMAT_ARGB2101010:
1206 mod = u_vector_add(&dri2_dpy->wl_modifiers.argb2101010);
1207 dri2_dpy->formats |= HAS_ARGB2101010;
1208 break;
1209 case WL_DRM_FORMAT_XRGB2101010:
1210 mod = u_vector_add(&dri2_dpy->wl_modifiers.xrgb2101010);
1211 dri2_dpy->formats |= HAS_XRGB2101010;
1212 break;
1213 case WL_DRM_FORMAT_ARGB8888:
1214 mod = u_vector_add(&dri2_dpy->wl_modifiers.argb8888);
1215 dri2_dpy->formats |= HAS_ARGB8888;
1216 break;
1217 case WL_DRM_FORMAT_XRGB8888:
1218 mod = u_vector_add(&dri2_dpy->wl_modifiers.xrgb8888);
1219 dri2_dpy->formats |= HAS_XRGB8888;
1220 break;
1221 case WL_DRM_FORMAT_RGB565:
1222 mod = u_vector_add(&dri2_dpy->wl_modifiers.rgb565);
1223 dri2_dpy->formats |= HAS_RGB565;
1224 break;
1225 default:
1226 break;
1227 }
1228
1229 if (!mod)
1230 return;
1231
1232 *mod = (uint64_t) modifier_hi << 32;
1233 *mod |= (uint64_t) (modifier_lo & 0xffffffff);
1234 }
1235
1236 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
1237 .format = dmabuf_ignore_format,
1238 .modifier = dmabuf_handle_modifier,
1239 };
1240
1241 static void
1242 registry_handle_global_drm(void *data, struct wl_registry *registry,
1243 uint32_t name, const char *interface,
1244 uint32_t version)
1245 {
1246 struct dri2_egl_display *dri2_dpy = data;
1247
1248 if (strcmp(interface, "wl_drm") == 0) {
1249 dri2_dpy->wl_drm =
1250 wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));
1251 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
1252 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) {
1253 dri2_dpy->wl_dmabuf =
1254 wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface,
1255 MIN2(version, 3));
1256 zwp_linux_dmabuf_v1_add_listener(dri2_dpy->wl_dmabuf, &dmabuf_listener,
1257 dri2_dpy);
1258 }
1259 }
1260
1261 static void
1262 registry_handle_global_remove(void *data, struct wl_registry *registry,
1263 uint32_t name)
1264 {
1265 }
1266
1267 static const struct wl_registry_listener registry_listener_drm = {
1268 .global = registry_handle_global_drm,
1269 .global_remove = registry_handle_global_remove
1270 };
1271
1272 static void
1273 dri2_wl_setup_swap_interval(_EGLDisplay *disp)
1274 {
1275 /* We can't use values greater than 1 on Wayland because we are using the
1276 * frame callback to synchronise the frame and the only way we be sure to
1277 * get a frame callback is to attach a new buffer. Therefore we can't just
1278 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1279
1280 dri2_setup_swap_interval(disp, 1);
1281 }
1282
1283 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1284 .authenticate = dri2_wl_authenticate,
1285 .create_window_surface = dri2_wl_create_window_surface,
1286 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1287 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1288 .destroy_surface = dri2_wl_destroy_surface,
1289 .create_image = dri2_create_image_khr,
1290 .swap_buffers = dri2_wl_swap_buffers,
1291 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1292 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1293 .set_damage_region = dri2_fallback_set_damage_region,
1294 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1295 .copy_buffers = dri2_fallback_copy_buffers,
1296 .query_buffer_age = dri2_wl_query_buffer_age,
1297 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1298 .get_sync_values = dri2_fallback_get_sync_values,
1299 .get_dri_drawable = dri2_surface_get_dri_drawable,
1300 };
1301
1302 static const __DRIextension *dri2_loader_extensions[] = {
1303 &dri2_loader_extension.base,
1304 &image_loader_extension.base,
1305 &image_lookup_extension.base,
1306 &use_invalidate.base,
1307 NULL,
1308 };
1309
1310 static const __DRIextension *image_loader_extensions[] = {
1311 &image_loader_extension.base,
1312 &image_lookup_extension.base,
1313 &use_invalidate.base,
1314 NULL,
1315 };
1316
1317 static EGLBoolean
1318 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1319 {
1320 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1321 unsigned int format_count[ARRAY_SIZE(dri2_wl_visuals)] = { 0 };
1322 unsigned int count = 0;
1323
1324 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
1325 for (unsigned j = 0; j < ARRAY_SIZE(dri2_wl_visuals); j++) {
1326 struct dri2_egl_config *dri2_conf;
1327
1328 if (!(dri2_dpy->formats & dri2_wl_visuals[j].has_format))
1329 continue;
1330
1331 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1332 count + 1, EGL_WINDOW_BIT, NULL, dri2_wl_visuals[j].rgba_masks);
1333 if (dri2_conf) {
1334 if (dri2_conf->base.ConfigID == count + 1)
1335 count++;
1336 format_count[j]++;
1337 }
1338 }
1339 }
1340
1341 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
1342 if (!format_count[i]) {
1343 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1344 dri2_wl_visuals[i].format_name);
1345 }
1346 }
1347
1348 return (count != 0);
1349 }
1350
1351 static EGLBoolean
1352 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1353 {
1354 struct dri2_egl_display *dri2_dpy;
1355
1356 loader_set_logger(_eglLog);
1357
1358 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1359 if (!dri2_dpy)
1360 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1361
1362 dri2_dpy->fd = -1;
1363 disp->DriverData = (void *) dri2_dpy;
1364 if (disp->PlatformDisplay == NULL) {
1365 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1366 if (dri2_dpy->wl_dpy == NULL)
1367 goto cleanup;
1368 dri2_dpy->own_device = true;
1369 } else {
1370 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1371 }
1372
1373 if (!u_vector_init(&dri2_dpy->wl_modifiers.xrgb2101010, sizeof(uint64_t), 32) ||
1374 !u_vector_init(&dri2_dpy->wl_modifiers.argb2101010, sizeof(uint64_t), 32) ||
1375 !u_vector_init(&dri2_dpy->wl_modifiers.xrgb8888, sizeof(uint64_t), 32) ||
1376 !u_vector_init(&dri2_dpy->wl_modifiers.argb8888, sizeof(uint64_t), 32) ||
1377 !u_vector_init(&dri2_dpy->wl_modifiers.rgb565, sizeof(uint64_t), 32)) {
1378 goto cleanup;
1379 }
1380
1381 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1382
1383 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1384 if (dri2_dpy->wl_dpy_wrapper == NULL)
1385 goto cleanup;
1386
1387 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1388 dri2_dpy->wl_queue);
1389
1390 if (dri2_dpy->own_device)
1391 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1392
1393 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1394 wl_registry_add_listener(dri2_dpy->wl_registry,
1395 &registry_listener_drm, dri2_dpy);
1396 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1397 goto cleanup;
1398
1399 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1400 goto cleanup;
1401
1402 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1403 goto cleanup;
1404
1405 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1406 &dri2_dpy->is_different_gpu);
1407 if (dri2_dpy->is_different_gpu) {
1408 free(dri2_dpy->device_name);
1409 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1410 if (!dri2_dpy->device_name) {
1411 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1412 "for requested GPU");
1413 goto cleanup;
1414 }
1415 }
1416
1417 /* we have to do the check now, because loader_get_user_preferred_fd
1418 * will return a render-node when the requested gpu is different
1419 * to the server, but also if the client asks for the same gpu than
1420 * the server by requesting its pci-id */
1421 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1422
1423 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1424 if (dri2_dpy->driver_name == NULL) {
1425 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1426 goto cleanup;
1427 }
1428
1429 /* render nodes cannot use Gem names, and thus do not support
1430 * the __DRI_DRI2_LOADER extension */
1431 if (!dri2_dpy->is_render_node) {
1432 dri2_dpy->loader_extensions = dri2_loader_extensions;
1433 if (!dri2_load_driver(disp)) {
1434 _eglError(EGL_BAD_ALLOC, "DRI2: failed to load driver");
1435 goto cleanup;
1436 }
1437 } else {
1438 dri2_dpy->loader_extensions = image_loader_extensions;
1439 if (!dri2_load_driver_dri3(disp)) {
1440 _eglError(EGL_BAD_ALLOC, "DRI3: failed to load driver");
1441 goto cleanup;
1442 }
1443 }
1444
1445 if (!dri2_create_screen(disp))
1446 goto cleanup;
1447
1448 if (!dri2_setup_extensions(disp))
1449 goto cleanup;
1450
1451 dri2_setup_screen(disp);
1452
1453 dri2_wl_setup_swap_interval(disp);
1454
1455 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1456 * createImageFromFds support indicates that Prime export/import
1457 * is supported by the driver. Fall back to
1458 * gem names if we don't have Prime support. */
1459
1460 if (dri2_dpy->image->base.version < 7 ||
1461 dri2_dpy->image->createImageFromFds == NULL)
1462 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1463
1464 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1465 * The server needs to accept them */
1466 if (dri2_dpy->is_render_node &&
1467 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1468 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1469 goto cleanup;
1470 }
1471
1472 if (dri2_dpy->is_different_gpu &&
1473 (dri2_dpy->image->base.version < 9 ||
1474 dri2_dpy->image->blitImage == NULL)) {
1475 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1476 "Image extension in the driver is not "
1477 "compatible. Version 9 or later and blitImage() "
1478 "are required");
1479 goto cleanup;
1480 }
1481
1482 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1483 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1484 goto cleanup;
1485 }
1486
1487 dri2_set_WL_bind_wayland_display(drv, disp);
1488 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1489 * because the buffer of the EGLImage has likely a tiling mode the server
1490 * gpu won't support. These is no way to check for now. Thus do not support the
1491 * extension */
1492 if (!dri2_dpy->is_different_gpu)
1493 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1494
1495 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1496
1497 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1498
1499 /* Fill vtbl last to prevent accidentally calling virtual function during
1500 * initialization.
1501 */
1502 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1503
1504 return EGL_TRUE;
1505
1506 cleanup:
1507 dri2_display_destroy(disp);
1508 return EGL_FALSE;
1509 }
1510
1511 static int
1512 dri2_wl_swrast_get_stride_for_format(int format, int w)
1513 {
1514 if (format == WL_SHM_FORMAT_RGB565)
1515 return 2 * w;
1516 else /* ARGB8888 || XRGB8888 || ARGB2101010 || XRGB2101010 */
1517 return 4 * w;
1518 }
1519
1520 /*
1521 * Taken from weston shared/os-compatibility.c
1522 */
1523
1524 #ifndef HAVE_MKOSTEMP
1525
1526 static int
1527 set_cloexec_or_close(int fd)
1528 {
1529 long flags;
1530
1531 if (fd == -1)
1532 return -1;
1533
1534 flags = fcntl(fd, F_GETFD);
1535 if (flags == -1)
1536 goto err;
1537
1538 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1539 goto err;
1540
1541 return fd;
1542
1543 err:
1544 close(fd);
1545 return -1;
1546 }
1547
1548 #endif
1549
1550 /*
1551 * Taken from weston shared/os-compatibility.c
1552 */
1553
1554 static int
1555 create_tmpfile_cloexec(char *tmpname)
1556 {
1557 int fd;
1558
1559 #ifdef HAVE_MKOSTEMP
1560 fd = mkostemp(tmpname, O_CLOEXEC);
1561 if (fd >= 0)
1562 unlink(tmpname);
1563 #else
1564 fd = mkstemp(tmpname);
1565 if (fd >= 0) {
1566 fd = set_cloexec_or_close(fd);
1567 unlink(tmpname);
1568 }
1569 #endif
1570
1571 return fd;
1572 }
1573
1574 /*
1575 * Taken from weston shared/os-compatibility.c
1576 *
1577 * Create a new, unique, anonymous file of the given size, and
1578 * return the file descriptor for it. The file descriptor is set
1579 * CLOEXEC. The file is immediately suitable for mmap()'ing
1580 * the given size at offset zero.
1581 *
1582 * The file should not have a permanent backing store like a disk,
1583 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1584 *
1585 * The file name is deleted from the file system.
1586 *
1587 * The file is suitable for buffer sharing between processes by
1588 * transmitting the file descriptor over Unix sockets using the
1589 * SCM_RIGHTS methods.
1590 *
1591 * If the C library implements posix_fallocate(), it is used to
1592 * guarantee that disk space is available for the file at the
1593 * given size. If disk space is insufficent, errno is set to ENOSPC.
1594 * If posix_fallocate() is not supported, program may receive
1595 * SIGBUS on accessing mmap()'ed file contents instead.
1596 */
1597 static int
1598 os_create_anonymous_file(off_t size)
1599 {
1600 static const char templ[] = "/mesa-shared-XXXXXX";
1601 const char *path;
1602 char *name;
1603 int fd;
1604 int ret;
1605
1606 path = getenv("XDG_RUNTIME_DIR");
1607 if (!path) {
1608 errno = ENOENT;
1609 return -1;
1610 }
1611
1612 name = malloc(strlen(path) + sizeof(templ));
1613 if (!name)
1614 return -1;
1615
1616 strcpy(name, path);
1617 strcat(name, templ);
1618
1619 fd = create_tmpfile_cloexec(name);
1620
1621 free(name);
1622
1623 if (fd < 0)
1624 return -1;
1625
1626 ret = ftruncate(fd, size);
1627 if (ret < 0) {
1628 close(fd);
1629 return -1;
1630 }
1631
1632 return fd;
1633 }
1634
1635
1636 static EGLBoolean
1637 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1638 int format, int w, int h,
1639 void **data, int *size,
1640 struct wl_buffer **buffer)
1641 {
1642 struct dri2_egl_display *dri2_dpy =
1643 dri2_egl_display(dri2_surf->base.Resource.Display);
1644 struct wl_shm_pool *pool;
1645 int fd, stride, size_map;
1646 void *data_map;
1647
1648 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1649 size_map = h * stride;
1650
1651 /* Create a sharable buffer */
1652 fd = os_create_anonymous_file(size_map);
1653 if (fd < 0)
1654 return EGL_FALSE;
1655
1656 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1657 if (data_map == MAP_FAILED) {
1658 close(fd);
1659 return EGL_FALSE;
1660 }
1661
1662 /* Share it in a wl_buffer */
1663 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1664 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1665 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1666 wl_shm_pool_destroy(pool);
1667 close(fd);
1668
1669 *data = data_map;
1670 *size = size_map;
1671 return EGL_TRUE;
1672 }
1673
1674 static int
1675 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1676 {
1677 struct dri2_egl_display *dri2_dpy =
1678 dri2_egl_display(dri2_surf->base.Resource.Display);
1679
1680 /* we need to do the following operations only once per frame */
1681 if (dri2_surf->back)
1682 return 0;
1683
1684 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1685 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1686
1687 dri2_wl_release_buffers(dri2_surf);
1688
1689 dri2_surf->base.Width = dri2_surf->wl_win->width;
1690 dri2_surf->base.Height = dri2_surf->wl_win->height;
1691 dri2_surf->dx = dri2_surf->wl_win->dx;
1692 dri2_surf->dy = dri2_surf->wl_win->dy;
1693 dri2_surf->current = NULL;
1694 }
1695
1696 /* find back buffer */
1697
1698 /* There might be a buffer release already queued that wasn't processed */
1699 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1700
1701 /* try get free buffer already created */
1702 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1703 if (!dri2_surf->color_buffers[i].locked &&
1704 dri2_surf->color_buffers[i].wl_buffer) {
1705 dri2_surf->back = &dri2_surf->color_buffers[i];
1706 break;
1707 }
1708 }
1709
1710 /* else choose any another free location */
1711 if (!dri2_surf->back) {
1712 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1713 if (!dri2_surf->color_buffers[i].locked) {
1714 dri2_surf->back = &dri2_surf->color_buffers[i];
1715 if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1716 dri2_surf->format,
1717 dri2_surf->base.Width,
1718 dri2_surf->base.Height,
1719 &dri2_surf->back->data,
1720 &dri2_surf->back->data_size,
1721 &dri2_surf->back->wl_buffer)) {
1722 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1723 return -1;
1724 }
1725 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1726 &wl_buffer_listener, dri2_surf);
1727 break;
1728 }
1729 }
1730 }
1731
1732 if (!dri2_surf->back) {
1733 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1734 return -1;
1735 }
1736
1737 dri2_surf->back->locked = true;
1738
1739 /* If we have an extra unlocked buffer at this point, we had to do triple
1740 * buffering for a while, but now can go back to just double buffering.
1741 * That means we can free any unlocked buffer now. */
1742 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1743 if (!dri2_surf->color_buffers[i].locked &&
1744 dri2_surf->color_buffers[i].wl_buffer) {
1745 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1746 munmap(dri2_surf->color_buffers[i].data,
1747 dri2_surf->color_buffers[i].data_size);
1748 dri2_surf->color_buffers[i].wl_buffer = NULL;
1749 dri2_surf->color_buffers[i].data = NULL;
1750 }
1751 }
1752
1753 return 0;
1754 }
1755
1756 static void*
1757 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1758 {
1759 /* if there has been a resize: */
1760 if (!dri2_surf->current)
1761 return NULL;
1762
1763 return dri2_surf->current->data;
1764 }
1765
1766 static void*
1767 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1768 {
1769 assert(dri2_surf->back);
1770 return dri2_surf->back->data;
1771 }
1772
1773 static void
1774 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1775 {
1776 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1777
1778 while (dri2_surf->throttle_callback != NULL)
1779 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1780 dri2_surf->wl_queue) == -1)
1781 return;
1782
1783 if (dri2_surf->base.SwapInterval > 0) {
1784 dri2_surf->throttle_callback =
1785 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1786 wl_callback_add_listener(dri2_surf->throttle_callback,
1787 &throttle_listener, dri2_surf);
1788 }
1789
1790 dri2_surf->current = dri2_surf->back;
1791 dri2_surf->back = NULL;
1792
1793 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1794 dri2_surf->current->wl_buffer,
1795 dri2_surf->dx, dri2_surf->dy);
1796
1797 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1798 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1799 /* reset resize growing parameters */
1800 dri2_surf->dx = 0;
1801 dri2_surf->dy = 0;
1802
1803 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1804 0, 0, INT32_MAX, INT32_MAX);
1805 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1806
1807 /* If we're not waiting for a frame callback then we'll at least throttle
1808 * to a sync callback so that we always give a chance for the compositor to
1809 * handle the commit and send a release event before checking for a free
1810 * buffer */
1811 if (dri2_surf->throttle_callback == NULL) {
1812 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy_wrapper);
1813 wl_callback_add_listener(dri2_surf->throttle_callback,
1814 &throttle_listener, dri2_surf);
1815 }
1816
1817 wl_display_flush(dri2_dpy->wl_dpy);
1818 }
1819
1820 static void
1821 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1822 int *x, int *y, int *w, int *h,
1823 void *loaderPrivate)
1824 {
1825 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1826
1827 (void) swrast_update_buffers(dri2_surf);
1828 *x = 0;
1829 *y = 0;
1830 *w = dri2_surf->base.Width;
1831 *h = dri2_surf->base.Height;
1832 }
1833
1834 static void
1835 dri2_wl_swrast_get_image(__DRIdrawable * read,
1836 int x, int y, int w, int h,
1837 char *data, void *loaderPrivate)
1838 {
1839 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1840 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1841 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1842 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1843 int dst_stride = copy_width;
1844 char *src, *dst;
1845
1846 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1847 if (!src) {
1848 memset(data, 0, copy_width * h);
1849 return;
1850 }
1851
1852 assert(data != src);
1853 assert(copy_width <= src_stride);
1854
1855 src += x_offset;
1856 src += y * src_stride;
1857 dst = data;
1858
1859 if (copy_width > src_stride-x_offset)
1860 copy_width = src_stride-x_offset;
1861 if (h > dri2_surf->base.Height-y)
1862 h = dri2_surf->base.Height-y;
1863
1864 for (; h>0; h--) {
1865 memcpy(dst, src, copy_width);
1866 src += src_stride;
1867 dst += dst_stride;
1868 }
1869 }
1870
1871 static void
1872 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1873 int x, int y, int w, int h, int stride,
1874 char *data, void *loaderPrivate)
1875 {
1876 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1877 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1878 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1879 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1880 char *src, *dst;
1881
1882 assert(copy_width <= stride);
1883
1884 (void) swrast_update_buffers(dri2_surf);
1885 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1886
1887 /* partial copy, copy old content */
1888 if (copy_width < dst_stride)
1889 dri2_wl_swrast_get_image(draw, 0, 0,
1890 dri2_surf->base.Width, dri2_surf->base.Height,
1891 dst, loaderPrivate);
1892
1893 dst += x_offset;
1894 dst += y * dst_stride;
1895
1896 src = data;
1897
1898 /* drivers expect we do these checks (and some rely on it) */
1899 if (copy_width > dst_stride-x_offset)
1900 copy_width = dst_stride-x_offset;
1901 if (h > dri2_surf->base.Height-y)
1902 h = dri2_surf->base.Height-y;
1903
1904 for (; h>0; h--) {
1905 memcpy(dst, src, copy_width);
1906 src += stride;
1907 dst += dst_stride;
1908 }
1909 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1910 }
1911
1912 static void
1913 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1914 int x, int y, int w, int h,
1915 char *data, void *loaderPrivate)
1916 {
1917 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1918 int stride;
1919
1920 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1921 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1922 stride, data, loaderPrivate);
1923 }
1924
1925 static EGLBoolean
1926 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1927 {
1928 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1929 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1930
1931 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1932 return EGL_TRUE;
1933 }
1934
1935 static void
1936 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1937 {
1938 struct dri2_egl_display *dri2_dpy = data;
1939
1940 switch (format) {
1941 case WL_SHM_FORMAT_ARGB2101010:
1942 dri2_dpy->formats |= HAS_ARGB2101010;
1943 break;
1944 case WL_SHM_FORMAT_XRGB2101010:
1945 dri2_dpy->formats |= HAS_XRGB2101010;
1946 break;
1947 case WL_SHM_FORMAT_ARGB8888:
1948 dri2_dpy->formats |= HAS_ARGB8888;
1949 break;
1950 case WL_SHM_FORMAT_XRGB8888:
1951 dri2_dpy->formats |= HAS_XRGB8888;
1952 break;
1953 case WL_SHM_FORMAT_RGB565:
1954 dri2_dpy->formats |= HAS_RGB565;
1955 break;
1956 }
1957 }
1958
1959 static const struct wl_shm_listener shm_listener = {
1960 .format = shm_handle_format
1961 };
1962
1963 static void
1964 registry_handle_global_swrast(void *data, struct wl_registry *registry,
1965 uint32_t name, const char *interface,
1966 uint32_t version)
1967 {
1968 struct dri2_egl_display *dri2_dpy = data;
1969
1970 if (strcmp(interface, "wl_shm") == 0) {
1971 dri2_dpy->wl_shm =
1972 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1973 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1974 }
1975 }
1976
1977 static const struct wl_registry_listener registry_listener_swrast = {
1978 .global = registry_handle_global_swrast,
1979 .global_remove = registry_handle_global_remove
1980 };
1981
1982 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1983 .authenticate = NULL,
1984 .create_window_surface = dri2_wl_create_window_surface,
1985 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1986 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1987 .destroy_surface = dri2_wl_destroy_surface,
1988 .create_image = dri2_create_image_khr,
1989 .swap_buffers = dri2_wl_swrast_swap_buffers,
1990 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1991 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1992 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1993 .copy_buffers = dri2_fallback_copy_buffers,
1994 .query_buffer_age = dri2_fallback_query_buffer_age,
1995 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1996 .get_sync_values = dri2_fallback_get_sync_values,
1997 .get_dri_drawable = dri2_surface_get_dri_drawable,
1998 };
1999
2000 static const __DRIswrastLoaderExtension swrast_loader_extension = {
2001 .base = { __DRI_SWRAST_LOADER, 2 },
2002
2003 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
2004 .putImage = dri2_wl_swrast_put_image,
2005 .getImage = dri2_wl_swrast_get_image,
2006 .putImage2 = dri2_wl_swrast_put_image2,
2007 };
2008
2009 static const __DRIextension *swrast_loader_extensions[] = {
2010 &swrast_loader_extension.base,
2011 &image_lookup_extension.base,
2012 NULL,
2013 };
2014
2015 static EGLBoolean
2016 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
2017 {
2018 struct dri2_egl_display *dri2_dpy;
2019
2020 loader_set_logger(_eglLog);
2021
2022 dri2_dpy = calloc(1, sizeof *dri2_dpy);
2023 if (!dri2_dpy)
2024 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
2025
2026 dri2_dpy->fd = -1;
2027 disp->DriverData = (void *) dri2_dpy;
2028 if (disp->PlatformDisplay == NULL) {
2029 dri2_dpy->wl_dpy = wl_display_connect(NULL);
2030 if (dri2_dpy->wl_dpy == NULL)
2031 goto cleanup;
2032 dri2_dpy->own_device = true;
2033 } else {
2034 dri2_dpy->wl_dpy = disp->PlatformDisplay;
2035 }
2036
2037 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
2038
2039 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
2040 if (dri2_dpy->wl_dpy_wrapper == NULL)
2041 goto cleanup;
2042
2043 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
2044 dri2_dpy->wl_queue);
2045
2046 if (dri2_dpy->own_device)
2047 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
2048
2049 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
2050 wl_registry_add_listener(dri2_dpy->wl_registry,
2051 &registry_listener_swrast, dri2_dpy);
2052
2053 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
2054 goto cleanup;
2055
2056 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
2057 goto cleanup;
2058
2059 dri2_dpy->driver_name = strdup("swrast");
2060 if (!dri2_load_driver_swrast(disp))
2061 goto cleanup;
2062
2063 dri2_dpy->loader_extensions = swrast_loader_extensions;
2064
2065 if (!dri2_create_screen(disp))
2066 goto cleanup;
2067
2068 if (!dri2_setup_extensions(disp))
2069 goto cleanup;
2070
2071 dri2_setup_screen(disp);
2072
2073 dri2_wl_setup_swap_interval(disp);
2074
2075 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
2076 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
2077 goto cleanup;
2078 }
2079
2080 /* Fill vtbl last to prevent accidentally calling virtual function during
2081 * initialization.
2082 */
2083 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
2084
2085 return EGL_TRUE;
2086
2087 cleanup:
2088 dri2_display_destroy(disp);
2089 return EGL_FALSE;
2090 }
2091
2092 EGLBoolean
2093 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
2094 {
2095 EGLBoolean initialized = EGL_FALSE;
2096
2097 if (!disp->Options.ForceSoftware)
2098 initialized = dri2_initialize_wayland_drm(drv, disp);
2099
2100 if (!initialized)
2101 initialized = dri2_initialize_wayland_swrast(drv, disp);
2102
2103 return initialized;
2104
2105 }
2106
2107 void
2108 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy)
2109 {
2110 if (dri2_dpy->wl_drm)
2111 wl_drm_destroy(dri2_dpy->wl_drm);
2112 if (dri2_dpy->wl_dmabuf)
2113 zwp_linux_dmabuf_v1_destroy(dri2_dpy->wl_dmabuf);
2114 if (dri2_dpy->wl_shm)
2115 wl_shm_destroy(dri2_dpy->wl_shm);
2116 if (dri2_dpy->wl_registry)
2117 wl_registry_destroy(dri2_dpy->wl_registry);
2118 if (dri2_dpy->wl_queue)
2119 wl_event_queue_destroy(dri2_dpy->wl_queue);
2120 if (dri2_dpy->wl_dpy_wrapper)
2121 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
2122 u_vector_finish(&dri2_dpy->wl_modifiers.argb2101010);
2123 u_vector_finish(&dri2_dpy->wl_modifiers.xrgb2101010);
2124 u_vector_finish(&dri2_dpy->wl_modifiers.argb8888);
2125 u_vector_finish(&dri2_dpy->wl_modifiers.xrgb8888);
2126 u_vector_finish(&dri2_dpy->wl_modifiers.rgb565);
2127 if (dri2_dpy->own_device)
2128 wl_display_disconnect(dri2_dpy->wl_dpy);
2129 }