gralloc4: Print usages in a human-readable way

Bug: 180774763
Test: boot, run faceauth test
Change-Id: I3103c08fff5a68d7291a6cbbc61f3eb5eb27066b
diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp
index 4cc6e77..eab8114 100644
--- a/gralloc4/src/Android.bp
+++ b/gralloc4/src/Android.bp
@@ -41,6 +41,7 @@
 		"core/format_info.cpp",
 		"core/mali_gralloc_formats.cpp",
 		"core/mali_gralloc_bufferallocation.cpp",
+		"core/mali_gralloc_bufferdescriptor.cpp",
 		"core/mali_gralloc_reference.cpp",
 		"core/mali_gralloc_debug.cpp",
 		":libgralloc_hidl_common_shared_metadata",
diff --git a/gralloc4/src/core/Android.bp b/gralloc4/src/core/Android.bp
index 634764f..8d3f51d 100644
--- a/gralloc4/src/core/Android.bp
+++ b/gralloc4/src/core/Android.bp
@@ -52,6 +52,7 @@
 	srcs: [
 		"mali_gralloc_bufferaccess.cpp",
 		"mali_gralloc_bufferallocation.cpp",
+		"mali_gralloc_bufferdescriptor.cpp",
 		"mali_gralloc_formats.cpp",
 		"mali_gralloc_reference.cpp",
 		"mali_gralloc_debug.cpp",
diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp
index 1736abb..e360cbd 100644
--- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp
+++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp
@@ -889,8 +889,8 @@
 	                                                         bufDescriptor->width * bufDescriptor->height);
 	if (bufDescriptor->alloc_format == MALI_GRALLOC_FORMAT_INTERNAL_UNDEFINED)
 	{
-		MALI_GRALLOC_LOGE("ERROR: Unrecognized and/or unsupported format 0x%" PRIx64 " and usage 0x%" PRIx64,
-		       bufDescriptor->hal_format, usage);
+		MALI_GRALLOC_LOGE("ERROR: Unrecognized and/or unsupported format 0x%" PRIx64 " and usage (%s 0x%" PRIx64 ")",
+		       bufDescriptor->hal_format, describe_usage(usage).c_str(), usage);
 		return -EINVAL;
 	}
 
diff --git a/gralloc4/src/core/mali_gralloc_bufferdescriptor.cpp b/gralloc4/src/core/mali_gralloc_bufferdescriptor.cpp
new file mode 100644
index 0000000..672ef3e
--- /dev/null
+++ b/gralloc4/src/core/mali_gralloc_bufferdescriptor.cpp
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2020 Google Inc. All rights reserved.
+ *
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mali_gralloc_bufferdescriptor.h"
+#include "mali_gralloc_usages.h"
+
+#include <sstream>
+
+using android::hardware::graphics::common::V1_2::BufferUsage;
+
+#define BUFFERUSAGE(n)     { static_cast<uint64_t>(BufferUsage::n), #n }
+#define USAGE(prefix, n)   { prefix ## n, #n }
+static struct usage_name {
+	uint64_t usage;
+	const char *name;
+} usage_names[] = {
+// graphics common v1.0 usages
+	BUFFERUSAGE(GPU_TEXTURE),
+	BUFFERUSAGE(GPU_RENDER_TARGET),
+	BUFFERUSAGE(COMPOSER_OVERLAY),
+	BUFFERUSAGE(COMPOSER_CLIENT_TARGET),
+	BUFFERUSAGE(PROTECTED),
+	BUFFERUSAGE(COMPOSER_CURSOR),
+	BUFFERUSAGE(VIDEO_ENCODER),
+	BUFFERUSAGE(CAMERA_OUTPUT),
+	BUFFERUSAGE(CAMERA_INPUT),
+	BUFFERUSAGE(RENDERSCRIPT),
+	BUFFERUSAGE(VIDEO_DECODER),
+	BUFFERUSAGE(SENSOR_DIRECT_DATA),
+	BUFFERUSAGE(GPU_DATA_BUFFER),
+// graphics common v1.1 usages
+	BUFFERUSAGE(GPU_CUBE_MAP),
+	BUFFERUSAGE(GPU_MIPMAP_COMPLETE),
+// graphics common v1.2 usages
+	BUFFERUSAGE(HW_IMAGE_ENCODER),
+// Google usages
+	USAGE(GRALLOC_USAGE_, GOOGLE_IP_BO),
+	USAGE(GRALLOC_USAGE_, GOOGLE_IP_MFC),
+	USAGE(GS101_GRALLOC_USAGE_, TPU_INPUT),
+	USAGE(GS101_GRALLOC_USAGE_, TPU_OUTPUT),
+	USAGE(GS101_GRALLOC_USAGE_, CAMERA_STATS),
+// Exynos specific usages
+	USAGE(GRALLOC_USAGE_, PROTECTED_DPB),
+	USAGE(GRALLOC_USAGE_, PRIVATE_NONSECURE),
+	USAGE(GRALLOC_USAGE_, NOZEROED),
+	USAGE(GRALLOC_USAGE_, YUV_RANGE_FULL),
+	USAGE(GRALLOC_USAGE_, VIDEO_PRIVATE_DATA),
+	USAGE(GRALLOC_USAGE_, HFR_MODE),
+	USAGE(GRALLOC_USAGE_, SBWC_REQUEST_10BIT),
+};
+
+std::string describe_usage(uint64_t usage)
+{
+	std::ostringstream stream;
+	switch (static_cast<BufferUsage>(usage & BufferUsage::CPU_READ_MASK)) {
+		case BufferUsage::CPU_READ_NEVER:
+			stream << "CPU_READ_NEVER";
+			break;
+		case BufferUsage::CPU_READ_RARELY:
+			stream << "CPU_READ_RARELY";
+			break;
+		case BufferUsage::CPU_READ_OFTEN:
+			stream << "CPU_READ_OFTEN";
+			break;
+		default:
+			stream << "<unknown CPU read value 0x" << std::hex << (usage & 0x0full) << ">";
+			break;
+	}
+	stream << "|";
+	switch (static_cast<BufferUsage>(usage & BufferUsage::CPU_WRITE_MASK)) {
+		case BufferUsage::CPU_WRITE_NEVER:
+			stream << "CPU_WRITE_NEVER";
+			break;
+		case BufferUsage::CPU_WRITE_RARELY:
+			stream << "CPU_WRITE_RARELY";
+			break;
+		case BufferUsage::CPU_WRITE_OFTEN:
+			stream << "CPU_WRITE_OFTEN";
+			break;
+		default:
+			stream << "<unknown CPU write value 0x" << std::hex << (usage & 0xf0ull) << ">";
+			break;
+	}
+	usage &= ~(0xffull);
+	for (uint64_t i = 0;
+		 i < (sizeof(usage_names) / sizeof(usage_name)) && usage;
+		 ++i)
+	{
+		if ((usage & usage_names[i].usage) == usage_names[i].usage)
+		{
+			usage = usage & (~usage_names[i].usage);
+			stream << "|";
+			stream << usage_names[i].name;
+		}
+	}
+	if (usage) {
+		stream << std::dec;
+		for (uint64_t i = 0; (i < 64) && usage; ++i)
+		{
+			if (usage & (1 << i))
+			{
+				stream << "|(1<<" << i << ")";
+				usage &= ~(1 << i);
+			}
+		}
+	}
+	return stream.str();
+}
diff --git a/gralloc4/src/core/mali_gralloc_bufferdescriptor.h b/gralloc4/src/core/mali_gralloc_bufferdescriptor.h
index 2f815f0..3c7c45f 100644
--- a/gralloc4/src/core/mali_gralloc_bufferdescriptor.h
+++ b/gralloc4/src/core/mali_gralloc_bufferdescriptor.h
@@ -25,6 +25,8 @@
 
 typedef uint64_t gralloc_buffer_descriptor_t;
 
