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