OSDN Git Service

Merge branch 'upstream-master'
[android-x86/external-minigbm.git] / drv.c
diff --git a/drv.c b/drv.c
index e2eb7a1..bc1f782 100644 (file)
--- a/drv.c
+++ b/drv.c
@@ -1,9 +1,10 @@
 /*
- * Copyright (c) 2016 The Chromium OS Authors. All rights reserved.
+ * Copyright 2016 The Chromium OS Authors. All rights reserved.
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file.
  */
 #include <assert.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <pthread.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <sys/types.h>
+#include <unistd.h>
 #include <xf86drm.h>
 
+#ifdef __ANDROID__
+#include <cutils/log.h>
+#include <libgen.h>
+#endif
+
 #include "drv_priv.h"
 #include "helpers.h"
 #include "util.h"
 
 #ifdef DRV_AMDGPU
-extern struct backend backend_amdgpu;
+extern const struct backend backend_amdgpu;
 #endif
-extern struct backend backend_cirrus;
-extern struct backend backend_evdi;
+extern const struct backend backend_evdi;
 #ifdef DRV_EXYNOS
-extern struct backend backend_exynos;
+extern const struct backend backend_exynos;
 #endif
-extern struct backend backend_gma500;
 #ifdef DRV_I915
-extern struct backend backend_i915;
+extern const struct backend backend_i915;
 #endif
 #ifdef DRV_MARVELL
-extern struct backend backend_marvell;
+extern const struct backend backend_marvell;
 #endif
 #ifdef DRV_MEDIATEK
-extern struct backend backend_mediatek;
+extern const struct backend backend_mediatek;
+#endif
+#ifdef DRV_MESON
+extern const struct backend backend_meson;
+#endif
+#ifdef DRV_MSM
+extern const struct backend backend_msm;
+#endif
+extern const struct backend backend_nouveau;
+#ifdef DRV_RADEON
+extern const struct backend backend_radeon;
 #endif
 #ifdef DRV_ROCKCHIP
-extern struct backend backend_rockchip;
+extern const struct backend backend_rockchip;
 #endif
 #ifdef DRV_TEGRA
-extern struct backend backend_tegra;
+extern const struct backend backend_tegra;
+#endif
+extern const struct backend backend_udl;
+#ifdef DRV_VC4
+extern const struct backend backend_vc4;
 #endif
-extern struct backend backend_udl;
-extern struct backend backend_vgem;
-extern struct backend backend_virtio_gpu;
+extern const struct backend backend_vgem;
+extern const struct backend backend_virtio_gpu;
 
