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