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