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>
33 #include <sys/types.h>
40 static struct gbm_bo
*
41 lock_front_buffer(struct gbm_surface
*_surf
)
43 struct gbm_dri_surface
*surf
= (struct gbm_dri_surface
*) _surf
;
44 struct dri2_egl_surface
*dri2_surf
= surf
->dri_private
;
47 if (dri2_surf
->current
== NULL
) {
48 _eglError(EGL_BAD_SURFACE
, "no front buffer");
52 bo
= dri2_surf
->current
->bo
;
53 dri2_surf
->current
->locked
= 1;
54 dri2_surf
->current
= NULL
;
60 release_buffer(struct gbm_surface
*_surf
, struct gbm_bo
*bo
)
62 struct gbm_dri_surface
*surf
= (struct gbm_dri_surface
*) _surf
;
63 struct dri2_egl_surface
*dri2_surf
= surf
->dri_private
;
66 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
67 if (dri2_surf
->color_buffers
[i
].bo
== bo
) {
68 dri2_surf
->color_buffers
[i
].locked
= 0;
74 has_free_buffers(struct gbm_surface
*_surf
)
76 struct gbm_dri_surface
*surf
= (struct gbm_dri_surface
*) _surf
;
77 struct dri2_egl_surface
*dri2_surf
= surf
->dri_private
;
80 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
81 if (!dri2_surf
->color_buffers
[i
].locked
)
88 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
89 _EGLConfig
*conf
, EGLNativeWindowType window
,
90 const EGLint
*attrib_list
)
92 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
93 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
94 struct dri2_egl_surface
*dri2_surf
;
95 struct gbm_dri_surface
*surf
;
99 dri2_surf
= malloc(sizeof *dri2_surf
);
101 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
105 memset(dri2_surf
, 0, sizeof *dri2_surf
);
106 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
111 surf
= gbm_dri_surface((struct gbm_surface
*) window
);
112 dri2_surf
->gbm_surf
= surf
;
113 dri2_surf
->base
.Width
= surf
->base
.width
;
114 dri2_surf
->base
.Height
= surf
->base
.height
;
115 surf
->dri_private
= dri2_surf
;
121 dri2_surf
->dri_drawable
=
122 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
123 dri2_conf
->dri_double_config
,
124 dri2_surf
->gbm_surf
);
126 if (dri2_surf
->dri_drawable
== NULL
) {
127 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
131 return &dri2_surf
->base
;
140 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
141 _EGLConfig
*conf
, EGLNativeWindowType window
,
142 const EGLint
*attrib_list
)
144 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
145 window
, attrib_list
);
149 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
151 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
152 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
155 if (!_eglPutSurface(surf
))
158 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
160 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
161 if (dri2_surf
->color_buffers
[i
].bo
)
162 gbm_bo_destroy(dri2_surf
->color_buffers
[i
].bo
);
165 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++) {
166 if (dri2_surf
->dri_buffers
[i
])
167 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
168 dri2_surf
->dri_buffers
[i
]);
177 get_back_bo(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
179 struct dri2_egl_display
*dri2_dpy
=
180 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
181 struct gbm_dri_bo
*bo
;
182 struct gbm_dri_surface
*surf
= dri2_surf
->gbm_surf
;
185 if (dri2_surf
->back
== NULL
) {
186 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
187 if (!dri2_surf
->color_buffers
[i
].locked
) {
188 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
194 if (dri2_surf
->back
== NULL
)
196 if (dri2_surf
->back
->bo
== NULL
)
197 dri2_surf
->back
->bo
= gbm_bo_create(&dri2_dpy
->gbm_dri
->base
.base
,
198 surf
->base
.width
, surf
->base
.height
,
199 surf
->base
.format
, surf
->base
.flags
);
200 if (dri2_surf
->back
->bo
== NULL
)
203 bo
= (struct gbm_dri_bo
*) dri2_surf
->back
->bo
;
205 dri2_dpy
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
206 dri2_dpy
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
208 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
210 buffer
->pitch
= pitch
;
218 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
219 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
221 struct dri2_egl_display
*dri2_dpy
=
222 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
226 if (dri2_surf
->dri_buffers
[attachment
])
227 b
= dri2_surf
->dri_buffers
[attachment
];
229 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
231 dri2_surf
->base
.Width
,
232 dri2_surf
->base
.Height
);
236 memcpy(buffer
, b
, sizeof *buffer
);
242 dri2_get_buffers_with_format(__DRIdrawable
*driDrawable
,
243 int *width
, int *height
,
244 unsigned int *attachments
, int count
,
245 int *out_count
, void *loaderPrivate
)
247 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
250 dri2_surf
->buffer_count
= 0;
251 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
252 assert(attachments
[i
] < __DRI_BUFFER_COUNT
);
253 assert(dri2_surf
->buffer_count
< 5);
255 switch (attachments
[i
]) {
256 case __DRI_BUFFER_BACK_LEFT
:
257 if (get_back_bo(dri2_surf
, &dri2_surf
->buffers
[j
]) < 0) {
258 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
263 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
264 &dri2_surf
->buffers
[j
]) < 0) {
265 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
276 *width
= dri2_surf
->base
.Width
;
277 *height
= dri2_surf
->base
.Height
;
279 return dri2_surf
->buffers
;
283 dri2_get_buffers(__DRIdrawable
* driDrawable
,
284 int *width
, int *height
,
285 unsigned int *attachments
, int count
,
286 int *out_count
, void *loaderPrivate
)
288 unsigned int *attachments_with_format
;
290 const unsigned int format
= 32;
293 attachments_with_format
= calloc(count
* 2, sizeof(unsigned int));
294 if (!attachments_with_format
) {
299 for (i
= 0; i
< count
; ++i
) {
300 attachments_with_format
[2*i
] = attachments
[i
];
301 attachments_with_format
[2*i
+ 1] = format
;
305 dri2_get_buffers_with_format(driDrawable
,
307 attachments_with_format
, count
,
308 out_count
, loaderPrivate
);
310 free(attachments_with_format
);
316 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
319 (void) loaderPrivate
;
323 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
325 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
326 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
328 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
329 if (dri2_surf
->current
)
330 _eglError(EGL_BAD_SURFACE
, "dri2_swap_buffers");
331 dri2_surf
->current
= dri2_surf
->back
;
332 dri2_surf
->back
= NULL
;
335 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
336 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
342 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
343 EGLClientBuffer buffer
, const EGLint
*attr_list
)
345 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
346 struct gbm_dri_bo
*dri_bo
= gbm_dri_bo((struct gbm_bo
*) buffer
);
347 struct dri2_egl_image
*dri2_img
;
349 dri2_img
= malloc(sizeof *dri2_img
);
351 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr_pixmap");
355 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
360 dri2_img
->dri_image
= dri2_dpy
->image
->dupImage(dri_bo
->image
, dri2_img
);
361 if (dri2_img
->dri_image
== NULL
) {
363 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr_pixmap");
367 return &dri2_img
->base
;
371 dri2_drm_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
372 _EGLContext
*ctx
, EGLenum target
,
373 EGLClientBuffer buffer
, const EGLint
*attr_list
)
378 case EGL_NATIVE_PIXMAP_KHR
:
379 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
381 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
386 dri2_drm_authenticate(_EGLDisplay
*disp
, uint32_t id
)
388 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
390 return drmAuthMagic(dri2_dpy
->fd
, id
);
394 dri2_initialize_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
396 struct dri2_egl_display
*dri2_dpy
;
397 struct gbm_device
*gbm
;
401 dri2_dpy
= malloc(sizeof *dri2_dpy
);
403 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
405 memset(dri2_dpy
, 0, sizeof *dri2_dpy
);
407 disp
->DriverData
= (void *) dri2_dpy
;
409 gbm
= disp
->PlatformDisplay
;
411 fd
= open("/dev/dri/card0", O_RDWR
);
412 dri2_dpy
->own_device
= 1;
413 gbm
= gbm_create_device(fd
);
418 if (strcmp(gbm_device_get_backend_name(gbm
), "drm") != 0) {
423 dri2_dpy
->gbm_dri
= gbm_dri_device(gbm
);
424 if (dri2_dpy
->gbm_dri
->base
.type
!= GBM_DRM_DRIVER_TYPE_DRI
) {
430 fd
= dup(gbm_device_get_fd(gbm
));
438 dri2_dpy
->device_name
= dri2_get_device_name_for_fd(dri2_dpy
->fd
);
439 dri2_dpy
->driver_name
= dri2_dpy
->gbm_dri
->base
.driver_name
;
441 dri2_dpy
->dri_screen
= dri2_dpy
->gbm_dri
->screen
;
442 dri2_dpy
->core
= dri2_dpy
->gbm_dri
->core
;
443 dri2_dpy
->dri2
= dri2_dpy
->gbm_dri
->dri2
;
444 dri2_dpy
->image
= dri2_dpy
->gbm_dri
->image
;
445 dri2_dpy
->flush
= dri2_dpy
->gbm_dri
->flush
;
446 dri2_dpy
->driver_configs
= dri2_dpy
->gbm_dri
->driver_configs
;
448 dri2_dpy
->gbm_dri
->lookup_image
= dri2_lookup_egl_image
;
449 dri2_dpy
->gbm_dri
->lookup_user_data
= disp
;
451 dri2_dpy
->gbm_dri
->get_buffers
= dri2_get_buffers
;
452 dri2_dpy
->gbm_dri
->flush_front_buffer
= dri2_flush_front_buffer
;
453 dri2_dpy
->gbm_dri
->get_buffers_with_format
= dri2_get_buffers_with_format
;
455 dri2_dpy
->gbm_dri
->base
.base
.surface_lock_front_buffer
= lock_front_buffer
;
456 dri2_dpy
->gbm_dri
->base
.base
.surface_release_buffer
= release_buffer
;
457 dri2_dpy
->gbm_dri
->base
.base
.surface_has_free_buffers
= has_free_buffers
;
459 dri2_setup_screen(disp
);
461 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
462 dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
463 i
+ 1, 0, EGL_WINDOW_BIT
, NULL
, NULL
);
465 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
466 drv
->API
.DestroySurface
= dri2_destroy_surface
;
467 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
468 drv
->API
.CreateImageKHR
= dri2_drm_create_image_khr
;
470 #ifdef HAVE_WAYLAND_PLATFORM
471 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
473 dri2_dpy
->authenticate
= dri2_drm_authenticate
;
475 /* we're supporting EGL 1.4 */
476 disp
->VersionMajor
= 1;
477 disp
->VersionMinor
= 4;