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