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