gralloc: add cros_gralloc_convert_usage to unify usage resolution

The current gralloc0 specific usage conversion is already a superset.
This change also fixes a misalignment on video encoder mapping due to
the original fix missing to hit gralloc4.

BUG=b:199524294
TEST=CtsNativeHardwareTestCases
TEST=gralloc4 builds on aosp

Change-Id: Ib1e37ba09deb50b754863b261423e201ab6b4910
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/minigbm/+/3166892
Tested-by: Yiwei Zhang <zzyiwei@chromium.org>
Reviewed-by: Jason Macnak <natsu@google.com>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
Commit-Queue: Yiwei Zhang <zzyiwei@chromium.org>
diff --git a/cros_gralloc/gralloc0/gralloc0.cc b/cros_gralloc/gralloc0/gralloc0.cc
index b322bee..cc87f3b 100644
--- a/cros_gralloc/gralloc0/gralloc0.cc
+++ b/cros_gralloc/gralloc0/gralloc0.cc
@@ -61,71 +61,6 @@
 };
 // clang-format on
 
-// Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
-// passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
-// entirety, so we can detect the video decoder flag passed by IAllocator clients.
-#define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
-
-// Gralloc0 doesn't define the BufferUsage::GPU_DATA_BUFFER flag. Define here to
-// align accordingly since AHardwareBuffer and Vulkan interop requires gralloc
-// to support allocating with AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER.
-#define BUFFER_USAGE_GPU_DATA_BUFFER (1 << 24)
-
-static uint64_t gralloc0_convert_usage(int usage)
-{
-	uint64_t use_flags = BO_USE_NONE;
-
-	if (usage & GRALLOC_USAGE_CURSOR)
-		use_flags |= BO_USE_NONE;
-	if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
-		use_flags |= BO_USE_SW_READ_RARELY;
-	if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
-		use_flags |= BO_USE_SW_READ_OFTEN;
-	if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
-		use_flags |= BO_USE_SW_WRITE_RARELY;
-	if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
-		use_flags |= BO_USE_SW_WRITE_OFTEN;
-	if (usage & GRALLOC_USAGE_HW_TEXTURE)
-		use_flags |= BO_USE_TEXTURE;
-	if (usage & GRALLOC_USAGE_HW_RENDER)
-		use_flags |= BO_USE_RENDERING;
-	if (usage & GRALLOC_USAGE_HW_2D)
-		use_flags |= BO_USE_RENDERING;
-	if (usage & GRALLOC_USAGE_HW_COMPOSER)
-		/* HWC wants to use display hardware, but can defer to OpenGL. */
-		use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
-	if (usage & GRALLOC_USAGE_HW_FB)
-		use_flags |= BO_USE_NONE;
-	if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
-		/*
-		 * This flag potentially covers external display for the normal drivers (i915,
-		 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
-		 * */
-		use_flags |= BO_USE_NONE;
-	/* Map this flag to linear until real HW protection is available on Android. */
-	if (usage & GRALLOC_USAGE_PROTECTED)
-		use_flags |= BO_USE_LINEAR;
-	if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
-		use_flags |= BO_USE_HW_VIDEO_ENCODER;
-		/*HACK: See b/30054495 */
-		use_flags |= BO_USE_SW_READ_OFTEN;
-	}
-	if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
-		use_flags |= BO_USE_CAMERA_WRITE;
-	if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
-		use_flags |= BO_USE_CAMERA_READ;
-	if (usage & GRALLOC_USAGE_RENDERSCRIPT)
-		use_flags |= BO_USE_RENDERSCRIPT;
-	if (usage & BUFFER_USAGE_VIDEO_DECODER)
-		use_flags |= BO_USE_HW_VIDEO_DECODER;
-	if (usage & BUFFER_USAGE_FRONT_RENDERING)
-		use_flags |= BO_USE_FRONT_RENDERING;
-	if (usage & BUFFER_USAGE_GPU_DATA_BUFFER)
-		use_flags |= BO_USE_GPU_DATA_BUFFER;
-
-	return use_flags;
-}
-
 static uint32_t gralloc0_convert_map_usage(int map_usage)
 {
 	uint32_t map_flags = BO_MAP_NONE;
@@ -158,7 +93,7 @@
 	descriptor.droid_format = format;
 	descriptor.droid_usage = usage;
 	descriptor.drm_format = cros_gralloc_convert_format(format);
-	descriptor.use_flags = gralloc0_convert_usage(usage);
+	descriptor.use_flags = cros_gralloc_convert_usage(usage);
 	descriptor.reserved_region_size = 0;
 
 	supported = mod->driver->is_supported(&descriptor);