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