OSDN Git Service

minigbm: cros_gralloc: map protected flag to linear
[android-x86/external-minigbm.git] / helpers.c
index ad8a9f0..d8c2818 100644 (file)
--- a/helpers.c
+++ b/helpers.c
@@ -10,8 +10,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <sys/types.h>
+#include <unistd.h>
 #include <xf86drm.h>
-#include <xf86drmMode.h>
 
 #include "drv_priv.h"
 #include "helpers.h"
@@ -93,6 +94,9 @@ static const struct planar_layout *layout_from_format(uint32_t format)
        case DRM_FORMAT_RGB332:
                return &packed_1bpp_layout;
 
+       case DRM_FORMAT_R16:
+               return &packed_2bpp_layout;
+
        case DRM_FORMAT_YVU420:
        case DRM_FORMAT_YVU420_ANDROID:
                return &triplanar_yuv_420_layout;
@@ -177,6 +181,20 @@ size_t drv_num_planes_from_format(uint32_t format)
        return layout ? layout->num_planes : 0;
 }
 
+size_t drv_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
+{
+       size_t planes = drv_num_planes_from_format(format);
+
+       /* Disallow unsupported formats. */
+       if (!planes)
+               return 0;
+
+       if (drv->backend->num_planes_from_modifier && modifier != DRM_FORMAT_MOD_INVALID)
+               return drv->backend->num_planes_from_modifier(drv, format, modifier);
+
+       return planes;
+}
+
 uint32_t drv_height_from_format(uint32_t format, uint32_t height, size_t plane)
 {
        const struct planar_layout *layout = layout_from_format(format);
@@ -186,6 +204,15 @@ uint32_t drv_height_from_format(uint32_t format, uint32_t height, size_t plane)
        return DIV_ROUND_UP(height, layout->vertical_subsampling[plane]);
 }
 
+uint32_t drv_vertical_subsampling_from_format(uint32_t format, size_t plane)
+{
+       const struct planar_layout *layout = layout_from_format(format);
+
+       assert(plane < layout->num_planes);
+
+       return layout->vertical_subsampling[plane];
+}
+
 uint32_t drv_bytes_per_pixel_from_format(uint32_t format, size_t plane)
 {
        const struct planar_layout *layout = layout_from_format(format);
@@ -242,7 +269,13 @@ static uint32_t subsample_stride(uint32_t stride, uint32_t format, size_t plane)
  */
 int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
 {
+       uint32_t padding[DRV_MAX_PLANES] = { 0 };
+       return drv_bo_from_format_and_padding(bo, stride, aligned_height, format, padding);
+}
 
+int drv_bo_from_format_and_padding(struct bo *bo, uint32_t stride, uint32_t aligned_height,
+                                  uint32_t format, uint32_t padding[DRV_MAX_PLANES])
+{
        size_t p, num_planes;
        uint32_t offset = 0;
 
@@ -263,7 +296,8 @@ int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height,
        for (p = 0; p < num_planes; p++) {
                bo->meta.strides[p] = subsample_stride(stride, format, p);
                bo->meta.sizes[p] =
-                   drv_size_from_format(format, bo->meta.strides[p], aligned_height, p);
+                   drv_size_from_format(format, bo->meta.strides[p], aligned_height, p) +
+                   padding[p];
                bo->meta.offsets[p] = offset;
                offset += bo->meta.sizes[p];
        }
@@ -272,29 +306,38 @@ int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height,
        return 0;
 }
 
-int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
-                      uint64_t use_flags)
+int drv_dumb_bo_create_ex(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
+                         uint64_t use_flags, uint64_t quirks)
 {
        int ret;
        size_t plane;
        uint32_t aligned_width, aligned_height;
-       struct drm_mode_create_dumb create_dumb;
+       struct drm_mode_create_dumb create_dumb = { 0 };
 
        aligned_width = width;
        aligned_height = height;
        switch (format) {
+       case DRM_FORMAT_R16:
+               /* HAL_PIXEL_FORMAT_Y16 requires that the buffer's width be 16 pixel
+                * aligned. See hardware/interfaces/graphics/common/1.0/types.hal. */
+               aligned_width = ALIGN(width, 16);
+               break;
        case DRM_FORMAT_YVU420_ANDROID:
+               /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not
+                * be aligned. Update 'height' so that drv_bo_from_format below
+                * uses the non-aligned height. */
+               height = bo->meta.height;
+
                /* Align width to 32 pixels, so chroma strides are 16 bytes as
                 * Android requires. */
                aligned_width = ALIGN(width, 32);
-               /* Adjust the height to include room for chroma planes.
-                *
-                * HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not
-                * be aligned. */
-               aligned_height = 3 * DIV_ROUND_UP(bo->meta.height, 2);
+
+               /* Adjust the height to include room for chroma planes. */
+               aligned_height = 3 * DIV_ROUND_UP(height, 2);
                break;
        case DRM_FORMAT_YVU420:
        case DRM_FORMAT_NV12:
+       case DRM_FORMAT_NV21:
                /* Adjust the height to include room for chroma planes */
                aligned_height = 3 * DIV_ROUND_UP(height, 2);
                break;
@@ -302,10 +345,15 @@ int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t
                break;
        }
 
-       memset(&create_dumb, 0, sizeof(create_dumb));
-       create_dumb.height = aligned_height;
+       if (quirks & BO_QUIRK_DUMB32BPP) {
+               aligned_width =
+                   DIV_ROUND_UP(aligned_width * layout_from_format(format)->bytes_per_pixel[0], 4);
+               create_dumb.bpp = 32;
+       } else {
+               create_dumb.bpp = layout_from_format(format)->bytes_per_pixel[0] * 8;
+       }
        create_dumb.width = aligned_width;
-       create_dumb.bpp = layout_from_format(format)->bytes_per_pixel[0] * 8;
+       create_dumb.height = aligned_height;
        create_dumb.flags = 0;
 
        ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
@@ -323,14 +371,18 @@ int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t
        return 0;
 }
 
