-struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
-{
- uint64_t flag, usage;
- struct kms_item *items;
- uint32_t i, j, k, allocations, item_size;
-
- drmModePlanePtr plane;
- drmModePropertyPtr prop;
- drmModePlaneResPtr resources;
- drmModeObjectPropertiesPtr props;
-
- /* 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
- * 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 = 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) {
- flag = props->prop_values[j];
- }
-
- drmModeFreeProperty(prop);
- }
- }
-
- switch (flag) {
- case DRM_PLANE_TYPE_OVERLAY:
- case DRM_PLANE_TYPE_PRIMARY:
- usage = BO_USE_SCANOUT;
- break;
- case DRM_PLANE_TYPE_CURSOR:
- usage = BO_USE_CURSOR;
- break;
- default:
- assert(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);
- }
-
- drmModeFreePlaneResources(resources);
-out:
- if (items && item_size == 0) {
- free(items);
- items = NULL;
- }
-
- *num_items = item_size;
- return items;
-}
-