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