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