EGL/Android: Add EGL_EXT_buffer_age extension
[mesa.git] / src / egl / drivers / dri2 / platform_android.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright (C) 2010-2011 LunarG Inc.
6 *
7 * Based on platform_x11, which has
8 *
9 * Copyright © 2011 Intel Corporation
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29
30 #include <errno.h>
31 #include <dlfcn.h>
32 #include <fcntl.h>
33 #include <xf86drm.h>
34 #include <stdbool.h>
35
36 #if ANDROID_VERSION >= 0x402
37 #include <sync/sync.h>
38 #endif
39
40 #include "loader.h"
41 #include "egl_dri2.h"
42 #include "egl_dri2_fallbacks.h"
43 #include "gralloc_drm.h"
44
45 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
46
47 struct droid_yuv_format {
48 /* Lookup keys */
49 int native; /* HAL_PIXEL_FORMAT_ */
50 int is_ycrcb; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
51 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
52
53 /* Result */
54 int fourcc; /* __DRI_IMAGE_FOURCC_ */
55 };
56
57 /* The following table is used to look up a DRI image FourCC based
58 * on native format and information contained in android_ycbcr struct. */
59 static const struct droid_yuv_format droid_yuv_formats[] = {
60 /* Native format, YCrCb, Chroma step, DRI image FourCC */
61 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
62 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
63 { HAL_PIXEL_FORMAT_YCbCr_420_888, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
64 { HAL_PIXEL_FORMAT_YV12, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
65 };
66
67 static int
68 get_fourcc_yuv(int native, int is_ycrcb, int chroma_step)
69 {
70 int i;
71
72 for (i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
73 if (droid_yuv_formats[i].native == native &&
74 droid_yuv_formats[i].is_ycrcb == is_ycrcb &&
75 droid_yuv_formats[i].chroma_step == chroma_step)
76 return droid_yuv_formats[i].fourcc;
77
78 return -1;
79 }
80
81 static bool
82 is_yuv(int native)
83 {
84 int i;
85
86 for (i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
87 if (droid_yuv_formats[i].native == native)
88 return true;
89
90 return false;
91 }
92
93 static int
94 get_format_bpp(int native)
95 {
96 int bpp;
97
98 switch (native) {
99 case HAL_PIXEL_FORMAT_RGBA_8888:
100 case HAL_PIXEL_FORMAT_RGBX_8888:
101 case HAL_PIXEL_FORMAT_BGRA_8888:
102 bpp = 4;
103 break;
104 case HAL_PIXEL_FORMAT_RGB_565:
105 bpp = 2;
106 break;
107 default:
108 bpp = 0;
109 break;
110 }
111
112 return bpp;
113 }
114
115 /* createImageFromFds requires fourcc format */
116 static int get_fourcc(int native)
117 {
118 switch (native) {
119 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FOURCC_RGB565;
120 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FOURCC_ARGB8888;
121 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FOURCC_ABGR8888;
122 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FOURCC_XBGR8888;
123 default:
124 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
125 }
126 return -1;
127 }
128
129 static int get_format(int format)
130 {
131 switch (format) {
132 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
133 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
134 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
135 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
136 default:
137 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
138 }
139 return -1;
140 }
141
142 static int
143 get_native_buffer_fd(struct ANativeWindowBuffer *buf)
144 {
145 native_handle_t *handle = (native_handle_t *)buf->handle;
146 /*
147 * Various gralloc implementations exist, but the dma-buf fd tends
148 * to be first. Access it directly to avoid a dependency on specific
149 * gralloc versions.
150 */
151 return (handle && handle->numFds) ? handle->data[0] : -1;
152 }
153
154 static int
155 get_native_buffer_name(struct ANativeWindowBuffer *buf)
156 {
157 return gralloc_drm_get_gem_handle(buf->handle);
158 }
159
160 static EGLBoolean
161 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
162 {
163 #if ANDROID_VERSION >= 0x0402
164 int fence_fd;
165
166 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
167 &fence_fd))
168 return EGL_FALSE;
169
170 /* If access to the buffer is controlled by a sync fence, then block on the
171 * fence.
172 *
173 * It may be more performant to postpone blocking until there is an
174 * immediate need to write to the buffer. But doing so would require adding
175 * hooks to the DRI2 loader.
176 *
177 * From the ANativeWindow::dequeueBuffer documentation:
178 *
179 * The libsync fence file descriptor returned in the int pointed to by
180 * the fenceFd argument will refer to the fence that must signal
181 * before the dequeued buffer may be written to. A value of -1
182 * indicates that the caller may access the buffer immediately without
183 * waiting on a fence. If a valid file descriptor is returned (i.e.
184 * any value except -1) then the caller is responsible for closing the
185 * file descriptor.
186 */
187 if (fence_fd >= 0) {
188 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
189 *
190 * Waits indefinitely if timeout < 0.
191 */
192 int timeout = -1;
193 sync_wait(fence_fd, timeout);
194 close(fence_fd);
195 }
196
197 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
198 #else
199 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
200 return EGL_FALSE;
201
202 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
203 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
204 #endif
205
206 /* Record all the buffers created by ANativeWindow and update back buffer
207 * for updating buffer's age in swap_buffers.
208 */
209 EGLBoolean updated = EGL_FALSE;
210 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
211 if (!dri2_surf->color_buffers[i].buffer) {
212 dri2_surf->color_buffers[i].buffer = dri2_surf->buffer;
213 }
214 if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) {
215 dri2_surf->back = &dri2_surf->color_buffers[i];
216 updated = EGL_TRUE;
217 break;
218 }
219 }
220
221 if (!updated) {
222 /* In case of all the buffers were recreated by ANativeWindow, reset
223 * the color_buffers
224 */
225 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
226 dri2_surf->color_buffers[i].buffer = NULL;
227 dri2_surf->color_buffers[i].age = 0;
228 }
229 dri2_surf->color_buffers[0].buffer = dri2_surf->buffer;
230 dri2_surf->back = &dri2_surf->color_buffers[0];
231 }
232
233 return EGL_TRUE;
234 }
235
236 static EGLBoolean
237 droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
238 {
239 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
240
241 /* To avoid blocking other EGL calls, release the display mutex before
242 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
243 * return.
244 */
245 mtx_unlock(&disp->Mutex);
246
247 #if ANDROID_VERSION >= 0x0402
248 /* Queue the buffer without a sync fence. This informs the ANativeWindow
249 * that it may access the buffer immediately.
250 *
251 * From ANativeWindow::dequeueBuffer:
252 *
253 * The fenceFd argument specifies a libsync fence file descriptor for
254 * a fence that must signal before the buffer can be accessed. If
255 * the buffer can be accessed immediately then a value of -1 should
256 * be used. The caller must not use the file descriptor after it
257 * is passed to queueBuffer, and the ANativeWindow implementation
258 * is responsible for closing it.
259 */
260 int fence_fd = -1;
261 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
262 fence_fd);
263 #else
264 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
265 #endif
266
267 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
268 dri2_surf->buffer = NULL;
269 dri2_surf->back = NULL;
270
271 mtx_lock(&disp->Mutex);
272
273 if (dri2_surf->dri_image_back) {
274 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
275 dri2_surf->dri_image_back = NULL;
276 }
277
278 return EGL_TRUE;
279 }
280
281 static void
282 droid_window_cancel_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
283 {
284 /* no cancel buffer? */
285 droid_window_enqueue_buffer(disp, dri2_surf);
286 }
287
288 static __DRIbuffer *
289 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
290 unsigned int att, unsigned int format)
291 {
292 struct dri2_egl_display *dri2_dpy =
293 dri2_egl_display(dri2_surf->base.Resource.Display);
294
295 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
296 return NULL;
297
298 if (!dri2_surf->local_buffers[att]) {
299 dri2_surf->local_buffers[att] =
300 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
301 dri2_surf->base.Width, dri2_surf->base.Height);
302 }
303
304 return dri2_surf->local_buffers[att];
305 }
306
307 static void
308 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
309 {
310 struct dri2_egl_display *dri2_dpy =
311 dri2_egl_display(dri2_surf->base.Resource.Display);
312 int i;
313
314 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
315 if (dri2_surf->local_buffers[i]) {
316 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
317 dri2_surf->local_buffers[i]);
318 dri2_surf->local_buffers[i] = NULL;
319 }
320 }
321 }
322
323 static _EGLSurface *
324 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
325 _EGLConfig *conf, void *native_window,
326 const EGLint *attrib_list)
327 {
328 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
329 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
330 struct dri2_egl_surface *dri2_surf;
331 struct ANativeWindow *window = native_window;
332 const __DRIconfig *config;
333
334 dri2_surf = calloc(1, sizeof *dri2_surf);
335 if (!dri2_surf) {
336 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
337 return NULL;
338 }
339
340 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
341 goto cleanup_surface;
342
343 if (type == EGL_WINDOW_BIT) {
344 int format;
345
346 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
347 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
348 goto cleanup_surface;
349 }
350 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
351 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
352 goto cleanup_surface;
353 }
354
355 if (format != dri2_conf->base.NativeVisualID) {
356 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
357 format, dri2_conf->base.NativeVisualID);
358 }
359
360 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
361 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
362 }
363
364 config = dri2_get_dri_config(dri2_conf, type,
365 dri2_surf->base.GLColorspace);
366 if (!config)
367 goto cleanup_surface;
368
369 dri2_surf->dri_drawable =
370 dri2_dpy->dri2->createNewDrawable(dri2_dpy->dri_screen, config,
371 dri2_surf);
372 if (dri2_surf->dri_drawable == NULL) {
373 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
374 goto cleanup_surface;
375 }
376
377 if (window) {
378 window->common.incRef(&window->common);
379 dri2_surf->window = window;
380 }
381
382 return &dri2_surf->base;
383
384 cleanup_surface:
385 free(dri2_surf);
386
387 return NULL;
388 }
389
390 static _EGLSurface *
391 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
392 _EGLConfig *conf, void *native_window,
393 const EGLint *attrib_list)
394 {
395 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
396 native_window, attrib_list);
397 }
398
399 static _EGLSurface *
400 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
401 _EGLConfig *conf, const EGLint *attrib_list)
402 {
403 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
404 NULL, attrib_list);
405 }
406
407 static EGLBoolean
408 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
409 {
410 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
411 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
412
413 droid_free_local_buffers(dri2_surf);
414
415 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
416 if (dri2_surf->buffer)
417 droid_window_cancel_buffer(disp, dri2_surf);
418
419 dri2_surf->window->common.decRef(&dri2_surf->window->common);
420 }
421
422 if (dri2_surf->dri_image_back) {
423 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
424 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
425 dri2_surf->dri_image_back = NULL;
426 }
427
428 if (dri2_surf->dri_image_front) {
429 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
430 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
431 dri2_surf->dri_image_front = NULL;
432 }
433
434 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
435
436 free(dri2_surf);
437
438 return EGL_TRUE;
439 }
440
441 static int
442 update_buffers(struct dri2_egl_surface *dri2_surf)
443 {
444 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
445 return 0;
446
447 /* try to dequeue the next back buffer */
448 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
449 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
450 return -1;
451 }
452
453 /* free outdated buffers and update the surface size */
454 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
455 dri2_surf->base.Height != dri2_surf->buffer->height) {
456 droid_free_local_buffers(dri2_surf);
457 dri2_surf->base.Width = dri2_surf->buffer->width;
458 dri2_surf->base.Height = dri2_surf->buffer->height;
459 }
460
461 return 0;
462 }
463
464 static int
465 get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
466 {
467 struct dri2_egl_display *dri2_dpy =
468 dri2_egl_display(dri2_surf->base.Resource.Display);
469
470 if (dri2_surf->dri_image_front)
471 return 0;
472
473 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
474 /* According current EGL spec, front buffer rendering
475 * for window surface is not supported now.
476 * and mesa doesn't have the implementation of this case.
477 * Add warning message, but not treat it as error.
478 */
479 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
480 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
481 dri2_surf->dri_image_front =
482 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
483 dri2_surf->base.Width,
484 dri2_surf->base.Height,
485 format,
486 0,
487 dri2_surf);
488 if (!dri2_surf->dri_image_front) {
489 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
490 return -1;
491 }
492 }
493
494 return 0;
495 }
496
497 static int
498 get_back_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
499 {
500 struct dri2_egl_display *dri2_dpy =
501 dri2_egl_display(dri2_surf->base.Resource.Display);
502 int fourcc, pitch;
503 int offset = 0, fd;
504
505 if (dri2_surf->dri_image_back)
506 return 0;
507
508 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
509 if (!dri2_surf->buffer) {
510 _eglLog(_EGL_WARNING, "Could not get native buffer");
511 return -1;
512 }
513
514 fd = get_native_buffer_fd(dri2_surf->buffer);
515 if (fd < 0) {
516 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
517 return -1;
518 }
519
520 fourcc = get_fourcc(dri2_surf->buffer->format);
521
522 pitch = dri2_surf->buffer->stride *
523 get_format_bpp(dri2_surf->buffer->format);
524
525 if (fourcc == -1 || pitch == 0) {
526 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
527 fourcc, pitch);
528 return -1;
529 }
530
531 dri2_surf->dri_image_back =
532 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
533 dri2_surf->base.Width,
534 dri2_surf->base.Height,
535 fourcc,
536 &fd,
537 1,
538 &pitch,
539 &offset,
540 dri2_surf);
541 if (!dri2_surf->dri_image_back) {
542 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
543 return -1;
544 }
545 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
546 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
547 * the spec states that they have a back buffer but no front buffer, in
548 * contrast to pixmaps, which have a front buffer but no back buffer.
549 *
550 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
551 * from the spec, following the precedent of Mesa's EGL X11 platform. The
552 * X11 platform correctly assigns pbuffers to single-buffered configs, but
553 * assigns the pbuffer a front buffer instead of a back buffer.
554 *
555 * Pbuffers in the X11 platform mostly work today, so let's just copy its
556 * behavior instead of trying to fix (and hence potentially breaking) the
557 * world.
558 */
559 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
560 }
561
562 return 0;
563 }
564
565 /* Some drivers will pass multiple bits in buffer_mask.
566 * For such case, will go through all the bits, and
567 * will not return error when unsupported buffer is requested, only
568 * return error when the allocation for supported buffer failed.
569 */
570 static int
571 droid_image_get_buffers(__DRIdrawable *driDrawable,
572 unsigned int format,
573 uint32_t *stamp,
574 void *loaderPrivate,
575 uint32_t buffer_mask,
576 struct __DRIimageList *images)
577 {
578 struct dri2_egl_surface *dri2_surf = loaderPrivate;
579
580 images->image_mask = 0;
581 images->front = NULL;
582 images->back = NULL;
583
584 if (update_buffers(dri2_surf) < 0)
585 return 0;
586
587 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
588 if (get_front_bo(dri2_surf, format) < 0)
589 return 0;
590
591 if (dri2_surf->dri_image_front) {
592 images->front = dri2_surf->dri_image_front;
593 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
594 }
595 }
596
597 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
598 if (get_back_bo(dri2_surf, format) < 0)
599 return 0;
600
601 if (dri2_surf->dri_image_back) {
602 images->back = dri2_surf->dri_image_back;
603 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
604 }
605 }
606
607 return 1;
608 }
609
610 static EGLint
611 droid_query_buffer_age(_EGLDriver *drv,
612 _EGLDisplay *disp, _EGLSurface *surface)
613 {
614 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
615
616 if (update_buffers(dri2_surf) < 0) {
617 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
618 return 0;
619 }
620
621 return dri2_surf->back->age;
622 }
623
624 static EGLBoolean
625 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
626 {
627 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
628 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
629
630 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
631 return EGL_TRUE;
632
633 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
634 if (dri2_surf->color_buffers[i].age > 0)
635 dri2_surf->color_buffers[i].age++;
636 }
637 dri2_surf->back->age = 1;
638
639 dri2_flush_drawable_for_swapbuffers(disp, draw);
640
641 if (dri2_surf->buffer)
642 droid_window_enqueue_buffer(disp, dri2_surf);
643
644 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
645
646 return EGL_TRUE;
647 }
648
649 static _EGLImage *
650 droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
651 struct ANativeWindowBuffer *buf, int fd)
652 {
653 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
654 struct android_ycbcr ycbcr;
655 size_t offsets[3];
656 size_t pitches[3];
657 int is_ycrcb;
658 int fourcc;
659 int ret;
660
661 if (!dri2_dpy->gralloc->lock_ycbcr) {
662 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
663 return NULL;
664 }
665
666 memset(&ycbcr, 0, sizeof(ycbcr));
667 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
668 0, 0, 0, 0, 0, &ycbcr);
669 if (ret) {
670 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
671 return NULL;
672 }
673 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
674
675 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
676 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
677 * so they can be interpreted as offsets. */
678 offsets[0] = (size_t)ycbcr.y;
679 /* We assume here that all the planes are located in one DMA-buf. */
680 is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
681 if (is_ycrcb) {
682 offsets[1] = (size_t)ycbcr.cr;
683 offsets[2] = (size_t)ycbcr.cb;
684 } else {
685 offsets[1] = (size_t)ycbcr.cb;
686 offsets[2] = (size_t)ycbcr.cr;
687 }
688
689 /* .ystride is the line length (in bytes) of the Y plane,
690 * .cstride is the line length (in bytes) of any of the remaining
691 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
692 * planar formats. */
693 pitches[0] = ycbcr.ystride;
694 pitches[1] = pitches[2] = ycbcr.cstride;
695
696 /* .chroma_step is the byte distance between the same chroma channel
697 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
698 fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
699 if (fourcc == -1) {
700 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
701 buf->format, is_ycrcb, ycbcr.chroma_step);
702 return NULL;
703 }
704
705 if (ycbcr.chroma_step == 2) {
706 /* Semi-planar Y + CbCr or Y + CbCr format. */
707 const EGLint attr_list_2plane[] = {
708 EGL_WIDTH, buf->width,
709 EGL_HEIGHT, buf->height,
710 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
711 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
712 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
713 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
714 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
715 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
716 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
717 EGL_NONE, 0
718 };
719
720 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
721 } else {
722 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
723 const EGLint attr_list_3plane[] = {
724 EGL_WIDTH, buf->width,
725 EGL_HEIGHT, buf->height,
726 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
727 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
728 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
729 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
730 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
731 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
732 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
733 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
734 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
735 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
736 EGL_NONE, 0
737 };
738
739 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
740 }
741 }
742
743 static _EGLImage *
744 droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
745 struct ANativeWindowBuffer *buf, int fd)
746 {
747 unsigned int pitch;
748
749 if (is_yuv(buf->format))
750 return droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd);
751
752 const int fourcc = get_fourcc(buf->format);
753 if (fourcc == -1) {
754 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
755 return NULL;
756 }
757
758 pitch = buf->stride * get_format_bpp(buf->format);
759 if (pitch == 0) {
760 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
761 return NULL;
762 }
763
764 const EGLint attr_list[] = {
765 EGL_WIDTH, buf->width,
766 EGL_HEIGHT, buf->height,
767 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
768 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
769 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
770 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
771 EGL_NONE, 0
772 };
773
774 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
775 }
776
777 static _EGLImage *
778 droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
779 struct ANativeWindowBuffer *buf)
780 {
781 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
782 struct dri2_egl_image *dri2_img;
783 int name;
784 int format;
785
786 name = get_native_buffer_name(buf);
787 if (!name) {
788 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
789 return NULL;
790 }
791
792 format = get_format(buf->format);
793 if (format == -1)
794 return NULL;
795
796 dri2_img = calloc(1, sizeof(*dri2_img));
797 if (!dri2_img) {
798 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
799 return NULL;
800 }
801
802 if (!_eglInitImage(&dri2_img->base, disp)) {
803 free(dri2_img);
804 return NULL;
805 }
806
807 dri2_img->dri_image =
808 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
809 buf->width,
810 buf->height,
811 format,
812 name,
813 buf->stride,
814 dri2_img);
815 if (!dri2_img->dri_image) {
816 free(dri2_img);
817 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
818 return NULL;
819 }
820
821 return &dri2_img->base;
822 }
823
824 static EGLBoolean
825 droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
826 EGLint attribute, EGLint *value)
827 {
828 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
829 switch (attribute) {
830 case EGL_WIDTH:
831 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
832 dri2_surf->window->query(dri2_surf->window,
833 NATIVE_WINDOW_DEFAULT_WIDTH, value);
834 return EGL_TRUE;
835 }
836 break;
837 case EGL_HEIGHT:
838 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
839 dri2_surf->window->query(dri2_surf->window,
840 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
841 return EGL_TRUE;
842 }
843 break;
844 default:
845 break;
846 }
847 return _eglQuerySurface(drv, dpy, surf, attribute, value);
848 }
849
850 static _EGLImage *
851 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
852 _EGLContext *ctx,
853 struct ANativeWindowBuffer *buf)
854 {
855 int fd;
856
857 if (ctx != NULL) {
858 /* From the EGL_ANDROID_image_native_buffer spec:
859 *
860 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
861 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
862 */
863 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
864 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
865 "EGL_NO_CONTEXT");
866 return NULL;
867 }
868
869 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
870 buf->common.version != sizeof(*buf)) {
871 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
872 return NULL;
873 }
874
875 fd = get_native_buffer_fd(buf);
876 if (fd >= 0)
877 return droid_create_image_from_prime_fd(disp, ctx, buf, fd);
878
879 return droid_create_image_from_name(disp, ctx, buf);
880 }
881
882 static _EGLImage *
883 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
884 _EGLContext *ctx, EGLenum target,
885 EGLClientBuffer buffer, const EGLint *attr_list)
886 {
887 switch (target) {
888 case EGL_NATIVE_BUFFER_ANDROID:
889 return dri2_create_image_android_native_buffer(disp, ctx,
890 (struct ANativeWindowBuffer *) buffer);
891 default:
892 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
893 }
894 }
895
896 static void
897 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
898 {
899 }
900
901 static int
902 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
903 unsigned int *attachments, int count)
904 {
905 int num_buffers = 0, i;
906
907 /* fill dri2_surf->buffers */
908 for (i = 0; i < count * 2; i += 2) {
909 __DRIbuffer *buf, *local;
910
911 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
912 buf = &dri2_surf->buffers[num_buffers];
913
914 switch (attachments[i]) {
915 case __DRI_BUFFER_BACK_LEFT:
916 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
917 buf->attachment = attachments[i];
918 buf->name = get_native_buffer_name(dri2_surf->buffer);
919 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
920 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
921 buf->flags = 0;
922
923 if (buf->name)
924 num_buffers++;
925
926 break;
927 }
928 /* fall through for pbuffers */
929 case __DRI_BUFFER_DEPTH:
930 case __DRI_BUFFER_STENCIL:
931 case __DRI_BUFFER_ACCUM:
932 case __DRI_BUFFER_DEPTH_STENCIL:
933 case __DRI_BUFFER_HIZ:
934 local = droid_alloc_local_buffer(dri2_surf,
935 attachments[i], attachments[i + 1]);
936
937 if (local) {
938 *buf = *local;
939 num_buffers++;
940 }
941 break;
942 case __DRI_BUFFER_FRONT_LEFT:
943 case __DRI_BUFFER_FRONT_RIGHT:
944 case __DRI_BUFFER_FAKE_FRONT_LEFT:
945 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
946 case __DRI_BUFFER_BACK_RIGHT:
947 default:
948 /* no front or right buffers */
949 break;
950 }
951 }
952
953 return num_buffers;
954 }
955
956 static __DRIbuffer *
957 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
958 int *width, int *height,
959 unsigned int *attachments, int count,
960 int *out_count, void *loaderPrivate)
961 {
962 struct dri2_egl_surface *dri2_surf = loaderPrivate;
963
964 if (update_buffers(dri2_surf) < 0)
965 return NULL;
966
967 dri2_surf->buffer_count =
968 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
969
970 if (width)
971 *width = dri2_surf->base.Width;
972 if (height)
973 *height = dri2_surf->base.Height;
974
975 *out_count = dri2_surf->buffer_count;
976
977 return dri2_surf->buffers;
978 }
979
980 static EGLBoolean
981 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
982 {
983 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
984 static const struct {
985 int format;
986 unsigned int rgba_masks[4];
987 } visuals[] = {
988 { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
989 { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
990 { HAL_PIXEL_FORMAT_RGB_565, { 0xf800, 0x7e0, 0x1f, 0x0 } },
991 { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
992 };
993 EGLint config_attrs[] = {
994 EGL_NATIVE_VISUAL_ID, 0,
995 EGL_NATIVE_VISUAL_TYPE, 0,
996 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
997 EGL_RECORDABLE_ANDROID, EGL_TRUE,
998 EGL_NONE
999 };
1000 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1001 int count, i, j;
1002
1003 count = 0;
1004 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1005 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1006 struct dri2_egl_config *dri2_conf;
1007
1008 for (j = 0; j < ARRAY_SIZE(visuals); j++) {
1009 config_attrs[1] = visuals[j].format;
1010 config_attrs[3] = visuals[j].format;
1011
1012 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[i],
1013 count + 1, surface_type, config_attrs, visuals[j].rgba_masks);
1014 if (dri2_conf) {
1015 count++;
1016 format_count[j]++;
1017 }
1018 }
1019 }
1020
1021 for (i = 0; i < ARRAY_SIZE(format_count); i++) {
1022 if (!format_count[i]) {
1023 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
1024 visuals[i].format);
1025 }
1026 }
1027
1028 return (count != 0);
1029 }
1030
1031 static int
1032 droid_open_device(struct dri2_egl_display *dri2_dpy)
1033 {
1034 int fd = -1, err = -EINVAL;
1035
1036 if (dri2_dpy->gralloc->perform)
1037 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1038 GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1039 &fd);
1040 if (err || fd < 0) {
1041 _eglLog(_EGL_WARNING, "fail to get drm fd");
1042 fd = -1;
1043 }
1044
1045 return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1;
1046 }
1047
1048 /* support versions < JellyBean */
1049 #ifndef ALOGW
1050 #define ALOGW LOGW
1051 #endif
1052 #ifndef ALOGD
1053 #define ALOGD LOGD
1054 #endif
1055 #ifndef ALOGI
1056 #define ALOGI LOGI
1057 #endif
1058
1059 static void
1060 droid_log(EGLint level, const char *msg)
1061 {
1062 switch (level) {
1063 case _EGL_DEBUG:
1064 ALOGD("%s", msg);
1065 break;
1066 case _EGL_INFO:
1067 ALOGI("%s", msg);
1068 break;
1069 case _EGL_WARNING:
1070 ALOGW("%s", msg);
1071 break;
1072 case _EGL_FATAL:
1073 LOG_FATAL("%s", msg);
1074 break;
1075 default:
1076 break;
1077 }
1078 }
1079
1080 static struct dri2_egl_display_vtbl droid_display_vtbl = {
1081 .authenticate = NULL,
1082 .create_window_surface = droid_create_window_surface,
1083 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
1084 .create_pbuffer_surface = droid_create_pbuffer_surface,
1085 .destroy_surface = droid_destroy_surface,
1086 .create_image = droid_create_image_khr,
1087 .swap_interval = dri2_fallback_swap_interval,
1088 .swap_buffers = droid_swap_buffers,
1089 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1090 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1091 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1092 .copy_buffers = dri2_fallback_copy_buffers,
1093 .query_buffer_age = droid_query_buffer_age,
1094 .query_surface = droid_query_surface,
1095 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1096 .get_sync_values = dri2_fallback_get_sync_values,
1097 .get_dri_drawable = dri2_surface_get_dri_drawable,
1098 };
1099
1100 static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1101 .base = { __DRI_DRI2_LOADER, 3 },
1102
1103 .getBuffers = NULL,
1104 .flushFrontBuffer = droid_flush_front_buffer,
1105 .getBuffersWithFormat = droid_get_buffers_with_format,
1106 };
1107
1108 static const __DRIimageLoaderExtension droid_image_loader_extension = {
1109 .base = { __DRI_IMAGE_LOADER, 1 },
1110
1111 .getBuffers = droid_image_get_buffers,
1112 .flushFrontBuffer = droid_flush_front_buffer,
1113 };
1114
1115 static const __DRIextension *droid_dri2_loader_extensions[] = {
1116 &droid_dri2_loader_extension.base,
1117 &image_lookup_extension.base,
1118 &use_invalidate.base,
1119 NULL,
1120 };
1121
1122 static const __DRIextension *droid_image_loader_extensions[] = {
1123 &droid_image_loader_extension.base,
1124 &image_lookup_extension.base,
1125 &use_invalidate.base,
1126 NULL,
1127 };
1128
1129 EGLBoolean
1130 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
1131 {
1132 struct dri2_egl_display *dri2_dpy;
1133 const char *err;
1134 int ret;
1135
1136 _eglSetLogProc(droid_log);
1137
1138 loader_set_logger(_eglLog);
1139
1140 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1141 if (!dri2_dpy)
1142 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1143
1144 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1145 (const hw_module_t **)&dri2_dpy->gralloc);
1146 if (ret) {
1147 err = "DRI2: failed to get gralloc module";
1148 goto cleanup_display;
1149 }
1150
1151 dpy->DriverData = (void *) dri2_dpy;
1152
1153 dri2_dpy->fd = droid_open_device(dri2_dpy);
1154 if (dri2_dpy->fd < 0) {
1155 err = "DRI2: failed to open device";
1156 goto cleanup_display;
1157 }
1158
1159 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1160 if (dri2_dpy->driver_name == NULL) {
1161 err = "DRI2: failed to get driver name";
1162 goto cleanup_device;
1163 }
1164
1165 if (!dri2_load_driver(dpy)) {
1166 err = "DRI2: failed to load driver";
1167 goto cleanup_driver_name;
1168 }
1169
1170 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1171
1172 /* render nodes cannot use Gem names, and thus do not support
1173 * the __DRI_DRI2_LOADER extension */
1174 if (!dri2_dpy->is_render_node)
1175 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
1176 else
1177 dri2_dpy->loader_extensions = droid_image_loader_extensions;
1178
1179 if (!dri2_create_screen(dpy)) {
1180 err = "DRI2: failed to create screen";
1181 goto cleanup_driver;
1182 }
1183
1184 if (!droid_add_configs_for_visuals(drv, dpy)) {
1185 err = "DRI2: failed to add configs";
1186 goto cleanup_screen;
1187 }
1188
1189 dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
1190 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
1191 dpy->Extensions.ANDROID_recordable = EGL_TRUE;
1192 dpy->Extensions.EXT_buffer_age = EGL_TRUE;
1193
1194 /* Fill vtbl last to prevent accidentally calling virtual function during
1195 * initialization.
1196 */
1197 dri2_dpy->vtbl = &droid_display_vtbl;
1198
1199 return EGL_TRUE;
1200
1201 cleanup_screen:
1202 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1203 cleanup_driver:
1204 dlclose(dri2_dpy->driver);
1205 cleanup_driver_name:
1206 free(dri2_dpy->driver_name);
1207 cleanup_device:
1208 close(dri2_dpy->fd);
1209 cleanup_display:
1210 free(dri2_dpy);
1211 dpy->DriverData = NULL;
1212
1213 return _eglError(EGL_NOT_INITIALIZED, err);
1214 }