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.
34 #if ANDROID_VERSION >= 0x402
35 #include <sync/sync.h>
40 #include "egl_dri2_fallbacks.h"
41 #include "gralloc_drm.h"
43 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
46 get_format_bpp(int native
)
51 case HAL_PIXEL_FORMAT_RGBA_8888
:
52 case HAL_PIXEL_FORMAT_RGBX_8888
:
53 case HAL_PIXEL_FORMAT_BGRA_8888
:
56 case HAL_PIXEL_FORMAT_RGB_888
:
59 case HAL_PIXEL_FORMAT_RGB_565
:
62 case HAL_PIXEL_FORMAT_YV12
:
73 /* createImageFromFds requires fourcc format */
74 static int get_fourcc(int native
)
77 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FOURCC_RGB565
;
78 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
79 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
80 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
81 case HAL_PIXEL_FORMAT_YV12
: return __DRI_IMAGE_FOURCC_YVU420
;
83 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
88 static int get_format(int format
)
91 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
92 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
93 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
94 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
95 case HAL_PIXEL_FORMAT_RGB_888
:
98 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
104 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
106 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
108 * Various gralloc implementations exist, but the dma-buf fd tends
109 * to be first. Access it directly to avoid a dependency on specific
112 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
116 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
118 return gralloc_drm_get_gem_handle(buf
->handle
);
122 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
124 #if ANDROID_VERSION >= 0x0402
127 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
131 /* If access to the buffer is controlled by a sync fence, then block on the
134 * It may be more performant to postpone blocking until there is an
135 * immediate need to write to the buffer. But doing so would require adding
136 * hooks to the DRI2 loader.
138 * From the ANativeWindow::dequeueBuffer documentation:
140 * The libsync fence file descriptor returned in the int pointed to by
141 * the fenceFd argument will refer to the fence that must signal
142 * before the dequeued buffer may be written to. A value of -1
143 * indicates that the caller may access the buffer immediately without
144 * waiting on a fence. If a valid file descriptor is returned (i.e.
145 * any value except -1) then the caller is responsible for closing the
149 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
151 * Waits indefinitely if timeout < 0.
154 sync_wait(fence_fd
, timeout
);
158 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
160 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
))
163 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
164 dri2_surf
->window
->lockBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
171 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
173 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
175 /* To avoid blocking other EGL calls, release the display mutex before
176 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
179 mtx_unlock(&disp
->Mutex
);
181 #if ANDROID_VERSION >= 0x0402
182 /* Queue the buffer without a sync fence. This informs the ANativeWindow
183 * that it may access the buffer immediately.
185 * From ANativeWindow::dequeueBuffer:
187 * The fenceFd argument specifies a libsync fence file descriptor for
188 * a fence that must signal before the buffer can be accessed. If
189 * the buffer can be accessed immediately then a value of -1 should
190 * be used. The caller must not use the file descriptor after it
191 * is passed to queueBuffer, and the ANativeWindow implementation
192 * is responsible for closing it.
195 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
198 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
201 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
202 dri2_surf
->buffer
= NULL
;
204 mtx_lock(&disp
->Mutex
);
206 if (dri2_surf
->dri_image
) {
207 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image
);
208 dri2_surf
->dri_image
= NULL
;
215 droid_window_cancel_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
217 /* no cancel buffer? */
218 droid_window_enqueue_buffer(disp
, dri2_surf
);
222 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
223 unsigned int att
, unsigned int format
)
225 struct dri2_egl_display
*dri2_dpy
=
226 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
228 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
231 if (!dri2_surf
->local_buffers
[att
]) {
232 dri2_surf
->local_buffers
[att
] =
233 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
234 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
237 return dri2_surf
->local_buffers
[att
];
241 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
243 struct dri2_egl_display
*dri2_dpy
=
244 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
247 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
248 if (dri2_surf
->local_buffers
[i
]) {
249 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
250 dri2_surf
->local_buffers
[i
]);
251 dri2_surf
->local_buffers
[i
] = NULL
;
257 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
258 _EGLConfig
*conf
, void *native_window
,
259 const EGLint
*attrib_list
)
261 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
262 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
263 struct dri2_egl_surface
*dri2_surf
;
264 struct ANativeWindow
*window
= native_window
;
265 const __DRIconfig
*config
;
267 dri2_surf
= calloc(1, sizeof *dri2_surf
);
269 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
273 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
274 goto cleanup_surface
;
276 if (type
== EGL_WINDOW_BIT
) {
279 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
280 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
281 goto cleanup_surface
;
283 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
284 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
285 goto cleanup_surface
;
288 if (format
!= dri2_conf
->base
.NativeVisualID
) {
289 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
290 format
, dri2_conf
->base
.NativeVisualID
);
293 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
294 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
297 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
298 dri2_surf
->base
.GLColorspace
);
300 goto cleanup_surface
;
302 dri2_surf
->dri_drawable
=
303 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
305 if (dri2_surf
->dri_drawable
== NULL
) {
306 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
307 goto cleanup_surface
;
311 window
->common
.incRef(&window
->common
);
312 dri2_surf
->window
= window
;
315 return &dri2_surf
->base
;
324 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
325 _EGLConfig
*conf
, void *native_window
,
326 const EGLint
*attrib_list
)
328 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
329 native_window
, attrib_list
);
333 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
334 _EGLConfig
*conf
, const EGLint
*attrib_list
)
336 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
341 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
343 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
344 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
346 if (!_eglPutSurface(surf
))
349 droid_free_local_buffers(dri2_surf
);
351 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
352 if (dri2_surf
->buffer
)
353 droid_window_cancel_buffer(disp
, dri2_surf
);
355 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
358 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
366 update_buffers(struct dri2_egl_surface
*dri2_surf
)
368 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
371 /* try to dequeue the next back buffer */
372 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
373 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
377 /* free outdated buffers and update the surface size */
378 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
379 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
380 droid_free_local_buffers(dri2_surf
);
381 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
382 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
389 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
391 struct dri2_egl_display
*dri2_dpy
=
392 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
396 if (dri2_surf
->dri_image
)
399 if (!dri2_surf
->buffer
)
402 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
404 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
408 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
410 pitch
= dri2_surf
->buffer
->stride
*
411 get_format_bpp(dri2_surf
->buffer
->format
);
413 if (fourcc
== -1 || pitch
== 0) {
414 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
419 dri2_surf
->dri_image
=
420 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
421 dri2_surf
->base
.Width
,
422 dri2_surf
->base
.Height
,
429 if (!dri2_surf
->dri_image
)
436 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
440 uint32_t buffer_mask
,
441 struct __DRIimageList
*images
)
443 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
445 images
->image_mask
= 0;
447 if (update_buffers(dri2_surf
) < 0)
450 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
452 * We don't support front buffers and GLES doesn't require them for
453 * window surfaces, but some DRI drivers will request them anyway.
454 * We just ignore such request as other platforms backends do.
458 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
459 if (get_back_bo(dri2_surf
) < 0)
462 images
->back
= dri2_surf
->dri_image
;
463 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
470 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
472 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
473 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
475 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
478 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
480 if (dri2_surf
->buffer
)
481 droid_window_enqueue_buffer(disp
, dri2_surf
);
483 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
489 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
490 struct ANativeWindowBuffer
*buf
, int fd
)
492 unsigned int offsets
[3] = { 0, 0, 0 };
493 unsigned int pitches
[3] = { 0, 0, 0 };
495 const int fourcc
= get_fourcc(buf
->format
);
497 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
501 pitches
[0] = buf
->stride
* get_format_bpp(buf
->format
);
502 if (pitches
[0] == 0) {
503 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
507 switch (buf
->format
) {
508 case HAL_PIXEL_FORMAT_YV12
:
509 /* Y plane is assumed to be at offset 0. */
510 /* Cr plane is located after Y plane */
511 offsets
[1] = offsets
[0] + pitches
[0] * buf
->height
;
512 pitches
[1] = ALIGN(pitches
[0] / 2, 16);
513 /* Cb plane is located after Cr plane */
514 offsets
[2] = offsets
[1] + pitches
[1] * buf
->height
/ 2;
515 pitches
[2] = pitches
[1];
517 const EGLint attr_list_yv12
[] = {
518 EGL_WIDTH
, buf
->width
,
519 EGL_HEIGHT
, buf
->height
,
520 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
521 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
522 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
523 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
524 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
525 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
526 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
527 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
528 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
529 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
533 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_yv12
);
536 const EGLint attr_list
[] = {
537 EGL_WIDTH
, buf
->width
,
538 EGL_HEIGHT
, buf
->height
,
539 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
540 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
541 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
542 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
546 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
550 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
551 struct ANativeWindowBuffer
*buf
)
553 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
554 struct dri2_egl_image
*dri2_img
;
558 name
= get_native_buffer_name(buf
);
560 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
564 format
= get_format(buf
->format
);
568 dri2_img
= calloc(1, sizeof(*dri2_img
));
570 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
574 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
579 dri2_img
->dri_image
=
580 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
587 if (!dri2_img
->dri_image
) {
589 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
593 return &dri2_img
->base
;
597 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
598 EGLint attribute
, EGLint
*value
)
600 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
603 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
604 dri2_surf
->window
->query(dri2_surf
->window
,
605 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
610 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
611 dri2_surf
->window
->query(dri2_surf
->window
,
612 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
619 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
623 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
625 struct ANativeWindowBuffer
*buf
)
630 /* From the EGL_ANDROID_image_native_buffer spec:
632 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
633 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
635 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
636 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
641 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
642 buf
->common
.version
!= sizeof(*buf
)) {
643 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
647 fd
= get_native_buffer_fd(buf
);
649 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
651 return droid_create_image_from_name(disp
, ctx
, buf
);
655 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
656 _EGLContext
*ctx
, EGLenum target
,
657 EGLClientBuffer buffer
, const EGLint
*attr_list
)
660 case EGL_NATIVE_BUFFER_ANDROID
:
661 return dri2_create_image_android_native_buffer(disp
, ctx
,
662 (struct ANativeWindowBuffer
*) buffer
);
664 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
669 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
674 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
675 unsigned int *attachments
, int count
)
677 int num_buffers
= 0, i
;
679 /* fill dri2_surf->buffers */
680 for (i
= 0; i
< count
* 2; i
+= 2) {
681 __DRIbuffer
*buf
, *local
;
683 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
684 buf
= &dri2_surf
->buffers
[num_buffers
];
686 switch (attachments
[i
]) {
687 case __DRI_BUFFER_BACK_LEFT
:
688 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
689 buf
->attachment
= attachments
[i
];
690 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
691 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
692 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
700 /* fall through for pbuffers */
701 case __DRI_BUFFER_DEPTH
:
702 case __DRI_BUFFER_STENCIL
:
703 case __DRI_BUFFER_ACCUM
:
704 case __DRI_BUFFER_DEPTH_STENCIL
:
705 case __DRI_BUFFER_HIZ
:
706 local
= droid_alloc_local_buffer(dri2_surf
,
707 attachments
[i
], attachments
[i
+ 1]);
714 case __DRI_BUFFER_FRONT_LEFT
:
715 case __DRI_BUFFER_FRONT_RIGHT
:
716 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
717 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
718 case __DRI_BUFFER_BACK_RIGHT
:
720 /* no front or right buffers */
729 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
730 int *width
, int *height
,
731 unsigned int *attachments
, int count
,
732 int *out_count
, void *loaderPrivate
)
734 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
736 if (update_buffers(dri2_surf
) < 0)
739 dri2_surf
->buffer_count
=
740 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
743 *width
= dri2_surf
->base
.Width
;
745 *height
= dri2_surf
->base
.Height
;
747 *out_count
= dri2_surf
->buffer_count
;
749 return dri2_surf
->buffers
;
753 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
755 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
758 unsigned int rgba_masks
[4];
760 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
761 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
762 { HAL_PIXEL_FORMAT_RGB_888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
763 { HAL_PIXEL_FORMAT_RGB_565
, { 0xf800, 0x7e0, 0x1f, 0x0 } },
764 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
765 { 0, { 0, 0, 0, 0 } }
767 EGLint config_attrs
[] = {
768 EGL_NATIVE_VISUAL_ID
, 0,
769 EGL_NATIVE_VISUAL_TYPE
, 0,
770 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
771 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
777 for (i
= 0; visuals
[i
].format
; i
++) {
778 int format_count
= 0;
780 config_attrs
[1] = visuals
[i
].format
;
781 config_attrs
[3] = visuals
[i
].format
;
783 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
784 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
785 struct dri2_egl_config
*dri2_conf
;
786 unsigned int double_buffered
= 0;
788 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
789 __DRI_ATTRIB_DOUBLE_BUFFER
, &double_buffered
);
791 /* support only double buffered configs */
792 if (!double_buffered
)
795 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
796 count
+ 1, surface_type
, config_attrs
, visuals
[i
].rgba_masks
);
804 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
809 /* post-process configs */
810 for (i
= 0; i
< dpy
->Configs
->Size
; i
++) {
811 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(dpy
->Configs
->Elements
[i
]);
813 /* there is no front buffer so no OpenGL */
814 dri2_conf
->base
.RenderableType
&= ~EGL_OPENGL_BIT
;
815 dri2_conf
->base
.Conformant
&= ~EGL_OPENGL_BIT
;
822 droid_open_device(void)
824 const hw_module_t
*mod
;
827 err
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
, &mod
);
829 const gralloc_module_t
*gr
= (gralloc_module_t
*) mod
;
833 err
= gr
->perform(gr
, GRALLOC_MODULE_PERFORM_GET_DRM_FD
, &fd
);
836 _eglLog(_EGL_WARNING
, "fail to get drm fd");
840 return (fd
>= 0) ? dup(fd
) : -1;
843 /* support versions < JellyBean */
855 droid_log(EGLint level
, const char *msg
)
868 LOG_FATAL("%s", msg
);
875 static struct dri2_egl_display_vtbl droid_display_vtbl
= {
876 .authenticate
= NULL
,
877 .create_window_surface
= droid_create_window_surface
,
878 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
879 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
880 .destroy_surface
= droid_destroy_surface
,
881 .create_image
= droid_create_image_khr
,
882 .swap_interval
= dri2_fallback_swap_interval
,
883 .swap_buffers
= droid_swap_buffers
,
884 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
885 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
886 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
887 .copy_buffers
= dri2_fallback_copy_buffers
,
888 .query_buffer_age
= dri2_fallback_query_buffer_age
,
889 .query_surface
= droid_query_surface
,
890 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
891 .get_sync_values
= dri2_fallback_get_sync_values
,
892 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
895 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
896 .base
= { __DRI_IMAGE_LOADER
, 1 },
898 .getBuffers
= droid_image_get_buffers
,
899 .flushFrontBuffer
= droid_flush_front_buffer
,
903 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
905 struct dri2_egl_display
*dri2_dpy
;
908 _eglSetLogProc(droid_log
);
910 loader_set_logger(_eglLog
);
912 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
914 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
916 dpy
->DriverData
= (void *) dri2_dpy
;
918 dri2_dpy
->fd
= droid_open_device();
919 if (dri2_dpy
->fd
< 0) {
920 err
= "DRI2: failed to open device";
921 goto cleanup_display
;
924 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
925 if (dri2_dpy
->driver_name
== NULL
) {
926 err
= "DRI2: failed to get driver name";
930 if (!dri2_load_driver(dpy
)) {
931 err
= "DRI2: failed to load driver";
932 goto cleanup_driver_name
;
935 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
937 /* render nodes cannot use Gem names, and thus do not support
938 * the __DRI_DRI2_LOADER extension */
939 if (!dri2_dpy
->is_render_node
) {
940 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
941 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
942 dri2_dpy
->dri2_loader_extension
.getBuffers
= NULL
;
943 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= droid_flush_front_buffer
;
944 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
945 droid_get_buffers_with_format
;
946 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
948 dri2_dpy
->extensions
[0] = &droid_image_loader_extension
.base
;
950 dri2_dpy
->extensions
[1] = &use_invalidate
.base
;
951 dri2_dpy
->extensions
[2] = &image_lookup_extension
.base
;
952 dri2_dpy
->extensions
[3] = NULL
;
955 if (!dri2_create_screen(dpy
)) {
956 err
= "DRI2: failed to create screen";
960 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
961 err
= "DRI2: failed to add configs";
965 dpy
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
966 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
967 dpy
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
968 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
970 /* Fill vtbl last to prevent accidentally calling virtual function during
973 dri2_dpy
->vtbl
= &droid_display_vtbl
;
978 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
980 dlclose(dri2_dpy
->driver
);
982 free(dri2_dpy
->driver_name
);
987 dpy
->DriverData
= NULL
;
989 return _eglError(EGL_NOT_INITIALIZED
, err
);