blob: 2c897cf4ff15d13775cc0cb83f71fb35ab79a904 [file] [log] [blame]
Marissa Wall87c8ba72019-06-20 14:20:52 -07001/*
2 * Copyright 2019 The Android Open Source Project
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 "Gralloc4"
18
19#include <hidl/ServiceManagement.h>
20#include <hwbinder/IPCThreadState.h>
21#include <ui/Gralloc4.h>
22
23#include <inttypes.h>
24#include <log/log.h>
25#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wzero-length-array"
27#include <sync/sync.h>
28#pragma clang diagnostic pop
29
Marissa Wall22b2de12019-12-02 18:11:43 -080030using aidl::android::hardware::graphics::common::ExtendableType;
31using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
32using aidl::android::hardware::graphics::common::StandardMetadataType;
33using android::hardware::hidl_vec;
Marissa Wall87c8ba72019-06-20 14:20:52 -070034using android::hardware::graphics::allocator::V4_0::IAllocator;
35using android::hardware::graphics::common::V1_2::BufferUsage;
36using android::hardware::graphics::mapper::V4_0::BufferDescriptor;
37using android::hardware::graphics::mapper::V4_0::Error;
38using android::hardware::graphics::mapper::V4_0::IMapper;
Marissa Wall22b2de12019-12-02 18:11:43 -080039using BufferDump = android::hardware::graphics::mapper::V4_0::IMapper::BufferDump;
40using MetadataDump = android::hardware::graphics::mapper::V4_0::IMapper::MetadataDump;
41using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
42using MetadataTypeDescription =
43 android::hardware::graphics::mapper::V4_0::IMapper::MetadataTypeDescription;
Marissa Wall87c8ba72019-06-20 14:20:52 -070044
45namespace android {
46
47namespace {
48
49static constexpr Error kTransactionError = Error::NO_RESOURCES;
50
51uint64_t getValidUsageBits() {
52 static const uint64_t validUsageBits = []() -> uint64_t {
53 uint64_t bits = 0;
54 for (const auto bit :
55 hardware::hidl_enum_range<hardware::graphics::common::V1_2::BufferUsage>()) {
56 bits = bits | bit;
57 }
58 return bits;
59 }();
60 return validUsageBits;
61}
62
63static inline IMapper::Rect sGralloc4Rect(const Rect& rect) {
64 IMapper::Rect outRect{};
65 outRect.left = rect.left;
66 outRect.top = rect.top;
67 outRect.width = rect.width();
68 outRect.height = rect.height();
69 return outRect;
70}
Marissa Wall22b2de12019-12-02 18:11:43 -080071static inline void sBufferDescriptorInfo(std::string name, uint32_t width, uint32_t height,
72 PixelFormat format, uint32_t layerCount, uint64_t usage,
Marissa Wall87c8ba72019-06-20 14:20:52 -070073 IMapper::BufferDescriptorInfo* outDescriptorInfo) {
Marissa Wall22b2de12019-12-02 18:11:43 -080074 outDescriptorInfo->name = name;
Marissa Wall87c8ba72019-06-20 14:20:52 -070075 outDescriptorInfo->width = width;
76 outDescriptorInfo->height = height;
77 outDescriptorInfo->layerCount = layerCount;
78 outDescriptorInfo->format = static_cast<hardware::graphics::common::V1_2::PixelFormat>(format);
79 outDescriptorInfo->usage = usage;
80}
81
82} // anonymous namespace
83
84void Gralloc4Mapper::preload() {
85 android::hardware::preloadPassthroughService<IMapper>();
86}
87
88Gralloc4Mapper::Gralloc4Mapper() {
89 mMapper = IMapper::getService();
90 if (mMapper == nullptr) {
91 ALOGI("mapper 4.x is not supported");
92 return;
93 }
94 if (mMapper->isRemote()) {
95 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
96 }
97}
98
99bool Gralloc4Mapper::isLoaded() const {
100 return mMapper != nullptr;
101}
102
103status_t Gralloc4Mapper::validateBufferDescriptorInfo(
104 IMapper::BufferDescriptorInfo* descriptorInfo) const {
105 uint64_t validUsageBits = getValidUsageBits();
106
107 if (descriptorInfo->usage & ~validUsageBits) {
108 ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
109 descriptorInfo->usage & ~validUsageBits);
110 return BAD_VALUE;
111 }
112 return NO_ERROR;
113}
114
115status_t Gralloc4Mapper::createDescriptor(void* bufferDescriptorInfo,
116 void* outBufferDescriptor) const {
117 IMapper::BufferDescriptorInfo* descriptorInfo =
118 static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo);
119 BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor);
120
121 status_t status = validateBufferDescriptorInfo(descriptorInfo);
122 if (status != NO_ERROR) {
123 return status;
124 }
125
126 Error error;
127 auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) {
128 error = tmpError;
129 if (error != Error::NONE) {
130 return;
131 }
132 *outDescriptor = tmpDescriptor;
133 };
134
135 hardware::Return<void> ret = mMapper->createDescriptor(*descriptorInfo, hidl_cb);
136
137 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
138}
139
140status_t Gralloc4Mapper::importBuffer(const hardware::hidl_handle& rawHandle,
141 buffer_handle_t* outBufferHandle) const {
142 Error error;
143 auto ret = mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
144 error = tmpError;
145 if (error != Error::NONE) {
146 return;
147 }
148 *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer);
149 });
150
151 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
152}
153
154void Gralloc4Mapper::freeBuffer(buffer_handle_t bufferHandle) const {
155 auto buffer = const_cast<native_handle_t*>(bufferHandle);
156 auto ret = mMapper->freeBuffer(buffer);
157
158 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
159 ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", buffer, error);
160}
161
162status_t Gralloc4Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
Marissa Wall22b2de12019-12-02 18:11:43 -0800163 uint32_t height, PixelFormat format,
Marissa Wall87c8ba72019-06-20 14:20:52 -0700164 uint32_t layerCount, uint64_t usage,
165 uint32_t stride) const {
166 IMapper::BufferDescriptorInfo descriptorInfo;
Marissa Wall22b2de12019-12-02 18:11:43 -0800167 sBufferDescriptorInfo("validateBufferSize", width, height, format, layerCount, usage,
168 &descriptorInfo);
Marissa Wall87c8ba72019-06-20 14:20:52 -0700169
170 auto buffer = const_cast<native_handle_t*>(bufferHandle);
171 auto ret = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
172
173 return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError);
174}
175
176void Gralloc4Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
177 uint32_t* outNumInts) const {
178 *outNumFds = uint32_t(bufferHandle->numFds);
179 *outNumInts = uint32_t(bufferHandle->numInts);
180
181 Error error;
182 auto buffer = const_cast<native_handle_t*>(bufferHandle);
183 auto ret = mMapper->getTransportSize(buffer,
184 [&](const auto& tmpError, const auto& tmpNumFds,
185 const auto& tmpNumInts) {
186 error = tmpError;
187 if (error != Error::NONE) {
188 return;
189 }
190 *outNumFds = tmpNumFds;
191 *outNumInts = tmpNumInts;
192 });
193
194 error = (ret.isOk()) ? error : kTransactionError;
195
196 ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error);
197}
198
199status_t Gralloc4Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
200 int acquireFence, void** outData, int32_t* outBytesPerPixel,
201 int32_t* outBytesPerStride) const {
Marissa Wall22b2de12019-12-02 18:11:43 -0800202 std::vector<ui::PlaneLayout> planeLayouts;
203 status_t err = getPlaneLayouts(bufferHandle, &planeLayouts);
204
205 if (err != NO_ERROR && !planeLayouts.empty()) {
206 if (outBytesPerPixel) {
207 int32_t bitsPerPixel = planeLayouts.front().sampleIncrementInBits;
208 for (const auto& planeLayout : planeLayouts) {
209 if (bitsPerPixel != planeLayout.sampleIncrementInBits) {
210 bitsPerPixel = -1;
211 }
212 }
213 if (bitsPerPixel >= 0 && bitsPerPixel % 8 == 0) {
214 *outBytesPerPixel = bitsPerPixel / 8;
215 } else {
216 *outBytesPerPixel = -1;
217 }
218 }
219 if (outBytesPerStride) {
220 int32_t bytesPerStride = planeLayouts.front().strideInBytes;
221 for (const auto& planeLayout : planeLayouts) {
222 if (bytesPerStride != planeLayout.strideInBytes) {
223 bytesPerStride = -1;
224 }
225 }
226 if (bytesPerStride >= 0) {
227 *outBytesPerStride = bytesPerStride;
228 } else {
229 *outBytesPerStride = -1;
230 }
231 }
Marissa Wall20611c62019-11-05 15:06:24 -0800232 }
233
Marissa Wall87c8ba72019-06-20 14:20:52 -0700234 auto buffer = const_cast<native_handle_t*>(bufferHandle);
235
236 IMapper::Rect accessRegion = sGralloc4Rect(bounds);
237
238 // put acquireFence in a hidl_handle
239 hardware::hidl_handle acquireFenceHandle;
240 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
241 if (acquireFence >= 0) {
242 auto h = native_handle_init(acquireFenceStorage, 1, 0);
243 h->data[0] = acquireFence;
244 acquireFenceHandle = h;
245 }
246
247 Error error;
248 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
Marissa Wall20611c62019-11-05 15:06:24 -0800249 [&](const auto& tmpError, const auto& tmpData) {
Marissa Wall87c8ba72019-06-20 14:20:52 -0700250 error = tmpError;
251 if (error != Error::NONE) {
252 return;
253 }
254 *outData = tmpData;
Marissa Wall87c8ba72019-06-20 14:20:52 -0700255 });
256
257 // we own acquireFence even on errors
258 if (acquireFence >= 0) {
259 close(acquireFence);
260 }
261
262 error = (ret.isOk()) ? error : kTransactionError;
263
264 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
265
266 return static_cast<status_t>(error);
267}
268
Marissa Wall22b2de12019-12-02 18:11:43 -0800269status_t Gralloc4Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
270 int acquireFence, android_ycbcr* outYcbcr) const {
271 if (!outYcbcr) {
272 return BAD_VALUE;
273 }
274
275 std::vector<ui::PlaneLayout> planeLayouts;
276 status_t error = getPlaneLayouts(bufferHandle, &planeLayouts);
277 if (error != NO_ERROR) {
278 return error;
279 }
280
281 void* data = nullptr;
282 error = lock(bufferHandle, usage, bounds, acquireFence, &data, nullptr, nullptr);
283 if (error != NO_ERROR) {
284 return error;
285 }
286
287 android_ycbcr ycbcr;
288
289 ycbcr.y = nullptr;
290 ycbcr.cb = nullptr;
291 ycbcr.cr = nullptr;
292 ycbcr.ystride = 0;
293 ycbcr.cstride = 0;
294 ycbcr.chroma_step = 0;
295
296 for (const auto& planeLayout : planeLayouts) {
297 for (const auto& planeLayoutComponent : planeLayout.components) {
298 if (!gralloc4::isStandardPlaneLayoutComponentType(planeLayoutComponent.type)) {
299 continue;
300 }
301 if (0 != planeLayoutComponent.offsetInBits % 8) {
302 unlock(bufferHandle);
303 return BAD_VALUE;
304 }
305
306 uint8_t* tmpData = static_cast<uint8_t*>(data) + planeLayout.offsetInBytes +
307 (planeLayoutComponent.offsetInBits / 8);
308 uint64_t sampleIncrementInBytes;
309
310 auto type = static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value);
311 switch (type) {
312 case PlaneLayoutComponentType::Y:
313 if ((ycbcr.y != nullptr) || (planeLayoutComponent.sizeInBits != 8) ||
314 (planeLayout.sampleIncrementInBits != 8)) {
315 unlock(bufferHandle);
316 return BAD_VALUE;
317 }
318 ycbcr.y = tmpData;
319 ycbcr.ystride = planeLayout.strideInBytes;
320 break;
321
322 case PlaneLayoutComponentType::CB:
323 case PlaneLayoutComponentType::CR:
324 if (planeLayout.sampleIncrementInBits % 8 != 0) {
325 unlock(bufferHandle);
326 return BAD_VALUE;
327 }
328
329 sampleIncrementInBytes = planeLayout.sampleIncrementInBits / 8;
330 if ((sampleIncrementInBytes != 1) && (sampleIncrementInBytes != 2)) {
331 unlock(bufferHandle);
332 return BAD_VALUE;
333 }
334
335 if (ycbcr.cstride == 0 && ycbcr.chroma_step == 0) {
336 ycbcr.cstride = planeLayout.strideInBytes;
337 ycbcr.chroma_step = sampleIncrementInBytes;
338 } else {
339 if ((static_cast<int64_t>(ycbcr.cstride) != planeLayout.strideInBytes) ||
340 (ycbcr.chroma_step != sampleIncrementInBytes)) {
341 unlock(bufferHandle);
342 return BAD_VALUE;
343 }
344 }
345
346 if (type == PlaneLayoutComponentType::CB) {
347 if (ycbcr.cb != nullptr) {
348 unlock(bufferHandle);
349 return BAD_VALUE;
350 }
351 ycbcr.cb = tmpData;
352 } else {
353 if (ycbcr.cr != nullptr) {
354 unlock(bufferHandle);
355 return BAD_VALUE;
356 }
357 ycbcr.cr = tmpData;
358 }
359 break;
360 default:
361 break;
362 };
363 }
364 }
365
366 *outYcbcr = ycbcr;
Marissa Wall20611c62019-11-05 15:06:24 -0800367 return static_cast<status_t>(Error::UNSUPPORTED);
Marissa Wall87c8ba72019-06-20 14:20:52 -0700368}
369
370int Gralloc4Mapper::unlock(buffer_handle_t bufferHandle) const {
371 auto buffer = const_cast<native_handle_t*>(bufferHandle);
372
373 int releaseFence = -1;
374 Error error;
375 auto ret = mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
376 error = tmpError;
377 if (error != Error::NONE) {
378 return;
379 }
380
381 auto fenceHandle = tmpReleaseFence.getNativeHandle();
382 if (fenceHandle && fenceHandle->numFds == 1) {
383 int fd = dup(fenceHandle->data[0]);
384 if (fd >= 0) {
385 releaseFence = fd;
386 } else {
387 ALOGD("failed to dup unlock release fence");
388 sync_wait(fenceHandle->data[0], -1);
389 }
390 }
391 });
392
393 if (!ret.isOk()) {
394 error = kTransactionError;
395 }
396
397 if (error != Error::NONE) {
398 ALOGE("unlock(%p) failed with %d", buffer, error);
399 }
400
401 return releaseFence;
402}
403
Marissa Wall22b2de12019-12-02 18:11:43 -0800404status_t Gralloc4Mapper::isSupported(uint32_t width, uint32_t height, PixelFormat format,
Marissa Wall87c8ba72019-06-20 14:20:52 -0700405 uint32_t layerCount, uint64_t usage,
406 bool* outSupported) const {
407 IMapper::BufferDescriptorInfo descriptorInfo;
Marissa Wall22b2de12019-12-02 18:11:43 -0800408 sBufferDescriptorInfo("isSupported", width, height, format, layerCount, usage, &descriptorInfo);
Marissa Wall87c8ba72019-06-20 14:20:52 -0700409
410 Error error;
411 auto ret = mMapper->isSupported(descriptorInfo,
412 [&](const auto& tmpError, const auto& tmpSupported) {
413 error = tmpError;
414 if (error != Error::NONE) {
415 return;
416 }
417 if (outSupported) {
418 *outSupported = tmpSupported;
419 }
420 });
421
422 if (!ret.isOk()) {
423 error = kTransactionError;
424 }
425
426 if (error != Error::NONE) {
427 ALOGE("isSupported(%u, %u, %d, %u, ...) failed with %d", width, height, format, layerCount,
428 error);
429 }
430
431 return static_cast<status_t>(error);
432}
433
Marissa Wall22b2de12019-12-02 18:11:43 -0800434template <class T>
435status_t Gralloc4Mapper::get(buffer_handle_t bufferHandle, const MetadataType& metadataType,
436 DecodeFunction<T> decodeFunction, T* outMetadata) const {
437 if (!outMetadata) {
438 return BAD_VALUE;
439 }
440
441 hidl_vec<uint8_t> vec;
442 Error error;
443 auto ret = mMapper->get(const_cast<native_handle_t*>(bufferHandle), metadataType,
444 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
445 error = tmpError;
446 vec = tmpVec;
447 });
448
449 if (!ret.isOk()) {
450 error = kTransactionError;
451 }
452
453 if (error != Error::NONE) {
454 ALOGE("get(%s, %" PRIu64 ", ...) failed with %d", metadataType.name.c_str(),
455 metadataType.value, error);
456 return static_cast<status_t>(error);
457 }
458
459 return decodeFunction(vec, outMetadata);
460}
461
462status_t Gralloc4Mapper::getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) const {
463 return get(bufferHandle, gralloc4::MetadataType_BufferId, gralloc4::decodeBufferId,
464 outBufferId);
465}
466
467status_t Gralloc4Mapper::getName(buffer_handle_t bufferHandle, std::string* outName) const {
468 return get(bufferHandle, gralloc4::MetadataType_Name, gralloc4::decodeName, outName);
469}
470
471status_t Gralloc4Mapper::getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth) const {
472 return get(bufferHandle, gralloc4::MetadataType_Width, gralloc4::decodeWidth, outWidth);
473}
474
475status_t Gralloc4Mapper::getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight) const {
476 return get(bufferHandle, gralloc4::MetadataType_Height, gralloc4::decodeHeight, outHeight);
477}
478
479status_t Gralloc4Mapper::getLayerCount(buffer_handle_t bufferHandle,
480 uint64_t* outLayerCount) const {
481 return get(bufferHandle, gralloc4::MetadataType_LayerCount, gralloc4::decodeLayerCount,
482 outLayerCount);
483}
484
485status_t Gralloc4Mapper::getPixelFormatRequested(buffer_handle_t bufferHandle,
486 ui::PixelFormat* outPixelFormatRequested) const {
487 return get(bufferHandle, gralloc4::MetadataType_PixelFormatRequested,
488 gralloc4::decodePixelFormatRequested, outPixelFormatRequested);
489}
490
491status_t Gralloc4Mapper::getPixelFormatFourCC(buffer_handle_t bufferHandle,
492 uint32_t* outPixelFormatFourCC) const {
493 return get(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC,
494 gralloc4::decodePixelFormatFourCC, outPixelFormatFourCC);
495}
496
497status_t Gralloc4Mapper::getPixelFormatModifier(buffer_handle_t bufferHandle,
498 uint64_t* outPixelFormatModifier) const {
499 return get(bufferHandle, gralloc4::MetadataType_PixelFormatModifier,
500 gralloc4::decodePixelFormatModifier, outPixelFormatModifier);
501}
502
503status_t Gralloc4Mapper::getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage) const {
504 return get(bufferHandle, gralloc4::MetadataType_Usage, gralloc4::decodeUsage, outUsage);
505}
506
507status_t Gralloc4Mapper::getAllocationSize(buffer_handle_t bufferHandle,
508 uint64_t* outAllocationSize) const {
509 return get(bufferHandle, gralloc4::MetadataType_AllocationSize, gralloc4::decodeAllocationSize,
510 outAllocationSize);
511}
512
513status_t Gralloc4Mapper::getProtectedContent(buffer_handle_t bufferHandle,
514 uint64_t* outProtectedContent) const {
515 return get(bufferHandle, gralloc4::MetadataType_ProtectedContent,
516 gralloc4::decodeProtectedContent, outProtectedContent);
517}
518
519status_t Gralloc4Mapper::getCompression(buffer_handle_t bufferHandle,
520 ExtendableType* outCompression) const {
521 return get(bufferHandle, gralloc4::MetadataType_Compression, gralloc4::decodeCompression,
522 outCompression);
523}
524
525status_t Gralloc4Mapper::getCompression(buffer_handle_t bufferHandle,
526 ui::Compression* outCompression) const {
527 if (!outCompression) {
528 return BAD_VALUE;
529 }
530 ExtendableType compression;
531 status_t error = getCompression(bufferHandle, &compression);
532 if (error) {
533 return error;
534 }
535 if (!gralloc4::isStandardCompression(compression)) {
536 return BAD_TYPE;
537 }
538 *outCompression = gralloc4::getStandardCompressionValue(compression);
539 return NO_ERROR;
540}
541
542status_t Gralloc4Mapper::getInterlaced(buffer_handle_t bufferHandle,
543 ExtendableType* outInterlaced) const {
544 return get(bufferHandle, gralloc4::MetadataType_Interlaced, gralloc4::decodeInterlaced,
545 outInterlaced);
546}
547
548status_t Gralloc4Mapper::getInterlaced(buffer_handle_t bufferHandle,
549 ui::Interlaced* outInterlaced) const {
550 if (!outInterlaced) {
551 return BAD_VALUE;
552 }
553 ExtendableType interlaced;
554 status_t error = getInterlaced(bufferHandle, &interlaced);
555 if (error) {
556 return error;
557 }
558 if (!gralloc4::isStandardInterlaced(interlaced)) {
559 return BAD_TYPE;
560 }
561 *outInterlaced = gralloc4::getStandardInterlacedValue(interlaced);
562 return NO_ERROR;
563}
564
565status_t Gralloc4Mapper::getChromaSiting(buffer_handle_t bufferHandle,
566 ExtendableType* outChromaSiting) const {
567 return get(bufferHandle, gralloc4::MetadataType_ChromaSiting, gralloc4::decodeChromaSiting,
568 outChromaSiting);
569}
570
571status_t Gralloc4Mapper::getChromaSiting(buffer_handle_t bufferHandle,
572 ui::ChromaSiting* outChromaSiting) const {
573 if (!outChromaSiting) {
574 return BAD_VALUE;
575 }
576 ExtendableType chromaSiting;
577 status_t error = getChromaSiting(bufferHandle, &chromaSiting);
578 if (error) {
579 return error;
580 }
581 if (!gralloc4::isStandardChromaSiting(chromaSiting)) {
582 return BAD_TYPE;
583 }
584 *outChromaSiting = gralloc4::getStandardChromaSitingValue(chromaSiting);
585 return NO_ERROR;
586}
587
588status_t Gralloc4Mapper::getPlaneLayouts(buffer_handle_t bufferHandle,
589 std::vector<ui::PlaneLayout>* outPlaneLayouts) const {
590 return get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, gralloc4::decodePlaneLayouts,
591 outPlaneLayouts);
592}
593
594status_t Gralloc4Mapper::getDataspace(buffer_handle_t bufferHandle,
595 ui::Dataspace* outDataspace) const {
596 if (!outDataspace) {
597 return BAD_VALUE;
598 }
599 aidl::android::hardware::graphics::common::Dataspace dataspace;
600 status_t error = get(bufferHandle, gralloc4::MetadataType_Dataspace, gralloc4::decodeDataspace,
601 &dataspace);
602 if (error) {
603 return error;
604 }
605
606 // Gralloc4 uses stable AIDL dataspace but the rest of the system still uses HIDL dataspace
607 *outDataspace = static_cast<ui::Dataspace>(dataspace);
608 return NO_ERROR;
609}
610
611status_t Gralloc4Mapper::getBlendMode(buffer_handle_t bufferHandle,
612 ui::BlendMode* outBlendMode) const {
613 return get(bufferHandle, gralloc4::MetadataType_BlendMode, gralloc4::decodeBlendMode,
614 outBlendMode);
615}
616
617template <class T>
618status_t Gralloc4Mapper::getDefault(uint32_t width, uint32_t height, PixelFormat format,
619 uint32_t layerCount, uint64_t usage,
620 const MetadataType& metadataType,
621 DecodeFunction<T> decodeFunction, T* outMetadata) const {
622 if (!outMetadata) {
623 return BAD_VALUE;
624 }
625
626 IMapper::BufferDescriptorInfo descriptorInfo;
627 sBufferDescriptorInfo("getDefault", width, height, format, layerCount, usage, &descriptorInfo);
628
629 hidl_vec<uint8_t> vec;
630 Error error;
631 auto ret = mMapper->getFromBufferDescriptorInfo(descriptorInfo, metadataType,
632 [&](const auto& tmpError,
633 const hidl_vec<uint8_t>& tmpVec) {
634 error = tmpError;
635 vec = tmpVec;
636 });
637
638 if (!ret.isOk()) {
639 error = kTransactionError;
640 }
641
642 if (error != Error::NONE) {
643 ALOGE("getDefault(%s, %" PRIu64 ", ...) failed with %d", metadataType.name.c_str(),
644 metadataType.value, error);
645 return static_cast<status_t>(error);
646 }
647
648 return decodeFunction(vec, outMetadata);
649}
650
651status_t Gralloc4Mapper::getDefaultPixelFormatFourCC(uint32_t width, uint32_t height,
652 PixelFormat format, uint32_t layerCount,
653 uint64_t usage,
654 uint32_t* outPixelFormatFourCC) const {
655 return getDefault(width, height, format, layerCount, usage,
656 gralloc4::MetadataType_PixelFormatFourCC, gralloc4::decodePixelFormatFourCC,
657 outPixelFormatFourCC);
658}
659
660status_t Gralloc4Mapper::getDefaultPixelFormatModifier(uint32_t width, uint32_t height,
661 PixelFormat format, uint32_t layerCount,
662 uint64_t usage,
663 uint64_t* outPixelFormatModifier) const {
664 return getDefault(width, height, format, layerCount, usage,
665 gralloc4::MetadataType_PixelFormatModifier,
666 gralloc4::decodePixelFormatModifier, outPixelFormatModifier);
667}
668
669status_t Gralloc4Mapper::getDefaultAllocationSize(uint32_t width, uint32_t height,
670 PixelFormat format, uint32_t layerCount,
671 uint64_t usage,
672 uint64_t* outAllocationSize) const {
673 return getDefault(width, height, format, layerCount, usage,
674 gralloc4::MetadataType_AllocationSize, gralloc4::decodeAllocationSize,
675 outAllocationSize);
676}
677
678status_t Gralloc4Mapper::getDefaultProtectedContent(uint32_t width, uint32_t height,
679 PixelFormat format, uint32_t layerCount,
680 uint64_t usage,
681 uint64_t* outProtectedContent) const {
682 return getDefault(width, height, format, layerCount, usage,
683 gralloc4::MetadataType_ProtectedContent, gralloc4::decodeProtectedContent,
684 outProtectedContent);
685}
686
687status_t Gralloc4Mapper::getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format,
688 uint32_t layerCount, uint64_t usage,
689 ExtendableType* outCompression) const {
690 return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_Compression,
691 gralloc4::decodeCompression, outCompression);
692}
693
694status_t Gralloc4Mapper::getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format,
695 uint32_t layerCount, uint64_t usage,
696 ui::Compression* outCompression) const {
697 if (!outCompression) {
698 return BAD_VALUE;
699 }
700 ExtendableType compression;
701 status_t error = getDefaultCompression(width, height, format, layerCount, usage, &compression);
702 if (error) {
703 return error;
704 }
705 if (!gralloc4::isStandardCompression(compression)) {
706 return BAD_TYPE;
707 }
708 *outCompression = gralloc4::getStandardCompressionValue(compression);
709 return NO_ERROR;
710}
711
712status_t Gralloc4Mapper::getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format,
713 uint32_t layerCount, uint64_t usage,
714 ExtendableType* outInterlaced) const {
715 return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_Interlaced,
716 gralloc4::decodeInterlaced, outInterlaced);
717}
718
719status_t Gralloc4Mapper::getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format,
720 uint32_t layerCount, uint64_t usage,
721 ui::Interlaced* outInterlaced) const {
722 if (!outInterlaced) {
723 return BAD_VALUE;
724 }
725 ExtendableType interlaced;
726 status_t error = getDefaultInterlaced(width, height, format, layerCount, usage, &interlaced);
727 if (error) {
728 return error;
729 }
730 if (!gralloc4::isStandardInterlaced(interlaced)) {
731 return BAD_TYPE;
732 }
733 *outInterlaced = gralloc4::getStandardInterlacedValue(interlaced);
734 return NO_ERROR;
735}
736
737status_t Gralloc4Mapper::getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format,
738 uint32_t layerCount, uint64_t usage,
739 ExtendableType* outChromaSiting) const {
740 return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_ChromaSiting,
741 gralloc4::decodeChromaSiting, outChromaSiting);
742}
743
744status_t Gralloc4Mapper::getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format,
745 uint32_t layerCount, uint64_t usage,
746 ui::ChromaSiting* outChromaSiting) const {
747 if (!outChromaSiting) {
748 return BAD_VALUE;
749 }
750 ExtendableType chromaSiting;
751 status_t error =
752 getDefaultChromaSiting(width, height, format, layerCount, usage, &chromaSiting);
753 if (error) {
754 return error;
755 }
756 if (!gralloc4::isStandardChromaSiting(chromaSiting)) {
757 return BAD_TYPE;
758 }
759 *outChromaSiting = gralloc4::getStandardChromaSitingValue(chromaSiting);
760 return NO_ERROR;
761}
762
763status_t Gralloc4Mapper::getDefaultPlaneLayouts(
764 uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount, uint64_t usage,
765 std::vector<ui::PlaneLayout>* outPlaneLayouts) const {
766 return getDefault(width, height, format, layerCount, usage, gralloc4::MetadataType_PlaneLayouts,
767 gralloc4::decodePlaneLayouts, outPlaneLayouts);
768}
769
770std::vector<MetadataTypeDescription> Gralloc4Mapper::listSupportedMetadataTypes() const {
771 hidl_vec<MetadataTypeDescription> descriptions;
772 Error error;
773 auto ret = mMapper->listSupportedMetadataTypes(
774 [&](const auto& tmpError, const auto& tmpDescriptions) {
775 error = tmpError;
776 descriptions = tmpDescriptions;
777 });
778
779 if (!ret.isOk()) {
780 error = kTransactionError;
781 }
782
783 if (error != Error::NONE) {
784 ALOGE("listSupportedMetadataType() failed with %d", error);
785 return {};
786 }
787
788 return static_cast<std::vector<MetadataTypeDescription>>(descriptions);
789}
790
791template <class T>
792status_t Gralloc4Mapper::metadataDumpHelper(const BufferDump& bufferDump,
793 StandardMetadataType metadataType,
794 DecodeFunction<T> decodeFunction, T* outT) const {
795 const auto& metadataDump = bufferDump.metadataDump;
796
797 auto itr =
798 std::find_if(metadataDump.begin(), metadataDump.end(),
799 [&](const MetadataDump& tmpMetadataDump) {
800 if (!gralloc4::isStandardMetadataType(tmpMetadataDump.metadataType)) {
801 return false;
802 }
803 return metadataType ==
804 gralloc4::getStandardMetadataTypeValue(
805 tmpMetadataDump.metadataType);
806 });
807 if (itr == metadataDump.end()) {
808 return BAD_VALUE;
809 }
810
811 return decodeFunction(itr->metadata, outT);
812}
813
814status_t Gralloc4Mapper::bufferDumpHelper(const BufferDump& bufferDump, std::ostringstream* outDump,
815 uint64_t* outAllocationSize, bool less) const {
816 uint64_t bufferId;
817 std::string name;
818 uint64_t width;
819 uint64_t height;
820 uint64_t layerCount;
821 ui::PixelFormat pixelFormatRequested;
822 uint32_t pixelFormatFourCC;
823 uint64_t pixelFormatModifier;
824 uint64_t usage;
825 uint64_t allocationSize;
826 uint64_t protectedContent;
827 ExtendableType compression;
828 ExtendableType interlaced;
829 ExtendableType chromaSiting;
830 std::vector<ui::PlaneLayout> planeLayouts;
831
832 status_t error = metadataDumpHelper(bufferDump, StandardMetadataType::BUFFER_ID,
833 gralloc4::decodeBufferId, &bufferId);
834 if (error != NO_ERROR) {
835 return error;
836 }
837 error = metadataDumpHelper(bufferDump, StandardMetadataType::NAME, gralloc4::decodeName, &name);
838 if (error != NO_ERROR) {
839 return error;
840 }
841 error = metadataDumpHelper(bufferDump, StandardMetadataType::WIDTH, gralloc4::decodeWidth,
842 &width);
843 if (error != NO_ERROR) {
844 return error;
845 }
846 error = metadataDumpHelper(bufferDump, StandardMetadataType::HEIGHT, gralloc4::decodeHeight,
847 &height);
848 if (error != NO_ERROR) {
849 return error;
850 }
851 error = metadataDumpHelper(bufferDump, StandardMetadataType::LAYER_COUNT,
852 gralloc4::decodeLayerCount, &layerCount);
853 if (error != NO_ERROR) {
854 return error;
855 }
856 error = metadataDumpHelper(bufferDump, StandardMetadataType::PIXEL_FORMAT_REQUESTED,
857 gralloc4::decodePixelFormatRequested, &pixelFormatRequested);
858 if (error != NO_ERROR) {
859 return error;
860 }
861 error = metadataDumpHelper(bufferDump, StandardMetadataType::PIXEL_FORMAT_FOURCC,
862 gralloc4::decodePixelFormatFourCC, &pixelFormatFourCC);
863 if (error != NO_ERROR) {
864 return error;
865 }
866 error = metadataDumpHelper(bufferDump, StandardMetadataType::PIXEL_FORMAT_MODIFIER,
867 gralloc4::decodePixelFormatModifier, &pixelFormatModifier);
868 if (error != NO_ERROR) {
869 return error;
870 }
871 error = metadataDumpHelper(bufferDump, StandardMetadataType::USAGE, gralloc4::decodeUsage,
872 &usage);
873 if (error != NO_ERROR) {
874 return error;
875 }
876 error = metadataDumpHelper(bufferDump, StandardMetadataType::ALLOCATION_SIZE,
877 gralloc4::decodeAllocationSize, &allocationSize);
878 if (error != NO_ERROR) {
879 return error;
880 }
881 error = metadataDumpHelper(bufferDump, StandardMetadataType::PROTECTED_CONTENT,
882 gralloc4::decodeProtectedContent, &protectedContent);
883 if (error != NO_ERROR) {
884 return error;
885 }
886 error = metadataDumpHelper(bufferDump, StandardMetadataType::COMPRESSION,
887 gralloc4::decodeCompression, &compression);
888 if (error != NO_ERROR) {
889 return error;
890 }
891 error = metadataDumpHelper(bufferDump, StandardMetadataType::INTERLACED,
892 gralloc4::decodeInterlaced, &interlaced);
893 if (error != NO_ERROR) {
894 return error;
895 }
896 error = metadataDumpHelper(bufferDump, StandardMetadataType::CHROMA_SITING,
897 gralloc4::decodeChromaSiting, &chromaSiting);
898 if (error != NO_ERROR) {
899 return error;
900 }
901 error = metadataDumpHelper(bufferDump, StandardMetadataType::PLANE_LAYOUTS,
902 gralloc4::decodePlaneLayouts, &planeLayouts);
903 if (error != NO_ERROR) {
904 return error;
905 }
906
907 if (outAllocationSize) {
908 *outAllocationSize = allocationSize;
909 }
910 double allocationSizeKiB = static_cast<double>(allocationSize) / 1024;
911
912 *outDump << "+ name:" << name << ", id:" << bufferId << ", size:" << allocationSizeKiB
913 << "KiB, w/h:" << width << "x" << height << ", usage: 0x" << std::hex << usage
914 << std::dec << ", req fmt:" << static_cast<int32_t>(pixelFormatRequested)
915 << ", fourcc/mod:" << pixelFormatFourCC << "/" << pixelFormatModifier
916 << ", compressed: ";
917
918 if (less) {
919 bool isCompressed = !gralloc4::isStandardCompression(compression) ||
920 (gralloc4::getStandardCompressionValue(compression) != ui::Compression::NONE);
921 *outDump << std::boolalpha << isCompressed << "\n";
922 } else {
923 *outDump << gralloc4::getCompressionName(compression) << "\n";
924 }
925
926 bool firstPlane = true;
927 for (const auto& planeLayout : planeLayouts) {
928 if (firstPlane) {
929 firstPlane = false;
930 *outDump << "\tplanes: ";
931 } else {
932 *outDump << "\t ";
933 }
934
935 for (size_t i = 0; i < planeLayout.components.size(); i++) {
936 const auto& planeLayoutComponent = planeLayout.components[i];
937 *outDump << gralloc4::getPlaneLayoutComponentTypeName(planeLayoutComponent.type);
938 if (i < planeLayout.components.size() - 1) {
939 *outDump << "/";
940 } else {
941 *outDump << ":\t";
942 }
943 }
944 *outDump << " w/h:" << planeLayout.widthInSamples << "x" << planeLayout.heightInSamples
945 << ", stride:" << planeLayout.strideInBytes
946 << " bytes, size:" << planeLayout.totalSizeInBytes;
947 if (!less) {
948 *outDump << ", inc:" << planeLayout.sampleIncrementInBits
949 << " bits, subsampling w/h:" << planeLayout.horizontalSubsampling << "x"
950 << planeLayout.verticalSubsampling;
951 }
952 *outDump << "\n";
953 }
954
955 if (!less) {
956 *outDump << "\tlayer cnt: " << layerCount << ", protected content: " << protectedContent
957 << ", interlaced: " << gralloc4::getInterlacedName(interlaced)
958 << ", chroma siting:" << gralloc4::getChromaSitingName(chromaSiting) << "\n";
959 }
960
961 return NO_ERROR;
962}
963
964std::string Gralloc4Mapper::dumpBuffer(buffer_handle_t bufferHandle, bool less) const {
965 auto buffer = const_cast<native_handle_t*>(bufferHandle);
966
967 BufferDump bufferDump;
968 Error error;
969 auto ret = mMapper->dumpBuffer(buffer, [&](const auto& tmpError, const auto& tmpBufferDump) {
970 error = tmpError;
971 bufferDump = tmpBufferDump;
972 });
973
974 if (!ret.isOk()) {
975 error = kTransactionError;
976 }
977
978 if (error != Error::NONE) {
979 ALOGE("dumpBuffer() failed with %d", error);
980 return "";
981 }
982
983 std::ostringstream stream;
984 stream.precision(2);
985
986 status_t err = bufferDumpHelper(bufferDump, &stream, nullptr, less);
987 if (err != NO_ERROR) {
988 ALOGE("bufferDumpHelper() failed with %d", err);
989 return "";
990 }
991
992 return stream.str();
993}
994
995std::string Gralloc4Mapper::dumpBuffers(bool less) const {
996 hidl_vec<BufferDump> bufferDumps;
997 Error error;
998 auto ret = mMapper->dumpBuffers([&](const auto& tmpError, const auto& tmpBufferDump) {
999 error = tmpError;
1000 bufferDumps = tmpBufferDump;
1001 });
1002
1003 if (!ret.isOk()) {
1004 error = kTransactionError;
1005 }
1006
1007 if (error != Error::NONE) {
1008 ALOGE("dumpBuffer() failed with %d", error);
1009 return "";
1010 }
1011
1012 uint64_t totalAllocationSize = 0;
1013 std::ostringstream stream;
1014 stream.precision(2);
1015
1016 stream << "Imported gralloc buffers:\n";
1017
1018 for (const auto& bufferDump : bufferDumps) {
1019 uint64_t allocationSize = 0;
1020 status_t err = bufferDumpHelper(bufferDump, &stream, &allocationSize, less);
1021 if (err != NO_ERROR) {
1022 ALOGE("bufferDumpHelper() failed with %d", err);
1023 return "";
1024 }
1025 totalAllocationSize += allocationSize;
1026 }
1027
1028 double totalAllocationSizeKiB = static_cast<double>(totalAllocationSize) / 1024;
1029 stream << "Total imported by gralloc: " << totalAllocationSizeKiB << "KiB\n";
1030 return stream.str();
1031}
1032
Marissa Wall87c8ba72019-06-20 14:20:52 -07001033Gralloc4Allocator::Gralloc4Allocator(const Gralloc4Mapper& mapper) : mMapper(mapper) {
1034 mAllocator = IAllocator::getService();
1035 if (mAllocator == nullptr) {
1036 ALOGW("allocator 3.x is not supported");
1037 return;
1038 }
1039}
1040
1041bool Gralloc4Allocator::isLoaded() const {
1042 return mAllocator != nullptr;
1043}
1044
Marissa Wall22b2de12019-12-02 18:11:43 -08001045std::string Gralloc4Allocator::dumpDebugInfo(bool less) const {
1046 return mMapper.dumpBuffers(less);
Marissa Wall87c8ba72019-06-20 14:20:52 -07001047}
1048
Marissa Wall22b2de12019-12-02 18:11:43 -08001049status_t Gralloc4Allocator::allocate(std::string requestorName, uint32_t width, uint32_t height,
1050 android::PixelFormat format, uint32_t layerCount,
1051 uint64_t usage, uint32_t bufferCount, uint32_t* outStride,
1052 buffer_handle_t* outBufferHandles, bool importBuffers) const {
Marissa Wall87c8ba72019-06-20 14:20:52 -07001053 IMapper::BufferDescriptorInfo descriptorInfo;
Marissa Wall22b2de12019-12-02 18:11:43 -08001054 sBufferDescriptorInfo(requestorName, width, height, format, layerCount, usage, &descriptorInfo);
Marissa Wall87c8ba72019-06-20 14:20:52 -07001055
1056 BufferDescriptor descriptor;
1057 status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo),
1058 static_cast<void*>(&descriptor));
1059 if (error != NO_ERROR) {
1060 return error;
1061 }
1062
1063 auto ret = mAllocator->allocate(descriptor, bufferCount,
1064 [&](const auto& tmpError, const auto& tmpStride,
1065 const auto& tmpBuffers) {
1066 error = static_cast<status_t>(tmpError);
1067 if (tmpError != Error::NONE) {
1068 return;
1069 }
1070
Marissa Wallbfcf81f2019-11-27 10:36:29 -08001071 if (importBuffers) {
1072 for (uint32_t i = 0; i < bufferCount; i++) {
1073 error = mMapper.importBuffer(tmpBuffers[i],
1074 &outBufferHandles[i]);
1075 if (error != NO_ERROR) {
1076 for (uint32_t j = 0; j < i; j++) {
1077 mMapper.freeBuffer(outBufferHandles[j]);
1078 outBufferHandles[j] = nullptr;
1079 }
1080 return;
Marissa Wall87c8ba72019-06-20 14:20:52 -07001081 }
Marissa Wallbfcf81f2019-11-27 10:36:29 -08001082 }
1083 } else {
1084 for (uint32_t i = 0; i < bufferCount; i++) {
1085 outBufferHandles[i] = native_handle_clone(
1086 tmpBuffers[i].getNativeHandle());
1087 if (!outBufferHandles[i]) {
1088 for (uint32_t j = 0; j < i; j++) {
1089 auto buffer = const_cast<native_handle_t*>(
1090 outBufferHandles[j]);
1091 native_handle_close(buffer);
1092 native_handle_delete(buffer);
1093 outBufferHandles[j] = nullptr;
1094 }
1095 }
Marissa Wall87c8ba72019-06-20 14:20:52 -07001096 }
1097 }
1098 *outStride = tmpStride;
1099 });
1100
1101 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
1102 hardware::IPCThreadState::self()->flushCommands();
1103
1104 return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError);
1105}
1106
1107} // namespace android