OSDN Git Service

Revert "Add support to query tiling mode."
[android-x86/external-minigbm.git] / helpers.c
index 951093c..6bfeb09 100644 (file)
--- a/helpers.c
+++ b/helpers.c
 
 #include "drv_priv.h"
 #include "helpers.h"
+#include "i915_private.h"
 #include "util.h"
 
-int drv_bpp_from_format(uint32_t format, size_t plane)
+static uint32_t subsample_stride(uint32_t stride, uint32_t format, size_t plane)
+{
+
+       if (plane != 0) {
+               switch (format) {
+               case DRM_FORMAT_YVU420:
+               case DRM_FORMAT_YVU420_ANDROID:
+                       stride = DIV_ROUND_UP(stride, 2);
+                       break;
+               }
+       }
+
+       return stride;
+}
+
+static uint32_t bpp_from_format(uint32_t format, size_t plane)
 {
        assert(plane < drv_num_planes_from_format(format));
 
@@ -31,25 +47,9 @@ int drv_bpp_from_format(uint32_t format, size_t plane)
        case DRM_FORMAT_YVU420_ANDROID:
                return 8;
 
-       /*
-        * NV12 is laid out as follows. Each letter represents a byte.
-        * Y plane:
-        * Y0_0, Y0_1, Y0_2, Y0_3, ..., Y0_N
-        * Y1_0, Y1_1, Y1_2, Y1_3, ..., Y1_N
-        * ...
-        * YM_0, YM_1, YM_2, YM_3, ..., YM_N
-        * CbCr plane:
-        * Cb01_01, Cr01_01, Cb01_23, Cr01_23, ..., Cb01_(N-1)N, Cr01_(N-1)N
-        * Cb23_01, Cr23_01, Cb23_23, Cr23_23, ..., Cb23_(N-1)N, Cr23_(N-1)N
-        * ...
-        * Cb(M-1)M_01, Cr(M-1)M_01, ..., Cb(M-1)M_(N-1)N, Cr(M-1)M_(N-1)N
-        *
-        * Pixel (0, 0) requires Y0_0, Cb01_01 and Cr01_01. Pixel (0, 1) requires
-        * Y0_1, Cb01_01 and Cr01_01.  So for a single pixel, 2 bytes of luma data
-        * are required.
-        */
        case DRM_FORMAT_NV12:
-               return (plane == 0) ? 8 : 16;
+       case DRM_FORMAT_NV21:
+               return (plane == 0) ? 8 : 4;
 
        case DRM_FORMAT_ABGR1555:
        case DRM_FORMAT_ABGR4444:
@@ -101,8 +101,13 @@ int drv_bpp_from_format(uint32_t format, size_t plane)
                return 32;
        }
 
-       fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
-       return 0;
+       return i915_private_bpp_from_format(format, plane);
+}
+
+uint32_t drv_bo_get_stride_in_pixels(struct bo *bo)
+{
+       uint32_t bytes_per_pixel = DIV_ROUND_UP(bpp_from_format(bo->format, 0), 8);
+       return DIV_ROUND_UP(bo->strides[0], bytes_per_pixel);
 }
 
 /*
@@ -110,41 +115,42 @@ int drv_bpp_from_format(uint32_t format, size_t plane)
  */
 uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
 {
-       uint32_t stride = DIV_ROUND_UP(width * drv_bpp_from_format(format, plane),
-                                      8);
-
-       /*
-        * Only downsample for certain multiplanar formats which have horizontal
-        * subsampling for chroma planes.  Only formats supported by our drivers
-        * are listed here -- add more as needed.
-        */
-       if (plane != 0) {
-               switch (format) {
-               case DRM_FORMAT_NV12:
-               case DRM_FORMAT_YVU420:
-               case DRM_FORMAT_YVU420_ANDROID:
-                       stride = DIV_ROUND_UP(stride, 2);
-                       break;
-               }
-       }
+       uint32_t stride = DIV_ROUND_UP(width * bpp_from_format(format, plane), 8);
 
        /*
         * The stride of Android YV12 buffers is required to be aligned to 16 bytes
         * (see <system/graphics.h>).
         */
        if (format == DRM_FORMAT_YVU420_ANDROID)
-               stride = ALIGN(stride, 16);
+               stride = (plane == 0) ? ALIGN(stride, 32) : ALIGN(stride, 16);
 
        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:
+               i915_private_vertical_subsampling_from_format(&vertical_subsampling, format, plane);
+       }
+
+       return stride * DIV_ROUND_UP(height, vertical_subsampling);
+}
+
 /*
- * This function fills in the buffer object given driver aligned dimensions
- * (in pixels) and a format. This function assumes there is just one kernel
- * buffer per buffer object.
+ * 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
+ * one kernel buffer per buffer object.
  */
