egl/android: make use of dri2_display_destroy() helper
[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 = EGL_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.Lost)
435 return -1;
436
437 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
438 return 0;
439
440 /* try to dequeue the next back buffer */
441 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
442 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
443 dri2_surf->base.Lost = EGL_TRUE;
444 return -1;
445 }
446
447 /* free outdated buffers and update the surface size */
448 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
449 dri2_surf->base.Height != dri2_surf->buffer->height) {
450 droid_free_local_buffers(dri2_surf);
451 dri2_surf->base.Width = dri2_surf->buffer->width;
452 dri2_surf->base.Height = dri2_surf->buffer->height;
453 }
454
455 return 0;
456 }
457
458 static int
459 get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
460 {
461 struct dri2_egl_display *dri2_dpy =
462 dri2_egl_display(dri2_surf->base.Resource.Display);
463
464 if (dri2_surf->dri_image_front)
465 return 0;
466
467 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
468 /* According current EGL spec, front buffer rendering
469 * for window surface is not supported now.
470 * and mesa doesn't have the implementation of this case.
471 * Add warning message, but not treat it as error.
472 */
473 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
474 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
475 dri2_surf->dri_image_front =
476 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
477 dri2_surf->base.Width,
478 dri2_surf->base.Height,
479 format,
480 0,
481 dri2_surf);
482 if (!dri2_surf->dri_image_front) {
483 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
484 return -1;
485 }
486 }
487
488 return 0;
489 }
490
491 static int
492 get_back_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
493 {
494 struct dri2_egl_display *dri2_dpy =
495 dri2_egl_display(dri2_surf->base.Resource.Display);
496 int fourcc, pitch;
497 int offset = 0, fd;
498
499 if (dri2_surf->dri_image_back)
500 return 0;
501
502 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
503 if (!dri2_surf->buffer) {
504 _eglLog(_EGL_WARNING, "Could not get native buffer");
505 return -1;
506 }
507
508 fd = get_native_buffer_fd(dri2_surf->buffer);
509 if (fd < 0) {
510 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
511 return -1;
512 }
513
514 fourcc = get_fourcc(dri2_surf->buffer->format);
515
516 pitch = dri2_surf->buffer->stride *
517 get_format_bpp(dri2_surf->buffer->format);
518
519 if (fourcc == -1 || pitch == 0) {
520 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
521 fourcc, pitch);
522 return -1;
523 }
524
525 dri2_surf->dri_image_back =
526 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
527 dri2_surf->base.Width,
528 dri2_surf->base.Height,
529 fourcc,
530 &fd,
531 1,
532 &pitch,
533 &offset,
534 dri2_surf);
535 if (!dri2_surf->dri_image_back) {
536 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
537 return -1;
538 }
539 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
540 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
541 * the spec states that they have a back buffer but no front buffer, in
542 * contrast to pixmaps, which have a front buffer but no back buffer.
543 *
544 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
545 * from the spec, following the precedent of Mesa's EGL X11 platform. The
546 * X11 platform correctly assigns pbuffers to single-buffered configs, but
547 * assigns the pbuffer a front buffer instead of a back buffer.
548 *
549 * Pbuffers in the X11 platform mostly work today, so let's just copy its
550 * behavior instead of trying to fix (and hence potentially breaking) the
551 * world.
552 */
553 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
554 }
555
556 return 0;
557 }
558
559 /* Some drivers will pass multiple bits in buffer_mask.
560 * For such case, will go through all the bits, and
561 * will not return error when unsupported buffer is requested, only
562 * return error when the allocation for supported buffer failed.
563 */
564 static int
565 droid_image_get_buffers(__DRIdrawable *driDrawable,
566 unsigned int format,
567 uint32_t *stamp,
568 void *loaderPrivate,
569 uint32_t buffer_mask,
570 struct __DRIimageList *images)
571 {
572 struct dri2_egl_surface *dri2_surf = loaderPrivate;
573
574 images->image_mask = 0;
575 images->front = NULL;
576 images->back = NULL;
577
578 if (update_buffers(dri2_surf) < 0)
579 return 0;
580
581 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
582 if (get_front_bo(dri2_surf, format) < 0)
583 return 0;
584
585 if (dri2_surf->dri_image_front) {
586 images->front = dri2_surf->dri_image_front;
587 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
588 }
589 }
590
591 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
592 if (get_back_bo(dri2_surf, format) < 0)
593 return 0;
594
595 if (dri2_surf->dri_image_back) {
596 images->back = dri2_surf->dri_image_back;
597 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
598 }
599 }
600
601 return 1;
602 }
603
604 static EGLint
605 droid_query_buffer_age(_EGLDriver *drv,
606 _EGLDisplay *disp, _EGLSurface *surface)
607 {
608 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
609
610 if (update_buffers(dri2_surf) < 0) {
611 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
612 return 0;
613 }
614
615 return dri2_surf->back->age;
616 }
617
618 static EGLBoolean
619 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
620 {
621 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
622 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
623
624 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
625 return EGL_TRUE;
626
627 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
628 if (dri2_surf->color_buffers[i].age > 0)
629 dri2_surf->color_buffers[i].age++;
630 }
631
632 /* "XXX: we don't use get_back_bo() since it causes regressions in
633 * several dEQP tests.
634 */
635 if (dri2_surf->back)
636 dri2_surf->back->age = 1;
637
638 dri2_flush_drawable_for_swapbuffers(disp, draw);
639
640 /* dri2_surf->buffer can be null even when no error has occured. For
641 * example, if the user has called no GL rendering commands since the
642 * previous eglSwapBuffers, then the driver may have not triggered
643 * a callback to ANativeWindow::dequeueBuffer, in which case
644 * dri2_surf->buffer remains null.
645 */
646 if (dri2_surf->buffer)
647 droid_window_enqueue_buffer(disp, dri2_surf);
648
649 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
650
651 return EGL_TRUE;
652 }
653
654 static _EGLImage *
655 droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
656 struct ANativeWindowBuffer *buf, int fd)
657 {
658 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
659 struct android_ycbcr ycbcr;
660 size_t offsets[3];
661 size_t pitches[3];
662 int is_ycrcb;
663 int fourcc;
664 int ret;
665
666 if (!dri2_dpy->gralloc->lock_ycbcr) {
667 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
668 return NULL;
669 }
670
671 memset(&ycbcr, 0, sizeof(ycbcr));
672 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
673 0, 0, 0, 0, 0, &ycbcr);
674 if (ret) {
675 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
676 return NULL;
677 }
678 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
679
680 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
681 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
682 * so they can be interpreted as offsets. */
683 offsets[0] = (size_t)ycbcr.y;
684 /* We assume here that all the planes are located in one DMA-buf. */
685 is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
686 if (is_ycrcb) {
687 offsets[1] = (size_t)ycbcr.cr;
688 offsets[2] = (size_t)ycbcr.cb;
689 } else {
690 offsets[1] = (size_t)ycbcr.cb;
691 offsets[2] = (size_t)ycbcr.cr;
692 }
693
694 /* .ystride is the line length (in bytes) of the Y plane,
695 * .cstride is the line length (in bytes) of any of the remaining
696 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
697 * planar formats. */
698 pitches[0] = ycbcr.ystride;
699 pitches[1] = pitches[2] = ycbcr.cstride;
700
701 /* .chroma_step is the byte distance between the same chroma channel
702 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
703 fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
704 if (fourcc == -1) {
705 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
706 buf->format, is_ycrcb, ycbcr.chroma_step);
707 return NULL;
708 }
709
710 if (ycbcr.chroma_step == 2) {
711 /* Semi-planar Y + CbCr or Y + CbCr format. */
712 const EGLint attr_list_2plane[] = {
713 EGL_WIDTH, buf->width,
714 EGL_HEIGHT, buf->height,
715 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
716 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
717 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
718 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
719 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
720 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
721 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
722 EGL_NONE, 0
723 };
724
725 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
726 } else {
727 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
728 const EGLint attr_list_3plane[] = {
729 EGL_WIDTH, buf->width,
730 EGL_HEIGHT, buf->height,
731 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
732 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
733 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
734 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
735 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
736 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
737 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
738 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
739 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
740 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
741 EGL_NONE, 0
742 };
743
744 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
745 }
746 }
747
748 static _EGLImage *
749 droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
750 struct ANativeWindowBuffer *buf, int fd)
751 {
752 unsigned int pitch;
753
754 if (is_yuv(buf->format))
755 return droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd);
756
757 const int fourcc = get_fourcc(buf->format);
758 if (fourcc == -1) {
759 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
760 return NULL;
761 }
762
763 pitch = buf->stride * get_format_bpp(buf->format);
764 if (pitch == 0) {
765 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
766 return NULL;
767 }
768
769 const EGLint attr_list[] = {
770 EGL_WIDTH, buf->width,
771 EGL_HEIGHT, buf->height,
772 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
773 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
774 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
775 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
776 EGL_NONE, 0
777 };
778
779 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
780 }
781
782 static _EGLImage *
783 droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
784 struct ANativeWindowBuffer *buf)
785 {
786 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
787 struct dri2_egl_image *dri2_img;
788 int name;
789 int format;
790
791 name = get_native_buffer_name(buf);
792 if (!name) {
793 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
794 return NULL;
795 }
796
797 format = get_format(buf->format);
798 if (format == -1)
799 return NULL;
800
801 dri2_img = calloc(1, sizeof(*dri2_img));
802 if (!dri2_img) {
803 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
804 return NULL;
805 }
806
807 if (!_eglInitImage(&dri2_img->base, disp)) {
808 free(dri2_img);
809 return NULL;
810 }
811
812 dri2_img->dri_image =
813 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
814 buf->width,
815 buf->height,
816 format,
817 name,
818 buf->stride,
819 dri2_img);
820 if (!dri2_img->dri_image) {
821 free(dri2_img);
822 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
823 return NULL;
824 }
825
826 return &dri2_img->base;
827 }
828
829 static EGLBoolean
830 droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
831 EGLint attribute, EGLint *value)
832 {
833 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
834 switch (attribute) {
835 case EGL_WIDTH:
836 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
837 dri2_surf->window->query(dri2_surf->window,
838 NATIVE_WINDOW_DEFAULT_WIDTH, value);
839 return EGL_TRUE;
840 }
841 break;
842 case EGL_HEIGHT:
843 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
844 dri2_surf->window->query(dri2_surf->window,
845 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
846 return EGL_TRUE;
847 }
848 break;
849 default:
850 break;
851 }
852 return _eglQuerySurface(drv, dpy, surf, attribute, value);
853 }
854
855 static _EGLImage *
856 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
857 _EGLContext *ctx,
858 struct ANativeWindowBuffer *buf)
859 {
860 int fd;
861
862 if (ctx != NULL) {
863 /* From the EGL_ANDROID_image_native_buffer spec:
864 *
865 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
866 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
867 */
868 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
869 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
870 "EGL_NO_CONTEXT");
871 return NULL;
872 }
873
874 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
875 buf->common.version != sizeof(*buf)) {
876 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
877 return NULL;
878 }
879
880 fd = get_native_buffer_fd(buf);
881 if (fd >= 0)
882 return droid_create_image_from_prime_fd(disp, ctx, buf, fd);
883
884 return droid_create_image_from_name(disp, ctx, buf);
885 }
886
887 static _EGLImage *
888 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
889 _EGLContext *ctx, EGLenum target,
890 EGLClientBuffer buffer, const EGLint *attr_list)
891 {
892 switch (target) {
893 case EGL_NATIVE_BUFFER_ANDROID:
894 return dri2_create_image_android_native_buffer(disp, ctx,
895 (struct ANativeWindowBuffer *) buffer);
896 default:
897 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
898 }
899 }
900
901 static void
902 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
903 {
904 }
905
906 static int
907 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
908 unsigned int *attachments, int count)
909 {
910 int num_buffers = 0, i;
911
912 /* fill dri2_surf->buffers */
913 for (i = 0; i < count * 2; i += 2) {
914 __DRIbuffer *buf, *local;
915
916 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
917 buf = &dri2_surf->buffers[num_buffers];
918
919 switch (attachments[i]) {
920 case __DRI_BUFFER_BACK_LEFT:
921 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
922 buf->attachment = attachments[i];
923 buf->name = get_native_buffer_name(dri2_surf->buffer);
924 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
925 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
926 buf->flags = 0;
927
928 if (buf->name)
929 num_buffers++;
930
931 break;
932 }
933 /* fall through for pbuffers */
934 case __DRI_BUFFER_DEPTH:
935 case __DRI_BUFFER_STENCIL:
936 case __DRI_BUFFER_ACCUM:
937 case __DRI_BUFFER_DEPTH_STENCIL:
938 case __DRI_BUFFER_HIZ:
939 local = droid_alloc_local_buffer(dri2_surf,
940 attachments[i], attachments[i + 1]);
941
942 if (local) {
943 *buf = *local;
944 num_buffers++;
945 }
946 break;
947 case __DRI_BUFFER_FRONT_LEFT:
948 case __DRI_BUFFER_FRONT_RIGHT:
949 case __DRI_BUFFER_FAKE_FRONT_LEFT:
950 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
951 case __DRI_BUFFER_BACK_RIGHT:
952 default:
953 /* no front or right buffers */
954 break;
955 }
956 }
957
958 return num_buffers;
959 }
960
961 static __DRIbuffer *
962 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
963 int *width, int *height,
964 unsigned int *attachments, int count,
965 int *out_count, void *loaderPrivate)
966 {
967 struct dri2_egl_surface *dri2_surf = loaderPrivate;
968
969 if (update_buffers(dri2_surf) < 0)
970 return NULL;
971
972 dri2_surf->buffer_count =
973 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
974
975 if (width)
976 *width = dri2_surf->base.Width;
977 if (height)
978 *height = dri2_surf->base.Height;
979
980 *out_count = dri2_surf->buffer_count;
981
982 return dri2_surf->buffers;
983 }
984
985 static EGLBoolean
986 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
987 {
988 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
989 static const struct {
990 int format;
991 unsigned int rgba_masks[4];
992 } visuals[] = {
993 { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
994 { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
995 { HAL_PIXEL_FORMAT_RGB_565, { 0xf800, 0x7e0, 0x1f, 0x0 } },
996 { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
997 };
998 EGLint config_attrs[] = {
999 EGL_NATIVE_VISUAL_ID, 0,
1000 EGL_NATIVE_VISUAL_TYPE, 0,
1001 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1002 EGL_RECORDABLE_ANDROID, EGL_TRUE,
1003 EGL_NONE
1004 };
1005 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1006 int count, i, j;
1007
1008 count = 0;
1009 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1010 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1011 struct dri2_egl_config *dri2_conf;
1012
1013 for (j = 0; j < ARRAY_SIZE(visuals); j++) {
1014 config_attrs[1] = visuals[j].format;
1015 config_attrs[3] = visuals[j].format;
1016
1017 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[i],
1018 count + 1, surface_type, config_attrs, visuals[j].rgba_masks);
1019 if (dri2_conf) {
1020 count++;
1021 format_count[j]++;
1022 }
1023 }
1024 }
1025
1026 for (i = 0; i < ARRAY_SIZE(format_count); i++) {
1027 if (!format_count[i]) {
1028 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
1029 visuals[i].format);
1030 }
1031 }
1032
1033 return (count != 0);
1034 }
1035
1036 static int
1037 droid_open_device(struct dri2_egl_display *dri2_dpy)
1038 {
1039 int fd = -1, err = -EINVAL;
1040
1041 if (dri2_dpy->gralloc->perform)
1042 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1043 GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1044 &fd);
1045 if (err || fd < 0) {
1046 _eglLog(_EGL_WARNING, "fail to get drm fd");
1047 fd = -1;
1048 }
1049
1050 return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1;
1051 }
1052
1053 static struct dri2_egl_display_vtbl droid_display_vtbl = {
1054 .authenticate = NULL,
1055 .create_window_surface = droid_create_window_surface,
1056 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
1057 .create_pbuffer_surface = droid_create_pbuffer_surface,
1058 .destroy_surface = droid_destroy_surface,
1059 .create_image = droid_create_image_khr,
1060 .swap_interval = dri2_fallback_swap_interval,
1061 .swap_buffers = droid_swap_buffers,
1062 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1063 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1064 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1065 .copy_buffers = dri2_fallback_copy_buffers,
1066 .query_buffer_age = droid_query_buffer_age,
1067 .query_surface = droid_query_surface,
1068 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1069 .get_sync_values = dri2_fallback_get_sync_values,
1070 .get_dri_drawable = dri2_surface_get_dri_drawable,
1071 };
1072
1073 static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1074 .base = { __DRI_DRI2_LOADER, 3 },
1075
1076 .getBuffers = NULL,
1077 .flushFrontBuffer = droid_flush_front_buffer,
1078 .getBuffersWithFormat = droid_get_buffers_with_format,
1079 };
1080
1081 static const __DRIimageLoaderExtension droid_image_loader_extension = {
1082 .base = { __DRI_IMAGE_LOADER, 1 },
1083
1084 .getBuffers = droid_image_get_buffers,
1085 .flushFrontBuffer = droid_flush_front_buffer,
1086 };
1087
1088 static const __DRIextension *droid_dri2_loader_extensions[] = {
1089 &droid_dri2_loader_extension.base,
1090 &image_lookup_extension.base,
1091 &use_invalidate.base,
1092 NULL,
1093 };
1094
1095 static const __DRIextension *droid_image_loader_extensions[] = {
1096 &droid_image_loader_extension.base,
1097 &image_lookup_extension.base,
1098 &use_invalidate.base,
1099 NULL,
1100 };
1101
1102 EGLBoolean
1103 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
1104 {
1105 struct dri2_egl_display *dri2_dpy;
1106 const char *err;
1107 int ret;
1108
1109 loader_set_logger(_eglLog);
1110
1111 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1112 if (!dri2_dpy)
1113 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1114
1115 dri2_dpy->fd = -1;
1116 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1117 (const hw_module_t **)&dri2_dpy->gralloc);
1118 if (ret) {
1119 err = "DRI2: failed to get gralloc module";
1120 goto cleanup;
1121 }
1122
1123 dpy->DriverData = (void *) dri2_dpy;
1124
1125 dri2_dpy->fd = droid_open_device(dri2_dpy);
1126 if (dri2_dpy->fd < 0) {
1127 err = "DRI2: failed to open device";
1128 goto cleanup;
1129 }
1130
1131 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1132 if (dri2_dpy->driver_name == NULL) {
1133 err = "DRI2: failed to get driver name";
1134 goto cleanup;
1135 }
1136
1137 if (!dri2_load_driver(dpy)) {
1138 err = "DRI2: failed to load driver";
1139 goto cleanup;
1140 }
1141
1142 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1143
1144 /* render nodes cannot use Gem names, and thus do not support
1145 * the __DRI_DRI2_LOADER extension */
1146 if (!dri2_dpy->is_render_node)
1147 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
1148 else
1149 dri2_dpy->loader_extensions = droid_image_loader_extensions;
1150
1151 if (!dri2_create_screen(dpy)) {
1152 err = "DRI2: failed to create screen";
1153 goto cleanup;
1154 }
1155
1156 if (!droid_add_configs_for_visuals(drv, dpy)) {
1157 err = "DRI2: failed to add configs";
1158 goto cleanup;
1159 }
1160
1161 dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
1162 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
1163 dpy->Extensions.ANDROID_recordable = EGL_TRUE;
1164 dpy->Extensions.EXT_buffer_age = EGL_TRUE;
1165
1166 /* Fill vtbl last to prevent accidentally calling virtual function during
1167 * initialization.
1168 */
1169 dri2_dpy->vtbl = &droid_display_vtbl;
1170
1171 return EGL_TRUE;
1172
1173 cleanup:
1174 dri2_display_destroy(dpy);
1175 return _eglError(EGL_NOT_INITIALIZED, err);
1176 }