OSDN Git Service

minigbm: fix renderscipt allocation
[android-x86/external-minigbm.git] / helpers.c
index 75c982f..081eb61 100644 (file)
--- a/helpers.c
+++ b/helpers.c
@@ -1,10 +1,11 @@
 /*
- * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+ * Copyright 2014 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 <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -153,6 +154,8 @@ int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
                return ret;
        }
 
+       bo->width = width;
+       bo->height = height;
        bo->handles[0].u32 = create_dumb.handle;
        bo->offsets[0] = 0;
        bo->total_size = bo->sizes[0] = create_dumb.size;
@@ -208,6 +211,46 @@ int drv_gem_bo_destroy(struct bo *bo)
        return error;
 }
 
+int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
+{
+       int ret;
+       size_t plane;
+       struct drm_prime_handle prime_handle;
+
+       for (plane = 0; plane < bo->num_planes; plane++) {
+               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);
+
+               if (ret) {
+                       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,
+                        * if any. Adjust the num_planes variable to be the
+                        * plane that failed, so GEM close will be called on
+                        * planes before that plane.
+                        */
+                       bo->num_planes = plane;
+                       drv_gem_bo_destroy(bo);
+                       return ret;
+               }
+
+               bo->handles[plane].u32 = prime_handle.handle;
+       }
+
+       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);
+       }
+
+       return 0;
+}
+
 void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
 {
        int ret;
@@ -276,81 +319,77 @@ uint32_t drv_log_base2(uint32_t value)
        return ret;
 }
 
-/* Inserts a combination into list -- caller should have lock on driver. */
-void drv_insert_supported_combination(struct driver *drv, uint32_t format,
-                                     uint64_t usage, uint64_t modifier)
+int drv_add_combination(struct driver *drv, uint32_t format,
+                       struct format_metadata *metadata, uint64_t usage)
 {
-       struct combination_list_element *elem;
+       struct combinations *combos = &drv->backend->combos;
+       if (combos->size >= combos->allocations) {
+               struct combination *new_data;
+               combos->allocations *= 2;
+               new_data = realloc(combos->data, combos->allocations
+                                  * sizeof(*combos->data));
+               if (!new_data)
+                       return -ENOMEM;
+
+               combos->data = new_data;
+       }
 
-       elem = calloc(1, sizeof(*elem));
-       elem->combination.format = format;
-       elem->combination.modifier = modifier;
-       elem->combination.usage = usage;
-       LIST_ADD(&elem->link, &drv->backend->combinations);
+       combos->data[combos->size].format = 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->size++;
+       return 0;
 }
 
-void drv_insert_combinations(struct driver *drv, struct supported_combination *combos,
-                            uint32_t size)
+int drv_add_combinations(struct driver *drv, const uint32_t *formats,
+                        uint32_t num_formats, struct format_metadata *metadata,
+                        uint64_t usage)
 {
-       unsigned int i;
-
-       pthread_mutex_lock(&drv->driver_lock);
-
-       for (i = 0; i < size; i++)
-               drv_insert_supported_combination(drv, combos[i].format,
-                                                combos[i].usage,
-                                                combos[i].modifier);
+       int ret;
+       uint32_t i;
+       for (i = 0; i < num_formats; i++) {
+               ret = drv_add_combination(drv, formats[i], metadata, usage);
+               if (ret)
+                       return ret;
+       }
 
-       pthread_mutex_unlock(&drv->driver_lock);
+       return 0;
 }
 
-void drv_modify_supported_combination(struct driver *drv, uint32_t format,
-                                     uint64_t usage, uint64_t modifier)
+void drv_modify_combination(struct driver *drv, uint32_t format,
+                           struct format_metadata *metadata, uint64_t usage)
 {
-       /*
-        * Attempts to add the specified usage to an existing {format, modifier}
-        * pair. If the pair is not present, a new combination is created.
-        */
-       int found = 0;
-
-       pthread_mutex_lock(&drv->driver_lock);
-
-       list_for_each_entry(struct combination_list_element,
-                           elem, &drv->backend->combinations, link) {
-               if (elem->combination.format == format &&
-                   elem->combination.modifier == modifier) {
-                       elem->combination.usage |= usage;
-                       found = 1;
-               }
+       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 &&
+                   combo->metadata.modifier == metadata->modifier)
+                       combo->usage |= usage;
        }
-
-
-       if (!found)
-               drv_insert_supported_combination(drv, format, usage, modifier);
-
-       pthread_mutex_unlock(&drv->driver_lock);
 }
 
