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