-int drv_bo_from_format(struct bo *bo, uint32_t aligned_width,
-                      uint32_t aligned_height, uint32_t format)
+int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
 {
 
        size_t p, num_planes;
@@ -152,46 +158,55 @@ int drv_bo_from_format(struct bo *bo, uint32_t aligned_width,
 
        num_planes = drv_num_planes_from_format(format);
        assert(num_planes);
-       bo->total_size = 0;
+
+       /*
+        * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
+        *  - the aligned height is same as the buffer's height.
+        *  - the chroma stride is 16 bytes aligned, i.e., the luma's strides
+        *    is 32 bytes aligned.
+        */
+       if (format == DRM_FORMAT_YVU420_ANDROID) {
+               assert(aligned_height == bo->height);
+               assert(stride == ALIGN(stride, 32));
+       }
 
        for (p = 0; p < num_planes; p++) {
-               bo->strides[p] = drv_stride_from_format(format, aligned_width,
-                                                       p);
-               bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
-                                                   bo->height, p);
+               bo->strides[p] = subsample_stride(stride, format, p);
+               bo->sizes[p] = drv_size_from_format(format, bo->strides[p], aligned_height, p);
                bo->offsets[p] = offset;
                offset += bo->sizes[p];
-               bo->total_size += drv_size_from_format(format, bo->strides[p],
-                                                      aligned_height, p);
        }
 
+       bo->total_size = offset;
        return 0;
 }
 
-int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
-                      uint32_t format, uint32_t flags)
+int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
+                      uint64_t use_flags)
 {
        int ret;
        size_t plane;
-       uint32_t aligned_width, aligned_height, bytes_per_pixel;
+       uint32_t aligned_width, aligned_height;
        struct drm_mode_create_dumb create_dumb;
 
        aligned_width = width;
        aligned_height = height;
-       bytes_per_pixel = drv_bytes_per_pixel(format, 0);
        if (format == DRM_FORMAT_YVU420_ANDROID) {
                /*
-                * Align width to 16 pixels, so chroma strides are 16 bytes as
+                * Align width to 32 pixels, so chroma strides are 16 bytes as
                 * Android requires.
                 */
                aligned_width = ALIGN(width, 32);
+       }
+
+       if (format == DRM_FORMAT_YVU420_ANDROID || format == DRM_FORMAT_YVU420) {
                aligned_height = 3 * DIV_ROUND_UP(height, 2);
        }
 
        memset(&create_dumb, 0, sizeof(create_dumb));
        create_dumb.height = aligned_height;
        create_dumb.width = aligned_width;
-       create_dumb.bpp = drv_bpp_from_format(format, 0);
+       create_dumb.bpp = bpp_from_format(format, 0);
        create_dumb.flags = 0;
 
        ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
@@ -200,8 +215,7 @@ int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
                return ret;
        }
 
-       drv_bo_from_format(bo, DIV_ROUND_UP(create_dumb.pitch, bytes_per_pixel),
-                          height, format);
+       drv_bo_from_format(bo, create_dumb.pitch, height, format);
 
        for (plane = 0; plane < bo->num_planes; plane++)
                bo->handles[plane].u32 = create_dumb.handle;
