egl: android: directly use dri2_create_image_dma_buf()
[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 <xf86drm.h>
33
34 #if ANDROID_VERSION >= 0x402
35 #include <sync/sync.h>
36 #endif
37
38 #include "loader.h"
39 #include "egl_dri2.h"
40 #include "egl_dri2_fallbacks.h"
41 #include "gralloc_drm.h"
42
43 static int
44 get_format_bpp(int native)
45 {
46 int bpp;
47
48 switch (native) {
49 case HAL_PIXEL_FORMAT_RGBA_8888:
50 case HAL_PIXEL_FORMAT_RGBX_8888:
51 case HAL_PIXEL_FORMAT_BGRA_8888:
52 bpp = 4;
53 break;
54 case HAL_PIXEL_FORMAT_RGB_888:
55 bpp = 3;
56 break;
57 case HAL_PIXEL_FORMAT_RGB_565:
58 bpp = 2;
59 break;
60 default:
61 bpp = 0;
62 break;
63 }
64
65 return bpp;
66 }
67
68 /* createImageFromFds requires fourcc format */
69 static int get_fourcc(int format)
70 {
71 switch(format) {
72 case __DRI_IMAGE_FORMAT_RGB565: return __DRI_IMAGE_FOURCC_RGB565;
73 case __DRI_IMAGE_FORMAT_ARGB8888: return __DRI_IMAGE_FOURCC_ARGB8888;
74 case __DRI_IMAGE_FORMAT_XRGB8888: return __DRI_IMAGE_FOURCC_XRGB8888;
75 case __DRI_IMAGE_FORMAT_ABGR8888: return __DRI_IMAGE_FOURCC_ABGR8888;
76 case __DRI_IMAGE_FORMAT_XBGR8888: return __DRI_IMAGE_FOURCC_XBGR8888;
77 }
78 return -1;
79 }
80
81 static int get_format(int format)
82 {
83 switch (format) {
84 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
85 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
86 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
87 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
88 case HAL_PIXEL_FORMAT_RGB_888:
89 /* unsupported */
90 default:
91 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
92 }
93 return -1;
94 }
95 static int
96 get_native_buffer_fd(struct ANativeWindowBuffer *buf)
97 {
98 native_handle_t *handle = (native_handle_t *)buf->handle;
99 /*
100 * Various gralloc implementations exist, but the dma-buf fd tends
101 * to be first. Access it directly to avoid a dependency on specific
102 * gralloc versions.
103 */
104 return (handle && handle->numFds) ? handle->data[0] : -1;
105 }
106
107 static int
108 get_native_buffer_name(struct ANativeWindowBuffer *buf)
109 {
110 return gralloc_drm_get_gem_handle(buf->handle);
111 }
112
113 static EGLBoolean
114 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
115 {
116 #if ANDROID_VERSION >= 0x0402
117 int fence_fd;
118
119 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
120 &fence_fd))
121 return EGL_FALSE;
122
123 /* If access to the buffer is controlled by a sync fence, then block on the
124 * fence.
125 *
126 * It may be more performant to postpone blocking until there is an
127 * immediate need to write to the buffer. But doing so would require adding
128 * hooks to the DRI2 loader.
129 *
130 * From the ANativeWindow::dequeueBuffer documentation:
131 *
132 * The libsync fence file descriptor returned in the int pointed to by
133 * the fenceFd argument will refer to the fence that must signal
134 * before the dequeued buffer may be written to. A value of -1
135 * indicates that the caller may access the buffer immediately without
136 * waiting on a fence. If a valid file descriptor is returned (i.e.
137 * any value except -1) then the caller is responsible for closing the
138 * file descriptor.
139 */
140 if (fence_fd >= 0) {
141 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
142 *
143 * Waits indefinitely if timeout < 0.
144 */
145 int timeout = -1;
146 sync_wait(fence_fd, timeout);
147 close(fence_fd);
148 }
149
150 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
151 #else
152 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
153 return EGL_FALSE;
154
155 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
156 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
157 #endif
158
159 return EGL_TRUE;
160 }
161
162 static EGLBoolean
163 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
164 {
165 #if ANDROID_VERSION >= 0x0402
166 /* Queue the buffer without a sync fence. This informs the ANativeWindow
167 * that it may access the buffer immediately.
168 *
169 * From ANativeWindow::dequeueBuffer:
170 *
171 * The fenceFd argument specifies a libsync fence file descriptor for
172 * a fence that must signal before the buffer can be accessed. If
173 * the buffer can be accessed immediately then a value of -1 should
174 * be used. The caller must not use the file descriptor after it
175 * is passed to queueBuffer, and the ANativeWindow implementation
176 * is responsible for closing it.
177 */
178 int fence_fd = -1;
179 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
180 fence_fd);
181 #else
182 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
183 #endif
184
185 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
186 dri2_surf->buffer = NULL;
187
188 return EGL_TRUE;
189 }
190
191 static void
192 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
193 {
194 /* no cancel buffer? */
195 droid_window_enqueue_buffer(dri2_surf);
196 }
197
198 static __DRIbuffer *
199 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
200 unsigned int att, unsigned int format)
201 {
202 struct dri2_egl_display *dri2_dpy =
203 dri2_egl_display(dri2_surf->base.Resource.Display);
204
205 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
206 return NULL;
207
208 if (!dri2_surf->local_buffers[att]) {
209 dri2_surf->local_buffers[att] =
210 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
211 dri2_surf->base.Width, dri2_surf->base.Height);
212 }
213
214 return dri2_surf->local_buffers[att];
215 }
216
217 static void
218 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
219 {
220 struct dri2_egl_display *dri2_dpy =
221 dri2_egl_display(dri2_surf->base.Resource.Display);
222 int i;
223
224 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
225 if (dri2_surf->local_buffers[i]) {
226 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
227 dri2_surf->local_buffers[i]);
228 dri2_surf->local_buffers[i] = NULL;
229 }
230 }
231 }
232
233 static _EGLSurface *
234 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
235 _EGLConfig *conf, void *native_window,
236 const EGLint *attrib_list)
237 {
238 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
239 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
240 struct dri2_egl_surface *dri2_surf;
241 struct ANativeWindow *window = native_window;
242 const __DRIconfig *config;
243
244 dri2_surf = calloc(1, sizeof *dri2_surf);
245 if (!dri2_surf) {
246 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
247 return NULL;
248 }
249
250 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
251 goto cleanup_surface;
252
253 if (type == EGL_WINDOW_BIT) {
254 int format;
255
256 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
257 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
258 goto cleanup_surface;
259 }
260 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
261 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
262 goto cleanup_surface;
263 }
264
265 if (format != dri2_conf->base.NativeVisualID) {
266 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
267 format, dri2_conf->base.NativeVisualID);
268 }
269
270 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
271 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
272 }
273
274 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
275 dri2_surf->base.GLColorspace);
276
277 dri2_surf->dri_drawable =
278 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
279 dri2_surf);
280 if (dri2_surf->dri_drawable == NULL) {
281 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
282 goto cleanup_surface;
283 }
284
285 if (window) {
286 window->common.incRef(&window->common);
287 dri2_surf->window = window;
288 }
289
290 return &dri2_surf->base;
291
292 cleanup_surface:
293 free(dri2_surf);
294
295 return NULL;
296 }
297
298 static _EGLSurface *
299 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
300 _EGLConfig *conf, void *native_window,
301 const EGLint *attrib_list)
302 {
303 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
304 native_window, attrib_list);
305 }
306
307 static _EGLSurface *
308 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
309 _EGLConfig *conf, const EGLint *attrib_list)
310 {
311 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
312 NULL, attrib_list);
313 }
314
315 static EGLBoolean
316 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
317 {
318 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
319 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
320
321 if (!_eglPutSurface(surf))
322 return EGL_TRUE;
323
324 droid_free_local_buffers(dri2_surf);
325
326 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
327 if (dri2_surf->buffer)
328 droid_window_cancel_buffer(dri2_surf);
329
330 dri2_surf->window->common.decRef(&dri2_surf->window->common);
331 }
332
333 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
334
335 free(dri2_surf);
336
337 return EGL_TRUE;
338 }
339
340 static int
341 update_buffers(struct dri2_egl_surface *dri2_surf)
342 {
343 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
344 return 0;
345
346 /* try to dequeue the next back buffer */
347 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
348 return -1;
349
350 /* free outdated buffers and update the surface size */
351 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
352 dri2_surf->base.Height != dri2_surf->buffer->height) {
353 droid_free_local_buffers(dri2_surf);
354 dri2_surf->base.Width = dri2_surf->buffer->width;
355 dri2_surf->base.Height = dri2_surf->buffer->height;
356 }
357
358 return 0;
359 }
360
361 static int
362 get_back_bo(struct dri2_egl_surface *dri2_surf)
363 {
364 struct dri2_egl_display *dri2_dpy =
365 dri2_egl_display(dri2_surf->base.Resource.Display);
366 int fourcc, pitch;
367 int offset = 0, fd;
368
369 if (!dri2_surf->buffer)
370 return -1;
371
372 fd = get_native_buffer_fd(dri2_surf->buffer);
373 if (fd < 0)
374 return -1;
375
376 fourcc = get_fourcc(get_format(dri2_surf->buffer->format));
377
378 pitch = dri2_surf->buffer->stride *
379 get_format_bpp(dri2_surf->buffer->format);
380
381 if (fourcc == -1 || pitch == 0)
382 return -1;
383
384 dri2_surf->dri_image =
385 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
386 dri2_surf->base.Width,
387 dri2_surf->base.Height,
388 fourcc,
389 &fd,
390 1,
391 &pitch,
392 &offset,
393 dri2_surf);
394 if (!dri2_surf->dri_image)
395 return -1;
396
397 return 0;
398 }
399
400 static int
401 droid_image_get_buffers(__DRIdrawable *driDrawable,
402 unsigned int format,
403 uint32_t *stamp,
404 void *loaderPrivate,
405 uint32_t buffer_mask,
406 struct __DRIimageList *images)
407 {
408 struct dri2_egl_surface *dri2_surf = loaderPrivate;
409
410 if (update_buffers(dri2_surf) < 0)
411 return 0;
412
413 if (get_back_bo(dri2_surf) < 0) {
414 _eglError(EGL_BAD_PARAMETER, "get_back_bo");
415 return 0;
416 }
417
418 images->image_mask = __DRI_IMAGE_BUFFER_BACK;
419 images->back = dri2_surf->dri_image;
420
421 return 1;
422 }
423
424 static EGLBoolean
425 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
426 {
427 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
428 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
429 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
430 _EGLContext *ctx;
431
432 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
433 return EGL_TRUE;
434
435 if (dri2_drv->glFlush) {
436 ctx = _eglGetCurrentContext();
437 if (ctx && ctx->DrawSurface == &dri2_surf->base)
438 dri2_drv->glFlush();
439 }
440
441 dri2_flush_drawable_for_swapbuffers(disp, draw);
442
443 if (dri2_surf->buffer)
444 droid_window_enqueue_buffer(dri2_surf);
445
446 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
447
448 return EGL_TRUE;
449 }
450
451 static _EGLImage *
452 dri2_create_image_android_native_buffer(_EGLDriver *drv, _EGLDisplay *disp,
453 _EGLContext *ctx,
454 struct ANativeWindowBuffer *buf)
455 {
456 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
457 struct dri2_egl_image *dri2_img;
458 int name, fd;
459 int format;
460
461 if (ctx != NULL) {
462 /* From the EGL_ANDROID_image_native_buffer spec:
463 *
464 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
465 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
466 */
467 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
468 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
469 "EGL_NO_CONTEXT");
470 return NULL;
471 }
472
473 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
474 buf->common.version != sizeof(*buf)) {
475 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
476 return NULL;
477 }
478
479 fd = get_native_buffer_fd(buf);
480 if (fd >= 0) {
481 const int fourcc = get_fourcc(get_format(buf->format));
482 const int pitch = buf->stride * get_format_bpp(buf->format);
483
484 const EGLint attr_list[14] = {
485 EGL_WIDTH, buf->width,
486 EGL_HEIGHT, buf->height,
487 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
488 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
489 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
490 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
491 EGL_NONE, 0
492 };
493
494 if (fourcc == -1 || pitch == 0)
495 return NULL;
496
497 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
498 }
499
500 name = get_native_buffer_name(buf);
501 if (!name) {
502 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
503 return NULL;
504 }
505
506 format = get_format(buf->format);
507 if (format == -1)
508 return NULL;
509
510 dri2_img = calloc(1, sizeof(*dri2_img));
511 if (!dri2_img) {
512 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
513 return NULL;
514 }
515
516 if (!_eglInitImage(&dri2_img->base, disp)) {
517 free(dri2_img);
518 return NULL;
519 }
520
521 dri2_img->dri_image =
522 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
523 buf->width,
524 buf->height,
525 format,
526 name,
527 buf->stride,
528 dri2_img);
529 if (!dri2_img->dri_image) {
530 free(dri2_img);
531 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
532 return NULL;
533 }
534
535 return &dri2_img->base;
536 }
537
538 static _EGLImage *
539 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
540 _EGLContext *ctx, EGLenum target,
541 EGLClientBuffer buffer, const EGLint *attr_list)
542 {
543 switch (target) {
544 case EGL_NATIVE_BUFFER_ANDROID:
545 return dri2_create_image_android_native_buffer(drv, disp, ctx,
546 (struct ANativeWindowBuffer *) buffer);
547 default:
548 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
549 }
550 }
551
552 static void
553 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
554 {
555 }
556
557 static int
558 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
559 unsigned int *attachments, int count)
560 {
561 int num_buffers = 0, i;
562
563 /* fill dri2_surf->buffers */
564 for (i = 0; i < count * 2; i += 2) {
565 __DRIbuffer *buf, *local;
566
567 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
568 buf = &dri2_surf->buffers[num_buffers];
569
570 switch (attachments[i]) {
571 case __DRI_BUFFER_BACK_LEFT:
572 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
573 buf->attachment = attachments[i];
574 buf->name = get_native_buffer_name(dri2_surf->buffer);
575 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
576 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
577 buf->flags = 0;
578
579 if (buf->name)
580 num_buffers++;
581
582 break;
583 }
584 /* fall through for pbuffers */
585 case __DRI_BUFFER_DEPTH:
586 case __DRI_BUFFER_STENCIL:
587 case __DRI_BUFFER_ACCUM:
588 case __DRI_BUFFER_DEPTH_STENCIL:
589 case __DRI_BUFFER_HIZ:
590 local = droid_alloc_local_buffer(dri2_surf,
591 attachments[i], attachments[i + 1]);
592
593 if (local) {
594 *buf = *local;
595 num_buffers++;
596 }
597 break;
598 case __DRI_BUFFER_FRONT_LEFT:
599 case __DRI_BUFFER_FRONT_RIGHT:
600 case __DRI_BUFFER_FAKE_FRONT_LEFT:
601 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
602 case __DRI_BUFFER_BACK_RIGHT:
603 default:
604 /* no front or right buffers */
605 break;
606 }
607 }
608
609 return num_buffers;
610 }
611
612 static __DRIbuffer *
613 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
614 int *width, int *height,
615 unsigned int *attachments, int count,
616 int *out_count, void *loaderPrivate)
617 {
618 struct dri2_egl_surface *dri2_surf = loaderPrivate;
619 struct dri2_egl_display *dri2_dpy =
620 dri2_egl_display(dri2_surf->base.Resource.Display);
621 int i;
622
623 if (update_buffers(dri2_surf) < 0)
624 return NULL;
625
626 dri2_surf->buffer_count =
627 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
628
629 if (width)
630 *width = dri2_surf->base.Width;
631 if (height)
632 *height = dri2_surf->base.Height;
633
634 *out_count = dri2_surf->buffer_count;
635
636 return dri2_surf->buffers;
637 }
638
639 static EGLBoolean
640 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
641 {
642 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
643 const struct {
644 int format;
645 unsigned int rgba_masks[4];
646 } visuals[] = {
647 { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
648 { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
649 { HAL_PIXEL_FORMAT_RGB_888, { 0xff, 0xff00, 0xff0000, 0x0 } },
650 { HAL_PIXEL_FORMAT_RGB_565, { 0xf800, 0x7e0, 0x1f, 0x0 } },
651 { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
652 { 0, { 0, 0, 0, 0 } }
653 };
654 EGLint config_attrs[] = {
655 EGL_NATIVE_VISUAL_ID, 0,
656 EGL_NATIVE_VISUAL_TYPE, 0,
657 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
658 EGL_RECORDABLE_ANDROID, EGL_TRUE,
659 EGL_NONE
660 };
661 int count, i, j;
662
663 count = 0;
664 for (i = 0; visuals[i].format; i++) {
665 int format_count = 0;
666
667 config_attrs[1] = visuals[i].format;
668 config_attrs[3] = visuals[i].format;
669
670 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
671 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
672 struct dri2_egl_config *dri2_conf;
673 unsigned int double_buffered = 0;
674
675 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
676 __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
677
678 /* support only double buffered configs */
679 if (!double_buffered)
680 continue;
681
682 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
683 count + 1, surface_type, config_attrs, visuals[i].rgba_masks);
684 if (dri2_conf) {
685 count++;
686 format_count++;
687 }
688 }
689
690 if (!format_count) {
691 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
692 visuals[i].format);
693 }
694 }
695
696 /* post-process configs */
697 for (i = 0; i < dpy->Configs->Size; i++) {
698 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
699
700 /* there is no front buffer so no OpenGL */
701 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
702 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
703 }
704
705 return (count != 0);
706 }
707
708 static int
709 droid_open_device(void)
710 {
711 const hw_module_t *mod;
712 int fd = -1, err;
713
714 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
715 if (!err) {
716 const gralloc_module_t *gr = (gralloc_module_t *) mod;
717
718 err = -EINVAL;
719 if (gr->perform)
720 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
721 }
722 if (err || fd < 0) {
723 _eglLog(_EGL_WARNING, "fail to get drm fd");
724 fd = -1;
725 }
726
727 return (fd >= 0) ? dup(fd) : -1;
728 }
729
730 /* support versions < JellyBean */
731 #ifndef ALOGW
732 #define ALOGW LOGW
733 #endif
734 #ifndef ALOGD
735 #define ALOGD LOGD
736 #endif
737 #ifndef ALOGI
738 #define ALOGI LOGI
739 #endif
740
741 static void
742 droid_log(EGLint level, const char *msg)
743 {
744 switch (level) {
745 case _EGL_DEBUG:
746 ALOGD("%s", msg);
747 break;
748 case _EGL_INFO:
749 ALOGI("%s", msg);
750 break;
751 case _EGL_WARNING:
752 ALOGW("%s", msg);
753 break;
754 case _EGL_FATAL:
755 LOG_FATAL("%s", msg);
756 break;
757 default:
758 break;
759 }
760 }
761
762 static struct dri2_egl_display_vtbl droid_display_vtbl = {
763 .authenticate = NULL,
764 .create_window_surface = droid_create_window_surface,
765 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
766 .create_pbuffer_surface = droid_create_pbuffer_surface,
767 .destroy_surface = droid_destroy_surface,
768 .create_image = droid_create_image_khr,
769 .swap_interval = dri2_fallback_swap_interval,
770 .swap_buffers = droid_swap_buffers,
771 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
772 .swap_buffers_region = dri2_fallback_swap_buffers_region,
773 .post_sub_buffer = dri2_fallback_post_sub_buffer,
774 .copy_buffers = dri2_fallback_copy_buffers,
775 .query_buffer_age = dri2_fallback_query_buffer_age,
776 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
777 .get_sync_values = dri2_fallback_get_sync_values,
778 .get_dri_drawable = dri2_surface_get_dri_drawable,
779 };
780
781 static const __DRIimageLoaderExtension droid_image_loader_extension = {
782 .base = { __DRI_IMAGE_LOADER, 1 },
783
784 .getBuffers = droid_image_get_buffers,
785 .flushFrontBuffer = droid_flush_front_buffer,
786 };
787
788 EGLBoolean
789 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
790 {
791 struct dri2_egl_display *dri2_dpy;
792 const char *err;
793
794 _eglSetLogProc(droid_log);
795
796 loader_set_logger(_eglLog);
797
798 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
799 if (!dri2_dpy)
800 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
801
802 dpy->DriverData = (void *) dri2_dpy;
803
804 dri2_dpy->fd = droid_open_device();
805 if (dri2_dpy->fd < 0) {
806 err = "DRI2: failed to open device";
807 goto cleanup_display;
808 }
809
810 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
811 if (dri2_dpy->driver_name == NULL) {
812 err = "DRI2: failed to get driver name";
813 goto cleanup_device;
814 }
815
816 if (!dri2_load_driver(dpy)) {
817 err = "DRI2: failed to load driver";
818 goto cleanup_driver_name;
819 }
820
821 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
822
823 dri2_dpy->extensions[0] = &droid_image_loader_extension.base;
824 dri2_dpy->extensions[1] = &use_invalidate.base;
825 dri2_dpy->extensions[2] = &image_lookup_extension.base;
826
827 /* render nodes cannot use Gem names, and thus do not support
828 * the __DRI_DRI2_LOADER extension */
829 if (!dri2_dpy->is_render_node) {
830 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
831 dri2_dpy->dri2_loader_extension.base.version = 3;
832 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
833 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
834 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
835 droid_get_buffers_with_format;
836 dri2_dpy->extensions[3] = &dri2_dpy->dri2_loader_extension.base;
837 dri2_dpy->extensions[4] = NULL;
838 } else
839 dri2_dpy->extensions[3] = NULL;
840
841
842 if (!dri2_create_screen(dpy)) {
843 err = "DRI2: failed to create screen";
844 goto cleanup_driver;
845 }
846
847 if (!droid_add_configs_for_visuals(drv, dpy)) {
848 err = "DRI2: failed to add configs";
849 goto cleanup_screen;
850 }
851
852 dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
853 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
854 dpy->Extensions.ANDROID_recordable = EGL_TRUE;
855 dpy->Extensions.KHR_image_base = EGL_TRUE;
856
857 /* Fill vtbl last to prevent accidentally calling virtual function during
858 * initialization.
859 */
860 dri2_dpy->vtbl = &droid_display_vtbl;
861
862 return EGL_TRUE;
863
864 cleanup_screen:
865 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
866 cleanup_driver:
867 dlclose(dri2_dpy->driver);
868 cleanup_driver_name:
869 free(dri2_dpy->driver_name);
870 cleanup_device:
871 close(dri2_dpy->fd);
872 cleanup_display:
873 free(dri2_dpy);
874
875 return _eglError(EGL_NOT_INITIALIZED, err);
876 }