• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 Samsung Electronics Co. Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "VendorGraphicBuffer"
18 
19 #include <log/log.h>
20 #include <gralloctypes/Gralloc4.h>
21 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
22 
23 #include "VendorGraphicBuffer.h"
24 #include "mali_gralloc_buffer.h"
25 #include "mali_gralloc_formats.h"
26 #include "hidl_common/SharedMetadata.h"
27 #include "hidl_common/SharedMetadata_struct.h"
28 #include "exynos_format.h"
29 
30 #include <pixel-gralloc/metadata.h>
31 
32 using namespace android;
33 using namespace vendor::graphics;
34 
35 using arm::mapper::common::shared_metadata;
36 using aidl::android::hardware::graphics::common::Dataspace;
37 using android::gralloc4::encodeDataspace;
38 using android::gralloc4::decodeDataspace;
39 using android::gralloc4::decodePixelFormatFourCC;
40 using android::gralloc4::decodePixelFormatModifier;
41 using android::hardware::graphics::mapper::V4_0::IMapper;
42 using android::hardware::graphics::mapper::V4_0::Error;
43 using MapperMetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
44 
45 #define UNUSED(x) ((void)x)
46 #define SZ_4k 0x1000
47 
48 // TODO(b/191912915): This is a non-thread safe version of the validate function
49 // from mapper-4.0-impl library. Ideally this should be decoupled from `impl`
50 // libraries and should depend upon HAL (and it's extension) to call into
51 // Gralloc.
mali_gralloc_reference_validate(buffer_handle_t handle)52 int mali_gralloc_reference_validate(buffer_handle_t handle) {
53 	return private_handle_t::validate(handle);
54 }
55 
convertNativeHandleToPrivateHandle(buffer_handle_t handle)56 const private_handle_t * convertNativeHandleToPrivateHandle(buffer_handle_t handle) {
57 	if (mali_gralloc_reference_validate(handle) < 0)
58 		return nullptr;
59 
60 	return static_cast<const private_handle_t *>(handle);
61 }
62 
get_mapper()63 android::sp<IMapper> get_mapper() {
64 	static android::sp<IMapper> mapper = []() {
65 		auto mapper = IMapper::getService();
66 		if (!mapper) {
67 			ALOGE("Failed to get mapper service");
68 		}
69 		return mapper;
70 	}();
71 
72 	return mapper;
73 }
74 
get_video_metadata_fd(buffer_handle_t)75 int VendorGraphicBufferMeta::get_video_metadata_fd(buffer_handle_t /*hnd*/)
76 {
77 	ALOGE("%s function is obsolete and should not be used", __FUNCTION__);
78 	__builtin_trap();
79 }
80 
get_dataspace(buffer_handle_t hnd)81 int VendorGraphicBufferMeta::get_dataspace(buffer_handle_t hnd)
82 {
83 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
84 	if (!handle) {
85 		return -EINVAL;
86 	}
87 
88 	Error error = Error::NONE;
89 	Dataspace dataspace;
90 	get_mapper()->get(handle, android::gralloc4::MetadataType_Dataspace,
91 	                  [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) {
92 	                  	error = tmpError;
93 	                  	if (error != Error::NONE) {
94 	                  		return;
95 	                  	}
96 	                  	error = static_cast<Error>(decodeDataspace(tmpVec, &dataspace));
97 	                  });
98 
99 
100 	if (error != Error::NONE) {
101 		ALOGE("Failed to get datasapce");
102 		return -EINVAL;
103 	}
104 
105 	return static_cast<int>(dataspace);
106 }
107 
set_dataspace(buffer_handle_t hnd,android_dataspace_t dataspace)108 int VendorGraphicBufferMeta::set_dataspace(buffer_handle_t hnd, android_dataspace_t dataspace)
109 {
110 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
111 	if (!handle) {
112 		return -EINVAL;
113 	}
114 
115 	Error error = Error::NONE;
116 	android::hardware::hidl_vec<uint8_t> vec;
117 	error = static_cast<Error>(encodeDataspace(static_cast<Dataspace>(dataspace), &vec));
118 	if (error != Error::NONE) {
119 		ALOGE("Error encoding dataspace");
120 		return -EINVAL;
121 	}
122 	error = get_mapper()->set(handle, android::gralloc4::MetadataType_Dataspace, vec);
123 
124 	if (error != Error::NONE) {
125 		ALOGE("Failed to set datasapce");
126 		return -EINVAL;
127 	}
128 
129 	return 0;
130 }
131 
is_afbc(buffer_handle_t hnd)132 int VendorGraphicBufferMeta::is_afbc(buffer_handle_t hnd)
133 {
134 	const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
135 
136 	if (!gralloc_hnd)
137 		return 0;
138 
139 	if (gralloc_hnd->alloc_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK)
140 		return 1;
141 
142 	return 0;
143 }
144 
is_sbwc(buffer_handle_t hnd)145 int VendorGraphicBufferMeta::is_sbwc(buffer_handle_t hnd)
146 {
147 	return is_sbwc_format(VendorGraphicBufferMeta::get_internal_format(hnd));
148 }
149 
150 #define GRALLOC_META_GETTER(__type__, __name__, __member__) \
151 __type__ VendorGraphicBufferMeta::get_##__name__(buffer_handle_t hnd) \
152 { \
153 	const private_handle_t *gralloc_hnd = static_cast<const private_handle_t *>(hnd); \
154 	if (!gralloc_hnd) return 0; \
155 	return gralloc_hnd->__member__; \
156 } \
157 
158 
get_format(buffer_handle_t hnd)159 uint32_t VendorGraphicBufferMeta::get_format(buffer_handle_t hnd)
160 {
161 	const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
162 
163 	if (!gralloc_hnd)
164 		return 0;
165 
166 	return static_cast<uint32_t>(gralloc_hnd->alloc_format);
167 }
168 
get_internal_format(buffer_handle_t hnd)169 uint64_t VendorGraphicBufferMeta::get_internal_format(buffer_handle_t hnd)
170 {
171 	const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);;
172 
173 	if (!gralloc_hnd)
174 		return 0;
175 
176 	return static_cast<uint64_t>(gralloc_hnd->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK);
177 }
178 
179 GRALLOC_META_GETTER(uint64_t, frameworkFormat, req_format);
180 
181 GRALLOC_META_GETTER(int, width, width);
182 GRALLOC_META_GETTER(int, height, height);
183 GRALLOC_META_GETTER(uint32_t, stride, stride);
184 GRALLOC_META_GETTER(uint32_t, stride_in_bytes, plane_info[0].byte_stride);
185 GRALLOC_META_GETTER(uint32_t, vstride, plane_info[0].alloc_height);
186 
187 GRALLOC_META_GETTER(uint64_t, producer_usage, producer_usage);
188 GRALLOC_META_GETTER(uint64_t, consumer_usage, consumer_usage);
189 
190 GRALLOC_META_GETTER(uint64_t, flags, flags);
191 
192 
get_fd(buffer_handle_t hnd,int num)193 int VendorGraphicBufferMeta::get_fd(buffer_handle_t hnd, int num)
194 {
195 	const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
196 
197 	if (!gralloc_hnd)
198 		return -1;
199 
200 	if (num > 2)
201 		return -1;
202 
203 	return gralloc_hnd->fds[num];
204 }
205 
get_size(buffer_handle_t hnd,int num)206 int VendorGraphicBufferMeta::get_size(buffer_handle_t hnd, int num)
207 {
208 	const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
209 
210 	if (!gralloc_hnd)
211 		return 0;
212 
213 	if (num > 2)
214 		return 0;
215 
216 	return gralloc_hnd->alloc_sizes[num];
217 }
218 
219 
get_usage(buffer_handle_t hnd)220 uint64_t VendorGraphicBufferMeta::get_usage(buffer_handle_t hnd)
221 {
222 	const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(hnd);
223 
224 	if (!gralloc_hnd)
225 		return 0;
226 
227 	return gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage;
228 }
229 
decodePointer(const android::hardware::hidl_vec<uint8_t> & tmpVec)230 void* decodePointer(const android::hardware::hidl_vec<uint8_t>& tmpVec) {
231 	constexpr uint8_t kPtrSize = sizeof(void*);
232 	assert(tmpVec.size() == kPtrSize);
233 
234 	void* data_ptr;
235 	std::memcpy(&data_ptr, tmpVec.data(), kPtrSize);
236 
237 	return data_ptr;
238 }
239 
get_video_metadata(buffer_handle_t hnd)240 void* VendorGraphicBufferMeta::get_video_metadata(buffer_handle_t hnd)
241 {
242 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
243 	if (!handle) {
244 		return nullptr;
245 	}
246 
247 	MapperMetadataType metadata_type{
248 		.name = ::pixel::graphics::kPixelMetadataTypeName,
249 		.value = static_cast<int64_t>(::pixel::graphics::MetadataType::VIDEO_HDR),
250 	};
251 
252 	Error error = Error::NONE;
253 	void* output = nullptr;
254 
255 	get_mapper()->get(handle, metadata_type,
256 	                  [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) {
257 	                  	error = tmpError;
258 	                  	if (error != Error::NONE) {
259 	                  		return;
260 	                  	}
261 	                  	output = decodePointer(tmpVec);
262 	                  });
263 
264 
265 	if (error != Error::NONE) {
266 		ALOGE("Failed to get video HDR metadata");
267 		return nullptr;
268 	}
269 
270 	return output;
271 }
272 
get_video_metadata_roiinfo(buffer_handle_t hnd)273 void* VendorGraphicBufferMeta::get_video_metadata_roiinfo(buffer_handle_t hnd)
274 {
275 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
276 	if (!handle) {
277 		return nullptr;
278 	}
279 
280 	MapperMetadataType metadata_type{
281 		.name = ::pixel::graphics::kPixelMetadataTypeName,
282 		.value = static_cast<int64_t>(::pixel::graphics::MetadataType::VIDEO_ROI),
283 	};
284 
285 	Error error = Error::NONE;
286 	void* output = nullptr;
287 
288 	get_mapper()->get(handle, metadata_type,
289 	                  [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) {
290 	                  	error = tmpError;
291 	                  	if (error != Error::NONE) {
292 	                  		return;
293 	                  	}
294 	                  	output = decodePointer(tmpVec);
295 	                  });
296 
297 
298 	if (error != Error::NONE) {
299 		ALOGE("Failed to get video HDR metadata");
300 		return nullptr;
301 	}
302 
303 	return output;
304 }
305 
get_format_fourcc(buffer_handle_t hnd)306 uint32_t VendorGraphicBufferMeta::get_format_fourcc(buffer_handle_t hnd) {
307 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
308 	if (!handle) {
309 		return DRM_FORMAT_INVALID;
310 	}
311 
312 	Error error = Error::NONE;
313 	uint32_t fourcc;
314 	get_mapper()->get(handle, android::gralloc4::MetadataType_PixelFormatFourCC,
315 	                  [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) {
316 	                  	error = tmpError;
317 	                  	if (error != Error::NONE) {
318 	                  		return;
319 	                  	}
320 	                  	error = static_cast<Error>(decodePixelFormatFourCC(tmpVec, &fourcc));
321 	                  });
322 
323 
324 	if (error != Error::NONE) {
325 		ALOGE("Failed to get fourcc");
326 		return DRM_FORMAT_INVALID;
327 	}
328 
329 	return fourcc;
330 }
331 
get_format_modifier(buffer_handle_t hnd)332 uint64_t VendorGraphicBufferMeta::get_format_modifier(buffer_handle_t hnd) {
333 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
334 	if (!handle) {
335 		return DRM_FORMAT_MOD_INVALID;
336 	}
337 
338 	Error error = Error::NONE;
339 	uint64_t modifier;
340 	get_mapper()->get(handle, android::gralloc4::MetadataType_PixelFormatModifier,
341 	                  [&](const auto& tmpError, const android::hardware::hidl_vec<uint8_t>& tmpVec) {
342 	                  	error = tmpError;
343 	                  	if (error != Error::NONE) {
344 	                  		return;
345 	                  	}
346 	                  	error = static_cast<Error>(decodePixelFormatModifier(tmpVec, &modifier));
347 	                  });
348 
349 
350 	if (error != Error::NONE) {
351 		ALOGE("Failed to get format modifier");
352 		return DRM_FORMAT_MOD_INVALID;
353 	}
354 
355 	return modifier;
356 }
357 
init(const buffer_handle_t handle)358 void VendorGraphicBufferMeta::init(const buffer_handle_t handle)
359 {
360 	const auto *gralloc_hnd = convertNativeHandleToPrivateHandle(handle);
361 
362 	if (!gralloc_hnd)
363 		return;
364 
365 	fd = gralloc_hnd->fds[0];
366 	fd1 = gralloc_hnd->fds[1];
367 	fd2 = gralloc_hnd->fds[2];
368 
369 	size   = gralloc_hnd->alloc_sizes[0];
370 	size1  = gralloc_hnd->alloc_sizes[1];
371 	size2  = gralloc_hnd->alloc_sizes[2];
372 
373 	offsets[0] = gralloc_hnd->plane_info[0].offset;
374 	offsets[1] = gralloc_hnd->plane_info[1].offset;
375 	offsets[2] = gralloc_hnd->plane_info[2].offset;
376 
377 	uint64_t usage = gralloc_hnd->producer_usage | gralloc_hnd->consumer_usage;
378 	if (usage & VendorGraphicBufferUsage::VIDEO_PRIVATE_DATA) {
379 		switch (gralloc_hnd->get_share_attr_fd_index()) {
380 		case 1:
381 			size1 = gralloc_hnd->attr_size;
382 			break;
383 		case 2:
384 			size2 = gralloc_hnd->attr_size;
385 			break;
386 		}
387 	}
388 
389 	internal_format = gralloc_hnd->alloc_format;
390 	frameworkFormat = gralloc_hnd->req_format;
391 
392 	width  = gralloc_hnd->width;
393 	height = gralloc_hnd->height;
394 	stride = gralloc_hnd->stride;
395 	vstride = gralloc_hnd->plane_info[0].alloc_height;
396 
397 	producer_usage = gralloc_hnd->producer_usage;
398 	consumer_usage = gralloc_hnd->consumer_usage;
399 
400 	flags = gralloc_hnd->flags;
401 
402 	unique_id = gralloc_hnd->backing_store_id;
403 }
404 
import_buffer(buffer_handle_t hnd)405 buffer_handle_t VendorGraphicBufferMeta::import_buffer(buffer_handle_t hnd)
406 {
407 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
408 	if (!handle) {
409 		return nullptr;
410 	}
411 
412 	native_handle_t* bufferHandle = nullptr;
413 	Error error = Error::NONE;
414 	get_mapper()->importBuffer(handle, [&](const auto& tmpError, const auto& tmpBuffer) {
415 				error = tmpError;
416 				if (error != Error::NONE) {
417 					return;
418 				}
419 				bufferHandle = static_cast<native_handle_t*>(tmpBuffer);
420 			});
421 
422 	if (error != Error::NONE) {
423 		ALOGE("[%s] Unable to import buffer", __FUNCTION__);
424 		return nullptr;
425 	}
426 
427 	return bufferHandle;
428 }
429 
free_buffer(buffer_handle_t hnd)430 int VendorGraphicBufferMeta::free_buffer(buffer_handle_t hnd)
431 {
432 	native_handle_t* handle = const_cast<native_handle_t*>(hnd);
433 	if (!handle) {
434 		return -EINVAL;
435 	}
436 	Error error = get_mapper()->freeBuffer(handle);
437 	if (error != Error::NONE) {
438 		ALOGE("[%s] Failed to free buffer", __FUNCTION__);
439 		return -EINVAL;
440 	}
441 	return 0;
442 }
443 
VendorGraphicBufferMeta(buffer_handle_t handle)444 VendorGraphicBufferMeta::VendorGraphicBufferMeta(buffer_handle_t handle)
445 {
446 	init(handle);
447 }
448 
449