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