-static struct backend *drv_get_backend(int fd)
+static const struct backend *drv_get_backend(int fd)
 {
        drmVersionPtr drm_version;
        unsigned int i;
@@ -55,16 +74,14 @@ static struct backend *drv_get_backend(int fd)
        if (!drm_version)
                return NULL;
 
-       struct backend *backend_list[] = {
+       const struct backend *backend_list[] = {
 #ifdef DRV_AMDGPU
                &backend_amdgpu,
 #endif
-               &backend_cirrus,
                &backend_evdi,
 #ifdef DRV_EXYNOS
                &backend_exynos,
 #endif
-               &backend_gma500,
 #ifdef DRV_I915
                &backend_i915,
 #endif
@@ -74,6 +91,16 @@ static struct backend *drv_get_backend(int fd)
 #ifdef DRV_MEDIATEK
                &backend_mediatek,
 #endif
+#ifdef DRV_MESON
+               &backend_meson,
+#endif
+#ifdef DRV_MSM
+               &backend_msm,
+#endif
+               &backend_nouveau,
+#ifdef DRV_RADEON
+               &backend_radeon,
+#endif
 #ifdef DRV_ROCKCHIP
                &backend_rockchip,
 #endif
@@ -81,11 +108,13 @@ static struct backend *drv_get_backend(int fd)
                &backend_tegra,
 #endif
                &backend_udl,
-               &backend_vgem,
-               &backend_virtio_gpu,
+#ifdef DRV_VC4
+               &backend_vc4,
+#endif
+               &backend_vgem,     &backend_virtio_gpu,
        };
 
-       for(i = 0; i < ARRAY_SIZE(backend_list); i++)
+       for (i = 0; i < ARRAY_SIZE(backend_list); i++)
                if (!strcmp(drm_version->name, backend_list[i]->name)) {
                        drmFreeVersion(drm_version);
                        return backend_list[i];
@@ -100,7 +129,7 @@ struct driver *drv_create(int fd)
        struct driver *drv;
        int ret;
 
-       drv = (struct driver *) calloc(1, sizeof(*drv));
+       drv = (struct driver *)calloc(1, sizeof(*drv));
 
        if (!drv)
                return NULL;
@@ -111,31 +140,37 @@ struct driver *drv_create(int fd)
        if (!drv->backend)
                goto free_driver;
 
-       if (pthread_mutex_init(&drv->table_lock, NULL))
+       if (pthread_mutex_init(&drv->driver_lock, NULL))
                goto free_driver;
 
        drv->buffer_table = drmHashCreate();
        if (!drv->buffer_table)
                goto free_lock;
 
-       drv->map_table = drmHashCreate();
-       if (!drv->map_table)
+       drv->mappings = drv_array_init(sizeof(struct mapping));
+       if (!drv->mappings)
                goto free_buffer_table;
 
+       drv->combos = drv_array_init(sizeof(struct combination));
+       if (!drv->combos)
+               goto free_mappings;
+
        if (drv->backend->init) {
                ret = drv->backend->init(drv);
-               if (ret)
-                       goto free_map_table;
+               if (ret) {
+                       drv_array_destroy(drv->combos);
+                       goto free_mappings;
+               }
        }
 
        return drv;
 
-free_map_table:
-       drmHashDestroy(drv->map_table);
+free_mappings:
+       drv_array_destroy(drv->mappings);
 free_buffer_table:
        drmHashDestroy(drv->buffer_table);
 free_lock:
-       pthread_mutex_destroy(&drv->table_lock);
+       pthread_mutex_destroy(&drv->driver_lock);
 free_driver:
        free(drv);
        return NULL;
@@ -143,12 +178,17 @@ free_driver:
 
 void drv_destroy(struct driver *drv)
 {
+       pthread_mutex_lock(&drv->driver_lock);
+
        if (drv->backend->close)
                drv->backend->close(drv);
 
-       pthread_mutex_destroy(&drv->table_lock);
        drmHashDestroy(drv->buffer_table);
-       drmHashDestroy(drv->map_table);
+       drv_array_destroy(drv->mappings);
+       drv_array_destroy(drv->combos);
+
+       pthread_mutex_unlock(&drv->driver_lock);
+       pthread_mutex_destroy(&drv->driver_lock);
 
        free(drv);
 }
@@ -158,39 +198,36 @@ int drv_get_fd(struct driver *drv)
        return drv->fd;
 }
 
-const char *
-drv_get_name(struct driver *drv)
+const char *drv_get_name(struct driver *drv)
 {
        return drv->backend->name;
 }
 
-int drv_is_format_supported(struct driver *drv, drv_format_t format,
-                           uint64_t usage)
+struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
 {
-       unsigned int i;
+       struct combination *curr, *best;
 
-       if (format == DRV_FORMAT_NONE || usage == DRV_BO_USE_NONE)
+       if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
                return 0;
 
-       for (i = 0 ; i < ARRAY_SIZE(drv->backend->format_list); i++)
-       {
-               if (!drv->backend->format_list[i].format)
-                       break;
-
-               if (drv->backend->format_list[i].format == format &&
-                     (drv->backend->format_list[i].usage & usage) == usage)
-                       return 1;
+       best = NULL;
+       uint32_t i;
+       for (i = 0; i < drv_array_size(drv->combos); i++) {
+               curr = drv_array_at_idx(drv->combos, i);
+               if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
+                       if (!best || best->metadata.priority < curr->metadata.priority)
+                               best = curr;
        }
 
-       return 0;
+       return best;
 }
 
-struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height,
-                     drv_format_t format)
+struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
+                     uint64_t use_flags)
 {
 
        struct bo *bo;
-       bo = (struct bo *) calloc(1, sizeof(*bo));
+       bo = (struct bo *)calloc(1, sizeof(*bo));
 
        if (!bo)
                return NULL;
@@ -199,6 +236,7 @@ struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height,
        bo->width = width;
        bo->height = height;
        bo->format = format;
+       bo->use_flags = use_flags;
        bo->num_planes = drv_num_planes_from_format(format);
 
        if (!bo->num_planes) {
@@ -209,31 +247,73 @@ struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height,
        return bo;
 }
 
-struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height,
-                        drv_format_t format, uint64_t flags)
+struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
+                        uint64_t use_flags)
 {
        int ret;
        size_t plane;
        struct bo *bo;
 
-       bo = drv_bo_new(drv, width, height, format);
+       bo = drv_bo_new(drv, width, height, format, use_flags);
 
        if (!bo)
                return NULL;
 
-       ret = drv->backend->bo_create(bo, width, height, format, flags);
+       ret = drv->backend->bo_create(bo, width, height, format, use_flags);
 
        if (ret) {
                free(bo);
                return NULL;
        }
 
-       pthread_mutex_lock(&drv->table_lock);
+       pthread_mutex_lock(&drv->driver_lock);
+
+       for (plane = 0; plane < bo->num_planes; plane++) {
+               if (plane > 0)
+                       assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
 
-       for (plane = 0; plane < bo->num_planes; plane++)
                drv_increment_reference_count(drv, bo, plane);
+       }
 
-       pthread_mutex_unlock(&drv->table_lock);
+       pthread_mutex_unlock(&drv->driver_lock);
+
+       return bo;
+}
+
+struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
+                                       uint32_t format, const uint64_t *modifiers, uint32_t count)
+{
+       int ret;
+       size_t plane;
+       struct bo *bo;
+
+       if (!drv->backend->bo_create_with_modifiers) {
+               errno = ENOENT;
+               return NULL;
+       }
+
+       bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
+
+       if (!bo)
+               return NULL;
+
+       ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
+
+       if (ret) {
+               free(bo);
+               return NULL;
+       }
+
+       pthread_mutex_lock(&drv->driver_lock);
+
+       for (plane = 0; plane < bo->num_planes; plane++) {
+               if (plane > 0)
+                       assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
+
+               drv_increment_reference_count(drv, bo, plane);
+       }
+
+       pthread_mutex_unlock(&drv->driver_lock);
 
        return bo;
 }
