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