egl/wayland: group wl_win specific code together
[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 break;
1061 case WL_DRM_FORMAT_XRGB8888:
1062 mod = u_vector_add(&dri2_dpy->wl_modifiers.xrgb8888);
1063 break;
1064 case WL_DRM_FORMAT_RGB565:
1065 mod = u_vector_add(&dri2_dpy->wl_modifiers.rgb565);
1066 break;
1067 default:
1068 break;
1069 }
1070
1071 if (!mod)
1072 return;
1073
1074 *mod = (uint64_t) modifier_hi << 32;
1075 *mod |= (uint64_t) (modifier_lo & 0xffffffff);
1076 }
1077
1078 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
1079 .format = dmabuf_ignore_format,
1080 .modifier = dmabuf_handle_modifier,
1081 };
1082
1083 static void
1084 registry_handle_global_drm(void *data, struct wl_registry *registry,
1085 uint32_t name, const char *interface,
1086 uint32_t version)
1087 {
1088 struct dri2_egl_display *dri2_dpy = data;
1089
1090 if (strcmp(interface, "wl_drm") == 0) {
1091 dri2_dpy->wl_drm =
1092 wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));
1093 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
1094 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) {
1095 dri2_dpy->wl_dmabuf =
1096 wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface,
1097 MIN2(version, 3));
1098 zwp_linux_dmabuf_v1_add_listener(dri2_dpy->wl_dmabuf, &dmabuf_listener,
1099 dri2_dpy);
1100 }
1101 }
1102
1103 static void
1104 registry_handle_global_remove(void *data, struct wl_registry *registry,
1105 uint32_t name)
1106 {
1107 }
1108
1109 static const struct wl_registry_listener registry_listener_drm = {
1110 .global = registry_handle_global_drm,
1111 .global_remove = registry_handle_global_remove
1112 };
1113
1114 static void
1115 dri2_wl_setup_swap_interval(_EGLDisplay *disp)
1116 {
1117 /* We can't use values greater than 1 on Wayland because we are using the
1118 * frame callback to synchronise the frame and the only way we be sure to
1119 * get a frame callback is to attach a new buffer. Therefore we can't just
1120 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1121
1122 dri2_setup_swap_interval(disp, 1);
1123 }
1124
1125 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1126 .authenticate = dri2_wl_authenticate,
1127 .create_window_surface = dri2_wl_create_window_surface,
1128 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1129 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1130 .destroy_surface = dri2_wl_destroy_surface,
1131 .create_image = dri2_create_image_khr,
1132 .swap_buffers = dri2_wl_swap_buffers,
1133 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1134 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1135 .set_damage_region = dri2_fallback_set_damage_region,
1136 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1137 .copy_buffers = dri2_fallback_copy_buffers,
1138 .query_buffer_age = dri2_wl_query_buffer_age,
1139 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1140 .get_sync_values = dri2_fallback_get_sync_values,
1141 .get_dri_drawable = dri2_surface_get_dri_drawable,
1142 };
1143
1144 static const __DRIextension *dri2_loader_extensions[] = {
1145 &dri2_loader_extension.base,
1146 &image_loader_extension.base,
1147 &image_lookup_extension.base,
1148 &use_invalidate.base,
1149 NULL,
1150 };
1151
1152 static const __DRIextension *image_loader_extensions[] = {
1153 &image_loader_extension.base,
1154 &image_lookup_extension.base,
1155 &use_invalidate.base,
1156 NULL,
1157 };
1158
1159 static EGLBoolean
1160 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1161 {
1162 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1163 static const struct {
1164 const char *format_name;
1165 int has_format;
1166 unsigned int rgba_masks[4];
1167 } visuals[] = {
1168 { "XRGB8888", HAS_XRGB8888, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1169 { "ARGB8888", HAS_ARGB8888, { 0xff0000, 0xff00, 0x00ff, 0 } },
1170 { "RGB565", HAS_RGB565, { 0x00f800, 0x07e0, 0x001f, 0 } },
1171 };
1172 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1173 unsigned int count = 0;
1174
1175 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
1176 for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
1177 struct dri2_egl_config *dri2_conf;
1178
1179 if (!(dri2_dpy->formats & visuals[j].has_format))
1180 continue;
1181
1182 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1183 count + 1, EGL_WINDOW_BIT, NULL, visuals[j].rgba_masks);
1184 if (dri2_conf) {
1185 if (dri2_conf->base.ConfigID == count + 1)
1186 count++;
1187 format_count[j]++;
1188 }
1189 }
1190 }
1191
1192 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
1193 if (!format_count[i]) {
1194 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1195 visuals[i].format_name);
1196 }
1197 }
1198
1199 return (count != 0);
1200 }
1201
1202 static EGLBoolean
1203 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1204 {
1205 struct dri2_egl_display *dri2_dpy;
1206
1207 loader_set_logger(_eglLog);
1208
1209 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1210 if (!dri2_dpy)
1211 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1212
1213 dri2_dpy->fd = -1;
1214 disp->DriverData = (void *) dri2_dpy;
1215 if (disp->PlatformDisplay == NULL) {
1216 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1217 if (dri2_dpy->wl_dpy == NULL)
1218 goto cleanup;
1219 dri2_dpy->own_device = true;
1220 } else {
1221 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1222 }
1223
1224 if (!u_vector_init(&dri2_dpy->wl_modifiers.xrgb8888, sizeof(uint64_t), 32) ||
1225 !u_vector_init(&dri2_dpy->wl_modifiers.argb8888, sizeof(uint64_t), 32) ||
1226 !u_vector_init(&dri2_dpy->wl_modifiers.rgb565, sizeof(uint64_t), 32)) {
1227 goto cleanup;
1228 }
1229
1230 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1231
1232 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1233 if (dri2_dpy->wl_dpy_wrapper == NULL)
1234 goto cleanup;
1235
1236 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1237 dri2_dpy->wl_queue);
1238
1239 if (dri2_dpy->own_device)
1240 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1241
1242 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1243 wl_registry_add_listener(dri2_dpy->wl_registry,
1244 &registry_listener_drm, dri2_dpy);
1245 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1246 goto cleanup;
1247
1248 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1249 goto cleanup;
1250
1251 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1252 goto cleanup;
1253
1254 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1255 &dri2_dpy->is_different_gpu);
1256 if (dri2_dpy->is_different_gpu) {
1257 free(dri2_dpy->device_name);
1258 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1259 if (!dri2_dpy->device_name) {
1260 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1261 "for requested GPU");
1262 goto cleanup;
1263 }
1264 }
1265
1266 /* we have to do the check now, because loader_get_user_preferred_fd
1267 * will return a render-node when the requested gpu is different
1268 * to the server, but also if the client asks for the same gpu than
1269 * the server by requesting its pci-id */
1270 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1271
1272 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1273 if (dri2_dpy->driver_name == NULL) {
1274 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1275 goto cleanup;
1276 }
1277
1278 /* render nodes cannot use Gem names, and thus do not support
1279 * the __DRI_DRI2_LOADER extension */
1280 if (!dri2_dpy->is_render_node) {
1281 dri2_dpy->loader_extensions = dri2_loader_extensions;
1282 if (!dri2_load_driver(disp)) {
1283 _eglError(EGL_BAD_ALLOC, "DRI2: failed to load driver");
1284 goto cleanup;
1285 }
1286 } else {
1287 dri2_dpy->loader_extensions = image_loader_extensions;
1288 if (!dri2_load_driver_dri3(disp)) {
1289 _eglError(EGL_BAD_ALLOC, "DRI3: failed to load driver");
1290 goto cleanup;
1291 }
1292 }
1293
1294 if (!dri2_create_screen(disp))
1295 goto cleanup;
1296
1297 if (!dri2_setup_extensions(disp))
1298 goto cleanup;
1299
1300 dri2_setup_screen(disp);
1301
1302 dri2_wl_setup_swap_interval(disp);
1303
1304 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1305 * createImageFromFds support indicates that Prime export/import
1306 * is supported by the driver. Fall back to
1307 * gem names if we don't have Prime support. */
1308
1309 if (dri2_dpy->image->base.version < 7 ||
1310 dri2_dpy->image->createImageFromFds == NULL)
1311 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1312
1313 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1314 * The server needs to accept them */
1315 if (dri2_dpy->is_render_node &&
1316 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1317 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1318 goto cleanup;
1319 }
1320
1321 if (dri2_dpy->is_different_gpu &&
1322 (dri2_dpy->image->base.version < 9 ||
1323 dri2_dpy->image->blitImage == NULL)) {
1324 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1325 "Image extension in the driver is not "
1326 "compatible. Version 9 or later and blitImage() "
1327 "are required");
1328 goto cleanup;
1329 }
1330
1331 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1332 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1333 goto cleanup;
1334 }
1335
1336 dri2_set_WL_bind_wayland_display(drv, disp);
1337 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1338 * because the buffer of the EGLImage has likely a tiling mode the server
1339 * gpu won't support. These is no way to check for now. Thus do not support the
1340 * extension */
1341 if (!dri2_dpy->is_different_gpu)
1342 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1343
1344 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1345
1346 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1347
1348 /* Fill vtbl last to prevent accidentally calling virtual function during
1349 * initialization.
1350 */
1351 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1352
1353 return EGL_TRUE;
1354
1355 cleanup:
1356 dri2_display_destroy(disp);
1357 return EGL_FALSE;
1358 }
1359
1360 static int
1361 dri2_wl_swrast_get_stride_for_format(int format, int w)
1362 {
1363 if (format == WL_SHM_FORMAT_RGB565)
1364 return 2 * w;
1365 else /* ARGB8888 || XRGB8888 */
1366 return 4 * w;
1367 }
1368
1369 /*
1370 * Taken from weston shared/os-compatibility.c
1371 */
1372
1373 #ifndef HAVE_MKOSTEMP
1374
1375 static int
1376 set_cloexec_or_close(int fd)
1377 {
1378 long flags;
1379
1380 if (fd == -1)
1381 return -1;
1382
1383 flags = fcntl(fd, F_GETFD);
1384 if (flags == -1)
1385 goto err;
1386
1387 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1388 goto err;
1389
1390 return fd;
1391
1392 err:
1393 close(fd);
1394 return -1;
1395 }
1396
1397 #endif
1398
1399 /*
1400 * Taken from weston shared/os-compatibility.c
1401 */
1402
1403 static int
1404 create_tmpfile_cloexec(char *tmpname)
1405 {
1406 int fd;
1407
1408 #ifdef HAVE_MKOSTEMP
1409 fd = mkostemp(tmpname, O_CLOEXEC);
1410 if (fd >= 0)
1411 unlink(tmpname);
1412 #else
1413 fd = mkstemp(tmpname);
1414 if (fd >= 0) {
1415 fd = set_cloexec_or_close(fd);
1416 unlink(tmpname);
1417 }
1418 #endif
1419
1420 return fd;
1421 }
1422
1423 /*
1424 * Taken from weston shared/os-compatibility.c
1425 *
1426 * Create a new, unique, anonymous file of the given size, and
1427 * return the file descriptor for it. The file descriptor is set
1428 * CLOEXEC. The file is immediately suitable for mmap()'ing
1429 * the given size at offset zero.
1430 *
1431 * The file should not have a permanent backing store like a disk,
1432 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1433 *
1434 * The file name is deleted from the file system.
1435 *
1436 * The file is suitable for buffer sharing between processes by
1437 * transmitting the file descriptor over Unix sockets using the
1438 * SCM_RIGHTS methods.
1439 *
1440 * If the C library implements posix_fallocate(), it is used to
1441 * guarantee that disk space is available for the file at the
1442 * given size. If disk space is insufficent, errno is set to ENOSPC.
1443 * If posix_fallocate() is not supported, program may receive
1444 * SIGBUS on accessing mmap()'ed file contents instead.
1445 */
1446 static int
1447 os_create_anonymous_file(off_t size)
1448 {
1449 static const char template[] = "/mesa-shared-XXXXXX";
1450 const char *path;
1451 char *name;
1452 int fd;
1453 int ret;
1454
1455 path = getenv("XDG_RUNTIME_DIR");
1456 if (!path) {
1457 errno = ENOENT;
1458 return -1;
1459 }
1460
1461 name = malloc(strlen(path) + sizeof(template));
1462 if (!name)
1463 return -1;
1464
1465 strcpy(name, path);
1466 strcat(name, template);
1467
1468 fd = create_tmpfile_cloexec(name);
1469
1470 free(name);
1471
1472 if (fd < 0)
1473 return -1;
1474
1475 ret = ftruncate(fd, size);
1476 if (ret < 0) {
1477 close(fd);
1478 return -1;
1479 }
1480
1481 return fd;
1482 }
1483
1484
1485 static EGLBoolean
1486 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1487 int format, int w, int h,
1488 void **data, int *size,
1489 struct wl_buffer **buffer)
1490 {
1491 struct dri2_egl_display *dri2_dpy =
1492 dri2_egl_display(dri2_surf->base.Resource.Display);
1493 struct wl_shm_pool *pool;
1494 int fd, stride, size_map;
1495 void *data_map;
1496
1497 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1498 size_map = h * stride;
1499
1500 /* Create a sharable buffer */
1501 fd = os_create_anonymous_file(size_map);
1502 if (fd < 0)
1503 return EGL_FALSE;
1504
1505 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1506 if (data_map == MAP_FAILED) {
1507 close(fd);
1508 return EGL_FALSE;
1509 }
1510
1511 /* Share it in a wl_buffer */
1512 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1513 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1514 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1515 wl_shm_pool_destroy(pool);
1516 close(fd);
1517
1518 *data = data_map;
1519 *size = size_map;
1520 return EGL_TRUE;
1521 }
1522
1523 static int
1524 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1525 {
1526 struct dri2_egl_display *dri2_dpy =
1527 dri2_egl_display(dri2_surf->base.Resource.Display);
1528
1529 /* we need to do the following operations only once per frame */
1530 if (dri2_surf->back)
1531 return 0;
1532
1533 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1534 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1535
1536 dri2_wl_release_buffers(dri2_surf);
1537
1538 dri2_surf->base.Width = dri2_surf->wl_win->width;
1539 dri2_surf->base.Height = dri2_surf->wl_win->height;
1540 dri2_surf->dx = dri2_surf->wl_win->dx;
1541 dri2_surf->dy = dri2_surf->wl_win->dy;
1542 dri2_surf->current = NULL;
1543 }
1544
1545 /* find back buffer */
1546
1547 /* There might be a buffer release already queued that wasn't processed */
1548 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1549
1550 /* try get free buffer already created */
1551 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1552 if (!dri2_surf->color_buffers[i].locked &&
1553 dri2_surf->color_buffers[i].wl_buffer) {
1554 dri2_surf->back = &dri2_surf->color_buffers[i];
1555 break;
1556 }
1557 }
1558
1559 /* else choose any another free location */
1560 if (!dri2_surf->back) {
1561 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1562 if (!dri2_surf->color_buffers[i].locked) {
1563 dri2_surf->back = &dri2_surf->color_buffers[i];
1564 if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1565 dri2_surf->format,
1566 dri2_surf->base.Width,
1567 dri2_surf->base.Height,
1568 &dri2_surf->back->data,
1569 &dri2_surf->back->data_size,
1570 &dri2_surf->back->wl_buffer)) {
1571 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1572 return -1;
1573 }
1574 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1575 &wl_buffer_listener, dri2_surf);
1576 break;
1577 }
1578 }
1579 }
1580
1581 if (!dri2_surf->back) {
1582 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1583 return -1;
1584 }
1585
1586 dri2_surf->back->locked = true;
1587
1588 /* If we have an extra unlocked buffer at this point, we had to do triple
1589 * buffering for a while, but now can go back to just double buffering.
1590 * That means we can free any unlocked buffer now. */
1591 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1592 if (!dri2_surf->color_buffers[i].locked &&
1593 dri2_surf->color_buffers[i].wl_buffer) {
1594 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1595 munmap(dri2_surf->color_buffers[i].data,
1596 dri2_surf->color_buffers[i].data_size);
1597 dri2_surf->color_buffers[i].wl_buffer = NULL;
1598 dri2_surf->color_buffers[i].data = NULL;
1599 }
1600 }
1601
1602 return 0;
1603 }
1604
1605 static void*
1606 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1607 {
1608 /* if there has been a resize: */
1609 if (!dri2_surf->current)
1610 return NULL;
1611
1612 return dri2_surf->current->data;
1613 }
1614
1615 static void*
1616 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1617 {
1618 assert(dri2_surf->back);
1619 return dri2_surf->back->data;
1620 }
1621
1622 static void
1623 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1624 {
1625 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1626
1627 while (dri2_surf->throttle_callback != NULL)
1628 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1629 dri2_surf->wl_queue) == -1)
1630 return;
1631
1632 if (dri2_surf->base.SwapInterval > 0) {
1633 dri2_surf->throttle_callback =
1634 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1635 wl_callback_add_listener(dri2_surf->throttle_callback,
1636 &throttle_listener, dri2_surf);
1637 }
1638
1639 dri2_surf->current = dri2_surf->back;
1640 dri2_surf->back = NULL;
1641
1642 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1643 dri2_surf->current->wl_buffer,
1644 dri2_surf->dx, dri2_surf->dy);
1645
1646 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1647 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1648 /* reset resize growing parameters */
1649 dri2_surf->dx = 0;
1650 dri2_surf->dy = 0;
1651
1652 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1653 0, 0, INT32_MAX, INT32_MAX);
1654 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1655
1656 /* If we're not waiting for a frame callback then we'll at least throttle
1657 * to a sync callback so that we always give a chance for the compositor to
1658 * handle the commit and send a release event before checking for a free
1659 * buffer */
1660 if (dri2_surf->throttle_callback == NULL) {
1661 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy_wrapper);
1662 wl_callback_add_listener(dri2_surf->throttle_callback,
1663 &throttle_listener, dri2_surf);
1664 }
1665
1666 wl_display_flush(dri2_dpy->wl_dpy);
1667 }
1668
1669 static void
1670 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1671 int *x, int *y, int *w, int *h,
1672 void *loaderPrivate)
1673 {
1674 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1675
1676 (void) swrast_update_buffers(dri2_surf);
1677 *x = 0;
1678 *y = 0;
1679 *w = dri2_surf->base.Width;
1680 *h = dri2_surf->base.Height;
1681 }
1682
1683 static void
1684 dri2_wl_swrast_get_image(__DRIdrawable * read,
1685 int x, int y, int w, int h,
1686 char *data, void *loaderPrivate)
1687 {
1688 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1689 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1690 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1691 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1692 int dst_stride = copy_width;
1693 char *src, *dst;
1694
1695 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1696 if (!src) {
1697 memset(data, 0, copy_width * h);
1698 return;
1699 }
1700
1701 assert(data != src);
1702 assert(copy_width <= src_stride);
1703
1704 src += x_offset;
1705 src += y * src_stride;
1706 dst = data;
1707
1708 if (copy_width > src_stride-x_offset)
1709 copy_width = src_stride-x_offset;
1710 if (h > dri2_surf->base.Height-y)
1711 h = dri2_surf->base.Height-y;
1712
1713 for (; h>0; h--) {
1714 memcpy(dst, src, copy_width);
1715 src += src_stride;
1716 dst += dst_stride;
1717 }
1718 }
1719
1720 static void
1721 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1722 int x, int y, int w, int h, int stride,
1723 char *data, void *loaderPrivate)
1724 {
1725 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1726 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1727 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1728 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1729 char *src, *dst;
1730
1731 assert(copy_width <= stride);
1732
1733 (void) swrast_update_buffers(dri2_surf);
1734 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1735
1736 /* partial copy, copy old content */
1737 if (copy_width < dst_stride)
1738 dri2_wl_swrast_get_image(draw, 0, 0,
1739 dri2_surf->base.Width, dri2_surf->base.Height,
1740 dst, loaderPrivate);
1741
1742 dst += x_offset;
1743 dst += y * dst_stride;
1744
1745 src = data;
1746
1747 /* drivers expect we do these checks (and some rely on it) */
1748 if (copy_width > dst_stride-x_offset)
1749 copy_width = dst_stride-x_offset;
1750 if (h > dri2_surf->base.Height-y)
1751 h = dri2_surf->base.Height-y;
1752
1753 for (; h>0; h--) {
1754 memcpy(dst, src, copy_width);
1755 src += stride;
1756 dst += dst_stride;
1757 }
1758 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1759 }
1760
1761 static void
1762 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1763 int x, int y, int w, int h,
1764 char *data, void *loaderPrivate)
1765 {
1766 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1767 int stride;
1768
1769 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1770 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1771 stride, data, loaderPrivate);
1772 }
1773
1774 static EGLBoolean
1775 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1776 {
1777 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1778 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1779
1780 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1781 return EGL_TRUE;
1782 }
1783
1784 static void
1785 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1786 {
1787 struct dri2_egl_display *dri2_dpy = data;
1788
1789 switch (format) {
1790 case WL_SHM_FORMAT_ARGB8888:
1791 dri2_dpy->formats |= HAS_ARGB8888;
1792 break;
1793 case WL_SHM_FORMAT_XRGB8888:
1794 dri2_dpy->formats |= HAS_XRGB8888;
1795 break;
1796 case WL_SHM_FORMAT_RGB565:
1797 dri2_dpy->formats |= HAS_RGB565;
1798 break;
1799 }
1800 }
1801
1802 static const struct wl_shm_listener shm_listener = {
1803 .format = shm_handle_format
1804 };
1805
1806 static void
1807 registry_handle_global_swrast(void *data, struct wl_registry *registry,
1808 uint32_t name, const char *interface,
1809 uint32_t version)
1810 {
1811 struct dri2_egl_display *dri2_dpy = data;
1812
1813 if (strcmp(interface, "wl_shm") == 0) {
1814 dri2_dpy->wl_shm =
1815 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1816 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1817 }
1818 }
1819
1820 static const struct wl_registry_listener registry_listener_swrast = {
1821 .global = registry_handle_global_swrast,
1822 .global_remove = registry_handle_global_remove
1823 };
1824
1825 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1826 .authenticate = NULL,
1827 .create_window_surface = dri2_wl_create_window_surface,
1828 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1829 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1830 .destroy_surface = dri2_wl_destroy_surface,
1831 .create_image = dri2_create_image_khr,
1832 .swap_buffers = dri2_wl_swrast_swap_buffers,
1833 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1834 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1835 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1836 .copy_buffers = dri2_fallback_copy_buffers,
1837 .query_buffer_age = dri2_fallback_query_buffer_age,
1838 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1839 .get_sync_values = dri2_fallback_get_sync_values,
1840 .get_dri_drawable = dri2_surface_get_dri_drawable,
1841 };
1842
1843 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1844 .base = { __DRI_SWRAST_LOADER, 2 },
1845
1846 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
1847 .putImage = dri2_wl_swrast_put_image,
1848 .getImage = dri2_wl_swrast_get_image,
1849 .putImage2 = dri2_wl_swrast_put_image2,
1850 };
1851
1852 static const __DRIextension *swrast_loader_extensions[] = {
1853 &swrast_loader_extension.base,
1854 &image_lookup_extension.base,
1855 NULL,
1856 };
1857
1858 static EGLBoolean
1859 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1860 {
1861 struct dri2_egl_display *dri2_dpy;
1862
1863 loader_set_logger(_eglLog);
1864
1865 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1866 if (!dri2_dpy)
1867 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1868
1869 dri2_dpy->fd = -1;
1870 disp->DriverData = (void *) dri2_dpy;
1871 if (disp->PlatformDisplay == NULL) {
1872 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1873 if (dri2_dpy->wl_dpy == NULL)
1874 goto cleanup;
1875 dri2_dpy->own_device = true;
1876 } else {
1877 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1878 }
1879
1880 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1881
1882 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1883 if (dri2_dpy->wl_dpy_wrapper == NULL)
1884 goto cleanup;
1885
1886 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1887 dri2_dpy->wl_queue);
1888
1889 if (dri2_dpy->own_device)
1890 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1891
1892 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1893 wl_registry_add_listener(dri2_dpy->wl_registry,
1894 &registry_listener_swrast, dri2_dpy);
1895
1896 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
1897 goto cleanup;
1898
1899 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
1900 goto cleanup;
1901
1902 dri2_dpy->driver_name = strdup("swrast");
1903 if (!dri2_load_driver_swrast(disp))
1904 goto cleanup;
1905
1906 dri2_dpy->loader_extensions = swrast_loader_extensions;
1907
1908 if (!dri2_create_screen(disp))
1909 goto cleanup;
1910
1911 if (!dri2_setup_extensions(disp))
1912 goto cleanup;
1913
1914 dri2_setup_screen(disp);
1915
1916 dri2_wl_setup_swap_interval(disp);
1917
1918 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1919 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1920 goto cleanup;
1921 }
1922
1923 /* Fill vtbl last to prevent accidentally calling virtual function during
1924 * initialization.
1925 */
1926 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
1927
1928 return EGL_TRUE;
1929
1930 cleanup:
1931 dri2_display_destroy(disp);
1932 return EGL_FALSE;
1933 }
1934
1935 EGLBoolean
1936 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
1937 {
1938 EGLBoolean initialized = EGL_TRUE;
1939
1940 int hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1941
1942 if (hw_accel) {
1943 if (!dri2_initialize_wayland_drm(drv, disp)) {
1944 initialized = dri2_initialize_wayland_swrast(drv, disp);
1945 }
1946 } else {
1947 initialized = dri2_initialize_wayland_swrast(drv, disp);
1948 }
1949
1950 return initialized;
1951
1952 }