@@ -244,7 +324,7 @@ void drv_bo_destroy(struct bo *bo)
        uintptr_t total = 0;
        struct driver *drv = bo->drv;
 
-       pthread_mutex_lock(&drv->table_lock);
+       pthread_mutex_lock(&drv->driver_lock);
 
        for (plane = 0; plane < bo->num_planes; plane++)
                drv_decrement_reference_count(drv, bo, plane);
@@ -252,10 +332,12 @@ void drv_bo_destroy(struct bo *bo)
        for (plane = 0; plane < bo->num_planes; plane++)
                total += drv_get_reference_count(drv, bo, plane);
 
-       pthread_mutex_unlock(&drv->table_lock);
+       pthread_mutex_unlock(&drv->driver_lock);
 
-       if (total == 0)
+       if (total == 0) {
+               assert(drv_mapping_destroy(bo) == 0);
                bo->drv->backend->bo_destroy(bo);
+       }
 
        free(bo);
 }
@@ -265,113 +347,179 @@ struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
        int ret;
        size_t plane;
        struct bo *bo;
-       struct drm_prime_handle prime_handle;
+       off_t seek_end;
 
-       bo = drv_bo_new(drv, data->width, data->height, data->format);
+       bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
 
        if (!bo)
                return NULL;
 
-       for (plane = 0; plane < bo->num_planes; plane++) {
-
-               memset(&prime_handle, 0, sizeof(prime_handle));
-               prime_handle.fd = data->fds[plane];
+       ret = drv->backend->bo_import(bo, data);
+       if (ret) {
+               free(bo);
+               return NULL;
+       }
 
-               ret = drmIoctl(drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
-                              &prime_handle);
+       for (plane = 0; plane < bo->num_planes; plane++) {
+               bo->strides[plane] = data->strides[plane];
+               bo->offsets[plane] = data->offsets[plane];
+               bo->format_modifiers[plane] = data->format_modifiers[plane];
 
-               if (ret) {
-                       fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed "
-                               "(fd=%u)\n", prime_handle.fd);
+               seek_end = lseek(data->fds[plane], 0, SEEK_END);
+               if (seek_end == (off_t)(-1)) {
+                       drv_log("lseek() failed with %s\n", strerror(errno));
+                       goto destroy_bo;
+               }
 
-                       if (plane > 0) {
-                               bo->num_planes = plane;
-                               drv_bo_destroy(bo);
-                       } else {
-                               free(bo);
-                       }
+               lseek(data->fds[plane], 0, SEEK_SET);
+               if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
+                       bo->sizes[plane] = seek_end - data->offsets[plane];
+               else
+                       bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
 
-                       return NULL;
+               if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
+                       drv_log("buffer size is too large.\n");
+                       goto destroy_bo;
                }
 
-               bo->handles[plane].u32 = prime_handle.handle;
-               bo->strides[plane] = data->strides[plane];
-               bo->offsets[plane] = data->offsets[plane];
-               bo->sizes[plane] = data->sizes[plane];
-               bo->total_size += data->sizes[plane];
-
-               pthread_mutex_lock(&drv->table_lock);
-               drv_increment_reference_count(drv, bo, plane);
-               pthread_mutex_unlock(&drv->table_lock);
+               bo->total_size += bo->sizes[plane];
        }
 
        return bo;
