1 /*
2  * Copyright (C) 2020 Google Inc. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "mali_gralloc_bufferdescriptor.h"
20 #include "mali_gralloc_usages.h"
21 
22 #include <sstream>
23 
24 using android::hardware::graphics::common::V1_2::BufferUsage;
25 
26 #define BUFFERUSAGE(n)     { static_cast<uint64_t>(BufferUsage::n), #n }
27 #define USAGE(prefix, n)   { prefix ## n, #n }
28 static struct usage_name {
29 	uint64_t usage;
30 	const char *name;
31 } usage_names[] = {
32 // graphics common v1.0 usages
33 	BUFFERUSAGE(GPU_TEXTURE),
34 	BUFFERUSAGE(GPU_RENDER_TARGET),
35 	BUFFERUSAGE(COMPOSER_OVERLAY),
36 	BUFFERUSAGE(COMPOSER_CLIENT_TARGET),
37 	BUFFERUSAGE(PROTECTED),
38 	BUFFERUSAGE(COMPOSER_CURSOR),
39 	BUFFERUSAGE(VIDEO_ENCODER),
40 	BUFFERUSAGE(CAMERA_OUTPUT),
41 	BUFFERUSAGE(CAMERA_INPUT),
42 	BUFFERUSAGE(RENDERSCRIPT),
43 	BUFFERUSAGE(VIDEO_DECODER),
44 	BUFFERUSAGE(SENSOR_DIRECT_DATA),
45 	BUFFERUSAGE(GPU_DATA_BUFFER),
46 // graphics common v1.1 usages
47 	BUFFERUSAGE(GPU_CUBE_MAP),
48 	BUFFERUSAGE(GPU_MIPMAP_COMPLETE),
49 // graphics common v1.2 usages
50 	BUFFERUSAGE(HW_IMAGE_ENCODER),
51 // Google usages
52 	USAGE(GRALLOC_USAGE_, GOOGLE_IP_BO),
53 	USAGE(GRALLOC_USAGE_, GOOGLE_IP_MFC),
54 	USAGE(GS101_GRALLOC_USAGE_, TPU_INPUT),
55 	USAGE(GS101_GRALLOC_USAGE_, TPU_OUTPUT),
56 	USAGE(GS101_GRALLOC_USAGE_, CAMERA_STATS),
57 // Exynos specific usages
58 	USAGE(GRALLOC_USAGE_, PRIVATE_NONSECURE),
59 	USAGE(GRALLOC_USAGE_, NOZEROED),
60 	USAGE(GRALLOC_USAGE_, VIDEO_PRIVATE_DATA),
61 };
62 
describe_usage(uint64_t usage)63 std::string describe_usage(uint64_t usage)
64 {
65 	std::ostringstream stream;
66 	switch (static_cast<BufferUsage>(usage & BufferUsage::CPU_READ_MASK)) {
67 		case BufferUsage::CPU_READ_NEVER:
68 			stream << "CPU_READ_NEVER";
69 			break;
70 		case BufferUsage::CPU_READ_RARELY:
71 			stream << "CPU_READ_RARELY";
72 			break;
73 		case BufferUsage::CPU_READ_OFTEN:
74 			stream << "CPU_READ_OFTEN";
75 			break;
76 		default:
77 			stream << "<unknown CPU read value 0x" << std::hex << (usage & 0x0full) << ">";
78 			break;
79 	}
80 	stream << "|";
81 	switch (static_cast<BufferUsage>(usage & BufferUsage::CPU_WRITE_MASK)) {
82 		case BufferUsage::CPU_WRITE_NEVER:
83 			stream << "CPU_WRITE_NEVER";
84 			break;
85 		case BufferUsage::CPU_WRITE_RARELY:
86 			stream << "CPU_WRITE_RARELY";
87 			break;
88 		case BufferUsage::CPU_WRITE_OFTEN:
89 			stream << "CPU_WRITE_OFTEN";
90 			break;
91 		default:
92 			stream << "<unknown CPU write value 0x" << std::hex << (usage & 0xf0ull) << ">";
93 			break;
94 	}
95 	usage &= ~(0xffull);
96 	for (uint64_t i = 0;
97 		 i < (sizeof(usage_names) / sizeof(usage_name)) && usage;
98 		 ++i)
99 	{
100 		if ((usage & usage_names[i].usage) == usage_names[i].usage)
101 		{
102 			usage = usage & (~usage_names[i].usage);
103 			stream << "|";
104 			stream << usage_names[i].name;
105 		}
106 	}
107 	if (usage) {
108 		stream << std::dec;
109 		for (uint64_t i = 0; (i < 64) && usage; ++i)
110 		{
111 			if (usage & (1 << i))
112 			{
113 				stream << "|(1<<" << i << ")";
114 				usage &= ~(1 << i);
115 			}
116 		}
117 	}
118 	return stream.str();
119 }
120