egl_dri2: Remove depth argument from dri2_add_config()
[mesa.git] / src / egl / drivers / dri2 / platform_wayland.c
1 /*
2 * Copyright © 2011-2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <xf86drm.h>
37
38 #include "egl_dri2.h"
39
40 #include <wayland-client.h>
41 #include "wayland-drm-client-protocol.h"
42
43 enum wl_drm_format_flags {
44 HAS_ARGB8888 = 1,
45 HAS_XRGB8888 = 2,
46 HAS_RGB565 = 4,
47 };
48
49 static void
50 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
51 {
52 int *done = data;
53
54 *done = 1;
55 wl_callback_destroy(callback);
56 }
57
58 static const struct wl_callback_listener sync_listener = {
59 sync_callback
60 };
61
62 static int
63 roundtrip(struct dri2_egl_display *dri2_dpy)
64 {
65 struct wl_callback *callback;
66 int done = 0, ret = 0;
67
68 callback = wl_display_sync(dri2_dpy->wl_dpy);
69 wl_callback_add_listener(callback, &sync_listener, &done);
70 wl_proxy_set_queue((struct wl_proxy *) callback, dri2_dpy->wl_queue);
71 while (ret != -1 && !done)
72 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
73
74 if (!done)
75 wl_callback_destroy(callback);
76
77 return ret;
78 }
79
80 static void
81 wl_buffer_release(void *data, struct wl_buffer *buffer)
82 {
83 struct dri2_egl_surface *dri2_surf = data;
84 int i;
85
86 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
87 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
88 break;
89
90 if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
91 wl_buffer_destroy(buffer);
92 return;
93 }
94
95 dri2_surf->color_buffers[i].locked = 0;
96 }
97
98 static struct wl_buffer_listener wl_buffer_listener = {
99 wl_buffer_release
100 };
101
102 static void
103 resize_callback(struct wl_egl_window *wl_win, void *data)
104 {
105 struct dri2_egl_surface *dri2_surf = data;
106 struct dri2_egl_display *dri2_dpy =
107 dri2_egl_display(dri2_surf->base.Resource.Display);
108
109 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
110 }
111
112 /**
113 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
114 */
115 static _EGLSurface *
116 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
117 _EGLConfig *conf, EGLNativeWindowType window,
118 const EGLint *attrib_list)
119 {
120 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
121 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
122 struct dri2_egl_surface *dri2_surf;
123
124 (void) drv;
125
126 dri2_surf = malloc(sizeof *dri2_surf);
127 if (!dri2_surf) {
128 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
129 return NULL;
130 }
131
132 memset(dri2_surf, 0, sizeof *dri2_surf);
133 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
134 goto cleanup_surf;
135
136 if (conf->RedSize == 5)
137 dri2_surf->format = WL_DRM_FORMAT_RGB565;
138 else if (conf->AlphaSize == 0)
139 dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
140 else
141 dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
142
143 switch (type) {
144 case EGL_WINDOW_BIT:
145 dri2_surf->wl_win = (struct wl_egl_window *) window;
146
147 dri2_surf->wl_win->private = dri2_surf;
148 dri2_surf->wl_win->resize_callback = resize_callback;
149
150 dri2_surf->base.Width = -1;
151 dri2_surf->base.Height = -1;
152 break;
153 default:
154 goto cleanup_surf;
155 }
156
157 dri2_surf->dri_drawable =
158 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
159 type == EGL_WINDOW_BIT ?
160 dri2_conf->dri_double_config :
161 dri2_conf->dri_single_config,
162 dri2_surf);
163 if (dri2_surf->dri_drawable == NULL) {
164 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
165 goto cleanup_dri_drawable;
166 }
167
168 return &dri2_surf->base;
169
170 cleanup_dri_drawable:
171 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
172 cleanup_surf:
173 free(dri2_surf);
174
175 return NULL;
176 }
177
178 /**
179 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
180 */
181 static _EGLSurface *
182 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
183 _EGLConfig *conf, EGLNativeWindowType window,
184 const EGLint *attrib_list)
185 {
186 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
187 window, attrib_list);
188 }
189
190 /**
191 * Called via eglDestroySurface(), drv->API.DestroySurface().
192 */
193 static EGLBoolean
194 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
195 {
196 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
197 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
198 int i;
199
200 (void) drv;
201
202 if (!_eglPutSurface(surf))
203 return EGL_TRUE;
204
205 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
206
207 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
208 if (dri2_surf->color_buffers[i].wl_buffer)
209 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
210 if (dri2_surf->color_buffers[i].dri_image)
211 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
212 }
213
214 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
215 if (dri2_surf->dri_buffers[i] &&
216 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
217 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
218 dri2_surf->dri_buffers[i]);
219
220 if (dri2_surf->frame_callback)
221 wl_callback_destroy(dri2_surf->frame_callback);
222
223 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
224 dri2_surf->wl_win->private = NULL;
225 dri2_surf->wl_win->resize_callback = NULL;
226 }
227
228 free(surf);
229
230 return EGL_TRUE;
231 }
232
233 static void
234 dri2_release_buffers(struct dri2_egl_surface *dri2_surf)
235 {
236 struct dri2_egl_display *dri2_dpy =
237 dri2_egl_display(dri2_surf->base.Resource.Display);
238 int i;
239
240 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
241 if (dri2_surf->color_buffers[i].wl_buffer &&
242 !dri2_surf->color_buffers[i].locked)
243 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
244 if (dri2_surf->color_buffers[i].dri_image)
245 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
246
247 dri2_surf->color_buffers[i].wl_buffer = NULL;
248 dri2_surf->color_buffers[i].dri_image = NULL;
249 dri2_surf->color_buffers[i].locked = 0;
250 }
251
252 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
253 if (dri2_surf->dri_buffers[i] &&
254 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
255 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
256 dri2_surf->dri_buffers[i]);
257 }
258
259 static int
260 get_back_bo(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
261 {
262 struct dri2_egl_display *dri2_dpy =
263 dri2_egl_display(dri2_surf->base.Resource.Display);
264 __DRIimage *image;
265 int i, name, pitch;
266
267 /* There might be a buffer release already queued that wasn't processed */
268 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
269
270 if (dri2_surf->back == NULL) {
271 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
272 /* Get an unlocked buffer, preferrably one with a dri_buffer already
273 * allocated. */
274 if (dri2_surf->color_buffers[i].locked)
275 continue;
276 if (dri2_surf->back == NULL)
277 dri2_surf->back = &dri2_surf->color_buffers[i];
278 else if (dri2_surf->back->dri_image == NULL)
279 dri2_surf->back = &dri2_surf->color_buffers[i];
280 }
281 }
282
283 if (dri2_surf->back == NULL)
284 return -1;
285 if (dri2_surf->back->dri_image == NULL) {
286 dri2_surf->back->dri_image =
287 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
288 dri2_surf->base.Width,
289 dri2_surf->base.Height,
290 __DRI_IMAGE_FORMAT_ARGB8888,
291 __DRI_IMAGE_USE_SHARE,
292 NULL);
293 dri2_surf->back->age = 0;
294 }
295 if (dri2_surf->back->dri_image == NULL)
296 return -1;
297
298 image = dri2_surf->back->dri_image;
299
300 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
301 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
302
303 dri2_surf->back->name = name;
304 dri2_surf->back->pitch = pitch;
305
306 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
307 buffer->name = name;
308 buffer->pitch = pitch;
309 buffer->cpp = 4;
310 buffer->flags = 0;
311
312 dri2_surf->back->locked = 1;
313
314 return 0;
315 }
316
317 static int
318 get_aux_bo(struct dri2_egl_surface *dri2_surf,
319 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
320 {
321 struct dri2_egl_display *dri2_dpy =
322 dri2_egl_display(dri2_surf->base.Resource.Display);
323 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
324
325 if (b == NULL) {
326 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
327 attachment, format,
328 dri2_surf->base.Width,
329 dri2_surf->base.Height);
330 dri2_surf->dri_buffers[attachment] = b;
331 }
332 if (b == NULL)
333 return -1;
334
335 memcpy(buffer, b, sizeof *buffer);
336
337 return 0;
338 }
339
340 static __DRIbuffer *
341 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
342 int *width, int *height,
343 unsigned int *attachments, int count,
344 int *out_count, void *loaderPrivate)
345 {
346 struct dri2_egl_surface *dri2_surf = loaderPrivate;
347 struct dri2_egl_display *dri2_dpy =
348 dri2_egl_display(dri2_surf->base.Resource.Display);
349 int i, j;
350
351 if (dri2_surf->base.Type == EGL_WINDOW_BIT &&
352 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
353 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
354
355 dri2_release_buffers(dri2_surf);
356
357 dri2_surf->base.Width = dri2_surf->wl_win->width;
358 dri2_surf->base.Height = dri2_surf->wl_win->height;
359 dri2_surf->dx = dri2_surf->wl_win->dx;
360 dri2_surf->dy = dri2_surf->wl_win->dy;
361 }
362
363 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
364 switch (attachments[i]) {
365 case __DRI_BUFFER_BACK_LEFT:
366 if (get_back_bo(dri2_surf, &dri2_surf->buffers[j]) < 0) {
367 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
368 return NULL;
369 }
370 break;
371 default:
372 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
373 &dri2_surf->buffers[j]) < 0) {
374 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
375 return NULL;
376 }
377 break;
378 }
379 }
380
381 /* If we have an extra unlocked buffer at this point, we had to do triple
382 * buffering for a while, but now can go back to just double buffering.
383 * That means we can free any unlocked buffer now. */
384 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
385 if (!dri2_surf->color_buffers[i].locked &&
386 dri2_surf->color_buffers[i].wl_buffer) {
387 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
388 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
389 dri2_surf->color_buffers[i].wl_buffer = NULL;
390 dri2_surf->color_buffers[i].dri_image = NULL;
391 }
392 }
393
394 *out_count = j;
395 if (j == 0)
396 return NULL;
397
398 *width = dri2_surf->base.Width;
399 *height = dri2_surf->base.Height;
400
401 return dri2_surf->buffers;
402 }
403
404 static __DRIbuffer *
405 dri2_get_buffers(__DRIdrawable * driDrawable,
406 int *width, int *height,
407 unsigned int *attachments, int count,
408 int *out_count, void *loaderPrivate)
409 {
410 unsigned int *attachments_with_format;
411 __DRIbuffer *buffer;
412 const unsigned int format = 32;
413 int i;
414
415 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
416 if (!attachments_with_format) {
417 *out_count = 0;
418 return NULL;
419 }
420
421 for (i = 0; i < count; ++i) {
422 attachments_with_format[2*i] = attachments[i];
423 attachments_with_format[2*i + 1] = format;
424 }
425
426 buffer =
427 dri2_get_buffers_with_format(driDrawable,
428 width, height,
429 attachments_with_format, count,
430 out_count, loaderPrivate);
431
432 free(attachments_with_format);
433
434 return buffer;
435 }
436
437 static void
438 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
439 {
440 (void) driDrawable;
441 (void) loaderPrivate;
442 }
443
444 static void
445 wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time)
446 {
447 struct dri2_egl_surface *dri2_surf = data;
448
449 dri2_surf->frame_callback = NULL;
450 wl_callback_destroy(callback);
451 }
452
453 static const struct wl_callback_listener frame_listener = {
454 wayland_frame_callback
455 };
456
457 static void
458 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
459 {
460 struct dri2_egl_display *dri2_dpy =
461 dri2_egl_display(dri2_surf->base.Resource.Display);
462 int fd;
463
464 if (dri2_surf->current->wl_buffer != NULL)
465 return;
466
467 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
468 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
469 __DRI_IMAGE_ATTRIB_FD, &fd);
470
471 dri2_surf->current->wl_buffer =
472 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
473 fd,
474 dri2_surf->base.Width,
475 dri2_surf->base.Height,
476 dri2_surf->format,
477 0, dri2_surf->current->pitch,
478 0, 0,
479 0, 0);
480 close(fd);
481 } else {
482 dri2_surf->current->wl_buffer =
483 wl_drm_create_buffer(dri2_dpy->wl_drm,
484 dri2_surf->current->name,
485 dri2_surf->base.Width,
486 dri2_surf->base.Height,
487 dri2_surf->current->pitch,
488 dri2_surf->format);
489 }
490
491 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
492 dri2_dpy->wl_queue);
493 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
494 &wl_buffer_listener, dri2_surf);
495 }
496
497 /**
498 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
499 */
500 static EGLBoolean
501 dri2_swap_buffers_with_damage(_EGLDriver *drv,
502 _EGLDisplay *disp,
503 _EGLSurface *draw,
504 const EGLint *rects,
505 EGLint n_rects)
506 {
507 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
508 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
509 __DRIbuffer buffer;
510 int i, ret = 0;
511
512 while (dri2_surf->frame_callback && ret != -1)
513 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
514 if (ret < 0)
515 return EGL_FALSE;
516
517 dri2_surf->frame_callback = wl_surface_frame(dri2_surf->wl_win->surface);
518 wl_callback_add_listener(dri2_surf->frame_callback,
519 &frame_listener, dri2_surf);
520 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->frame_callback,
521 dri2_dpy->wl_queue);
522
523 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
524 if (dri2_surf->color_buffers[i].age > 0)
525 dri2_surf->color_buffers[i].age++;
526
527 /* Make sure we have a back buffer in case we're swapping without ever
528 * rendering. */
529 if (get_back_bo(dri2_surf, &buffer) < 0) {
530 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
531 return EGL_FALSE;
532 }
533
534 dri2_surf->back->age = 1;
535 dri2_surf->current = dri2_surf->back;
536 dri2_surf->back = NULL;
537
538 create_wl_buffer(dri2_surf);
539
540 wl_surface_attach(dri2_surf->wl_win->surface,
541 dri2_surf->current->wl_buffer,
542 dri2_surf->dx, dri2_surf->dy);
543
544 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
545 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
546 /* reset resize growing parameters */
547 dri2_surf->dx = 0;
548 dri2_surf->dy = 0;
549
550 if (n_rects == 0) {
551 wl_surface_damage(dri2_surf->wl_win->surface, 0, 0,
552 dri2_surf->base.Width, dri2_surf->base.Height);
553 } else {
554 for (i = 0; i < n_rects; i++) {
555 const int *rect = &rects[i * 4];
556 wl_surface_damage(dri2_surf->wl_win->surface,
557 rect[0],
558 dri2_surf->base.Height - rect[1] - rect[3],
559 rect[2], rect[3]);
560 }
561 }
562
563 wl_surface_commit(dri2_surf->wl_win->surface);
564
565 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
566 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
567
568 return EGL_TRUE;
569 }
570
571 static EGLint
572 dri2_query_buffer_age(_EGLDriver *drv,
573 _EGLDisplay *disp, _EGLSurface *surface)
574 {
575 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
576 __DRIbuffer buffer;
577
578 if (get_back_bo(dri2_surf, &buffer) < 0) {
579 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
580 return 0;
581 }
582
583 return dri2_surf->back->age;
584 }
585
586 static EGLBoolean
587 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
588 {
589 return dri2_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
590 }
591
592 static int
593 dri2_wayland_authenticate(_EGLDisplay *disp, uint32_t id)
594 {
595 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
596 int ret = 0;
597
598 dri2_dpy->authenticated = 0;
599
600 wl_drm_authenticate(dri2_dpy->wl_drm, id);
601 if (roundtrip(dri2_dpy) < 0)
602 ret = -1;
603
604 if (!dri2_dpy->authenticated)
605 ret = -1;
606
607 /* reset authenticated */
608 dri2_dpy->authenticated = 1;
609
610 return ret;
611 }
612
613 /**
614 * Called via eglTerminate(), drv->API.Terminate().
615 */
616 static EGLBoolean
617 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
618 {
619 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
620
621 _eglReleaseDisplayResources(drv, disp);
622 _eglCleanupDisplay(disp);
623
624 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
625 close(dri2_dpy->fd);
626 dlclose(dri2_dpy->driver);
627 free(dri2_dpy->driver_name);
628 free(dri2_dpy->device_name);
629 wl_drm_destroy(dri2_dpy->wl_drm);
630 if (dri2_dpy->own_device)
631 wl_display_disconnect(dri2_dpy->wl_dpy);
632 free(dri2_dpy);
633 disp->DriverData = NULL;
634
635 return EGL_TRUE;
636 }
637
638 static void
639 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
640 {
641 struct dri2_egl_display *dri2_dpy = data;
642 drm_magic_t magic;
643
644 dri2_dpy->device_name = strdup(device);
645 if (!dri2_dpy->device_name)
646 return;
647
648 #ifdef O_CLOEXEC
649 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
650 if (dri2_dpy->fd == -1 && errno == EINVAL)
651 #endif
652 {
653 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
654 if (dri2_dpy->fd != -1)
655 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
656 FD_CLOEXEC);
657 }
658 if (dri2_dpy->fd == -1) {
659 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
660 dri2_dpy->device_name, strerror(errno));
661 return;
662 }
663
664 drmGetMagic(dri2_dpy->fd, &magic);
665 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
666 }
667
668 static void
669 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
670 {
671 struct dri2_egl_display *dri2_dpy = data;
672
673 switch (format) {
674 case WL_DRM_FORMAT_ARGB8888:
675 dri2_dpy->formats |= HAS_ARGB8888;
676 break;
677 case WL_DRM_FORMAT_XRGB8888:
678 dri2_dpy->formats |= HAS_XRGB8888;
679 break;
680 case WL_DRM_FORMAT_RGB565:
681 dri2_dpy->formats |= HAS_RGB565;
682 break;
683 }
684 }
685
686 static void
687 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
688 {
689 struct dri2_egl_display *dri2_dpy = data;
690
691 dri2_dpy->capabilities = value;
692 }
693
694 static void
695 drm_handle_authenticated(void *data, struct wl_drm *drm)
696 {
697 struct dri2_egl_display *dri2_dpy = data;
698
699 dri2_dpy->authenticated = 1;
700 }
701
702 static const struct wl_drm_listener drm_listener = {
703 drm_handle_device,
704 drm_handle_format,
705 drm_handle_authenticated,
706 drm_handle_capabilities
707 };
708
709 static void
710 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
711 const char *interface, uint32_t version)
712 {
713 struct dri2_egl_display *dri2_dpy = data;
714
715 if (version > 1)
716 version = 2;
717 if (strcmp(interface, "wl_drm") == 0) {
718 dri2_dpy->wl_drm =
719 wl_registry_bind(registry, name, &wl_drm_interface, version);
720 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
721 }
722 }
723
724 static void
725 registry_handle_global_remove(void *data, struct wl_registry *registry,
726 uint32_t name)
727 {
728 }
729
730 static const struct wl_registry_listener registry_listener = {
731 registry_handle_global,
732 registry_handle_global_remove
733 };
734
735 EGLBoolean
736 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
737 {
738 struct dri2_egl_display *dri2_dpy;
739 const __DRIconfig *config;
740 uint32_t types;
741 int i;
742 static const unsigned int argb_masks[4] =
743 { 0xff0000, 0xff00, 0xff, 0xff000000 };
744 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
745 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
746
747 drv->API.CreateWindowSurface = dri2_create_window_surface;
748 drv->API.DestroySurface = dri2_destroy_surface;
749 drv->API.SwapBuffers = dri2_swap_buffers;
750 drv->API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
751 drv->API.Terminate = dri2_terminate;
752 drv->API.QueryBufferAge = dri2_query_buffer_age;
753
754 dri2_dpy = calloc(1, sizeof *dri2_dpy);
755 if (!dri2_dpy)
756 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
757
758 disp->DriverData = (void *) dri2_dpy;
759 if (disp->PlatformDisplay == NULL) {
760 dri2_dpy->wl_dpy = wl_display_connect(NULL);
761 if (dri2_dpy->wl_dpy == NULL)
762 goto cleanup_dpy;
763 dri2_dpy->own_device = 1;
764 } else {
765 dri2_dpy->wl_dpy = disp->PlatformDisplay;
766 }
767
768 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
769
770 if (dri2_dpy->own_device)
771 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
772
773 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
774 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
775 dri2_dpy->wl_queue);
776 wl_registry_add_listener(dri2_dpy->wl_registry,
777 &registry_listener, dri2_dpy);
778 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
779 goto cleanup_dpy;
780
781 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
782 goto cleanup_drm;
783
784 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
785 goto cleanup_fd;
786
787 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
788 if (dri2_dpy->driver_name == NULL) {
789 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
790 goto cleanup_fd;
791 }
792
793 if (!dri2_load_driver(disp))
794 goto cleanup_driver_name;
795
796 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
797 dri2_dpy->dri2_loader_extension.base.version = 3;
798 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
799 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
800 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
801 dri2_get_buffers_with_format;
802
803 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
804 dri2_dpy->extensions[1] = &image_lookup_extension.base;
805 dri2_dpy->extensions[2] = &use_invalidate.base;
806 dri2_dpy->extensions[3] = NULL;
807
808 if (!dri2_create_screen(disp))
809 goto cleanup_driver;
810
811 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
812 * doesn't have createImageFromFds, since we're using the same driver on
813 * both sides. We don't want crash if that happens anyway, so fall back to
814 * gem names if we don't have prime support. */
815
816 if (dri2_dpy->image->base.version < 7 ||
817 dri2_dpy->image->createImageFromFds == NULL)
818 dri2_dpy->capabilities &= WL_DRM_CAPABILITY_PRIME;
819
820 types = EGL_WINDOW_BIT;
821 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
822 config = dri2_dpy->driver_configs[i];
823 if (dri2_dpy->formats & HAS_XRGB8888)
824 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
825 if (dri2_dpy->formats & HAS_ARGB8888)
826 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
827 if (dri2_dpy->formats & HAS_RGB565)
828 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
829 }
830
831 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
832 disp->Extensions.EXT_buffer_age = EGL_TRUE;
833 dri2_dpy->authenticate = dri2_wayland_authenticate;
834
835 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
836
837 /* we're supporting EGL 1.4 */
838 disp->VersionMajor = 1;
839 disp->VersionMinor = 4;
840
841 return EGL_TRUE;
842
843 cleanup_driver:
844 dlclose(dri2_dpy->driver);
845 cleanup_driver_name:
846 free(dri2_dpy->driver_name);
847 cleanup_fd:
848 close(dri2_dpy->fd);
849 cleanup_drm:
850 free(dri2_dpy->device_name);
851 wl_drm_destroy(dri2_dpy->wl_drm);
852 cleanup_dpy:
853 free(dri2_dpy);
854
855 return EGL_FALSE;
856 }