@@ -220,8 +234,8 @@ int drv_dumb_bo_destroy(struct bo *bo)
 
        ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
        if (ret) {
-               fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
-                               "(handle=%x)\n", bo->handles[0].u32);
+               fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n",
+                       bo->handles[0].u32);
                return ret;
        }
 
@@ -247,9 +261,8 @@ int drv_gem_bo_destroy(struct bo *bo)
 
                ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
                if (ret) {
-                       fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
-                                       "(handle=%x) error %d\n",
-                                       bo->handles[plane].u32, ret);
+                       fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
+                               bo->handles[plane].u32, ret);
                        error = ret;
                }
        }
@@ -267,12 +280,11 @@ int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
                memset(&prime_handle, 0, sizeof(prime_handle));
                prime_handle.fd = data->fds[plane];
 
-               ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
-                              &prime_handle);
+               ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
 
                if (ret) {
-                       fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
-                               "failed (fd=%u)\n", prime_handle.fd);
+                       fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n",
+                               prime_handle.fd);
 
                        /*
                         * Need to call GEM close on planes that were opened,
@@ -288,16 +300,16 @@ int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
                bo->handles[plane].u32 = prime_handle.handle;
        }
 
+       ATOMIC_LOCK(&bo->drv->driver_lock);
        for (plane = 0; plane < bo->num_planes; plane++) {
-               pthread_mutex_lock(&bo->drv->driver_lock);
                drv_increment_reference_count(bo->drv, bo, plane);
-               pthread_mutex_unlock(&bo->drv->driver_lock);
        }
+       ATOMIC_UNLOCK(&bo->drv->driver_lock);
 
        return 0;
 }
 
-void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
+void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane, uint32_t map_flags)
 {
        int ret;
        size_t i;
@@ -316,43 +328,77 @@ void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
                if (bo->handles[i].u32 == bo->handles[plane].u32)
                        data->length += bo->sizes[i];
 
-       return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
-                   bo->drv->fd, map_dumb.offset);
+       return mmap(0, data->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
+                   map_dumb.offset);
 }
 
-uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
-                                 size_t plane)
+int drv_bo_munmap(struct bo *bo, struct map_info *data)
+{
+       return munmap(data->addr, data->length);
+}
+
+int drv_map_info_destroy(struct bo *bo)
+{
+       int ret;
+       void *ptr;
+       size_t plane;
+       struct map_info *data;
+
+       /*
+        * 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)) {
+                       data = (struct map_info *)ptr;
+                       ret = bo->drv->backend->bo_unmap(bo, data);
+                       if (ret) {
+                               fprintf(stderr, "drv: munmap failed");
+                               return ret;
+                       }
+
+                       drmHashDelete(bo->drv->map_table, data->handle);
+                       free(data);
+               }
+       }
+
+       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)
 {
        void *count;
        uintptr_t num = 0;
 
        if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
-               num = (uintptr_t) (count);
+               num = (uintptr_t)(count);
 
        return num;
 }
 
-void drv_increment_reference_count(struct driver *drv, struct bo *bo,
-                                  size_t plane)
+void drv_increment_reference_count(struct driver *drv, struct bo *bo, size_t plane)
 {
        uintptr_t num = drv_get_reference_count(drv, bo, plane);
 
        /* If a value isn't in the table, drmHashDelete is a no-op */
        drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
-       drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
-                     (void *) (num + 1));
+       drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
 }
 
-void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
-                                  size_t plane)
+void drv_decrement_reference_count(struct driver *drv, struct bo *bo, size_t plane)
 {
        uintptr_t num = drv_get_reference_count(drv, bo, plane);
 
        drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
 
        if (num > 0)
-               drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
-                             (void *) (num - 1));
+               drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num - 1));
 }
 
 uint32_t drv_log_base2(uint32_t value)
