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