*/
#include <assert.h>
+#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "helpers.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));
case DRM_FORMAT_R8:
case DRM_FORMAT_RGB332:
case DRM_FORMAT_YVU420:
+ 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:
return 32;
}
- fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
+ drv_log("UNKNOWN FORMAT %d\n", format);
return 0;
}
+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);
+}
+
/*
- * This function fills in the buffer object given driver aligned dimensions
- * and a format. This function assumes there is just one kernel buffer per
- * buffer object.
+ * This function returns the stride for a given format, width and plane.
*/
-int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
- uint32_t format)
+uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
+{
+ 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 = (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:
+ 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
+ * one kernel buffer per buffer object.
+ */
+int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t aligned_height, uint32_t format)
{
size_t p, num_planes;
num_planes = drv_num_planes_from_format(format);
assert(num_planes);
+ /*
+ * 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, width, p);
- bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
- 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 = bo->offsets[bo->num_planes - 1] +
- bo->sizes[bo->num_planes - 1];
-
+ 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)
{
- struct drm_mode_create_dumb create_dumb;
int ret;
+ size_t plane;
+ uint32_t aligned_width, aligned_height;
+ struct drm_mode_create_dumb create_dumb;
- /* Only single-plane formats are supported */
- assert(drv_num_planes_from_format(format) == 1);
+ aligned_width = width;
+ aligned_height = height;
+ if (format == DRM_FORMAT_YVU420_ANDROID) {
+ /*
+ * 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 = height;
- create_dumb.width = width;
- create_dumb.bpp = drv_bpp_from_format(format, 0);
+ create_dumb.height = aligned_height;
+ create_dumb.width = aligned_width;
+ create_dumb.bpp = bpp_from_format(format, 0);
create_dumb.flags = 0;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
if (ret) {
- fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
+ drv_log("DRM_IOCTL_MODE_CREATE_DUMB failed (%d, %d)\n", bo->drv->fd, errno);
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;
- bo->strides[0] = create_dumb.pitch;
+ 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;
+
+ bo->total_size = create_dumb.size;
return 0;
}
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);
+ drv_log("DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n", bo->handles[0].u32);
return ret;
}
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);
+ drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n",
+ bo->handles[plane].u32, ret);
error = ret;
}
}
return error;
}
-void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
+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) {
+ drv_log("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 vma *vma, size_t plane, uint32_t map_flags)
{
int ret;
size_t i;
ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
if (ret) {
- fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
+ drv_log("DRM_IOCTL_MODE_MAP_DUMB failed\n");
return MAP_FAILED;
}
for (i = 0; i < bo->num_planes; i++)
if (bo->handles[i].u32 == bo->handles[plane].u32)
- data->length += bo->sizes[i];
+ vma->length += bo->sizes[i];
+
+ return mmap(0, vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
+ map_dumb.offset);
+}
+
+int drv_bo_munmap(struct bo *bo, struct vma *vma)
+{
+ return munmap(vma->addr, vma->length);
+}
+
+int drv_mapping_destroy(struct bo *bo)
+{
+ int ret;
+ size_t plane;
+ struct mapping *mapping;
+ uint32_t idx;
+
+ /*
+ * This function is called right before the buffer is destroyed. It will free any mappings
+ * associated with the buffer.
+ */
+
+ idx = 0;
+ for (plane = 0; plane < bo->num_planes; plane++) {
+ while (idx < drv_array_size(bo->drv->mappings)) {
+ mapping = (struct mapping *)drv_array_at_idx(bo->drv->mappings, idx);
+ if (mapping->vma->handle != bo->handles[plane].u32) {
+ idx++;
+ continue;
+ }
+
+ if (!--mapping->vma->refcount) {
+ ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
+ if (ret) {
+ drv_log("munmap failed\n");
+ return ret;
+ }
+
+ free(mapping->vma);
+ }
- return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
- bo->drv->fd, map_dumb.offset);
+ /* This shrinks and shifts the array, so don't increment idx. */
+ drv_array_remove(bo->drv->mappings, idx);
+ }
+ }
+
+ return 0;
}
-uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
- size_t plane)
+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)
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)
+void drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
+ struct format_metadata *metadata, uint64_t use_flags)
{
- struct combination_list_element *elem;
+ uint32_t i;
- elem = calloc(1, sizeof(*elem));
- elem->combination.format = format;
- elem->combination.modifier = modifier;
- elem->combination.usage = usage;
- LIST_ADD(&elem->link, &drv->backend->combinations);
-}
+ for (i = 0; i < num_formats; i++) {
+ struct combination combo = { .format = formats[i],
+ .metadata = *metadata,
+ .use_flags = use_flags };
-void drv_insert_combinations(struct driver *drv, struct supported_combination *combos,
- uint32_t size)
-{
- 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);
-
- pthread_mutex_unlock(&drv->driver_lock);
+ drv_array_append(drv->combos, &combo);
+ }
}
-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 use_flags)
{
- /*
- * 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 flags to an existing combination. */
+ for (i = 0; i < drv_array_size(drv->combos); i++) {
+ combo = (struct combination *)drv_array_at_idx(drv->combos, i);
+ if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
+ combo->metadata.modifier == metadata->modifier)
+ combo->use_flags |= use_flags;
}
-
-
- 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 drv_array *drv_query_kms(struct driver *drv)
{
- int ret;
- uint32_t i, j;
- uint64_t flag, usage;
+ struct drv_array *kms_items;
+ uint64_t plane_type, use_flag;
+ uint32_t i, j, k;
+
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,
- BO_USE_SCANOUT, 0);
- drv_modify_supported_combination(drv, DRM_FORMAT_ARGB8888,
- BO_USE_SCANOUT, 0);
+ kms_items = drv_array_init(sizeof(struct kms_item));
+ if (!kms_items)
+ goto out;
/*
* The ability to return universal planes is only complete on
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);
+ 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];
+ 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);
}
- 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 < 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.
+ */
+ 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;
+}
-err:
- ret = -1;
- return ret;
+/*
+ * 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;
}