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 int drv_bpp_from_format(uint32_t format, size_t plane)
23 assert(plane < drv_num_planes_from_format(format));
26 case DRM_FORMAT_BGR233:
29 case DRM_FORMAT_RGB332:
30 case DRM_FORMAT_YVU420:
31 case DRM_FORMAT_YVU420_ANDROID:
35 * NV12 is laid out as follows. Each letter represents a byte.
37 * Y0_0, Y0_1, Y0_2, Y0_3, ..., Y0_N
38 * Y1_0, Y1_1, Y1_2, Y1_3, ..., Y1_N
40 * YM_0, YM_1, YM_2, YM_3, ..., YM_N
42 * Cb01_01, Cr01_01, Cb01_23, Cr01_23, ..., Cb01_(N-1)N, Cr01_(N-1)N
43 * Cb23_01, Cr23_01, Cb23_23, Cr23_23, ..., Cb23_(N-1)N, Cr23_(N-1)N
45 * Cb(M-1)M_01, Cr(M-1)M_01, ..., Cb(M-1)M_(N-1)N, Cr(M-1)M_(N-1)N
47 * Pixel (0, 0) requires Y0_0, Cb01_01 and Cr01_01. Pixel (0, 1) requires
48 * Y0_1, Cb01_01 and Cr01_01. So for a single pixel, 2 bytes of luma data
52 return (plane == 0) ? 8 : 16;
54 case DRM_FORMAT_ABGR1555:
55 case DRM_FORMAT_ABGR4444:
56 case DRM_FORMAT_ARGB1555:
57 case DRM_FORMAT_ARGB4444:
58 case DRM_FORMAT_BGR565:
59 case DRM_FORMAT_BGRA4444:
60 case DRM_FORMAT_BGRA5551:
61 case DRM_FORMAT_BGRX4444:
62 case DRM_FORMAT_BGRX5551:
65 case DRM_FORMAT_RGB565:
66 case DRM_FORMAT_RGBA4444:
67 case DRM_FORMAT_RGBA5551:
68 case DRM_FORMAT_RGBX4444:
69 case DRM_FORMAT_RGBX5551:
72 case DRM_FORMAT_XBGR1555:
73 case DRM_FORMAT_XBGR4444:
74 case DRM_FORMAT_XRGB1555:
75 case DRM_FORMAT_XRGB4444:
80 case DRM_FORMAT_BGR888:
81 case DRM_FORMAT_RGB888:
84 case DRM_FORMAT_ABGR2101010:
85 case DRM_FORMAT_ABGR8888:
86 case DRM_FORMAT_ARGB2101010:
87 case DRM_FORMAT_ARGB8888:
89 case DRM_FORMAT_BGRA1010102:
90 case DRM_FORMAT_BGRA8888:
91 case DRM_FORMAT_BGRX1010102:
92 case DRM_FORMAT_BGRX8888:
93 case DRM_FORMAT_RGBA1010102:
94 case DRM_FORMAT_RGBA8888:
95 case DRM_FORMAT_RGBX1010102:
96 case DRM_FORMAT_RGBX8888:
97 case DRM_FORMAT_XBGR2101010:
98 case DRM_FORMAT_XBGR8888:
99 case DRM_FORMAT_XRGB2101010:
100 case DRM_FORMAT_XRGB8888:
104 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
109 * This function returns the stride for a given format, width and plane.
111 uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
113 uint32_t stride = DIV_ROUND_UP(width * drv_bpp_from_format(format, plane),
117 * Only downsample for certain multiplanar formats which have horizontal
118 * subsampling for chroma planes. Only formats supported by our drivers
119 * are listed here -- add more as needed.
123 case DRM_FORMAT_NV12:
124 case DRM_FORMAT_YVU420:
125 case DRM_FORMAT_YVU420_ANDROID:
126 stride = DIV_ROUND_UP(stride, 2);
132 * The stride of Android YV12 buffers is required to be aligned to 16 bytes
133 * (see <system/graphics.h>).
135 if (format == DRM_FORMAT_YVU420_ANDROID)
136 stride = ALIGN(stride, 16);
142 * This function fills in the buffer object given driver aligned dimensions
143 * (in pixels) and a format. This function assumes there is just one kernel
144 * buffer per buffer object.
146 int drv_bo_from_format(struct bo *bo, uint32_t aligned_width,
147 uint32_t aligned_height, uint32_t format)
150 size_t p, num_planes;
153 num_planes = drv_num_planes_from_format(format);
157 for (p = 0; p < num_planes; p++) {
158 bo->strides[p] = drv_stride_from_format(format, aligned_width,
160 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
162 bo->offsets[p] = offset;
163 offset += bo->sizes[p];
164 bo->total_size += drv_size_from_format(format, bo->strides[p],
171 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
172 uint32_t format, uint32_t flags)
176 uint32_t aligned_width, aligned_height, bytes_per_pixel;
177 struct drm_mode_create_dumb create_dumb;
179 aligned_width = width;
180 aligned_height = height;
181 bytes_per_pixel = drv_bytes_per_pixel(format, 0);
182 if (format == DRM_FORMAT_YVU420_ANDROID) {
184 * Align width to 16 pixels, so chroma strides are 16 bytes as
187 aligned_width = ALIGN(width, 32);
188 aligned_height = 3 * DIV_ROUND_UP(height, 2);
191 memset(&create_dumb, 0, sizeof(create_dumb));
192 create_dumb.height = aligned_height;
193 create_dumb.width = aligned_width;
194 create_dumb.bpp = drv_bpp_from_format(format, 0);
195 create_dumb.flags = 0;
197 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
199 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
203 drv_bo_from_format(bo, DIV_ROUND_UP(create_dumb.pitch, bytes_per_pixel),
206 for (plane = 0; plane < bo->num_planes; plane++)
207 bo->handles[plane].u32 = create_dumb.handle;
209 bo->total_size = create_dumb.size;
213 int drv_dumb_bo_destroy(struct bo *bo)
215 struct drm_mode_destroy_dumb destroy_dumb;
218 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
219 destroy_dumb.handle = bo->handles[0].u32;
221 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
223 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
224 "(handle=%x)\n", bo->handles[0].u32);
231 int drv_gem_bo_destroy(struct bo *bo)
233 struct drm_gem_close gem_close;
237 for (plane = 0; plane < bo->num_planes; plane++) {
238 for (i = 0; i < plane; i++)
239 if (bo->handles[i].u32 == bo->handles[plane].u32)
241 /* Make sure close hasn't already been called on this handle */
245 memset(&gem_close, 0, sizeof(gem_close));
246 gem_close.handle = bo->handles[plane].u32;
248 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
250 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
251 "(handle=%x) error %d\n",
252 bo->handles[plane].u32, ret);
260 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
264 struct drm_prime_handle prime_handle;
266 for (plane = 0; plane < bo->num_planes; plane++) {
267 memset(&prime_handle, 0, sizeof(prime_handle));
268 prime_handle.fd = data->fds[plane];
270 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
274 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
275 "failed (fd=%u)\n", prime_handle.fd);
278 * Need to call GEM close on planes that were opened,
279 * if any. Adjust the num_planes variable to be the
280 * plane that failed, so GEM close will be called on
281 * planes before that plane.
283 bo->num_planes = plane;
284 drv_gem_bo_destroy(bo);
288 bo->handles[plane].u32 = prime_handle.handle;
291 for (plane = 0; plane < bo->num_planes; plane++) {
292 pthread_mutex_lock(&bo->drv->driver_lock);
293 drv_increment_reference_count(bo->drv, bo, plane);
294 pthread_mutex_unlock(&bo->drv->driver_lock);
300 void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
304 struct drm_mode_map_dumb map_dumb;
306 memset(&map_dumb, 0, sizeof(map_dumb));
307 map_dumb.handle = bo->handles[plane].u32;
309 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
311 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
315 for (i = 0; i < bo->num_planes; i++)
316 if (bo->handles[i].u32 == bo->handles[plane].u32)
317 data->length += bo->sizes[i];
319 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
320 bo->drv->fd, map_dumb.offset);
323 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
329 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
330 num = (uintptr_t) (count);
335 void drv_increment_reference_count(struct driver *drv, struct bo *bo,
338 uintptr_t num = drv_get_reference_count(drv, bo, plane);
340 /* If a value isn't in the table, drmHashDelete is a no-op */
341 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
342 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
346 void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
349 uintptr_t num = drv_get_reference_count(drv, bo, plane);
351 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
354 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
358 uint32_t drv_log_base2(uint32_t value)
368 int drv_add_combination(struct driver *drv, uint32_t format,
369 struct format_metadata *metadata, uint64_t usage)
371 struct combinations *combos = &drv->backend->combos;
372 if (combos->size >= combos->allocations) {
373 struct combination *new_data;
374 combos->allocations *= 2;
375 new_data = realloc(combos->data, combos->allocations
376 * sizeof(*combos->data));
380 combos->data = new_data;
383 combos->data[combos->size].format = format;
384 combos->data[combos->size].metadata.priority = metadata->priority;
385 combos->data[combos->size].metadata.tiling = metadata->tiling;
386 combos->data[combos->size].metadata.modifier = metadata->modifier;
387 combos->data[combos->size].usage = usage;
392 int drv_add_combinations(struct driver *drv, const uint32_t *formats,
393 uint32_t num_formats, struct format_metadata *metadata,
398 for (i = 0; i < num_formats; i++) {
399 ret = drv_add_combination(drv, formats[i], metadata, usage);
407 void drv_modify_combination(struct driver *drv, uint32_t format,
408 struct format_metadata *metadata, uint64_t usage)
411 struct combination *combo;
412 /* Attempts to add the specified usage to an existing combination. */
413 for (i = 0; i < drv->backend->combos.size; i++) {
414 combo = &drv->backend->combos.data[i];
415 if (combo->format == format &&
416 combo->metadata.tiling == metadata->tiling &&
417 combo->metadata.modifier == metadata->modifier)
418 combo->usage |= usage;
422 struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
424 uint64_t flag, usage;
425 struct kms_item *items;
426 uint32_t i, j, k, allocations, item_size;
428 drmModePlanePtr plane;
429 drmModePropertyPtr prop;
430 drmModePlaneResPtr resources;
431 drmModeObjectPropertiesPtr props;
433 /* Start with a power of 2 number of allocations. */
436 items = calloc(allocations, sizeof(*items));
441 * The ability to return universal planes is only complete on
442 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
443 * therefore might return an error code, so don't check it. If it
444 * fails, it'll just return the plane list as overlay planes, which is
445 * fine in our case (our drivers already have cursor bits set).
446 * modetest in libdrm does the same thing.
448 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
450 resources = drmModeGetPlaneResources(drv->fd);
454 for (i = 0; i < resources->count_planes; i++) {
455 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
459 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
460 DRM_MODE_OBJECT_PLANE);
464 for (j = 0; j < props->count_props; j++) {
465 prop = drmModeGetProperty(drv->fd, props->props[j]);
467 if (strcmp(prop->name, "type") == 0) {
468 flag = props->prop_values[j];
471 drmModeFreeProperty(prop);
476 case DRM_PLANE_TYPE_OVERLAY:
477 case DRM_PLANE_TYPE_PRIMARY:
478 usage = BO_USE_SCANOUT;
480 case DRM_PLANE_TYPE_CURSOR:
481 usage = BO_USE_CURSOR;
487 for (j = 0; j < plane->count_formats; j++) {
489 for (k = 0; k < item_size; k++) {
490 if (items[k].format == plane->formats[j] &&
491 items[k].modifier == DRM_FORMAT_MOD_NONE) {
492 items[k].usage |= usage;
498 if (!found && item_size >= allocations) {
499 struct kms_item *new_data = NULL;
501 new_data = realloc(items, allocations *
512 items[item_size].format = plane->formats[j];
513 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
514 items[item_size].usage = usage;
519 drmModeFreeObjectProperties(props);
520 drmModeFreePlane(plane);
524 drmModeFreePlaneResources(resources);
526 if (items && item_size == 0) {
531 *num_items = item_size;
535 int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
536 uint32_t num_formats)
539 uint32_t i, j, num_items;
540 struct kms_item *items;
541 struct combination *combo;
542 struct format_metadata metadata;
545 metadata.priority = 1;
546 metadata.modifier = DRM_FORMAT_MOD_NONE;
548 ret = drv_add_combinations(drv, formats, num_formats, &metadata,
553 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
554 * plane and as a cursor. Some drivers don't support
555 * drmModeGetPlaneResources, so add the combination here. Note that the
556 * kernel disregards the alpha component of ARGB unless it's an overlay
559 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
560 BO_USE_CURSOR | BO_USE_SCANOUT);
561 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
562 BO_USE_CURSOR | BO_USE_SCANOUT);
564 items = drv_query_kms(drv, &num_items);
565 if (!items || !num_items)
568 for (i = 0; i < num_items; i++) {
569 for (j = 0; j < drv->backend->combos.size; j++) {
570 combo = &drv->backend->combos.data[j];
571 if (items[i].format == combo->format)
572 combo->usage |= BO_USE_SCANOUT;