2 * Copyright © 2011 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>
39 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
42 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
43 _EGLConfig
*conf
, EGLNativeWindowType window
,
44 const EGLint
*attrib_list
)
46 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
47 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
48 struct dri2_egl_surface
*dri2_surf
;
49 struct dri2_egl_buffer
*dri2_buf
;
54 dri2_surf
= malloc(sizeof *dri2_surf
);
56 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
60 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
63 for (i
= 0; i
< WL_BUFFER_COUNT
; ++i
)
64 dri2_surf
->wl_drm_buffer
[i
] = NULL
;
66 for (i
= 0; i
< __DRI_BUFFER_COUNT
; ++i
)
67 dri2_surf
->dri_buffers
[i
] = NULL
;
69 dri2_surf
->block_swap_buffers
= EGL_FALSE
;
73 dri2_surf
->wl_win
= (struct wl_egl_window
*) window
;
74 dri2_surf
->type
= DRI2_WINDOW_SURFACE
;
76 dri2_surf
->base
.Width
= -1;
77 dri2_surf
->base
.Height
= -1;
80 dri2_surf
->wl_pix
= (struct wl_egl_pixmap
*) window
;
81 dri2_surf
->type
= DRI2_PIXMAP_SURFACE
;
83 dri2_surf
->base
.Width
= dri2_surf
->wl_pix
->width
;
84 dri2_surf
->base
.Height
= dri2_surf
->wl_pix
->height
;
86 if (dri2_surf
->wl_pix
->name
> 0) {
87 dri2_buf
= dri2_surf
->wl_pix
->driver_private
;
88 dri2_surf
->dri_buffers
[__DRI_BUFFER_FRONT_LEFT
] = dri2_buf
->dri_buffer
;
95 dri2_surf
->dri_drawable
=
96 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
97 dri2_conf
->dri_config
, dri2_surf
);
98 if (dri2_surf
->dri_drawable
== NULL
) {
99 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
100 goto cleanup_dri_drawable
;
103 return &dri2_surf
->base
;
105 cleanup_dri_drawable
:
106 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
114 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
117 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
118 _EGLConfig
*conf
, EGLNativeWindowType window
,
119 const EGLint
*attrib_list
)
121 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
122 window
, attrib_list
);
126 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
127 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
128 const EGLint
*attrib_list
)
130 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
131 pixmap
, attrib_list
);
135 * Called via eglDestroySurface(), drv->API.DestroySurface().
138 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
140 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
141 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
146 if (!_eglPutSurface(surf
))
149 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
151 for (i
= 0; i
< WL_BUFFER_COUNT
; ++i
)
152 if (dri2_surf
->wl_drm_buffer
[i
])
153 wl_buffer_destroy(dri2_surf
->wl_drm_buffer
[i
]);
155 for (i
= 0; i
< __DRI_BUFFER_COUNT
; ++i
)
156 if (dri2_surf
->dri_buffers
[i
] && !(i
== __DRI_BUFFER_FRONT_LEFT
&&
157 dri2_surf
->type
== DRI2_PIXMAP_SURFACE
))
158 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
159 dri2_surf
->dri_buffers
[i
]);
167 dri2_wl_egl_pixmap_destroy(struct wl_egl_pixmap
*egl_pixmap
)
169 struct dri2_egl_buffer
*dri2_buf
= egl_pixmap
->driver_private
;
173 dri2_buf
->dri2_dpy
->dri2
->releaseBuffer(dri2_buf
->dri2_dpy
->dri_screen
,
174 dri2_buf
->dri_buffer
);
178 egl_pixmap
->driver_private
= NULL
;
179 egl_pixmap
->destroy
= NULL
;
180 egl_pixmap
->name
= 0;
184 dri2_process_front_buffer(struct dri2_egl_surface
*dri2_surf
, unsigned format
)
186 struct dri2_egl_display
*dri2_dpy
=
187 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
188 struct dri2_egl_buffer
*dri2_buf
;
190 /* allocate a back buffer for our double-buffered window*/
191 switch (dri2_surf
->type
) {
192 case DRI2_WINDOW_SURFACE
:
193 dri2_surf
->dri_buffers
[__DRI_BUFFER_BACK_LEFT
] =
194 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
195 __DRI_BUFFER_BACK_LEFT
, format
,
196 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
198 case DRI2_PIXMAP_SURFACE
:
199 dri2_buf
= malloc(sizeof *dri2_buf
);
203 dri2_buf
->dri_buffer
= dri2_surf
->dri_buffers
[__DRI_BUFFER_FRONT_LEFT
];
204 dri2_buf
->dri2_dpy
= dri2_dpy
;
206 dri2_surf
->wl_pix
->name
= dri2_buf
->dri_buffer
->name
;
207 dri2_surf
->wl_pix
->stride
= dri2_buf
->dri_buffer
->pitch
;
208 dri2_surf
->wl_pix
->driver_private
= dri2_buf
;
209 dri2_surf
->wl_pix
->destroy
= dri2_wl_egl_pixmap_destroy
;
217 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
218 int *width
, int *height
,
219 unsigned int *attachments
, int count
,
220 int *out_count
, void *loaderPrivate
)
222 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
223 struct dri2_egl_display
*dri2_dpy
=
224 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
227 if (dri2_surf
->type
== DRI2_WINDOW_SURFACE
&&
228 (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
229 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
)) {
230 for (i
= 0; i
< __DRI_BUFFER_COUNT
; ++i
) {
231 if (dri2_surf
->dri_buffers
[i
]) {
232 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
233 dri2_surf
->dri_buffers
[i
]);
234 dri2_surf
->dri_buffers
[i
] = NULL
;
238 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
239 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
240 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
241 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
243 for (i
= 0; i
< WL_BUFFER_COUNT
; ++i
) {
244 if (dri2_surf
->wl_drm_buffer
[i
])
245 wl_buffer_destroy(dri2_surf
->wl_drm_buffer
[i
]);
246 dri2_surf
->wl_drm_buffer
[i
] = NULL
;
250 dri2_surf
->buffer_count
= 0;
251 for (i
= 0; i
< 2*count
; i
+=2) {
252 assert(attachments
[i
] < __DRI_BUFFER_COUNT
);
253 assert(dri2_surf
->buffer_count
< 5);
255 if (dri2_surf
->dri_buffers
[attachments
[i
]] == NULL
) {
257 dri2_surf
->dri_buffers
[attachments
[i
]] =
258 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
259 attachments
[i
], attachments
[i
+1],
260 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
262 if (!dri2_surf
->dri_buffers
[attachments
[i
]])
265 if (attachments
[i
] == __DRI_BUFFER_FRONT_LEFT
)
266 dri2_process_front_buffer(dri2_surf
, attachments
[i
+1]);
269 memcpy(&dri2_surf
->buffers
[dri2_surf
->buffer_count
],
270 dri2_surf
->dri_buffers
[attachments
[i
]],
271 sizeof(__DRIbuffer
));
273 dri2_surf
->buffer_count
++;
276 assert(dri2_surf
->type
== DRI2_PIXMAP_SURFACE
||
277 dri2_surf
->dri_buffers
[__DRI_BUFFER_BACK_LEFT
]);
279 *out_count
= dri2_surf
->buffer_count
;
280 if (dri2_surf
->buffer_count
== 0)
283 *width
= dri2_surf
->base
.Width
;
284 *height
= dri2_surf
->base
.Height
;
286 return dri2_surf
->buffers
;
290 dri2_get_buffers(__DRIdrawable
* driDrawable
,
291 int *width
, int *height
,
292 unsigned int *attachments
, int count
,
293 int *out_count
, void *loaderPrivate
)
295 unsigned int *attachments_with_format
;
297 const unsigned int format
= 32;
300 attachments_with_format
= calloc(count
* 2, sizeof(unsigned int));
301 if (!attachments_with_format
) {
306 for (i
= 0; i
< count
; ++i
) {
307 attachments_with_format
[2*i
] = attachments
[i
];
308 attachments_with_format
[2*i
+ 1] = format
;
312 dri2_get_buffers_with_format(driDrawable
,
314 attachments_with_format
, count
,
315 out_count
, loaderPrivate
);
317 free(attachments_with_format
);
324 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
328 /* FIXME: Does EGL support front buffer rendering at all? */
331 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
333 dri2WaitGL(dri2_surf
);
335 (void) loaderPrivate
;
339 static struct wl_buffer
*
340 wayland_create_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
342 struct dri2_egl_display
*dri2_dpy
=
343 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
345 return wl_drm_create_buffer(dri2_dpy
->wl_dpy
->drm
, buffer
->name
,
346 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
347 buffer
->pitch
, dri2_surf
->wl_win
->visual
);
351 wayland_frame_callback(void *data
, uint32_t time
)
353 struct dri2_egl_surface
*dri2_surf
= data
;
355 dri2_surf
->block_swap_buffers
= EGL_FALSE
;
359 pointer_swap(const void **p1
, const void **p2
)
361 const void *tmp
= *p1
;
367 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
370 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
372 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
373 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
374 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
376 while (dri2_surf
->block_swap_buffers
)
377 wl_display_iterate(dri2_dpy
->wl_dpy
->display
, WL_DISPLAY_READABLE
);
379 dri2_surf
->block_swap_buffers
= EGL_TRUE
;
380 wl_display_frame_callback(dri2_dpy
->wl_dpy
->display
,
381 wayland_frame_callback
, dri2_surf
);
383 if (dri2_surf
->type
== DRI2_WINDOW_SURFACE
) {
385 (const void **) &dri2_surf
->dri_buffers
[__DRI_BUFFER_FRONT_LEFT
],
386 (const void **) &dri2_surf
->dri_buffers
[__DRI_BUFFER_BACK_LEFT
]);
388 dri2_surf
->dri_buffers
[__DRI_BUFFER_FRONT_LEFT
]->attachment
=
389 __DRI_BUFFER_FRONT_LEFT
;
390 dri2_surf
->dri_buffers
[__DRI_BUFFER_BACK_LEFT
]->attachment
=
391 __DRI_BUFFER_BACK_LEFT
;
393 pointer_swap((const void **) &dri2_surf
->wl_drm_buffer
[WL_BUFFER_FRONT
],
394 (const void **) &dri2_surf
->wl_drm_buffer
[WL_BUFFER_BACK
]);
396 if (!dri2_surf
->wl_drm_buffer
[WL_BUFFER_FRONT
])
397 dri2_surf
->wl_drm_buffer
[WL_BUFFER_FRONT
] =
398 wayland_create_buffer(dri2_surf
,
399 dri2_surf
->dri_buffers
[__DRI_BUFFER_BACK_LEFT
]);
401 wl_surface_attach(dri2_surf
->wl_win
->surface
,
402 dri2_surf
->wl_drm_buffer
[WL_BUFFER_FRONT
],
403 dri2_surf
->dx
, dri2_surf
->dy
);
405 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
406 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
407 /* reset resize growing parameters */
411 wl_surface_damage(dri2_surf
->wl_win
->surface
, 0, 0,
412 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
416 if (dri2_drv
->glFlush
) {
417 ctx
= _eglGetCurrentContext();
418 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
422 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
423 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
429 * Called via eglCreateImageKHR(), drv->API.CreateImageKHR().
432 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
433 EGLClientBuffer buffer
, const EGLint
*attr_list
)
435 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
436 struct wl_egl_pixmap
*wl_egl_pixmap
= (struct wl_egl_pixmap
*) buffer
;
437 struct dri2_egl_buffer
*dri2_buf
;
438 EGLint wl_attr_list
[] = {
441 EGL_DRM_BUFFER_STRIDE_MESA
, 0,
442 EGL_DRM_BUFFER_FORMAT_MESA
, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
,
446 dri2_buf
= malloc(sizeof *dri2_buf
);
450 dri2_buf
->dri2_dpy
= dri2_dpy
;
451 dri2_buf
->dri_buffer
=
452 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
453 __DRI_BUFFER_FRONT_LEFT
, 32,
454 wl_egl_pixmap
->width
,
455 wl_egl_pixmap
->height
);
457 wl_egl_pixmap
->name
= dri2_buf
->dri_buffer
->name
;
458 wl_egl_pixmap
->stride
= dri2_buf
->dri_buffer
->pitch
;
459 wl_egl_pixmap
->destroy
= dri2_wl_egl_pixmap_destroy
;
460 wl_egl_pixmap
->driver_private
= dri2_buf
;
462 wl_attr_list
[1] = wl_egl_pixmap
->width
;
463 wl_attr_list
[3] = wl_egl_pixmap
->height
;
464 wl_attr_list
[5] = wl_egl_pixmap
->stride
/ 4;
467 return dri2_create_image_khr(disp
->Driver
, disp
, ctx
, EGL_DRM_BUFFER_MESA
,
468 (EGLClientBuffer
)(intptr_t) wl_egl_pixmap
->name
, wl_attr_list
);
472 dri2_wayland_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
473 _EGLContext
*ctx
, EGLenum target
,
474 EGLClientBuffer buffer
, const EGLint
*attr_list
)
479 case EGL_NATIVE_PIXMAP_KHR
:
480 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
482 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
487 * Called via eglTerminate(), drv->API.Terminate().
490 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
492 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
494 _eglReleaseDisplayResources(drv
, disp
);
495 _eglCleanupDisplay(disp
);
497 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
499 dlclose(dri2_dpy
->driver
);
500 free(dri2_dpy
->driver_name
);
502 disp
->DriverData
= NULL
;
508 sync_callback(void *data
)
516 force_roundtrip(struct wl_display
*display
)
520 wl_display_sync_callback(display
, sync_callback
, &done
);
521 wl_display_iterate(display
, WL_DISPLAY_WRITABLE
);
523 wl_display_iterate(display
, WL_DISPLAY_READABLE
);
527 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
529 struct dri2_egl_display
*dri2_dpy
;
532 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
533 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
534 drv
->API
.DestroySurface
= dri2_destroy_surface
;
535 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
536 drv
->API
.CreateImageKHR
= dri2_wayland_create_image_khr
;
537 drv
->API
.Terminate
= dri2_terminate
;
539 dri2_dpy
= malloc(sizeof *dri2_dpy
);
541 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
543 disp
->DriverData
= (void *) dri2_dpy
;
544 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
546 if (dri2_dpy
->wl_dpy
->fd
== -1)
547 force_roundtrip(dri2_dpy
->wl_dpy
->display
);
548 if (dri2_dpy
->wl_dpy
->fd
== -1)
551 dri2_dpy
->fd
= dup(dri2_dpy
->wl_dpy
->fd
);
552 if (dri2_dpy
->fd
< 0) {
553 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to dup fd");
557 if (!dri2_dpy
->wl_dpy
->authenticated
)
558 force_roundtrip(dri2_dpy
->wl_dpy
->display
);
559 if (!dri2_dpy
->wl_dpy
->authenticated
)
562 dri2_dpy
->driver_name
= dri2_get_driver_for_fd(dri2_dpy
->fd
);
563 if (dri2_dpy
->driver_name
== NULL
) {
564 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
568 if (!dri2_load_driver(disp
))
569 goto cleanup_driver_name
;
571 dri2_dpy
->loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
572 dri2_dpy
->loader_extension
.base
.version
= 3;
573 dri2_dpy
->loader_extension
.getBuffers
= dri2_get_buffers
;
574 dri2_dpy
->loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
575 dri2_dpy
->loader_extension
.getBuffersWithFormat
=
576 dri2_get_buffers_with_format
;
578 dri2_dpy
->extensions
[0] = &dri2_dpy
->loader_extension
.base
;
579 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
580 dri2_dpy
->extensions
[2] = NULL
;
582 if (!dri2_create_screen(disp
))
585 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
586 dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
], i
+ 1, 0,
587 EGL_WINDOW_BIT
| EGL_PIXMAP_BIT
);
590 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
591 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
592 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
593 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
594 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
596 /* we're supporting EGL 1.4 */
597 disp
->VersionMajor
= 1;
598 disp
->VersionMinor
= 4;
603 dlclose(dri2_dpy
->driver
);
605 free(dri2_dpy
->driver_name
);