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