• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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.
5  */
6 
7 #include "drv_helpers.h"
8 
9 #include <assert.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/mman.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <xf86drm.h>
18 
19 #ifdef __ANDROID__
20 #include <cutils/properties.h>
21 #endif
22 
23 #include "drv_priv.h"
24 #include "util.h"
25 
26 struct planar_layout {
27 	size_t num_planes;
28 	int horizontal_subsampling[DRV_MAX_PLANES];
29 	int vertical_subsampling[DRV_MAX_PLANES];
30 	int bytes_per_pixel[DRV_MAX_PLANES];
31 };
32 
33 // clang-format off
34 
35 static const struct planar_layout packed_1bpp_layout = {
36 	.num_planes = 1,
37 	.horizontal_subsampling = { 1 },
38 	.vertical_subsampling = { 1 },
39 	.bytes_per_pixel = { 1 }
40 };
41 
42 static const struct planar_layout packed_2bpp_layout = {
43 	.num_planes = 1,
44 	.horizontal_subsampling = { 1 },
45 	.vertical_subsampling = { 1 },
46 	.bytes_per_pixel = { 2 }
47 };
48 
49 static const struct planar_layout packed_3bpp_layout = {
50 	.num_planes = 1,
51 	.horizontal_subsampling = { 1 },
52 	.vertical_subsampling = { 1 },
53 	.bytes_per_pixel = { 3 }
54 };
55 
56 static const struct planar_layout packed_4bpp_layout = {
57 	.num_planes = 1,
58 	.horizontal_subsampling = { 1 },
59 	.vertical_subsampling = { 1 },
60 	.bytes_per_pixel = { 4 }
61 };
62 
63 static const struct planar_layout packed_8bpp_layout = {
64 	.num_planes = 1,
65 	.horizontal_subsampling = { 1 },
66 	.vertical_subsampling = { 1 },
67 	.bytes_per_pixel = { 8 }
68 };
69 
70 static const struct planar_layout biplanar_yuv_420_layout = {
71 	.num_planes = 2,
72 	.horizontal_subsampling = { 1, 2 },
73 	.vertical_subsampling = { 1, 2 },
74 	.bytes_per_pixel = { 1, 2 }
75 };
76 
77 static const struct planar_layout triplanar_yuv_420_layout = {
78 	.num_planes = 3,
79 	.horizontal_subsampling = { 1, 2, 2 },
80 	.vertical_subsampling = { 1, 2, 2 },
81 	.bytes_per_pixel = { 1, 1, 1 }
82 };
83 
84 static const struct planar_layout biplanar_yuv_p010_layout = {
85 	.num_planes = 2,
86 	.horizontal_subsampling = { 1, 2 },
87 	.vertical_subsampling = { 1, 2 },
88 	.bytes_per_pixel = { 2, 4 }
89 };
90 
91 // clang-format on
92 
layout_from_format(uint32_t format)93 static const struct planar_layout *layout_from_format(uint32_t format)
94 {
95 	switch (format) {
96 	case DRM_FORMAT_BGR233:
97 	case DRM_FORMAT_C8:
98 	case DRM_FORMAT_R8:
99 	case DRM_FORMAT_RGB332:
100 		return &packed_1bpp_layout;
101 
102 	case DRM_FORMAT_R16:
103 		return &packed_2bpp_layout;
104 
105 	case DRM_FORMAT_YVU420:
106 	case DRM_FORMAT_YVU420_ANDROID:
107 		return &triplanar_yuv_420_layout;
108 
109 	case DRM_FORMAT_NV12:
110 	case DRM_FORMAT_NV21:
111 		return &biplanar_yuv_420_layout;
112 
113 	case DRM_FORMAT_P010:
114 		return &biplanar_yuv_p010_layout;
115 
116 	case DRM_FORMAT_ABGR1555:
117 	case DRM_FORMAT_ABGR4444:
118 	case DRM_FORMAT_ARGB1555:
119 	case DRM_FORMAT_ARGB4444:
120 	case DRM_FORMAT_BGR565:
121 	case DRM_FORMAT_BGRA4444:
122 	case DRM_FORMAT_BGRA5551:
123 	case DRM_FORMAT_BGRX4444:
124 	case DRM_FORMAT_BGRX5551:
125 	case DRM_FORMAT_GR88:
126 	case DRM_FORMAT_RG88:
127 	case DRM_FORMAT_RGB565:
128 	case DRM_FORMAT_RGBA4444:
129 	case DRM_FORMAT_RGBA5551:
130 	case DRM_FORMAT_RGBX4444:
131 	case DRM_FORMAT_RGBX5551:
132 	case DRM_FORMAT_UYVY:
133 	case DRM_FORMAT_VYUY:
134 	case DRM_FORMAT_XBGR1555:
135 	case DRM_FORMAT_XBGR4444:
136 	case DRM_FORMAT_XRGB1555:
137 	case DRM_FORMAT_XRGB4444:
138 	case DRM_FORMAT_YUYV:
139 	case DRM_FORMAT_YVYU:
140 	case DRM_FORMAT_MTISP_SXYZW10:
141 		return &packed_2bpp_layout;
142 
143 	case DRM_FORMAT_BGR888:
144 	case DRM_FORMAT_RGB888:
145 		return &packed_3bpp_layout;
146 
147 	case DRM_FORMAT_ABGR2101010:
148 	case DRM_FORMAT_ABGR8888:
149 	case DRM_FORMAT_ARGB2101010:
150 	case DRM_FORMAT_ARGB8888:
151 	case DRM_FORMAT_AYUV:
152 	case DRM_FORMAT_BGRA1010102:
153 	case DRM_FORMAT_BGRA8888:
154 	case DRM_FORMAT_BGRX1010102:
155 	case DRM_FORMAT_BGRX8888:
156 	case DRM_FORMAT_RGBA1010102:
157 	case DRM_FORMAT_RGBA8888:
158 	case DRM_FORMAT_RGBX1010102:
159 	case DRM_FORMAT_RGBX8888:
160 	case DRM_FORMAT_XBGR2101010:
161 	case DRM_FORMAT_XBGR8888:
162 	case DRM_FORMAT_XRGB2101010:
163 	case DRM_FORMAT_XRGB8888:
164 		return &packed_4bpp_layout;
165 
166 	case DRM_FORMAT_ABGR16161616F:
167 		return &packed_8bpp_layout;
168 
169 	default:
170 		drv_loge("UNKNOWN FORMAT %d\n", format);
171 		return NULL;
172 	}
173 }
174 
drv_num_planes_from_format(uint32_t format)175 size_t drv_num_planes_from_format(uint32_t format)
176 {
177 	const struct planar_layout *layout = layout_from_format(format);
178 
179 	/*
180 	 * drv_bo_new calls this function early to query number of planes and
181 	 * considers 0 planes to mean unknown format, so we have to support
182 	 * that.  All other layout_from_format() queries can assume that the
183 	 * format is supported and that the return value is non-NULL.
184 	 */
185 
186 	return layout ? layout->num_planes : 0;
187 }
188 
drv_num_planes_from_modifier(struct driver * drv,uint32_t format,uint64_t modifier)189 size_t drv_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
190 {
191 	size_t planes = drv_num_planes_from_format(format);
192 
193 	/* Disallow unsupported formats. */
194 	if (!planes)
195 		return 0;
196 
197 	if (drv->backend->num_planes_from_modifier && modifier != DRM_FORMAT_MOD_INVALID &&
198 	    modifier != DRM_FORMAT_MOD_LINEAR)
199 		return drv->backend->num_planes_from_modifier(drv, format, modifier);
200 
201 	return planes;
202 }
203 
drv_height_from_format(uint32_t format,uint32_t height,size_t plane)204 uint32_t drv_height_from_format(uint32_t format, uint32_t height, size_t plane)
205 {
206 	const struct planar_layout *layout = layout_from_format(format);
207 
208 	assert(plane < layout->num_planes);
209 
210 	return DIV_ROUND_UP(height, layout->vertical_subsampling[plane]);
211 }
212 
drv_vertical_subsampling_from_format(uint32_t format,size_t plane)213 uint32_t drv_vertical_subsampling_from_format(uint32_t format, size_t plane)
214 {
215 	const struct planar_layout *layout = layout_from_format(format);
216 
217 	assert(plane < layout->num_planes);
218 
219 	return layout->vertical_subsampling[plane];
220 }
221 
drv_bytes_per_pixel_from_format(uint32_t format,size_t plane)222 uint32_t drv_bytes_per_pixel_from_format(uint32_t format, size_t plane)
223 {
224 	const struct planar_layout *layout = layout_from_format(format);
225 
226 	assert(plane < layout->num_planes);
227 
228 	return layout->bytes_per_pixel[plane];
229 }
230 
231 /*
232  * This function returns the stride for a given format, width and plane.
233  */
drv_stride_from_format(uint32_t format,uint32_t width,size_t plane)234 uint32_t drv_stride_from_format(uint32_t format, uint32_t width, size_t plane)
235 {
236 	const struct planar_layout *layout = layout_from_format(format);
237 	assert(plane < layout->num_planes);
238 
239 	uint32_t plane_width = DIV_ROUND_UP(width, layout->horizontal_subsampling[plane]);
240 	uint32_t stride = plane_width * layout->bytes_per_pixel[plane];
241 
242 	/*
243 	 * The stride of Android YV12 buffers is required to be aligned to 16 bytes
244 	 * (see <system/graphics.h>).
245 	 */
246 	if (format == DRM_FORMAT_YVU420_ANDROID)
247 		stride = (plane == 0) ? ALIGN(stride, 32) : ALIGN(stride, 16);
248 
249 	return stride;
250 }
251 
drv_size_from_format(uint32_t format,uint32_t stride,uint32_t height,size_t plane)252 uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
253 {
254 	return stride * drv_height_from_format(format, height, plane);
255 }
256 
subsample_stride(uint32_t stride,uint32_t stride_align,uint32_t format,size_t plane)257 static uint32_t subsample_stride(uint32_t stride, uint32_t stride_align, uint32_t format,
258 				 size_t plane)
259 {
260 	uint32_t plane_stride = stride;
261 
262 	if (plane != 0) {
263 		switch (format) {
264 		case DRM_FORMAT_YVU420:
265 		case DRM_FORMAT_YVU420_ANDROID:
266 			plane_stride = DIV_ROUND_UP(plane_stride, 2);
267 			break;
268 		}
269 	}
270 
271 	plane_stride = ALIGN(plane_stride, stride_align);
272 
273 	if (format == DRM_FORMAT_YVU420_ANDROID) {
274 		if (plane != 0)
275 			assert(plane_stride == ALIGN(stride / 2, 16));
276 		else
277 			assert(plane_stride % 16 == 0);
278 	}
279 
280 	return plane_stride;
281 }
282 
283 /*
284  * This function fills in the buffer object given the driver aligned stride of
285  * the first plane, height and a format. This function assumes there is just
286  * one kernel buffer per buffer object.
287  */
drv_bo_from_format(struct bo * bo,uint32_t stride,uint32_t stride_align,uint32_t aligned_height,uint32_t format)288 int drv_bo_from_format(struct bo *bo, uint32_t stride, uint32_t stride_align,
289 		       uint32_t aligned_height, uint32_t format)
290 {
291 	uint32_t padding[DRV_MAX_PLANES] = { 0 };
292 	return drv_bo_from_format_and_padding(bo, stride, stride_align, aligned_height, format,
293 					      padding);
294 }
295 
drv_bo_from_format_and_padding(struct bo * bo,uint32_t stride,uint32_t stride_align,uint32_t aligned_height,uint32_t format,uint32_t padding[DRV_MAX_PLANES])296 int drv_bo_from_format_and_padding(struct bo *bo, uint32_t stride, uint32_t stride_align,
297 				   uint32_t aligned_height, uint32_t format,
298 				   uint32_t padding[DRV_MAX_PLANES])
299 {
300 	size_t p, num_planes;
301 	uint32_t offset = 0;
302 
303 	num_planes = drv_num_planes_from_format(format);
304 	assert(num_planes);
305 
306 	/*
307 	 * HAL_PIXEL_FORMAT_YV12 requires that (see <system/graphics.h>):
308 	 *  - the aligned height is same as the buffer's height.
309 	 *  - the luma stride is 16 bytes aligned
310 	 *  - the chroma stride is ALIGN(luma_stride / 2, 16)
311 	 */
312 	if (format == DRM_FORMAT_YVU420_ANDROID) {
313 		assert(aligned_height == bo->meta.height);
314 		/* force 16 if the caller has no requirement */
315 		if (stride_align <= 1)
316 			stride_align = 16;
317 	}
318 
319 	for (p = 0; p < num_planes; p++) {
320 		bo->meta.strides[p] = subsample_stride(stride, stride_align, format, p);
321 		bo->meta.sizes[p] =
322 		    drv_size_from_format(format, bo->meta.strides[p], aligned_height, p) +
323 		    padding[p];
324 		bo->meta.offsets[p] = offset;
325 		offset += bo->meta.sizes[p];
326 	}
327 
328 	bo->meta.total_size = offset;
329 	return 0;
330 }
331 
drv_dumb_bo_create_ex(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags,uint64_t quirks)332 int drv_dumb_bo_create_ex(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
333 			  uint64_t use_flags, uint64_t quirks)
334 {
335 	int ret;
336 	uint32_t aligned_width, aligned_height;
337 	struct drm_mode_create_dumb create_dumb = { 0 };
338 
339 	aligned_width = width;
340 	aligned_height = height;
341 	switch (format) {
342 	case DRM_FORMAT_R16:
343 		/* HAL_PIXEL_FORMAT_Y16 requires that the buffer's width be 16 pixel
344 		 * aligned. See hardware/interfaces/graphics/common/1.0/types.hal. */
345 		aligned_width = ALIGN(width, 16);
346 		break;
347 	case DRM_FORMAT_YVU420_ANDROID:
348 		/* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not
349 		 * be aligned. Update 'height' so that drv_bo_from_format below
350 		 * uses the non-aligned height. */
351 		height = bo->meta.height;
352 
353 		/* Align width to 32 pixels, so chroma strides are 16 bytes as
354 		 * Android requires. */
355 		aligned_width = ALIGN(width, 32);
356 
357 		/* Adjust the height to include room for chroma planes. */
358 		aligned_height = 3 * DIV_ROUND_UP(height, 2);
359 		break;
360 	case DRM_FORMAT_YVU420:
361 	case DRM_FORMAT_NV12:
362 	case DRM_FORMAT_NV21:
363 	case DRM_FORMAT_P010:
364 		/* Adjust the height to include room for chroma planes */
365 		aligned_height = 3 * DIV_ROUND_UP(height, 2);
366 		break;
367 	default:
368 		break;
369 	}
370 
371 	if (quirks & BO_QUIRK_DUMB32BPP) {
372 		aligned_width =
373 		    DIV_ROUND_UP(aligned_width * layout_from_format(format)->bytes_per_pixel[0], 4);
374 		create_dumb.bpp = 32;
375 	} else {
376 		/* Align for llvmpipe 64-byte tile size for dumb_driver */
377 		aligned_width = ALIGN(aligned_width, MESA_LLVMPIPE_TILE_SIZE);
378 		aligned_height = ALIGN(aligned_height, MESA_LLVMPIPE_TILE_SIZE);
379 		create_dumb.bpp = layout_from_format(format)->bytes_per_pixel[0] * 8;
380 	}
381 	create_dumb.width = aligned_width;
382 	create_dumb.height = aligned_height;
383 	create_dumb.flags = 0;
384 
385 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
386 	if (ret) {
387 		drv_loge("DRM_IOCTL_MODE_CREATE_DUMB failed (%d, %d)\n", bo->drv->fd, errno);
388 		return -errno;
389 	}
390 
391 	drv_bo_from_format(bo, create_dumb.pitch, 1, height, format);
392 
393 	bo->handle.u32 = create_dumb.handle;
394 
395 	bo->meta.total_size = create_dumb.size;
396 	return 0;
397 }
398 
drv_dumb_bo_create(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)399 int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
400 		       uint64_t use_flags)
401 {
402 	return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_NONE);
403 }
404 
drv_dumb_bo_destroy(struct bo * bo)405 int drv_dumb_bo_destroy(struct bo *bo)
406 {
407 	int ret;
408 	struct drm_mode_destroy_dumb destroy_dumb = { 0 };
409 
410 	destroy_dumb.handle = bo->handle.u32;
411 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
412 	if (ret) {
413 		drv_loge("DRM_IOCTL_MODE_DESTROY_DUMB failed (handle=%x)\n", bo->handle.u32);
414 		return -errno;
415 	}
416 
417 	return 0;
418 }
419 
drv_gem_close(struct driver * drv,uint32_t gem_handle)420 int drv_gem_close(struct driver *drv, uint32_t gem_handle)
421 {
422 	struct drm_gem_close gem_close;
423 	int ret, error = 0;
424 
425 	memset(&gem_close, 0, sizeof(gem_close));
426 	gem_close.handle = gem_handle;
427 
428 	ret = drmIoctl(drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
429 	if (ret) {
430 		drv_loge("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n", gem_handle, ret);
431 		error = -errno;
432 	}
433 
434 	return error;
435 }
436 
drv_gem_bo_destroy(struct bo * bo)437 int drv_gem_bo_destroy(struct bo *bo)
438 {
439 	return drv_gem_close(bo->drv, bo->handle.u32);
440 }
441 
drv_prime_bo_import(struct bo * bo,struct drv_import_fd_data * data)442 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data)
443 {
444 	int ret;
445 	size_t plane;
446 	struct drm_prime_handle prime_handle;
447 
448 	for (plane = 0; plane < bo->meta.num_planes; plane++) {
449 		memset(&prime_handle, 0, sizeof(prime_handle));
450 		prime_handle.fd = data->fds[plane];
451 
452 		ret = drmIoctl(bo->drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_handle);
453 
454 		if (plane > 0 && !ret && bo->handle.u32 != prime_handle.handle) {
455 			drv_gem_close(bo->drv, prime_handle.handle);
456 			ret = -1;
457 			errno = EINVAL;
458 		}
459 
460 		if (ret) {
461 			drv_loge("DRM_IOCTL_PRIME_FD_TO_HANDLE failed (fd=%u)\n", prime_handle.fd);
462 			if (plane > 0)
463 				drv_gem_close(bo->drv, bo->handle.u32);
464 			return -errno;
465 		}
466 
467 		bo->handle.u32 = prime_handle.handle;
468 	}
469 	bo->meta.tiling = data->tiling;
470 
471 	return 0;
472 }
473 
drv_dumb_bo_map(struct bo * bo,struct vma * vma,uint32_t map_flags)474 void *drv_dumb_bo_map(struct bo *bo, struct vma *vma, uint32_t map_flags)
475 {
476 	int ret;
477 	size_t i;
478 	struct drm_mode_map_dumb map_dumb;
479 
480 	memset(&map_dumb, 0, sizeof(map_dumb));
481 	map_dumb.handle = bo->handle.u32;
482 
483 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
484 	if (ret) {
485 		drv_loge("DRM_IOCTL_MODE_MAP_DUMB failed\n");
486 		return MAP_FAILED;
487 	}
488 
489 	for (i = 0; i < bo->meta.num_planes; i++)
490 		vma->length += bo->meta.sizes[i];
491 
492 	return mmap(0, vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
493 		    map_dumb.offset);
494 }
495 
drv_bo_munmap(struct bo * bo,struct vma * vma)496 int drv_bo_munmap(struct bo *bo, struct vma *vma)
497 {
498 	return munmap(vma->addr, vma->length);
499 }
500 
drv_get_prot(uint32_t map_flags)501 int drv_get_prot(uint32_t map_flags)
502 {
503 	return (BO_MAP_WRITE & map_flags) ? PROT_WRITE | PROT_READ : PROT_READ;
504 }
505 
drv_add_combination(struct driver * drv,const uint32_t format,struct format_metadata * metadata,uint64_t use_flags)506 void drv_add_combination(struct driver *drv, const uint32_t format,
507 			 struct format_metadata *metadata, uint64_t use_flags)
508 {
509 	struct combination combo = { .format = format,
510 				     .metadata = *metadata,
511 				     .use_flags = use_flags };
512 
513 	drv_array_append(drv->combos, &combo);
514 }
515 
drv_add_combinations(struct driver * drv,const uint32_t * formats,uint32_t num_formats,struct format_metadata * metadata,uint64_t use_flags)516 void drv_add_combinations(struct driver *drv, const uint32_t *formats, uint32_t num_formats,
517 			  struct format_metadata *metadata, uint64_t use_flags)
518 {
519 	uint32_t i;
520 
521 	for (i = 0; i < num_formats; i++) {
522 		struct combination combo = { .format = formats[i],
523 					     .metadata = *metadata,
524 					     .use_flags = use_flags };
525 
526 		drv_array_append(drv->combos, &combo);
527 	}
528 }
529 
drv_modify_combination(struct driver * drv,uint32_t format,struct format_metadata * metadata,uint64_t use_flags)530 void drv_modify_combination(struct driver *drv, uint32_t format, struct format_metadata *metadata,
531 			    uint64_t use_flags)
532 {
533 	uint32_t i;
534 	struct combination *combo;
535 	/* Attempts to add the specified flags to an existing combination. */
536 	for (i = 0; i < drv_array_size(drv->combos); i++) {
537 		combo = (struct combination *)drv_array_at_idx(drv->combos, i);
538 		if (combo->format == format && combo->metadata.tiling == metadata->tiling &&
539 		    combo->metadata.modifier == metadata->modifier)
540 			combo->use_flags |= use_flags;
541 	}
542 }
543 
drv_modify_linear_combinations(struct driver * drv)544 int drv_modify_linear_combinations(struct driver *drv)
545 {
546 	/*
547 	 * All current drivers can scanout linear XRGB8888/ARGB8888 as a primary
548 	 * plane and as a cursor.
549 	 */
550 	drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
551 			       BO_USE_CURSOR | BO_USE_SCANOUT);
552 	drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
553 			       BO_USE_CURSOR | BO_USE_SCANOUT);
554 	return 0;
555 }
556 
557 /*
558  * Pick the best modifier from modifiers, according to the ordering
559  * given by modifier_order.
560  */
drv_pick_modifier(const uint64_t * modifiers,uint32_t count,const uint64_t * modifier_order,uint32_t order_count)561 uint64_t drv_pick_modifier(const uint64_t *modifiers, uint32_t count,
562 			   const uint64_t *modifier_order, uint32_t order_count)
563 {
564 	uint32_t i, j;
565 
566 	for (i = 0; i < order_count; i++) {
567 		for (j = 0; j < count; j++) {
568 			if (modifiers[j] == modifier_order[i]) {
569 				return modifiers[j];
570 			}
571 		}
572 	}
573 
574 	return DRM_FORMAT_MOD_LINEAR;
575 }
576 
577 /*
578  * Search a list of modifiers to see if a given modifier is present
579  */
drv_has_modifier(const uint64_t * list,uint32_t count,uint64_t modifier)580 bool drv_has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
581 {
582 	uint32_t i;
583 	for (i = 0; i < count; i++)
584 		if (list[i] == modifier)
585 			return true;
586 
587 	return false;
588 }
589 
drv_resolve_format_and_use_flags_helper(struct driver * drv,uint32_t format,uint64_t use_flags,uint32_t * out_format,uint64_t * out_use_flags)590 void drv_resolve_format_and_use_flags_helper(struct driver *drv, uint32_t format,
591 					     uint64_t use_flags, uint32_t *out_format,
592 					     uint64_t *out_use_flags)
593 {
594 	*out_format = format;
595 	*out_use_flags = use_flags;
596 	switch (format) {
597 	case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
598 		/* Common camera implementation defined format. */
599 		if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
600 			*out_format = DRM_FORMAT_NV12;
601 		} else {
602 			/* HACK: See b/28671744 */
603 			*out_format = DRM_FORMAT_XBGR8888;
604 			*out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
605 		}
606 		break;
607 	case DRM_FORMAT_FLEX_YCbCr_420_888:
608 		/* Common flexible video format. */
609 		*out_format = DRM_FORMAT_NV12;
610 		break;
611 	case DRM_FORMAT_YVU420_ANDROID:
612 		*out_use_flags &= ~BO_USE_SCANOUT;
613 		break;
614 	default:
615 		break;
616 	}
617 }
618 
drv_get_os_option(const char * name)619 const char *drv_get_os_option(const char *name)
620 {
621 	const char *ret = getenv(name);
622 #ifdef __ANDROID__
623 	if (!ret) {
624 		static char prop[PROPERTY_VALUE_MAX];
625 		return property_get(name, prop, NULL) > 1 ? prop : NULL;
626 	}
627 #endif
628 	return ret;
629 }
630 
lru_remove_entry(struct lru_entry * entry)631 static void lru_remove_entry(struct lru_entry *entry)
632 {
633 	entry->prev->next = entry->next;
634 	entry->next->prev = entry->prev;
635 }
636 
lru_link_entry(struct lru * lru,struct lru_entry * entry)637 static void lru_link_entry(struct lru *lru, struct lru_entry *entry)
638 {
639 	struct lru_entry *head = &lru->head;
640 	entry->prev = head;
641 	entry->next = head->next;
642 
643 	head->next->prev = entry;
644 	head->next = entry;
645 }
646 
lru_find(struct lru * lru,bool (* eq)(struct lru_entry * e,void * data),void * data)647 struct lru_entry *lru_find(struct lru *lru, bool (*eq)(struct lru_entry *e, void *data), void *data)
648 {
649 	struct lru_entry *head = &lru->head;
650 	struct lru_entry *cur = head->next;
651 
652 	while (cur != head) {
653 		if (eq(cur, data)) {
654 			lru_remove_entry(cur);
655 			lru_link_entry(lru, cur);
656 			return cur;
657 		}
658 		cur = cur->next;
659 	}
660 
661 	return NULL;
662 }
663 
lru_insert(struct lru * lru,struct lru_entry * entry)664 void lru_insert(struct lru *lru, struct lru_entry *entry)
665 {
666 	if (lru->count == lru->max) {
667 		lru_remove_entry(lru->head.prev);
668 	} else {
669 		lru->count++;
670 	}
671 	lru_link_entry(lru, entry);
672 }
673 
lru_init(struct lru * lru,int max)674 void lru_init(struct lru *lru, int max)
675 {
676 	lru->head.next = &lru->head;
677 	lru->head.prev = &lru->head;
678 	lru->count = 0;
679 	lru->max = max;
680 }
681