2 * Copyright © 2011-2012 Intel Corporation
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:
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
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.
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
40 #include <wayland-client.h>
41 #include "wayland-drm-client-protocol.h"
43 enum wl_drm_format_flags
{
49 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
54 wl_callback_destroy(callback
);
57 static const struct wl_callback_listener sync_listener
= {
62 roundtrip(struct dri2_egl_display
*dri2_dpy
)
64 struct wl_callback
*callback
;
65 int done
= 0, ret
= 0;
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
);
74 wl_callback_destroy(callback
);
80 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
82 struct dri2_egl_surface
*dri2_surf
= data
;
85 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
86 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
89 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
90 wl_buffer_destroy(buffer
);
94 dri2_surf
->color_buffers
[i
].locked
= 0;
97 static struct wl_buffer_listener wl_buffer_listener
= {
102 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
104 struct dri2_egl_surface
*dri2_surf
= data
;
105 struct dri2_egl_display
*dri2_dpy
=
106 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
108 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
112 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
115 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
116 _EGLConfig
*conf
, EGLNativeWindowType window
,
117 const EGLint
*attrib_list
)
119 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
120 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
121 struct dri2_egl_surface
*dri2_surf
;
125 dri2_surf
= malloc(sizeof *dri2_surf
);
127 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
131 memset(dri2_surf
, 0, sizeof *dri2_surf
);
132 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
135 if (conf
->AlphaSize
== 0)
136 dri2_surf
->format
= WL_DRM_FORMAT_XRGB8888
;
138 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
142 dri2_surf
->wl_win
= (struct wl_egl_window
*) window
;
144 dri2_surf
->wl_win
->private = dri2_surf
;
145 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
147 dri2_surf
->base
.Width
= -1;
148 dri2_surf
->base
.Height
= -1;
154 dri2_surf
->dri_drawable
=
155 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
156 type
== EGL_WINDOW_BIT
?
157 dri2_conf
->dri_double_config
:
158 dri2_conf
->dri_single_config
,
160 if (dri2_surf
->dri_drawable
== NULL
) {
161 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
162 goto cleanup_dri_drawable
;
165 return &dri2_surf
->base
;
167 cleanup_dri_drawable
:
168 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
176 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
179 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
180 _EGLConfig
*conf
, EGLNativeWindowType window
,
181 const EGLint
*attrib_list
)
183 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
184 window
, attrib_list
);
188 * Called via eglDestroySurface(), drv->API.DestroySurface().
191 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
193 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
194 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
199 if (!_eglPutSurface(surf
))
202 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
204 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
205 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
206 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
207 if (dri2_surf
->color_buffers
[i
].dri_image
)
208 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
211 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
212 if (dri2_surf
->dri_buffers
[i
] &&
213 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
214 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
215 dri2_surf
->dri_buffers
[i
]);
217 if (dri2_surf
->frame_callback
)
218 wl_callback_destroy(dri2_surf
->frame_callback
);
220 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
221 dri2_surf
->wl_win
->private = NULL
;
222 dri2_surf
->wl_win
->resize_callback
= NULL
;
231 dri2_release_buffers(struct dri2_egl_surface
*dri2_surf
)
233 struct dri2_egl_display
*dri2_dpy
=
234 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
237 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
238 if (dri2_surf
->color_buffers
[i
].wl_buffer
&&
239 !dri2_surf
->color_buffers
[i
].locked
)
240 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
241 if (dri2_surf
->color_buffers
[i
].dri_image
)
242 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
244 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
245 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
246 dri2_surf
->color_buffers
[i
].locked
= 0;
249 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++)
250 if (dri2_surf
->dri_buffers
[i
] &&
251 dri2_surf
->dri_buffers
[i
]->attachment
!= __DRI_BUFFER_BACK_LEFT
)
252 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
253 dri2_surf
->dri_buffers
[i
]);
257 get_back_bo(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
259 struct dri2_egl_display
*dri2_dpy
=
260 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
264 /* There might be a buffer release already queued that wasn't processed */
265 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
267 if (dri2_surf
->back
== NULL
) {
268 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
269 /* Get an unlocked buffer, preferrably one with a dri_buffer already
271 if (dri2_surf
->color_buffers
[i
].locked
)
273 if (dri2_surf
->back
== NULL
)
274 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
275 else if (dri2_surf
->back
->dri_image
== NULL
)
276 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
280 if (dri2_surf
->back
== NULL
)
282 if (dri2_surf
->back
->dri_image
== NULL
) {
283 dri2_surf
->back
->dri_image
=
284 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
285 dri2_surf
->base
.Width
,
286 dri2_surf
->base
.Height
,
287 __DRI_IMAGE_FORMAT_ARGB8888
,
288 __DRI_IMAGE_USE_SHARE
,
290 dri2_surf
->back
->age
= 0;
292 if (dri2_surf
->back
->dri_image
== NULL
)
295 image
= dri2_surf
->back
->dri_image
;
297 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
298 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
300 dri2_surf
->back
->name
= name
;
301 dri2_surf
->back
->pitch
= pitch
;
303 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
305 buffer
->pitch
= pitch
;
309 dri2_surf
->back
->locked
= 1;
315 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
316 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
318 struct dri2_egl_display
*dri2_dpy
=
319 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
320 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
323 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
325 dri2_surf
->base
.Width
,
326 dri2_surf
->base
.Height
);
327 dri2_surf
->dri_buffers
[attachment
] = b
;
332 memcpy(buffer
, b
, sizeof *buffer
);
338 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
339 int *width
, int *height
,
340 unsigned int *attachments
, int count
,
341 int *out_count
, void *loaderPrivate
)
343 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
344 struct dri2_egl_display
*dri2_dpy
=
345 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
348 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&&
349 (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
350 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
)) {
352 dri2_release_buffers(dri2_surf
);
354 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
355 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
356 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
357 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
360 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
361 switch (attachments
[i
]) {
362 case __DRI_BUFFER_BACK_LEFT
:
363 if (get_back_bo(dri2_surf
, &dri2_surf
->buffers
[j
]) < 0) {
364 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
369 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
370 &dri2_surf
->buffers
[j
]) < 0) {
371 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
378 /* If we have an extra unlocked buffer at this point, we had to do triple
379 * buffering for a while, but now can go back to just double buffering.
380 * That means we can free any unlocked buffer now. */
381 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
382 if (!dri2_surf
->color_buffers
[i
].locked
&&
383 dri2_surf
->color_buffers
[i
].wl_buffer
) {
384 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
385 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
386 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
387 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
395 *width
= dri2_surf
->base
.Width
;
396 *height
= dri2_surf
->base
.Height
;
398 return dri2_surf
->buffers
;
402 dri2_get_buffers(__DRIdrawable
* driDrawable
,
403 int *width
, int *height
,
404 unsigned int *attachments
, int count
,
405 int *out_count
, void *loaderPrivate
)
407 unsigned int *attachments_with_format
;
409 const unsigned int format
= 32;
412 attachments_with_format
= calloc(count
* 2, sizeof(unsigned int));
413 if (!attachments_with_format
) {
418 for (i
= 0; i
< count
; ++i
) {
419 attachments_with_format
[2*i
] = attachments
[i
];
420 attachments_with_format
[2*i
+ 1] = format
;
424 dri2_get_buffers_with_format(driDrawable
,
426 attachments_with_format
, count
,
427 out_count
, loaderPrivate
);
429 free(attachments_with_format
);
435 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
438 (void) loaderPrivate
;
442 wayland_frame_callback(void *data
, struct wl_callback
*callback
, uint32_t time
)
444 struct dri2_egl_surface
*dri2_surf
= data
;
446 dri2_surf
->frame_callback
= NULL
;
447 wl_callback_destroy(callback
);
450 static const struct wl_callback_listener frame_listener
= {
451 wayland_frame_callback
455 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
457 struct dri2_egl_display
*dri2_dpy
=
458 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
461 if (dri2_surf
->current
->wl_buffer
!= NULL
)
464 if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
465 dri2_dpy
->image
->queryImage(dri2_surf
->current
->dri_image
,
466 __DRI_IMAGE_ATTRIB_FD
, &fd
);
468 dri2_surf
->current
->wl_buffer
=
469 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
471 dri2_surf
->base
.Width
,
472 dri2_surf
->base
.Height
,
474 0, dri2_surf
->current
->pitch
,
479 dri2_surf
->current
->wl_buffer
=
480 wl_drm_create_buffer(dri2_dpy
->wl_drm
,
481 dri2_surf
->current
->name
,
482 dri2_surf
->base
.Width
,
483 dri2_surf
->base
.Height
,
484 dri2_surf
->current
->pitch
,
488 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
490 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
491 &wl_buffer_listener
, dri2_surf
);
495 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
498 dri2_swap_buffers_with_damage(_EGLDriver
*drv
,
504 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
505 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
509 while (dri2_surf
->frame_callback
&& ret
!= -1)
510 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
514 dri2_surf
->frame_callback
= wl_surface_frame(dri2_surf
->wl_win
->surface
);
515 wl_callback_add_listener(dri2_surf
->frame_callback
,
516 &frame_listener
, dri2_surf
);
517 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->frame_callback
,
520 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
521 if (dri2_surf
->color_buffers
[i
].age
> 0)
522 dri2_surf
->color_buffers
[i
].age
++;
524 /* Make sure we have a back buffer in case we're swapping without ever
526 if (get_back_bo(dri2_surf
, &buffer
) < 0) {
527 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
531 dri2_surf
->back
->age
= 1;
532 dri2_surf
->current
= dri2_surf
->back
;
533 dri2_surf
->back
= NULL
;
535 create_wl_buffer(dri2_surf
);
537 wl_surface_attach(dri2_surf
->wl_win
->surface
,
538 dri2_surf
->current
->wl_buffer
,
539 dri2_surf
->dx
, dri2_surf
->dy
);
541 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
542 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
543 /* reset resize growing parameters */
548 wl_surface_damage(dri2_surf
->wl_win
->surface
, 0, 0,
549 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
551 for (i
= 0; i
< n_rects
; i
++) {
552 const int *rect
= &rects
[i
* 4];
553 wl_surface_damage(dri2_surf
->wl_win
->surface
,
555 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
560 wl_surface_commit(dri2_surf
->wl_win
->surface
);
562 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
563 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
569 dri2_query_buffer_age(_EGLDriver
*drv
,
570 _EGLDisplay
*disp
, _EGLSurface
*surface
)
572 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
575 if (get_back_bo(dri2_surf
, &buffer
) < 0) {
576 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
580 return dri2_surf
->back
->age
;
584 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
586 return dri2_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
590 dri2_wayland_authenticate(_EGLDisplay
*disp
, uint32_t id
)
592 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
595 dri2_dpy
->authenticated
= 0;
597 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
598 if (roundtrip(dri2_dpy
) < 0)
601 if (!dri2_dpy
->authenticated
)
604 /* reset authenticated */
605 dri2_dpy
->authenticated
= 1;
611 * Called via eglTerminate(), drv->API.Terminate().
614 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
616 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
618 _eglReleaseDisplayResources(drv
, disp
);
619 _eglCleanupDisplay(disp
);
621 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
623 dlclose(dri2_dpy
->driver
);
624 free(dri2_dpy
->driver_name
);
625 free(dri2_dpy
->device_name
);
626 wl_drm_destroy(dri2_dpy
->wl_drm
);
627 if (dri2_dpy
->own_device
)
628 wl_display_disconnect(dri2_dpy
->wl_dpy
);
630 disp
->DriverData
= NULL
;
636 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
638 struct dri2_egl_display
*dri2_dpy
= data
;
641 dri2_dpy
->device_name
= strdup(device
);
642 if (!dri2_dpy
->device_name
)
646 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
647 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
650 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
651 if (dri2_dpy
->fd
!= -1)
652 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
655 if (dri2_dpy
->fd
== -1) {
656 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
657 dri2_dpy
->device_name
, strerror(errno
));
661 drmGetMagic(dri2_dpy
->fd
, &magic
);
662 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
666 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
668 struct dri2_egl_display
*dri2_dpy
= data
;
671 case WL_DRM_FORMAT_ARGB8888
:
672 dri2_dpy
->formats
|= HAS_ARGB8888
;
674 case WL_DRM_FORMAT_XRGB8888
:
675 dri2_dpy
->formats
|= HAS_XRGB8888
;
681 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
683 struct dri2_egl_display
*dri2_dpy
= data
;
685 dri2_dpy
->capabilities
= value
;
689 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
691 struct dri2_egl_display
*dri2_dpy
= data
;
693 dri2_dpy
->authenticated
= 1;
696 static const struct wl_drm_listener drm_listener
= {
699 drm_handle_authenticated
,
700 drm_handle_capabilities
704 registry_handle_global(void *data
, struct wl_registry
*registry
, uint32_t name
,
705 const char *interface
, uint32_t version
)
707 struct dri2_egl_display
*dri2_dpy
= data
;
711 if (strcmp(interface
, "wl_drm") == 0) {
713 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
714 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
718 static const struct wl_registry_listener registry_listener
= {
719 registry_handle_global
723 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
725 struct dri2_egl_display
*dri2_dpy
;
726 const __DRIconfig
*config
;
729 static const unsigned int argb_masks
[4] =
730 { 0xff0000, 0xff00, 0xff, 0xff000000 };
731 static const unsigned int rgb_masks
[4] = { 0xff0000, 0xff00, 0xff, 0 };
733 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
734 drv
->API
.DestroySurface
= dri2_destroy_surface
;
735 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
736 drv
->API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
737 drv
->API
.Terminate
= dri2_terminate
;
738 drv
->API
.QueryBufferAge
= dri2_query_buffer_age
;
740 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
742 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
744 disp
->DriverData
= (void *) dri2_dpy
;
745 if (disp
->PlatformDisplay
== NULL
) {
746 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
747 if (dri2_dpy
->wl_dpy
== NULL
)
749 dri2_dpy
->own_device
= 1;
751 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
754 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
756 if (dri2_dpy
->own_device
)
757 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
759 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy
);
760 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_registry
,
762 wl_registry_add_listener(dri2_dpy
->wl_registry
,
763 ®istry_listener
, dri2_dpy
);
764 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
767 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
770 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
773 dri2_dpy
->driver_name
= dri2_get_driver_for_fd(dri2_dpy
->fd
);
774 if (dri2_dpy
->driver_name
== NULL
) {
775 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
779 if (!dri2_load_driver(disp
))
780 goto cleanup_driver_name
;
782 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
783 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
784 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
785 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
786 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
787 dri2_get_buffers_with_format
;
789 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
790 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
791 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
792 dri2_dpy
->extensions
[3] = NULL
;
794 if (!dri2_create_screen(disp
))
797 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
798 * doesn't have createImageFromFds, since we're using the same driver on
799 * both sides. We don't want crash if that happens anyway, so fall back to
800 * gem names if we don't have prime support. */
802 if (dri2_dpy
->image
->base
.version
< 7 ||
803 dri2_dpy
->image
->createImageFromFds
== NULL
)
804 dri2_dpy
->capabilities
&= WL_DRM_CAPABILITY_PRIME
;
806 types
= EGL_WINDOW_BIT
;
807 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
808 config
= dri2_dpy
->driver_configs
[i
];
809 if (dri2_dpy
->formats
& HAS_XRGB8888
)
810 dri2_add_config(disp
, config
, i
+ 1, 0, types
, NULL
, rgb_masks
);
811 if (dri2_dpy
->formats
& HAS_ARGB8888
)
812 dri2_add_config(disp
, config
, i
+ 1, 0, types
, NULL
, argb_masks
);
815 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
816 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
817 dri2_dpy
->authenticate
= dri2_wayland_authenticate
;
819 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
821 /* we're supporting EGL 1.4 */
822 disp
->VersionMajor
= 1;
823 disp
->VersionMinor
= 4;
828 dlclose(dri2_dpy
->driver
);
830 free(dri2_dpy
->driver_name
);
834 free(dri2_dpy
->device_name
);
835 wl_drm_destroy(dri2_dpy
->wl_drm
);