• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 "cros_gralloc_helpers.h"
8 
9 #include <sync/sync.h>
10 
cros_gralloc_convert_format(int format)11 uint32_t cros_gralloc_convert_format(int format)
12 {
13 	/*
14 	 * Conversion from HAL to fourcc-based DRV formats based on
15 	 * platform_android.c in mesa.
16 	 */
17 
18 	switch (format) {
19 	case HAL_PIXEL_FORMAT_BGRA_8888:
20 		return DRM_FORMAT_ARGB8888;
21 	case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
22 		return DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
23 	case HAL_PIXEL_FORMAT_RAW16:
24 		return DRM_FORMAT_R16;
25 	case HAL_PIXEL_FORMAT_RGB_565:
26 		return DRM_FORMAT_RGB565;
27 	case HAL_PIXEL_FORMAT_RGB_888:
28 		return DRM_FORMAT_BGR888;
29 	case HAL_PIXEL_FORMAT_RGBA_8888:
30 		return DRM_FORMAT_ABGR8888;
31 	case HAL_PIXEL_FORMAT_RGBX_8888:
32 		return DRM_FORMAT_XBGR8888;
33 	case HAL_PIXEL_FORMAT_YCbCr_420_888:
34 		return DRM_FORMAT_FLEX_YCbCr_420_888;
35 	case HAL_PIXEL_FORMAT_YV12:
36 		return DRM_FORMAT_YVU420_ANDROID;
37 	/*
38 	 * Choose DRM_FORMAT_R8 because <system/graphics.h> requires the buffers
39 	 * with a format HAL_PIXEL_FORMAT_BLOB have a height of 1, and width
40 	 * equal to their size in bytes.
41 	 */
42 	case HAL_PIXEL_FORMAT_BLOB:
43 		return DRM_FORMAT_R8;
44 #if ANDROID_VERSION >= 0x0a00
45 	case HAL_PIXEL_FORMAT_RGBA_1010102:
46 		return DRM_FORMAT_ABGR2101010;
47 	case HAL_PIXEL_FORMAT_RGBA_FP16:
48 		return DRM_FORMAT_ABGR16161616F;
49 #endif
50 	}
51 
52 	return DRM_FORMAT_NONE;
53 }
54 
cros_gralloc_convert_handle(buffer_handle_t handle)55 cros_gralloc_handle_t cros_gralloc_convert_handle(buffer_handle_t handle)
56 {
57 	auto hnd = reinterpret_cast<cros_gralloc_handle_t>(handle);
58 	if (!hnd || hnd->magic != cros_gralloc_magic)
59 		return nullptr;
60 
61 	return hnd;
62 }
63 
cros_gralloc_sync_wait(int32_t fence,bool close_fence)64 int32_t cros_gralloc_sync_wait(int32_t fence, bool close_fence)
65 {
66 	if (fence < 0)
67 		return 0;
68 
69 	/*
70 	 * Wait initially for 1000 ms, and then wait indefinitely. The SYNC_IOC_WAIT
71 	 * documentation states the caller waits indefinitely on the fence if timeout < 0.
72 	 */
73 	int err = sync_wait(fence, 1000);
74 	if (err < 0) {
75 		drv_log("Timed out on sync wait, err = %s\n", strerror(errno));
76 		err = sync_wait(fence, -1);
77 		if (err < 0) {
78 			drv_log("sync wait error = %s\n", strerror(errno));
79 			return -errno;
80 		}
81 	}
82 
83 	if (close_fence) {
84 		err = close(fence);
85 		if (err) {
86 			drv_log("Unable to close fence fd, err = %s\n", strerror(errno));
87 			return -errno;
88 		}
89 	}
90 
91 	return 0;
92 }
93 
get_drm_format_string(uint32_t drm_format)94 std::string get_drm_format_string(uint32_t drm_format)
95 {
96 	char *sequence = (char *)&drm_format;
97 	std::string s(sequence, 4);
98 	return "DRM_FOURCC_" + s;
99 }
100