Revert "egl: Configs w/o double buffering support have no `EGL_WINDOW_BIT`."
[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 <cutils/properties.h>
31 #include <errno.h>
32 #include <dirent.h>
33 #include <dlfcn.h>
34 #include <fcntl.h>
35 #include <xf86drm.h>
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <sync/sync.h>
39 #include <sys/types.h>
40 #include <drm-uapi/drm_fourcc.h>
41
42 #include "loader.h"
43 #include "egl_dri2.h"
44 #include "egl_dri2_fallbacks.h"
45
46 #ifdef HAVE_DRM_GRALLOC
47 #include <gralloc_drm_handle.h>
48 #include "gralloc_drm.h"
49 #endif /* HAVE_DRM_GRALLOC */
50
51 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
52
53 enum chroma_order {
54 YCbCr,
55 YCrCb,
56 };
57
58 struct droid_yuv_format {
59 /* Lookup keys */
60 int native; /* HAL_PIXEL_FORMAT_ */
61 enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
62 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
63
64 /* Result */
65 int fourcc; /* DRM_FORMAT_ */
66 };
67
68 /* The following table is used to look up a DRI image FourCC based
69 * on native format and information contained in android_ycbcr struct. */
70 static const struct droid_yuv_format droid_yuv_formats[] = {
71 /* Native format, YCrCb, Chroma step, DRI image FourCC */
72 { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12 },
73 { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420 },
74 { HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420 },
75 { HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420 },
76 /* HACK: See droid_create_image_from_prime_fds() and
77 * https://issuetracker.google.com/32077885. */
78 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12 },
79 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420 },
80 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420 },
81 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV },
82 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888 },
83 };
84
85 static int
86 get_fourcc_yuv(int native, enum chroma_order chroma_order, int chroma_step)
87 {
88 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
89 if (droid_yuv_formats[i].native == native &&
90 droid_yuv_formats[i].chroma_order == chroma_order &&
91 droid_yuv_formats[i].chroma_step == chroma_step)
92 return droid_yuv_formats[i].fourcc;
93
94 return -1;
95 }
96
97 static bool
98 is_yuv(int native)
99 {
100 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
101 if (droid_yuv_formats[i].native == native)
102 return true;
103
104 return false;
105 }
106
107 static int
108 get_format_bpp(int native)
109 {
110 int bpp;
111
112 switch (native) {
113 case HAL_PIXEL_FORMAT_RGBA_FP16:
114 bpp = 8;
115 break;
116 case HAL_PIXEL_FORMAT_RGBA_8888:
117 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
118 /*
119 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
120 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
121 */
122 case HAL_PIXEL_FORMAT_RGBX_8888:
123 case HAL_PIXEL_FORMAT_BGRA_8888:
124 case HAL_PIXEL_FORMAT_RGBA_1010102:
125 bpp = 4;
126 break;
127 case HAL_PIXEL_FORMAT_RGB_565:
128 bpp = 2;
129 break;
130 default:
131 bpp = 0;
132 break;
133 }
134
135 return bpp;
136 }
137
138 /* createImageFromFds requires fourcc format */
139 static int get_fourcc(int native)
140 {
141 switch (native) {
142 case HAL_PIXEL_FORMAT_RGB_565: return DRM_FORMAT_RGB565;
143 case HAL_PIXEL_FORMAT_BGRA_8888: return DRM_FORMAT_ARGB8888;
144 case HAL_PIXEL_FORMAT_RGBA_8888: return DRM_FORMAT_ABGR8888;
145 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
146 /*
147 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
148 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
149 */
150 case HAL_PIXEL_FORMAT_RGBX_8888: return DRM_FORMAT_XBGR8888;
151 case HAL_PIXEL_FORMAT_RGBA_FP16: return DRM_FORMAT_ABGR16161616F;
152 case HAL_PIXEL_FORMAT_RGBA_1010102: return DRM_FORMAT_ABGR2101010;
153 default:
154 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
155 }
156 return -1;
157 }
158
159 static int get_format(int format)
160 {
161 switch (format) {
162 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
163 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
164 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
165 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
166 /*
167 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
168 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
169 */
170 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
171 case HAL_PIXEL_FORMAT_RGBA_FP16: return __DRI_IMAGE_FORMAT_ABGR16161616F;
172 case HAL_PIXEL_FORMAT_RGBA_1010102: return __DRI_IMAGE_FORMAT_ABGR2101010;
173 default:
174 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
175 }
176 return -1;
177 }
178
179 /* returns # of fds, and by reference the actual fds */
180 static unsigned
181 get_native_buffer_fds(struct ANativeWindowBuffer *buf, int fds[3])
182 {
183 native_handle_t *handle = (native_handle_t *)buf->handle;
184
185 if (!handle)
186 return 0;
187
188 /*
189 * Various gralloc implementations exist, but the dma-buf fd tends
190 * to be first. Access it directly to avoid a dependency on specific
191 * gralloc versions.
192 */
193 for (int i = 0; i < handle->numFds; i++)
194 fds[i] = handle->data[i];
195
196 return handle->numFds;
197 }
198
199 #ifdef HAVE_DRM_GRALLOC
200 static int
201 get_native_buffer_name(struct ANativeWindowBuffer *buf)
202 {
203 return gralloc_drm_get_gem_handle(buf->handle);
204 }
205 #endif /* HAVE_DRM_GRALLOC */
206
207 static EGLBoolean
208 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
209 {
210 int fence_fd;
211
212 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
213 &fence_fd))
214 return EGL_FALSE;
215
216 /* If access to the buffer is controlled by a sync fence, then block on the
217 * fence.
218 *
219 * It may be more performant to postpone blocking until there is an
220 * immediate need to write to the buffer. But doing so would require adding
221 * hooks to the DRI2 loader.
222 *
223 * From the ANativeWindow::dequeueBuffer documentation:
224 *
225 * The libsync fence file descriptor returned in the int pointed to by
226 * the fenceFd argument will refer to the fence that must signal
227 * before the dequeued buffer may be written to. A value of -1
228 * indicates that the caller may access the buffer immediately without
229 * waiting on a fence. If a valid file descriptor is returned (i.e.
230 * any value except -1) then the caller is responsible for closing the
231 * file descriptor.
232 */
233 if (fence_fd >= 0) {
234 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
235 *
236 * Waits indefinitely if timeout < 0.
237 */
238 int timeout = -1;
239 sync_wait(fence_fd, timeout);
240 close(fence_fd);
241 }
242
243 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
244
245 /* Record all the buffers created by ANativeWindow and update back buffer
246 * for updating buffer's age in swap_buffers.
247 */
248 EGLBoolean updated = EGL_FALSE;
249 for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
250 if (!dri2_surf->color_buffers[i].buffer) {
251 dri2_surf->color_buffers[i].buffer = dri2_surf->buffer;
252 }
253 if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) {
254 dri2_surf->back = &dri2_surf->color_buffers[i];
255 updated = EGL_TRUE;
256 break;
257 }
258 }
259
260 if (!updated) {
261 /* In case of all the buffers were recreated by ANativeWindow, reset
262 * the color_buffers
263 */
264 for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
265 dri2_surf->color_buffers[i].buffer = NULL;
266 dri2_surf->color_buffers[i].age = 0;
267 }
268 dri2_surf->color_buffers[0].buffer = dri2_surf->buffer;
269 dri2_surf->back = &dri2_surf->color_buffers[0];
270 }
271
272 return EGL_TRUE;
273 }
274
275 static EGLBoolean
276 droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
277 {
278 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
279
280 /* To avoid blocking other EGL calls, release the display mutex before
281 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
282 * return.
283 */
284 mtx_unlock(&disp->Mutex);
285
286 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
287 * consumer may choose to wait for the fence to signal before accessing
288 * it. If fence fd value is -1, buffer can be accessed by consumer
289 * immediately. Consumer or application shouldn't rely on timestamp
290 * associated with fence if the fence fd is -1.
291 *
292 * Ownership of fd is transferred to consumer after queueBuffer and the
293 * consumer is responsible for closing it. Caller must not use the fd
294 * after passing it to queueBuffer.
295 */
296 int fence_fd = dri2_surf->out_fence_fd;
297 dri2_surf->out_fence_fd = -1;
298 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
299 fence_fd);
300
301 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
302 dri2_surf->buffer = NULL;
303 dri2_surf->back = NULL;
304
305 mtx_lock(&disp->Mutex);
306
307 if (dri2_surf->dri_image_back) {
308 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
309 dri2_surf->dri_image_back = NULL;
310 }
311
312 return EGL_TRUE;
313 }
314
315 static void
316 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
317 {
318 int ret;
319 int fence_fd = dri2_surf->out_fence_fd;
320
321 dri2_surf->out_fence_fd = -1;
322 ret = dri2_surf->window->cancelBuffer(dri2_surf->window,
323 dri2_surf->buffer, fence_fd);
324 if (ret < 0) {
325 _eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed");
326 dri2_surf->base.Lost = EGL_TRUE;
327 }
328 }
329
330 static bool
331 droid_set_shared_buffer_mode(_EGLDisplay *disp, _EGLSurface *surf, bool mode)
332 {
333 #if ANDROID_API_LEVEL >= 24
334 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
335 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
336 struct ANativeWindow *window = dri2_surf->window;
337
338 assert(surf->Type == EGL_WINDOW_BIT);
339 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf->base));
340
341 _eglLog(_EGL_DEBUG, "%s: mode=%d", __func__, mode);
342
343 if (native_window_set_shared_buffer_mode(window, mode)) {
344 _eglLog(_EGL_WARNING, "failed native_window_set_shared_buffer_mode"
345 "(window=%p, mode=%d)", window, mode);
346 return false;
347 }
348
349 return true;
350 #else
351 _eglLog(_EGL_FATAL, "%s:%d: internal error: unreachable", __FILE__, __LINE__);
352 return false;
353 #endif
354 }
355
356 static _EGLSurface *
357 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
358 _EGLConfig *conf, void *native_window,
359 const EGLint *attrib_list)
360 {
361 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
362 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
363 struct dri2_egl_surface *dri2_surf;
364 struct ANativeWindow *window = native_window;
365 const __DRIconfig *config;
366
367 dri2_surf = calloc(1, sizeof *dri2_surf);
368 if (!dri2_surf) {
369 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
370 return NULL;
371 }
372
373 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
374 true, native_window))
375 goto cleanup_surface;
376
377 if (type == EGL_WINDOW_BIT) {
378 int format;
379 int buffer_count;
380
381 if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
382 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
383 goto cleanup_surface;
384 }
385 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
386 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
387 goto cleanup_surface;
388 }
389
390 /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, set buffer count
391 * and allocate color_buffers.
392 */
393 if (window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
394 &buffer_count)) {
395 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
396 goto cleanup_surface;
397 }
398 if (native_window_set_buffer_count(window, buffer_count+1)) {
399 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
400 goto cleanup_surface;
401 }
402 dri2_surf->color_buffers = calloc(buffer_count+1,
403 sizeof(*dri2_surf->color_buffers));
404 if (!dri2_surf->color_buffers) {
405 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
406 goto cleanup_surface;
407 }
408 dri2_surf->color_buffers_count = buffer_count+1;
409
410 if (format != dri2_conf->base.NativeVisualID) {
411 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
412 format, dri2_conf->base.NativeVisualID);
413 }
414
415 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
416 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
417 }
418
419 config = dri2_get_dri_config(dri2_conf, type,
420 dri2_surf->base.GLColorspace);
421 if (!config) {
422 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
423 goto cleanup_surface;
424 }
425
426 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
427 goto cleanup_surface;
428
429 if (window) {
430 window->common.incRef(&window->common);
431 dri2_surf->window = window;
432 }
433
434 return &dri2_surf->base;
435
436 cleanup_surface:
437 if (dri2_surf->color_buffers_count)
438 free(dri2_surf->color_buffers);
439 free(dri2_surf);
440
441 return NULL;
442 }
443
444 static _EGLSurface *
445 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
446 _EGLConfig *conf, void *native_window,
447 const EGLint *attrib_list)
448 {
449 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
450 native_window, attrib_list);
451 }
452
453 static _EGLSurface *
454 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
455 _EGLConfig *conf, const EGLint *attrib_list)
456 {
457 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
458 NULL, attrib_list);
459 }
460
461 static EGLBoolean
462 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
463 {
464 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
465 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
466
467 dri2_egl_surface_free_local_buffers(dri2_surf);
468
469 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
470 if (dri2_surf->buffer)
471 droid_window_cancel_buffer(dri2_surf);
472
473 dri2_surf->window->common.decRef(&dri2_surf->window->common);
474 }
475
476 if (dri2_surf->dri_image_back) {
477 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
478 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
479 dri2_surf->dri_image_back = NULL;
480 }
481
482 if (dri2_surf->dri_image_front) {
483 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
484 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
485 dri2_surf->dri_image_front = NULL;
486 }
487
488 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
489
490 dri2_fini_surface(surf);
491 free(dri2_surf->color_buffers);
492 free(dri2_surf);
493
494 return EGL_TRUE;
495 }
496
497 static EGLBoolean
498 droid_swap_interval(_EGLDriver *drv, _EGLDisplay *disp,
499 _EGLSurface *surf, EGLint interval)
500 {
501 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
502 struct ANativeWindow *window = dri2_surf->window;
503
504 if (window->setSwapInterval(window, interval))
505 return EGL_FALSE;
506
507 surf->SwapInterval = interval;
508 return EGL_TRUE;
509 }
510
511 static int
512 update_buffers(struct dri2_egl_surface *dri2_surf)
513 {
514 if (dri2_surf->base.Lost)
515 return -1;
516
517 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
518 return 0;
519
520 /* try to dequeue the next back buffer */
521 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
522 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
523 dri2_surf->base.Lost = EGL_TRUE;
524 return -1;
525 }
526
527 /* free outdated buffers and update the surface size */
528 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
529 dri2_surf->base.Height != dri2_surf->buffer->height) {
530 dri2_egl_surface_free_local_buffers(dri2_surf);
531 dri2_surf->base.Width = dri2_surf->buffer->width;
532 dri2_surf->base.Height = dri2_surf->buffer->height;
533 }
534
535 return 0;
536 }
537
538 static int
539 get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
540 {
541 struct dri2_egl_display *dri2_dpy =
542 dri2_egl_display(dri2_surf->base.Resource.Display);
543
544 if (dri2_surf->dri_image_front)
545 return 0;
546
547 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
548 /* According current EGL spec, front buffer rendering
549 * for window surface is not supported now.
550 * and mesa doesn't have the implementation of this case.
551 * Add warning message, but not treat it as error.
552 */
553 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
554 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
555 dri2_surf->dri_image_front =
556 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
557 dri2_surf->base.Width,
558 dri2_surf->base.Height,
559 format,
560 0,
561 dri2_surf);
562 if (!dri2_surf->dri_image_front) {
563 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
564 return -1;
565 }
566 }
567
568 return 0;
569 }
570
571 static int
572 get_back_bo(struct dri2_egl_surface *dri2_surf)
573 {
574 struct dri2_egl_display *dri2_dpy =
575 dri2_egl_display(dri2_surf->base.Resource.Display);
576 int fourcc, pitch;
577 int offset = 0, fds[3];
578 unsigned num_fds;
579
580 if (dri2_surf->dri_image_back)
581 return 0;
582
583 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
584 if (!dri2_surf->buffer) {
585 _eglLog(_EGL_WARNING, "Could not get native buffer");
586 return -1;
587 }
588
589 num_fds = get_native_buffer_fds(dri2_surf->buffer, fds);
590 if (num_fds == 0) {
591 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
592 return -1;
593 }
594
595 fourcc = get_fourcc(dri2_surf->buffer->format);
596
597 pitch = dri2_surf->buffer->stride *
598 get_format_bpp(dri2_surf->buffer->format);
599
600 if (fourcc == -1 || pitch == 0) {
601 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
602 fourcc, pitch);
603 return -1;
604 }
605
606 dri2_surf->dri_image_back =
607 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
608 dri2_surf->base.Width,
609 dri2_surf->base.Height,
610 fourcc,
611 fds,
612 num_fds,
613 &pitch,
614 &offset,
615 dri2_surf);
616 if (!dri2_surf->dri_image_back) {
617 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
618 return -1;
619 }
620 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
621 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
622 * the spec states that they have a back buffer but no front buffer, in
623 * contrast to pixmaps, which have a front buffer but no back buffer.
624 *
625 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
626 * from the spec, following the precedent of Mesa's EGL X11 platform. The
627 * X11 platform correctly assigns pbuffers to single-buffered configs, but
628 * assigns the pbuffer a front buffer instead of a back buffer.
629 *
630 * Pbuffers in the X11 platform mostly work today, so let's just copy its
631 * behavior instead of trying to fix (and hence potentially breaking) the
632 * world.
633 */
634 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
635 }
636
637 return 0;
638 }
639
640 /* Some drivers will pass multiple bits in buffer_mask.
641 * For such case, will go through all the bits, and
642 * will not return error when unsupported buffer is requested, only
643 * return error when the allocation for supported buffer failed.
644 */
645 static int
646 droid_image_get_buffers(__DRIdrawable *driDrawable,
647 unsigned int format,
648 uint32_t *stamp,
649 void *loaderPrivate,
650 uint32_t buffer_mask,
651 struct __DRIimageList *images)
652 {
653 struct dri2_egl_surface *dri2_surf = loaderPrivate;
654
655 images->image_mask = 0;
656 images->front = NULL;
657 images->back = NULL;
658
659 if (update_buffers(dri2_surf) < 0)
660 return 0;
661
662 if (_eglSurfaceInSharedBufferMode(&dri2_surf->base)) {
663 if (get_back_bo(dri2_surf) < 0)
664 return 0;
665
666 /* We have dri_image_back because this is a window surface and
667 * get_back_bo() succeeded.
668 */
669 assert(dri2_surf->dri_image_back);
670 images->back = dri2_surf->dri_image_back;
671 images->image_mask |= __DRI_IMAGE_BUFFER_SHARED;
672
673 /* There exists no accompanying back nor front buffer. */
674 return 1;
675 }
676
677 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
678 if (get_front_bo(dri2_surf, format) < 0)
679 return 0;
680
681 if (dri2_surf->dri_image_front) {
682 images->front = dri2_surf->dri_image_front;
683 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
684 }
685 }
686
687 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
688 if (get_back_bo(dri2_surf) < 0)
689 return 0;
690
691 if (dri2_surf->dri_image_back) {
692 images->back = dri2_surf->dri_image_back;
693 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
694 }
695 }
696
697 return 1;
698 }
699
700 static EGLint
701 droid_query_buffer_age(_EGLDriver *drv,
702 _EGLDisplay *disp, _EGLSurface *surface)
703 {
704 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
705
706 if (update_buffers(dri2_surf) < 0) {
707 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
708 return -1;
709 }
710
711 return dri2_surf->back ? dri2_surf->back->age : 0;
712 }
713
714 static EGLBoolean
715 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
716 {
717 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
718 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
719 const bool has_mutable_rb = _eglSurfaceHasMutableRenderBuffer(draw);
720
721 /* From the EGL_KHR_mutable_render_buffer spec (v12):
722 *
723 * If surface is a single-buffered window, pixmap, or pbuffer surface
724 * for which there is no pending change to the EGL_RENDER_BUFFER
725 * attribute, eglSwapBuffers has no effect.
726 */
727 if (has_mutable_rb &&
728 draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER &&
729 draw->ActiveRenderBuffer == EGL_SINGLE_BUFFER) {
730 _eglLog(_EGL_DEBUG, "%s: remain in shared buffer mode", __func__);
731 return EGL_TRUE;
732 }
733
734 for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
735 if (dri2_surf->color_buffers[i].age > 0)
736 dri2_surf->color_buffers[i].age++;
737 }
738
739 /* "XXX: we don't use get_back_bo() since it causes regressions in
740 * several dEQP tests.
741 */
742 if (dri2_surf->back)
743 dri2_surf->back->age = 1;
744
745 dri2_flush_drawable_for_swapbuffers(disp, draw);
746
747 /* dri2_surf->buffer can be null even when no error has occured. For
748 * example, if the user has called no GL rendering commands since the
749 * previous eglSwapBuffers, then the driver may have not triggered
750 * a callback to ANativeWindow::dequeueBuffer, in which case
751 * dri2_surf->buffer remains null.
752 */
753 if (dri2_surf->buffer)
754 droid_window_enqueue_buffer(disp, dri2_surf);
755
756 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
757
758 /* Update the shared buffer mode */
759 if (has_mutable_rb &&
760 draw->ActiveRenderBuffer != draw->RequestedRenderBuffer) {
761 bool mode = (draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER);
762 _eglLog(_EGL_DEBUG, "%s: change to shared buffer mode %d",
763 __func__, mode);
764
765 if (!droid_set_shared_buffer_mode(disp, draw, mode))
766 return EGL_FALSE;
767 draw->ActiveRenderBuffer = draw->RequestedRenderBuffer;
768 }
769
770 return EGL_TRUE;
771 }
772
773 static _EGLImage *
774 droid_create_image_from_prime_fds_yuv(_EGLDisplay *disp, _EGLContext *ctx,
775 struct ANativeWindowBuffer *buf,
776 int num_fds, int fds[3])
777 {
778 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
779 struct android_ycbcr ycbcr;
780 size_t offsets[3];
781 size_t pitches[3];
782 enum chroma_order chroma_order;
783 int fourcc;
784 int ret;
785
786 if (!dri2_dpy->gralloc->lock_ycbcr) {
787 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
788 return NULL;
789 }
790
791 memset(&ycbcr, 0, sizeof(ycbcr));
792 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
793 0, 0, 0, 0, 0, &ycbcr);
794 if (ret) {
795 /* HACK: See droid_create_image_from_prime_fds() and
796 * https://issuetracker.google.com/32077885.*/
797 if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
798 return NULL;
799
800 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
801 return NULL;
802 }
803 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
804
805 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
806 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
807 * so they can be interpreted as offsets. */
808 offsets[0] = (size_t)ycbcr.y;
809 /* We assume here that all the planes are located in one DMA-buf. */
810 if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
811 chroma_order = YCrCb;
812 offsets[1] = (size_t)ycbcr.cr;
813 offsets[2] = (size_t)ycbcr.cb;
814 } else {
815 chroma_order = YCbCr;
816 offsets[1] = (size_t)ycbcr.cb;
817 offsets[2] = (size_t)ycbcr.cr;
818 }
819
820 /* .ystride is the line length (in bytes) of the Y plane,
821 * .cstride is the line length (in bytes) of any of the remaining
822 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
823 * planar formats. */
824 pitches[0] = ycbcr.ystride;
825 pitches[1] = pitches[2] = ycbcr.cstride;
826
827 /* .chroma_step is the byte distance between the same chroma channel
828 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
829 fourcc = get_fourcc_yuv(buf->format, chroma_order, ycbcr.chroma_step);
830 if (fourcc == -1) {
831 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
832 buf->format, chroma_order == YCbCr ? "YCbCr" : "YCrCb", ycbcr.chroma_step);
833 return NULL;
834 }
835
836 /*
837 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
838 * the single-fd case cannot happen. So handle eithe single
839 * fd or fd-per-plane case:
840 */
841 if (num_fds == 1) {
842 fds[2] = fds[1] = fds[0];
843 } else {
844 int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
845 assert(num_fds == expected_planes);
846 }
847
848 if (ycbcr.chroma_step == 2) {
849 /* Semi-planar Y + CbCr or Y + CrCb format. */
850 const EGLint attr_list_2plane[] = {
851 EGL_WIDTH, buf->width,
852 EGL_HEIGHT, buf->height,
853 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
854 EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
855 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
856 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
857 EGL_DMA_BUF_PLANE1_FD_EXT, fds[1],
858 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
859 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
860 EGL_NONE, 0
861 };
862
863 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
864 } else {
865 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
866 const EGLint attr_list_3plane[] = {
867 EGL_WIDTH, buf->width,
868 EGL_HEIGHT, buf->height,
869 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
870 EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
871 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
872 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
873 EGL_DMA_BUF_PLANE1_FD_EXT, fds[1],
874 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
875 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
876 EGL_DMA_BUF_PLANE2_FD_EXT, fds[2],
877 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
878 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
879 EGL_NONE, 0
880 };
881
882 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
883 }
884 }
885
886 static _EGLImage *
887 droid_create_image_from_prime_fds(_EGLDisplay *disp, _EGLContext *ctx,
888 struct ANativeWindowBuffer *buf, int num_fds, int fds[3])
889 {
890 unsigned int pitch;
891
892 if (is_yuv(buf->format)) {
893 _EGLImage *image;
894
895 image = droid_create_image_from_prime_fds_yuv(disp, ctx, buf, num_fds, fds);
896 /*
897 * HACK: https://issuetracker.google.com/32077885
898 * There is no API available to properly query the IMPLEMENTATION_DEFINED
899 * format. As a workaround we rely here on gralloc allocating either
900 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
901 * by lock_ycbcr failing.
902 */
903 if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
904 return image;
905 }
906
907 /*
908 * Non-YUV formats could *also* have multiple planes, such as ancillary
909 * color compression state buffer, but the rest of the code isn't ready
910 * yet to deal with modifiers:
911 */
912 assert(num_fds == 1);
913
914 const int fourcc = get_fourcc(buf->format);
915 if (fourcc == -1) {
916 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
917 return NULL;
918 }
919
920 pitch = buf->stride * get_format_bpp(buf->format);
921 if (pitch == 0) {
922 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
923 return NULL;
924 }
925
926 const EGLint attr_list[] = {
927 EGL_WIDTH, buf->width,
928 EGL_HEIGHT, buf->height,
929 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
930 EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
931 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
932 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
933 EGL_NONE, 0
934 };
935
936 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
937 }
938
939 #ifdef HAVE_DRM_GRALLOC
940 static _EGLImage *
941 droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
942 struct ANativeWindowBuffer *buf)
943 {
944 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
945 struct dri2_egl_image *dri2_img;
946 int name;
947 int format;
948
949 name = get_native_buffer_name(buf);
950 if (!name) {
951 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
952 return NULL;
953 }
954
955 format = get_format(buf->format);
956 if (format == -1)
957 return NULL;
958
959 dri2_img = calloc(1, sizeof(*dri2_img));
960 if (!dri2_img) {
961 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
962 return NULL;
963 }
964
965 _eglInitImage(&dri2_img->base, disp);
966
967 dri2_img->dri_image =
968 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
969 buf->width,
970 buf->height,
971 format,
972 name,
973 buf->stride,
974 dri2_img);
975 if (!dri2_img->dri_image) {
976 free(dri2_img);
977 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
978 return NULL;
979 }
980
981 return &dri2_img->base;
982 }
983 #endif /* HAVE_DRM_GRALLOC */
984
985 static EGLBoolean
986 droid_query_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
987 EGLint attribute, EGLint *value)
988 {
989 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
990 switch (attribute) {
991 case EGL_WIDTH:
992 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
993 dri2_surf->window->query(dri2_surf->window,
994 NATIVE_WINDOW_DEFAULT_WIDTH, value);
995 return EGL_TRUE;
996 }
997 break;
998 case EGL_HEIGHT:
999 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
1000 dri2_surf->window->query(dri2_surf->window,
1001 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
1002 return EGL_TRUE;
1003 }
1004 break;
1005 default:
1006 break;
1007 }
1008 return _eglQuerySurface(drv, disp, surf, attribute, value);
1009 }
1010
1011 static _EGLImage *
1012 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
1013 _EGLContext *ctx,
1014 struct ANativeWindowBuffer *buf)
1015 {
1016 int fds[3];
1017 unsigned num_fds;
1018
1019 if (ctx != NULL) {
1020 /* From the EGL_ANDROID_image_native_buffer spec:
1021 *
1022 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1023 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1024 */
1025 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
1026 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1027 "EGL_NO_CONTEXT");
1028 return NULL;
1029 }
1030
1031 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
1032 buf->common.version != sizeof(*buf)) {
1033 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
1034 return NULL;
1035 }
1036
1037 num_fds = get_native_buffer_fds(buf, fds);
1038 if (num_fds > 0)
1039 return droid_create_image_from_prime_fds(disp, ctx, buf, num_fds, fds);
1040
1041 #ifdef HAVE_DRM_GRALLOC
1042 return droid_create_image_from_name(disp, ctx, buf);
1043 #else
1044 return NULL;
1045 #endif
1046 }
1047
1048 static _EGLImage *
1049 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1050 _EGLContext *ctx, EGLenum target,
1051 EGLClientBuffer buffer, const EGLint *attr_list)
1052 {
1053 switch (target) {
1054 case EGL_NATIVE_BUFFER_ANDROID:
1055 return dri2_create_image_android_native_buffer(disp, ctx,
1056 (struct ANativeWindowBuffer *) buffer);
1057 default:
1058 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
1059 }
1060 }
1061
1062 static void
1063 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
1064 {
1065 }
1066
1067 #ifdef HAVE_DRM_GRALLOC
1068 static int
1069 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
1070 unsigned int *attachments, int count)
1071 {
1072 int num_buffers = 0;
1073
1074 /* fill dri2_surf->buffers */
1075 for (int i = 0; i < count * 2; i += 2) {
1076 __DRIbuffer *buf, *local;
1077
1078 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
1079 buf = &dri2_surf->buffers[num_buffers];
1080
1081 switch (attachments[i]) {
1082 case __DRI_BUFFER_BACK_LEFT:
1083 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
1084 buf->attachment = attachments[i];
1085 buf->name = get_native_buffer_name(dri2_surf->buffer);
1086 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
1087 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
1088 buf->flags = 0;
1089
1090 if (buf->name)
1091 num_buffers++;
1092
1093 break;
1094 }
1095 /* fall through for pbuffers */
1096 case __DRI_BUFFER_DEPTH:
1097 case __DRI_BUFFER_STENCIL:
1098 case __DRI_BUFFER_ACCUM:
1099 case __DRI_BUFFER_DEPTH_STENCIL:
1100 case __DRI_BUFFER_HIZ:
1101 local = dri2_egl_surface_alloc_local_buffer(dri2_surf,
1102 attachments[i], attachments[i + 1]);
1103
1104 if (local) {
1105 *buf = *local;
1106 num_buffers++;
1107 }
1108 break;
1109 case __DRI_BUFFER_FRONT_LEFT:
1110 case __DRI_BUFFER_FRONT_RIGHT:
1111 case __DRI_BUFFER_FAKE_FRONT_LEFT:
1112 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
1113 case __DRI_BUFFER_BACK_RIGHT:
1114 default:
1115 /* no front or right buffers */
1116 break;
1117 }
1118 }
1119
1120 return num_buffers;
1121 }
1122
1123 static __DRIbuffer *
1124 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
1125 int *width, int *height,
1126 unsigned int *attachments, int count,
1127 int *out_count, void *loaderPrivate)
1128 {
1129 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1130
1131 if (update_buffers(dri2_surf) < 0)
1132 return NULL;
1133
1134 *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
1135
1136 if (width)
1137 *width = dri2_surf->base.Width;
1138 if (height)
1139 *height = dri2_surf->base.Height;
1140
1141 return dri2_surf->buffers;
1142 }
1143 #endif /* HAVE_DRM_GRALLOC */
1144
1145 static unsigned
1146 droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
1147 {
1148 /* Note: loaderPrivate is _EGLDisplay* */
1149 switch (cap) {
1150 case DRI_LOADER_CAP_RGBA_ORDERING:
1151 return 1;
1152 default:
1153 return 0;
1154 }
1155 }
1156
1157 static EGLBoolean
1158 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1159 {
1160 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1161 static const struct {
1162 int format;
1163 int rgba_shifts[4];
1164 unsigned int rgba_sizes[4];
1165 } visuals[] = {
1166 { HAL_PIXEL_FORMAT_RGBA_8888, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1167 { HAL_PIXEL_FORMAT_RGBX_8888, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1168 { HAL_PIXEL_FORMAT_RGB_565, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
1169 /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
1170 * visual if it turns out RGBA visual is not available.
1171 */
1172 { HAL_PIXEL_FORMAT_BGRA_8888, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
1173 };
1174
1175 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1176 int config_count = 0;
1177
1178 /* The nesting of loops is significant here. Also significant is the order
1179 * of the HAL pixel formats. Many Android apps (such as Google's official
1180 * NDK GLES2 example app), and even portions the core framework code (such
1181 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1182 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1183 * window's native format, and instead choose the first EGLConfig whose
1184 * channel sizes match those of the native window format while ignoring the
1185 * channel *ordering*.
1186 *
1187 * We can detect such buggy clients in logcat when they call
1188 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1189 * format and the window's format.
1190 *
1191 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1192 * pixel format i precede those for HAL pixel format i+1. In my
1193 * (chadversary) testing on Android Nougat, this was good enough to pacify
1194 * the buggy clients.
1195 */
1196 bool has_rgba = false;
1197 for (int i = 0; i < ARRAY_SIZE(visuals); i++) {
1198 /* Only enable BGRA configs when RGBA is not available. BGRA configs are
1199 * buggy on stock Android.
1200 */
1201 if (visuals[i].format == HAL_PIXEL_FORMAT_BGRA_8888 && has_rgba)
1202 continue;
1203 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
1204 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1205
1206 const EGLint config_attrs[] = {
1207 EGL_NATIVE_VISUAL_ID, visuals[i].format,
1208 EGL_NATIVE_VISUAL_TYPE, visuals[i].format,
1209 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1210 EGL_RECORDABLE_ANDROID, EGL_TRUE,
1211 EGL_NONE
1212 };
1213
1214 struct dri2_egl_config *dri2_conf =
1215 dri2_add_config(disp, dri2_dpy->driver_configs[j],
1216 config_count + 1, surface_type, config_attrs,
1217 visuals[i].rgba_shifts, visuals[i].rgba_sizes);
1218 if (dri2_conf) {
1219 if (dri2_conf->base.ConfigID == config_count + 1)
1220 config_count++;
1221 format_count[i]++;
1222 }
1223 }
1224 if (visuals[i].format == HAL_PIXEL_FORMAT_RGBA_8888 && format_count[i])
1225 has_rgba = true;
1226 }
1227
1228 for (int i = 0; i < ARRAY_SIZE(format_count); i++) {
1229 if (!format_count[i]) {
1230 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
1231 visuals[i].format);
1232 }
1233 }
1234
1235 return (config_count != 0);
1236 }
1237
1238 static const struct dri2_egl_display_vtbl droid_display_vtbl = {
1239 .authenticate = NULL,
1240 .create_window_surface = droid_create_window_surface,
1241 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
1242 .create_pbuffer_surface = droid_create_pbuffer_surface,
1243 .destroy_surface = droid_destroy_surface,
1244 .create_image = droid_create_image_khr,
1245 .swap_buffers = droid_swap_buffers,
1246 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, /* Android implements the function */
1247 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1248 .swap_interval = droid_swap_interval,
1249 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1250 .copy_buffers = dri2_fallback_copy_buffers,
1251 .query_buffer_age = droid_query_buffer_age,
1252 .query_surface = droid_query_surface,
1253 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1254 .get_sync_values = dri2_fallback_get_sync_values,
1255 .get_dri_drawable = dri2_surface_get_dri_drawable,
1256 .set_shared_buffer_mode = droid_set_shared_buffer_mode,
1257 };
1258
1259 #ifdef HAVE_DRM_GRALLOC
1260 static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1261 .base = { __DRI_DRI2_LOADER, 4 },
1262
1263 .getBuffers = NULL,
1264 .flushFrontBuffer = droid_flush_front_buffer,
1265 .getBuffersWithFormat = droid_get_buffers_with_format,
1266 .getCapability = droid_get_capability,
1267 };
1268
1269 static const __DRIextension *droid_dri2_loader_extensions[] = {
1270 &droid_dri2_loader_extension.base,
1271 &image_lookup_extension.base,
1272 &use_invalidate.base,
1273 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1274 * __DRI_IMAGE_LOADER.
1275 */
1276 NULL,
1277 };
1278 #endif /* HAVE_DRM_GRALLOC */
1279
1280 static const __DRIimageLoaderExtension droid_image_loader_extension = {
1281 .base = { __DRI_IMAGE_LOADER, 2 },
1282
1283 .getBuffers = droid_image_get_buffers,
1284 .flushFrontBuffer = droid_flush_front_buffer,
1285 .getCapability = droid_get_capability,
1286 };
1287
1288 static void
1289 droid_display_shared_buffer(__DRIdrawable *driDrawable, int fence_fd,
1290 void *loaderPrivate)
1291 {
1292 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1293 struct ANativeWindowBuffer *old_buffer UNUSED = dri2_surf->buffer;
1294
1295 if (!_eglSurfaceInSharedBufferMode(&dri2_surf->base)) {
1296 _eglLog(_EGL_WARNING, "%s: internal error: buffer is not shared",
1297 __func__);
1298 return;
1299 }
1300
1301 if (fence_fd >= 0) {
1302 /* The driver's fence is more recent than the surface's out fence, if it
1303 * exists at all. So use the driver's fence.
1304 */
1305 if (dri2_surf->out_fence_fd >= 0) {
1306 close(dri2_surf->out_fence_fd);
1307 dri2_surf->out_fence_fd = -1;
1308 }
1309 } else if (dri2_surf->out_fence_fd >= 0) {
1310 fence_fd = dri2_surf->out_fence_fd;
1311 dri2_surf->out_fence_fd = -1;
1312 }
1313
1314 if (dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
1315 fence_fd)) {
1316 _eglLog(_EGL_WARNING, "%s: ANativeWindow::queueBuffer failed", __func__);
1317 close(fence_fd);
1318 return;
1319 }
1320
1321 fence_fd = -1;
1322
1323 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
1324 &fence_fd)) {
1325 /* Tear down the surface because it no longer has a back buffer. */
1326 struct dri2_egl_display *dri2_dpy =
1327 dri2_egl_display(dri2_surf->base.Resource.Display);
1328
1329 _eglLog(_EGL_WARNING, "%s: ANativeWindow::dequeueBuffer failed", __func__);
1330
1331 dri2_surf->base.Lost = true;
1332 dri2_surf->buffer = NULL;
1333 dri2_surf->back = NULL;
1334
1335 if (dri2_surf->dri_image_back) {
1336 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
1337 dri2_surf->dri_image_back = NULL;
1338 }
1339
1340 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
1341 return;
1342 }
1343
1344 if (fence_fd < 0)
1345 return;
1346
1347 /* Access to the buffer is controlled by a sync fence. Block on it.
1348 *
1349 * Ideally, we would submit the fence to the driver, and the driver would
1350 * postpone command execution until it signalled. But DRI lacks API for
1351 * that (as of 2018-04-11).
1352 *
1353 * SYNC_IOC_WAIT waits forever if timeout < 0
1354 */
1355 sync_wait(fence_fd, -1);
1356 close(fence_fd);
1357 }
1358
1359 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension = {
1360 .base = { __DRI_MUTABLE_RENDER_BUFFER_LOADER, 1 },
1361 .displaySharedBuffer = droid_display_shared_buffer,
1362 };
1363
1364 static const __DRIextension *droid_image_loader_extensions[] = {
1365 &droid_image_loader_extension.base,
1366 &image_lookup_extension.base,
1367 &use_invalidate.base,
1368 &droid_mutable_render_buffer_extension.base,
1369 NULL,
1370 };
1371
1372 static EGLBoolean
1373 droid_load_driver(_EGLDisplay *disp, bool swrast)
1374 {
1375 struct dri2_egl_display *dri2_dpy = disp->DriverData;
1376 const char *err;
1377
1378 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1379 if (dri2_dpy->driver_name == NULL)
1380 return false;
1381
1382 #ifdef HAVE_DRM_GRALLOC
1383 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1384 * for backwards compatibility with drm_gralloc. (Do not use on new
1385 * systems.) */
1386 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
1387 if (!dri2_load_driver(disp)) {
1388 err = "DRI2: failed to load driver";
1389 goto error;
1390 }
1391 #else
1392 if (swrast) {
1393 /* Use kms swrast only with vgem / virtio_gpu.
1394 * virtio-gpu fallbacks to software rendering when 3D features
1395 * are unavailable since 6c5ab.
1396 */
1397 if (strcmp(dri2_dpy->driver_name, "vgem") == 0 ||
1398 strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0) {
1399 free(dri2_dpy->driver_name);
1400 dri2_dpy->driver_name = strdup("kms_swrast");
1401 } else {
1402 err = "DRI3: failed to find software capable driver";
1403 goto error;
1404 }
1405 }
1406
1407 dri2_dpy->loader_extensions = droid_image_loader_extensions;
1408 if (!dri2_load_driver_dri3(disp)) {
1409 err = "DRI3: failed to load driver";
1410 goto error;
1411 }
1412 #endif
1413
1414 return true;
1415
1416 error:
1417 free(dri2_dpy->driver_name);
1418 dri2_dpy->driver_name = NULL;
1419 return false;
1420 }
1421
1422 static void
1423 droid_unload_driver(_EGLDisplay *disp)
1424 {
1425 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1426
1427 dlclose(dri2_dpy->driver);
1428 dri2_dpy->driver = NULL;
1429 free(dri2_dpy->driver_name);
1430 dri2_dpy->driver_name = NULL;
1431 }
1432
1433 static int
1434 droid_filter_device(_EGLDisplay *disp, int fd, const char *vendor)
1435 {
1436 drmVersionPtr ver = drmGetVersion(fd);
1437 if (!ver)
1438 return -1;
1439
1440 if (strcmp(vendor, ver->name) != 0) {
1441 drmFreeVersion(ver);
1442 return -1;
1443 }
1444
1445 drmFreeVersion(ver);
1446 return 0;
1447 }
1448
1449 static EGLBoolean
1450 droid_probe_device(_EGLDisplay *disp, bool swrast)
1451 {
1452 /* Check that the device is supported, by attempting to:
1453 * - load the dri module
1454 * - and, create a screen
1455 */
1456 if (!droid_load_driver(disp, swrast))
1457 return EGL_FALSE;
1458
1459 if (!dri2_create_screen(disp)) {
1460 _eglLog(_EGL_WARNING, "DRI2: failed to create screen");
1461 droid_unload_driver(disp);
1462 return EGL_FALSE;
1463 }
1464 return EGL_TRUE;
1465 }
1466
1467 #ifdef HAVE_DRM_GRALLOC
1468 static EGLBoolean
1469 droid_open_device(_EGLDisplay *disp, bool swrast)
1470 {
1471 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1472 int fd = -1, err = -EINVAL;
1473
1474 if (swrast)
1475 return EGL_FALSE;
1476
1477 if (dri2_dpy->gralloc->perform)
1478 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1479 GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1480 &fd);
1481 if (err || fd < 0) {
1482 _eglLog(_EGL_WARNING, "fail to get drm fd");
1483 return EGL_FALSE;
1484 }
1485
1486 dri2_dpy->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
1487 if (dri2_dpy->fd < 0)
1488 return EGL_FALSE;
1489
1490 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER)
1491 return EGL_FALSE;
1492
1493 return droid_probe_device(disp, swrast);
1494 }
1495 #else
1496 static EGLBoolean
1497 droid_open_device(_EGLDisplay *disp, bool swrast)
1498 {
1499 #define MAX_DRM_DEVICES 64
1500 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1501 drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
1502 int num_devices;
1503
1504 char *vendor_name = NULL;
1505 char vendor_buf[PROPERTY_VALUE_MAX];
1506
1507 #ifdef EGL_FORCE_RENDERNODE
1508 const unsigned node_type = DRM_NODE_RENDER;
1509 #else
1510 const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
1511 #endif
1512
1513 if (property_get("drm.gpu.vendor_name", vendor_buf, NULL) > 0)
1514 vendor_name = vendor_buf;
1515
1516 num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
1517 if (num_devices < 0)
1518 return EGL_FALSE;
1519
1520 for (int i = 0; i < num_devices; i++) {
1521 device = devices[i];
1522
1523 if (!(device->available_nodes & (1 << node_type)))
1524 continue;
1525
1526 dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
1527 if (dri2_dpy->fd < 0) {
1528 _eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s",
1529 __func__, device->nodes[node_type]);
1530 continue;
1531 }
1532
1533 /* If a vendor is explicitly provided, we use only that.
1534 * Otherwise we fall-back the first device that is supported.
1535 */
1536 if (vendor_name) {
1537 if (droid_filter_device(disp, dri2_dpy->fd, vendor_name)) {
1538 /* Device does not match - try next device */
1539 close(dri2_dpy->fd);
1540 dri2_dpy->fd = -1;
1541 continue;
1542 }
1543 /* If the requested device matches - use it. Regardless if
1544 * init fails, do not fall-back to any other device.
1545 */
1546 if (!droid_probe_device(disp, false)) {
1547 close(dri2_dpy->fd);
1548 dri2_dpy->fd = -1;
1549 }
1550
1551 break;
1552 }
1553 if (droid_probe_device(disp, swrast))
1554 break;
1555
1556 /* No explicit request - attempt the next device */
1557 close(dri2_dpy->fd);
1558 dri2_dpy->fd = -1;
1559 }
1560 drmFreeDevices(devices, num_devices);
1561
1562 if (dri2_dpy->fd < 0) {
1563 _eglLog(_EGL_WARNING, "Failed to open %s DRM device",
1564 vendor_name ? "desired": "any");
1565 return EGL_FALSE;
1566 }
1567
1568 return EGL_TRUE;
1569 #undef MAX_DRM_DEVICES
1570 }
1571
1572 #endif
1573
1574 EGLBoolean
1575 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp)
1576 {
1577 _EGLDevice *dev;
1578 bool device_opened = false;
1579 struct dri2_egl_display *dri2_dpy;
1580 const char *err;
1581 int ret;
1582
1583 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1584 if (!dri2_dpy)
1585 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1586
1587 dri2_dpy->fd = -1;
1588 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1589 (const hw_module_t **)&dri2_dpy->gralloc);
1590 if (ret) {
1591 err = "DRI2: failed to get gralloc module";
1592 goto cleanup;
1593 }
1594
1595 disp->DriverData = (void *) dri2_dpy;
1596 if (!disp->Options.ForceSoftware)
1597 device_opened = droid_open_device(disp, false);
1598 if (!device_opened)
1599 device_opened = droid_open_device(disp, true);
1600
1601 if (!device_opened) {
1602 err = "DRI2: failed to open device";
1603 goto cleanup;
1604 }
1605
1606 dev = _eglAddDevice(dri2_dpy->fd, false);
1607 if (!dev) {
1608 err = "DRI2: failed to find EGLDevice";
1609 goto cleanup;
1610 }
1611
1612 disp->Device = dev;
1613
1614 if (!dri2_setup_extensions(disp)) {
1615 err = "DRI2: failed to setup extensions";
1616 goto cleanup;
1617 }
1618
1619 dri2_setup_screen(disp);
1620
1621 /* We set the maximum swap interval as 1 for Android platform, since it is
1622 * the maximum value supported by Android according to the value of
1623 * ANativeWindow::maxSwapInterval.
1624 */
1625 dri2_setup_swap_interval(disp, 1);
1626
1627 disp->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
1628 disp->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
1629 disp->Extensions.ANDROID_recordable = EGL_TRUE;
1630 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1631 disp->Extensions.KHR_image = EGL_TRUE;
1632 #if ANDROID_API_LEVEL >= 24
1633 if (dri2_dpy->mutable_render_buffer &&
1634 dri2_dpy->loader_extensions == droid_image_loader_extensions) {
1635 disp->Extensions.KHR_mutable_render_buffer = EGL_TRUE;
1636 }
1637 #endif
1638
1639 /* Create configs *after* enabling extensions because presence of DRI
1640 * driver extensions can affect the capabilities of EGLConfigs.
1641 */
1642 if (!droid_add_configs_for_visuals(drv, disp)) {
1643 err = "DRI2: failed to add configs";
1644 goto cleanup;
1645 }
1646
1647 /* Fill vtbl last to prevent accidentally calling virtual function during
1648 * initialization.
1649 */
1650 dri2_dpy->vtbl = &droid_display_vtbl;
1651
1652 return EGL_TRUE;
1653
1654 cleanup:
1655 dri2_display_destroy(disp);
1656 return _eglError(EGL_NOT_INITIALIZED, err);
1657 }