wayland: Drop support for ill-defined, unused wl_egl_pixmap
[mesa.git] / src / egl / drivers / dri2 / platform_wayland.c
1 /*
2 * Copyright © 2011 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 };
47
48 static void
49 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
50 {
51 int *done = data;
52
53 *done = 1;
54 wl_callback_destroy(callback);
55 }
56
57 static const struct wl_callback_listener sync_listener = {
58 sync_callback
59 };
60
61 static int
62 roundtrip(struct dri2_egl_display *dri2_dpy)
63 {
64 struct wl_callback *callback;
65 int done = 0, ret = 0;
66
67 callback = wl_display_sync(dri2_dpy->wl_dpy);
68 wl_callback_add_listener(callback, &sync_listener, &done);
69 wl_proxy_set_queue((struct wl_proxy *) callback, dri2_dpy->wl_queue);
70 while (ret != -1 && !done)
71 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
72
73 return ret;
74 }
75
76 static void
77 wl_buffer_release(void *data, struct wl_buffer *buffer)
78 {
79 struct dri2_egl_surface *dri2_surf = data;
80 int i;
81
82 for (i = 0; i < WL_BUFFER_COUNT; ++i)
83 if (dri2_surf->wl_drm_buffer[i] == buffer)
84 break;
85
86 assert(i <= WL_BUFFER_COUNT);
87
88 /* not found? */
89 if (i == WL_BUFFER_COUNT)
90 return;
91
92 dri2_surf->wl_buffer_lock[i] = 0;
93
94 }
95
96 static struct wl_buffer_listener wl_buffer_listener = {
97 wl_buffer_release
98 };
99
100 /**
101 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
102 */
103 static _EGLSurface *
104 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
105 _EGLConfig *conf, EGLNativeWindowType window,
106 const EGLint *attrib_list)
107 {
108 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
109 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
110 struct dri2_egl_surface *dri2_surf;
111 int i;
112
113 (void) drv;
114
115 dri2_surf = malloc(sizeof *dri2_surf);
116 if (!dri2_surf) {
117 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
118 return NULL;
119 }
120
121 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
122 goto cleanup_surf;
123
124 for (i = 0; i < WL_BUFFER_COUNT; ++i) {
125 dri2_surf->wl_drm_buffer[i] = NULL;
126 dri2_surf->wl_buffer_lock[i] = 0;
127 }
128
129 for (i = 0; i < __DRI_BUFFER_COUNT; ++i)
130 dri2_surf->dri_buffers[i] = NULL;
131
132 dri2_surf->pending_buffer = NULL;
133 dri2_surf->third_buffer = NULL;
134 dri2_surf->frame_callback = NULL;
135
136 if (conf->AlphaSize == 0)
137 dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
138 else
139 dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
140
141 switch (type) {
142 case EGL_WINDOW_BIT:
143 dri2_surf->wl_win = (struct wl_egl_window *) window;
144
145 dri2_surf->base.Width = -1;
146 dri2_surf->base.Height = -1;
147 break;
148 default:
149 goto cleanup_surf;
150 }
151
152 dri2_surf->dri_drawable =
153 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
154 type == EGL_WINDOW_BIT ?
155 dri2_conf->dri_double_config :
156 dri2_conf->dri_single_config,
157 dri2_surf);
158 if (dri2_surf->dri_drawable == NULL) {
159 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
160 goto cleanup_dri_drawable;
161 }
162
163 return &dri2_surf->base;
164
165 cleanup_dri_drawable:
166 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
167 cleanup_surf:
168 free(dri2_surf);
169
170 return NULL;
171 }
172
173 /**
174 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
175 */
176 static _EGLSurface *
177 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
178 _EGLConfig *conf, EGLNativeWindowType window,
179 const EGLint *attrib_list)
180 {
181 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
182 window, attrib_list);
183 }
184
185 /**
186 * Called via eglDestroySurface(), drv->API.DestroySurface().
187 */
188 static EGLBoolean
189 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
190 {
191 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
192 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
193 int i;
194
195 (void) drv;
196
197 if (!_eglPutSurface(surf))
198 return EGL_TRUE;
199
200 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
201
202 for (i = 0; i < WL_BUFFER_COUNT; ++i)
203 if (dri2_surf->wl_drm_buffer[i])
204 wl_buffer_destroy(dri2_surf->wl_drm_buffer[i]);
205
206 for (i = 0; i < __DRI_BUFFER_COUNT; ++i)
207 if (dri2_surf->dri_buffers[i])
208 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
209 dri2_surf->dri_buffers[i]);
210
211 if (dri2_surf->third_buffer) {
212 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
213 dri2_surf->third_buffer);
214 }
215
216 free(surf);
217
218 return EGL_TRUE;
219 }
220
221 static struct wl_buffer *
222 wayland_create_buffer(struct dri2_egl_surface *dri2_surf,
223 __DRIbuffer *buffer)
224 {
225 struct dri2_egl_display *dri2_dpy =
226 dri2_egl_display(dri2_surf->base.Resource.Display);
227 struct wl_buffer *buf;
228
229 buf = wl_drm_create_buffer(dri2_dpy->wl_drm, buffer->name,
230 dri2_surf->base.Width, dri2_surf->base.Height,
231 buffer->pitch, dri2_surf->format);
232 wl_buffer_add_listener(buf, &wl_buffer_listener, dri2_surf);
233
234 return buf;
235 }
236
237 static void
238 dri2_process_back_buffer(struct dri2_egl_surface *dri2_surf, unsigned format)
239 {
240 struct dri2_egl_display *dri2_dpy =
241 dri2_egl_display(dri2_surf->base.Resource.Display);
242
243 (void) format;
244
245 switch (dri2_surf->base.Type) {
246 case EGL_WINDOW_BIT:
247 /* allocate a front buffer for our double-buffered window*/
248 if (dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT] != NULL)
249 break;
250 dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT] =
251 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
252 __DRI_BUFFER_FRONT_LEFT, format,
253 dri2_surf->base.Width, dri2_surf->base.Height);
254 break;
255 default:
256 break;
257 }
258 }
259
260 static void
261 dri2_release_pending_buffer(void *data,
262 struct wl_callback *callback, uint32_t time)
263 {
264 struct dri2_egl_surface *dri2_surf = data;
265 struct dri2_egl_display *dri2_dpy =
266 dri2_egl_display(dri2_surf->base.Resource.Display);
267
268 /* FIXME: print internal error */
269 if (!dri2_surf->pending_buffer)
270 return;
271
272 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
273 dri2_surf->pending_buffer);
274 dri2_surf->pending_buffer = NULL;
275
276 wl_callback_destroy(callback);
277 }
278
279 static const struct wl_callback_listener release_buffer_listener = {
280 dri2_release_pending_buffer
281 };
282
283 static void
284 dri2_release_buffers(struct dri2_egl_surface *dri2_surf)
285 {
286 struct dri2_egl_display *dri2_dpy =
287 dri2_egl_display(dri2_surf->base.Resource.Display);
288 struct wl_callback *callback;
289 int i;
290
291 if (dri2_surf->third_buffer) {
292 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
293 dri2_surf->third_buffer);
294 dri2_surf->third_buffer = NULL;
295 }
296
297 for (i = 0; i < __DRI_BUFFER_COUNT; ++i) {
298 if (dri2_surf->dri_buffers[i]) {
299 switch (i) {
300 case __DRI_BUFFER_FRONT_LEFT:
301 if (dri2_surf->pending_buffer)
302 roundtrip(dri2_dpy);
303 dri2_surf->pending_buffer = dri2_surf->dri_buffers[i];
304 callback = wl_display_sync(dri2_dpy->wl_dpy);
305 wl_callback_add_listener(callback,
306 &release_buffer_listener, dri2_surf);
307 wl_proxy_set_queue((struct wl_proxy *) callback,
308 dri2_dpy->wl_queue);
309 break;
310 default:
311 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
312 dri2_surf->dri_buffers[i]);
313 break;
314 }
315 dri2_surf->dri_buffers[i] = NULL;
316 }
317 }
318 }
319
320 static inline void
321 pointer_swap(const void **p1, const void **p2)
322 {
323 const void *tmp = *p1;
324 *p1 = *p2;
325 *p2 = tmp;
326 }
327
328 static void
329 destroy_third_buffer(struct dri2_egl_surface *dri2_surf)
330 {
331 struct dri2_egl_display *dri2_dpy =
332 dri2_egl_display(dri2_surf->base.Resource.Display);
333
334 if (dri2_surf->third_buffer == NULL)
335 return;
336
337 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
338 dri2_surf->third_buffer);
339 dri2_surf->third_buffer = NULL;
340
341 if (dri2_surf->wl_drm_buffer[WL_BUFFER_THIRD])
342 wl_buffer_destroy(dri2_surf->wl_drm_buffer[WL_BUFFER_THIRD]);
343 dri2_surf->wl_drm_buffer[WL_BUFFER_THIRD] = NULL;
344 dri2_surf->wl_buffer_lock[WL_BUFFER_THIRD] = 0;
345 }
346
347 static void
348 swap_wl_buffers(struct dri2_egl_surface *dri2_surf,
349 enum wayland_buffer_type a, enum wayland_buffer_type b)
350 {
351 int tmp;
352
353 tmp = dri2_surf->wl_buffer_lock[a];
354 dri2_surf->wl_buffer_lock[a] = dri2_surf->wl_buffer_lock[b];
355 dri2_surf->wl_buffer_lock[b] = tmp;
356
357 pointer_swap((const void **) &dri2_surf->wl_drm_buffer[a],
358 (const void **) &dri2_surf->wl_drm_buffer[b]);
359 }
360
361 static void
362 swap_back_and_third(struct dri2_egl_surface *dri2_surf)
363 {
364 if (dri2_surf->wl_buffer_lock[WL_BUFFER_THIRD])
365 destroy_third_buffer(dri2_surf);
366
367 pointer_swap((const void **) &dri2_surf->dri_buffers[__DRI_BUFFER_BACK_LEFT],
368 (const void **) &dri2_surf->third_buffer);
369
370 swap_wl_buffers(dri2_surf, WL_BUFFER_BACK, WL_BUFFER_THIRD);
371 }
372
373 static void
374 dri2_prior_buffer_creation(struct dri2_egl_surface *dri2_surf,
375 unsigned int type)
376 {
377 switch (type) {
378 case __DRI_BUFFER_BACK_LEFT:
379 if (dri2_surf->wl_buffer_lock[WL_BUFFER_BACK])
380 swap_back_and_third(dri2_surf);
381 else if (dri2_surf->third_buffer)
382 destroy_third_buffer(dri2_surf);
383 break;
384 default:
385 break;
386
387 }
388 }
389
390 static __DRIbuffer *
391 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
392 int *width, int *height,
393 unsigned int *attachments, int count,
394 int *out_count, void *loaderPrivate)
395 {
396 struct dri2_egl_surface *dri2_surf = loaderPrivate;
397 struct dri2_egl_display *dri2_dpy =
398 dri2_egl_display(dri2_surf->base.Resource.Display);
399 int i;
400
401 if (dri2_surf->base.Type == EGL_WINDOW_BIT &&
402 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
403 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
404
405 dri2_release_buffers(dri2_surf);
406
407 dri2_surf->base.Width = dri2_surf->wl_win->width;
408 dri2_surf->base.Height = dri2_surf->wl_win->height;
409 dri2_surf->dx = dri2_surf->wl_win->dx;
410 dri2_surf->dy = dri2_surf->wl_win->dy;
411
412 for (i = 0; i < WL_BUFFER_COUNT; ++i) {
413 if (dri2_surf->wl_drm_buffer[i])
414 wl_buffer_destroy(dri2_surf->wl_drm_buffer[i]);
415 dri2_surf->wl_drm_buffer[i] = NULL;
416 dri2_surf->wl_buffer_lock[i] = 0;
417 }
418 }
419
420 dri2_surf->buffer_count = 0;
421 for (i = 0; i < 2*count; i+=2) {
422 assert(attachments[i] < __DRI_BUFFER_COUNT);
423 assert(dri2_surf->buffer_count < 5);
424
425 dri2_prior_buffer_creation(dri2_surf, attachments[i]);
426
427 if (dri2_surf->dri_buffers[attachments[i]] == NULL) {
428
429 dri2_surf->dri_buffers[attachments[i]] =
430 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
431 attachments[i], attachments[i+1],
432 dri2_surf->base.Width, dri2_surf->base.Height);
433
434 if (!dri2_surf->dri_buffers[attachments[i]])
435 continue;
436
437 if (attachments[i] == __DRI_BUFFER_BACK_LEFT)
438 dri2_process_back_buffer(dri2_surf, attachments[i+1]);
439 }
440
441 memcpy(&dri2_surf->buffers[dri2_surf->buffer_count],
442 dri2_surf->dri_buffers[attachments[i]],
443 sizeof(__DRIbuffer));
444
445 dri2_surf->buffer_count++;
446 }
447
448 assert(dri2_surf->dri_buffers[__DRI_BUFFER_BACK_LEFT]);
449
450 *out_count = dri2_surf->buffer_count;
451 if (dri2_surf->buffer_count == 0)
452 return NULL;
453
454 *width = dri2_surf->base.Width;
455 *height = dri2_surf->base.Height;
456
457 return dri2_surf->buffers;
458 }
459
460 static __DRIbuffer *
461 dri2_get_buffers(__DRIdrawable * driDrawable,
462 int *width, int *height,
463 unsigned int *attachments, int count,
464 int *out_count, void *loaderPrivate)
465 {
466 unsigned int *attachments_with_format;
467 __DRIbuffer *buffer;
468 const unsigned int format = 32;
469 int i;
470
471 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
472 if (!attachments_with_format) {
473 *out_count = 0;
474 return NULL;
475 }
476
477 for (i = 0; i < count; ++i) {
478 attachments_with_format[2*i] = attachments[i];
479 attachments_with_format[2*i + 1] = format;
480 }
481
482 buffer =
483 dri2_get_buffers_with_format(driDrawable,
484 width, height,
485 attachments_with_format, count,
486 out_count, loaderPrivate);
487
488 free(attachments_with_format);
489
490 return buffer;
491 }
492
493
494 static void
495 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
496 {
497 (void) driDrawable;
498
499 /* FIXME: Does EGL support front buffer rendering at all? */
500
501 #if 0
502 struct dri2_egl_surface *dri2_surf = loaderPrivate;
503
504 dri2WaitGL(dri2_surf);
505 #else
506 (void) loaderPrivate;
507 #endif
508 }
509
510 static void
511 wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time)
512 {
513 struct dri2_egl_surface *dri2_surf = data;
514
515 dri2_surf->frame_callback = NULL;
516 wl_callback_destroy(callback);
517 }
518
519 static const struct wl_callback_listener frame_listener = {
520 wayland_frame_callback
521 };
522
523 /**
524 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
525 */
526 static EGLBoolean
527 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
528 {
529 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
530 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
531 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
532 int ret = 0;
533
534 while (dri2_surf->frame_callback && ret != -1)
535 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
536 if (ret < 0)
537 return EGL_FALSE;
538
539 dri2_surf->frame_callback = wl_surface_frame(dri2_surf->wl_win->surface);
540 wl_callback_add_listener(dri2_surf->frame_callback,
541 &frame_listener, dri2_surf);
542 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->frame_callback,
543 dri2_dpy->wl_queue);
544
545 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
546 pointer_swap(
547 (const void **) &dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT],
548 (const void **) &dri2_surf->dri_buffers[__DRI_BUFFER_BACK_LEFT]);
549
550 dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT]->attachment =
551 __DRI_BUFFER_FRONT_LEFT;
552 dri2_surf->dri_buffers[__DRI_BUFFER_BACK_LEFT]->attachment =
553 __DRI_BUFFER_BACK_LEFT;
554
555 swap_wl_buffers(dri2_surf, WL_BUFFER_FRONT, WL_BUFFER_BACK);
556
557 if (!dri2_surf->wl_drm_buffer[WL_BUFFER_FRONT])
558 dri2_surf->wl_drm_buffer[WL_BUFFER_FRONT] =
559 wayland_create_buffer(dri2_surf,
560 dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT]);
561
562 wl_surface_attach(dri2_surf->wl_win->surface,
563 dri2_surf->wl_drm_buffer[WL_BUFFER_FRONT],
564 dri2_surf->dx, dri2_surf->dy);
565 dri2_surf->wl_buffer_lock[WL_BUFFER_FRONT] = 1;
566
567 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
568 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
569 /* reset resize growing parameters */
570 dri2_surf->dx = 0;
571 dri2_surf->dy = 0;
572
573 wl_surface_damage(dri2_surf->wl_win->surface, 0, 0,
574 dri2_surf->base.Width, dri2_surf->base.Height);
575
576 wl_surface_commit(dri2_surf->wl_win->surface);
577 }
578
579 _EGLContext *ctx;
580 if (dri2_drv->glFlush) {
581 ctx = _eglGetCurrentContext();
582 if (ctx && ctx->DrawSurface == &dri2_surf->base)
583 dri2_drv->glFlush();
584 }
585
586 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
587 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
588
589 return EGL_TRUE;
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 }
681 }
682
683 static void
684 drm_handle_authenticated(void *data, struct wl_drm *drm)
685 {
686 struct dri2_egl_display *dri2_dpy = data;
687
688 dri2_dpy->authenticated = 1;
689 }
690
691 static const struct wl_drm_listener drm_listener = {
692 drm_handle_device,
693 drm_handle_format,
694 drm_handle_authenticated
695 };
696
697 static void
698 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
699 const char *interface, uint32_t version)
700 {
701 struct dri2_egl_display *dri2_dpy = data;
702
703 if (strcmp(interface, "wl_drm") == 0) {
704 dri2_dpy->wl_drm =
705 wl_registry_bind(registry, name, &wl_drm_interface, 1);
706 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
707 }
708 }
709
710 static const struct wl_registry_listener registry_listener = {
711 registry_handle_global
712 };
713
714 EGLBoolean
715 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
716 {
717 struct dri2_egl_display *dri2_dpy;
718 const __DRIconfig *config;
719 uint32_t types;
720 int i;
721 static const unsigned int argb_masks[4] =
722 { 0xff0000, 0xff00, 0xff, 0xff000000 };
723 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
724
725 drv->API.CreateWindowSurface = dri2_create_window_surface;
726 drv->API.DestroySurface = dri2_destroy_surface;
727 drv->API.SwapBuffers = dri2_swap_buffers;
728 drv->API.Terminate = dri2_terminate;
729
730 dri2_dpy = calloc(1, sizeof *dri2_dpy);
731 if (!dri2_dpy)
732 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
733
734 disp->DriverData = (void *) dri2_dpy;
735 if (disp->PlatformDisplay == NULL) {
736 dri2_dpy->wl_dpy = wl_display_connect(NULL);
737 if (dri2_dpy->wl_dpy == NULL)
738 goto cleanup_dpy;
739 dri2_dpy->own_device = 1;
740 } else {
741 dri2_dpy->wl_dpy = disp->PlatformDisplay;
742 }
743
744 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
745 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
746 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
747 dri2_dpy->wl_queue);
748 wl_registry_add_listener(dri2_dpy->wl_registry,
749 &registry_listener, dri2_dpy);
750 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
751 goto cleanup_dpy;
752
753 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
754 goto cleanup_drm;
755
756 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
757 goto cleanup_fd;
758
759 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
760 if (dri2_dpy->driver_name == NULL) {
761 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
762 goto cleanup_fd;
763 }
764
765 if (!dri2_load_driver(disp))
766 goto cleanup_driver_name;
767
768 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
769 dri2_dpy->dri2_loader_extension.base.version = 3;
770 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
771 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
772 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
773 dri2_get_buffers_with_format;
774
775 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
776 dri2_dpy->extensions[1] = &image_lookup_extension.base;
777 dri2_dpy->extensions[2] = &use_invalidate.base;
778 dri2_dpy->extensions[3] = NULL;
779
780 if (!dri2_create_screen(disp))
781 goto cleanup_driver;
782
783 types = EGL_WINDOW_BIT;
784 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
785 config = dri2_dpy->driver_configs[i];
786 if (dri2_dpy->formats & HAS_XRGB8888)
787 dri2_add_config(disp, config, i + 1, 0, types, NULL, rgb_masks);
788 if (dri2_dpy->formats & HAS_ARGB8888)
789 dri2_add_config(disp, config, i + 1, 0, types, NULL, argb_masks);
790 }
791
792 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
793 dri2_dpy->authenticate = dri2_wayland_authenticate;
794
795 /* we're supporting EGL 1.4 */
796 disp->VersionMajor = 1;
797 disp->VersionMinor = 4;
798
799 return EGL_TRUE;
800
801 cleanup_driver:
802 dlclose(dri2_dpy->driver);
803 cleanup_driver_name:
804 free(dri2_dpy->driver_name);
805 cleanup_fd:
806 close(dri2_dpy->fd);
807 cleanup_drm:
808 free(dri2_dpy->device_name);
809 wl_drm_destroy(dri2_dpy->wl_drm);
810 cleanup_dpy:
811 free(dri2_dpy);
812
813 return EGL_FALSE;
814 }