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