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