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