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