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