+int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
+                      uint64_t use_flags)
+{
+       return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_NONE);
+}
+
 int drv_dumb_bo_destroy(struct bo *bo)
 {
-       struct drm_mode_destroy_dumb destroy_dumb;
        int ret;
+       struct drm_mode_destroy_dumb destroy_dumb = { 0 };
 
-       memset(&destroy_dumb, 0, sizeof(destroy_dumb));
        destroy_dumb.handle = bo->handles[0].u32;
-
        ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
        if (ret) {
                drv_log("DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n", bo->handles[0].u32);
@@ -396,6 +448,7 @@ int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
 
                bo->handles[plane].u32 = prime_handle.handle;
        }
+       bo->meta.tiling = data->tiling;
 
        return 0;
 }
@@ -540,137 +593,16 @@ void drv_modify_combination(struct driver *drv, uint32_t format, struct format_m
        }
 }
 
-struct drv_array *drv_query_kms(struct driver *drv)
-{
-       struct drv_array *kms_items;
-       uint64_t plane_type = UINT64_MAX;
-       uint64_t use_flag;
-       uint32_t i, j, k;
-
-       drmModePlanePtr plane;
-       drmModePropertyPtr prop;
-       drmModePlaneResPtr resources;
-       drmModeObjectPropertiesPtr props;
-
-       kms_items = drv_array_init(sizeof(struct kms_item));
-       if (!kms_items)
-               goto out;
-
-       /*
-        * The ability to return universal planes is only complete on
-        * ChromeOS kernel versions >= v3.18.  The SET_CLIENT_CAP ioctl
-        * therefore might return an error code, so don't check it.  If it
-        * fails, it'll just return the plane list as overlay planes, which is
-        * fine in our case (our drivers already have cursor bits set).
-        * modetest in libdrm does the same thing.
-        */
-       drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
-
-       resources = drmModeGetPlaneResources(drv->fd);
-       if (!resources)
-               goto out;
-
-       for (i = 0; i < resources->count_planes; i++) {
-               plane_type = UINT64_MAX;
-               plane = drmModeGetPlane(drv->fd, resources->planes[i]);
-               if (!plane)
-                       goto out;
-
-               props = drmModeObjectGetProperties(drv->fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
-               if (!props)
-                       goto out;
-
-               for (j = 0; j < props->count_props; j++) {
-                       prop = drmModeGetProperty(drv->fd, props->props[j]);
-                       if (prop) {
-                               if (strcmp(prop->name, "type") == 0) {
-                                       plane_type = props->prop_values[j];
-                               }
-
-                               drmModeFreeProperty(prop);
-                       }
-               }
-
-               switch (plane_type) {
-               case DRM_PLANE_TYPE_OVERLAY:
-               case DRM_PLANE_TYPE_PRIMARY:
-                       use_flag = BO_USE_SCANOUT;
-                       break;
-               case DRM_PLANE_TYPE_CURSOR:
-                       use_flag = BO_USE_CURSOR;
-                       break;
-               default:
-                       assert(0);
-               }
-
-               for (j = 0; j < plane->count_formats; j++) {
-                       bool found = false;
-                       for (k = 0; k < drv_array_size(kms_items); k++) {
-                               struct kms_item *item = drv_array_at_idx(kms_items, k);
-                               if (item->format == plane->formats[j] &&
-                                   item->modifier == DRM_FORMAT_MOD_LINEAR) {
-                                       item->use_flags |= use_flag;
-                                       found = true;
-                                       break;
-                               }
-                       }
-
-                       if (!found) {
-                               struct kms_item item = { .format = plane->formats[j],
-                                                        .modifier = DRM_FORMAT_MOD_LINEAR,
-                                                        .use_flags = use_flag };
-
-                               drv_array_append(kms_items, &item);
-                       }
-               }
-
-               drmModeFreeObjectProperties(props);
-               drmModeFreePlane(plane);
-       }
-
-       drmModeFreePlaneResources(resources);
-out:
-       if (kms_items && !drv_array_size(kms_items)) {
-               drv_array_destroy(kms_items);
-               return NULL;
-       }
-
-       return kms_items;
-}
-
 int drv_modify_linear_combinations(struct driver *drv)
 {
-       uint32_t i, j;
-       struct kms_item *item;
-       struct combination *combo;
-       struct drv_array *kms_items;
-
        /*
         * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
-        * plane and as a cursor. Some drivers don't support
-        * drmModeGetPlaneResources, so add the combination here. Note that the
-        * kernel disregards the alpha component of ARGB unless it's an overlay
-        * plane.
+        * plane and as a cursor.
         */
        drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
                               BO_USE_CURSOR | BO_USE_SCANOUT);
        drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
                               BO_USE_CURSOR | BO_USE_SCANOUT);
-
-       kms_items = drv_query_kms(drv);
-       if (!kms_items)
-               return 0;
-
-       for (i = 0; i < drv_array_size(kms_items); i++) {
-               item = (struct kms_item *)drv_array_at_idx(kms_items, i);
-               for (j = 0; j < drv_array_size(drv->combos); j++) {
-                       combo = drv_array_at_idx(drv->combos, j);
-                       if (item->format == combo->format)
-                               combo->use_flags |= BO_USE_SCANOUT;
-               }
-       }
-
-       drv_array_destroy(kms_items);
        return 0;
 }