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
))
113 surf
= gbm_dri_surface((struct gbm_surface
*) window
);
114 dri2_surf
->gbm_surf
= surf
;
115 dri2_surf
->base
.Width
= surf
->base
.width
;
116 dri2_surf
->base
.Height
= surf
->base
.height
;
117 surf
->dri_private
= dri2_surf
;
123 dri2_surf
->dri_drawable
=
124 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
125 dri2_conf
->dri_double_config
,
126 dri2_surf
->gbm_surf
);
128 if (dri2_surf
->dri_drawable
== NULL
) {
129 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
133 return &dri2_surf
->base
;
142 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
143 _EGLConfig
*conf
, EGLNativeWindowType window
,
144 const EGLint
*attrib_list
)
146 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
147 window
, attrib_list
);
151 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
153 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
154 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
157 if (!_eglPutSurface(surf
))
160 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
162 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
163 if (dri2_surf
->color_buffers
[i
].bo
)
164 gbm_bo_destroy(dri2_surf
->color_buffers
[i
].bo
);
167 for (i
= 0; i
< __DRI_BUFFER_COUNT
; i
++) {
168 if (dri2_surf
->dri_buffers
[i
])
169 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
170 dri2_surf
->dri_buffers
[i
]);
179 get_back_bo(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
181 struct dri2_egl_display
*dri2_dpy
=
182 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
183 struct gbm_dri_bo
*bo
;
184 struct gbm_dri_surface
*surf
= dri2_surf
->gbm_surf
;
187 if (dri2_surf
->back
== NULL
) {
188 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
189 if (!dri2_surf
->color_buffers
[i
].locked
) {
190 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
196 if (dri2_surf
->back
== NULL
)
198 if (dri2_surf
->back
->bo
== NULL
)
199 dri2_surf
->back
->bo
= gbm_bo_create(&dri2_dpy
->gbm_dri
->base
.base
,
200 surf
->base
.width
, surf
->base
.height
,
201 surf
->base
.format
, surf
->base
.flags
);
202 if (dri2_surf
->back
->bo
== NULL
)
205 bo
= (struct gbm_dri_bo
*) dri2_surf
->back
->bo
;
207 dri2_dpy
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
208 dri2_dpy
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
210 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
212 buffer
->pitch
= pitch
;
220 get_aux_bo(struct dri2_egl_surface
*dri2_surf
,
221 unsigned int attachment
, unsigned int format
, __DRIbuffer
*buffer
)
223 struct dri2_egl_display
*dri2_dpy
=
224 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
225 __DRIbuffer
*b
= dri2_surf
->dri_buffers
[attachment
];
228 b
= dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
,
230 dri2_surf
->base
.Width
,
231 dri2_surf
->base
.Height
);
232 dri2_surf
->dri_buffers
[attachment
] = b
;
237 memcpy(buffer
, b
, sizeof *buffer
);
243 dri2_get_buffers_with_format(__DRIdrawable
*driDrawable
,
244 int *width
, int *height
,
245 unsigned int *attachments
, int count
,
246 int *out_count
, void *loaderPrivate
)
248 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
251 dri2_surf
->buffer_count
= 0;
252 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
253 assert(attachments
[i
] < __DRI_BUFFER_COUNT
);
254 assert(dri2_surf
->buffer_count
< 5);
256 switch (attachments
[i
]) {
257 case __DRI_BUFFER_BACK_LEFT
:
258 if (get_back_bo(dri2_surf
, &dri2_surf
->buffers
[j
]) < 0) {
259 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
264 if (get_aux_bo(dri2_surf
, attachments
[i
], attachments
[i
+ 1],
265 &dri2_surf
->buffers
[j
]) < 0) {
266 _eglError(EGL_BAD_ALLOC
, "failed to allocate aux buffer");
277 *width
= dri2_surf
->base
.Width
;
278 *height
= dri2_surf
->base
.Height
;
280 return dri2_surf
->buffers
;
284 dri2_get_buffers(__DRIdrawable
* driDrawable
,
285 int *width
, int *height
,
286 unsigned int *attachments
, int count
,
287 int *out_count
, void *loaderPrivate
)
289 unsigned int *attachments_with_format
;
291 const unsigned int format
= 32;
294 attachments_with_format
= calloc(count
* 2, sizeof(unsigned int));
295 if (!attachments_with_format
) {
300 for (i
= 0; i
< count
; ++i
) {
301 attachments_with_format
[2*i
] = attachments
[i
];
302 attachments_with_format
[2*i
+ 1] = format
;
306 dri2_get_buffers_with_format(driDrawable
,
308 attachments_with_format
, count
,
309 out_count
, loaderPrivate
);
311 free(attachments_with_format
);
317 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
320 (void) loaderPrivate
;
324 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
326 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
327 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
329 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
330 if (dri2_surf
->current
)
331 _eglError(EGL_BAD_SURFACE
, "dri2_swap_buffers");
332 dri2_surf
->current
= dri2_surf
->back
;
333 dri2_surf
->back
= NULL
;
336 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
337 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
343 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
344 EGLClientBuffer buffer
, const EGLint
*attr_list
)
346 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
347 struct gbm_dri_bo
*dri_bo
= gbm_dri_bo((struct gbm_bo
*) buffer
);
348 struct dri2_egl_image
*dri2_img
;
350 dri2_img
= malloc(sizeof *dri2_img
);
352 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr_pixmap");
356 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
361 dri2_img
->dri_image
= dri2_dpy
->image
->dupImage(dri_bo
->image
, dri2_img
);
362 if (dri2_img
->dri_image
== NULL
) {
364 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr_pixmap");
368 return &dri2_img
->base
;
372 dri2_drm_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
373 _EGLContext
*ctx
, EGLenum target
,
374 EGLClientBuffer buffer
, const EGLint
*attr_list
)
379 case EGL_NATIVE_PIXMAP_KHR
:
380 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
382 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
387 dri2_drm_authenticate(_EGLDisplay
*disp
, uint32_t id
)
389 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
391 return drmAuthMagic(dri2_dpy
->fd
, id
);
395 dri2_initialize_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
397 struct dri2_egl_display
*dri2_dpy
;
398 struct gbm_device
*gbm
;
402 dri2_dpy
= malloc(sizeof *dri2_dpy
);
404 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
406 memset(dri2_dpy
, 0, sizeof *dri2_dpy
);
408 disp
->DriverData
= (void *) dri2_dpy
;
410 gbm
= disp
->PlatformDisplay
;
412 fd
= open("/dev/dri/card0", O_RDWR
);
413 dri2_dpy
->own_device
= 1;
414 gbm
= gbm_create_device(fd
);
419 if (strcmp(gbm_device_get_backend_name(gbm
), "drm") != 0) {
424 dri2_dpy
->gbm_dri
= gbm_dri_device(gbm
);
425 if (dri2_dpy
->gbm_dri
->base
.type
!= GBM_DRM_DRIVER_TYPE_DRI
) {
431 fd
= dup(gbm_device_get_fd(gbm
));
439 dri2_dpy
->device_name
= dri2_get_device_name_for_fd(dri2_dpy
->fd
);
440 dri2_dpy
->driver_name
= dri2_dpy
->gbm_dri
->base
.driver_name
;
442 dri2_dpy
->dri_screen
= dri2_dpy
->gbm_dri
->screen
;
443 dri2_dpy
->core
= dri2_dpy
->gbm_dri
->core
;
444 dri2_dpy
->dri2
= dri2_dpy
->gbm_dri
->dri2
;
445 dri2_dpy
->image
= dri2_dpy
->gbm_dri
->image
;
446 dri2_dpy
->flush
= dri2_dpy
->gbm_dri
->flush
;
447 dri2_dpy
->driver_configs
= dri2_dpy
->gbm_dri
->driver_configs
;
449 dri2_dpy
->gbm_dri
->lookup_image
= dri2_lookup_egl_image
;
450 dri2_dpy
->gbm_dri
->lookup_user_data
= disp
;
452 dri2_dpy
->gbm_dri
->get_buffers
= dri2_get_buffers
;
453 dri2_dpy
->gbm_dri
->flush_front_buffer
= dri2_flush_front_buffer
;
454 dri2_dpy
->gbm_dri
->get_buffers_with_format
= dri2_get_buffers_with_format
;
456 dri2_dpy
->gbm_dri
->base
.base
.surface_lock_front_buffer
= lock_front_buffer
;
457 dri2_dpy
->gbm_dri
->base
.base
.surface_release_buffer
= release_buffer
;
458 dri2_dpy
->gbm_dri
->base
.base
.surface_has_free_buffers
= has_free_buffers
;
460 dri2_setup_screen(disp
);
462 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
463 dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
464 i
+ 1, 0, EGL_WINDOW_BIT
, NULL
, NULL
);
466 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
467 drv
->API
.DestroySurface
= dri2_destroy_surface
;
468 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
469 drv
->API
.CreateImageKHR
= dri2_drm_create_image_khr
;
471 #ifdef HAVE_WAYLAND_PLATFORM
472 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
474 dri2_dpy
->authenticate
= dri2_drm_authenticate
;
476 /* we're supporting EGL 1.4 */
477 disp
->VersionMajor
= 1;
478 disp
->VersionMinor
= 4;