OSDN Git Service

minigbm: amdgpu: add supported formats for camera
[android-x86/external-minigbm.git] / helpers.c
index 0a09b5a..555fb85 100644 (file)
--- a/helpers.c
+++ b/helpers.c
@@ -127,6 +127,24 @@ uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
        return stride;
 }
 
+uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
+{
+       assert(plane < drv_num_planes_from_format(format));
+       uint32_t vertical_subsampling;
+
+       switch (format) {
+       case DRM_FORMAT_NV12:
+       case DRM_FORMAT_YVU420:
+       case DRM_FORMAT_YVU420_ANDROID:
+               vertical_subsampling = (plane == 0) ? 1 : 2;
+               break;
+       default:
+               vertical_subsampling = 1;
+       }
+
+       return stride * DIV_ROUND_UP(height, vertical_subsampling);
+}
+
 /*
  * This function fills in the buffer object given the driver aligned stride of
  * the first plane, height and a format. This function assumes there is just
@@ -164,7 +182,7 @@ int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height,
 }
 
 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
-                      uint32_t flags)
+                      uint64_t use_flags)
 {
        int ret;
        size_t plane;
@@ -291,7 +309,7 @@ int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
        return 0;
 }
 
-void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane, int prot)
+void *drv_dumb_bo_map(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags)
 {
        int ret;
        size_t i;
@@ -308,9 +326,50 @@ void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane, int pr
 
        for (i = 0; i < bo->num_planes; i++)
                if (bo->handles[i].u32 == bo->handles[plane].u32)
-                       data->length += bo->sizes[i];
+                       mapping->vma->length += bo->sizes[i];
 
-       return mmap(0, data->length, prot, MAP_SHARED, bo->drv->fd, map_dumb.offset);
+       return mmap(0, mapping->vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
+                   map_dumb.offset);
+}
+
+int drv_bo_munmap(struct bo *bo, struct mapping *mapping)
+{
+       return munmap(mapping->vma->addr, mapping->vma->length);
+}
+
+int drv_mapping_destroy(struct bo *bo)
+{
+       int ret;
+       void *ptr;
+       size_t plane;
+       struct mapping *mapping;
+
+       /*
+        * This function is called right before the buffer is destroyed. It will free any mappings
+        * associated with the buffer.
+        */
+
+       for (plane = 0; plane < bo->num_planes; plane++) {
+               if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
+                       mapping = (struct mapping *)ptr;
+                       ret = bo->drv->backend->bo_unmap(bo, mapping);
+                       if (ret) {
+                               fprintf(stderr, "drv: munmap failed");
+                               return ret;
+                       }
+
+                       drmHashDelete(bo->drv->map_table, mapping->vma->handle);
+                       free(mapping->vma);
+                       free(mapping);
+               }
+       }
+
+       return 0;
+}
+
+int drv_get_prot(uint32_t map_flags)
+{
+       return (BO_MAP_WRITE & map_flags) ? PROT_WRITE | PROT_READ : PROT_READ;
 }
 
 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, size_t plane)
@@ -354,7 +413,7 @@ uint32_t drv_log_base2(uint32_t value)
 }
 
 int drv_add_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
-                       uint64_t usage)
+                       uint64_t use_flags)
 {
        struct combinations *combos = &drv->combos;
        if (combos->size >= combos->allocations) {
@@ -371,18 +430,18 @@ int drv_add_combination(struct driver *drv, uint32_t format, struct format_metad
        combos->data[combos->size].metadata.priority = metadata->priority;
        combos->data[combos->size].metadata.tiling = metadata->tiling;
        combos->data[combos->size].metadata.modifier = metadata->modifier;
-       combos->data[combos->size].usage = usage;
+       combos->data[combos->size].use_flags = use_flags;
        combos->size++;
        return 0;
 }
 
 int drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
-                        struct format_metadata *metadata, uint64_t usage)
+                        struct format_metadata *metadata, uint64_t use_flags)
 {
        int ret;
        uint32_t i;
        for (i = 0; i < num_formats; i++) {
-               ret = drv_add_combination(drv, formats[i], metadata, usage);
+               ret = drv_add_combination(drv, formats[i], metadata, use_flags);
                if (ret)
                        return ret;
        }
@@ -391,23 +450,23 @@ int drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t n
 }
 
 void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