+std::string describe_usage(uint64_t usage);
+
 /* A buffer_descriptor contains the requested parameters for the buffer
  * as well as the calculated parameters that are passed to the allocator.
  */
@@ -78,7 +80,7 @@
 	void dump(const std::string &str) const {
 		ALOGI("buffer_descriptor: %s "
 			"wh(%u %u) "
-			"usage_pc(0x%" PRIx64 " 0x%" PRIx64 ") "
+			"usage_pc(%s 0x%" PRIx64 " %s 0x%" PRIx64 ") "
 			"hal_format(0x%" PRIx64 ") "
 			"layer_count(%u) "
 			"format_type(%u) "
@@ -92,7 +94,10 @@
 			"plane[2](offset %" PRId64 ", idx %u, size %" PRIu64 " byte_stride %u, wh %u %u)"
 			"\n",
 			str.c_str(),
-			width, height, producer_usage, consumer_usage, hal_format,
+			width, height,
+			describe_usage(producer_usage).c_str(), producer_usage,
+			describe_usage(consumer_usage).c_str(), consumer_usage,
+			hal_format,
 			layer_count,
 			format_type,
 			name.c_str(),
diff --git a/gralloc4/src/core/mali_gralloc_formats.cpp b/gralloc4/src/core/mali_gralloc_formats.cpp
index 60f2a20..85735f4 100644
--- a/gralloc4/src/core/mali_gralloc_formats.cpp
+++ b/gralloc4/src/core/mali_gralloc_formats.cpp
@@ -431,9 +431,9 @@
 		}
 	}
 
-	MALI_GRALLOC_LOGV("%s: alloc_format=0x%" PRIx64 " usage=0x%" PRIx64
-	      " alloc_width=%u, alloc_height=%u",
-	      __FUNCTION__, alloc_format, usage, *width, *height);
+	MALI_GRALLOC_LOGV("%s: alloc_format=0x%" PRIx64 " usage=(%s 0x%" PRIx64
+	      ") alloc_width=%u, alloc_height=%u",
+	      __FUNCTION__, alloc_format, describe_usage(usage).c_str(), usage, *width, *height);
 }
 
 
@@ -1461,7 +1461,7 @@
 	/* Reject if usage specified is outside white list of valid usages. */
 	if (type != MALI_GRALLOC_FORMAT_TYPE_INTERNAL && (usage & (~VALID_USAGE)) != 0)
 	{
-		MALI_GRALLOC_LOGE("Invalid usage specified: 0x%" PRIx64, usage);
+		MALI_GRALLOC_LOGE("Invalid usage specified: %s 0x%" PRIx64, describe_usage(usage).c_str(), usage);
 	}
 
 	/* TODO: Make a function for finding formats that should be allocated as the request format */
@@ -1540,9 +1540,9 @@
 	}
 
 out:
-	MALI_GRALLOC_LOGV("mali_gralloc_select_format: req_format=0x%08" PRIx64 ", usage=0x%" PRIx64
-	      ", req_base_format=0x%" PRIx32 ", alloc_format=0x%" PRIx64,
-	      req_format, usage, req_base_format, alloc_format);
+	MALI_GRALLOC_LOGV("mali_gralloc_select_format: req_format=0x%08" PRIx64 ", usage=(%s 0x%" PRIx64
+	      "), req_base_format=0x%" PRIx32 ", alloc_format=0x%" PRIx64,
+	      req_format, describe_usage(usage).c_str(), usage, req_base_format, alloc_format);
 
 	return alloc_format;
 }