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