cc16fd8118f32bf1d09fbd274ddd01615a4a6cde
[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 _EGLSurface *
307 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
308 _EGLConfig *conf, void *native_window,
309 const EGLint *attrib_list)
310 {
311 __DRIcreateNewDrawableFunc createNewDrawable;
312 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
313 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
314 struct dri2_egl_surface *dri2_surf;
315 struct ANativeWindow *window = native_window;
316 const __DRIconfig *config;
317
318 dri2_surf = calloc(1, sizeof *dri2_surf);
319 if (!dri2_surf) {
320 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
321 return NULL;
322 }
323
324 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true))
325 goto cleanup_surface;
326
327 if (type == EGL_WINDOW_BIT) {
328 int format;
329
330 if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
331 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
332 goto cleanup_surface;
333 }
334 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
335 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
336 goto cleanup_surface;
337 }
338
339 if (format != dri2_conf->base.NativeVisualID) {
340 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
341 format, dri2_conf->base.NativeVisualID);
342 }
343
344 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
345 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
346 }
347
348 config = dri2_get_dri_config(dri2_conf, type,
349 dri2_surf->base.GLColorspace);
350 if (!config) {
351 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
352 goto cleanup_surface;
353 }
354
355 if (dri2_dpy->image_driver)
356 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
357 else
358 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
359
360 dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
361 dri2_surf);
362 if (dri2_surf->dri_drawable == NULL) {
363 _eglError(EGL_BAD_ALLOC, "createNewDrawable");
364 goto cleanup_surface;
365 }
366
367 if (window) {
368 window->common.incRef(&window->common);
369 dri2_surf->window = window;
370 }
371
372 return &dri2_surf->base;
373
374 cleanup_surface:
375 free(dri2_surf);
376
377 return NULL;
378 }
379
380 static _EGLSurface *
381 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
382 _EGLConfig *conf, void *native_window,
383 const EGLint *attrib_list)
384 {
385 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
386 native_window, attrib_list);
387 }
388
389 static _EGLSurface *
390 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
391 _EGLConfig *conf, const EGLint *attrib_list)
392 {
393 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
394 NULL, attrib_list);
395 }
396
397 static EGLBoolean
398 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
399 {
400 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
401 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
402
403 dri2_egl_surface_free_local_buffers(dri2_surf);
404
405 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
406 if (dri2_surf->buffer)
407 droid_window_cancel_buffer(dri2_surf);
408
409 dri2_surf->window->common.decRef(&dri2_surf->window->common);
410 }
411
412 if (dri2_surf->dri_image_back) {
413 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
414 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
415 dri2_surf->dri_image_back = NULL;
416 }
417
418 if (dri2_surf->dri_image_front) {
419 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
420 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
421 dri2_surf->dri_image_front = NULL;
422 }
423
424 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
425
426 dri2_fini_surface(surf);
427 free(dri2_surf);
428
429 return EGL_TRUE;
430 }
431
432 static EGLBoolean
433 droid_swap_interval(_EGLDriver *drv, _EGLDisplay *dpy,
434 _EGLSurface *surf, EGLint interval)
435 {
436 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
437 struct ANativeWindow *window = dri2_surf->window;
438
439 if (window->setSwapInterval(window, interval))
440 return EGL_FALSE;
441
442 surf->SwapInterval = interval;
443 return EGL_TRUE;
444 }
445
446 static int
447 update_buffers(struct dri2_egl_surface *dri2_surf)
448 {
449 if (dri2_surf->base.Lost)
450 return -1;
451
452 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
453 return 0;
454
455 /* try to dequeue the next back buffer */
456 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
457 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
458 dri2_surf->base.Lost = EGL_TRUE;
459 return -1;
460 }
461
462 /* free outdated buffers and update the surface size */
463 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
464 dri2_surf->base.Height != dri2_surf->buffer->height) {
465 dri2_egl_surface_free_local_buffers(dri2_surf);
466 dri2_surf->base.Width = dri2_surf->buffer->width;
467 dri2_surf->base.Height = dri2_surf->buffer->height;
468 }
469
470 return 0;
471 }
472
473 static int
474 get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
475 {
476 struct dri2_egl_display *dri2_dpy =
477 dri2_egl_display(dri2_surf->base.Resource.Display);
478
479 if (dri2_surf->dri_image_front)
480 return 0;
481
482 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
483 /* According current EGL spec, front buffer rendering
484 * for window surface is not supported now.
485 * and mesa doesn't have the implementation of this case.
486 * Add warning message, but not treat it as error.
487 */
488 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
489 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
490 dri2_surf->dri_image_front =
491 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
492 dri2_surf->base.Width,
493 dri2_surf->base.Height,
494 format,
495 0,
496 dri2_surf);
497 if (!dri2_surf->dri_image_front) {
498 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
499 return -1;
500 }
501 }
502
503 return 0;
504 }
505
506 static int
507 get_back_bo(struct dri2_egl_surface *dri2_surf)
508 {
509 struct dri2_egl_display *dri2_dpy =
510 dri2_egl_display(dri2_surf->base.Resource.Display);
511 int fourcc, pitch;
512 int offset = 0, fd;
513
514 if (dri2_surf->dri_image_back)
515 return 0;
516
517 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
518 if (!dri2_surf->buffer) {
519 _eglLog(_EGL_WARNING, "Could not get native buffer");
520 return -1;
521 }
522
523 fd = get_native_buffer_fd(dri2_surf->buffer);
524 if (fd < 0) {
525 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
526 return -1;
527 }
528
529 fourcc = get_fourcc(dri2_surf->buffer->format);
530
531 pitch = dri2_surf->buffer->stride *
532 get_format_bpp(dri2_surf->buffer->format);
533
534 if (fourcc == -1 || pitch == 0) {
535 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
536 fourcc, pitch);
537 return -1;
538 }
539
540 dri2_surf->dri_image_back =
541 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
542 dri2_surf->base.Width,
543 dri2_surf->base.Height,
544 fourcc,
545 &fd,
546 1,
547 &pitch,
548 &offset,
549 dri2_surf);
550 if (!dri2_surf->dri_image_back) {
551 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
552 return -1;
553 }
554 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
555 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
556 * the spec states that they have a back buffer but no front buffer, in
557 * contrast to pixmaps, which have a front buffer but no back buffer.
558 *
559 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
560 * from the spec, following the precedent of Mesa's EGL X11 platform. The
561 * X11 platform correctly assigns pbuffers to single-buffered configs, but
562 * assigns the pbuffer a front buffer instead of a back buffer.
563 *
564 * Pbuffers in the X11 platform mostly work today, so let's just copy its
565 * behavior instead of trying to fix (and hence potentially breaking) the
566 * world.
567 */
568 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
569 }
570
571 return 0;
572 }
573
574 /* Some drivers will pass multiple bits in buffer_mask.
575 * For such case, will go through all the bits, and
576 * will not return error when unsupported buffer is requested, only
577 * return error when the allocation for supported buffer failed.
578 */
579 static int
580 droid_image_get_buffers(__DRIdrawable *driDrawable,
581 unsigned int format,
582 uint32_t *stamp,
583 void *loaderPrivate,
584 uint32_t buffer_mask,
585 struct __DRIimageList *images)
586 {
587 struct dri2_egl_surface *dri2_surf = loaderPrivate;
588
589 images->image_mask = 0;
590 images->front = NULL;
591 images->back = NULL;
592
593 if (update_buffers(dri2_surf) < 0)
594 return 0;
595
596 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
597 if (get_front_bo(dri2_surf, format) < 0)
598 return 0;
599
600 if (dri2_surf->dri_image_front) {
601 images->front = dri2_surf->dri_image_front;
602 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
603 }
604 }
605
606 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
607 if (get_back_bo(dri2_surf) < 0)
608 return 0;
609
610 if (dri2_surf->dri_image_back) {
611 images->back = dri2_surf->dri_image_back;
612 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
613 }
614 }
615
616 return 1;
617 }
618
619 static EGLint
620 droid_query_buffer_age(_EGLDriver *drv,
621 _EGLDisplay *disp, _EGLSurface *surface)
622 {
623 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
624
625 if (update_buffers(dri2_surf) < 0) {
626 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
627 return -1;
628 }
629
630 return dri2_surf->back ? dri2_surf->back->age : 0;
631 }
632
633 static EGLBoolean
634 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
635 {
636 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
637 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
638
639 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
640 return EGL_TRUE;
641
642 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
643 if (dri2_surf->color_buffers[i].age > 0)
644 dri2_surf->color_buffers[i].age++;
645 }
646
647 /* "XXX: we don't use get_back_bo() since it causes regressions in
648 * several dEQP tests.
649 */
650 if (dri2_surf->back)
651 dri2_surf->back->age = 1;
652
653 dri2_flush_drawable_for_swapbuffers(disp, draw);
654
655 /* dri2_surf->buffer can be null even when no error has occured. For
656 * example, if the user has called no GL rendering commands since the
657 * previous eglSwapBuffers, then the driver may have not triggered
658 * a callback to ANativeWindow::dequeueBuffer, in which case
659 * dri2_surf->buffer remains null.
660 */
661 if (dri2_surf->buffer)
662 droid_window_enqueue_buffer(disp, dri2_surf);
663
664 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
665
666 return EGL_TRUE;
667 }
668
669 #if ANDROID_API_LEVEL >= 23
670 static EGLBoolean
671 droid_set_damage_region(_EGLDriver *drv,
672 _EGLDisplay *disp,
673 _EGLSurface *draw, const EGLint* rects, EGLint n_rects)
674 {
675 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
676 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
677 android_native_rect_t* droid_rects = NULL;
678 int ret;
679
680 if (n_rects == 0)
681 return EGL_TRUE;
682
683 droid_rects = malloc(n_rects * sizeof(android_native_rect_t));
684 if (droid_rects == NULL)
685 return _eglError(EGL_BAD_ALLOC, "eglSetDamageRegionKHR");
686
687 for (EGLint num_drects = 0; num_drects < n_rects; num_drects++) {
688 EGLint i = num_drects * 4;
689 droid_rects[num_drects].left = rects[i];
690 droid_rects[num_drects].bottom = rects[i + 1];
691 droid_rects[num_drects].right = rects[i] + rects[i + 2];
692 droid_rects[num_drects].top = rects[i + 1] + rects[i + 3];
693 }
694
695 /*
696 * XXX/TODO: Need to check for other return values
697 */
698
699 ret = native_window_set_surface_damage(dri2_surf->window, droid_rects, n_rects);
700 free(droid_rects);
701
702 return ret == 0 ? EGL_TRUE : EGL_FALSE;
703 }
704 #endif
705
706 static _EGLImage *
707 droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
708 struct ANativeWindowBuffer *buf, int fd)
709 {
710 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
711 struct android_ycbcr ycbcr;
712 size_t offsets[3];
713 size_t pitches[3];
714 int is_ycrcb;
715 int fourcc;
716 int ret;
717
718 if (!dri2_dpy->gralloc->lock_ycbcr) {
719 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
720 return NULL;
721 }
722
723 memset(&ycbcr, 0, sizeof(ycbcr));
724 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
725 0, 0, 0, 0, 0, &ycbcr);
726 if (ret) {
727 /* HACK: See droid_create_image_from_prime_fd() and
728 * https://issuetracker.google.com/32077885.*/
729 if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
730 return NULL;
731
732 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
733 return NULL;
734 }
735 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
736
737 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
738 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
739 * so they can be interpreted as offsets. */
740 offsets[0] = (size_t)ycbcr.y;
741 /* We assume here that all the planes are located in one DMA-buf. */
742 is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
743 if (is_ycrcb) {
744 offsets[1] = (size_t)ycbcr.cr;
745 offsets[2] = (size_t)ycbcr.cb;
746 } else {
747 offsets[1] = (size_t)ycbcr.cb;
748 offsets[2] = (size_t)ycbcr.cr;
749 }
750
751 /* .ystride is the line length (in bytes) of the Y plane,
752 * .cstride is the line length (in bytes) of any of the remaining
753 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
754 * planar formats. */
755 pitches[0] = ycbcr.ystride;
756 pitches[1] = pitches[2] = ycbcr.cstride;
757
758 /* .chroma_step is the byte distance between the same chroma channel
759 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
760 fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
761 if (fourcc == -1) {
762 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
763 buf->format, is_ycrcb, ycbcr.chroma_step);
764 return NULL;
765 }
766
767 if (ycbcr.chroma_step == 2) {
768 /* Semi-planar Y + CbCr or Y + CrCb format. */
769 const EGLint attr_list_2plane[] = {
770 EGL_WIDTH, buf->width,
771 EGL_HEIGHT, buf->height,
772 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
773 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
774 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
775 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
776 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
777 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
778 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
779 EGL_NONE, 0
780 };
781
782 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
783 } else {
784 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
785 const EGLint attr_list_3plane[] = {
786 EGL_WIDTH, buf->width,
787 EGL_HEIGHT, buf->height,
788 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
789 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
790 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
791 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
792 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
793 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
794 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
795 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
796 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
797 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
798 EGL_NONE, 0
799 };
800
801 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
802 }
803 }
804
805 static _EGLImage *
806 droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
807 struct ANativeWindowBuffer *buf, int fd)
808 {
809 unsigned int pitch;
810
811 if (is_yuv(buf->format)) {
812 _EGLImage *image;
813
814 image = droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd);
815 /*
816 * HACK: https://issuetracker.google.com/32077885
817 * There is no API available to properly query the IMPLEMENTATION_DEFINED
818 * format. As a workaround we rely here on gralloc allocating either
819 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
820 * by lock_ycbcr failing.
821 */
822 if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
823 return image;
824 }
825
826 const int fourcc = get_fourcc(buf->format);
827 if (fourcc == -1) {
828 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
829 return NULL;
830 }
831
832 pitch = buf->stride * get_format_bpp(buf->format);
833 if (pitch == 0) {
834 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
835 return NULL;
836 }
837
838 const EGLint attr_list[] = {
839 EGL_WIDTH, buf->width,
840 EGL_HEIGHT, buf->height,
841 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
842 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
843 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
844 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
845 EGL_NONE, 0
846 };
847
848 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
849 }
850
851 #ifdef HAVE_DRM_GRALLOC
852 static _EGLImage *
853 droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
854 struct ANativeWindowBuffer *buf)
855 {
856 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
857 struct dri2_egl_image *dri2_img;
858 int name;
859 int format;
860
861 name = get_native_buffer_name(buf);
862 if (!name) {
863 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
864 return NULL;
865 }
866
867 format = get_format(buf->format);
868 if (format == -1)
869 return NULL;
870
871 dri2_img = calloc(1, sizeof(*dri2_img));
872 if (!dri2_img) {
873 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
874 return NULL;
875 }
876
877 _eglInitImage(&dri2_img->base, disp);
878
879 dri2_img->dri_image =
880 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
881 buf->width,
882 buf->height,
883 format,
884 name,
885 buf->stride,
886 dri2_img);
887 if (!dri2_img->dri_image) {
888 free(dri2_img);
889 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
890 return NULL;
891 }
892
893 return &dri2_img->base;
894 }
895 #endif /* HAVE_DRM_GRALLOC */
896
897 static EGLBoolean
898 droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
899 EGLint attribute, EGLint *value)
900 {
901 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
902 switch (attribute) {
903 case EGL_WIDTH:
904 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
905 dri2_surf->window->query(dri2_surf->window,
906 NATIVE_WINDOW_DEFAULT_WIDTH, value);
907 return EGL_TRUE;
908 }
909 break;
910 case EGL_HEIGHT:
911 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
912 dri2_surf->window->query(dri2_surf->window,
913 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
914 return EGL_TRUE;
915 }
916 break;
917 default:
918 break;
919 }
920 return _eglQuerySurface(drv, dpy, surf, attribute, value);
921 }
922
923 static _EGLImage *
924 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
925 _EGLContext *ctx,
926 struct ANativeWindowBuffer *buf)
927 {
928 int fd;
929
930 if (ctx != NULL) {
931 /* From the EGL_ANDROID_image_native_buffer spec:
932 *
933 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
934 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
935 */
936 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
937 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
938 "EGL_NO_CONTEXT");
939 return NULL;
940 }
941
942 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
943 buf->common.version != sizeof(*buf)) {
944 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
945 return NULL;
946 }
947
948 fd = get_native_buffer_fd(buf);
949 if (fd >= 0)
950 return droid_create_image_from_prime_fd(disp, ctx, buf, fd);
951
952 #ifdef HAVE_DRM_GRALLOC
953 return droid_create_image_from_name(disp, ctx, buf);
954 #else
955 return NULL;
956 #endif
957 }
958
959 static _EGLImage *
960 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
961 _EGLContext *ctx, EGLenum target,
962 EGLClientBuffer buffer, const EGLint *attr_list)
963 {
964 switch (target) {
965 case EGL_NATIVE_BUFFER_ANDROID:
966 return dri2_create_image_android_native_buffer(disp, ctx,
967 (struct ANativeWindowBuffer *) buffer);
968 default:
969 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
970 }
971 }
972
973 static void
974 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
975 {
976 }
977
978 #ifdef HAVE_DRM_GRALLOC
979 static int
980 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
981 unsigned int *attachments, int count)
982 {
983 int num_buffers = 0;
984
985 /* fill dri2_surf->buffers */
986 for (int i = 0; i < count * 2; i += 2) {
987 __DRIbuffer *buf, *local;
988
989 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
990 buf = &dri2_surf->buffers[num_buffers];
991
992 switch (attachments[i]) {
993 case __DRI_BUFFER_BACK_LEFT:
994 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
995 buf->attachment = attachments[i];
996 buf->name = get_native_buffer_name(dri2_surf->buffer);
997 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
998 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
999 buf->flags = 0;
1000
1001 if (buf->name)
1002 num_buffers++;
1003
1004 break;
1005 }
1006 /* fall through for pbuffers */
1007 case __DRI_BUFFER_DEPTH:
1008 case __DRI_BUFFER_STENCIL:
1009 case __DRI_BUFFER_ACCUM:
1010 case __DRI_BUFFER_DEPTH_STENCIL:
1011 case __DRI_BUFFER_HIZ:
1012 local = dri2_egl_surface_alloc_local_buffer(dri2_surf,
1013 attachments[i], attachments[i + 1]);
1014
1015 if (local) {
1016 *buf = *local;
1017 num_buffers++;
1018 }
1019 break;
1020 case __DRI_BUFFER_FRONT_LEFT:
1021 case __DRI_BUFFER_FRONT_RIGHT:
1022 case __DRI_BUFFER_FAKE_FRONT_LEFT:
1023 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
1024 case __DRI_BUFFER_BACK_RIGHT:
1025 default:
1026 /* no front or right buffers */
1027 break;
1028 }
1029 }
1030
1031 return num_buffers;
1032 }
1033
1034 static __DRIbuffer *
1035 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
1036 int *width, int *height,
1037 unsigned int *attachments, int count,
1038 int *out_count, void *loaderPrivate)
1039 {
1040 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1041
1042 if (update_buffers(dri2_surf) < 0)
1043 return NULL;
1044
1045 *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
1046
1047 if (width)
1048 *width = dri2_surf->base.Width;
1049 if (height)
1050 *height = dri2_surf->base.Height;
1051
1052 return dri2_surf->buffers;
1053 }
1054 #endif /* HAVE_DRM_GRALLOC */
1055
1056 static unsigned
1057 droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
1058 {
1059 /* Note: loaderPrivate is _EGLDisplay* */
1060 switch (cap) {
1061 case DRI_LOADER_CAP_RGBA_ORDERING:
1062 return 1;
1063 default:
1064 return 0;
1065 }
1066 }
1067
1068 static EGLBoolean
1069 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
1070 {
1071 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1072 static const struct {
1073 int format;
1074 unsigned int rgba_masks[4];
1075 } visuals[] = {
1076 { HAL_PIXEL_FORMAT_RGBA_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1077 { HAL_PIXEL_FORMAT_RGBX_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1078 { HAL_PIXEL_FORMAT_RGB_565, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1079 { HAL_PIXEL_FORMAT_BGRA_8888, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1080 };
1081
1082 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1083 int config_count = 0;
1084
1085 /* The nesting of loops is significant here. Also significant is the order
1086 * of the HAL pixel formats. Many Android apps (such as Google's official
1087 * NDK GLES2 example app), and even portions the core framework code (such
1088 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1089 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1090 * window's native format, and instead choose the first EGLConfig whose
1091 * channel sizes match those of the native window format while ignoring the
1092 * channel *ordering*.
1093 *
1094 * We can detect such buggy clients in logcat when they call
1095 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1096 * format and the window's format.
1097 *
1098 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1099 * pixel format i precede those for HAL pixel format i+1. In my
1100 * (chadversary) testing on Android Nougat, this was good enough to pacify
1101 * the buggy clients.
1102 */
1103 for (int i = 0; i < ARRAY_SIZE(visuals); i++) {
1104 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
1105 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1106
1107 const EGLint config_attrs[] = {
1108 EGL_NATIVE_VISUAL_ID, visuals[i].format,
1109 EGL_NATIVE_VISUAL_TYPE, visuals[i].format,
1110 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1111 EGL_RECORDABLE_ANDROID, EGL_TRUE,
1112 EGL_NONE
1113 };
1114
1115 struct dri2_egl_config *dri2_conf =
1116 dri2_add_config(dpy, dri2_dpy->driver_configs[j],
1117 config_count + 1, surface_type, config_attrs,
1118 visuals[i].rgba_masks);
1119 if (dri2_conf) {
1120 if (dri2_conf->base.ConfigID == config_count + 1)
1121 config_count++;
1122 format_count[i]++;
1123 }
1124 }
1125 }
1126
1127 for (int i = 0; i < ARRAY_SIZE(format_count); i++) {
1128 if (!format_count[i]) {
1129 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
1130 visuals[i].format);
1131 }
1132 }
1133
1134 return (config_count != 0);
1135 }
1136
1137 static const struct dri2_egl_display_vtbl droid_display_vtbl = {
1138 .authenticate = NULL,
1139 .create_window_surface = droid_create_window_surface,
1140 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
1141 .create_pbuffer_surface = droid_create_pbuffer_surface,
1142 .destroy_surface = droid_destroy_surface,
1143 .create_image = droid_create_image_khr,
1144 .swap_buffers = droid_swap_buffers,
1145 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, /* Android implements the function */
1146 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1147 .swap_interval = droid_swap_interval,
1148 #if ANDROID_API_LEVEL >= 23
1149 .set_damage_region = droid_set_damage_region,
1150 #else
1151 .set_damage_region = dri2_fallback_set_damage_region,
1152 #endif
1153 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1154 .copy_buffers = dri2_fallback_copy_buffers,
1155 .query_buffer_age = droid_query_buffer_age,
1156 .query_surface = droid_query_surface,
1157 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1158 .get_sync_values = dri2_fallback_get_sync_values,
1159 .get_dri_drawable = dri2_surface_get_dri_drawable,
1160 };
1161
1162 #ifdef HAVE_DRM_GRALLOC
1163 static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1164 .base = { __DRI_DRI2_LOADER, 4 },
1165
1166 .getBuffers = NULL,
1167 .flushFrontBuffer = droid_flush_front_buffer,
1168 .getBuffersWithFormat = droid_get_buffers_with_format,
1169 .getCapability = droid_get_capability,
1170 };
1171 #endif /* HAVE_DRM_GRALLOC */
1172
1173 static const __DRIimageLoaderExtension droid_image_loader_extension = {
1174 .base = { __DRI_IMAGE_LOADER, 2 },
1175
1176 .getBuffers = droid_image_get_buffers,
1177 .flushFrontBuffer = droid_flush_front_buffer,
1178 .getCapability = droid_get_capability,
1179 };
1180
1181 #ifdef HAVE_DRM_GRALLOC
1182 static const __DRIextension *droid_dri2_loader_extensions[] = {
1183 &droid_dri2_loader_extension.base,
1184 &image_lookup_extension.base,
1185 &use_invalidate.base,
1186 NULL,
1187 };
1188 #endif /* HAVE_DRM_GRALLOC */
1189
1190 static const __DRIextension *droid_image_loader_extensions[] = {
1191 &droid_image_loader_extension.base,
1192 &image_lookup_extension.base,
1193 &use_invalidate.base,
1194 NULL,
1195 };
1196
1197 EGLBoolean
1198 droid_load_driver(_EGLDisplay *disp)
1199 {
1200 struct dri2_egl_display *dri2_dpy = disp->DriverData;
1201 const char *err;
1202
1203 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1204 if (dri2_dpy->driver_name == NULL)
1205 return false;
1206
1207 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1208
1209 if (!dri2_dpy->is_render_node) {
1210 #ifdef HAVE_DRM_GRALLOC
1211 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1212 * for backwards compatibility with drm_gralloc. (Do not use on new
1213 * systems.) */
1214 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
1215 if (!dri2_load_driver(disp)) {
1216 err = "DRI2: failed to load driver";
1217 goto error;
1218 }
1219 #else
1220 err = "DRI2: handle is not for a render node";
1221 goto error;
1222 #endif
1223 } else {
1224 dri2_dpy->loader_extensions = droid_image_loader_extensions;
1225 if (!dri2_load_driver_dri3(disp)) {
1226 err = "DRI3: failed to load driver";
1227 goto error;
1228 }
1229 }
1230
1231 return true;
1232
1233 error:
1234 free(dri2_dpy->driver_name);
1235 dri2_dpy->driver_name = NULL;
1236 return false;
1237 }
1238
1239 static bool
1240 droid_probe_driver(int fd)
1241 {
1242 char *driver_name;
1243
1244 driver_name = loader_get_driver_for_fd(fd);
1245 if (driver_name == NULL)
1246 return false;
1247
1248 free(driver_name);
1249 return true;
1250 }
1251
1252 typedef enum {
1253 probe_fail = -1,
1254 probe_success = 0,
1255 probe_filtered_out = 1,
1256 } probe_ret_t;
1257
1258 static probe_ret_t
1259 droid_probe_device(_EGLDisplay *disp, int fd, const char *vendor)
1260 {
1261 int ret;
1262
1263 drmVersionPtr ver = drmGetVersion(fd);
1264 if (!ver)
1265 return probe_fail;
1266
1267 if (!ver->name) {
1268 ret = probe_fail;
1269 goto cleanup;
1270 }
1271
1272 if (vendor && strncmp(vendor, ver->name, PROPERTY_VALUE_MAX) != 0) {
1273 ret = probe_filtered_out;
1274 goto cleanup;
1275 }
1276
1277 if (!droid_probe_driver(fd)) {
1278 ret = probe_fail;
1279 goto cleanup;
1280 }
1281
1282 ret = probe_success;
1283
1284 cleanup:
1285 drmFreeVersion(ver);
1286 return ret;
1287 }
1288
1289 static int
1290 droid_open_device(_EGLDisplay *disp)
1291 {
1292 const int MAX_DRM_DEVICES = 32;
1293 int prop_set, num_devices;
1294 int fd = -1, fallback_fd = -1;
1295
1296 char *vendor_name = NULL;
1297 char vendor_buf[PROPERTY_VALUE_MAX];
1298
1299 if (property_get("drm.gpu.vendor_name", vendor_buf, NULL) > 0)
1300 vendor_name = vendor_buf;
1301
1302 const char *drm_dir_name = "/dev/dri";
1303 DIR *sysdir = opendir(drm_dir_name);
1304
1305 if (!sysdir)
1306 return -errno;
1307
1308 struct dirent *dent;
1309 while ((dent = readdir(sysdir))) {
1310 char dev_path[128];
1311 const char render_dev_prefix[] = "renderD";
1312 size_t prefix_len = sizeof(render_dev_prefix) - 1;
1313
1314 if (strncmp(render_dev_prefix, dent->d_name, prefix_len) != 0)
1315 continue;
1316
1317 snprintf(dev_path, sizeof(dev_path), "%s/%s", drm_dir_name, dent->d_name);
1318 fd = loader_open_device(dev_path);
1319 if (fd < 0) {
1320 _eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s",
1321 __func__, dev_path);
1322 continue;
1323 }
1324
1325 int ret = droid_probe_device(disp, fd, vendor_name);
1326 switch (ret) {
1327 case probe_success:
1328 goto success;
1329 case probe_filtered_out:
1330 /* Set as fallback */
1331 if (fallback_fd == -1)
1332 fallback_fd = fd;
1333 break;
1334 case probe_fail:
1335 break;
1336 }
1337
1338 if (fallback_fd != fd)
1339 close(fd);
1340 fd = -1;
1341 }
1342
1343 success:
1344 closedir(sysdir);
1345
1346 if (fallback_fd < 0 && fd < 0) {
1347 _eglLog(_EGL_WARNING, "Failed to open any DRM device");
1348 return -1;
1349 }
1350
1351 if (fd < 0) {
1352 _eglLog(_EGL_WARNING, "Failed to open desired DRM device, using fallback");
1353 return fallback_fd;
1354 }
1355
1356 close(fallback_fd);
1357 return fd;
1358 }
1359
1360 EGLBoolean
1361 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp)
1362 {
1363 struct dri2_egl_display *dri2_dpy;
1364 const char *err;
1365 int ret;
1366
1367 /* Not supported yet */
1368 if (disp->Options.ForceSoftware)
1369 return EGL_FALSE;
1370
1371 loader_set_logger(_eglLog);
1372
1373 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1374 if (!dri2_dpy)
1375 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1376
1377 dri2_dpy->fd = -1;
1378 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1379 (const hw_module_t **)&dri2_dpy->gralloc);
1380 if (ret) {
1381 err = "DRI2: failed to get gralloc module";
1382 goto cleanup;
1383 }
1384
1385 disp->DriverData = (void *) dri2_dpy;
1386
1387 dri2_dpy->fd = droid_open_device(disp);
1388 if (dri2_dpy->fd < 0) {
1389 err = "DRI2: failed to open device";
1390 goto cleanup;
1391 }
1392
1393 if (!droid_load_driver(disp)) {
1394 err = "DRI2: failed to load driver";
1395 goto cleanup;
1396 }
1397
1398 if (!dri2_create_screen(disp)) {
1399 err = "DRI2: failed to create screen";
1400 goto cleanup;
1401 }
1402
1403 if (!dri2_setup_extensions(disp)) {
1404 err = "DRI2: failed to setup extensions";
1405 goto cleanup;
1406 }
1407
1408 dri2_setup_screen(disp);
1409
1410 /* We set the maximum swap interval as 1 for Android platform, since it is
1411 * the maximum value supported by Android according to the value of
1412 * ANativeWindow::maxSwapInterval.
1413 */
1414 dri2_setup_swap_interval(disp, 1);
1415
1416 if (!droid_add_configs_for_visuals(drv, disp)) {
1417 err = "DRI2: failed to add configs";
1418 goto cleanup;
1419 }
1420
1421 disp->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
1422 disp->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
1423 disp->Extensions.ANDROID_recordable = EGL_TRUE;
1424 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1425 #if ANDROID_API_LEVEL >= 23
1426 disp->Extensions.KHR_partial_update = EGL_TRUE;
1427 #endif
1428 disp->Extensions.KHR_image = EGL_TRUE;
1429
1430 /* Fill vtbl last to prevent accidentally calling virtual function during
1431 * initialization.
1432 */
1433 dri2_dpy->vtbl = &droid_display_vtbl;
1434
1435 return EGL_TRUE;
1436
1437 cleanup:
1438 dri2_display_destroy(disp);
1439 return _eglError(EGL_NOT_INITIALIZED, err);
1440 }