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