+
+destroy_bo:
+       drv_bo_destroy(bo);
+       return NULL;
 }
 
-void *drv_bo_map(struct bo *bo, uint32_t x, uint32_t y, uint32_t width,
-                uint32_t height, uint32_t flags, void **map_data, size_t plane)
+void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
+                struct mapping **map_data, size_t plane)
 {
-       void *ptr;
+       uint32_t i;
        uint8_t *addr;
-       size_t offset;
-       struct map_info *data;
-
-       assert(width > 0);
-       assert(height > 0);
-       assert(x + width <= drv_bo_get_width(bo));
-       assert(y + height <= drv_bo_get_height(bo));
+       struct mapping mapping;
+
+       assert(rect->width >= 0);
+       assert(rect->height >= 0);
+       assert(rect->x + rect->width <= drv_bo_get_width(bo));
+       assert(rect->y + rect->height <= drv_bo_get_height(bo));
+       assert(BO_MAP_READ_WRITE & map_flags);
+       /* No CPU access for protected buffers. */
+       assert(!(bo->use_flags & BO_USE_PROTECTED));
+
+       memset(&mapping, 0, sizeof(mapping));
+       mapping.rect = *rect;
+       mapping.refcount = 1;
+
+       pthread_mutex_lock(&bo->drv->driver_lock);
+
+       for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
+               struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
+               if (prior->vma->handle != bo->handles[plane].u32 ||
+                   prior->vma->map_flags != map_flags)
+                       continue;
+
+               if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
+                   rect->width != prior->rect.width || rect->height != prior->rect.height)
+                       continue;
+
+               prior->refcount++;
+               *map_data = prior;
+               goto exact_match;
+       }
 
-       pthread_mutex_lock(&bo->drv->table_lock);
+       for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
+               struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
+               if (prior->vma->handle != bo->handles[plane].u32 ||
+                   prior->vma->map_flags != map_flags)
+                       continue;
 
-       if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
-               data = (struct map_info *) ptr;
-               data->refcount++;
+               prior->vma->refcount++;
+               mapping.vma = prior->vma;
                goto success;
        }
 
-       data = calloc(1, sizeof(*data));
-       addr = bo->drv->backend->bo_map(bo, data, plane);
+       mapping.vma = calloc(1, sizeof(*mapping.vma));
+       memcpy(mapping.vma->map_strides, bo->strides, sizeof(mapping.vma->map_strides));
+       addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
        if (addr == MAP_FAILED) {
                *map_data = NULL;
-               free(data);
-               pthread_mutex_unlock(&bo->drv->table_lock);
+               free(mapping.vma);
+               pthread_mutex_unlock(&bo->drv->driver_lock);
                return MAP_FAILED;
        }
 
-       data->refcount = 1;
-       data->addr = addr;
-       data->handle = bo->handles[plane].u32;
-       drmHashInsert(bo->drv->buffer_table, bo->handles[plane].u32,
-                     (void *) data);
+       mapping.vma->refcount = 1;
+       mapping.vma->addr = addr;
+       mapping.vma->handle = bo->handles[plane].u32;
+       mapping.vma->map_flags = map_flags;
 
 success:
-       *map_data = (void *) data;
-       offset = drv_bo_get_plane_stride(bo, plane) * y;
-       offset += drv_stride_from_format(bo->format, x, plane);
-       addr = (uint8_t *) data->addr;
-       addr += drv_bo_get_plane_offset(bo, plane) + offset;
-       pthread_mutex_unlock(&bo->drv->table_lock);
-
-       return (void *) addr;
+       *map_data = drv_array_append(bo->drv->mappings, &mapping);
+exact_match:
+       drv_bo_invalidate(bo, *map_data);
+       addr = (uint8_t *)((*map_data)->vma->addr);
+       addr += drv_bo_get_plane_offset(bo, plane);
+       pthread_mutex_unlock(&bo->drv->driver_lock);
+       return (void *)addr;
 }
 
