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.
33 #if ANDROID_VERSION >= 0x402
34 #include <sync/sync.h>
39 #include "gralloc_drm.h"
42 get_format_bpp(int native
)
47 case HAL_PIXEL_FORMAT_RGBA_8888
:
48 case HAL_PIXEL_FORMAT_RGBX_8888
:
49 case HAL_PIXEL_FORMAT_BGRA_8888
:
52 case HAL_PIXEL_FORMAT_RGB_888
:
55 case HAL_PIXEL_FORMAT_RGB_565
:
56 case HAL_PIXEL_FORMAT_RGBA_5551
:
57 case HAL_PIXEL_FORMAT_RGBA_4444
:
69 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
71 return gralloc_drm_get_gem_handle(buf
->handle
);
75 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
77 #if ANDROID_VERSION >= 0x0402
80 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
84 /* If access to the buffer is controlled by a sync fence, then block on the
87 * It may be more performant to postpone blocking until there is an
88 * immediate need to write to the buffer. But doing so would require adding
89 * hooks to the DRI2 loader.
91 * From the ANativeWindow::dequeueBuffer documentation:
93 * The libsync fence file descriptor returned in the int pointed to by
94 * the fenceFd argument will refer to the fence that must signal
95 * before the dequeued buffer may be written to. A value of -1
96 * indicates that the caller may access the buffer immediately without
97 * waiting on a fence. If a valid file descriptor is returned (i.e.
98 * any value except -1) then the caller is responsible for closing the
102 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
104 * Waits indefinitely if timeout < 0.
107 sync_wait(fence_fd
, timeout
);
111 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
113 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
))
116 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
117 dri2_surf
->window
->lockBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
124 droid_window_enqueue_buffer(struct dri2_egl_surface
*dri2_surf
)
126 #if ANDROID_VERSION >= 0x0402
127 /* Queue the buffer without a sync fence. This informs the ANativeWindow
128 * that it may access the buffer immediately.
130 * From ANativeWindow::dequeueBuffer:
132 * The fenceFd argument specifies a libsync fence file descriptor for
133 * a fence that must signal before the buffer can be accessed. If
134 * the buffer can be accessed immediately then a value of -1 should
135 * be used. The caller must not use the file descriptor after it
136 * is passed to queueBuffer, and the ANativeWindow implementation
137 * is responsible for closing it.
140 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
143 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
146 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
147 dri2_surf
->buffer
= NULL
;
153 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
155 /* no cancel buffer? */
156 droid_window_enqueue_buffer(dri2_surf
);
160 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
161 unsigned int att
, unsigned int format
)
163 struct dri2_egl_display
*dri2_dpy
=
164 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
166 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
169 if (!dri2_surf
->local_buffers
[att
]) {
170 dri2_surf
->local_buffers
[att
] =
171 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
172 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
175 return dri2_surf
->local_buffers
[att
];
179 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
181 struct dri2_egl_display
*dri2_dpy
=
182 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
185 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
186 if (dri2_surf
->local_buffers
[i
]) {
187 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
188 dri2_surf
->local_buffers
[i
]);
189 dri2_surf
->local_buffers
[i
] = NULL
;
195 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
196 _EGLConfig
*conf
, EGLNativeWindowType window
,
197 const EGLint
*attrib_list
)
199 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
200 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
201 struct dri2_egl_surface
*dri2_surf
;
202 dri2_surf
= calloc(1, sizeof *dri2_surf
);
204 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
208 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
209 goto cleanup_surface
;
211 if (type
== EGL_WINDOW_BIT
) {
214 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
215 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
216 goto cleanup_surface
;
218 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
219 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
220 goto cleanup_surface
;
223 if (format
!= dri2_conf
->base
.NativeVisualID
) {
224 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
225 format
, dri2_conf
->base
.NativeVisualID
);
228 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
229 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
232 dri2_surf
->dri_drawable
=
233 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
,
234 dri2_conf
->dri_double_config
,
236 if (dri2_surf
->dri_drawable
== NULL
) {
237 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
238 goto cleanup_surface
;
242 window
->common
.incRef(&window
->common
);
243 dri2_surf
->window
= window
;
246 return &dri2_surf
->base
;
255 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
256 _EGLConfig
*conf
, EGLNativeWindowType window
,
257 const EGLint
*attrib_list
)
259 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
260 window
, attrib_list
);
264 droid_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
265 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
266 const EGLint
*attrib_list
)
272 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
273 _EGLConfig
*conf
, const EGLint
*attrib_list
)
275 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
280 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
282 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
283 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
285 if (!_eglPutSurface(surf
))
288 droid_free_local_buffers(dri2_surf
);
290 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
291 if (dri2_surf
->buffer
)
292 droid_window_cancel_buffer(dri2_surf
);
294 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
297 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
305 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
307 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
308 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
309 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
312 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
315 if (dri2_drv
->glFlush
) {
316 ctx
= _eglGetCurrentContext();
317 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
321 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
323 if (dri2_surf
->buffer
)
324 droid_window_enqueue_buffer(dri2_surf
);
326 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
332 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
333 struct ANativeWindowBuffer
*buf
)
335 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
336 struct dri2_egl_image
*dri2_img
;
341 /* From the EGL_ANDROID_image_native_buffer spec:
343 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
344 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
346 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
347 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
352 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
353 buf
->common
.version
!= sizeof(*buf
)) {
354 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
358 name
= get_native_buffer_name(buf
);
360 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
364 /* see the table in droid_add_configs_for_visuals */
365 switch (buf
->format
) {
366 case HAL_PIXEL_FORMAT_BGRA_8888
:
367 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
369 case HAL_PIXEL_FORMAT_RGB_565
:
370 format
= __DRI_IMAGE_FORMAT_RGB565
;
372 case HAL_PIXEL_FORMAT_RGBA_8888
:
373 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
375 case HAL_PIXEL_FORMAT_RGBX_8888
:
376 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
378 case HAL_PIXEL_FORMAT_RGB_888
:
379 case HAL_PIXEL_FORMAT_RGBA_5551
:
380 case HAL_PIXEL_FORMAT_RGBA_4444
:
383 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", buf
->format
);
388 dri2_img
= calloc(1, sizeof(*dri2_img
));
390 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
394 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
399 dri2_img
->dri_image
=
400 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
407 if (!dri2_img
->dri_image
) {
409 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
413 return &dri2_img
->base
;
417 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
418 _EGLContext
*ctx
, EGLenum target
,
419 EGLClientBuffer buffer
, const EGLint
*attr_list
)
422 case EGL_NATIVE_BUFFER_ANDROID
:
423 return dri2_create_image_android_native_buffer(disp
, ctx
,
424 (struct ANativeWindowBuffer
*) buffer
);
426 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
431 droid_init_driver_functions(_EGLDriver
*drv
)
433 drv
->API
.CreateWindowSurface
= droid_create_window_surface
;
434 drv
->API
.CreatePixmapSurface
= droid_create_pixmap_surface
;
435 drv
->API
.CreatePbufferSurface
= droid_create_pbuffer_surface
;
436 drv
->API
.DestroySurface
= droid_destroy_surface
;
437 drv
->API
.SwapBuffers
= droid_swap_buffers
;
439 drv
->API
.CreateImageKHR
= droid_create_image_khr
;
443 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
448 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
449 unsigned int *attachments
, int count
)
451 int num_buffers
= 0, i
;
453 /* fill dri2_surf->buffers */
454 for (i
= 0; i
< count
* 2; i
+= 2) {
455 __DRIbuffer
*buf
, *local
;
457 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
458 buf
= &dri2_surf
->buffers
[num_buffers
];
460 switch (attachments
[i
]) {
461 case __DRI_BUFFER_BACK_LEFT
:
462 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
463 buf
->attachment
= attachments
[i
];
464 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
465 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
466 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
474 /* fall through for pbuffers */
475 case __DRI_BUFFER_DEPTH
:
476 case __DRI_BUFFER_STENCIL
:
477 case __DRI_BUFFER_ACCUM
:
478 case __DRI_BUFFER_DEPTH_STENCIL
:
479 case __DRI_BUFFER_HIZ
:
480 local
= droid_alloc_local_buffer(dri2_surf
,
481 attachments
[i
], attachments
[i
+ 1]);
488 case __DRI_BUFFER_FRONT_LEFT
:
489 case __DRI_BUFFER_FRONT_RIGHT
:
490 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
491 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
492 case __DRI_BUFFER_BACK_RIGHT
:
494 /* no front or right buffers */
503 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
504 int *width
, int *height
,
505 unsigned int *attachments
, int count
,
506 int *out_count
, void *loaderPrivate
)
508 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
509 struct dri2_egl_display
*dri2_dpy
=
510 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
513 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
514 /* try to dequeue the next back buffer */
515 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
))
518 /* free outdated buffers and update the surface size */
519 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
520 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
521 droid_free_local_buffers(dri2_surf
);
522 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
523 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
527 dri2_surf
->buffer_count
=
528 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
531 *width
= dri2_surf
->base
.Width
;
533 *height
= dri2_surf
->base
.Height
;
535 *out_count
= dri2_surf
->buffer_count
;;
537 return dri2_surf
->buffers
;
541 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
543 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
546 unsigned int rgba_masks
[4];
548 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
549 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
550 { HAL_PIXEL_FORMAT_RGB_888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
551 { HAL_PIXEL_FORMAT_RGB_565
, { 0xf800, 0x7e0, 0x1f, 0x0 } },
552 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
553 { 0, 0, { 0, 0, 0, 0 } }
558 for (i
= 0; visuals
[i
].format
; i
++) {
559 int format_count
= 0;
561 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
562 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
563 struct dri2_egl_config
*dri2_conf
;
564 unsigned int double_buffered
= 0;
566 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
567 __DRI_ATTRIB_DOUBLE_BUFFER
, &double_buffered
);
569 /* support only double buffered configs */
570 if (!double_buffered
)
573 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
574 count
+ 1, surface_type
, NULL
, visuals
[i
].rgba_masks
);
576 dri2_conf
->base
.NativeVisualID
= visuals
[i
].format
;
577 dri2_conf
->base
.NativeVisualType
= visuals
[i
].format
;
584 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
589 /* post-process configs */
590 for (i
= 0; i
< dpy
->Configs
->Size
; i
++) {
591 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(dpy
->Configs
->Elements
[i
]);
593 /* there is no front buffer so no OpenGL */
594 dri2_conf
->base
.RenderableType
&= ~EGL_OPENGL_BIT
;
595 dri2_conf
->base
.Conformant
&= ~EGL_OPENGL_BIT
;
602 droid_open_device(void)
604 const hw_module_t
*mod
;
607 err
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
, &mod
);
609 const gralloc_module_t
*gr
= (gralloc_module_t
*) mod
;
613 err
= gr
->perform(gr
, GRALLOC_MODULE_PERFORM_GET_DRM_FD
, &fd
);
616 _eglLog(_EGL_WARNING
, "fail to get drm fd");
620 return (fd
>= 0) ? dup(fd
) : -1;
623 /* support versions < JellyBean */
635 droid_log(EGLint level
, const char *msg
)
648 LOG_FATAL("%s", msg
);
656 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
658 struct dri2_egl_display
*dri2_dpy
;
661 _eglSetLogProc(droid_log
);
663 loader_set_logger(_eglLog
);
665 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
667 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
669 dpy
->DriverData
= (void *) dri2_dpy
;
671 dri2_dpy
->fd
= droid_open_device();
672 if (dri2_dpy
->fd
< 0) {
673 err
= "DRI2: failed to open device";
674 goto cleanup_display
;
677 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
678 if (dri2_dpy
->driver_name
== NULL
) {
679 err
= "DRI2: failed to get driver name";
683 if (!dri2_load_driver(dpy
)) {
684 err
= "DRI2: failed to load driver";
685 goto cleanup_driver_name
;
688 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
689 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
690 dri2_dpy
->dri2_loader_extension
.getBuffers
= NULL
;
691 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= droid_flush_front_buffer
;
692 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
693 droid_get_buffers_with_format
;
695 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
696 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
697 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
698 dri2_dpy
->extensions
[3] = NULL
;
700 if (!dri2_create_screen(dpy
)) {
701 err
= "DRI2: failed to create screen";
705 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
706 err
= "DRI2: failed to add configs";
710 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
711 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
713 /* we're supporting EGL 1.4 */
714 dpy
->VersionMajor
= 1;
715 dpy
->VersionMinor
= 4;
717 droid_init_driver_functions(drv
);
722 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
724 dlclose(dri2_dpy
->driver
);
726 free(dri2_dpy
->driver_name
);
732 return _eglError(EGL_NOT_INITIALIZED
, err
);