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