-int drv_bo_unmap(struct bo *bo, void *map_data)
+int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
 {
-       struct map_info *data = map_data;
+       uint32_t i;
        int ret = 0;
 
-       assert(data);
-       assert(data->refcount >= 0);
+       pthread_mutex_lock(&bo->drv->driver_lock);
 
-       pthread_mutex_lock(&bo->drv->table_lock);
+       if (--mapping->refcount)
+               goto out;
 
-       if (!--data->refcount) {
-               ret = munmap(data->addr, data->length);
-               drmHashDelete(bo->drv->map_table, data->handle);
-               free(data);
+       if (!--mapping->vma->refcount) {
+               ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
+               free(mapping->vma);
        }
 
-       pthread_mutex_unlock(&bo->drv->table_lock);
+       for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
+               if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
+                       drv_array_remove(bo->drv->mappings, i);
+                       break;
+               }
+       }
+
+out:
+       pthread_mutex_unlock(&bo->drv->driver_lock);
+       return ret;
+}
+
+int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
+{
+       int ret = 0;
+
+       assert(mapping);
+       assert(mapping->vma);
+       assert(mapping->refcount > 0);
+       assert(mapping->vma->refcount > 0);
+
+       if (bo->drv->backend->bo_invalidate)
+               ret = bo->drv->backend->bo_invalidate(bo, mapping);
+
+       return ret;
+}
+
+int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
+{
+       int ret = 0;
+
+       assert(mapping);
+       assert(mapping->vma);
+       assert(mapping->refcount > 0);
+       assert(mapping->vma->refcount > 0);
+       assert(!(bo->use_flags & BO_USE_PROTECTED));
+
+       if (bo->drv->backend->bo_flush)
+               ret = bo->drv->backend->bo_flush(bo, mapping);
+       else
+               ret = drv_bo_unmap(bo, mapping);
 
        return ret;
 }
@@ -411,11 +559,13 @@ int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
        int ret, fd;
        assert(plane < bo->num_planes);
 
-       ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32,
-                                DRM_CLOEXEC | DRM_RDWR, &fd);
+       ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
 
-       return (ret) ? ret : fd;
+       // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
+       if (ret)
+               ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
 
+       return (ret) ? ret : fd;
 }
 
 uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
@@ -438,47 +588,23 @@ uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
 
 uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
 {
-        assert(plane < bo->num_planes);
+       assert(plane < bo->num_planes);
        return bo->format_modifiers[plane];
 }
 
-drv_format_t drv_bo_get_format(struct bo *bo)
+uint32_t drv_bo_get_format(struct bo *bo)
 {
        return bo->format;
 }
 
-drv_format_t drv_resolve_format(struct driver *drv, drv_format_t format)
+uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
 {
        if (drv->backend->resolve_format)
-               return drv->backend->resolve_format(format);
+               return drv->backend->resolve_format(format, use_flags);
 
        return format;
 }
 
-/*
- * This function returns the stride for a given format, width and plane.
- */
-int drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
-{
-       /* Get stride of the first plane */
-       int stride = width * DIV_ROUND_UP(drv_bpp_from_format(format, 0), 8);
-
-       /*
-        * Only downsample for certain multiplanar formats which are not
-        * interleaved and have horizontal subsampling.  Only formats supported
-        * by our drivers are listed here -- add more as needed.
-        */
-       if (plane != 0) {
-               switch (format) {
-               case DRV_FORMAT_YVU420:
-                       stride = stride / 2;
-                       break;
-               }
-       }
-
-       return stride;
-}
-
 uint32_t drv_num_buffers_per_bo(struct bo *bo)
 {
        uint32_t count = 0;
@@ -494,3 +620,19 @@ uint32_t drv_num_buffers_per_bo(struct bo *bo)
 
        return count;
 }
+
+void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
+{
+       char buf[50];
+       snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
+
+       va_list args;
+       va_start(args, format);
+#ifdef __ANDROID__
+       __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
+#else
+       fprintf(stderr, "%s ", buf);
+       vfprintf(stderr, format, args);
+#endif
+       va_end(args);
+}