-                           uint64_t usage)
+                           uint64_t use_flags)
 {
        uint32_t i;
        struct combination *combo;
-       /* Attempts to add the specified usage to an existing combination. */
+       /* Attempts to add the specified flags to an existing combination. */
        for (i = 0; i < drv->combos.size; i++) {
                combo = &drv->combos.data[i];
                if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
                    combo->metadata.modifier == metadata->modifier)
-                       combo->usage |= usage;
+                       combo->use_flags |= use_flags;
        }
 }
 
 struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
 {
-       uint64_t flag, usage;
        struct kms_item *items;
+       uint64_t plane_type, use_flag;
        uint32_t i, j, k, allocations, item_size;
 
        drmModePlanePtr plane;
@@ -449,20 +508,20 @@ struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
                        prop = drmModeGetProperty(drv->fd, props->props[j]);
                        if (prop) {
                                if (strcmp(prop->name, "type") == 0) {
-                                       flag = props->prop_values[j];
+                                       plane_type = props->prop_values[j];
                                }
 
                                drmModeFreeProperty(prop);
                        }
                }
 
-               switch (flag) {
+               switch (plane_type) {
                case DRM_PLANE_TYPE_OVERLAY:
                case DRM_PLANE_TYPE_PRIMARY:
-                       usage = BO_USE_SCANOUT;
+                       use_flag = BO_USE_SCANOUT;
                        break;
                case DRM_PLANE_TYPE_CURSOR:
-                       usage = BO_USE_CURSOR;
+                       use_flag = BO_USE_CURSOR;
                        break;
                default:
                        assert(0);
@@ -472,8 +531,8 @@ struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
                        bool found = false;
                        for (k = 0; k < item_size; k++) {
                                if (items[k].format == plane->formats[j] &&
-                                   items[k].modifier == DRM_FORMAT_MOD_NONE) {
-                                       items[k].usage |= usage;
+                                   items[k].modifier == DRM_FORMAT_MOD_INVALID) {
+                                       items[k].use_flags |= use_flag;
                                        found = true;
                                        break;
                                }
@@ -493,8 +552,8 @@ struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
 
                        if (!found) {
                                items[item_size].format = plane->formats[j];
-                               items[item_size].modifier = DRM_FORMAT_MOD_NONE;
-                               items[item_size].usage = usage;
+                               items[item_size].modifier = DRM_FORMAT_MOD_INVALID;
+                               items[item_size].use_flags = use_flag;
                                item_size++;
                        }
                }
@@ -540,10 +599,30 @@ int drv_modify_linear_combinations(struct driver *drv)
                for (j = 0; j < drv->combos.size; j++) {
                        combo = &drv->combos.data[j];
                        if (items[i].format == combo->format)
-                               combo->usage |= BO_USE_SCANOUT;
+                               combo->use_flags |= BO_USE_SCANOUT;
                }
        }
 
        free(items);
        return 0;
 }
+
+/*
+ * Pick the best modifier from modifiers, according to the ordering
+ * given by modifier_order.
+ */
+uint64_t drv_pick_modifier(const uint64_t *modifiers, uint32_t count,
+                          const uint64_t *modifier_order, uint32_t order_count)
+{
+       uint32_t i, j;
+
+       for (i = 0; i < order_count; i++) {
+               for (j = 0; j < count; j++) {
+                       if (modifiers[j] == modifier_order[i]) {
+                               return modifiers[j];
+                       }
+               }
+       }
+
+       return DRM_FORMAT_MOD_LINEAR;
+}