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