-int drv_add_kms_flags(struct driver *drv)
+struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
 {
-       int ret;
-       uint32_t i, j;
        uint64_t flag, usage;
+       struct kms_item *items;
+       uint32_t i, j, k, allocations, item_size;
+
        drmModePlanePtr plane;
        drmModePropertyPtr prop;
        drmModePlaneResPtr resources;
        drmModeObjectPropertiesPtr props;
 
-       /*
-        * All current drivers can scanout XRGB8888/ARGB8888 as a primary plane.
-        * Some older kernel versions can only return overlay planes, so add the
-        * combination here. Note that the kernel disregards the alpha component
-        * of ARGB unless it's an overlay plane.
-        */
-       drv_modify_supported_combination(drv, DRM_FORMAT_XRGB8888,
-                                        DRV_BO_USE_SCANOUT, 0);
-       drv_modify_supported_combination(drv, DRM_FORMAT_ARGB8888,
-                                        DRV_BO_USE_SCANOUT, 0);
+       /* Start with a power of 2 number of allocations. */
+       allocations = 2;
+       item_size = 0;
+       items = calloc(allocations, sizeof(*items));
+       if (!items)
+               goto out;
 
        /*
         * The ability to return universal planes is only complete on
@@ -364,27 +403,25 @@ int drv_add_kms_flags(struct driver *drv)
 
        resources = drmModeGetPlaneResources(drv->fd);
        if (!resources)
-               goto err;
+               goto out;
 
        for (i = 0; i < resources->count_planes; i++) {
-
                plane = drmModeGetPlane(drv->fd, resources->planes[i]);
-
                if (!plane)
-                       goto err;
+                       goto out;
 
                props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
                                                   DRM_MODE_OBJECT_PLANE);
                if (!props)
-                       goto err;
+                       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) {
                                        flag = props->prop_values[j];
                                }
+
                                drmModeFreeProperty(prop);
                        }
                }
@@ -392,18 +429,46 @@ int drv_add_kms_flags(struct driver *drv)
                switch (flag) {
                case DRM_PLANE_TYPE_OVERLAY:
                case DRM_PLANE_TYPE_PRIMARY:
-                       usage = DRV_BO_USE_SCANOUT;
+                       usage = BO_USE_SCANOUT;
                        break;
                case DRM_PLANE_TYPE_CURSOR:
-                       usage = DRV_BO_USE_CURSOR;
+                       usage = BO_USE_CURSOR;
                        break;
                default:
                        assert(0);
                }
 
-               for (j = 0; j < plane->count_formats; j++)
-                       drv_modify_supported_combination(drv, plane->formats[j],
-                                                        usage, 0);
+               for (j = 0; j < plane->count_formats; j++) {
+                       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;
+                                       found = true;
+                                       break;
+                               }
+                       }
+
+                       if (!found && item_size >= allocations) {
+                               struct kms_item *new_data = NULL;
+                               allocations *= 2;
+                               new_data = realloc(items, allocations *
+                                                   sizeof(*items));
+                               if (!new_data) {
+                                       item_size = 0;
+                                       goto out;
+                               }
+
+                               items = new_data;
+                       }
+
+                       if (!found) {
+                               items[item_size].format = plane->formats[j];
+                               items[item_size].modifier = DRM_FORMAT_MOD_NONE;
+                               items[item_size].usage = usage;
+                               item_size++;
+                       }
+               }
 
                drmModeFreeObjectProperties(props);
                drmModeFreePlane(plane);
@@ -411,9 +476,59 @@ int drv_add_kms_flags(struct driver *drv)
        }
 
        drmModeFreePlaneResources(resources);
-       return 0;
+out:
+       if (items && item_size == 0) {
+               free(items);
+               items = NULL;
+       }
 
-err:
-       ret = -1;
-       return ret;
+       *num_items = item_size;
+       return items;
+}
+
+int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
+                               uint32_t num_formats)
+{
+       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
+        * drmModeGetPlaneResources, so add the combination here. Note that the
+        * kernel disregards the alpha component of ARGB unless it's an overlay
+        * plane.
+        */
+       drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
+                              BO_USE_CURSOR | BO_USE_SCANOUT);
+       drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
+                              BO_USE_CURSOR | BO_USE_SCANOUT);
+
+       items = drv_query_kms(drv, &num_items);
+       if (!items || !num_items)
+               return 0;
+
+       for (i = 0; i < num_items; i++) {
+               for (j = 0; j < drv->backend->combos.size; j++) {
+                       combo = &drv->backend->combos.data[j];
+                       if (items[i].format == combo->format)
+                               combo->usage |= BO_USE_SCANOUT;
+
+
+               }
+       }
+
+       free(items);
+       return 0;
 }