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 fills in the buffer object given driver aligned dimensions
110 * (in pixels) and a format. This function assumes there is just one kernel
111 * buffer per buffer object.
113 int drv_bo_from_format(struct bo *bo, uint32_t aligned_width,
114 uint32_t aligned_height, uint32_t format)
117 size_t p, num_planes;
120 num_planes = drv_num_planes_from_format(format);
124 for (p = 0; p < num_planes; p++) {
125 bo->strides[p] = drv_stride_from_format(format, aligned_width,
127 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
129 bo->offsets[p] = offset;
130 offset += bo->sizes[p];
131 bo->total_size += drv_size_from_format(format, bo->strides[p],
138 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
139 uint32_t format, uint32_t flags)
143 uint32_t aligned_width, aligned_height, bytes_per_pixel;
144 struct drm_mode_create_dumb create_dumb;
146 aligned_width = width;
147 aligned_height = height;
148 bytes_per_pixel = DIV_ROUND_UP(drv_bpp_from_format(format, 0), 8);
149 if (format == DRM_FORMAT_YVU420_ANDROID) {
151 * Align width to 16 pixels, so chroma strides are 16 bytes as
154 aligned_width = ALIGN(width, 32);
155 aligned_height = 3 * DIV_ROUND_UP(height, 2);
158 memset(&create_dumb, 0, sizeof(create_dumb));
159 create_dumb.height = aligned_height;
160 create_dumb.width = aligned_width;
161 create_dumb.bpp = drv_bpp_from_format(format, 0);
162 create_dumb.flags = 0;
164 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
166 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
170 drv_bo_from_format(bo, DIV_ROUND_UP(create_dumb.pitch, bytes_per_pixel),
173 for (plane = 0; plane < bo->num_planes; plane++)
174 bo->handles[plane].u32 = create_dumb.handle;
176 bo->total_size = create_dumb.size;
180 int drv_dumb_bo_destroy(struct bo *bo)
182 struct drm_mode_destroy_dumb destroy_dumb;
185 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
186 destroy_dumb.handle = bo->handles[0].u32;
188 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
190 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
191 "(handle=%x)\n", bo->handles[0].u32);
198 int drv_gem_bo_destroy(struct bo *bo)
200 struct drm_gem_close gem_close;
204 for (plane = 0; plane < bo->num_planes; plane++) {
205 for (i = 0; i < plane; i++)
206 if (bo->handles[i].u32 == bo->handles[plane].u32)
208 /* Make sure close hasn't already been called on this handle */
212 memset(&gem_close, 0, sizeof(gem_close));
213 gem_close.handle = bo->handles[plane].u32;
215 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
217 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
218 "(handle=%x) error %d\n",
219 bo->handles[plane].u32, ret);
227 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
231 struct drm_prime_handle prime_handle;
233 for (plane = 0; plane < bo->num_planes; plane++) {
234 memset(&prime_handle, 0, sizeof(prime_handle));
235 prime_handle.fd = data->fds[plane];
237 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
241 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE "
242 "failed (fd=%u)\n", prime_handle.fd);
245 * Need to call GEM close on planes that were opened,
246 * if any. Adjust the num_planes variable to be the
247 * plane that failed, so GEM close will be called on
248 * planes before that plane.
250 bo->num_planes = plane;
251 drv_gem_bo_destroy(bo);
255 bo->handles[plane].u32 = prime_handle.handle;
258 for (plane = 0; plane < bo->num_planes; plane++) {
259 pthread_mutex_lock(&bo->drv->driver_lock);
260 drv_increment_reference_count(bo->drv, bo, plane);
261 pthread_mutex_unlock(&bo->drv->driver_lock);
267 void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
271 struct drm_mode_map_dumb map_dumb;
273 memset(&map_dumb, 0, sizeof(map_dumb));
274 map_dumb.handle = bo->handles[plane].u32;
276 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
278 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
282 for (i = 0; i < bo->num_planes; i++)
283 if (bo->handles[i].u32 == bo->handles[plane].u32)
284 data->length += bo->sizes[i];
286 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
287 bo->drv->fd, map_dumb.offset);
290 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
296 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
297 num = (uintptr_t) (count);
302 void drv_increment_reference_count(struct driver *drv, struct bo *bo,
305 uintptr_t num = drv_get_reference_count(drv, bo, plane);
307 /* If a value isn't in the table, drmHashDelete is a no-op */
308 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
309 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
313 void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
316 uintptr_t num = drv_get_reference_count(drv, bo, plane);
318 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
321 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
325 uint32_t drv_log_base2(uint32_t value)
335 int drv_add_combination(struct driver *drv, uint32_t format,
336 struct format_metadata *metadata, uint64_t usage)
338 struct combinations *combos = &drv->backend->combos;
339 if (combos->size >= combos->allocations) {
340 struct combination *new_data;
341 combos->allocations *= 2;
342 new_data = realloc(combos->data, combos->allocations
343 * sizeof(*combos->data));
347 combos->data = new_data;
350 combos->data[combos->size].format = format;
351 combos->data[combos->size].metadata.priority = metadata->priority;
352 combos->data[combos->size].metadata.tiling = metadata->tiling;
353 combos->data[combos->size].metadata.modifier = metadata->modifier;
354 combos->data[combos->size].usage = usage;
359 int drv_add_combinations(struct driver *drv, const uint32_t *formats,
360 uint32_t num_formats, struct format_metadata *metadata,
365 for (i = 0; i < num_formats; i++) {
366 ret = drv_add_combination(drv, formats[i], metadata, usage);
374 void drv_modify_combination(struct driver *drv, uint32_t format,
375 struct format_metadata *metadata, uint64_t usage)
378 struct combination *combo;
379 /* Attempts to add the specified usage to an existing combination. */
380 for (i = 0; i < drv->backend->combos.size; i++) {
381 combo = &drv->backend->combos.data[i];
382 if (combo->format == format &&
383 combo->metadata.tiling == metadata->tiling &&
384 combo->metadata.modifier == metadata->modifier)
385 combo->usage |= usage;
389 struct kms_item *drv_query_kms(struct driver *drv, uint32_t *num_items)
391 uint64_t flag, usage;
392 struct kms_item *items;
393 uint32_t i, j, k, allocations, item_size;
395 drmModePlanePtr plane;
396 drmModePropertyPtr prop;
397 drmModePlaneResPtr resources;
398 drmModeObjectPropertiesPtr props;
400 /* Start with a power of 2 number of allocations. */
403 items = calloc(allocations, sizeof(*items));
408 * The ability to return universal planes is only complete on
409 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
410 * therefore might return an error code, so don't check it. If it
411 * fails, it'll just return the plane list as overlay planes, which is
412 * fine in our case (our drivers already have cursor bits set).
413 * modetest in libdrm does the same thing.
415 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
417 resources = drmModeGetPlaneResources(drv->fd);
421 for (i = 0; i < resources->count_planes; i++) {
422 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
426 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
427 DRM_MODE_OBJECT_PLANE);
431 for (j = 0; j < props->count_props; j++) {
432 prop = drmModeGetProperty(drv->fd, props->props[j]);
434 if (strcmp(prop->name, "type") == 0) {
435 flag = props->prop_values[j];
438 drmModeFreeProperty(prop);
443 case DRM_PLANE_TYPE_OVERLAY:
444 case DRM_PLANE_TYPE_PRIMARY:
445 usage = BO_USE_SCANOUT;
447 case DRM_PLANE_TYPE_CURSOR:
448 usage = BO_USE_CURSOR;
454 for (j = 0; j < plane->count_formats; j++) {
456 for (k = 0; k < item_size; k++) {
457 if (items[k].format == plane->formats[j] &&
458 items[k].modifier == DRM_FORMAT_MOD_NONE) {
459 items[k].usage |= usage;
465 if (!found && item_size >= allocations) {
466 struct kms_item *new_data = NULL;
468 new_data = realloc(items, allocations *
479 items[item_size].format = plane->formats[j];
480 items[item_size].modifier = DRM_FORMAT_MOD_NONE;
481 items[item_size].usage = usage;
486 drmModeFreeObjectProperties(props);
487 drmModeFreePlane(plane);
491 drmModeFreePlaneResources(resources);
493 if (items && item_size == 0) {
498 *num_items = item_size;
502 int drv_add_linear_combinations(struct driver *drv, const uint32_t *formats,
503 uint32_t num_formats)
506 uint32_t i, j, num_items;
507 struct kms_item *items;
508 struct combination *combo;
509 struct format_metadata metadata;
512 metadata.priority = 1;
513 metadata.modifier = DRM_FORMAT_MOD_NONE;
515 ret = drv_add_combinations(drv, formats, num_formats, &metadata,
520 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
521 * plane and as a cursor. Some drivers don't support
522 * drmModeGetPlaneResources, so add the combination here. Note that the
523 * kernel disregards the alpha component of ARGB unless it's an overlay
526 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
527 BO_USE_CURSOR | BO_USE_SCANOUT);
528 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
529 BO_USE_CURSOR | BO_USE_SCANOUT);
531 items = drv_query_kms(drv, &num_items);
532 if (!items || !num_items)
535 for (i = 0; i < num_items; i++) {
536 for (j = 0; j < drv->backend->combos.size; j++) {
537 combo = &drv->backend->combos.data[j];
538 if (items[i].format == combo->format)
539 combo->usage |= BO_USE_SCANOUT;