OSDN Git Service

minigbm: amdgpu: Add RGB565 to supported render target formats
[android-x86/external-minigbm.git] / drv.c
diff --git a/drv.c b/drv.c
index fc8132a..3ccf16e 100644 (file)
--- a/drv.c
+++ b/drv.c
@@ -259,8 +259,12 @@ struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, ui
 
        pthread_mutex_lock(&drv->driver_lock);
 
-       for (plane = 0; plane < bo->num_planes; plane++)
+       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);
 
@@ -293,8 +297,12 @@ struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint
 
        pthread_mutex_lock(&drv->driver_lock);
 
-       for (plane = 0; plane < bo->num_planes; plane++)
+       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);
 
@@ -330,6 +338,7 @@ struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
        int ret;
        size_t plane;
        struct bo *bo;
+       off_t seek_end;
 
        bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
 
@@ -346,18 +355,32 @@ struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
                bo->strides[plane] = data->strides[plane];
                bo->offsets[plane] = data->offsets[plane];
                bo->format_modifiers[plane] = data->format_modifiers[plane];
-               if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0) {
-                       bo->sizes[plane] =
-                           lseek(data->fds[plane], 0, SEEK_END) - data->offsets[plane];
-                       lseek(data->fds[plane], 0, SEEK_SET);
-               } else {
+
+               seek_end = lseek(data->fds[plane], 0, SEEK_END);
+               if (seek_end == (off_t)(-1)) {
+                       fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
+                       goto destroy_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];
+
+               if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
+                       fprintf(stderr, "drv: buffer size is too large.\n");
+                       goto destroy_bo;
                }
 
                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,
@@ -367,25 +390,27 @@ void *drv_bo_map(struct bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t
        uint8_t *addr;
        size_t offset;
        struct map_info *data;
-       int prot;
 
        assert(width > 0);
        assert(height > 0);
        assert(x + width <= drv_bo_get_width(bo));
        assert(y + 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));
 
        pthread_mutex_lock(&bo->drv->driver_lock);
 
        if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
                data = (struct map_info *)ptr;
+               /* TODO(gsingh): support mapping same buffer with different flags. */
+               assert(data->map_flags == map_flags);
                data->refcount++;
                goto success;
        }
 
        data = calloc(1, sizeof(*data));
-       prot = BO_MAP_WRITE & map_flags ? PROT_WRITE | PROT_READ : PROT_READ;
-       addr = bo->drv->backend->bo_map(bo, data, plane, prot);
+       addr = bo->drv->backend->bo_map(bo, data, plane, map_flags);
        if (addr == MAP_FAILED) {
                *map_data = NULL;
                free(data);
@@ -396,9 +421,11 @@ void *drv_bo_map(struct bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t
        data->refcount = 1;
        data->addr = addr;
        data->handle = bo->handles[plane].u32;
+       data->map_flags = map_flags;
        drmHashInsert(bo->drv->map_table, bo->handles[plane].u32, (void *)data);
 
 success:
+       drv_bo_invalidate(bo, data);
        *map_data = data;
        offset = drv_bo_get_plane_stride(bo, plane) * y;
        offset += drv_stride_from_format(bo->format, x, plane);
@@ -428,11 +455,24 @@ int drv_bo_unmap(struct bo *bo, struct map_info *data)
        return ret;
 }
 
+int drv_bo_invalidate(struct bo *bo, struct map_info *data)
+{
+       int ret = 0;
+       assert(data);
+       assert(data->refcount >= 0);
+
+       if (bo->drv->backend->bo_invalidate)
+               ret = bo->drv->backend->bo_invalidate(bo, data);
+
+       return ret;
+}
+
 int drv_bo_flush(struct bo *bo, struct map_info *data)
 {
        int ret = 0;
        assert(data);
        assert(data->refcount >= 0);
+       assert(!(bo->use_flags & BO_USE_PROTECTED));
 
        if (bo->drv->backend->bo_flush)
                ret = bo->drv->backend->bo_flush(bo, data);