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