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