@@ -365,15 +411,14 @@ uint32_t drv_log_base2(uint32_t value)
        return ret;
 }
 
-int drv_add_combination(struct driver *drv, uint32_t format,
-                       struct format_metadata *metadata, uint64_t usage)
+int drv_add_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
+                       uint64_t use_flags)
 {
-       struct combinations *combos = &drv->backend->combos;
+       struct combinations *combos = &drv->combos;
        if (combos->size >= combos->allocations) {
                struct combination *new_data;
                combos->allocations *= 2;
-               new_data = realloc(combos->data, combos->allocations
-                                  * sizeof(*combos->data));
+               new_data = realloc(combos->data, combos->allocations * sizeof(*combos->data));
                if (!new_data)
                        return -ENOMEM;
 
@@ -384,19 +429,18 @@ int drv_add_combination(struct driver *drv, uint32_t format,
        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)
+int drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
+                        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;
        }
@@ -404,25 +448,24 @@ int drv_add_combinations(struct driver *drv, const uint32_t *formats,
        return 0;
 }
 
-void drv_modify_combination(struct driver *drv, uint32_t format,
-                           struct format_metadata *metadata, uint64_t usage)
+void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
+                           uint64_t use_flags)
 {
        uint32_t i;
        struct combination *combo;
-       /* Attempts to add the specified usage to an existing combination. */
-       for (i = 0; i < drv->backend->combos.size; i++) {
-               combo = &drv->backend->combos.data[i];
-               if (combo->format == format &&
-                   combo->metadata.tiling == metadata->tiling &&
+       /* 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;
@@ -456,8 +499,7 @@ struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
                if (!plane)
                        goto out;
 
-               props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
-                                                  DRM_MODE_OBJECT_PLANE);
+               props = drmModeObjectGetProperties(drv->fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
                if (!props)
                        goto out;
 
@@ -465,20 +507,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);
@@ -488,8 +530,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;
                                }
@@ -498,8 +540,7 @@ struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
                        if (!found && item_size >= allocations) {
                                struct kms_item *new_data = NULL;
                                allocations *= 2;
-                               new_data = realloc(items, allocations *
-                                                   sizeof(*items));
+                               new_data = realloc(items, allocations * sizeof(*items));
                                if (!new_data) {
                                        item_size = 0;
                                        goto out;
@@ -510,15 +551,14 @@ 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++;
                        }
                }
 
                drmModeFreeObjectProperties(props);
                drmModeFreePlane(plane);
-
        }
 
        drmModeFreePlaneResources(resources);
@@ -532,23 +572,12 @@ out:
        return items;
 }
 
-int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
-                               uint32_t num_formats)
+int drv_modify_linear_combinations(struct driver *drv)
 {
-       int ret;
        uint32_t i, j, num_items;
        struct kms_item *items;
        struct combination *combo;
-       struct format_metadata metadata;
-
-       metadata.tiling = 0;
-       metadata.priority = 1;
-       metadata.modifier = DRM_FORMAT_MOD_NONE;
 
-       ret = drv_add_combinations(drv, formats, num_formats, &metadata,
-                                  BO_COMMON_USE_MASK);
-       if (ret)
-               return ret;
        /*
         * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
         * plane and as a cursor. Some drivers don't support
@@ -556,9 +585,9 @@ int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
         * kernel disregards the alpha component of ARGB unless it's an overlay
         * plane.
         */
-       drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
+       drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
                               BO_USE_CURSOR | BO_USE_SCANOUT);
-       drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
+       drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
                               BO_USE_CURSOR | BO_USE_SCANOUT);
 
        items = drv_query_kms(drv, &num_items);
@@ -566,15 +595,33 @@ int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
                return 0;
 
        for (i = 0; i < num_items; i++) {
-               for (j = 0; j < drv->backend->combos.size; j++) {
-                       combo = &drv->backend->combos.data[j];
+               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;
+}