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