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