2 * Mesa 3-D graphics library
4 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright (C) 2010-2011 LunarG Inc.
7 * Based on platform_x11, which has
9 * Copyright © 2011 Intel Corporation
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
36 #if ANDROID_VERSION >= 0x402
37 #include <sync/sync.h>
42 #include "egl_dri2_fallbacks.h"
43 #include "gralloc_drm.h"
45 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
47 struct droid_yuv_format
{
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. */
54 int fourcc
; /* __DRI_IMAGE_FOURCC_ */
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
},
68 get_fourcc_yuv(int native
, int is_ycrcb
, int chroma_step
)
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
;
86 for (i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
87 if (droid_yuv_formats
[i
].native
== native
)
94 get_format_bpp(int native
)
99 case HAL_PIXEL_FORMAT_RGBA_8888
:
100 case HAL_PIXEL_FORMAT_RGBX_8888
:
101 case HAL_PIXEL_FORMAT_BGRA_8888
:
104 case HAL_PIXEL_FORMAT_RGB_565
:
115 /* createImageFromFds requires fourcc format */
116 static int get_fourcc(int 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
;
124 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
129 static int get_format(int 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
;
137 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
143 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
145 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
147 * Various gralloc implementations exist, but the dma-buf fd tends
148 * to be first. Access it directly to avoid a dependency on specific
151 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
155 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
157 return gralloc_drm_get_gem_handle(buf
->handle
);
161 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
163 #if ANDROID_VERSION >= 0x0402
166 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
170 /* If access to the buffer is controlled by a sync fence, then block on the
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.
177 * From the ANativeWindow::dequeueBuffer documentation:
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
188 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
190 * Waits indefinitely if timeout < 0.
193 sync_wait(fence_fd
, timeout
);
197 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
199 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
))
202 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
203 dri2_surf
->window
->lockBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
206 /* Record all the buffers created by ANativeWindow and update back buffer
207 * for updating buffer's age in swap_buffers.
209 EGLBoolean updated
= EGL_FALSE
;
210 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
211 if (!dri2_surf
->color_buffers
[i
].buffer
) {
212 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
214 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
215 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
222 /* In case of all the buffers were recreated by ANativeWindow, reset
225 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
226 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
227 dri2_surf
->color_buffers
[i
].age
= 0;
229 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
230 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
237 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
239 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
241 /* To avoid blocking other EGL calls, release the display mutex before
242 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
245 mtx_unlock(&disp
->Mutex
);
247 #if ANDROID_VERSION >= 0x0402
248 /* Queue the buffer without a sync fence. This informs the ANativeWindow
249 * that it may access the buffer immediately.
251 * From ANativeWindow::dequeueBuffer:
253 * The fenceFd argument specifies a libsync fence file descriptor for
254 * a fence that must signal before the buffer can be accessed. If
255 * the buffer can be accessed immediately then a value of -1 should
256 * be used. The caller must not use the file descriptor after it
257 * is passed to queueBuffer, and the ANativeWindow implementation
258 * is responsible for closing it.
261 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
264 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
267 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
268 dri2_surf
->buffer
= NULL
;
269 dri2_surf
->back
= NULL
;
271 mtx_lock(&disp
->Mutex
);
273 if (dri2_surf
->dri_image_back
) {
274 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
275 dri2_surf
->dri_image_back
= NULL
;
282 droid_window_cancel_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
284 /* no cancel buffer? */
285 droid_window_enqueue_buffer(disp
, dri2_surf
);
289 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
290 unsigned int att
, unsigned int format
)
292 struct dri2_egl_display
*dri2_dpy
=
293 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
295 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
298 if (!dri2_surf
->local_buffers
[att
]) {
299 dri2_surf
->local_buffers
[att
] =
300 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
301 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
304 return dri2_surf
->local_buffers
[att
];
308 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
310 struct dri2_egl_display
*dri2_dpy
=
311 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
314 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
315 if (dri2_surf
->local_buffers
[i
]) {
316 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
317 dri2_surf
->local_buffers
[i
]);
318 dri2_surf
->local_buffers
[i
] = NULL
;
324 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
325 _EGLConfig
*conf
, void *native_window
,
326 const EGLint
*attrib_list
)
328 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
329 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
330 struct dri2_egl_surface
*dri2_surf
;
331 struct ANativeWindow
*window
= native_window
;
332 const __DRIconfig
*config
;
334 dri2_surf
= calloc(1, sizeof *dri2_surf
);
336 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
340 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
341 goto cleanup_surface
;
343 if (type
== EGL_WINDOW_BIT
) {
346 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
347 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
348 goto cleanup_surface
;
350 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
351 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
352 goto cleanup_surface
;
355 if (format
!= dri2_conf
->base
.NativeVisualID
) {
356 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
357 format
, dri2_conf
->base
.NativeVisualID
);
360 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
361 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
364 config
= dri2_get_dri_config(dri2_conf
, type
,
365 dri2_surf
->base
.GLColorspace
);
367 goto cleanup_surface
;
369 dri2_surf
->dri_drawable
=
370 dri2_dpy
->dri2
->createNewDrawable(dri2_dpy
->dri_screen
, config
,
372 if (dri2_surf
->dri_drawable
== NULL
) {
373 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
374 goto cleanup_surface
;
378 window
->common
.incRef(&window
->common
);
379 dri2_surf
->window
= window
;
382 return &dri2_surf
->base
;
391 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
392 _EGLConfig
*conf
, void *native_window
,
393 const EGLint
*attrib_list
)
395 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
396 native_window
, attrib_list
);
400 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
401 _EGLConfig
*conf
, const EGLint
*attrib_list
)
403 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
408 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
410 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
411 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
413 droid_free_local_buffers(dri2_surf
);
415 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
416 if (dri2_surf
->buffer
)
417 droid_window_cancel_buffer(disp
, dri2_surf
);
419 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
422 if (dri2_surf
->dri_image_back
) {
423 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
424 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
425 dri2_surf
->dri_image_back
= NULL
;
428 if (dri2_surf
->dri_image_front
) {
429 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
430 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
431 dri2_surf
->dri_image_front
= NULL
;
434 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
442 update_buffers(struct dri2_egl_surface
*dri2_surf
)
444 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
447 /* try to dequeue the next back buffer */
448 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
449 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
453 /* free outdated buffers and update the surface size */
454 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
455 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
456 droid_free_local_buffers(dri2_surf
);
457 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
458 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
465 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
467 struct dri2_egl_display
*dri2_dpy
=
468 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
470 if (dri2_surf
->dri_image_front
)
473 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
474 /* According current EGL spec, front buffer rendering
475 * for window surface is not supported now.
476 * and mesa doesn't have the implementation of this case.
477 * Add warning message, but not treat it as error.
479 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
480 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
481 dri2_surf
->dri_image_front
=
482 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
483 dri2_surf
->base
.Width
,
484 dri2_surf
->base
.Height
,
488 if (!dri2_surf
->dri_image_front
) {
489 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
498 get_back_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
500 struct dri2_egl_display
*dri2_dpy
=
501 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
505 if (dri2_surf
->dri_image_back
)
508 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
509 if (!dri2_surf
->buffer
) {
510 _eglLog(_EGL_WARNING
, "Could not get native buffer");
514 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
516 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
520 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
522 pitch
= dri2_surf
->buffer
->stride
*
523 get_format_bpp(dri2_surf
->buffer
->format
);
525 if (fourcc
== -1 || pitch
== 0) {
526 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
531 dri2_surf
->dri_image_back
=
532 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
533 dri2_surf
->base
.Width
,
534 dri2_surf
->base
.Height
,
541 if (!dri2_surf
->dri_image_back
) {
542 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
545 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
546 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
547 * the spec states that they have a back buffer but no front buffer, in
548 * contrast to pixmaps, which have a front buffer but no back buffer.
550 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
551 * from the spec, following the precedent of Mesa's EGL X11 platform. The
552 * X11 platform correctly assigns pbuffers to single-buffered configs, but
553 * assigns the pbuffer a front buffer instead of a back buffer.
555 * Pbuffers in the X11 platform mostly work today, so let's just copy its
556 * behavior instead of trying to fix (and hence potentially breaking) the
559 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
565 /* Some drivers will pass multiple bits in buffer_mask.
566 * For such case, will go through all the bits, and
567 * will not return error when unsupported buffer is requested, only
568 * return error when the allocation for supported buffer failed.
571 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
575 uint32_t buffer_mask
,
576 struct __DRIimageList
*images
)
578 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
580 images
->image_mask
= 0;
581 images
->front
= NULL
;
584 if (update_buffers(dri2_surf
) < 0)
587 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
588 if (get_front_bo(dri2_surf
, format
) < 0)
591 if (dri2_surf
->dri_image_front
) {
592 images
->front
= dri2_surf
->dri_image_front
;
593 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
597 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
598 if (get_back_bo(dri2_surf
, format
) < 0)
601 if (dri2_surf
->dri_image_back
) {
602 images
->back
= dri2_surf
->dri_image_back
;
603 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
611 droid_query_buffer_age(_EGLDriver
*drv
,
612 _EGLDisplay
*disp
, _EGLSurface
*surface
)
614 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
616 if (update_buffers(dri2_surf
) < 0) {
617 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
621 return dri2_surf
->back
->age
;
625 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
627 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
628 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
630 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
633 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
634 if (dri2_surf
->color_buffers
[i
].age
> 0)
635 dri2_surf
->color_buffers
[i
].age
++;
638 /* Make sure we have a back buffer in case we're swapping without
640 if (get_back_bo(dri2_surf
, 0) < 0) {
641 _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
645 dri2_surf
->back
->age
= 1;
647 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
649 if (dri2_surf
->buffer
)
650 droid_window_enqueue_buffer(disp
, dri2_surf
);
652 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
658 droid_create_image_from_prime_fd_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
659 struct ANativeWindowBuffer
*buf
, int fd
)
661 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
662 struct android_ycbcr ycbcr
;
669 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
670 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
674 memset(&ycbcr
, 0, sizeof(ycbcr
));
675 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
676 0, 0, 0, 0, 0, &ycbcr
);
678 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
681 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
683 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
684 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
685 * so they can be interpreted as offsets. */
686 offsets
[0] = (size_t)ycbcr
.y
;
687 /* We assume here that all the planes are located in one DMA-buf. */
688 is_ycrcb
= (size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
;
690 offsets
[1] = (size_t)ycbcr
.cr
;
691 offsets
[2] = (size_t)ycbcr
.cb
;
693 offsets
[1] = (size_t)ycbcr
.cb
;
694 offsets
[2] = (size_t)ycbcr
.cr
;
697 /* .ystride is the line length (in bytes) of the Y plane,
698 * .cstride is the line length (in bytes) of any of the remaining
699 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
701 pitches
[0] = ycbcr
.ystride
;
702 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
704 /* .chroma_step is the byte distance between the same chroma channel
705 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
706 fourcc
= get_fourcc_yuv(buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
708 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
709 buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
713 if (ycbcr
.chroma_step
== 2) {
714 /* Semi-planar Y + CbCr or Y + CbCr format. */
715 const EGLint attr_list_2plane
[] = {
716 EGL_WIDTH
, buf
->width
,
717 EGL_HEIGHT
, buf
->height
,
718 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
719 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
720 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
721 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
722 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
723 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
724 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
728 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
730 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
731 const EGLint attr_list_3plane
[] = {
732 EGL_WIDTH
, buf
->width
,
733 EGL_HEIGHT
, buf
->height
,
734 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
735 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
736 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
737 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
738 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
739 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
740 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
741 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
742 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
743 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
747 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
752 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
753 struct ANativeWindowBuffer
*buf
, int fd
)
757 if (is_yuv(buf
->format
))
758 return droid_create_image_from_prime_fd_yuv(disp
, ctx
, buf
, fd
);
760 const int fourcc
= get_fourcc(buf
->format
);
762 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
766 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
768 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
772 const EGLint attr_list
[] = {
773 EGL_WIDTH
, buf
->width
,
774 EGL_HEIGHT
, buf
->height
,
775 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
776 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
777 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
778 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
782 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
786 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
787 struct ANativeWindowBuffer
*buf
)
789 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
790 struct dri2_egl_image
*dri2_img
;
794 name
= get_native_buffer_name(buf
);
796 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
800 format
= get_format(buf
->format
);
804 dri2_img
= calloc(1, sizeof(*dri2_img
));
806 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
810 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
815 dri2_img
->dri_image
=
816 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
823 if (!dri2_img
->dri_image
) {
825 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
829 return &dri2_img
->base
;
833 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
834 EGLint attribute
, EGLint
*value
)
836 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
839 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
840 dri2_surf
->window
->query(dri2_surf
->window
,
841 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
846 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
847 dri2_surf
->window
->query(dri2_surf
->window
,
848 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
855 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
859 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
861 struct ANativeWindowBuffer
*buf
)
866 /* From the EGL_ANDROID_image_native_buffer spec:
868 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
869 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
871 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
872 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
877 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
878 buf
->common
.version
!= sizeof(*buf
)) {
879 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
883 fd
= get_native_buffer_fd(buf
);
885 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
887 return droid_create_image_from_name(disp
, ctx
, buf
);
891 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
892 _EGLContext
*ctx
, EGLenum target
,
893 EGLClientBuffer buffer
, const EGLint
*attr_list
)
896 case EGL_NATIVE_BUFFER_ANDROID
:
897 return dri2_create_image_android_native_buffer(disp
, ctx
,
898 (struct ANativeWindowBuffer
*) buffer
);
900 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
905 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
910 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
911 unsigned int *attachments
, int count
)
913 int num_buffers
= 0, i
;
915 /* fill dri2_surf->buffers */
916 for (i
= 0; i
< count
* 2; i
+= 2) {
917 __DRIbuffer
*buf
, *local
;
919 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
920 buf
= &dri2_surf
->buffers
[num_buffers
];
922 switch (attachments
[i
]) {
923 case __DRI_BUFFER_BACK_LEFT
:
924 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
925 buf
->attachment
= attachments
[i
];
926 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
927 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
928 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
936 /* fall through for pbuffers */
937 case __DRI_BUFFER_DEPTH
:
938 case __DRI_BUFFER_STENCIL
:
939 case __DRI_BUFFER_ACCUM
:
940 case __DRI_BUFFER_DEPTH_STENCIL
:
941 case __DRI_BUFFER_HIZ
:
942 local
= droid_alloc_local_buffer(dri2_surf
,
943 attachments
[i
], attachments
[i
+ 1]);
950 case __DRI_BUFFER_FRONT_LEFT
:
951 case __DRI_BUFFER_FRONT_RIGHT
:
952 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
953 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
954 case __DRI_BUFFER_BACK_RIGHT
:
956 /* no front or right buffers */
965 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
966 int *width
, int *height
,
967 unsigned int *attachments
, int count
,
968 int *out_count
, void *loaderPrivate
)
970 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
972 if (update_buffers(dri2_surf
) < 0)
975 dri2_surf
->buffer_count
=
976 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
979 *width
= dri2_surf
->base
.Width
;
981 *height
= dri2_surf
->base
.Height
;
983 *out_count
= dri2_surf
->buffer_count
;
985 return dri2_surf
->buffers
;
989 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
991 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
992 static const struct {
994 unsigned int rgba_masks
[4];
996 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
997 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
998 { HAL_PIXEL_FORMAT_RGB_565
, { 0xf800, 0x7e0, 0x1f, 0x0 } },
999 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
1001 EGLint config_attrs
[] = {
1002 EGL_NATIVE_VISUAL_ID
, 0,
1003 EGL_NATIVE_VISUAL_TYPE
, 0,
1004 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1005 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1008 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1012 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1013 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1014 struct dri2_egl_config
*dri2_conf
;
1016 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
1017 config_attrs
[1] = visuals
[j
].format
;
1018 config_attrs
[3] = visuals
[j
].format
;
1020 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[i
],
1021 count
+ 1, surface_type
, config_attrs
, visuals
[j
].rgba_masks
);
1029 for (i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1030 if (!format_count
[i
]) {
1031 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1036 return (count
!= 0);
1040 droid_open_device(struct dri2_egl_display
*dri2_dpy
)
1042 int fd
= -1, err
= -EINVAL
;
1044 if (dri2_dpy
->gralloc
->perform
)
1045 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1046 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1048 if (err
|| fd
< 0) {
1049 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1053 return (fd
>= 0) ? fcntl(fd
, F_DUPFD_CLOEXEC
, 3) : -1;
1056 /* support versions < JellyBean */
1068 droid_log(EGLint level
, const char *msg
)
1081 LOG_FATAL("%s", msg
);
1088 static struct dri2_egl_display_vtbl droid_display_vtbl
= {
1089 .authenticate
= NULL
,
1090 .create_window_surface
= droid_create_window_surface
,
1091 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1092 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1093 .destroy_surface
= droid_destroy_surface
,
1094 .create_image
= droid_create_image_khr
,
1095 .swap_interval
= dri2_fallback_swap_interval
,
1096 .swap_buffers
= droid_swap_buffers
,
1097 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1098 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1099 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1100 .copy_buffers
= dri2_fallback_copy_buffers
,
1101 .query_buffer_age
= droid_query_buffer_age
,
1102 .query_surface
= droid_query_surface
,
1103 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1104 .get_sync_values
= dri2_fallback_get_sync_values
,
1105 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1108 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1109 .base
= { __DRI_DRI2_LOADER
, 3 },
1112 .flushFrontBuffer
= droid_flush_front_buffer
,
1113 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1116 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1117 .base
= { __DRI_IMAGE_LOADER
, 1 },
1119 .getBuffers
= droid_image_get_buffers
,
1120 .flushFrontBuffer
= droid_flush_front_buffer
,
1123 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1124 &droid_dri2_loader_extension
.base
,
1125 &image_lookup_extension
.base
,
1126 &use_invalidate
.base
,
1130 static const __DRIextension
*droid_image_loader_extensions
[] = {
1131 &droid_image_loader_extension
.base
,
1132 &image_lookup_extension
.base
,
1133 &use_invalidate
.base
,
1138 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1140 struct dri2_egl_display
*dri2_dpy
;
1144 _eglSetLogProc(droid_log
);
1146 loader_set_logger(_eglLog
);
1148 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1150 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1152 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1153 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1155 err
= "DRI2: failed to get gralloc module";
1156 goto cleanup_display
;
1159 dpy
->DriverData
= (void *) dri2_dpy
;
1161 dri2_dpy
->fd
= droid_open_device(dri2_dpy
);
1162 if (dri2_dpy
->fd
< 0) {
1163 err
= "DRI2: failed to open device";
1164 goto cleanup_display
;
1167 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1168 if (dri2_dpy
->driver_name
== NULL
) {
1169 err
= "DRI2: failed to get driver name";
1170 goto cleanup_device
;
1173 if (!dri2_load_driver(dpy
)) {
1174 err
= "DRI2: failed to load driver";
1175 goto cleanup_driver_name
;
1178 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1180 /* render nodes cannot use Gem names, and thus do not support
1181 * the __DRI_DRI2_LOADER extension */
1182 if (!dri2_dpy
->is_render_node
)
1183 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1185 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1187 if (!dri2_create_screen(dpy
)) {
1188 err
= "DRI2: failed to create screen";
1189 goto cleanup_driver
;
1192 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
1193 err
= "DRI2: failed to add configs";
1194 goto cleanup_screen
;
1197 dpy
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1198 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1199 dpy
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1200 dpy
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1202 /* Fill vtbl last to prevent accidentally calling virtual function during
1205 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1210 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1212 dlclose(dri2_dpy
->driver
);
1213 cleanup_driver_name
:
1214 free(dri2_dpy
->driver_name
);
1216 close(dri2_dpy
->fd
);
1219 dpy
->DriverData
= NULL
;
1221 return _eglError(EGL_NOT_INITIALIZED
, err
);