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