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