2 * Copyright 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
15 #include <xf86drmMode.h>
21 static uint32_t subsample_stride(uint32_t stride, uint32_t format,
26 case DRM_FORMAT_YVU420:
27 case DRM_FORMAT_YVU420_ANDROID:
28 stride = DIV_ROUND_UP(stride, 2);
36 static uint32_t bpp_from_format(uint32_t format, size_t plane)
38 assert(plane < drv_num_planes_from_format(format));
41 case DRM_FORMAT_BGR233:
44 case DRM_FORMAT_RGB332:
45 case DRM_FORMAT_YVU420:
46 case DRM_FORMAT_YVU420_ANDROID:
50 return (plane == 0) ? 8 : 4;
52 case DRM_FORMAT_ABGR1555:
53 case DRM_FORMAT_ABGR4444:
54 case DRM_FORMAT_ARGB1555:
55 case DRM_FORMAT_ARGB4444:
56 case DRM_FORMAT_BGR565:
57 case DRM_FORMAT_BGRA4444:
58 case DRM_FORMAT_BGRA5551:
59 case DRM_FORMAT_BGRX4444:
60 case DRM_FORMAT_BGRX5551:
63 case DRM_FORMAT_RGB565:
64 case DRM_FORMAT_RGBA4444:
65 case DRM_FORMAT_RGBA5551:
66 case DRM_FORMAT_RGBX4444:
67 case DRM_FORMAT_RGBX5551:
70 case DRM_FORMAT_XBGR1555:
71 case DRM_FORMAT_XBGR4444:
72 case DRM_FORMAT_XRGB1555:
73 case DRM_FORMAT_XRGB4444:
78 case DRM_FORMAT_BGR888:
79 case DRM_FORMAT_RGB888:
82 case DRM_FORMAT_ABGR2101010:
83 case DRM_FORMAT_ABGR8888:
84 case DRM_FORMAT_ARGB2101010:
85 case DRM_FORMAT_ARGB8888:
87 case DRM_FORMAT_BGRA1010102:
88 case DRM_FORMAT_BGRA8888:
89 case DRM_FORMAT_BGRX1010102:
90 case DRM_FORMAT_BGRX8888:
91 case DRM_FORMAT_RGBA1010102:
92 case DRM_FORMAT_RGBA8888:
93 case DRM_FORMAT_RGBX1010102:
94 case DRM_FORMAT_RGBX8888:
95 case DRM_FORMAT_XBGR2101010:
96 case DRM_FORMAT_XBGR8888:
97 case DRM_FORMAT_XRGB2101010:
98 case DRM_FORMAT_XRGB8888:
102 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
106 uint32_t drv_bo_get_stride_in_pixels(struct bo *bo)
108 uint32_t bytes_per_pixel = DIV_ROUND_UP(bpp_from_format(bo->format, 0),
110 return DIV_ROUND_UP(bo->strides[0], bytes_per_pixel);
114 * This function returns the stride for a given format, width and plane.
116 uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
118 uint32_t stride = DIV_ROUND_UP(width * bpp_from_format(format, plane),
122 * The stride of Android YV12 buffers is required to be aligned to 16 bytes
123 * (see <system/graphics.h>).
125 if (format == DRM_FORMAT_YVU420_ANDROID)
126 stride = (plane == 0) ? ALIGN(stride, 32): ALIGN(stride, 16);
132 * This function fills in the buffer object given the driver aligned stride of
133 * the first plane, height and a format. This function assumes there is just
134 * one kernel buffer per buffer object.
136 int drv_bo_from_format(struct bo *bo, uint32_t stride,
137 uint32_t aligned_height, uint32_t format)
140 size_t p, num_planes;
143 num_planes = drv_num_planes_from_format(format);
147 for (p = 0; p < num_planes; p++) {
148 bo->strides[p] = subsample_stride(stride, format, p);
149 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
151 bo->offsets[p] = offset;
152 offset += bo->sizes[p];
153 bo->total_size += drv_size_from_format(format, bo->strides[p],
160 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
161 uint32_t format, uint32_t flags)
165 uint32_t aligned_width, aligned_height;
166 struct drm_mode_create_dumb create_dumb;
168 aligned_width = width;
169 aligned_height = height;
170 if (format == DRM_FORMAT_YVU420_ANDROID) {
172 * Align width to 32 pixels, so chroma strides are 16 bytes as
175 aligned_width = ALIGN(width, 32);
176 aligned_height = 3 * DIV_ROUND_UP(height, 2);
179 memset(&create_dumb, 0, sizeof(create_dumb));
180 create_dumb.height = aligned_height;
181 create_dumb.width = aligned_width;
182 create_dumb.bpp = bpp_from_format(format, 0);
183 create_dumb.flags = 0;
185 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
187 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
191 drv_bo_from_format(bo, create_dumb.pitch, height, format);
193 for (plane = 0; plane < bo->num_planes; plane++)
194 bo->handles[plane].u32 = create_dumb.handle;
196 bo->total_size = create_dumb.size;
200 int drv_dumb_bo_destroy(struct bo *bo)
202 struct drm_mode_destroy_dumb destroy_dumb;
205 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
206 destroy_dumb.handle = bo->handles[0].u32;
208 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
210 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
211 "(handle=%x)\n", bo->handles[0].u32);
218 int drv_gem_bo_destroy(struct bo *bo)
220 struct drm_gem_close gem_close;
224 for (plane = 0; plane < bo->num_planes; plane++) {
225 for (i = 0; i < plane; i++)
226 if (bo->handles[i].u32 == bo->handles[plane].u32)
228 /* Make sure close hasn't already been called on this handle */
232 memset(&gem_close, 0, sizeof(gem_close));
233 gem_close.handle = bo->handles[plane].u32;
235 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
237 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
238 "(handle=%x) error %d\n",
239 bo->handles[plane].u32, ret);
247 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
251 struct drm_prime_handle prime_handle;
253 for (plane = 0; plane < bo->num_planes; plane++) {
254 memset(&prime_handle, 0, sizeof(prime_handle));
255 prime_handle.fd = data->fds[plane];
257 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
261 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
262 "failed (fd=%u)\n", prime_handle.fd);
265 * Need to call GEM close on planes that were opened,
266 * if any. Adjust the num_planes variable to be the
267 * plane that failed, so GEM close will be called on
268 * planes before that plane.
270 bo->num_planes = plane;
271 drv_gem_bo_destroy(bo);
275 bo->handles[plane].u32 = prime_handle.handle;
278 for (plane = 0; plane < bo->num_planes; plane++) {
279 pthread_mutex_lock(&bo->drv->driver_lock);
280 drv_increment_reference_count(bo->drv, bo, plane);
281 pthread_mutex_unlock(&bo->drv->driver_lock);
287 void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
291 struct drm_mode_map_dumb map_dumb;
293 memset(&map_dumb, 0, sizeof(map_dumb));
294 map_dumb.handle = bo->handles[plane].u32;
296 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
298 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
302 for (i = 0; i < bo->num_planes; i++)
303 if (bo->handles[i].u32 == bo->handles[plane].u32)
304 data->length += bo->sizes[i];
306 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
307 bo->drv->fd, map_dumb.offset);
310 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
316 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
317 num = (uintptr_t) (count);
322 void drv_increment_reference_count(struct driver *drv, struct bo *bo,
325 uintptr_t num = drv_get_reference_count(drv, bo, plane);
327 /* If a value isn't in the table, drmHashDelete is a no-op */
328 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
329 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
333 void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
336 uintptr_t num = drv_get_reference_count(drv, bo, plane);
338 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
341 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
345 uint32_t drv_log_base2(uint32_t value)
355 int drv_add_combination(struct driver *drv, uint32_t format,
356 struct format_metadata *metadata, uint64_t usage)
358 struct combinations *combos = &drv->backend->combos;
359 if (combos->size >= combos->allocations) {
360 struct combination *new_data;
361 combos->allocations *= 2;
362 new_data = realloc(combos->data, combos->allocations
363 * sizeof(*combos->data));
367 combos->data = new_data;
370 combos->data[combos->size].format = format;
371 combos->data[combos->size].metadata.priority = metadata->priority;
372 combos->data[combos->size].metadata.tiling = metadata->tiling;
373 combos->data[combos->size].metadata.modifier = metadata->modifier;
374 combos->data[combos->size].usage = usage;
379 int drv_add_combinations(struct driver *drv, const uint32_t *formats,
380 uint32_t num_formats, struct format_metadata *metadata,
385 for (i = 0; i < num_formats; i++) {
386 ret = drv_add_combination(drv, formats[i], metadata, usage);
394 void drv_modify_combination(struct driver *drv, uint32_t format,
395 struct format_metadata *metadata, uint64_t usage)
398 struct combination *combo;
399 /* Attempts to add the specified usage to an existing combination. */
400 for (i = 0; i < drv->backend->combos.size; i++) {
401 combo = &drv->backend->combos.data[i];
402 if (combo->format == format &&
403 combo->metadata.tiling == metadata->tiling &&
404 combo->metadata.modifier == metadata->modifier)
405 combo->usage |= usage;
409 struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
411 uint64_t flag, usage;
412 struct kms_item *items;
413 uint32_t i, j, k, allocations, item_size;
415 drmModePlanePtr plane;
416 drmModePropertyPtr prop;
417 drmModePlaneResPtr resources;
418 drmModeObjectPropertiesPtr props;
420 /* Start with a power of 2 number of allocations. */
423 items = calloc(allocations, sizeof(*items));
428 * The ability to return universal planes is only complete on
429 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
430 * therefore might return an error code, so don't check it. If it
431 * fails, it'll just return the plane list as overlay planes, which is
432 * fine in our case (our drivers already have cursor bits set).
433 * modetest in libdrm does the same thing.
435 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
437 resources = drmModeGetPlaneResources(drv->fd);
441 for (i = 0; i < resources->count_planes; i++) {
442 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
446 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
447 DRM_MODE_OBJECT_PLANE);
451 for (j = 0; j < props->count_props; j++) {
452 prop = drmModeGetProperty(drv->fd, props->props[j]);
454 if (strcmp(prop->name, "type") == 0) {
455 flag = props->prop_values[j];
458 drmModeFreeProperty(prop);
463 case DRM_PLANE_TYPE_OVERLAY:
464 case DRM_PLANE_TYPE_PRIMARY:
465 usage = BO_USE_SCANOUT;
467 case DRM_PLANE_TYPE_CURSOR:
468 usage = BO_USE_CURSOR;
474 for (j = 0; j < plane->count_formats; j++) {
476 for (k = 0; k < item_size; k++) {
477 if (items[k].format == plane->formats[j] &&
478 items[k].modifier == DRM_FORMAT_MOD_NONE) {
479 items[k].usage |= usage;
485 if (!found && item_size >= allocations) {
486 struct kms_item *new_data = NULL;
488 new_data = realloc(items, allocations *
499 items[item_size].format = plane->formats[j];
500 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
501 items[item_size].usage = usage;
506 drmModeFreeObjectProperties(props);
507 drmModeFreePlane(plane);
511 drmModeFreePlaneResources(resources);
513 if (items && item_size == 0) {
518 *num_items = item_size;
522 int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
523 uint32_t num_formats)
526 uint32_t i, j, num_items;
527 struct kms_item *items;
528 struct combination *combo;
529 struct format_metadata metadata;
532 metadata.priority = 1;
533 metadata.modifier = DRM_FORMAT_MOD_NONE;
535 ret = drv_add_combinations(drv, formats, num_formats, &metadata,
540 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
541 * plane and as a cursor. Some drivers don't support
542 * drmModeGetPlaneResources, so add the combination here. Note that the
543 * kernel disregards the alpha component of ARGB unless it's an overlay
546 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
547 BO_USE_CURSOR | BO_USE_SCANOUT);
548 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
549 BO_USE_CURSOR | BO_USE_SCANOUT);
551 items = drv_query_kms(drv, &num_items);
552 if (!items || !num_items)
555 for (i = 0; i < num_items; i++) {
556 for (j = 0; j < drv->backend->combos.size; j++) {
557 combo = &drv->backend->combos.data[j];
558 if (items[i].format == combo->format)
559 combo->usage |= BO_USE_SCANOUT;