egl/gbm: Implement EGL_EXT_buffer_age
[mesa.git] / src / egl / drivers / dri2 / platform_drm.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <xf86drm.h>
32 #include <dlfcn.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include "egl_dri2.h"
39
40 static struct gbm_bo *
41 lock_front_buffer(struct gbm_surface *_surf)
42 {
43 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
44 struct dri2_egl_surface *dri2_surf = surf->dri_private;
45 struct gbm_bo *bo;
46
47 if (dri2_surf->current == NULL) {
48 _eglError(EGL_BAD_SURFACE, "no front buffer");
49 return NULL;
50 }
51
52 bo = dri2_surf->current->bo;
53 dri2_surf->current->locked = 1;
54 dri2_surf->current = NULL;
55
56 return bo;
57 }
58
59 static void
60 release_buffer(struct gbm_surface *_surf, struct gbm_bo *bo)
61 {
62 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
63 struct dri2_egl_surface *dri2_surf = surf->dri_private;
64 int i;
65
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;
69 }
70 }
71 }
72
73 static int
74 has_free_buffers(struct gbm_surface *_surf)
75 {
76 struct gbm_dri_surface *surf = (struct gbm_dri_surface *) _surf;
77 struct dri2_egl_surface *dri2_surf = surf->dri_private;
78 int i;
79
80 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
81 if (!dri2_surf->color_buffers[i].locked)
82 return 1;
83
84 return 0;
85 }
86
87 static _EGLSurface *
88 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
89 _EGLConfig *conf, EGLNativeWindowType window,
90 const EGLint *attrib_list)
91 {
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;
96
97 (void) drv;
98
99 dri2_surf = calloc(1, sizeof *dri2_surf);
100 if (!dri2_surf) {
101 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
102 return NULL;
103 }
104
105 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
106 goto cleanup_surf;
107
108 switch (type) {
109 case EGL_WINDOW_BIT:
110 if (!window)
111 return NULL;
112 surf = gbm_dri_surface((struct gbm_surface *) window);
113 dri2_surf->gbm_surf = surf;
114 dri2_surf->base.Width = surf->base.width;
115 dri2_surf->base.Height = surf->base.height;
116 surf->dri_private = dri2_surf;
117 break;
118 default:
119 goto cleanup_surf;
120 }
121
122 dri2_surf->dri_drawable =
123 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
124 dri2_conf->dri_double_config,
125 dri2_surf->gbm_surf);
126
127 if (dri2_surf->dri_drawable == NULL) {
128 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
129 goto cleanup_surf;
130 }
131
132 return &dri2_surf->base;
133
134 cleanup_surf:
135 free(dri2_surf);
136
137 return NULL;
138 }
139
140 static _EGLSurface *
141 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
142 _EGLConfig *conf, EGLNativeWindowType window,
143 const EGLint *attrib_list)
144 {
145 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
146 window, attrib_list);
147 }
148
149 static EGLBoolean
150 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
151 {
152 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
153 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
154 int i;
155
156 if (!_eglPutSurface(surf))
157 return EGL_TRUE;
158
159 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
160
161 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
162 if (dri2_surf->color_buffers[i].bo)
163 gbm_bo_destroy(dri2_surf->color_buffers[i].bo);
164 }
165
166 for (i = 0; i < __DRI_BUFFER_COUNT; i++) {
167 if (dri2_surf->dri_buffers[i])
168 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
169 dri2_surf->dri_buffers[i]);
170 }
171
172 free(surf);
173
174 return EGL_TRUE;
175 }
176
177 static int
178 get_back_bo(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
179 {
180 struct dri2_egl_display *dri2_dpy =
181 dri2_egl_display(dri2_surf->base.Resource.Display);
182 struct gbm_dri_bo *bo;
183 struct gbm_dri_surface *surf = dri2_surf->gbm_surf;
184 int i, name, pitch;
185
186 if (dri2_surf->back == NULL) {
187 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
188 if (!dri2_surf->color_buffers[i].locked) {
189 dri2_surf->back = &dri2_surf->color_buffers[i];
190 break;
191 }
192 }
193 }
194
195 if (dri2_surf->back == NULL)
196 return -1;
197 if (dri2_surf->back->bo == NULL)
198 dri2_surf->back->bo = gbm_bo_create(&dri2_dpy->gbm_dri->base.base,
199 surf->base.width, surf->base.height,
200 surf->base.format, surf->base.flags);
201 if (dri2_surf->back->bo == NULL)
202 return -1;
203
204 bo = (struct gbm_dri_bo *) dri2_surf->back->bo;
205
206 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_NAME, &name);
207 dri2_dpy->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
208
209 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
210 buffer->name = name;
211 buffer->pitch = pitch;
212 buffer->cpp = 4;
213 buffer->flags = 0;
214
215 return 0;
216 }
217
218 static int
219 get_aux_bo(struct dri2_egl_surface *dri2_surf,
220 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
221 {
222 struct dri2_egl_display *dri2_dpy =
223 dri2_egl_display(dri2_surf->base.Resource.Display);
224 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
225
226 if (b == NULL) {
227 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
228 attachment, format,
229 dri2_surf->base.Width,
230 dri2_surf->base.Height);
231 dri2_surf->dri_buffers[attachment] = b;
232 }
233 if (b == NULL)
234 return -1;
235
236 memcpy(buffer, b, sizeof *buffer);
237
238 return 0;
239 }
240
241 static __DRIbuffer *
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)
246 {
247 struct dri2_egl_surface *dri2_surf = loaderPrivate;
248 int i, j;
249
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);
254
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");
259 return NULL;
260 }
261 break;
262 default:
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");
266 return NULL;
267 }
268 break;
269 }
270 }
271
272 *out_count = j;
273 if (j == 0)
274 return NULL;
275
276 *width = dri2_surf->base.Width;
277 *height = dri2_surf->base.Height;
278
279 return dri2_surf->buffers;
280 }
281
282 static __DRIbuffer *
283 dri2_get_buffers(__DRIdrawable * driDrawable,
284 int *width, int *height,
285 unsigned int *attachments, int count,
286 int *out_count, void *loaderPrivate)
287 {
288 unsigned int *attachments_with_format;
289 __DRIbuffer *buffer;
290 const unsigned int format = 32;
291 int i;
292
293 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
294 if (!attachments_with_format) {
295 *out_count = 0;
296 return NULL;
297 }
298
299 for (i = 0; i < count; ++i) {
300 attachments_with_format[2*i] = attachments[i];
301 attachments_with_format[2*i + 1] = format;
302 }
303
304 buffer =
305 dri2_get_buffers_with_format(driDrawable,
306 width, height,
307 attachments_with_format, count,
308 out_count, loaderPrivate);
309
310 free(attachments_with_format);
311
312 return buffer;
313 }
314
315 static void
316 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
317 {
318 (void) driDrawable;
319 (void) loaderPrivate;
320 }
321
322 static EGLBoolean
323 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
324 {
325 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
326 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
327 int i;
328
329 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
330 if (dri2_surf->current)
331 _eglError(EGL_BAD_SURFACE, "dri2_swap_buffers");
332 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
333 if (dri2_surf->color_buffers[i].age > 0)
334 dri2_surf->color_buffers[i].age++;
335 dri2_surf->current = dri2_surf->back;
336 dri2_surf->current->age = 1;
337 dri2_surf->back = NULL;
338 }
339
340 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
341 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
342
343 return EGL_TRUE;
344 }
345
346 static EGLint
347 dri2_query_buffer_age(_EGLDriver *drv,
348 _EGLDisplay *disp, _EGLSurface *surface)
349 {
350 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
351 __DRIbuffer buffer;
352
353 if (get_back_bo(dri2_surf, &buffer) < 0) {
354 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
355 return 0;
356 }
357
358 return dri2_surf->back->age;
359 }
360
361 static _EGLImage *
362 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
363 EGLClientBuffer buffer, const EGLint *attr_list)
364 {
365 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
366 struct gbm_dri_bo *dri_bo = gbm_dri_bo((struct gbm_bo *) buffer);
367 struct dri2_egl_image *dri2_img;
368
369 dri2_img = malloc(sizeof *dri2_img);
370 if (!dri2_img) {
371 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
372 return NULL;
373 }
374
375 if (!_eglInitImage(&dri2_img->base, disp)) {
376 free(dri2_img);
377 return NULL;
378 }
379
380 dri2_img->dri_image = dri2_dpy->image->dupImage(dri_bo->image, dri2_img);
381 if (dri2_img->dri_image == NULL) {
382 free(dri2_img);
383 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr_pixmap");
384 return NULL;
385 }
386
387 return &dri2_img->base;
388 }
389
390 static _EGLImage *
391 dri2_drm_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
392 _EGLContext *ctx, EGLenum target,
393 EGLClientBuffer buffer, const EGLint *attr_list)
394 {
395 (void) drv;
396
397 switch (target) {
398 case EGL_NATIVE_PIXMAP_KHR:
399 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
400 default:
401 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
402 }
403 }
404
405 static int
406 dri2_drm_authenticate(_EGLDisplay *disp, uint32_t id)
407 {
408 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
409
410 return drmAuthMagic(dri2_dpy->fd, id);
411 }
412
413 EGLBoolean
414 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
415 {
416 struct dri2_egl_display *dri2_dpy;
417 struct gbm_device *gbm;
418 int fd = -1;
419 int i;
420
421 dri2_dpy = calloc(1, sizeof *dri2_dpy);
422 if (!dri2_dpy)
423 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
424
425 disp->DriverData = (void *) dri2_dpy;
426
427 gbm = disp->PlatformDisplay;
428 if (gbm == NULL) {
429 fd = open("/dev/dri/card0", O_RDWR);
430 dri2_dpy->own_device = 1;
431 gbm = gbm_create_device(fd);
432 if (gbm == NULL)
433 return EGL_FALSE;
434 }
435
436 if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
437 free(dri2_dpy);
438 return EGL_FALSE;
439 }
440
441 dri2_dpy->gbm_dri = gbm_dri_device(gbm);
442 if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI) {
443 free(dri2_dpy);
444 return EGL_FALSE;
445 }
446
447 if (fd < 0) {
448 fd = dup(gbm_device_get_fd(gbm));
449 if (fd < 0) {
450 free(dri2_dpy);
451 return EGL_FALSE;
452 }
453 }
454
455 dri2_dpy->fd = fd;
456 dri2_dpy->device_name = dri2_get_device_name_for_fd(dri2_dpy->fd);
457 dri2_dpy->driver_name = dri2_dpy->gbm_dri->base.driver_name;
458
459 dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
460 dri2_dpy->core = dri2_dpy->gbm_dri->core;
461 dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
462 dri2_dpy->image = dri2_dpy->gbm_dri->image;
463 dri2_dpy->flush = dri2_dpy->gbm_dri->flush;
464 dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;
465
466 dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
467 dri2_dpy->gbm_dri->lookup_user_data = disp;
468
469 dri2_dpy->gbm_dri->get_buffers = dri2_get_buffers;
470 dri2_dpy->gbm_dri->flush_front_buffer = dri2_flush_front_buffer;
471 dri2_dpy->gbm_dri->get_buffers_with_format = dri2_get_buffers_with_format;
472
473 dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer;
474 dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer;
475 dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers;
476
477 dri2_setup_screen(disp);
478
479 for (i = 0; dri2_dpy->driver_configs[i]; i++)
480 dri2_add_config(disp, dri2_dpy->driver_configs[i],
481 i + 1, 0, EGL_WINDOW_BIT, NULL, NULL);
482
483 drv->API.CreateWindowSurface = dri2_create_window_surface;
484 drv->API.DestroySurface = dri2_destroy_surface;
485 drv->API.SwapBuffers = dri2_swap_buffers;
486 drv->API.CreateImageKHR = dri2_drm_create_image_khr;
487 drv->API.QueryBufferAge = dri2_query_buffer_age;
488
489 disp->Extensions.EXT_buffer_age = EGL_TRUE;
490
491 #ifdef HAVE_WAYLAND_PLATFORM
492 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
493 #endif
494 dri2_dpy->authenticate = dri2_drm_authenticate;
495
496 /* we're supporting EGL 1.4 */
497 disp->VersionMajor = 1;
498 disp->VersionMinor = 4;
499
500 return EGL_TRUE;
501 }