1 /*
2  * Copyright (C) 2020 Arm Limited. All rights reserved.
3  *
4  * Copyright 2016 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 #ifndef GRALLOC_COMMON_MAPPER_METADATA_H
20 #define GRALLOC_COMMON_MAPPER_METADATA_H
21 
22 #include <inttypes.h>
23 #include "mali_gralloc_log.h"
24 #include "core/mali_gralloc_bufferdescriptor.h"
25 #include "mali_gralloc_buffer.h"
26 #include "mali_gralloc_error.h"
27 #include <cstring>
28 
29 #include "hidl_common/hidl_common.h"
30 #include <algorithm>
31 #include <iterator>
32 
33 #include <aidl/arm/graphics/Compression.h>
34 #include <aidl/arm/graphics/ArmMetadataType.h>
35 
36 namespace arm
37 {
38 namespace mapper
39 {
40 namespace common
41 {
42 using android::hardware::hidl_vec;
43 using aidl::android::hardware::graphics::common::ExtendableType;
44 
45 #define GRALLOC_ARM_COMPRESSION_TYPE_NAME "arm.graphics.Compression"
46 const static ExtendableType Compression_AFBC{ GRALLOC_ARM_COMPRESSION_TYPE_NAME,
47                                                   static_cast<int64_t>(aidl::arm::graphics::Compression::AFBC) };
48 
49 #define GRALLOC_ARM_METADATA_TYPE_NAME "arm.graphics.ArmMetadataType"
50 
51 
52 class MetadataType {
53  public:
54 	std::string name;
55 	uint64_t value;
56 #if defined(GRALLOC_MAPPER_4)
MetadataType(const IMapper::MetadataType & meta)57 	MetadataType(const IMapper::MetadataType &meta) {
58 		name = meta.name;
59 		value = meta.value;
60 	}
MetadataType()61 	operator IMapper::MetadataType() const {
62 		IMapper::MetadataType meta;
63 		meta.name = name;
64 		meta.value = value;
65 		return meta;
66 	}
67 #elif defined(GRALLOC_MAPPER_5)
MetadataType(const AIMapper_MetadataType & meta)68 	MetadataType(const AIMapper_MetadataType &meta) {
69 			name = meta.name;
70 			value = meta.value;
71 	}
AIMapper_MetadataType()72 	operator AIMapper_MetadataType() const {
73 			AIMapper_MetadataType meta;
74 			meta.name = strdup(name.c_str());
75 			meta.value = value;
76 			return meta;
77 	}
78 #endif // GRALLOC_MAPPER_4 or GRALLOC_MAPPER_5
79 
MetadataType()80 	MetadataType() {}
MetadataType(std::string strname,uint64_t val)81 	MetadataType(std::string strname, uint64_t val) {
82 		name = strname;
83 		value = val;
84 	}
MetadataType(StandardMetadataType meta)85 	MetadataType(StandardMetadataType meta) : MetadataType(GRALLOC4_STANDARD_METADATA_TYPE, static_cast<uint64_t>(meta)) {}
86 };
87 
88 const static MetadataType ArmMetadataType_PLANE_FDS = MetadataType(GRALLOC_ARM_METADATA_TYPE_NAME, static_cast<int64_t>(aidl::arm::graphics::ArmMetadataType::PLANE_FDS));
89 
90 constexpr int RES_SIZE = 32;
91 
92 struct MetadataTypeDescription {
93 	MetadataType metadataType;
94 	const char* description;
95 	bool isGettable;
96 	bool isSettable;
97 #if defined(GRALLOC_MAPPER_4)
MetadataTypeDescriptionMetadataTypeDescription98 	MetadataTypeDescription(const IMapper::MetadataTypeDescription &desc) {
99 		metadataType = desc.metadataType;
100 		description = (desc.description).c_str();
101 		isGettable = desc.isGettable;
102 		isSettable = desc.isSettable;
103 	}
MetadataTypeDescriptionMetadataTypeDescription104 	operator IMapper::MetadataTypeDescription() const {
105 		IMapper::MetadataTypeDescription desc;
106 		desc.metadataType = static_cast<IMapper::MetadataType>(metadataType);
107 		desc.description = description;
108 		desc.isGettable = isGettable;
109 		desc.isSettable = isSettable;
110 		return desc;
111 	}
112 #elif defined(GRALLOC_MAPPER_5)
MetadataTypeDescriptionMetadataTypeDescription113 	MetadataTypeDescription(const AIMapper_MetadataTypeDescription &desc) {
114 		metadataType = desc.metadataType;
115 		description = strdup(desc.description);
116 		isGettable = desc.isGettable;
117 		isSettable = desc.isSettable;
118 	}
AIMapper_MetadataTypeDescriptionMetadataTypeDescription119 	operator AIMapper_MetadataTypeDescription() const {
120 		AIMapper_MetadataTypeDescription desc;
121 		desc.metadataType = static_cast<AIMapper_MetadataType>(metadataType);
122 		desc.isGettable = isGettable;
123 		desc.isSettable = isSettable;
124 		desc.description = strdup(description);
125 		return desc;
126 	}
127 
128 #endif // GRALLOC_MAPPER_4 or GRALLOC_MAPPER_5
MetadataTypeDescriptionMetadataTypeDescription129 	MetadataTypeDescription(MetadataType meta, const char* desc, bool gettable, bool settable) {
130 		metadataType = meta;
131 		description = strdup(desc);
132 		isGettable = gettable;
133 		isSettable = settable;
134 	}
135 };
136 
137 struct MetadataDump {
138 	MetadataType metadataType;
139 	std::vector<uint8_t> metadata;
140 #if defined(GRALLOC_MAPPER_4)
MetadataDumpMetadataDump141 	MetadataDump(const IMapper::MetadataDump &meta) {
142 		metadataType = MetadataType(meta.metadataType);
143 		metadata = static_cast<std::vector<uint8_t> >(metadata);
144 	}
MetadataDumpMetadataDump145 	operator IMapper::MetadataDump() const {
146 		IMapper::MetadataDump dump;
147 		dump.metadataType = static_cast<IMapper::MetadataType>(metadataType);
148 		dump.metadata = hidl_vec(metadata);
149 		return dump;
150 	}
151 #endif // GRALLOC_MAPPER_4
MetadataDumpMetadataDump152 	MetadataDump() {}
MetadataDumpMetadataDump153 	MetadataDump(MetadataType metaType, std::vector<uint8_t> &meta) {
154 		metadataType = metaType;
155 		metadata = meta;
156 	}
157 };
158 
159 struct BufferDump {
160 	std::vector<MetadataDump> metadataDump;
161 #if defined(GRALLOC_MAPPER_4)
BufferDumpBufferDump162 	BufferDump(const IMapper::BufferDump &dump) {
163 		for (auto meta : dump.metadataDump)
164 			metadataDump.push_back(MetadataDump(meta));
165 	}
BufferDumpBufferDump166 	operator IMapper::BufferDump() const {
167 		IMapper::BufferDump bufferdump;
168 		std::vector<IMapper::MetadataDump> metaDump;
169 		for (auto meta : metadataDump) {
170 			metaDump.push_back(static_cast<IMapper::MetadataDump>(meta));
171 		}
172 		bufferdump.metadataDump = metaDump;
173 		return bufferdump;
174 	}
175 #endif // GRALLOC_MAPPER_4
BufferDumpBufferDump176 	BufferDump(std::vector<MetadataDump> &meta) { metadataDump = meta; }
BufferDumpBufferDump177 	BufferDump() {}
178 };
179 
180 
181 int get_num_planes(const private_handle_t *hnd);
182 
183 static std::vector<std::vector<PlaneLayoutComponent>> plane_layout_components_from_handle(const private_handle_t *hnd);
184 
185 android::status_t get_plane_layouts(const private_handle_t *handle, std::vector<PlaneLayout> *layouts);
186 
187 bool isStandardMetadataType(const MetadataType &metadataType);
188 
189 /**
190  * Retrieves a Buffer's metadata value.
191  *
192  * @param handle       [in] The private handle of the buffer to query for metadata.
193  * @param metadataType [in] The type of metadata queried.
194  * @param hidl_cb      [in] HIDL callback function generating -
195  *                          error: NONE on success.
196  *                                 UNSUPPORTED on error when reading or unsupported metadata type.
197  *                          metadata: Vector of bytes representing the metadata value.
198  */
199 Error get_metadata(const private_handle_t *handle, const MetadataType &metadataType, std::vector<uint8_t> &outVec);
200 
201 /**
202  * Sets a Buffer's metadata value.
203  *
204  * @param handle       [in] The private handle of the buffer for which to modify metadata.
205  * @param metadataType [in] The type of metadata to modify.
206  * @param metadata     [in] Vector of bytes representing the new value for the metadata associated with the buffer.
207  *
208  * @return Error::NONE on success.
209  *         Error::UNSUPPORTED on error when writing or unsupported metadata type.
210  */
211 Error set_metadata(const private_handle_t *handle, const MetadataType &metadataType, const frameworks_vec<uint8_t> &metadata);
212 
213 /**
214  * Query basic metadata information about a buffer form its descriptor before allocation.
215  *
216  * @param description  [in] The buffer descriptor.
217  * @param metadataType [in] The type of metadata to query
218  * @param hidl_cb      [in] HIDL callback function generating -
219  *                          error: NONE on success.
220  *                                 UNSUPPORTED on unsupported metadata type.
221  *                          metadata: Vector of bytes representing the metadata value.
222  */
223 #if defined(GRALLOC_MAPPER_4)
224 Error getFromBufferDescriptorInfo(IMapper::BufferDescriptorInfo const &description, MetadataType const &metadataType, std::vector<uint8_t> &outVec);
225 #endif // GRALLOC_MAPPER_4
226 
227 } // namespace common
228 } // namespace mapper
229 } // namespace arm
230 
231 #endif
232