2 * Mesa 3-D graphics library
5 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
6 * Copyright (C) 2010-2011 LunarG Inc.
8 * Based on platform_x11, which has
10 * Copyright © 2011 Intel Corporation
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
34 #if ANDROID_VERSION >= 0x402
35 #include <sync/sync.h>
38 /* for droid_get_pci_id */
41 #include <radeon_drm.h>
44 #include "gralloc_drm.h"
47 get_format_bpp(int native)
52 case HAL_PIXEL_FORMAT_RGBA_8888:
53 case HAL_PIXEL_FORMAT_RGBX_8888:
54 case HAL_PIXEL_FORMAT_BGRA_8888:
57 case HAL_PIXEL_FORMAT_RGB_888:
60 case HAL_PIXEL_FORMAT_RGB_565:
61 case HAL_PIXEL_FORMAT_RGBA_5551:
62 case HAL_PIXEL_FORMAT_RGBA_4444:
74 get_native_buffer_name(struct ANativeWindowBuffer *buf)
76 return gralloc_drm_get_gem_handle(buf->handle);
80 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
82 #if ANDROID_VERSION >= 0x0402
85 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
89 /* If access to the buffer is controlled by a sync fence, then block on the
92 * It may be more performant to postpone blocking until there is an
93 * immediate need to write to the buffer. But doing so would require adding
94 * hooks to the DRI2 loader.
96 * From the ANativeWindow::dequeueBuffer documentation:
98 * The libsync fence file descriptor returned in the int pointed to by
99 * the fenceFd argument will refer to the fence that must signal
100 * before the dequeued buffer may be written to. A value of -1
101 * indicates that the caller may access the buffer immediately without
102 * waiting on a fence. If a valid file descriptor is returned (i.e.
103 * any value except -1) then the caller is responsible for closing the
107 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
109 * Waits indefinitely if timeout < 0.
112 sync_wait(fence_fd, timeout);
116 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
118 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
121 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
122 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
129 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
131 #if ANDROID_VERSION >= 0x0402
132 /* Queue the buffer without a sync fence. This informs the ANativeWindow
133 * that it may access the buffer immediately.
135 * From ANativeWindow::dequeueBuffer:
137 * The fenceFd argument specifies a libsync fence file descriptor for
138 * a fence that must signal before the buffer can be accessed. If
139 * the buffer can be accessed immediately then a value of -1 should
140 * be used. The caller must not use the file descriptor after it
141 * is passed to queueBuffer, and the ANativeWindow implementation
142 * is responsible for closing it.
145 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
148 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
151 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
152 dri2_surf->buffer = NULL;
158 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
160 /* no cancel buffer? */
161 droid_window_enqueue_buffer(dri2_surf);
165 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
166 unsigned int att, unsigned int format)
168 struct dri2_egl_display *dri2_dpy =
169 dri2_egl_display(dri2_surf->base.Resource.Display);
171 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
174 if (!dri2_surf->local_buffers[att]) {
175 dri2_surf->local_buffers[att] =
176 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
177 dri2_surf->base.Width, dri2_surf->base.Height);
180 return dri2_surf->local_buffers[att];
184 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
186 struct dri2_egl_display *dri2_dpy =
187 dri2_egl_display(dri2_surf->base.Resource.Display);
190 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
191 if (dri2_surf->local_buffers[i]) {
192 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
193 dri2_surf->local_buffers[i]);
194 dri2_surf->local_buffers[i] = NULL;
200 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
201 _EGLConfig *conf, EGLNativeWindowType window,
202 const EGLint *attrib_list)
204 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
205 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
206 struct dri2_egl_surface *dri2_surf;
207 dri2_surf = calloc(1, sizeof *dri2_surf);
209 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
213 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
214 goto cleanup_surface;
216 if (type == EGL_WINDOW_BIT) {
219 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
220 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
221 goto cleanup_surface;
223 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
224 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
225 goto cleanup_surface;
228 if (format != dri2_conf->base.NativeVisualID) {
229 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
230 format, dri2_conf->base.NativeVisualID);
233 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
234 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
237 dri2_surf->dri_drawable =
238 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
239 dri2_conf->dri_double_config,
241 if (dri2_surf->dri_drawable == NULL) {
242 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
243 goto cleanup_surface;
247 window->common.incRef(&window->common);
248 dri2_surf->window = window;
251 return &dri2_surf->base;
260 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
261 _EGLConfig *conf, EGLNativeWindowType window,
262 const EGLint *attrib_list)
264 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
265 window, attrib_list);
269 droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
270 _EGLConfig *conf, EGLNativePixmapType pixmap,
271 const EGLint *attrib_list)
277 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
278 _EGLConfig *conf, const EGLint *attrib_list)
280 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
285 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
287 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
288 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
290 if (!_eglPutSurface(surf))
293 droid_free_local_buffers(dri2_surf);
295 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
296 if (dri2_surf->buffer)
297 droid_window_cancel_buffer(dri2_surf);
299 dri2_surf->window->common.decRef(&dri2_surf->window->common);
302 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
310 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
312 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
313 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
314 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
317 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
320 if (dri2_drv->glFlush) {
321 ctx = _eglGetCurrentContext();
322 if (ctx && ctx->DrawSurface == &dri2_surf->base)
326 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
328 if (dri2_surf->buffer)
329 droid_window_enqueue_buffer(dri2_surf);
331 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
337 dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
338 struct ANativeWindowBuffer *buf)
340 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
341 struct dri2_egl_image *dri2_img;
346 /* From the EGL_ANDROID_image_native_buffer spec:
348 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
349 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
351 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
352 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
357 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
358 buf->common.version != sizeof(*buf)) {
359 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
363 name = get_native_buffer_name(buf);
365 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
369 /* see the table in droid_add_configs_for_visuals */
370 switch (buf->format) {
371 case HAL_PIXEL_FORMAT_BGRA_8888:
372 format = __DRI_IMAGE_FORMAT_ARGB8888;
374 case HAL_PIXEL_FORMAT_RGB_565:
375 format = __DRI_IMAGE_FORMAT_RGB565;
377 case HAL_PIXEL_FORMAT_RGBA_8888:
378 format = __DRI_IMAGE_FORMAT_ABGR8888;
380 case HAL_PIXEL_FORMAT_RGBX_8888:
381 format = __DRI_IMAGE_FORMAT_XBGR8888;
383 case HAL_PIXEL_FORMAT_RGB_888:
384 case HAL_PIXEL_FORMAT_RGBA_5551:
385 case HAL_PIXEL_FORMAT_RGBA_4444:
388 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
393 dri2_img = calloc(1, sizeof(*dri2_img));
395 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
399 if (!_eglInitImage(&dri2_img->base, disp)) {
404 dri2_img->dri_image =
405 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
412 if (!dri2_img->dri_image) {
414 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
418 return &dri2_img->base;
422 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
423 _EGLContext *ctx, EGLenum target,
424 EGLClientBuffer buffer, const EGLint *attr_list)
427 case EGL_NATIVE_BUFFER_ANDROID:
428 return dri2_create_image_android_native_buffer(disp, ctx,
429 (struct ANativeWindowBuffer *) buffer);
431 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
436 droid_init_driver_functions(_EGLDriver *drv)
438 drv->API.CreateWindowSurface = droid_create_window_surface;
439 drv->API.CreatePixmapSurface = droid_create_pixmap_surface;
440 drv->API.CreatePbufferSurface = droid_create_pbuffer_surface;
441 drv->API.DestroySurface = droid_destroy_surface;
442 drv->API.SwapBuffers = droid_swap_buffers;
444 drv->API.CreateImageKHR = droid_create_image_khr;
448 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
453 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
454 unsigned int *attachments, int count)
456 int num_buffers = 0, i;
458 /* fill dri2_surf->buffers */
459 for (i = 0; i < count * 2; i += 2) {
460 __DRIbuffer *buf, *local;
462 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
463 buf = &dri2_surf->buffers[num_buffers];
465 switch (attachments[i]) {
466 case __DRI_BUFFER_BACK_LEFT:
467 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
468 buf->attachment = attachments[i];
469 buf->name = get_native_buffer_name(dri2_surf->buffer);
470 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
471 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
479 /* fall through for pbuffers */
480 case __DRI_BUFFER_DEPTH:
481 case __DRI_BUFFER_STENCIL:
482 case __DRI_BUFFER_ACCUM:
483 case __DRI_BUFFER_DEPTH_STENCIL:
484 case __DRI_BUFFER_HIZ:
485 local = droid_alloc_local_buffer(dri2_surf,
486 attachments[i], attachments[i + 1]);
493 case __DRI_BUFFER_FRONT_LEFT:
494 case __DRI_BUFFER_FRONT_RIGHT:
495 case __DRI_BUFFER_FAKE_FRONT_LEFT:
496 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
497 case __DRI_BUFFER_BACK_RIGHT:
499 /* no front or right buffers */
508 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
509 int *width, int *height,
510 unsigned int *attachments, int count,
511 int *out_count, void *loaderPrivate)
513 struct dri2_egl_surface *dri2_surf = loaderPrivate;
514 struct dri2_egl_display *dri2_dpy =
515 dri2_egl_display(dri2_surf->base.Resource.Display);
518 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
519 /* try to dequeue the next back buffer */
520 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
523 /* free outdated buffers and update the surface size */
524 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
525 dri2_surf->base.Height != dri2_surf->buffer->height) {
526 droid_free_local_buffers(dri2_surf);
527 dri2_surf->base.Width = dri2_surf->buffer->width;
528 dri2_surf->base.Height = dri2_surf->buffer->height;
532 dri2_surf->buffer_count =
533 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
536 *width = dri2_surf->base.Width;
538 *height = dri2_surf->base.Height;
540 *out_count = dri2_surf->buffer_count;;
542 return dri2_surf->buffers;
546 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
548 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
552 unsigned int rgba_masks[4];
554 { HAL_PIXEL_FORMAT_RGBA_8888, 32, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
555 { HAL_PIXEL_FORMAT_RGBX_8888, 32, { 0xff, 0xff00, 0xff0000, 0x0 } },
556 { HAL_PIXEL_FORMAT_RGB_888, 24, { 0xff, 0xff00, 0xff0000, 0x0 } },
557 { HAL_PIXEL_FORMAT_RGB_565, 16, { 0xf800, 0x7e0, 0x1f, 0x0 } },
558 { HAL_PIXEL_FORMAT_BGRA_8888, 32, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
559 { 0, 0, { 0, 0, 0, 0 } }
564 for (i = 0; visuals[i].format; i++) {
565 int format_count = 0;
567 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
568 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
569 struct dri2_egl_config *dri2_conf;
570 unsigned int double_buffered = 0;
572 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
573 __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
575 /* support only double buffered configs */
576 if (!double_buffered)
579 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
580 count + 1, visuals[i].size, surface_type, NULL,
581 visuals[i].rgba_masks);
583 dri2_conf->base.NativeVisualID = visuals[i].format;
584 dri2_conf->base.NativeVisualType = visuals[i].format;
591 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
596 /* post-process configs */
597 for (i = 0; i < dpy->Configs->Size; i++) {
598 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
600 /* there is no front buffer so no OpenGL */
601 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
602 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
609 droid_get_pci_id(int fd, int *vendor_id, int *chip_id)
611 drmVersionPtr version;
615 version = drmGetVersion(fd);
617 _eglLog(_EGL_WARNING, "invalid drm fd");
620 if (!version->name) {
621 _eglLog(_EGL_WARNING, "unable to determine the driver name");
622 drmFreeVersion(version);
626 if (strcmp(version->name, "i915") == 0) {
627 struct drm_i915_getparam gp;
632 memset(&gp, 0, sizeof(gp));
633 gp.param = I915_PARAM_CHIPSET_ID;
635 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
637 _eglLog(_EGL_WARNING, "failed to get param for i915");
641 else if (strcmp(version->name, "radeon") == 0) {
642 struct drm_radeon_info info;
647 memset(&info, 0, sizeof(info));
648 info.request = RADEON_INFO_DEVICE_ID;
649 info.value = (unsigned long) chip_id;
650 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
652 _eglLog(_EGL_WARNING, "failed to get info for radeon");
656 else if (strcmp(version->name, "nouveau") == 0) {
661 else if (strcmp(version->name, "vmwgfx") == 0) {
667 drmFreeVersion(version);
669 return (*chip_id >= 0);
672 #define DRIVER_MAP_DRI2_ONLY
673 #include "pci_ids/pci_id_driver_map.h"
675 droid_get_driver_name(int fd)
677 int vendor_id = -1, chip_id = -1;
681 if (!droid_get_pci_id(fd, &vendor_id, &chip_id))
684 for (idx = 0; driver_map[idx].driver; idx++) {
685 if (vendor_id != driver_map[idx].vendor_id)
688 if (driver_map[idx].num_chips_ids == -1)
691 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
692 if (driver_map[idx].chip_ids[i] == chip_id)
695 if (i < driver_map[idx].num_chips_ids)
699 _eglLog(_EGL_INFO, "pci id for fd %d: %04x:%04x, driver %s",
700 fd, vendor_id, chip_id, driver_map[idx].driver);
702 return driver_map[idx].driver;
706 droid_open_device(void)
708 const hw_module_t *mod;
711 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
713 const gralloc_module_t *gr = (gralloc_module_t *) mod;
717 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
720 _eglLog(_EGL_WARNING, "fail to get drm fd");
724 return (fd >= 0) ? dup(fd) : -1;
727 /* support versions < JellyBean */
739 droid_log(EGLint level, const char *msg)
752 LOG_FATAL("%s", msg);
760 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
762 struct dri2_egl_display *dri2_dpy;
765 _eglSetLogProc(droid_log);
767 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
769 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
771 dpy->DriverData = (void *) dri2_dpy;
773 dri2_dpy->fd = droid_open_device();
774 if (dri2_dpy->fd < 0) {
775 err = "DRI2: failed to open device";
776 goto cleanup_display;
779 dri2_dpy->driver_name = (char *) droid_get_driver_name(dri2_dpy->fd);
780 if (dri2_dpy->driver_name == NULL) {
781 err = "DRI2: failed to get driver name";
785 if (!dri2_load_driver(dpy)) {
786 err = "DRI2: failed to load driver";
790 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
791 dri2_dpy->dri2_loader_extension.base.version = 3;
792 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
793 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
794 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
795 droid_get_buffers_with_format;
797 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
798 dri2_dpy->extensions[1] = &image_lookup_extension.base;
799 dri2_dpy->extensions[2] = &use_invalidate.base;
800 dri2_dpy->extensions[3] = NULL;
802 if (!dri2_create_screen(dpy)) {
803 err = "DRI2: failed to create screen";
807 if (!droid_add_configs_for_visuals(drv, dpy)) {
808 err = "DRI2: failed to add configs";
812 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
813 dpy->Extensions.KHR_image_base = EGL_TRUE;
815 /* we're supporting EGL 1.4 */
816 dpy->VersionMajor = 1;
817 dpy->VersionMinor = 4;
819 droid_init_driver_functions(drv);
824 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
826 dlclose(dri2_dpy->driver);
832 return _eglError(EGL_NOT_INITIALIZED, err);