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