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
{
50 sync_callback(void *data
, struct wl_callback
*callback
, uint32_t serial
)
55 wl_callback_destroy(callback
);
58 static const struct wl_callback_listener sync_listener
= {
63 roundtrip(struct dri2_egl_display
*dri2_dpy
)
65 struct wl_callback
*callback
;
66 int done
= 0, ret
= 0;
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
);
75 wl_callback_destroy(callback
);
81 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
83 struct dri2_egl_surface
*dri2_surf
= data
;
86 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
87 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
90 if (i
== ARRAY_SIZE(dri2_surf
->color_buffers
)) {
91 wl_buffer_destroy(buffer
);
95 dri2_surf
->color_buffers
[i
].locked
= 0;
98 static struct wl_buffer_listener wl_buffer_listener
= {
103 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
105 struct dri2_egl_surface
*dri2_surf
= data
;
106 struct dri2_egl_display
*dri2_dpy
=
107 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
109 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
113 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
116 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
117 _EGLConfig
*conf
, EGLNativeWindowType window
,
118 const EGLint
*attrib_list
)
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
;
126 dri2_surf
= malloc(sizeof *dri2_surf
);
128 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
132 memset(dri2_surf
, 0, sizeof *dri2_surf
);
133 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
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
;
141 dri2_surf
->format
= WL_DRM_FORMAT_ARGB8888
;
145 dri2_surf
->wl_win
= (struct wl_egl_window
*) window
;
147 dri2_surf
->wl_win
->private = dri2_surf
;
148 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
150 dri2_surf
->base
.Width
= -1;
151 dri2_surf
->base
.Height
= -1;
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
,
163 if (dri2_surf
->dri_drawable
== NULL
) {
164 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
165 goto cleanup_dri_drawable
;
168 return &dri2_surf
->base
;
170 cleanup_dri_drawable
:
171 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
179 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
182 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
183 _EGLConfig
*conf
, EGLNativeWindowType window
,
184 const EGLint
*attrib_list
)
186 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
187 window
, attrib_list
);
191 * Called via eglDestroySurface(), drv->API.DestroySurface().
194 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
196 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
197 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
202 if (!_eglPutSurface(surf
))
205 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
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
);
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
]);
220 if (dri2_surf
->frame_callback
)
221 wl_callback_destroy(dri2_surf
->frame_callback
);
223 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
224 dri2_surf
->wl_win
->private = NULL
;
225 dri2_surf
->wl_win
->resize_callback
= NULL
;
234 dri2_release_buffers(struct dri2_egl_surface
*dri2_surf
)
236 struct dri2_egl_display
*dri2_dpy
=
237 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
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
);
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;
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
]);
260 get_back_bo(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
262 struct dri2_egl_display
*dri2_dpy
=
263 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
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
);
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
274 if (dri2_surf
->color_buffers
[i
].locked
)
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
];
283 if (dri2_surf
->back
== NULL
)
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
,
293 dri2_surf
->back
->age
= 0;
295 if (dri2_surf
->back
->dri_image
== NULL
)
298 image
= dri2_surf
->back
->dri_image
;
300 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
301 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
303 dri2_surf
->back
->name
= name
;
304 dri2_surf
->back
->pitch
= pitch
;
306 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
308 buffer
->pitch
= pitch
;
312 dri2_surf
->back
->locked
= 1;
318 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
319 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
321 struct dri2_egl_display
*dri2_dpy
=
322 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
323 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
326 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
328 dri2_surf
->base
.Width
,
329 dri2_surf
->base
.Height
);
330 dri2_surf
->dri_buffers
[attachment
] = b
;
335 memcpy(buffer
, b
, sizeof *buffer
);
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
)
346 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
347 struct dri2_egl_display
*dri2_dpy
=
348 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
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
)) {
355 dri2_release_buffers(dri2_surf
);
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
;
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");
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");
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
;
398 *width
= dri2_surf
->base
.Width
;
399 *height
= dri2_surf
->base
.Height
;
401 return dri2_surf
->buffers
;
405 dri2_get_buffers(__DRIdrawable
* driDrawable
,
406 int *width
, int *height
,
407 unsigned int *attachments
, int count
,
408 int *out_count
, void *loaderPrivate
)
410 unsigned int *attachments_with_format
;
412 const unsigned int format
= 32;
415 attachments_with_format
= calloc(count
* 2, sizeof(unsigned int));
416 if (!attachments_with_format
) {
421 for (i
= 0; i
< count
; ++i
) {
422 attachments_with_format
[2*i
] = attachments
[i
];
423 attachments_with_format
[2*i
+ 1] = format
;
427 dri2_get_buffers_with_format(driDrawable
,
429 attachments_with_format
, count
,
430 out_count
, loaderPrivate
);
432 free(attachments_with_format
);
438 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
441 (void) loaderPrivate
;
445 wayland_frame_callback(void *data
, struct wl_callback
*callback
, uint32_t time
)
447 struct dri2_egl_surface
*dri2_surf
= data
;
449 dri2_surf
->frame_callback
= NULL
;
450 wl_callback_destroy(callback
);
453 static const struct wl_callback_listener frame_listener
= {
454 wayland_frame_callback
458 create_wl_buffer(struct dri2_egl_surface
*dri2_surf
)
460 struct dri2_egl_display
*dri2_dpy
=
461 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
464 if (dri2_surf
->current
->wl_buffer
!= NULL
)
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
);
471 dri2_surf
->current
->wl_buffer
=
472 wl_drm_create_prime_buffer(dri2_dpy
->wl_drm
,
474 dri2_surf
->base
.Width
,
475 dri2_surf
->base
.Height
,
477 0, dri2_surf
->current
->pitch
,
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
,
491 wl_proxy_set_queue((struct wl_proxy
*) dri2_surf
->current
->wl_buffer
,
493 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
494 &wl_buffer_listener
, dri2_surf
);
498 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
501 dri2_swap_buffers_with_damage(_EGLDriver
*drv
,
507 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
508 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
512 while (dri2_surf
->frame_callback
&& ret
!= -1)
513 ret
= wl_display_dispatch_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
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
,
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
++;
527 /* Make sure we have a back buffer in case we're swapping without ever
529 if (get_back_bo(dri2_surf
, &buffer
) < 0) {
530 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
534 dri2_surf
->back
->age
= 1;
535 dri2_surf
->current
= dri2_surf
->back
;
536 dri2_surf
->back
= NULL
;
538 create_wl_buffer(dri2_surf
);
540 wl_surface_attach(dri2_surf
->wl_win
->surface
,
541 dri2_surf
->current
->wl_buffer
,
542 dri2_surf
->dx
, dri2_surf
->dy
);
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 */
551 wl_surface_damage(dri2_surf
->wl_win
->surface
, 0, 0,
552 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
554 for (i
= 0; i
< n_rects
; i
++) {
555 const int *rect
= &rects
[i
* 4];
556 wl_surface_damage(dri2_surf
->wl_win
->surface
,
558 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
563 wl_surface_commit(dri2_surf
->wl_win
->surface
);
565 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
566 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
572 dri2_query_buffer_age(_EGLDriver
*drv
,
573 _EGLDisplay
*disp
, _EGLSurface
*surface
)
575 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
578 if (get_back_bo(dri2_surf
, &buffer
) < 0) {
579 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
583 return dri2_surf
->back
->age
;
587 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
589 return dri2_swap_buffers_with_damage (drv
, disp
, draw
, NULL
, 0);
593 dri2_wayland_authenticate(_EGLDisplay
*disp
, uint32_t id
)
595 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
598 dri2_dpy
->authenticated
= 0;
600 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
601 if (roundtrip(dri2_dpy
) < 0)
604 if (!dri2_dpy
->authenticated
)
607 /* reset authenticated */
608 dri2_dpy
->authenticated
= 1;
614 * Called via eglTerminate(), drv->API.Terminate().
617 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
619 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
621 _eglReleaseDisplayResources(drv
, disp
);
622 _eglCleanupDisplay(disp
);
624 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
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
);
633 disp
->DriverData
= NULL
;
639 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
641 struct dri2_egl_display
*dri2_dpy
= data
;
644 dri2_dpy
->device_name
= strdup(device
);
645 if (!dri2_dpy
->device_name
)
649 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
650 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
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
) |
658 if (dri2_dpy
->fd
== -1) {
659 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
660 dri2_dpy
->device_name
, strerror(errno
));
664 drmGetMagic(dri2_dpy
->fd
, &magic
);
665 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
669 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
671 struct dri2_egl_display
*dri2_dpy
= data
;
674 case WL_DRM_FORMAT_ARGB8888
:
675 dri2_dpy
->formats
|= HAS_ARGB8888
;
677 case WL_DRM_FORMAT_XRGB8888
:
678 dri2_dpy
->formats
|= HAS_XRGB8888
;
680 case WL_DRM_FORMAT_RGB565
:
681 dri2_dpy
->formats
|= HAS_RGB565
;
687 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
689 struct dri2_egl_display
*dri2_dpy
= data
;
691 dri2_dpy
->capabilities
= value
;
695 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
697 struct dri2_egl_display
*dri2_dpy
= data
;
699 dri2_dpy
->authenticated
= 1;
702 static const struct wl_drm_listener drm_listener
= {
705 drm_handle_authenticated
,
706 drm_handle_capabilities
710 registry_handle_global(void *data
, struct wl_registry
*registry
, uint32_t name
,
711 const char *interface
, uint32_t version
)
713 struct dri2_egl_display
*dri2_dpy
= data
;
717 if (strcmp(interface
, "wl_drm") == 0) {
719 wl_registry_bind(registry
, name
, &wl_drm_interface
, version
);
720 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
725 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
730 static const struct wl_registry_listener registry_listener
= {
731 registry_handle_global
,
732 registry_handle_global_remove
736 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
738 struct dri2_egl_display
*dri2_dpy
;
739 const __DRIconfig
*config
;
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 };
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
;
754 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
756 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
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
)
763 dri2_dpy
->own_device
= 1;
765 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
768 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
770 if (dri2_dpy
->own_device
)
771 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
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
,
776 wl_registry_add_listener(dri2_dpy
->wl_registry
,
777 ®istry_listener
, dri2_dpy
);
778 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
781 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
784 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
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");
793 if (!dri2_load_driver(disp
))
794 goto cleanup_driver_name
;
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
;
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
;
808 if (!dri2_create_screen(disp
))
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. */
816 if (dri2_dpy
->image
->base
.version
< 7 ||
817 dri2_dpy
->image
->createImageFromFds
== NULL
)
818 dri2_dpy
->capabilities
&= WL_DRM_CAPABILITY_PRIME
;
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
);
831 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
832 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
833 dri2_dpy
->authenticate
= dri2_wayland_authenticate
;
835 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
837 /* we're supporting EGL 1.4 */
838 disp
->VersionMajor
= 1;
839 disp
->VersionMinor
= 4;
844 dlclose(dri2_dpy
->driver
);
846 free(dri2_dpy
->driver_name
);
850 free(dri2_dpy
->device_name
);
851 wl_drm_destroy(dri2_dpy
->wl_drm
);