egl: rename LIBGL_ALWAYS_SOFTWARE variable from UseFallback to ForceSoftware
[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 <errno.h>
31 #include <dlfcn.h>
32 #include <fcntl.h>
33 #include <xf86drm.h>
34 #include <stdbool.h>
35 #include <sync/sync.h>
36
37 #include "loader.h"
38 #include "egl_dri2.h"
39 #include "egl_dri2_fallbacks.h"
40 #include "gralloc_drm.h"
41
42 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
43
44 struct droid_yuv_format {
45 /* Lookup keys */
46 int native; /* HAL_PIXEL_FORMAT_ */
47 int is_ycrcb; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
48 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
49
50 /* Result */
51 int fourcc; /* __DRI_IMAGE_FOURCC_ */
52 };
53
54 /* The following table is used to look up a DRI image FourCC based
55 * on native format and information contained in android_ycbcr struct. */
56 static const struct droid_yuv_format droid_yuv_formats[] = {
57 /* Native format, YCrCb, Chroma step, DRI image FourCC */
58 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
59 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
60 { HAL_PIXEL_FORMAT_YCbCr_420_888, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
61 { HAL_PIXEL_FORMAT_YV12, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
62 /* HACK: See droid_create_image_from_prime_fd() and
63 * https://issuetracker.google.com/32077885. */
64 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
65 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
66 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
67 };
68
69 static int
70 get_fourcc_yuv(int native, int is_ycrcb, int chroma_step)
71 {
72 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
73 if (droid_yuv_formats[i].native == native &&
74 droid_yuv_formats[i].is_ycrcb == is_ycrcb &&
75 droid_yuv_formats[i].chroma_step == chroma_step)
76 return droid_yuv_formats[i].fourcc;
77
78 return -1;
79 }
80
81 static bool
82 is_yuv(int native)
83 {
84 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
85 if (droid_yuv_formats[i].native == native)
86 return true;
87
88 return false;
89 }
90
91 static int
92 get_format_bpp(int native)
93 {
94 int bpp;
95
96 switch (native) {
97 case HAL_PIXEL_FORMAT_RGBA_8888:
98 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
99 /*
100 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
101 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
102 */
103 case HAL_PIXEL_FORMAT_RGBX_8888:
104 case HAL_PIXEL_FORMAT_BGRA_8888:
105 bpp = 4;
106 break;
107 case HAL_PIXEL_FORMAT_RGB_565:
108 bpp = 2;
109 break;
110 default:
111 bpp = 0;
112 break;
113 }
114
115 return bpp;
116 }
117
118 /* createImageFromFds requires fourcc format */
119 static int get_fourcc(int native)
120 {
121 switch (native) {
122 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FOURCC_RGB565;
123 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FOURCC_ARGB8888;
124 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FOURCC_ABGR8888;
125 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
126 /*
127 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
128 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
129 */
130 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FOURCC_XBGR8888;
131 default:
132 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
133 }
134 return -1;
135 }
136
137 static int get_format(int format)
138 {
139 switch (format) {
140 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
141 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
142 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
143 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
144 /*
145 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
146 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
147 */
148 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
149 default:
150 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
151 }
152 return -1;
153 }
154
155 static int
156 get_native_buffer_fd(struct ANativeWindowBuffer *buf)
157 {
158 native_handle_t *handle = (native_handle_t *)buf->handle;
159 /*
160 * Various gralloc implementations exist, but the dma-buf fd tends
161 * to be first. Access it directly to avoid a dependency on specific
162 * gralloc versions.
163 */
164 return (handle && handle->numFds) ? handle->data[0] : -1;
165 }
166
167 static int
168 get_native_buffer_name(struct ANativeWindowBuffer *buf)
169 {
170 return gralloc_drm_get_gem_handle(buf->handle);
171 }
172
173 static EGLBoolean
174 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
175 {
176 int fence_fd;
177
178 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
179 &fence_fd))
180 return EGL_FALSE;
181
182 /* If access to the buffer is controlled by a sync fence, then block on the
183 * fence.
184 *
185 * It may be more performant to postpone blocking until there is an
186 * immediate need to write to the buffer. But doing so would require adding
187 * hooks to the DRI2 loader.
188 *
189 * From the ANativeWindow::dequeueBuffer documentation:
190 *
191 * The libsync fence file descriptor returned in the int pointed to by
192 * the fenceFd argument will refer to the fence that must signal
193 * before the dequeued buffer may be written to. A value of -1
194 * indicates that the caller may access the buffer immediately without
195 * waiting on a fence. If a valid file descriptor is returned (i.e.
196 * any value except -1) then the caller is responsible for closing the
197 * file descriptor.
198 */
199 if (fence_fd >= 0) {
200 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
201 *
202 * Waits indefinitely if timeout < 0.
203 */
204 int timeout = -1;
205 sync_wait(fence_fd, timeout);
206 close(fence_fd);
207 }
208
209 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
210
211 /* Record all the buffers created by ANativeWindow and update back buffer
212 * for updating buffer's age in swap_buffers.
213 */
214 EGLBoolean updated = EGL_FALSE;
215 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
216 if (!dri2_surf->color_buffers[i].buffer) {
217 dri2_surf->color_buffers[i].buffer = dri2_surf->buffer;
218 }
219 if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) {
220 dri2_surf->back = &dri2_surf->color_buffers[i];
221 updated = EGL_TRUE;
222 break;
223 }
224 }
225
226 if (!updated) {
227 /* In case of all the buffers were recreated by ANativeWindow, reset
228 * the color_buffers
229 */
230 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
231 dri2_surf->color_buffers[i].buffer = NULL;
232 dri2_surf->color_buffers[i].age = 0;
233 }
234 dri2_surf->color_buffers[0].buffer = dri2_surf->buffer;
235 dri2_surf->back = &dri2_surf->color_buffers[0];
236 }
237
238 return EGL_TRUE;
239 }
240
241 static EGLBoolean
242 droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
243 {
244 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
245
246 /* To avoid blocking other EGL calls, release the display mutex before
247 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
248 * return.
249 */
250 mtx_unlock(&disp->Mutex);
251
252 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
253 * consumer may choose to wait for the fence to signal before accessing
254 * it. If fence fd value is -1, buffer can be accessed by consumer
255 * immediately. Consumer or application shouldn't rely on timestamp
256 * associated with fence if the fence fd is -1.
257 *
258 * Ownership of fd is transferred to consumer after queueBuffer and the
259 * consumer is responsible for closing it. Caller must not use the fd
260 * after passing it to queueBuffer.
261 */
262 int fence_fd = dri2_surf->out_fence_fd;
263 dri2_surf->out_fence_fd = -1;
264 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
265 fence_fd);
266
267 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
268 dri2_surf->buffer = NULL;
269 dri2_surf->back = NULL;
270
271 mtx_lock(&disp->Mutex);
272
273 if (dri2_surf->dri_image_back) {
274 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
275 dri2_surf->dri_image_back = NULL;
276 }
277
278 return EGL_TRUE;
279 }
280
281 static void
282 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
283 {
284 int ret;
285 int fence_fd = dri2_surf->out_fence_fd;
286
287 dri2_surf->out_fence_fd = -1;
288 ret = dri2_surf->window->cancelBuffer(dri2_surf->window,
289 dri2_surf->buffer, fence_fd);
290 if (ret < 0) {
291 _eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed");
292 dri2_surf->base.Lost = EGL_TRUE;
293 }
294 }
295
296 static _EGLSurface *
297 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
298 _EGLConfig *conf, void *native_window,
299 const EGLint *attrib_list)
300 {
301 __DRIcreateNewDrawableFunc createNewDrawable;
302 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
303 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
304 struct dri2_egl_surface *dri2_surf;
305 struct ANativeWindow *window = native_window;
306 const __DRIconfig *config;
307
308 dri2_surf = calloc(1, sizeof *dri2_surf);
309 if (!dri2_surf) {
310 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
311 return NULL;
312 }
313
314 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true))
315 goto cleanup_surface;
316
317 if (type == EGL_WINDOW_BIT) {
318 int format;
319
320 if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
321 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
322 goto cleanup_surface;
323 }
324 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
325 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
326 goto cleanup_surface;
327 }
328
329 if (format != dri2_conf->base.NativeVisualID) {
330 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
331 format, dri2_conf->base.NativeVisualID);
332 }
333
334 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
335 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
336 }
337
338 config = dri2_get_dri_config(dri2_conf, type,
339 dri2_surf->base.GLColorspace);
340 if (!config)
341 goto cleanup_surface;
342
343 if (dri2_dpy->image_driver)
344 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
345 else
346 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
347
348 dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
349 dri2_surf);
350 if (dri2_surf->dri_drawable == NULL) {
351 _eglError(EGL_BAD_ALLOC, "createNewDrawable");
352 goto cleanup_surface;
353 }
354
355 if (window) {
356 window->common.incRef(&window->common);
357 dri2_surf->window = window;
358 }
359
360 return &dri2_surf->base;
361
362 cleanup_surface:
363 free(dri2_surf);
364
365 return NULL;
366 }
367
368 static _EGLSurface *
369 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
370 _EGLConfig *conf, void *native_window,
371 const EGLint *attrib_list)
372 {
373 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
374 native_window, attrib_list);
375 }
376
377 static _EGLSurface *
378 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
379 _EGLConfig *conf, const EGLint *attrib_list)
380 {
381 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
382 NULL, attrib_list);
383 }
384
385 static EGLBoolean
386 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
387 {
388 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
389 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
390
391 dri2_egl_surface_free_local_buffers(dri2_surf);
392
393 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
394 if (dri2_surf->buffer)
395 droid_window_cancel_buffer(dri2_surf);
396
397 dri2_surf->window->common.decRef(&dri2_surf->window->common);
398 }
399
400 if (dri2_surf->dri_image_back) {
401 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
402 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
403 dri2_surf->dri_image_back = NULL;
404 }
405
406 if (dri2_surf->dri_image_front) {
407 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
408 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
409 dri2_surf->dri_image_front = NULL;
410 }
411
412 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
413
414 dri2_fini_surface(surf);
415 free(dri2_surf);
416
417 return EGL_TRUE;
418 }
419
420 static int
421 update_buffers(struct dri2_egl_surface *dri2_surf)
422 {
423 if (dri2_surf->base.Lost)
424 return -1;
425
426 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
427 return 0;
428
429 /* try to dequeue the next back buffer */
430 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
431 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
432 dri2_surf->base.Lost = EGL_TRUE;
433 return -1;
434 }
435
436 /* free outdated buffers and update the surface size */
437 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
438 dri2_surf->base.Height != dri2_surf->buffer->height) {
439 dri2_egl_surface_free_local_buffers(dri2_surf);
440 dri2_surf->base.Width = dri2_surf->buffer->width;
441 dri2_surf->base.Height = dri2_surf->buffer->height;
442 }
443
444 return 0;
445 }
446
447 static int
448 get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
449 {
450 struct dri2_egl_display *dri2_dpy =
451 dri2_egl_display(dri2_surf->base.Resource.Display);
452
453 if (dri2_surf->dri_image_front)
454 return 0;
455
456 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
457 /* According current EGL spec, front buffer rendering
458 * for window surface is not supported now.
459 * and mesa doesn't have the implementation of this case.
460 * Add warning message, but not treat it as error.
461 */
462 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
463 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
464 dri2_surf->dri_image_front =
465 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
466 dri2_surf->base.Width,
467 dri2_surf->base.Height,
468 format,
469 0,
470 dri2_surf);
471 if (!dri2_surf->dri_image_front) {
472 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
473 return -1;
474 }
475 }
476
477 return 0;
478 }
479
480 static int
481 get_back_bo(struct dri2_egl_surface *dri2_surf)
482 {
483 struct dri2_egl_display *dri2_dpy =
484 dri2_egl_display(dri2_surf->base.Resource.Display);
485 int fourcc, pitch;
486 int offset = 0, fd;
487
488 if (dri2_surf->dri_image_back)
489 return 0;
490
491 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
492 if (!dri2_surf->buffer) {
493 _eglLog(_EGL_WARNING, "Could not get native buffer");
494 return -1;
495 }
496
497 fd = get_native_buffer_fd(dri2_surf->buffer);
498 if (fd < 0) {
499 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
500 return -1;
501 }
502
503 fourcc = get_fourcc(dri2_surf->buffer->format);
504
505 pitch = dri2_surf->buffer->stride *
506 get_format_bpp(dri2_surf->buffer->format);
507
508 if (fourcc == -1 || pitch == 0) {
509 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
510 fourcc, pitch);
511 return -1;
512 }
513
514 dri2_surf->dri_image_back =
515 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
516 dri2_surf->base.Width,
517 dri2_surf->base.Height,
518 fourcc,
519 &fd,
520 1,
521 &pitch,
522 &offset,
523 dri2_surf);
524 if (!dri2_surf->dri_image_back) {
525 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
526 return -1;
527 }
528 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
529 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
530 * the spec states that they have a back buffer but no front buffer, in
531 * contrast to pixmaps, which have a front buffer but no back buffer.
532 *
533 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
534 * from the spec, following the precedent of Mesa's EGL X11 platform. The
535 * X11 platform correctly assigns pbuffers to single-buffered configs, but
536 * assigns the pbuffer a front buffer instead of a back buffer.
537 *
538 * Pbuffers in the X11 platform mostly work today, so let's just copy its
539 * behavior instead of trying to fix (and hence potentially breaking) the
540 * world.
541 */
542 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
543 }
544
545 return 0;
546 }
547
548 /* Some drivers will pass multiple bits in buffer_mask.
549 * For such case, will go through all the bits, and
550 * will not return error when unsupported buffer is requested, only
551 * return error when the allocation for supported buffer failed.
552 */
553 static int
554 droid_image_get_buffers(__DRIdrawable *driDrawable,
555 unsigned int format,
556 uint32_t *stamp,
557 void *loaderPrivate,
558 uint32_t buffer_mask,
559 struct __DRIimageList *images)
560 {
561 struct dri2_egl_surface *dri2_surf = loaderPrivate;
562
563 images->image_mask = 0;
564 images->front = NULL;
565 images->back = NULL;
566
567 if (update_buffers(dri2_surf) < 0)
568 return 0;
569
570 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
571 if (get_front_bo(dri2_surf, format) < 0)
572 return 0;
573
574 if (dri2_surf->dri_image_front) {
575 images->front = dri2_surf->dri_image_front;
576 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
577 }
578 }
579
580 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
581 if (get_back_bo(dri2_surf) < 0)
582 return 0;
583
584 if (dri2_surf->dri_image_back) {
585 images->back = dri2_surf->dri_image_back;
586 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
587 }
588 }
589
590 return 1;
591 }
592
593 static EGLint
594 droid_query_buffer_age(_EGLDriver *drv,
595 _EGLDisplay *disp, _EGLSurface *surface)
596 {
597 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
598
599 if (update_buffers(dri2_surf) < 0) {
600 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
601 return -1;
602 }
603
604 return dri2_surf->back ? dri2_surf->back->age : 0;
605 }
606
607 static EGLBoolean
608 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
609 {
610 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
611 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
612
613 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
614 return EGL_TRUE;
615
616 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
617 if (dri2_surf->color_buffers[i].age > 0)
618 dri2_surf->color_buffers[i].age++;
619 }
620
621 /* "XXX: we don't use get_back_bo() since it causes regressions in
622 * several dEQP tests.
623 */
624 if (dri2_surf->back)
625 dri2_surf->back->age = 1;
626
627 dri2_flush_drawable_for_swapbuffers(disp, draw);
628
629 /* dri2_surf->buffer can be null even when no error has occured. For
630 * example, if the user has called no GL rendering commands since the
631 * previous eglSwapBuffers, then the driver may have not triggered
632 * a callback to ANativeWindow::dequeueBuffer, in which case
633 * dri2_surf->buffer remains null.
634 */
635 if (dri2_surf->buffer)
636 droid_window_enqueue_buffer(disp, dri2_surf);
637
638 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
639
640 return EGL_TRUE;
641 }
642
643 #if ANDROID_API_LEVEL >= 23
644 static EGLBoolean
645 droid_set_damage_region(_EGLDriver *drv,
646 _EGLDisplay *disp,
647 _EGLSurface *draw, const EGLint* rects, EGLint n_rects)
648 {
649 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
650 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
651 android_native_rect_t* droid_rects = NULL;
652 int ret;
653
654 if (n_rects == 0)
655 return EGL_TRUE;
656
657 droid_rects = malloc(n_rects * sizeof(android_native_rect_t));
658 if (droid_rects == NULL)
659 return _eglError(EGL_BAD_ALLOC, "eglSetDamageRegionKHR");
660
661 for (EGLint num_drects = 0; num_drects < n_rects; num_drects++) {
662 EGLint i = num_drects * 4;
663 droid_rects[num_drects].left = rects[i];
664 droid_rects[num_drects].bottom = rects[i + 1];
665 droid_rects[num_drects].right = rects[i] + rects[i + 2];
666 droid_rects[num_drects].top = rects[i + 1] + rects[i + 3];
667 }
668
669 /*
670 * XXX/TODO: Need to check for other return values
671 */
672
673 ret = native_window_set_surface_damage(dri2_surf->window, droid_rects, n_rects);
674 free(droid_rects);
675
676 return ret == 0 ? EGL_TRUE : EGL_FALSE;
677 }
678 #endif
679
680 static _EGLImage *
681 droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
682 struct ANativeWindowBuffer *buf, int fd)
683 {
684 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
685 struct android_ycbcr ycbcr;
686 size_t offsets[3];
687 size_t pitches[3];
688 int is_ycrcb;
689 int fourcc;
690 int ret;
691
692 if (!dri2_dpy->gralloc->lock_ycbcr) {
693 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
694 return NULL;
695 }
696
697 memset(&ycbcr, 0, sizeof(ycbcr));
698 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
699 0, 0, 0, 0, 0, &ycbcr);
700 if (ret) {
701 /* HACK: See droid_create_image_from_prime_fd() and
702 * https://issuetracker.google.com/32077885.*/
703 if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
704 return NULL;
705
706 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
707 return NULL;
708 }
709 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
710
711 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
712 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
713 * so they can be interpreted as offsets. */
714 offsets[0] = (size_t)ycbcr.y;
715 /* We assume here that all the planes are located in one DMA-buf. */
716 is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
717 if (is_ycrcb) {
718 offsets[1] = (size_t)ycbcr.cr;
719 offsets[2] = (size_t)ycbcr.cb;
720 } else {
721 offsets[1] = (size_t)ycbcr.cb;
722 offsets[2] = (size_t)ycbcr.cr;
723 }
724
725 /* .ystride is the line length (in bytes) of the Y plane,
726 * .cstride is the line length (in bytes) of any of the remaining
727 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
728 * planar formats. */
729 pitches[0] = ycbcr.ystride;
730 pitches[1] = pitches[2] = ycbcr.cstride;
731
732 /* .chroma_step is the byte distance between the same chroma channel
733 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
734 fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
735 if (fourcc == -1) {
736 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
737 buf->format, is_ycrcb, ycbcr.chroma_step);
738 return NULL;
739 }
740
741 if (ycbcr.chroma_step == 2) {
742 /* Semi-planar Y + CbCr or Y + CrCb format. */
743 const EGLint attr_list_2plane[] = {
744 EGL_WIDTH, buf->width,
745 EGL_HEIGHT, buf->height,
746 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
747 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
748 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
749 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
750 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
751 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
752 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
753 EGL_NONE, 0
754 };
755
756 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
757 } else {
758 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
759 const EGLint attr_list_3plane[] = {
760 EGL_WIDTH, buf->width,
761 EGL_HEIGHT, buf->height,
762 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
763 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
764 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
765 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
766 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
767 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
768 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
769 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
770 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
771 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
772 EGL_NONE, 0
773 };
774
775 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
776 }
777 }
778
779 static _EGLImage *
780 droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
781 struct ANativeWindowBuffer *buf, int fd)
782 {
783 unsigned int pitch;
784
785 if (is_yuv(buf->format)) {
786 _EGLImage *image;
787
788 image = droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd);
789 /*
790 * HACK: https://issuetracker.google.com/32077885
791 * There is no API available to properly query the IMPLEMENTATION_DEFINED
792 * format. As a workaround we rely here on gralloc allocating either
793 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
794 * by lock_ycbcr failing.
795 */
796 if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
797 return image;
798 }
799
800 const int fourcc = get_fourcc(buf->format);
801 if (fourcc == -1) {
802 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
803 return NULL;
804 }
805
806 pitch = buf->stride * get_format_bpp(buf->format);
807 if (pitch == 0) {
808 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
809 return NULL;
810 }
811
812 const EGLint attr_list[] = {
813 EGL_WIDTH, buf->width,
814 EGL_HEIGHT, buf->height,
815 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
816 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
817 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
818 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
819 EGL_NONE, 0
820 };
821
822 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
823 }
824
825 static _EGLImage *
826 droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
827 struct ANativeWindowBuffer *buf)
828 {
829 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
830 struct dri2_egl_image *dri2_img;
831 int name;
832 int format;
833
834 name = get_native_buffer_name(buf);
835 if (!name) {
836 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
837 return NULL;
838 }
839
840 format = get_format(buf->format);
841 if (format == -1)
842 return NULL;
843
844 dri2_img = calloc(1, sizeof(*dri2_img));
845 if (!dri2_img) {
846 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
847 return NULL;
848 }
849
850 _eglInitImage(&dri2_img->base, disp);
851
852 dri2_img->dri_image =
853 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
854 buf->width,
855 buf->height,
856 format,
857 name,
858 buf->stride,
859 dri2_img);
860 if (!dri2_img->dri_image) {
861 free(dri2_img);
862 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
863 return NULL;
864 }
865
866 return &dri2_img->base;
867 }
868
869 static EGLBoolean
870 droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
871 EGLint attribute, EGLint *value)
872 {
873 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
874 switch (attribute) {
875 case EGL_WIDTH:
876 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
877 dri2_surf->window->query(dri2_surf->window,
878 NATIVE_WINDOW_DEFAULT_WIDTH, value);
879 return EGL_TRUE;
880 }
881 break;
882 case EGL_HEIGHT:
883 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
884 dri2_surf->window->query(dri2_surf->window,
885 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
886 return EGL_TRUE;
887 }
888 break;
889 default:
890 break;
891 }
892 return _eglQuerySurface(drv, dpy, surf, attribute, value);
893 }
894
895 static _EGLImage *
896 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
897 _EGLContext *ctx,
898 struct ANativeWindowBuffer *buf)
899 {
900 int fd;
901
902 if (ctx != NULL) {
903 /* From the EGL_ANDROID_image_native_buffer spec:
904 *
905 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
906 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
907 */
908 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
909 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
910 "EGL_NO_CONTEXT");
911 return NULL;
912 }
913
914 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
915 buf->common.version != sizeof(*buf)) {
916 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
917 return NULL;
918 }
919
920 fd = get_native_buffer_fd(buf);
921 if (fd >= 0)
922 return droid_create_image_from_prime_fd(disp, ctx, buf, fd);
923
924 return droid_create_image_from_name(disp, ctx, buf);
925 }
926
927 static _EGLImage *
928 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
929 _EGLContext *ctx, EGLenum target,
930 EGLClientBuffer buffer, const EGLint *attr_list)
931 {
932 switch (target) {
933 case EGL_NATIVE_BUFFER_ANDROID:
934 return dri2_create_image_android_native_buffer(disp, ctx,
935 (struct ANativeWindowBuffer *) buffer);
936 default:
937 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
938 }
939 }
940
941 static void
942 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
943 {
944 }
945
946 static int
947 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
948 unsigned int *attachments, int count)
949 {
950 int num_buffers = 0;
951
952 /* fill dri2_surf->buffers */
953 for (int i = 0; i < count * 2; i += 2) {
954 __DRIbuffer *buf, *local;
955
956 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
957 buf = &dri2_surf->buffers[num_buffers];
958
959 switch (attachments[i]) {
960 case __DRI_BUFFER_BACK_LEFT:
961 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
962 buf->attachment = attachments[i];
963 buf->name = get_native_buffer_name(dri2_surf->buffer);
964 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
965 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
966 buf->flags = 0;
967
968 if (buf->name)
969 num_buffers++;
970
971 break;
972 }
973 /* fall through for pbuffers */
974 case __DRI_BUFFER_DEPTH:
975 case __DRI_BUFFER_STENCIL:
976 case __DRI_BUFFER_ACCUM:
977 case __DRI_BUFFER_DEPTH_STENCIL:
978 case __DRI_BUFFER_HIZ:
979 local = dri2_egl_surface_alloc_local_buffer(dri2_surf,
980 attachments[i], attachments[i + 1]);
981
982 if (local) {
983 *buf = *local;
984 num_buffers++;
985 }
986 break;
987 case __DRI_BUFFER_FRONT_LEFT:
988 case __DRI_BUFFER_FRONT_RIGHT:
989 case __DRI_BUFFER_FAKE_FRONT_LEFT:
990 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
991 case __DRI_BUFFER_BACK_RIGHT:
992 default:
993 /* no front or right buffers */
994 break;
995 }
996 }
997
998 return num_buffers;
999 }
1000
1001 static __DRIbuffer *
1002 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
1003 int *width, int *height,
1004 unsigned int *attachments, int count,
1005 int *out_count, void *loaderPrivate)
1006 {
1007 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1008
1009 if (update_buffers(dri2_surf) < 0)
1010 return NULL;
1011
1012 *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
1013
1014 if (width)
1015 *width = dri2_surf->base.Width;
1016 if (height)
1017 *height = dri2_surf->base.Height;
1018
1019 return dri2_surf->buffers;
1020 }
1021
1022 static unsigned
1023 droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
1024 {
1025 /* Note: loaderPrivate is _EGLDisplay* */
1026 switch (cap) {
1027 case DRI_LOADER_CAP_RGBA_ORDERING:
1028 return 1;
1029 default:
1030 return 0;
1031 }
1032 }
1033
1034 static EGLBoolean
1035 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
1036 {
1037 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
1038 static const struct {
1039 int format;
1040 unsigned int rgba_masks[4];
1041 } visuals[] = {
1042 { HAL_PIXEL_FORMAT_RGBA_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1043 { HAL_PIXEL_FORMAT_RGBX_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1044 { HAL_PIXEL_FORMAT_RGB_565, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1045 { HAL_PIXEL_FORMAT_BGRA_8888, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1046 };
1047
1048 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1049 int config_count = 0;
1050
1051 /* The nesting of loops is significant here. Also significant is the order
1052 * of the HAL pixel formats. Many Android apps (such as Google's official
1053 * NDK GLES2 example app), and even portions the core framework code (such
1054 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1055 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1056 * window's native format, and instead choose the first EGLConfig whose
1057 * channel sizes match those of the native window format while ignoring the
1058 * channel *ordering*.
1059 *
1060 * We can detect such buggy clients in logcat when they call
1061 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1062 * format and the window's format.
1063 *
1064 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1065 * pixel format i precede those for HAL pixel format i+1. In my
1066 * (chadversary) testing on Android Nougat, this was good enough to pacify
1067 * the buggy clients.
1068 */
1069 for (int i = 0; i < ARRAY_SIZE(visuals); i++) {
1070 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
1071 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1072
1073 const EGLint config_attrs[] = {
1074 EGL_NATIVE_VISUAL_ID, visuals[i].format,
1075 EGL_NATIVE_VISUAL_TYPE, visuals[i].format,
1076 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1077 EGL_RECORDABLE_ANDROID, EGL_TRUE,
1078 EGL_NONE
1079 };
1080
1081 struct dri2_egl_config *dri2_conf =
1082 dri2_add_config(dpy, dri2_dpy->driver_configs[j],
1083 config_count + 1, surface_type, config_attrs,
1084 visuals[i].rgba_masks);
1085 if (dri2_conf) {
1086 if (dri2_conf->base.ConfigID == config_count + 1)
1087 config_count++;
1088 format_count[i]++;
1089 }
1090 }
1091 }
1092
1093 for (int i = 0; i < ARRAY_SIZE(format_count); i++) {
1094 if (!format_count[i]) {
1095 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
1096 visuals[i].format);
1097 }
1098 }
1099
1100 return (config_count != 0);
1101 }
1102
1103 static int
1104 droid_open_device(struct dri2_egl_display *dri2_dpy)
1105 {
1106 int fd = -1, err = -EINVAL;
1107
1108 if (dri2_dpy->gralloc->perform)
1109 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1110 GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1111 &fd);
1112 if (err || fd < 0) {
1113 _eglLog(_EGL_WARNING, "fail to get drm fd");
1114 fd = -1;
1115 }
1116
1117 return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1;
1118 }
1119
1120 static const struct dri2_egl_display_vtbl droid_display_vtbl = {
1121 .authenticate = NULL,
1122 .create_window_surface = droid_create_window_surface,
1123 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
1124 .create_pbuffer_surface = droid_create_pbuffer_surface,
1125 .destroy_surface = droid_destroy_surface,
1126 .create_image = droid_create_image_khr,
1127 .swap_buffers = droid_swap_buffers,
1128 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, /* Android implements the function */
1129 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1130 #if ANDROID_API_LEVEL >= 23
1131 .set_damage_region = droid_set_damage_region,
1132 #else
1133 .set_damage_region = dri2_fallback_set_damage_region,
1134 #endif
1135 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1136 .copy_buffers = dri2_fallback_copy_buffers,
1137 .query_buffer_age = droid_query_buffer_age,
1138 .query_surface = droid_query_surface,
1139 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1140 .get_sync_values = dri2_fallback_get_sync_values,
1141 .get_dri_drawable = dri2_surface_get_dri_drawable,
1142 };
1143
1144 static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1145 .base = { __DRI_DRI2_LOADER, 4 },
1146
1147 .getBuffers = NULL,
1148 .flushFrontBuffer = droid_flush_front_buffer,
1149 .getBuffersWithFormat = droid_get_buffers_with_format,
1150 .getCapability = droid_get_capability,
1151 };
1152
1153 static const __DRIimageLoaderExtension droid_image_loader_extension = {
1154 .base = { __DRI_IMAGE_LOADER, 2 },
1155
1156 .getBuffers = droid_image_get_buffers,
1157 .flushFrontBuffer = droid_flush_front_buffer,
1158 .getCapability = droid_get_capability,
1159 };
1160
1161 static const __DRIextension *droid_dri2_loader_extensions[] = {
1162 &droid_dri2_loader_extension.base,
1163 &image_lookup_extension.base,
1164 &use_invalidate.base,
1165 NULL,
1166 };
1167
1168 static const __DRIextension *droid_image_loader_extensions[] = {
1169 &droid_image_loader_extension.base,
1170 &image_lookup_extension.base,
1171 &use_invalidate.base,
1172 NULL,
1173 };
1174
1175 EGLBoolean
1176 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
1177 {
1178 struct dri2_egl_display *dri2_dpy;
1179 const char *err;
1180 int ret;
1181
1182 /* Not supported yet */
1183 if (disp->Options.ForceSoftware)
1184 return EGL_FALSE;
1185
1186 loader_set_logger(_eglLog);
1187
1188 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1189 if (!dri2_dpy)
1190 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1191
1192 dri2_dpy->fd = -1;
1193 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1194 (const hw_module_t **)&dri2_dpy->gralloc);
1195 if (ret) {
1196 err = "DRI2: failed to get gralloc module";
1197 goto cleanup;
1198 }
1199
1200 dpy->DriverData = (void *) dri2_dpy;
1201
1202 dri2_dpy->fd = droid_open_device(dri2_dpy);
1203 if (dri2_dpy->fd < 0) {
1204 err = "DRI2: failed to open device";
1205 goto cleanup;
1206 }
1207
1208 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1209 if (dri2_dpy->driver_name == NULL) {
1210 err = "DRI2: failed to get driver name";
1211 goto cleanup;
1212 }
1213
1214 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1215
1216 /* render nodes cannot use Gem names, and thus do not support
1217 * the __DRI_DRI2_LOADER extension */
1218 if (!dri2_dpy->is_render_node) {
1219 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
1220 if (!dri2_load_driver(dpy)) {
1221 err = "DRI2: failed to load driver";
1222 goto cleanup;
1223 }
1224 } else {
1225 dri2_dpy->loader_extensions = droid_image_loader_extensions;
1226 if (!dri2_load_driver_dri3(dpy)) {
1227 err = "DRI3: failed to load driver";
1228 goto cleanup;
1229 }
1230 }
1231
1232 if (!dri2_create_screen(dpy)) {
1233 err = "DRI2: failed to create screen";
1234 goto cleanup;
1235 }
1236
1237 if (!dri2_setup_extensions(dpy)) {
1238 err = "DRI2: failed to setup extensions";
1239 goto cleanup;
1240 }
1241
1242 dri2_setup_screen(dpy);
1243
1244 if (!droid_add_configs_for_visuals(drv, dpy)) {
1245 err = "DRI2: failed to add configs";
1246 goto cleanup;
1247 }
1248
1249 dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
1250 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
1251 dpy->Extensions.ANDROID_recordable = EGL_TRUE;
1252 dpy->Extensions.EXT_buffer_age = EGL_TRUE;
1253 #if ANDROID_API_LEVEL >= 23
1254 dpy->Extensions.KHR_partial_update = EGL_TRUE;
1255 #endif
1256 dpy->Extensions.KHR_image = EGL_TRUE;
1257
1258 /* Fill vtbl last to prevent accidentally calling virtual function during
1259 * initialization.
1260 */
1261 dri2_dpy->vtbl = &droid_display_vtbl;
1262
1263 return EGL_TRUE;
1264
1265 cleanup:
1266 dri2_display_destroy(dpy);
1267 return _eglError(EGL_NOT_INITIALIZED, err);
1268 }