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
);
210 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
212 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
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
218 mtx_unlock(&disp
->Mutex
);
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.
224 * From ANativeWindow::dequeueBuffer:
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.
234 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
237 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
240 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
241 dri2_surf
->buffer
= NULL
;
243 mtx_lock(&disp
->Mutex
);
245 if (dri2_surf
->dri_image_back
) {
246 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
247 dri2_surf
->dri_image_back
= NULL
;
254 droid_window_cancel_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
256 /* no cancel buffer? */
257 droid_window_enqueue_buffer(disp
, dri2_surf
);
261 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
262 unsigned int att
, unsigned int format
)
264 struct dri2_egl_display
*dri2_dpy
=
265 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
267 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
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
);
276 return dri2_surf
->local_buffers
[att
];
280 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
282 struct dri2_egl_display
*dri2_dpy
=
283 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
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
;
296 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
297 _EGLConfig
*conf
, void *native_window
,
298 const EGLint
*attrib_list
)
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
;
306 dri2_surf
= calloc(1, sizeof *dri2_surf
);
308 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
312 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
313 goto cleanup_surface
;
315 if (type
== EGL_WINDOW_BIT
) {
318 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
319 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
320 goto cleanup_surface
;
322 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
323 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
324 goto cleanup_surface
;
327 if (format
!= dri2_conf
->base
.NativeVisualID
) {
328 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
329 format
, dri2_conf
->base
.NativeVisualID
);
332 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
333 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
336 config
= dri2_get_dri_config(dri2_conf
, type
,
337 dri2_surf
->base
.GLColorspace
);
339 goto cleanup_surface
;
341 dri2_surf
->dri_drawable
=
342 dri2_dpy
->dri2
->createNewDrawable(dri2_dpy
->dri_screen
, config
,
344 if (dri2_surf
->dri_drawable
== NULL
) {
345 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
346 goto cleanup_surface
;
350 window
->common
.incRef(&window
->common
);
351 dri2_surf
->window
= window
;
354 return &dri2_surf
->base
;
363 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
364 _EGLConfig
*conf
, void *native_window
,
365 const EGLint
*attrib_list
)
367 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
368 native_window
, attrib_list
);
372 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
373 _EGLConfig
*conf
, const EGLint
*attrib_list
)
375 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
380 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
382 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
383 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
385 droid_free_local_buffers(dri2_surf
);
387 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
388 if (dri2_surf
->buffer
)
389 droid_window_cancel_buffer(disp
, dri2_surf
);
391 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
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
;
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
;
406 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
414 update_buffers(struct dri2_egl_surface
*dri2_surf
)
416 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
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");
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
;
437 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
439 struct dri2_egl_display
*dri2_dpy
=
440 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
442 if (dri2_surf
->dri_image_front
)
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.
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
,
460 if (!dri2_surf
->dri_image_front
) {
461 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
470 get_back_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
472 struct dri2_egl_display
*dri2_dpy
=
473 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
477 if (dri2_surf
->dri_image_back
)
480 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
481 if (!dri2_surf
->buffer
) {
482 _eglLog(_EGL_WARNING
, "Could not get native buffer");
486 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
488 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
492 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
494 pitch
= dri2_surf
->buffer
->stride
*
495 get_format_bpp(dri2_surf
->buffer
->format
);
497 if (fourcc
== -1 || pitch
== 0) {
498 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
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
,
513 if (!dri2_surf
->dri_image_back
) {
514 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
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.
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.
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
531 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
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.
543 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
547 uint32_t buffer_mask
,
548 struct __DRIimageList
*images
)
550 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
552 images
->image_mask
= 0;
553 images
->front
= NULL
;
556 if (update_buffers(dri2_surf
) < 0)
559 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
560 if (get_front_bo(dri2_surf
, format
) < 0)
563 if (dri2_surf
->dri_image_front
) {
564 images
->front
= dri2_surf
->dri_image_front
;
565 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
569 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
570 if (get_back_bo(dri2_surf
, format
) < 0)
573 if (dri2_surf
->dri_image_back
) {
574 images
->back
= dri2_surf
->dri_image_back
;
575 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
583 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
585 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
586 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
588 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
591 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
593 if (dri2_surf
->buffer
)
594 droid_window_enqueue_buffer(disp
, dri2_surf
);
596 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
602 droid_create_image_from_prime_fd_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
603 struct ANativeWindowBuffer
*buf
, int fd
)
605 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
606 struct android_ycbcr ycbcr
;
613 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
614 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
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
);
622 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
625 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
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
;
634 offsets
[1] = (size_t)ycbcr
.cr
;
635 offsets
[2] = (size_t)ycbcr
.cb
;
637 offsets
[1] = (size_t)ycbcr
.cb
;
638 offsets
[2] = (size_t)ycbcr
.cr
;
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
645 pitches
[0] = ycbcr
.ystride
;
646 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
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
);
652 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
653 buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
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],
672 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
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],
691 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
696 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
697 struct ANativeWindowBuffer
*buf
, int fd
)
701 if (is_yuv(buf
->format
))
702 return droid_create_image_from_prime_fd_yuv(disp
, ctx
, buf
, fd
);
704 const int fourcc
= get_fourcc(buf
->format
);
706 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
710 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
712 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
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,
726 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
730 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
731 struct ANativeWindowBuffer
*buf
)
733 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
734 struct dri2_egl_image
*dri2_img
;
738 name
= get_native_buffer_name(buf
);
740 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
744 format
= get_format(buf
->format
);
748 dri2_img
= calloc(1, sizeof(*dri2_img
));
750 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
754 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
759 dri2_img
->dri_image
=
760 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
767 if (!dri2_img
->dri_image
) {
769 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
773 return &dri2_img
->base
;
777 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
778 EGLint attribute
, EGLint
*value
)
780 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
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
);
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
);
799 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
803 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
805 struct ANativeWindowBuffer
*buf
)
810 /* From the EGL_ANDROID_image_native_buffer spec:
812 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
813 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
815 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
816 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
821 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
822 buf
->common
.version
!= sizeof(*buf
)) {
823 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
827 fd
= get_native_buffer_fd(buf
);
829 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
831 return droid_create_image_from_name(disp
, ctx
, buf
);
835 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
836 _EGLContext
*ctx
, EGLenum target
,
837 EGLClientBuffer buffer
, const EGLint
*attr_list
)
840 case EGL_NATIVE_BUFFER_ANDROID
:
841 return dri2_create_image_android_native_buffer(disp
, ctx
,
842 (struct ANativeWindowBuffer
*) buffer
);
844 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
849 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
854 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
855 unsigned int *attachments
, int count
)
857 int num_buffers
= 0, i
;
859 /* fill dri2_surf->buffers */
860 for (i
= 0; i
< count
* 2; i
+= 2) {
861 __DRIbuffer
*buf
, *local
;
863 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
864 buf
= &dri2_surf
->buffers
[num_buffers
];
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
;
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]);
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
:
900 /* no front or right buffers */
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
)
914 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
916 if (update_buffers(dri2_surf
) < 0)
919 dri2_surf
->buffer_count
=
920 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
923 *width
= dri2_surf
->base
.Width
;
925 *height
= dri2_surf
->base
.Height
;
927 *out_count
= dri2_surf
->buffer_count
;
929 return dri2_surf
->buffers
;
933 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
935 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
936 static const struct {
938 unsigned int rgba_masks
[4];
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 } },
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
,
952 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 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
;
960 for (j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
961 config_attrs
[1] = visuals
[j
].format
;
962 config_attrs
[3] = visuals
[j
].format
;
964 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[i
],
965 count
+ 1, surface_type
, config_attrs
, visuals
[j
].rgba_masks
);
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",
984 droid_open_device(struct dri2_egl_display
*dri2_dpy
)
986 int fd
= -1, err
= -EINVAL
;
988 if (dri2_dpy
->gralloc
->perform
)
989 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
990 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
993 _eglLog(_EGL_WARNING
, "fail to get drm fd");
997 return (fd
>= 0) ? fcntl(fd
, F_DUPFD_CLOEXEC
, 3) : -1;
1000 /* support versions < JellyBean */
1012 droid_log(EGLint level
, const char *msg
)
1025 LOG_FATAL("%s", msg
);
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
,
1052 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1053 .base
= { __DRI_DRI2_LOADER
, 3 },
1056 .flushFrontBuffer
= droid_flush_front_buffer
,
1057 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1060 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1061 .base
= { __DRI_IMAGE_LOADER
, 1 },
1063 .getBuffers
= droid_image_get_buffers
,
1064 .flushFrontBuffer
= droid_flush_front_buffer
,
1067 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1068 &droid_dri2_loader_extension
.base
,
1069 &image_lookup_extension
.base
,
1070 &use_invalidate
.base
,
1074 static const __DRIextension
*droid_image_loader_extensions
[] = {
1075 &droid_image_loader_extension
.base
,
1076 &image_lookup_extension
.base
,
1077 &use_invalidate
.base
,
1082 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1084 struct dri2_egl_display
*dri2_dpy
;
1088 _eglSetLogProc(droid_log
);
1090 loader_set_logger(_eglLog
);
1092 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1094 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1096 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1097 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1099 err
= "DRI2: failed to get gralloc module";
1100 goto cleanup_display
;
1103 dpy
->DriverData
= (void *) dri2_dpy
;
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
;
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
;
1117 if (!dri2_load_driver(dpy
)) {
1118 err
= "DRI2: failed to load driver";
1119 goto cleanup_driver_name
;
1122 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
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
;
1129 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1131 if (!dri2_create_screen(dpy
)) {
1132 err
= "DRI2: failed to create screen";
1133 goto cleanup_driver
;
1136 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
1137 err
= "DRI2: failed to add configs";
1138 goto cleanup_screen
;
1141 dpy
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1142 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1143 dpy
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1145 /* Fill vtbl last to prevent accidentally calling virtual function during
1148 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1153 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1155 dlclose(dri2_dpy
->driver
);
1156 cleanup_driver_name
:
1157 free(dri2_dpy
->driver_name
);
1159 close(dri2_dpy
->fd
);
1162 dpy
->DriverData
= NULL
;
1164 return _eglError(EGL_NOT_INITIALIZED
, err
);