1 /*
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 "VtsHalGraphicsMapperV4_0TargetTest"
18 
19 #include <unistd.h>
20 #include <chrono>
21 #include <thread>
22 #include <vector>
23 
24 #include <aidl/Vintf.h>
25 #include <aidl/android/hardware/graphics/allocator/AllocationError.h>
26 #include <aidl/android/hardware/graphics/allocator/AllocationResult.h>
27 #include <aidl/android/hardware/graphics/common/PixelFormat.h>
28 #include <aidl/android/hardware/graphics/common/PlaneLayoutComponentType.h>
29 #include <aidlcommonsupport/NativeHandle.h>
30 
31 #include <android-base/logging.h>
32 #include <android-base/properties.h>
33 #include <android-base/unique_fd.h>
34 #include <android/sync.h>
35 #include <gralloctypes/Gralloc4.h>
36 #include <gtest/gtest.h>
37 #include <hidl/GtestPrinter.h>
38 #include <hidl/ServiceManagement.h>
39 
40 #include <mapper-vts/4.0/MapperVts.h>
41 #include <system/graphics.h>
42 
43 namespace android {
44 namespace hardware {
45 namespace graphics {
46 namespace mapper {
47 namespace V4_0 {
48 namespace vts {
49 namespace {
50 
51 using ::android::base::unique_fd;
52 using android::hardware::graphics::common::V1_2::BufferUsage;
53 using android::hardware::graphics::common::V1_2::PixelFormat;
54 using Tolerance = ::android::hardware::graphics::mapper::V4_0::vts::Gralloc::Tolerance;
55 using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
56 using aidl::android::hardware::graphics::common::BlendMode;
57 using aidl::android::hardware::graphics::common::Cta861_3;
58 using aidl::android::hardware::graphics::common::Dataspace;
59 using aidl::android::hardware::graphics::common::ExtendableType;
60 using aidl::android::hardware::graphics::common::PlaneLayout;
61 using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
62 using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
63 using aidl::android::hardware::graphics::common::Smpte2086;
64 using aidl::android::hardware::graphics::common::StandardMetadataType;
65 
66 using DecodeFunction = std::function<void(const IMapper::BufferDescriptorInfo& descriptorInfo,
67                                           const hidl_vec<uint8_t>& vec)>;
68 
69 struct YCbCr {
70     android_ycbcr yCbCr;
71     int64_t horizontalSubSampling;
72     int64_t verticalSubSampling;
73 };
74 
75 class GraphicsMapperHidlTest
76     : public ::testing::TestWithParam<std::tuple<std::string, std::string>> {
77   protected:
SetUp()78     void SetUp() override {
79         ASSERT_NO_FATAL_FAILURE(mGralloc = std::make_unique<Gralloc>(std::get<0>(GetParam()),
80                                                                      std::get<1>(GetParam())));
81         ASSERT_TRUE(mGralloc->hasAllocator());
82         ASSERT_NE(nullptr, mGralloc->getMapper().get());
83 
84         mDummyDescriptorInfo.name = "dummy";
85         mDummyDescriptorInfo.width = 64;
86         mDummyDescriptorInfo.height = 64;
87         mDummyDescriptorInfo.layerCount = 1;
88         mDummyDescriptorInfo.format = PixelFormat::RGBA_8888;
89         mDummyDescriptorInfo.usage =
90                 static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
91         mDummyDescriptorInfo.reservedSize = 0;
92     }
93 
TearDown()94     void TearDown() override {}
95 
testGet(const IMapper::BufferDescriptorInfo & descriptorInfo,const MetadataType & metadataType,DecodeFunction decode)96     void testGet(const IMapper::BufferDescriptorInfo& descriptorInfo,
97                  const MetadataType& metadataType, DecodeFunction decode) {
98         const native_handle_t* bufferHandle = nullptr;
99         ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(descriptorInfo, true));
100 
101         hidl_vec<uint8_t> vec;
102         const auto result = mGralloc->get(bufferHandle, metadataType, &vec);
103 
104         if (metadataType == gralloc4::MetadataType_Smpte2094_10 && result == Error::UNSUPPORTED) {
105             GTEST_SKIP() << "getting metadata for Smpte2094-10 is unsupported";
106         }
107 
108         ASSERT_EQ(Error::NONE, result);
109 
110         ASSERT_NO_FATAL_FAILURE(decode(descriptorInfo, vec));
111     }
112 
testSet(const IMapper::BufferDescriptorInfo & descriptorInfo,const MetadataType & metadataType,const hidl_vec<uint8_t> & metadata,DecodeFunction decode)113     void testSet(const IMapper::BufferDescriptorInfo& descriptorInfo,
114                  const MetadataType& metadataType, const hidl_vec<uint8_t>& metadata,
115                  DecodeFunction decode) {
116         const native_handle_t* bufferHandle = nullptr;
117         ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(descriptorInfo, true));
118 
119         Error err = mGralloc->set(bufferHandle, metadataType, metadata);
120         if (err == Error::UNSUPPORTED) {
121             GTEST_SUCCEED() << "setting this metadata is unsupported";
122             return;
123         }
124         ASSERT_EQ(err, Error::NONE);
125 
126         hidl_vec<uint8_t> vec;
127         ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, metadataType, &vec));
128 
129         ASSERT_NO_FATAL_FAILURE(decode(descriptorInfo, vec));
130     }
131 
verifyRGBA8888PlaneLayouts(const std::vector<PlaneLayout> & planeLayouts)132     void verifyRGBA8888PlaneLayouts(const std::vector<PlaneLayout>& planeLayouts) {
133         ASSERT_EQ(1, planeLayouts.size());
134 
135         const auto& planeLayout = planeLayouts.front();
136 
137         ASSERT_EQ(4, planeLayout.components.size());
138 
139         int64_t offsetInBitsR = -1;
140         int64_t offsetInBitsG = -1;
141         int64_t offsetInBitsB = -1;
142         int64_t offsetInBitsA = -1;
143 
144         for (const auto& component : planeLayout.components) {
145             if (!gralloc4::isStandardPlaneLayoutComponentType(component.type)) {
146                 continue;
147             }
148             EXPECT_EQ(8, component.sizeInBits);
149             if (component.type.value == gralloc4::PlaneLayoutComponentType_R.value) {
150                 offsetInBitsR = component.offsetInBits;
151             }
152             if (component.type.value == gralloc4::PlaneLayoutComponentType_G.value) {
153                 offsetInBitsG = component.offsetInBits;
154             }
155             if (component.type.value == gralloc4::PlaneLayoutComponentType_B.value) {
156                 offsetInBitsB = component.offsetInBits;
157             }
158             if (component.type.value == gralloc4::PlaneLayoutComponentType_A.value) {
159                 offsetInBitsA = component.offsetInBits;
160             }
161         }
162 
163         EXPECT_EQ(0, offsetInBitsR);
164         EXPECT_EQ(8, offsetInBitsG);
165         EXPECT_EQ(16, offsetInBitsB);
166         EXPECT_EQ(24, offsetInBitsA);
167 
168         EXPECT_EQ(0, planeLayout.offsetInBytes);
169         EXPECT_EQ(32, planeLayout.sampleIncrementInBits);
170         // Skip testing stride because any stride is valid
171         EXPECT_EQ(mDummyDescriptorInfo.width, planeLayout.widthInSamples);
172         EXPECT_EQ(mDummyDescriptorInfo.height, planeLayout.heightInSamples);
173         EXPECT_LE(planeLayout.widthInSamples * planeLayout.heightInSamples * 4,
174                   planeLayout.totalSizeInBytes);
175         EXPECT_EQ(1, planeLayout.horizontalSubsampling);
176         EXPECT_EQ(1, planeLayout.verticalSubsampling);
177     }
178 
verifyBufferDump(const IMapper::BufferDump & bufferDump,const native_handle_t * bufferHandle=nullptr)179     void verifyBufferDump(const IMapper::BufferDump& bufferDump,
180                           const native_handle_t* bufferHandle = nullptr) {
181         std::set<StandardMetadataType> foundMetadataTypes;
182 
183         const std::vector<IMapper::MetadataDump> metadataDump = bufferDump.metadataDump;
184 
185         for (const auto& dump : metadataDump) {
186             const auto& metadataType = dump.metadataType;
187             const auto& metadata = dump.metadata;
188 
189             if (!gralloc4::isStandardMetadataType(metadataType)) {
190                 continue;
191             }
192 
193             StandardMetadataType type = gralloc4::getStandardMetadataTypeValue(metadataType);
194 
195             if (sRequiredMetadataTypes.find(type) == sRequiredMetadataTypes.end()) {
196                 continue;
197             }
198 
199             ASSERT_EQ(foundMetadataTypes.find(type), foundMetadataTypes.end());
200             foundMetadataTypes.insert(type);
201 
202             if (!bufferHandle) {
203                 continue;
204             }
205 
206             hidl_vec<uint8_t> metadataFromGet;
207             ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, metadataType, &metadataFromGet));
208 
209             ASSERT_EQ(metadataFromGet, metadata);
210         }
211 
212         EXPECT_EQ(sRequiredMetadataTypes, foundMetadataTypes);
213     }
214 
getAndroidYCbCr(const native_handle_t * bufferHandle,uint8_t * data,android_ycbcr * outYCbCr,int64_t * hSubsampling,int64_t * vSubsampling)215     void getAndroidYCbCr(const native_handle_t* bufferHandle, uint8_t* data,
216                          android_ycbcr* outYCbCr, int64_t* hSubsampling, int64_t* vSubsampling) {
217         hidl_vec<uint8_t> vec;
218         ASSERT_EQ(Error::NONE,
219                   mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
220         std::vector<PlaneLayout> planeLayouts;
221         ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
222 
223         outYCbCr->y = nullptr;
224         outYCbCr->cb = nullptr;
225         outYCbCr->cr = nullptr;
226         outYCbCr->ystride = 0;
227         outYCbCr->cstride = 0;
228         outYCbCr->chroma_step = 0;
229 
230         for (const auto& planeLayout : planeLayouts) {
231             for (const auto& planeLayoutComponent : planeLayout.components) {
232                 if (!gralloc4::isStandardPlaneLayoutComponentType(planeLayoutComponent.type)) {
233                     continue;
234                 }
235                 ASSERT_EQ(0, planeLayoutComponent.offsetInBits % 8);
236 
237                 uint8_t* tmpData = data + planeLayout.offsetInBytes +
238                                    bitsToBytes(planeLayoutComponent.offsetInBits);
239                 uint64_t sampleIncrementInBytes;
240 
241                 auto type = static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value);
242                 switch (type) {
243                     case PlaneLayoutComponentType::Y:
244                         ASSERT_EQ(nullptr, outYCbCr->y);
245                         ASSERT_EQ(8, planeLayoutComponent.sizeInBits);
246                         ASSERT_EQ(8, planeLayout.sampleIncrementInBits);
247                         outYCbCr->y = tmpData;
248                         outYCbCr->ystride = planeLayout.strideInBytes;
249                         break;
250 
251                     case PlaneLayoutComponentType::CB:
252                     case PlaneLayoutComponentType::CR:
253                         ASSERT_EQ(0, planeLayout.sampleIncrementInBits % 8);
254 
255                         sampleIncrementInBytes = planeLayout.sampleIncrementInBits / 8;
256                         ASSERT_TRUE(sampleIncrementInBytes == 1 || sampleIncrementInBytes == 2);
257 
258                         if (outYCbCr->cstride == 0 && outYCbCr->chroma_step == 0) {
259                             outYCbCr->cstride = planeLayout.strideInBytes;
260                             outYCbCr->chroma_step = sampleIncrementInBytes;
261                         } else {
262                             ASSERT_EQ(outYCbCr->cstride, planeLayout.strideInBytes);
263                             ASSERT_EQ(outYCbCr->chroma_step, sampleIncrementInBytes);
264                         }
265 
266                         if (*hSubsampling == 0 && *vSubsampling == 0) {
267                             *hSubsampling = planeLayout.horizontalSubsampling;
268                             *vSubsampling = planeLayout.verticalSubsampling;
269                         } else {
270                             ASSERT_EQ(*hSubsampling, planeLayout.horizontalSubsampling);
271                             ASSERT_EQ(*vSubsampling, planeLayout.verticalSubsampling);
272                         }
273 
274                         if (type == PlaneLayoutComponentType::CB) {
275                             ASSERT_EQ(nullptr, outYCbCr->cb);
276                             outYCbCr->cb = tmpData;
277                         } else {
278                             ASSERT_EQ(nullptr, outYCbCr->cr);
279                             outYCbCr->cr = tmpData;
280                         }
281                         break;
282                     default:
283                         break;
284                 };
285             }
286         }
287 
288         ASSERT_NE(nullptr, outYCbCr->y);
289         ASSERT_NE(nullptr, outYCbCr->cb);
290         ASSERT_NE(nullptr, outYCbCr->cr);
291     }
292 
getAndroidYCbCr_P010(const native_handle_t * bufferHandle,uint8_t * data)293     YCbCr getAndroidYCbCr_P010(const native_handle_t* bufferHandle, uint8_t* data) {
294         YCbCr yCbCr_P010;
295         hidl_vec<uint8_t> vec;
296         EXPECT_EQ(Error::NONE,
297                   mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
298         std::vector<PlaneLayout> planeLayouts;
299         EXPECT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
300         EXPECT_EQ(2, planeLayouts.size());
301         EXPECT_EQ(1, planeLayouts[0].components.size());
302         EXPECT_EQ(2, planeLayouts[1].components.size());
303 
304         yCbCr_P010.yCbCr.y = nullptr;
305         yCbCr_P010.yCbCr.cb = nullptr;
306         yCbCr_P010.yCbCr.cr = nullptr;
307         yCbCr_P010.yCbCr.ystride = 0;
308         yCbCr_P010.yCbCr.cstride = 0;
309         yCbCr_P010.yCbCr.chroma_step = 0;
310         int64_t cb_offset = 0;
311         int64_t cr_offset = 0;
312 
313         for (const auto& planeLayout : planeLayouts) {
314             for (const auto& planeLayoutComponent : planeLayout.components) {
315                 if (!gralloc4::isStandardPlaneLayoutComponentType(planeLayoutComponent.type)) {
316                     continue;
317                 }
318 
319                 uint8_t* tmpData = data + planeLayout.offsetInBytes +
320                                    bitsToBytes(planeLayoutComponent.offsetInBits);
321                 uint64_t sampleIncrementInBytes = 0;
322                 auto type = static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value);
323                 switch (type) {
324                     case PlaneLayoutComponentType::Y:
325                         // For specs refer:
326                         // https://docs.microsoft.com/en-us/windows/win32/medfound/10-bit-and-16-bit-yuv-video-formats
327                         EXPECT_EQ(6, planeLayoutComponent.offsetInBits);
328                         EXPECT_EQ(nullptr, yCbCr_P010.yCbCr.y);
329                         EXPECT_EQ(10, planeLayoutComponent.sizeInBits);
330                         EXPECT_EQ(16, planeLayout.sampleIncrementInBits);
331 
332                         yCbCr_P010.yCbCr.y = tmpData;
333                         yCbCr_P010.yCbCr.ystride = planeLayout.strideInBytes;
334                         break;
335 
336                     case PlaneLayoutComponentType::CB:
337                     case PlaneLayoutComponentType::CR:
338                         sampleIncrementInBytes = bitsToBytes(planeLayout.sampleIncrementInBits);
339                         EXPECT_EQ(4, sampleIncrementInBytes);
340 
341                         if (yCbCr_P010.yCbCr.cstride == 0 && yCbCr_P010.yCbCr.chroma_step == 0) {
342                             yCbCr_P010.yCbCr.cstride = planeLayout.strideInBytes;
343                             yCbCr_P010.yCbCr.chroma_step = sampleIncrementInBytes;
344                         } else {
345                             EXPECT_EQ(yCbCr_P010.yCbCr.cstride, planeLayout.strideInBytes);
346                             EXPECT_EQ(yCbCr_P010.yCbCr.chroma_step, sampleIncrementInBytes);
347                         }
348 
349                         if (yCbCr_P010.horizontalSubSampling == 0 &&
350                             yCbCr_P010.verticalSubSampling == 0) {
351                             yCbCr_P010.horizontalSubSampling = planeLayout.horizontalSubsampling;
352                             yCbCr_P010.verticalSubSampling = planeLayout.verticalSubsampling;
353                         } else {
354                             EXPECT_EQ(yCbCr_P010.horizontalSubSampling,
355                                       planeLayout.horizontalSubsampling);
356                             EXPECT_EQ(yCbCr_P010.verticalSubSampling,
357                                       planeLayout.verticalSubsampling);
358                         }
359 
360                         if (type == PlaneLayoutComponentType::CB) {
361                             EXPECT_EQ(nullptr, yCbCr_P010.yCbCr.cb);
362                             yCbCr_P010.yCbCr.cb = tmpData;
363                             cb_offset = planeLayoutComponent.offsetInBits;
364                         } else {
365                             EXPECT_EQ(nullptr, yCbCr_P010.yCbCr.cr);
366                             yCbCr_P010.yCbCr.cr = tmpData;
367                             cr_offset = planeLayoutComponent.offsetInBits;
368                         }
369                         break;
370                     default:
371                         break;
372                 };
373             }
374         }
375 
376         EXPECT_EQ(cb_offset + bytesToBits(2), cr_offset);
377         EXPECT_NE(nullptr, yCbCr_P010.yCbCr.y);
378         EXPECT_NE(nullptr, yCbCr_P010.yCbCr.cb);
379         EXPECT_NE(nullptr, yCbCr_P010.yCbCr.cr);
380         return yCbCr_P010;
381     }
382 
fillRGBA8888(uint8_t * data,uint32_t height,size_t strideInBytes,size_t widthInBytes,uint32_t seed=0)383     void fillRGBA8888(uint8_t* data, uint32_t height, size_t strideInBytes, size_t widthInBytes,
384                       uint32_t seed = 0) {
385         for (uint32_t y = 0; y < height; y++) {
386             memset(data, y + seed, widthInBytes);
387             data += strideInBytes;
388         }
389     }
390 
verifyRGBA8888(const native_handle_t * bufferHandle,const uint8_t * data,uint32_t height,size_t strideInBytes,size_t widthInBytes,uint32_t seed=0)391     void verifyRGBA8888(const native_handle_t* bufferHandle, const uint8_t* data, uint32_t height,
392                         size_t strideInBytes, size_t widthInBytes, uint32_t seed = 0) {
393         hidl_vec<uint8_t> vec;
394         ASSERT_EQ(Error::NONE,
395                   mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
396         std::vector<PlaneLayout> planeLayouts;
397         ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
398 
399         verifyRGBA8888PlaneLayouts(planeLayouts);
400 
401         for (uint32_t y = 0; y < height; y++) {
402             for (size_t i = 0; i < widthInBytes; i++) {
403                 EXPECT_EQ(static_cast<uint8_t>(y + seed), data[i]);
404             }
405             data += strideInBytes;
406         }
407     }
408 
traverseYCbCrData(const android_ycbcr & yCbCr,int32_t width,int32_t height,int64_t hSubsampling,int64_t vSubsampling,std::function<void (uint8_t *,uint8_t)> traverseFuncion)409     void traverseYCbCrData(const android_ycbcr& yCbCr, int32_t width, int32_t height,
410                            int64_t hSubsampling, int64_t vSubsampling,
411                            std::function<void(uint8_t*, uint8_t)> traverseFuncion) {
412         auto yData = static_cast<uint8_t*>(yCbCr.y);
413         auto cbData = static_cast<uint8_t*>(yCbCr.cb);
414         auto crData = static_cast<uint8_t*>(yCbCr.cr);
415         auto yStride = yCbCr.ystride;
416         auto cStride = yCbCr.cstride;
417         auto chromaStep = yCbCr.chroma_step;
418 
419         for (uint32_t y = 0; y < height; y++) {
420             for (uint32_t x = 0; x < width; x++) {
421                 auto val = static_cast<uint8_t>(height * y + x);
422 
423                 traverseFuncion(yData + yStride * y + x, val);
424 
425                 if (y % vSubsampling == 0 && x % hSubsampling == 0) {
426                     uint32_t subSampleX = x / hSubsampling;
427                     uint32_t subSampleY = y / vSubsampling;
428                     const auto subSampleOffset = cStride * subSampleY + chromaStep * subSampleX;
429                     const auto subSampleVal =
430                             static_cast<uint8_t>(height * subSampleY + subSampleX);
431 
432                     traverseFuncion(cbData + subSampleOffset, subSampleVal);
433                     traverseFuncion(crData + subSampleOffset, subSampleVal + 1);
434                 }
435             }
436         }
437     }
438 
fillYCbCrData(const android_ycbcr & yCbCr,int32_t width,int32_t height,int64_t hSubsampling,int64_t vSubsampling)439     void fillYCbCrData(const android_ycbcr& yCbCr, int32_t width, int32_t height,
440                        int64_t hSubsampling, int64_t vSubsampling) {
441         traverseYCbCrData(yCbCr, width, height, hSubsampling, vSubsampling,
442                           [](auto address, auto fillingData) { *address = fillingData; });
443     }
444 
verifyYCbCrData(const android_ycbcr & yCbCr,int32_t width,int32_t height,int64_t hSubsampling,int64_t vSubsampling)445     void verifyYCbCrData(const android_ycbcr& yCbCr, int32_t width, int32_t height,
446                          int64_t hSubsampling, int64_t vSubsampling) {
447         traverseYCbCrData(
448                 yCbCr, width, height, hSubsampling, vSubsampling,
449                 [](auto address, auto expectedData) { EXPECT_EQ(*address, expectedData); });
450     }
451 
isEqual(float a,float b)452     bool isEqual(float a, float b) { return abs(a - b) < 0.0001; }
453 
bitsToBytes(int64_t bits)454     uint64_t bitsToBytes(int64_t bits) { return bits / 8; }
455 
bytesToBits(int64_t bytes)456     uint64_t bytesToBits(int64_t bytes) { return bytes * 8; }
457 
458     std::unique_ptr<Gralloc> mGralloc;
459     IMapper::BufferDescriptorInfo mDummyDescriptorInfo{};
460     static const std::set<StandardMetadataType> sRequiredMetadataTypes;
461 };
462 
463 const std::set<StandardMetadataType> GraphicsMapperHidlTest::sRequiredMetadataTypes{
464         StandardMetadataType::BUFFER_ID,
465         StandardMetadataType::NAME,
466         StandardMetadataType::WIDTH,
467         StandardMetadataType::HEIGHT,
468         StandardMetadataType::LAYER_COUNT,
469         StandardMetadataType::PIXEL_FORMAT_REQUESTED,
470         StandardMetadataType::PIXEL_FORMAT_FOURCC,
471         StandardMetadataType::PIXEL_FORMAT_MODIFIER,
472         StandardMetadataType::USAGE,
473         StandardMetadataType::ALLOCATION_SIZE,
474         StandardMetadataType::PROTECTED_CONTENT,
475         StandardMetadataType::COMPRESSION,
476         StandardMetadataType::INTERLACED,
477         StandardMetadataType::CHROMA_SITING,
478         StandardMetadataType::PLANE_LAYOUTS,
479         StandardMetadataType::DATASPACE,
480         StandardMetadataType::BLEND_MODE,
481 };
482 
483 /**
484  * Test IAllocator::allocate with valid buffer descriptors.
485  */
TEST_P(GraphicsMapperHidlTest,AllocatorAllocate)486 TEST_P(GraphicsMapperHidlTest, AllocatorAllocate) {
487     BufferDescriptor descriptor;
488     ASSERT_NO_FATAL_FAILURE(descriptor = mGralloc->createDescriptor(mDummyDescriptorInfo));
489 
490     for (uint32_t count = 0; count < 5; count++) {
491         std::vector<const native_handle_t*> bufferHandles;
492         uint32_t stride;
493         ASSERT_NO_FATAL_FAILURE(bufferHandles =
494                                         mGralloc->allocate(descriptor, count, false,
495                                                            Tolerance::kToleranceStrict, &stride));
496 
497         if (count >= 1) {
498             EXPECT_LE(mDummyDescriptorInfo.width, stride) << "invalid buffer stride";
499         }
500 
501         for (auto bufferHandle : bufferHandles) {
502             mGralloc->freeBuffer(bufferHandle);
503         }
504     }
505 }
506 
507 /**
508  * Test IAllocator::allocate with invalid buffer descriptors.
509  */
TEST_P(GraphicsMapperHidlTest,AllocatorAllocateNegative)510 TEST_P(GraphicsMapperHidlTest, AllocatorAllocateNegative) {
511     // this assumes any valid descriptor is non-empty
512     BufferDescriptor descriptor;
513 
514     mGralloc->rawAllocate(descriptor, 1, [&](const auto& tmpError, const auto&, const auto&) {
515         EXPECT_EQ(Error::BAD_DESCRIPTOR, tmpError);
516     });
517 }
518 
519 /**
520  * Test IAllocator::allocate does not leak.
521  */
TEST_P(GraphicsMapperHidlTest,AllocatorAllocateNoLeak)522 TEST_P(GraphicsMapperHidlTest, AllocatorAllocateNoLeak) {
523     auto info = mDummyDescriptorInfo;
524     info.width = 1024;
525     info.height = 1024;
526 
527     for (int i = 0; i < 2048; i++) {
528         auto bufferHandle = mGralloc->allocate(info, false);
529         mGralloc->freeBuffer(bufferHandle);
530     }
531 }
532 
533 /**
534  * Test that IAllocator::allocate is thread-safe.
535  */
TEST_P(GraphicsMapperHidlTest,AllocatorAllocateThreaded)536 TEST_P(GraphicsMapperHidlTest, AllocatorAllocateThreaded) {
537     BufferDescriptor descriptor;
538     ASSERT_NO_FATAL_FAILURE(descriptor = mGralloc->createDescriptor(mDummyDescriptorInfo));
539 
540     std::atomic<bool> timeUp(false);
541     std::atomic<uint64_t> allocationCount(0);
542     auto threadLoop = [&]() {
543         while (!timeUp) {
544             mGralloc->rawAllocate(descriptor, 1, [&](const auto&, const auto&, const auto&) {
545                 allocationCount++;
546             });
547         }
548     };
549 
550     std::vector<std::thread> threads;
551     for (int i = 0; i < 8; i++) {
552         threads.push_back(std::thread(threadLoop));
553     }
554 
555     std::this_thread::sleep_for(std::chrono::seconds(3));
556     timeUp = true;
557     LOG(VERBOSE) << "Made " << allocationCount << " threaded allocations";
558 
559     for (auto& thread : threads) {
560         thread.join();
561     }
562 }
563 
564 /**
565  * Test IMapper::createDescriptor with valid descriptor info.
566  */
TEST_P(GraphicsMapperHidlTest,CreateDescriptorBasic)567 TEST_P(GraphicsMapperHidlTest, CreateDescriptorBasic) {
568     ASSERT_NO_FATAL_FAILURE(mGralloc->createDescriptor(mDummyDescriptorInfo));
569 }
570 
571 /**
572  * Test IMapper::createDescriptor with invalid descriptor info.
573  */
TEST_P(GraphicsMapperHidlTest,CreateDescriptorNegative)574 TEST_P(GraphicsMapperHidlTest, CreateDescriptorNegative) {
575     auto info = mDummyDescriptorInfo;
576     info.width = 0;
577     mGralloc->getMapper()->createDescriptor(info, [&](const auto& tmpError, const auto&) {
578         EXPECT_EQ(Error::BAD_VALUE, tmpError) << "createDescriptor did not fail with BAD_VALUE";
579     });
580 }
581 
582 /**
583  * Test IMapper::importBuffer and IMapper::freeBuffer with allocated buffers.
584  */
TEST_P(GraphicsMapperHidlTest,ImportFreeBufferBasic)585 TEST_P(GraphicsMapperHidlTest, ImportFreeBufferBasic) {
586     const native_handle_t* bufferHandle;
587     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
588     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(bufferHandle));
589 }
590 
591 /**
592  * Test IMapper::importBuffer and IMapper::freeBuffer with cloned buffers.
593  */
TEST_P(GraphicsMapperHidlTest,ImportFreeBufferClone)594 TEST_P(GraphicsMapperHidlTest, ImportFreeBufferClone) {
595     const native_handle_t* clonedBufferHandle;
596     ASSERT_NO_FATAL_FAILURE(clonedBufferHandle = mGralloc->allocate(mDummyDescriptorInfo, false));
597 
598     // A cloned handle is a raw handle. Check that we can import it multiple
599     // times.
600     const native_handle_t* importedBufferHandles[2];
601     ASSERT_NO_FATAL_FAILURE(importedBufferHandles[0] = mGralloc->importBuffer(clonedBufferHandle));
602     ASSERT_NO_FATAL_FAILURE(importedBufferHandles[1] = mGralloc->importBuffer(clonedBufferHandle));
603     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(importedBufferHandles[0]));
604     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(importedBufferHandles[1]));
605 
606     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(clonedBufferHandle));
607 }
608 
609 /**
610  * Test IMapper::importBuffer and IMapper::freeBuffer cross mapper instances.
611  */
TEST_P(GraphicsMapperHidlTest,ImportFreeBufferSingleton)612 TEST_P(GraphicsMapperHidlTest, ImportFreeBufferSingleton) {
613     const native_handle_t* rawHandle;
614     ASSERT_NO_FATAL_FAILURE(rawHandle = mGralloc->allocate(mDummyDescriptorInfo, false));
615 
616     native_handle_t* importedHandle = nullptr;
617     mGralloc->getMapper()->importBuffer(rawHandle, [&](const auto& tmpError, const auto& buffer) {
618         ASSERT_EQ(Error::NONE, tmpError);
619         importedHandle = static_cast<native_handle_t*>(buffer);
620     });
621 
622     // free the imported handle with another mapper
623     std::unique_ptr<Gralloc> anotherGralloc;
624     ASSERT_NO_FATAL_FAILURE(anotherGralloc = std::make_unique<Gralloc>(std::get<0>(GetParam()),
625                                                                        std::get<1>(GetParam())));
626     Error error = mGralloc->getMapper()->freeBuffer(importedHandle);
627     ASSERT_EQ(Error::NONE, error);
628 
629     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(rawHandle));
630 }
631 
632 /**
633  * Test IMapper::importBuffer and IMapper::freeBuffer do not leak.
634  */
TEST_P(GraphicsMapperHidlTest,ImportFreeBufferNoLeak)635 TEST_P(GraphicsMapperHidlTest, ImportFreeBufferNoLeak) {
636     auto info = mDummyDescriptorInfo;
637     info.width = 1024;
638     info.height = 1024;
639 
640     for (int i = 0; i < 2048; i++) {
641         auto bufferHandle = mGralloc->allocate(info, true);
642         mGralloc->freeBuffer(bufferHandle);
643     }
644 }
645 
646 /**
647  * Test IMapper::importBuffer with invalid buffers.
648  */
TEST_P(GraphicsMapperHidlTest,ImportBufferNegative)649 TEST_P(GraphicsMapperHidlTest, ImportBufferNegative) {
650     native_handle_t* invalidHandle = nullptr;
651     mGralloc->getMapper()->importBuffer(invalidHandle, [&](const auto& tmpError, const auto&) {
652         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
653                 << "importBuffer with nullptr did not fail with BAD_BUFFER";
654     });
655 
656     invalidHandle = native_handle_create(0, 0);
657     mGralloc->getMapper()->importBuffer(invalidHandle, [&](const auto& tmpError, const auto&) {
658         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
659                 << "importBuffer with invalid handle did not fail with BAD_BUFFER";
660     });
661     native_handle_delete(invalidHandle);
662 }
663 
664 /**
665  * Test IMapper::freeBuffer with invalid buffers.
666  */
TEST_P(GraphicsMapperHidlTest,FreeBufferNegative)667 TEST_P(GraphicsMapperHidlTest, FreeBufferNegative) {
668     native_handle_t* invalidHandle = nullptr;
669     Error error = mGralloc->getMapper()->freeBuffer(invalidHandle);
670     EXPECT_EQ(Error::BAD_BUFFER, error) << "freeBuffer with nullptr did not fail with BAD_BUFFER";
671 
672     invalidHandle = native_handle_create(0, 0);
673     error = mGralloc->getMapper()->freeBuffer(invalidHandle);
674     EXPECT_EQ(Error::BAD_BUFFER, error)
675             << "freeBuffer with invalid handle did not fail with BAD_BUFFER";
676     native_handle_delete(invalidHandle);
677 
678     const native_handle_t* clonedBufferHandle;
679     ASSERT_NO_FATAL_FAILURE(clonedBufferHandle = mGralloc->allocate(mDummyDescriptorInfo, false));
680     error = mGralloc->getMapper()->freeBuffer(const_cast<native_handle_t*>(clonedBufferHandle));
681     EXPECT_EQ(Error::BAD_BUFFER, error)
682             << "freeBuffer with un-imported handle did not fail with BAD_BUFFER";
683 
684     mGralloc->freeBuffer(clonedBufferHandle);
685 }
686 
687 /**
688  * Test IMapper::lock and IMapper::unlock.
689  */
TEST_P(GraphicsMapperHidlTest,LockUnlockBasic)690 TEST_P(GraphicsMapperHidlTest, LockUnlockBasic) {
691     const auto& info = mDummyDescriptorInfo;
692 
693     const native_handle_t* bufferHandle;
694     uint32_t stride;
695     ASSERT_NO_FATAL_FAILURE(
696             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
697 
698     // lock buffer for writing
699     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
700                                static_cast<int32_t>(info.height)};
701     unique_fd fence;
702     uint8_t* data;
703     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
704                                                                         region, fence.release())));
705 
706     // RGBA_8888
707     fillRGBA8888(data, info.height, stride * 4, info.width * 4);
708 
709     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
710 
711     // lock again for reading
712     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
713                                                                         region, fence.release())));
714 
715     ASSERT_NO_FATAL_FAILURE(
716             verifyRGBA8888(bufferHandle, data, info.height, stride * 4, info.width * 4));
717 
718     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
719 }
720 
721 /**
722  *  Test multiple operations associated with different color formats
723  */
TEST_P(GraphicsMapperHidlTest,Lock_YCRCB_420_SP)724 TEST_P(GraphicsMapperHidlTest, Lock_YCRCB_420_SP) {
725     auto info = mDummyDescriptorInfo;
726     info.format = PixelFormat::YCRCB_420_SP;
727 
728     const native_handle_t* bufferHandle;
729     uint32_t stride;
730     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(
731                                     info, true, Tolerance::kToleranceUnSupported, &stride));
732     if (bufferHandle == nullptr) {
733         GTEST_SUCCEED() << "YCRCB_420_SP format is unsupported";
734         return;
735     }
736 
737     // lock buffer for writing
738     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
739                                static_cast<int32_t>(info.height)};
740     unique_fd fence;
741     uint8_t* data;
742 
743     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
744                                                                         region, fence.release())));
745 
746     android_ycbcr yCbCr;
747     int64_t hSubsampling = 0;
748     int64_t vSubsampling = 0;
749     ASSERT_NO_FATAL_FAILURE(
750             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
751 
752     constexpr uint32_t kCbCrSubSampleFactor = 2;
753     ASSERT_EQ(kCbCrSubSampleFactor, hSubsampling);
754     ASSERT_EQ(kCbCrSubSampleFactor, vSubsampling);
755 
756     auto cbData = static_cast<uint8_t*>(yCbCr.cb);
757     auto crData = static_cast<uint8_t*>(yCbCr.cr);
758     ASSERT_EQ(crData + 1, cbData);
759     ASSERT_EQ(2, yCbCr.chroma_step);
760 
761     fillYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
762 
763     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
764 
765     // lock again for reading
766     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
767                                                                         region, fence.release())));
768 
769     ASSERT_NO_FATAL_FAILURE(
770             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
771 
772     verifyYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
773 
774     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
775 }
776 
TEST_P(GraphicsMapperHidlTest,YV12SubsampleMetadata)777 TEST_P(GraphicsMapperHidlTest, YV12SubsampleMetadata) {
778     auto info = mDummyDescriptorInfo;
779     info.format = PixelFormat::YV12;
780 
781     const native_handle_t* bufferHandle;
782     uint32_t stride;
783     ASSERT_NO_FATAL_FAILURE(
784             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
785 
786     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
787                                static_cast<int32_t>(info.height)};
788     unique_fd fence;
789     ASSERT_NO_FATAL_FAILURE(mGralloc->lock(bufferHandle, info.usage, region, fence.release()));
790 
791     hidl_vec<uint8_t> vec;
792     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
793     std::vector<PlaneLayout> planeLayouts;
794     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
795 
796     ASSERT_EQ(3, planeLayouts.size());
797 
798     auto yPlane = planeLayouts[0];
799     auto crPlane = planeLayouts[1];
800     auto cbPlane = planeLayouts[2];
801 
802     constexpr uint32_t kCbCrSubSampleFactor = 2;
803     EXPECT_EQ(kCbCrSubSampleFactor, crPlane.horizontalSubsampling);
804     EXPECT_EQ(kCbCrSubSampleFactor, crPlane.verticalSubsampling);
805 
806     EXPECT_EQ(kCbCrSubSampleFactor, cbPlane.horizontalSubsampling);
807     EXPECT_EQ(kCbCrSubSampleFactor, cbPlane.verticalSubsampling);
808 
809     const long chromaSampleWidth = info.width / kCbCrSubSampleFactor;
810     const long chromaSampleHeight = info.height / kCbCrSubSampleFactor;
811 
812     EXPECT_EQ(info.width, yPlane.widthInSamples);
813     EXPECT_EQ(info.height, yPlane.heightInSamples);
814 
815     EXPECT_EQ(chromaSampleWidth, crPlane.widthInSamples);
816     EXPECT_EQ(chromaSampleHeight, crPlane.heightInSamples);
817 
818     EXPECT_EQ(chromaSampleWidth, cbPlane.widthInSamples);
819     EXPECT_EQ(chromaSampleHeight, cbPlane.heightInSamples);
820 
821     EXPECT_LE(crPlane.widthInSamples, crPlane.strideInBytes);
822     EXPECT_LE(cbPlane.widthInSamples, cbPlane.strideInBytes);
823 
824     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
825 }
826 
TEST_P(GraphicsMapperHidlTest,Lock_YV12)827 TEST_P(GraphicsMapperHidlTest, Lock_YV12) {
828     auto info = mDummyDescriptorInfo;
829     info.format = PixelFormat::YV12;
830 
831     const native_handle_t* bufferHandle;
832     uint32_t stride;
833     ASSERT_NO_FATAL_FAILURE(
834             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
835 
836     // lock buffer for writing
837     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
838                                static_cast<int32_t>(info.height)};
839     unique_fd fence;
840     uint8_t* data;
841 
842     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
843                                                                         region, fence.release())));
844 
845     android_ycbcr yCbCr;
846     int64_t hSubsampling = 0;
847     int64_t vSubsampling = 0;
848     ASSERT_NO_FATAL_FAILURE(
849             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
850 
851     constexpr uint32_t kCbCrSubSampleFactor = 2;
852     ASSERT_EQ(kCbCrSubSampleFactor, hSubsampling);
853     ASSERT_EQ(kCbCrSubSampleFactor, vSubsampling);
854 
855     auto cbData = static_cast<uint8_t*>(yCbCr.cb);
856     auto crData = static_cast<uint8_t*>(yCbCr.cr);
857     ASSERT_EQ(crData + yCbCr.cstride * info.height / vSubsampling, cbData);
858     ASSERT_EQ(1, yCbCr.chroma_step);
859 
860     fillYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
861 
862     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
863 
864     // lock again for reading
865     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
866                                                                         region, fence.release())));
867 
868     ASSERT_NO_FATAL_FAILURE(
869             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
870 
871     verifyYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
872 
873     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
874 }
875 
TEST_P(GraphicsMapperHidlTest,Lock_YCBCR_420_888)876 TEST_P(GraphicsMapperHidlTest, Lock_YCBCR_420_888) {
877     auto info = mDummyDescriptorInfo;
878     info.format = PixelFormat::YCBCR_420_888;
879 
880     const native_handle_t* bufferHandle;
881     uint32_t stride;
882     ASSERT_NO_FATAL_FAILURE(
883             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
884 
885     // lock buffer for writing
886     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
887                                static_cast<int32_t>(info.height)};
888     unique_fd fence;
889     uint8_t* data;
890 
891     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
892                                                                         region, fence.release())));
893 
894     android_ycbcr yCbCr;
895     int64_t hSubsampling = 0;
896     int64_t vSubsampling = 0;
897     ASSERT_NO_FATAL_FAILURE(
898             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
899 
900     constexpr uint32_t kCbCrSubSampleFactor = 2;
901     ASSERT_EQ(kCbCrSubSampleFactor, hSubsampling);
902     ASSERT_EQ(kCbCrSubSampleFactor, vSubsampling);
903 
904     fillYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
905 
906     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
907 
908     // lock again for reading
909     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
910                                                                         region, fence.release())));
911 
912     ASSERT_NO_FATAL_FAILURE(
913             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
914 
915     verifyYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
916 
917     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
918 }
919 
TEST_P(GraphicsMapperHidlTest,Lock_RAW10)920 TEST_P(GraphicsMapperHidlTest, Lock_RAW10) {
921     auto info = mDummyDescriptorInfo;
922     info.format = PixelFormat::RAW10;
923 
924     const native_handle_t* bufferHandle;
925     uint32_t stride;
926     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(
927                                     info, true, Tolerance::kToleranceUnSupported, &stride));
928     if (bufferHandle == nullptr) {
929         GTEST_SUCCEED() << "RAW10 format is unsupported";
930         return;
931     }
932 
933     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
934                                static_cast<int32_t>(info.height)};
935     unique_fd fence;
936 
937     ASSERT_NO_FATAL_FAILURE(mGralloc->lock(bufferHandle, info.usage, region, fence.release()));
938 
939     hidl_vec<uint8_t> vec;
940     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
941     std::vector<PlaneLayout> planeLayouts;
942     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
943 
944     ASSERT_EQ(1, planeLayouts.size());
945     auto planeLayout = planeLayouts[0];
946 
947     EXPECT_EQ(0, planeLayout.sampleIncrementInBits);
948     EXPECT_EQ(1, planeLayout.horizontalSubsampling);
949     EXPECT_EQ(1, planeLayout.verticalSubsampling);
950 
951     ASSERT_EQ(1, planeLayout.components.size());
952     auto planeLayoutComponent = planeLayout.components[0];
953 
954     EXPECT_EQ(PlaneLayoutComponentType::RAW,
955               static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value));
956     EXPECT_EQ(0, planeLayoutComponent.offsetInBits % 8);
957 
958     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
959 }
960 
TEST_P(GraphicsMapperHidlTest,Lock_RAW12)961 TEST_P(GraphicsMapperHidlTest, Lock_RAW12) {
962     auto info = mDummyDescriptorInfo;
963     info.format = PixelFormat::RAW12;
964 
965     const native_handle_t* bufferHandle;
966     uint32_t stride;
967     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(
968                                     info, true, Tolerance::kToleranceUnSupported, &stride));
969     if (bufferHandle == nullptr) {
970         GTEST_SUCCEED() << "RAW12 format is unsupported";
971         return;
972     }
973 
974     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
975                                static_cast<int32_t>(info.height)};
976     unique_fd fence;
977 
978     ASSERT_NO_FATAL_FAILURE(mGralloc->lock(bufferHandle, info.usage, region, fence.release()));
979 
980     hidl_vec<uint8_t> vec;
981     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
982     std::vector<PlaneLayout> planeLayouts;
983     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
984 
985     ASSERT_EQ(1, planeLayouts.size());
986     auto planeLayout = planeLayouts[0];
987 
988     EXPECT_EQ(0, planeLayout.sampleIncrementInBits);
989     EXPECT_EQ(1, planeLayout.horizontalSubsampling);
990     EXPECT_EQ(1, planeLayout.verticalSubsampling);
991 
992     ASSERT_EQ(1, planeLayout.components.size());
993     auto planeLayoutComponent = planeLayout.components[0];
994 
995     EXPECT_EQ(PlaneLayoutComponentType::RAW,
996               static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value));
997     EXPECT_EQ(0, planeLayoutComponent.offsetInBits % 8);
998 
999     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
1000 }
1001 
TEST_P(GraphicsMapperHidlTest,Lock_YCBCR_P010)1002 TEST_P(GraphicsMapperHidlTest, Lock_YCBCR_P010) {
1003     if (base::GetIntProperty("ro.vendor.api_level", __ANDROID_API_FUTURE__) < __ANDROID_API_T__) {
1004         GTEST_SKIP() << "Old vendor grallocs may not support P010";
1005     }
1006     auto info = mDummyDescriptorInfo;
1007     info.format = PixelFormat::YCBCR_P010;
1008 
1009     uint32_t stride;
1010     const native_handle_t* bufferHandle =
1011             mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride);
1012 
1013     if (::testing::Test::IsSkipped()) {
1014         GTEST_SKIP();
1015     }
1016 
1017     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
1018                                static_cast<int32_t>(info.height)};
1019     unique_fd fence;
1020     uint8_t* data;
1021 
1022     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
1023                                                                         region, fence.release())));
1024 
1025     YCbCr yCbCr;
1026     ASSERT_NO_FATAL_FAILURE(yCbCr = getAndroidYCbCr_P010(bufferHandle, data));
1027 
1028     constexpr uint32_t kCbCrSubSampleFactor = 2;
1029     ASSERT_EQ(kCbCrSubSampleFactor, yCbCr.horizontalSubSampling);
1030     ASSERT_EQ(kCbCrSubSampleFactor, yCbCr.verticalSubSampling);
1031 
1032     ASSERT_EQ(0, info.height % 2);
1033 
1034     // fill the data
1035     fillYCbCrData(yCbCr.yCbCr, info.width, info.height, yCbCr.horizontalSubSampling,
1036                   yCbCr.verticalSubSampling);
1037     // verify the YCbCr data
1038     verifyYCbCrData(yCbCr.yCbCr, info.width, info.height, yCbCr.horizontalSubSampling,
1039                     yCbCr.verticalSubSampling);
1040 
1041     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
1042 }
1043 
1044 /**
1045  * Test IMapper::unlock with bad access region
1046  */
TEST_P(GraphicsMapperHidlTest,LockBadAccessRegion)1047 TEST_P(GraphicsMapperHidlTest, LockBadAccessRegion) {
1048     const auto& info = mDummyDescriptorInfo;
1049 
1050     const native_handle_t* bufferHandle;
1051     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
1052 
1053     const IMapper::Rect accessRegion{0, 0, static_cast<int32_t>(info.width * 2),
1054                                      static_cast<int32_t>(info.height * 2)};
1055     int acquireFence = -1;
1056 
1057     NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
1058     hidl_handle acquireFenceHandle;
1059     if (acquireFence >= 0) {
1060         auto h = native_handle_init(acquireFenceStorage, 1, 0);
1061         h->data[0] = acquireFence;
1062         acquireFenceHandle = h;
1063     }
1064 
1065     auto buffer = const_cast<native_handle_t*>(bufferHandle);
1066     mGralloc->getMapper()->lock(buffer, info.usage, accessRegion, acquireFenceHandle,
1067                                 [&](const auto& tmpError, const auto& /*tmpData*/) {
1068                                     EXPECT_EQ(Error::BAD_VALUE, tmpError)
1069                                             << "locking with a bad access region should fail";
1070                                 });
1071 
1072     if (::testing::Test::HasFailure()) {
1073         if (acquireFence >= 0) {
1074             close(acquireFence);
1075         }
1076 
1077         int releaseFence = -1;
1078         ASSERT_NO_FATAL_FAILURE(releaseFence = mGralloc->unlock(bufferHandle));
1079 
1080         if (releaseFence >= 0) {
1081             close(releaseFence);
1082         }
1083     }
1084 }
1085 
1086 /**
1087  * Test IMapper::unlock with invalid buffers.
1088  */
TEST_P(GraphicsMapperHidlTest,UnlockNegative)1089 TEST_P(GraphicsMapperHidlTest, UnlockNegative) {
1090     native_handle_t* invalidHandle = nullptr;
1091     mGralloc->getMapper()->unlock(invalidHandle, [&](const auto& tmpError, const auto&) {
1092         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1093                 << "unlock with nullptr did not fail with BAD_BUFFER";
1094     });
1095 
1096     invalidHandle = native_handle_create(0, 0);
1097     mGralloc->getMapper()->unlock(invalidHandle, [&](const auto& tmpError, const auto&) {
1098         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1099                 << "unlock with invalid handle did not fail with BAD_BUFFER";
1100     });
1101     native_handle_delete(invalidHandle);
1102 
1103     ASSERT_NO_FATAL_FAILURE(invalidHandle = const_cast<native_handle_t*>(
1104                                     mGralloc->allocate(mDummyDescriptorInfo, false)));
1105     mGralloc->getMapper()->unlock(invalidHandle, [&](const auto& tmpError, const auto&) {
1106         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1107                 << "unlock with un-imported handle did not fail with BAD_BUFFER";
1108     });
1109     mGralloc->freeBuffer(invalidHandle);
1110 
1111 // disabled as it fails on many existing drivers
1112 #if 0
1113   ASSERT_NO_FATAL_FAILURE(invalidHandle = const_cast<native_handle_t*>(
1114                               mGralloc->allocate(mDummyDescriptorInfo, true)));
1115   mGralloc->getMapper()->unlock(
1116       invalidHandle, [&](const auto& tmpError, const auto&) {
1117         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1118             << "unlock with unlocked handle did not fail with BAD_BUFFER";
1119       });
1120   mGralloc->freeBuffer(invalidHandle);
1121 #endif
1122 }
1123 
1124 /**
1125  * Test IMapper::flush and IMapper::reread.
1126  */
TEST_P(GraphicsMapperHidlTest,FlushRereadBasic)1127 TEST_P(GraphicsMapperHidlTest, FlushRereadBasic) {
1128     const auto& info = mDummyDescriptorInfo;
1129 
1130     const native_handle_t* rawHandle;
1131     uint32_t stride;
1132     ASSERT_NO_FATAL_FAILURE(rawHandle = mGralloc->allocate(mDummyDescriptorInfo, false,
1133                                                            Tolerance::kToleranceStrict, &stride));
1134 
1135     const native_handle_t* writeBufferHandle;
1136     const native_handle_t* readBufferHandle;
1137     ASSERT_NO_FATAL_FAILURE(writeBufferHandle = mGralloc->importBuffer(rawHandle));
1138     ASSERT_NO_FATAL_FAILURE(readBufferHandle = mGralloc->importBuffer(rawHandle));
1139 
1140     // lock buffer for writing
1141     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
1142                                static_cast<int32_t>(info.height)};
1143     uint8_t* writeData;
1144     ASSERT_NO_FATAL_FAILURE(
1145             writeData = static_cast<uint8_t*>(mGralloc->lock(
1146                     writeBufferHandle, static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN), region,
1147                     -1)));
1148 
1149     uint8_t* readData;
1150     ASSERT_NO_FATAL_FAILURE(
1151             readData = static_cast<uint8_t*>(mGralloc->lock(
1152                     readBufferHandle, static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN), region,
1153                     -1)));
1154 
1155     fillRGBA8888(writeData, info.height, stride * 4, info.width * 4);
1156 
1157     unique_fd fence;
1158     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->flushLockedBuffer(writeBufferHandle)));
1159     if (fence >= 0) {
1160         ASSERT_EQ(0, sync_wait(fence, 3500));
1161     }
1162 
1163     ASSERT_NO_FATAL_FAILURE(mGralloc->rereadLockedBuffer(readBufferHandle));
1164 
1165     ASSERT_NO_FATAL_FAILURE(
1166             verifyRGBA8888(readBufferHandle, readData, info.height, stride * 4, info.width * 4));
1167 
1168     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(readBufferHandle)));
1169 
1170     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(writeBufferHandle)));
1171 }
1172 
1173 /**
1174  * Test IMapper::flushLockedBuffer with bad buffer
1175  */
TEST_P(GraphicsMapperHidlTest,FlushLockedBufferBadBuffer)1176 TEST_P(GraphicsMapperHidlTest, FlushLockedBufferBadBuffer) {
1177     ASSERT_NO_FATAL_FAILURE(mGralloc->getMapper()->flushLockedBuffer(
1178             nullptr, [&](const auto& tmpError, const auto& /*tmpReleaseFence*/) {
1179                 ASSERT_EQ(Error::BAD_BUFFER, tmpError);
1180             }));
1181 }
1182 
1183 /**
1184  * Test IMapper::rereadLockedBuffer with bad buffer
1185  */
TEST_P(GraphicsMapperHidlTest,RereadLockedBufferBadBuffer)1186 TEST_P(GraphicsMapperHidlTest, RereadLockedBufferBadBuffer) {
1187     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->getMapper()->rereadLockedBuffer(nullptr));
1188 }
1189 
1190 /**
1191  * Test IMapper::isSupported with required format RGBA_8888
1192  */
TEST_P(GraphicsMapperHidlTest,IsSupportedRGBA8888)1193 TEST_P(GraphicsMapperHidlTest, IsSupportedRGBA8888) {
1194     const auto& info = mDummyDescriptorInfo;
1195     bool supported = false;
1196 
1197     ASSERT_NO_FATAL_FAILURE(supported = mGralloc->isSupported(info));
1198     ASSERT_TRUE(supported);
1199 }
1200 
1201 /**
1202  * Test IMapper::isSupported with required format YV12
1203  */
TEST_P(GraphicsMapperHidlTest,IsSupportedYV12)1204 TEST_P(GraphicsMapperHidlTest, IsSupportedYV12) {
1205     auto info = mDummyDescriptorInfo;
1206     info.format = PixelFormat::YV12;
1207     bool supported = false;
1208 
1209     ASSERT_NO_FATAL_FAILURE(supported = mGralloc->isSupported(info));
1210     ASSERT_TRUE(supported);
1211 }
1212 
1213 /**
1214  * Test IMapper::isSupported with optional format Y16
1215  */
TEST_P(GraphicsMapperHidlTest,IsSupportedY16)1216 TEST_P(GraphicsMapperHidlTest, IsSupportedY16) {
1217     auto info = mDummyDescriptorInfo;
1218     info.format = PixelFormat::Y16;
1219     bool supported = false;
1220 
1221     ASSERT_NO_FATAL_FAILURE(supported = mGralloc->isSupported(info));
1222 }
1223 
1224 /**
1225  * Test IMapper::isSupported with optional format R_8
1226  */
TEST_P(GraphicsMapperHidlTest,IsSupportedR8)1227 TEST_P(GraphicsMapperHidlTest, IsSupportedR8) {
1228     auto info = mDummyDescriptorInfo;
1229     info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(
1230             aidl::android::hardware::graphics::common::PixelFormat::R_8);
1231     bool supported = false;
1232 
1233     supported = mGralloc->isSupportedNoFailure(info);
1234 
1235     if (!supported) {
1236         GTEST_SUCCEED() << "R_8 is optional; unsupported so skipping allocation test";
1237         return;
1238     }
1239 
1240     BufferDescriptor descriptor;
1241     ASSERT_NO_FATAL_FAILURE(descriptor = mGralloc->createDescriptor(info));
1242 
1243     constexpr uint32_t count = 1;
1244     std::vector<const native_handle_t*> bufferHandles;
1245     uint32_t stride;
1246     ASSERT_NO_FATAL_FAILURE(bufferHandles =
1247                                     mGralloc->allocate(descriptor, count, false,
1248                                                        Tolerance::kToleranceStrict, &stride));
1249 
1250     EXPECT_LE(info.width, stride) << "invalid buffer stride";
1251     EXPECT_EQ(1u, bufferHandles.size());
1252 
1253     for (auto bufferHandle : bufferHandles) {
1254         mGralloc->freeBuffer(bufferHandle);
1255     }
1256 }
1257 
1258 /**
1259  * Test IMapper::get(BufferId)
1260  */
TEST_P(GraphicsMapperHidlTest,GetBufferId)1261 TEST_P(GraphicsMapperHidlTest, GetBufferId) {
1262     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_BufferId,
1263             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1264                 uint64_t bufferId = 0;
1265                 ASSERT_EQ(NO_ERROR, gralloc4::decodeBufferId(vec, &bufferId));
1266             });
1267 }
1268 
1269 /**
1270  * Test IMapper::get(Name)
1271  */
TEST_P(GraphicsMapperHidlTest,GetName)1272 TEST_P(GraphicsMapperHidlTest, GetName) {
1273     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Name,
1274             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1275                 std::string name;
1276                 ASSERT_EQ(NO_ERROR, gralloc4::decodeName(vec, &name));
1277                 EXPECT_EQ(info.name, name);
1278             });
1279 }
1280 
1281 /**
1282  * Test IMapper::get(Width)
1283  */
TEST_P(GraphicsMapperHidlTest,GetWidth)1284 TEST_P(GraphicsMapperHidlTest, GetWidth) {
1285     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Width,
1286             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1287                 uint64_t width = 0;
1288                 ASSERT_EQ(NO_ERROR, gralloc4::decodeWidth(vec, &width));
1289                 EXPECT_EQ(info.width, width);
1290             });
1291 }
1292 
1293 /**
1294  * Test IMapper::get(Height)
1295  */
TEST_P(GraphicsMapperHidlTest,GetHeight)1296 TEST_P(GraphicsMapperHidlTest, GetHeight) {
1297     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Height,
1298             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1299                 uint64_t height = 0;
1300                 ASSERT_EQ(NO_ERROR, gralloc4::decodeHeight(vec, &height));
1301                 EXPECT_EQ(info.height, height);
1302             });
1303 }
1304 
1305 /**
1306  * Test IMapper::get(LayerCount)
1307  */
TEST_P(GraphicsMapperHidlTest,GetLayerCount)1308 TEST_P(GraphicsMapperHidlTest, GetLayerCount) {
1309     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_LayerCount,
1310             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1311                 uint64_t layerCount = 0;
1312                 ASSERT_EQ(NO_ERROR, gralloc4::decodeLayerCount(vec, &layerCount));
1313                 EXPECT_EQ(info.layerCount, layerCount);
1314             });
1315 }
1316 
1317 /**
1318  * Test IMapper::get(PixelFormatRequested)
1319  */
TEST_P(GraphicsMapperHidlTest,GetPixelFormatRequested)1320 TEST_P(GraphicsMapperHidlTest, GetPixelFormatRequested) {
1321     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatRequested,
1322             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1323                 PixelFormat pixelFormatRequested = PixelFormat::BLOB;
1324                 ASSERT_EQ(NO_ERROR,
1325                           gralloc4::decodePixelFormatRequested(vec, &pixelFormatRequested));
1326                 EXPECT_EQ(info.format, pixelFormatRequested);
1327             });
1328 }
1329 
1330 /**
1331  * Test IMapper::get(PixelFormatFourCC)
1332  */
TEST_P(GraphicsMapperHidlTest,GetPixelFormatFourCC)1333 TEST_P(GraphicsMapperHidlTest, GetPixelFormatFourCC) {
1334     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatFourCC,
1335             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1336                 uint32_t pixelFormatFourCC = 0;
1337                 ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, &pixelFormatFourCC));
1338             });
1339 }
1340 
1341 /**
1342  * Test IMapper::get(PixelFormatModifier)
1343  */
TEST_P(GraphicsMapperHidlTest,GetPixelFormatModifier)1344 TEST_P(GraphicsMapperHidlTest, GetPixelFormatModifier) {
1345     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatModifier,
1346             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1347                 uint64_t pixelFormatModifier = 0;
1348                 ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatModifier(vec, &pixelFormatModifier));
1349             });
1350 }
1351 
1352 /**
1353  * Test IMapper::get(Usage)
1354  */
TEST_P(GraphicsMapperHidlTest,GetUsage)1355 TEST_P(GraphicsMapperHidlTest, GetUsage) {
1356     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Usage,
1357             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1358                 uint64_t usage = 0;
1359                 ASSERT_EQ(NO_ERROR, gralloc4::decodeUsage(vec, &usage));
1360                 EXPECT_EQ(info.usage, usage);
1361             });
1362 }
1363 
1364 /**
1365  * Test IMapper::get(AllocationSize)
1366  */
TEST_P(GraphicsMapperHidlTest,GetAllocationSize)1367 TEST_P(GraphicsMapperHidlTest, GetAllocationSize) {
1368     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_AllocationSize,
1369             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1370                 uint64_t allocationSize = 0;
1371                 ASSERT_EQ(NO_ERROR, gralloc4::decodeAllocationSize(vec, &allocationSize));
1372             });
1373 }
1374 
1375 /**
1376  * Test IMapper::get(ProtectedContent)
1377  */
TEST_P(GraphicsMapperHidlTest,GetProtectedContent)1378 TEST_P(GraphicsMapperHidlTest, GetProtectedContent) {
1379     auto info = mDummyDescriptorInfo;
1380     info.usage = BufferUsage::PROTECTED | BufferUsage::COMPOSER_OVERLAY;
1381 
1382     const native_handle_t* bufferHandle = nullptr;
1383     bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceAllErrors);
1384     if (!bufferHandle) {
1385         GTEST_SUCCEED() << "unable to allocate protected content";
1386         return;
1387     }
1388 
1389     hidl_vec<uint8_t> vec;
1390     ASSERT_EQ(Error::NONE,
1391               mGralloc->get(bufferHandle, gralloc4::MetadataType_ProtectedContent, &vec));
1392 
1393     uint64_t protectedContent = 0;
1394     ASSERT_EQ(NO_ERROR, gralloc4::decodeProtectedContent(vec, &protectedContent));
1395     EXPECT_EQ(1, protectedContent);
1396 }
1397 
1398 /**
1399  * Test IMapper::get(Compression)
1400  */
TEST_P(GraphicsMapperHidlTest,GetCompression)1401 TEST_P(GraphicsMapperHidlTest, GetCompression) {
1402     auto info = mDummyDescriptorInfo;
1403     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
1404 
1405     testGet(info, gralloc4::MetadataType_Compression,
1406             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1407                 ExtendableType compression = gralloc4::Compression_DisplayStreamCompression;
1408                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCompression(vec, &compression));
1409 
1410                 EXPECT_EQ(gralloc4::Compression_None.name, compression.name);
1411                 EXPECT_EQ(gralloc4::Compression_None.value, compression.value);
1412             });
1413 }
1414 
1415 /**
1416  * Test IMapper::get(Interlaced)
1417  */
TEST_P(GraphicsMapperHidlTest,GetInterlaced)1418 TEST_P(GraphicsMapperHidlTest, GetInterlaced) {
1419     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Interlaced,
1420             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1421                 ExtendableType interlaced = gralloc4::Interlaced_TopBottom;
1422                 ASSERT_EQ(NO_ERROR, gralloc4::decodeInterlaced(vec, &interlaced));
1423 
1424                 EXPECT_EQ(gralloc4::Interlaced_None.name, interlaced.name);
1425                 EXPECT_EQ(gralloc4::Interlaced_None.value, interlaced.value);
1426             });
1427 }
1428 
1429 /**
1430  * Test IMapper::get(ChromaSiting)
1431  */
TEST_P(GraphicsMapperHidlTest,GetChromaSiting)1432 TEST_P(GraphicsMapperHidlTest, GetChromaSiting) {
1433     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_ChromaSiting,
1434             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1435                 ExtendableType chromaSiting = gralloc4::ChromaSiting_Unknown;
1436                 ASSERT_EQ(NO_ERROR, gralloc4::decodeChromaSiting(vec, &chromaSiting));
1437 
1438                 EXPECT_EQ(gralloc4::ChromaSiting_None.name, chromaSiting.name);
1439                 EXPECT_EQ(gralloc4::ChromaSiting_None.value, chromaSiting.value);
1440             });
1441 }
1442 
1443 /**
1444  * Test IMapper::get(PlaneLayouts)
1445  */
TEST_P(GraphicsMapperHidlTest,GetPlaneLayouts)1446 TEST_P(GraphicsMapperHidlTest, GetPlaneLayouts) {
1447     const native_handle_t* bufferHandle = nullptr;
1448     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
1449 
1450     hidl_vec<uint8_t> vec;
1451     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
1452 
1453     std::vector<PlaneLayout> planeLayouts;
1454     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
1455 
1456     ASSERT_NO_FATAL_FAILURE(verifyRGBA8888PlaneLayouts(planeLayouts));
1457 }
1458 
1459 /**
1460  * Test IMapper::get(Crop)
1461  */
TEST_P(GraphicsMapperHidlTest,GetCrop)1462 TEST_P(GraphicsMapperHidlTest, GetCrop) {
1463     auto info = mDummyDescriptorInfo;
1464     info.format = PixelFormat::RGBA_8888;
1465     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
1466 
1467     testGet(info, gralloc4::MetadataType_Crop,
1468             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1469                 std::vector<aidl::android::hardware::graphics::common::Rect> crops;
1470                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCrop(vec, &crops));
1471                 EXPECT_EQ(1, crops.size());
1472             });
1473 }
1474 
1475 /**
1476  * Test IMapper::get(Dataspace)
1477  */
TEST_P(GraphicsMapperHidlTest,GetDataspace)1478 TEST_P(GraphicsMapperHidlTest, GetDataspace) {
1479     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Dataspace,
1480             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1481                 Dataspace dataspace = Dataspace::DISPLAY_P3;
1482                 ASSERT_EQ(NO_ERROR, gralloc4::decodeDataspace(vec, &dataspace));
1483                 EXPECT_EQ(Dataspace::UNKNOWN, dataspace);
1484             });
1485 }
1486 
1487 /**
1488  * Test IMapper::get(BlendMode)
1489  */
TEST_P(GraphicsMapperHidlTest,GetBlendMode)1490 TEST_P(GraphicsMapperHidlTest, GetBlendMode) {
1491     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_BlendMode,
1492             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1493                 BlendMode blendMode = BlendMode::NONE;
1494                 ASSERT_EQ(NO_ERROR, gralloc4::decodeBlendMode(vec, &blendMode));
1495                 EXPECT_EQ(BlendMode::INVALID, blendMode);
1496             });
1497 }
1498 
1499 /**
1500  * Test IMapper::get(Smpte2086)
1501  */
TEST_P(GraphicsMapperHidlTest,GetSmpte2086)1502 TEST_P(GraphicsMapperHidlTest, GetSmpte2086) {
1503     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2086,
1504             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1505                 std::optional<Smpte2086> smpte2086;
1506                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2086(vec, &smpte2086));
1507                 EXPECT_FALSE(smpte2086.has_value());
1508             });
1509 }
1510 
1511 /**
1512  * Test IMapper::get(Cta861_3)
1513  */
TEST_P(GraphicsMapperHidlTest,GetCta861_3)1514 TEST_P(GraphicsMapperHidlTest, GetCta861_3) {
1515     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Cta861_3,
1516             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1517                 std::optional<Cta861_3> cta861_3;
1518                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCta861_3(vec, &cta861_3));
1519                 EXPECT_FALSE(cta861_3.has_value());
1520             });
1521 }
1522 
1523 /**
1524  * Test IMapper::get(Smpte2094_40)
1525  */
TEST_P(GraphicsMapperHidlTest,GetSmpte2094_40)1526 TEST_P(GraphicsMapperHidlTest, GetSmpte2094_40) {
1527     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_40,
1528             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1529                 std::optional<std::vector<uint8_t>> smpte2094_40;
1530                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, &smpte2094_40));
1531                 EXPECT_FALSE(smpte2094_40.has_value());
1532             });
1533 }
1534 
1535 /**
1536  * Test IMapper::get(Smpte2094_10)
1537  */
TEST_P(GraphicsMapperHidlTest,GetSmpte2094_10)1538 TEST_P(GraphicsMapperHidlTest, GetSmpte2094_10) {
1539     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_10,
1540             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1541                 std::optional<std::vector<uint8_t>> smpte2094_10;
1542                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_10(vec, &smpte2094_10));
1543                 EXPECT_FALSE(smpte2094_10.has_value());
1544             });
1545 }
1546 
1547 /**
1548  * Test IMapper::get(metadata) with a bad buffer
1549  */
TEST_P(GraphicsMapperHidlTest,GetMetadataBadValue)1550 TEST_P(GraphicsMapperHidlTest, GetMetadataBadValue) {
1551     const native_handle_t* bufferHandle = nullptr;
1552     hidl_vec<uint8_t> vec;
1553     ASSERT_EQ(Error::BAD_BUFFER,
1554               mGralloc->get(bufferHandle, gralloc4::MetadataType_BufferId, &vec));
1555     ASSERT_EQ(0, vec.size());
1556     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Name, &vec));
1557     ASSERT_EQ(0, vec.size());
1558     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Width, &vec));
1559     ASSERT_EQ(0, vec.size());
1560     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Height, &vec));
1561     ASSERT_EQ(0, vec.size());
1562     ASSERT_EQ(Error::BAD_BUFFER,
1563               mGralloc->get(bufferHandle, gralloc4::MetadataType_LayerCount, &vec));
1564     ASSERT_EQ(0, vec.size());
1565     ASSERT_EQ(Error::BAD_BUFFER,
1566               mGralloc->get(bufferHandle, gralloc4::MetadataType_PixelFormatRequested, &vec));
1567     ASSERT_EQ(0, vec.size());
1568     ASSERT_EQ(Error::BAD_BUFFER,
1569               mGralloc->get(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC, &vec));
1570     ASSERT_EQ(0, vec.size());
1571     ASSERT_EQ(Error::BAD_BUFFER,
1572               mGralloc->get(bufferHandle, gralloc4::MetadataType_PixelFormatModifier, &vec));
1573     ASSERT_EQ(0, vec.size());
1574     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Usage, &vec));
1575     ASSERT_EQ(0, vec.size());
1576     ASSERT_EQ(Error::BAD_BUFFER,
1577               mGralloc->get(bufferHandle, gralloc4::MetadataType_AllocationSize, &vec));
1578     ASSERT_EQ(0, vec.size());
1579     ASSERT_EQ(Error::BAD_BUFFER,
1580               mGralloc->get(bufferHandle, gralloc4::MetadataType_ProtectedContent, &vec));
1581     ASSERT_EQ(0, vec.size());
1582     ASSERT_EQ(Error::BAD_BUFFER,
1583               mGralloc->get(bufferHandle, gralloc4::MetadataType_Compression, &vec));
1584     ASSERT_EQ(0, vec.size());
1585     ASSERT_EQ(Error::BAD_BUFFER,
1586               mGralloc->get(bufferHandle, gralloc4::MetadataType_Interlaced, &vec));
1587     ASSERT_EQ(0, vec.size());
1588     ASSERT_EQ(Error::BAD_BUFFER,
1589               mGralloc->get(bufferHandle, gralloc4::MetadataType_ChromaSiting, &vec));
1590     ASSERT_EQ(0, vec.size());
1591     ASSERT_EQ(Error::BAD_BUFFER,
1592               mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
1593     ASSERT_EQ(0, vec.size());
1594     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Crop, &vec));
1595     ASSERT_EQ(0, vec.size());
1596     ASSERT_EQ(Error::BAD_BUFFER,
1597               mGralloc->get(bufferHandle, gralloc4::MetadataType_Dataspace, &vec));
1598     ASSERT_EQ(0, vec.size());
1599     ASSERT_EQ(Error::BAD_BUFFER,
1600               mGralloc->get(bufferHandle, gralloc4::MetadataType_BlendMode, &vec));
1601     ASSERT_EQ(0, vec.size());
1602     ASSERT_EQ(Error::BAD_BUFFER,
1603               mGralloc->get(bufferHandle, gralloc4::MetadataType_Smpte2086, &vec));
1604     ASSERT_EQ(0, vec.size());
1605     ASSERT_EQ(Error::BAD_BUFFER,
1606               mGralloc->get(bufferHandle, gralloc4::MetadataType_Cta861_3, &vec));
1607     ASSERT_EQ(0, vec.size());
1608     ASSERT_EQ(Error::BAD_BUFFER,
1609               mGralloc->get(bufferHandle, gralloc4::MetadataType_Smpte2094_40, &vec));
1610     ASSERT_EQ(0, vec.size());
1611     ASSERT_EQ(Error::BAD_BUFFER,
1612               mGralloc->get(bufferHandle, gralloc4::MetadataType_Smpte2094_10, &vec));
1613     ASSERT_EQ(0, vec.size());
1614 }
1615 
1616 /**
1617  * Test IMapper::get(metadata) for unsupported metadata
1618  */
TEST_P(GraphicsMapperHidlTest,GetUnsupportedMetadata)1619 TEST_P(GraphicsMapperHidlTest, GetUnsupportedMetadata) {
1620     const native_handle_t* bufferHandle = nullptr;
1621     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
1622 
1623     MetadataType metadataTypeFake = {"FAKE", 1};
1624 
1625     hidl_vec<uint8_t> vec;
1626     ASSERT_EQ(Error::UNSUPPORTED, mGralloc->get(bufferHandle, metadataTypeFake, &vec));
1627     ASSERT_EQ(0, vec.size());
1628 }
1629 
1630 /**
1631  * Test IMapper::get(metadata) for unsupported standard metadata
1632  */
TEST_P(GraphicsMapperHidlTest,GetUnsupportedStandardMetadata)1633 TEST_P(GraphicsMapperHidlTest, GetUnsupportedStandardMetadata) {
1634     const native_handle_t* bufferHandle = nullptr;
1635     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
1636 
1637     MetadataType metadataTypeFake = {GRALLOC4_STANDARD_METADATA_TYPE, 9999};
1638 
1639     hidl_vec<uint8_t> vec;
1640     ASSERT_EQ(Error::UNSUPPORTED, mGralloc->get(bufferHandle, metadataTypeFake, &vec));
1641     ASSERT_EQ(0, vec.size());
1642 }
1643 
1644 /**
1645  * Test IMapper::set(PixelFormatFourCC)
1646  */
TEST_P(GraphicsMapperHidlTest,SetPixelFormatFourCC)1647 TEST_P(GraphicsMapperHidlTest, SetPixelFormatFourCC) {
1648     uint32_t pixelFormatFourCC = 0x34324142;  // DRM_FORMAT_BGRA8888
1649     hidl_vec<uint8_t> vec;
1650     ASSERT_EQ(NO_ERROR, gralloc4::encodePixelFormatFourCC(pixelFormatFourCC, &vec));
1651 
1652     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatFourCC, vec,
1653             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1654                 uint32_t realPixelFormatFourCC = 0;
1655                 ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, &realPixelFormatFourCC));
1656                 EXPECT_EQ(pixelFormatFourCC, realPixelFormatFourCC);
1657             });
1658 }
1659 
1660 /**
1661  * Test IMapper::set(PixelFormatModifier)
1662  */
TEST_P(GraphicsMapperHidlTest,SetPixelFormatModifier)1663 TEST_P(GraphicsMapperHidlTest, SetPixelFormatModifier) {
1664     uint64_t pixelFormatModifier = 10;
1665     hidl_vec<uint8_t> vec;
1666     ASSERT_EQ(NO_ERROR, gralloc4::encodePixelFormatModifier(pixelFormatModifier, &vec));
1667 
1668     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatModifier, vec,
1669             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1670                 uint64_t realPixelFormatModifier = 0;
1671                 ASSERT_EQ(NO_ERROR,
1672                           gralloc4::decodePixelFormatModifier(vec, &realPixelFormatModifier));
1673                 EXPECT_EQ(pixelFormatModifier, realPixelFormatModifier);
1674             });
1675 }
1676 
1677 /**
1678  * Test IMapper::set(AllocationSize)
1679  */
TEST_P(GraphicsMapperHidlTest,SetAllocationSize)1680 TEST_P(GraphicsMapperHidlTest, SetAllocationSize) {
1681     uint64_t allocationSize = 1000000;
1682     hidl_vec<uint8_t> vec;
1683     ASSERT_EQ(NO_ERROR, gralloc4::encodeAllocationSize(allocationSize, &vec));
1684 
1685     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_AllocationSize, vec,
1686             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1687                 uint64_t realAllocationSize = 0;
1688                 ASSERT_EQ(NO_ERROR, gralloc4::decodeAllocationSize(vec, &realAllocationSize));
1689                 EXPECT_EQ(allocationSize, realAllocationSize);
1690             });
1691 }
1692 
1693 /**
1694  * Test IMapper::set(ProtectedContent)
1695  */
TEST_P(GraphicsMapperHidlTest,SetProtectedContent)1696 TEST_P(GraphicsMapperHidlTest, SetProtectedContent) {
1697     const native_handle_t* bufferHandle = nullptr;
1698     auto info = mDummyDescriptorInfo;
1699     info.usage = BufferUsage::PROTECTED | BufferUsage::COMPOSER_OVERLAY;
1700 
1701     bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceAllErrors);
1702     if (!bufferHandle) {
1703         GTEST_SUCCEED() << "unable to allocate protected content";
1704         return;
1705     }
1706 
1707     uint64_t protectedContent = 0;
1708     hidl_vec<uint8_t> vec;
1709     ASSERT_EQ(NO_ERROR, gralloc4::encodeProtectedContent(protectedContent, &vec));
1710 
1711     Error err = mGralloc->set(bufferHandle, gralloc4::MetadataType_ProtectedContent, vec);
1712     ASSERT_EQ(err, Error::UNSUPPORTED);
1713     vec.resize(0);
1714 
1715     uint64_t realProtectedContent = 0;
1716     ASSERT_EQ(Error::NONE,
1717               mGralloc->get(bufferHandle, gralloc4::MetadataType_ProtectedContent, &vec));
1718     ASSERT_EQ(NO_ERROR, gralloc4::decodeProtectedContent(vec, &realProtectedContent));
1719     EXPECT_EQ(1, realProtectedContent);
1720 }
1721 
1722 /**
1723  * Test IMapper::set(Compression)
1724  */
TEST_P(GraphicsMapperHidlTest,SetCompression)1725 TEST_P(GraphicsMapperHidlTest, SetCompression) {
1726     auto info = mDummyDescriptorInfo;
1727     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
1728 
1729     ExtendableType compression = gralloc4::Compression_DisplayStreamCompression;
1730     hidl_vec<uint8_t> vec;
1731     ASSERT_EQ(NO_ERROR, gralloc4::encodeCompression(compression, &vec));
1732 
1733     testSet(info, gralloc4::MetadataType_Compression, vec,
1734             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1735                 ExtendableType realCompression = gralloc4::Compression_None;
1736                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCompression(vec, &realCompression));
1737 
1738                 EXPECT_EQ(compression.name, realCompression.name);
1739                 EXPECT_EQ(compression.value, realCompression.value);
1740             });
1741 }
1742 
1743 /**
1744  * Test IMapper::set(Interlaced)
1745  */
TEST_P(GraphicsMapperHidlTest,SetInterlaced)1746 TEST_P(GraphicsMapperHidlTest, SetInterlaced) {
1747     ExtendableType interlaced = gralloc4::Interlaced_RightLeft;
1748     hidl_vec<uint8_t> vec;
1749     ASSERT_EQ(NO_ERROR, gralloc4::encodeInterlaced(interlaced, &vec));
1750 
1751     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Interlaced, vec,
1752             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1753                 ExtendableType realInterlaced = gralloc4::Interlaced_None;
1754                 ASSERT_EQ(NO_ERROR, gralloc4::decodeInterlaced(vec, &realInterlaced));
1755 
1756                 EXPECT_EQ(interlaced.name, realInterlaced.name);
1757                 EXPECT_EQ(interlaced.value, realInterlaced.value);
1758             });
1759 }
1760 
1761 /**
1762  * Test IMapper::set(ChromaSiting)
1763  */
TEST_P(GraphicsMapperHidlTest,SetChromaSiting)1764 TEST_P(GraphicsMapperHidlTest, SetChromaSiting) {
1765     ExtendableType chromaSiting = gralloc4::ChromaSiting_SitedInterstitial;
1766     hidl_vec<uint8_t> vec;
1767     ASSERT_EQ(NO_ERROR, gralloc4::encodeChromaSiting(chromaSiting, &vec));
1768 
1769     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_ChromaSiting, vec,
1770             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1771                 ExtendableType realChromaSiting = gralloc4::ChromaSiting_None;
1772                 ASSERT_EQ(NO_ERROR, gralloc4::decodeChromaSiting(vec, &realChromaSiting));
1773 
1774                 EXPECT_EQ(chromaSiting.name, realChromaSiting.name);
1775                 EXPECT_EQ(chromaSiting.value, realChromaSiting.value);
1776             });
1777 }
1778 
1779 /**
1780  * Test IMapper::set(PlaneLayouts)
1781  */
TEST_P(GraphicsMapperHidlTest,SetPlaneLayouts)1782 TEST_P(GraphicsMapperHidlTest, SetPlaneLayouts) {
1783     const native_handle_t* bufferHandle = nullptr;
1784     auto info = mDummyDescriptorInfo;
1785     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
1786 
1787     std::vector<PlaneLayout> planeLayouts;
1788     PlaneLayout planeLayoutA;
1789     PlaneLayout planeLayoutRGB;
1790     PlaneLayoutComponent component;
1791 
1792     planeLayoutA.offsetInBytes = 0;
1793     planeLayoutA.sampleIncrementInBits = 8;
1794     planeLayoutA.strideInBytes = info.width + 20;
1795     planeLayoutA.widthInSamples = info.width;
1796     planeLayoutA.heightInSamples = info.height;
1797     planeLayoutA.totalSizeInBytes = planeLayoutA.strideInBytes * info.height;
1798     planeLayoutA.horizontalSubsampling = 1;
1799     planeLayoutA.verticalSubsampling = 1;
1800 
1801     component.type = gralloc4::PlaneLayoutComponentType_A;
1802     component.offsetInBits = 0;
1803     component.sizeInBits = 8;
1804     planeLayoutA.components.push_back(component);
1805 
1806     planeLayouts.push_back(planeLayoutA);
1807 
1808     planeLayoutRGB.offsetInBytes = 0;
1809     planeLayoutRGB.sampleIncrementInBits = 24;
1810     planeLayoutRGB.strideInBytes = info.width + 20;
1811     planeLayoutRGB.widthInSamples = info.width;
1812     planeLayoutRGB.heightInSamples = info.height;
1813     planeLayoutRGB.totalSizeInBytes = planeLayoutRGB.strideInBytes * info.height;
1814     planeLayoutRGB.horizontalSubsampling = 1;
1815     planeLayoutRGB.verticalSubsampling = 1;
1816 
1817     component.type = gralloc4::PlaneLayoutComponentType_R;
1818     planeLayoutRGB.components.push_back(component);
1819     component.type = gralloc4::PlaneLayoutComponentType_G;
1820     planeLayoutRGB.components.push_back(component);
1821     component.type = gralloc4::PlaneLayoutComponentType_B;
1822     planeLayoutRGB.components.push_back(component);
1823 
1824     planeLayouts.push_back(planeLayoutRGB);
1825 
1826     hidl_vec<uint8_t> vec;
1827     ASSERT_EQ(NO_ERROR, gralloc4::encodePlaneLayouts(planeLayouts, &vec));
1828 
1829     Error err = mGralloc->set(bufferHandle, gralloc4::MetadataType_PlaneLayouts, vec);
1830     if (err == Error::UNSUPPORTED) {
1831         GTEST_SUCCEED() << "setting this metadata is unsupported";
1832         return;
1833     }
1834     ASSERT_EQ(err, Error::NONE);
1835 
1836     std::vector<PlaneLayout> realPlaneLayouts;
1837     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &realPlaneLayouts));
1838 
1839     ASSERT_EQ(planeLayouts.size(), realPlaneLayouts.size());
1840 
1841     for (int i = 0; i < realPlaneLayouts.size(); i++) {
1842         const auto& planeLayout = planeLayouts[i];
1843         const auto& realPlaneLayout = realPlaneLayouts[i];
1844 
1845         EXPECT_EQ(planeLayout.offsetInBytes, realPlaneLayout.offsetInBytes);
1846         EXPECT_EQ(planeLayout.sampleIncrementInBits, realPlaneLayout.sampleIncrementInBits);
1847         EXPECT_EQ(planeLayout.strideInBytes, realPlaneLayout.strideInBytes);
1848         EXPECT_EQ(planeLayout.widthInSamples, realPlaneLayout.widthInSamples);
1849         EXPECT_EQ(planeLayout.heightInSamples, realPlaneLayout.heightInSamples);
1850         EXPECT_LE(planeLayout.totalSizeInBytes, realPlaneLayout.totalSizeInBytes);
1851         EXPECT_EQ(planeLayout.horizontalSubsampling, realPlaneLayout.horizontalSubsampling);
1852         EXPECT_EQ(planeLayout.verticalSubsampling, realPlaneLayout.verticalSubsampling);
1853 
1854         ASSERT_EQ(planeLayout.components.size(), realPlaneLayout.components.size());
1855 
1856         for (int j = 0; j < realPlaneLayout.components.size(); j++) {
1857             const auto& component = planeLayout.components[j];
1858             const auto& realComponent = realPlaneLayout.components[j];
1859 
1860             EXPECT_EQ(component.type.name, realComponent.type.name);
1861             EXPECT_EQ(component.type.value, realComponent.type.value);
1862             EXPECT_EQ(component.sizeInBits, realComponent.sizeInBits);
1863             EXPECT_EQ(component.offsetInBits, realComponent.offsetInBits);
1864         }
1865     }
1866 }
1867 
1868 /**
1869  * Test IMapper::set(Crop)
1870  */
TEST_P(GraphicsMapperHidlTest,SetCrop)1871 TEST_P(GraphicsMapperHidlTest, SetCrop) {
1872     std::vector<aidl::android::hardware::graphics::common::Rect> crops{{0, 0, 32, 32}};
1873     hidl_vec<uint8_t> vec;
1874     ASSERT_EQ(NO_ERROR, gralloc4::encodeCrop(crops, &vec));
1875 
1876     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Crop, vec,
1877             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1878                 std::vector<aidl::android::hardware::graphics::common::Rect> realCrops;
1879                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCrop(vec, &realCrops));
1880                 ASSERT_EQ(1, realCrops.size());
1881                 ASSERT_EQ(crops.front().left, realCrops.front().left);
1882                 ASSERT_EQ(crops.front().top, realCrops.front().top);
1883                 ASSERT_EQ(crops.front().right, realCrops.front().right);
1884                 ASSERT_EQ(crops.front().bottom, realCrops.front().bottom);
1885             });
1886 }
1887 
1888 /**
1889  * Test IMapper::set(Dataspace)
1890  */
TEST_P(GraphicsMapperHidlTest,SetDataspace)1891 TEST_P(GraphicsMapperHidlTest, SetDataspace) {
1892     Dataspace dataspace = Dataspace::SRGB_LINEAR;
1893     hidl_vec<uint8_t> vec;
1894     ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(dataspace, &vec));
1895 
1896     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Dataspace, vec,
1897             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1898                 Dataspace realDataspace = Dataspace::UNKNOWN;
1899                 ASSERT_EQ(NO_ERROR, gralloc4::decodeDataspace(vec, &realDataspace));
1900                 EXPECT_EQ(dataspace, realDataspace);
1901             });
1902 }
1903 
1904 /**
1905  * Test IMapper::set(BlendMode)
1906  */
TEST_P(GraphicsMapperHidlTest,SetBlendMode)1907 TEST_P(GraphicsMapperHidlTest, SetBlendMode) {
1908     BlendMode blendMode = BlendMode::PREMULTIPLIED;
1909     hidl_vec<uint8_t> vec;
1910     ASSERT_EQ(NO_ERROR, gralloc4::encodeBlendMode(blendMode, &vec));
1911 
1912     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_BlendMode, vec,
1913             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1914                 BlendMode realBlendMode = BlendMode::INVALID;
1915                 ASSERT_EQ(NO_ERROR, gralloc4::decodeBlendMode(vec, &realBlendMode));
1916                 EXPECT_EQ(blendMode, realBlendMode);
1917             });
1918 }
1919 
1920 /**
1921  * Test IMapper::set(Smpte2086)
1922  */
TEST_P(GraphicsMapperHidlTest,SetSmpte2086)1923 TEST_P(GraphicsMapperHidlTest, SetSmpte2086) {
1924     /**
1925      * DISPLAY_P3 is a color space that uses the DCI_P3 primaries,
1926      * the D65 white point and the SRGB transfer functions.
1927      * Rendering Intent: Colorimetric
1928      * Primaries:
1929      *                  x       y
1930      *  green           0.265   0.690
1931      *  blue            0.150   0.060
1932      *  red             0.680   0.320
1933      *  white (D65)     0.3127  0.3290
1934      */
1935     Smpte2086 smpte2086;
1936     smpte2086.primaryRed.x = 0.680;
1937     smpte2086.primaryRed.y = 0.320;
1938     smpte2086.primaryGreen.x = 0.265;
1939     smpte2086.primaryGreen.y = 0.690;
1940     smpte2086.primaryBlue.x = 0.150;
1941     smpte2086.primaryBlue.y = 0.060;
1942     smpte2086.whitePoint.x = 0.3127;
1943     smpte2086.whitePoint.y = 0.3290;
1944     smpte2086.maxLuminance = 100.0;
1945     smpte2086.minLuminance = 0.1;
1946 
1947     hidl_vec<uint8_t> vec;
1948     ASSERT_EQ(NO_ERROR, gralloc4::encodeSmpte2086(smpte2086, &vec));
1949 
1950     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2086, vec,
1951             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1952                 std::optional<Smpte2086> realSmpte2086;
1953                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2086(vec, &realSmpte2086));
1954                 ASSERT_TRUE(realSmpte2086.has_value());
1955                 EXPECT_TRUE(isEqual(smpte2086.primaryRed.x, realSmpte2086->primaryRed.x));
1956                 EXPECT_TRUE(isEqual(smpte2086.primaryRed.y, realSmpte2086->primaryRed.y));
1957                 EXPECT_TRUE(isEqual(smpte2086.primaryGreen.x, realSmpte2086->primaryGreen.x));
1958                 EXPECT_TRUE(isEqual(smpte2086.primaryGreen.y, realSmpte2086->primaryGreen.y));
1959                 EXPECT_TRUE(isEqual(smpte2086.primaryBlue.x, realSmpte2086->primaryBlue.x));
1960                 EXPECT_TRUE(isEqual(smpte2086.primaryBlue.y, realSmpte2086->primaryBlue.y));
1961                 EXPECT_TRUE(isEqual(smpte2086.whitePoint.x, realSmpte2086->whitePoint.x));
1962                 EXPECT_TRUE(isEqual(smpte2086.whitePoint.y, realSmpte2086->whitePoint.y));
1963                 EXPECT_TRUE(isEqual(smpte2086.maxLuminance, realSmpte2086->maxLuminance));
1964                 EXPECT_TRUE(isEqual(smpte2086.minLuminance, realSmpte2086->minLuminance));
1965             });
1966 }
1967 
1968 /**
1969  * Test IMapper::set(Cta8613)
1970  */
TEST_P(GraphicsMapperHidlTest,SetCta861_3)1971 TEST_P(GraphicsMapperHidlTest, SetCta861_3) {
1972     Cta861_3 cta861_3;
1973     cta861_3.maxContentLightLevel = 78.0;
1974     cta861_3.maxFrameAverageLightLevel = 62.0;
1975 
1976     hidl_vec<uint8_t> vec;
1977     ASSERT_EQ(NO_ERROR, gralloc4::encodeCta861_3(cta861_3, &vec));
1978 
1979     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Cta861_3, vec,
1980             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1981                 std::optional<Cta861_3> realCta861_3;
1982                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCta861_3(vec, &realCta861_3));
1983                 ASSERT_TRUE(realCta861_3.has_value());
1984                 EXPECT_TRUE(
1985                         isEqual(cta861_3.maxContentLightLevel, realCta861_3->maxContentLightLevel));
1986                 EXPECT_TRUE(isEqual(cta861_3.maxFrameAverageLightLevel,
1987                                     realCta861_3->maxFrameAverageLightLevel));
1988             });
1989 }
1990 
1991 /**
1992  * Test IMapper::set(Smpte2094_40)
1993  */
TEST_P(GraphicsMapperHidlTest,SetSmpte2094_40)1994 TEST_P(GraphicsMapperHidlTest, SetSmpte2094_40) {
1995     hidl_vec<uint8_t> vec;
1996 
1997     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_40, vec,
1998             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1999                 std::optional<std::vector<uint8_t>> realSmpte2094_40;
2000                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, &realSmpte2094_40));
2001                 EXPECT_FALSE(realSmpte2094_40.has_value());
2002             });
2003 }
2004 
2005 /**
2006  * Test IMapper::set(Smpte2094_10)
2007  */
TEST_P(GraphicsMapperHidlTest,SetSmpte2094_10)2008 TEST_P(GraphicsMapperHidlTest, SetSmpte2094_10) {
2009     hidl_vec<uint8_t> vec;
2010 
2011     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_10, vec,
2012             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
2013                 std::optional<std::vector<uint8_t>> realSmpte2094_10;
2014                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_10(vec, &realSmpte2094_10));
2015                 EXPECT_FALSE(realSmpte2094_10.has_value());
2016             });
2017 }
2018 
2019 /**
2020  * Test IMapper::set(metadata) with a bad buffer
2021  */
TEST_P(GraphicsMapperHidlTest,SetMetadataNullBuffer)2022 TEST_P(GraphicsMapperHidlTest, SetMetadataNullBuffer) {
2023     const native_handle_t* bufferHandle = nullptr;
2024     hidl_vec<uint8_t> vec;
2025     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_BufferId, vec));
2026     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Name, vec));
2027     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Width, vec));
2028     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Height, vec));
2029     ASSERT_EQ(Error::BAD_BUFFER,
2030               mGralloc->set(bufferHandle, gralloc4::MetadataType_LayerCount, vec));
2031     ASSERT_EQ(Error::BAD_BUFFER,
2032               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatRequested, vec));
2033     ASSERT_EQ(Error::BAD_BUFFER,
2034               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC, vec));
2035     ASSERT_EQ(Error::BAD_BUFFER,
2036               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatModifier, vec));
2037     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Usage, vec));
2038     ASSERT_EQ(Error::BAD_BUFFER,
2039               mGralloc->set(bufferHandle, gralloc4::MetadataType_AllocationSize, vec));
2040     ASSERT_EQ(Error::BAD_BUFFER,
2041               mGralloc->set(bufferHandle, gralloc4::MetadataType_ProtectedContent, vec));
2042     ASSERT_EQ(Error::BAD_BUFFER,
2043               mGralloc->set(bufferHandle, gralloc4::MetadataType_Compression, vec));
2044     ASSERT_EQ(Error::BAD_BUFFER,
2045               mGralloc->set(bufferHandle, gralloc4::MetadataType_Interlaced, vec));
2046     ASSERT_EQ(Error::BAD_BUFFER,
2047               mGralloc->set(bufferHandle, gralloc4::MetadataType_ChromaSiting, vec));
2048     ASSERT_EQ(Error::BAD_BUFFER,
2049               mGralloc->set(bufferHandle, gralloc4::MetadataType_PlaneLayouts, vec));
2050     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Crop, vec));
2051     ASSERT_EQ(Error::BAD_BUFFER,
2052               mGralloc->set(bufferHandle, gralloc4::MetadataType_Dataspace, vec));
2053     ASSERT_EQ(Error::BAD_BUFFER,
2054               mGralloc->set(bufferHandle, gralloc4::MetadataType_BlendMode, vec));
2055     ASSERT_EQ(Error::BAD_BUFFER,
2056               mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2086, vec));
2057     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Cta861_3, vec));
2058     ASSERT_EQ(Error::BAD_BUFFER,
2059               mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2094_40, vec));
2060     ASSERT_EQ(Error::BAD_BUFFER,
2061               mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2094_10, vec));
2062 }
2063 
2064 /**
2065  * Test get::metadata with cloned native_handle
2066  */
TEST_P(GraphicsMapperHidlTest,GetMetadataClonedHandle)2067 TEST_P(GraphicsMapperHidlTest, GetMetadataClonedHandle) {
2068     const native_handle_t* bufferHandle = nullptr;
2069     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2070 
2071     const auto dataspace = Dataspace::SRGB_LINEAR;
2072     {
2073         hidl_vec<uint8_t> metadata;
2074         ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(dataspace, &metadata));
2075 
2076         Error err = mGralloc->set(bufferHandle, gralloc4::MetadataType_Dataspace, metadata);
2077         if (err == Error::UNSUPPORTED) {
2078             GTEST_SUCCEED() << "setting this metadata is unsupported";
2079             return;
2080         }
2081         ASSERT_EQ(Error::NONE, err);
2082     }
2083 
2084     const native_handle_t* importedHandle;
2085     {
2086         auto clonedHandle = native_handle_clone(bufferHandle);
2087         ASSERT_NO_FATAL_FAILURE(importedHandle = mGralloc->importBuffer(clonedHandle));
2088         native_handle_close(clonedHandle);
2089         native_handle_delete(clonedHandle);
2090     }
2091 
2092     Dataspace realSpace = Dataspace::UNKNOWN;
2093     {
2094         hidl_vec<uint8_t> metadata;
2095         ASSERT_EQ(Error::NONE,
2096                   mGralloc->get(importedHandle, gralloc4::MetadataType_Dataspace, &metadata));
2097         ASSERT_NO_FATAL_FAILURE(gralloc4::decodeDataspace(metadata, &realSpace));
2098     }
2099 
2100     EXPECT_EQ(dataspace, realSpace);
2101 }
2102 
2103 /**
2104  * Test set::metadata with cloned native_handle
2105  */
TEST_P(GraphicsMapperHidlTest,SetMetadataClonedHandle)2106 TEST_P(GraphicsMapperHidlTest, SetMetadataClonedHandle) {
2107     const native_handle_t* bufferHandle = nullptr;
2108     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2109 
2110     const native_handle_t* importedHandle;
2111     {
2112         auto clonedHandle = native_handle_clone(bufferHandle);
2113         ASSERT_NO_FATAL_FAILURE(importedHandle = mGralloc->importBuffer(clonedHandle));
2114         native_handle_close(clonedHandle);
2115         native_handle_delete(clonedHandle);
2116     }
2117 
2118     const auto dataspace = Dataspace::SRGB_LINEAR;
2119     {
2120         hidl_vec<uint8_t> metadata;
2121         ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(dataspace, &metadata));
2122 
2123         Error err = mGralloc->set(importedHandle, gralloc4::MetadataType_Dataspace, metadata);
2124         if (err == Error::UNSUPPORTED) {
2125             GTEST_SUCCEED() << "setting this metadata is unsupported";
2126             return;
2127         }
2128         ASSERT_EQ(Error::NONE, err);
2129     }
2130 
2131     Dataspace realSpace = Dataspace::UNKNOWN;
2132     {
2133         hidl_vec<uint8_t> metadata;
2134         ASSERT_EQ(Error::NONE,
2135                   mGralloc->get(bufferHandle, gralloc4::MetadataType_Dataspace, &metadata));
2136         ASSERT_NO_FATAL_FAILURE(gralloc4::decodeDataspace(metadata, &realSpace));
2137     }
2138 
2139     EXPECT_EQ(dataspace, realSpace);
2140 }
2141 
2142 /**
2143  * Test IMapper::set(metadata) for constant metadata
2144  */
TEST_P(GraphicsMapperHidlTest,SetConstantMetadata)2145 TEST_P(GraphicsMapperHidlTest, SetConstantMetadata) {
2146     const native_handle_t* bufferHandle = nullptr;
2147     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2148 
2149     uint64_t bufferId = 2;
2150     hidl_vec<uint8_t> bufferIdVec;
2151     ASSERT_EQ(NO_ERROR, gralloc4::encodeBufferId(bufferId, &bufferIdVec));
2152     ASSERT_EQ(Error::BAD_VALUE,
2153               mGralloc->set(bufferHandle, gralloc4::MetadataType_BufferId, bufferIdVec));
2154 
2155     std::string name{"new name"};
2156     hidl_vec<uint8_t> nameVec;
2157     ASSERT_EQ(NO_ERROR, gralloc4::encodeName(name, &nameVec));
2158     ASSERT_EQ(Error::BAD_VALUE, mGralloc->set(bufferHandle, gralloc4::MetadataType_Name, nameVec));
2159 
2160     uint64_t width = 32;
2161     hidl_vec<uint8_t> widthVec;
2162     ASSERT_EQ(NO_ERROR, gralloc4::encodeWidth(width, &widthVec));
2163     ASSERT_EQ(Error::BAD_VALUE,
2164               mGralloc->set(bufferHandle, gralloc4::MetadataType_Width, widthVec));
2165 
2166     uint64_t height = 32;
2167     hidl_vec<uint8_t> heightVec;
2168     ASSERT_EQ(NO_ERROR, gralloc4::encodeHeight(height, &heightVec));
2169     ASSERT_EQ(Error::BAD_VALUE,
2170               mGralloc->set(bufferHandle, gralloc4::MetadataType_Height, heightVec));
2171 
2172     uint64_t layerCount = 2;
2173     hidl_vec<uint8_t> layerCountVec;
2174     ASSERT_EQ(NO_ERROR, gralloc4::encodeLayerCount(layerCount, &layerCountVec));
2175     ASSERT_EQ(Error::BAD_VALUE,
2176               mGralloc->set(bufferHandle, gralloc4::MetadataType_LayerCount, layerCountVec));
2177 
2178     hardware::graphics::common::V1_2::PixelFormat pixelFormatRequested = PixelFormat::BLOB;
2179     hidl_vec<uint8_t> pixelFormatRequestedVec;
2180     ASSERT_EQ(NO_ERROR,
2181               gralloc4::encodePixelFormatRequested(pixelFormatRequested, &pixelFormatRequestedVec));
2182     ASSERT_EQ(Error::BAD_VALUE,
2183               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatRequested,
2184                             pixelFormatRequestedVec));
2185 
2186     uint64_t usage = 0;
2187     hidl_vec<uint8_t> usageVec;
2188     ASSERT_EQ(NO_ERROR, gralloc4::encodeUsage(usage, &usageVec));
2189     ASSERT_EQ(Error::BAD_VALUE,
2190               mGralloc->set(bufferHandle, gralloc4::MetadataType_Usage, usageVec));
2191 }
2192 
2193 /**
2194  * Test IMapper::set(metadata) for bad metadata
2195  */
TEST_P(GraphicsMapperHidlTest,SetBadMetadata)2196 TEST_P(GraphicsMapperHidlTest, SetBadMetadata) {
2197     const native_handle_t* bufferHandle = nullptr;
2198     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2199 
2200     hidl_vec<uint8_t> vec;
2201     ASSERT_EQ(Error::UNSUPPORTED,
2202               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC, vec));
2203     ASSERT_EQ(Error::UNSUPPORTED,
2204               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatModifier, vec));
2205     ASSERT_EQ(Error::UNSUPPORTED,
2206               mGralloc->set(bufferHandle, gralloc4::MetadataType_AllocationSize, vec));
2207     ASSERT_EQ(Error::UNSUPPORTED,
2208               mGralloc->set(bufferHandle, gralloc4::MetadataType_ProtectedContent, vec));
2209     ASSERT_EQ(Error::UNSUPPORTED,
2210               mGralloc->set(bufferHandle, gralloc4::MetadataType_Compression, vec));
2211     ASSERT_EQ(Error::UNSUPPORTED,
2212               mGralloc->set(bufferHandle, gralloc4::MetadataType_Interlaced, vec));
2213     ASSERT_EQ(Error::UNSUPPORTED,
2214               mGralloc->set(bufferHandle, gralloc4::MetadataType_ChromaSiting, vec));
2215     ASSERT_EQ(Error::UNSUPPORTED,
2216               mGralloc->set(bufferHandle, gralloc4::MetadataType_PlaneLayouts, vec));
2217     ASSERT_EQ(Error::UNSUPPORTED, mGralloc->set(bufferHandle, gralloc4::MetadataType_Crop, vec));
2218     ASSERT_EQ(Error::UNSUPPORTED,
2219               mGralloc->set(bufferHandle, gralloc4::MetadataType_Dataspace, vec));
2220     ASSERT_EQ(Error::UNSUPPORTED,
2221               mGralloc->set(bufferHandle, gralloc4::MetadataType_BlendMode, vec));
2222 
2223     // Keep optional metadata types below and populate the encoded metadata vec
2224     // with some arbitrary different metadata because the common gralloc4::decode*()
2225     // functions do not distinguish between an empty vec and bad value.
2226     if (base::GetIntProperty("ro.vendor.api_level", __ANDROID_API_FUTURE__) >= __ANDROID_API_T__) {
2227         // Some old grallocs shipped with broken validation.
2228         ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(Dataspace::SRGB_LINEAR, &vec));
2229         ASSERT_EQ(Error::UNSUPPORTED,
2230                   mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2086, vec));
2231         ASSERT_EQ(Error::UNSUPPORTED,
2232                   mGralloc->set(bufferHandle, gralloc4::MetadataType_Cta861_3, vec));
2233     }
2234 }
2235 
2236 /**
2237  * Test IMapper::getFromBufferDescriptorInfo(BufferId)
2238  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoBufferId)2239 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoBufferId) {
2240     hidl_vec<uint8_t> vec;
2241     ASSERT_EQ(Error::UNSUPPORTED,
2242               mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2243                                                     gralloc4::MetadataType_BufferId, &vec));
2244 }
2245 
2246 /**
2247  * Test IMapper::getFromBufferDescriptorInfo(Name)
2248  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoName)2249 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoName) {
2250     hidl_vec<uint8_t> vec;
2251     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2252                                    mDummyDescriptorInfo, gralloc4::MetadataType_Name, &vec));
2253 
2254     std::string name;
2255     ASSERT_EQ(NO_ERROR, gralloc4::decodeName(vec, &name));
2256     EXPECT_EQ(mDummyDescriptorInfo.name, name);
2257 }
2258 
2259 /**
2260  * Test IMapper::getFromBufferDescriptorInfo(Width)
2261  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoWidth)2262 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoWidth) {
2263     hidl_vec<uint8_t> vec;
2264     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2265                                    mDummyDescriptorInfo, gralloc4::MetadataType_Width, &vec));
2266 
2267     uint64_t width = 0;
2268     ASSERT_EQ(NO_ERROR, gralloc4::decodeWidth(vec, &width));
2269     EXPECT_EQ(mDummyDescriptorInfo.width, width);
2270 }
2271 
2272 /**
2273  * Test IMapper::getFromBufferDescriptorInfo(Height)
2274  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoHeight)2275 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoHeight) {
2276     hidl_vec<uint8_t> vec;
2277     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2278                                    mDummyDescriptorInfo, gralloc4::MetadataType_Height, &vec));
2279 
2280     uint64_t height = 0;
2281     ASSERT_EQ(NO_ERROR, gralloc4::decodeHeight(vec, &height));
2282     EXPECT_EQ(mDummyDescriptorInfo.height, height);
2283 }
2284 
2285 /**
2286  * Test IMapper::getFromBufferDescriptorInfo(PixelFormatRequested)
2287  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPixelFormatRequested)2288 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPixelFormatRequested) {
2289     hidl_vec<uint8_t> vec;
2290     ASSERT_EQ(Error::NONE,
2291               mGralloc->getFromBufferDescriptorInfo(
2292                       mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatRequested, &vec));
2293 
2294     PixelFormat pixelFormatRequested = PixelFormat::BLOB;
2295     ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatRequested(vec, &pixelFormatRequested));
2296     EXPECT_EQ(mDummyDescriptorInfo.format, pixelFormatRequested);
2297 }
2298 
2299 /**
2300  * Test IMapper::getFromBufferDescriptorInfo(PixelFormatFourCC)
2301  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPixelFormatFourCC)2302 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPixelFormatFourCC) {
2303     hidl_vec<uint8_t> vec;
2304     Error err = mGralloc->getFromBufferDescriptorInfo(
2305             mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatFourCC, &vec);
2306     if (err == Error::UNSUPPORTED) {
2307         GTEST_SUCCEED() << "setting this metadata is unsupported";
2308         return;
2309     }
2310     ASSERT_EQ(err, Error::NONE);
2311 
2312     uint32_t pixelFormatFourCC = 0;
2313     ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, &pixelFormatFourCC));
2314 }
2315 
2316 /**
2317  * Test IMapper::getFromBufferDescriptorInfo(PixelFormatModifier)
2318  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPixelFormatModifier)2319 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPixelFormatModifier) {
2320     hidl_vec<uint8_t> vec;
2321     Error err = mGralloc->getFromBufferDescriptorInfo(
2322             mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatModifier, &vec);
2323     if (err == Error::UNSUPPORTED) {
2324         GTEST_SUCCEED() << "setting this metadata is unsupported";
2325         return;
2326     }
2327     ASSERT_EQ(err, Error::NONE);
2328 
2329     uint64_t pixelFormatModifier = 0;
2330     ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatModifier(vec, &pixelFormatModifier));
2331 }
2332 
2333 /**
2334  * Test IMapper::getFromBufferDescriptorInfo(Usage)
2335  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoUsage)2336 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoUsage) {
2337     hidl_vec<uint8_t> vec;
2338     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2339                                    mDummyDescriptorInfo, gralloc4::MetadataType_Usage, &vec));
2340 
2341     uint64_t usage = 0;
2342     ASSERT_EQ(NO_ERROR, gralloc4::decodeUsage(vec, &usage));
2343     EXPECT_EQ(mDummyDescriptorInfo.usage, usage);
2344 }
2345 
2346 /**
2347  * Test IMapper::getFromBufferDescriptorInfo(AllocationSize)
2348  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoAllocationSize)2349 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoAllocationSize) {
2350     hidl_vec<uint8_t> vec;
2351     Error err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2352                                                       gralloc4::MetadataType_AllocationSize, &vec);
2353     if (err == Error::UNSUPPORTED) {
2354         GTEST_SUCCEED() << "setting this metadata is unsupported";
2355         return;
2356     }
2357     ASSERT_EQ(err, Error::NONE);
2358 
2359     uint64_t allocationSize = 0;
2360     ASSERT_EQ(NO_ERROR, gralloc4::decodeAllocationSize(vec, &allocationSize));
2361 }
2362 
2363 /**
2364  * Test IMapper::getFromBufferDescriptorInfo(ProtectedContent)
2365  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoProtectedContent)2366 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoProtectedContent) {
2367     auto info = mDummyDescriptorInfo;
2368     info.usage = BufferUsage::PROTECTED | BufferUsage::COMPOSER_OVERLAY;
2369 
2370     hidl_vec<uint8_t> vec;
2371     auto err = mGralloc->getFromBufferDescriptorInfo(info, gralloc4::MetadataType_ProtectedContent,
2372                                                      &vec);
2373     if (err == Error::UNSUPPORTED) {
2374         GTEST_SUCCEED() << "setting this metadata is unsupported";
2375         return;
2376     }
2377     ASSERT_EQ(err, Error::NONE);
2378 
2379     uint64_t protectedContent = 0;
2380     ASSERT_EQ(NO_ERROR, gralloc4::decodeProtectedContent(vec, &protectedContent));
2381     EXPECT_EQ(1, protectedContent);
2382 }
2383 
2384 /**
2385  * Test IMapper::getFromBufferDescriptorInfo(Compression)
2386  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoCompression)2387 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoCompression) {
2388     auto info = mDummyDescriptorInfo;
2389     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
2390 
2391     hidl_vec<uint8_t> vec;
2392     auto err =
2393             mGralloc->getFromBufferDescriptorInfo(info, gralloc4::MetadataType_Compression, &vec);
2394     if (err == Error::UNSUPPORTED) {
2395         GTEST_SUCCEED() << "setting this metadata is unsupported";
2396         return;
2397     }
2398     ASSERT_EQ(err, Error::NONE);
2399 
2400     ExtendableType compression = gralloc4::Compression_DisplayStreamCompression;
2401     ASSERT_EQ(NO_ERROR, gralloc4::decodeCompression(vec, &compression));
2402 
2403     EXPECT_EQ(gralloc4::Compression_None.name, compression.name);
2404     EXPECT_EQ(gralloc4::Compression_None.value, compression.value);
2405 }
2406 
2407 /**
2408  * Test IMapper::getFromBufferDescriptorInfo(Interlaced)
2409  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoInterlaced)2410 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoInterlaced) {
2411     hidl_vec<uint8_t> vec;
2412     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2413                                                      gralloc4::MetadataType_Interlaced, &vec);
2414     if (err == Error::UNSUPPORTED) {
2415         GTEST_SUCCEED() << "setting this metadata is unsupported";
2416         return;
2417     }
2418     ASSERT_EQ(err, Error::NONE);
2419 
2420     ExtendableType interlaced = gralloc4::Interlaced_TopBottom;
2421     ASSERT_EQ(NO_ERROR, gralloc4::decodeInterlaced(vec, &interlaced));
2422 
2423     EXPECT_EQ(gralloc4::Interlaced_None.name, interlaced.name);
2424     EXPECT_EQ(gralloc4::Interlaced_None.value, interlaced.value);
2425 }
2426 
2427 /**
2428  * Test IMapper::getFromBufferDescriptorInfo(ChromaSiting)
2429  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoChromaSiting)2430 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoChromaSiting) {
2431     hidl_vec<uint8_t> vec;
2432     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2433                                                      gralloc4::MetadataType_ChromaSiting, &vec);
2434     if (err == Error::UNSUPPORTED) {
2435         GTEST_SUCCEED() << "setting this metadata is unsupported";
2436         return;
2437     }
2438     ASSERT_EQ(err, Error::NONE);
2439 
2440     ExtendableType chromaSiting = gralloc4::ChromaSiting_CositedHorizontal;
2441     ASSERT_EQ(NO_ERROR, gralloc4::decodeChromaSiting(vec, &chromaSiting));
2442 
2443     EXPECT_EQ(gralloc4::ChromaSiting_None.name, chromaSiting.name);
2444     EXPECT_EQ(gralloc4::ChromaSiting_None.value, chromaSiting.value);
2445 }
2446 
2447 /**
2448  * Test IMapper::getFromBufferDescriptorInfo(PlaneLayouts)
2449  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPlaneLayouts)2450 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPlaneLayouts) {
2451     hidl_vec<uint8_t> vec;
2452     const auto ret = mGralloc->getFromBufferDescriptorInfo(
2453             mDummyDescriptorInfo, gralloc4::MetadataType_PlaneLayouts, &vec);
2454     if (ret == Error::NONE) {
2455         std::vector<PlaneLayout> planeLayouts;
2456         ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
2457         ASSERT_NO_FATAL_FAILURE(verifyRGBA8888PlaneLayouts(planeLayouts));
2458     } else {
2459         ASSERT_EQ(Error::UNSUPPORTED, ret);
2460     }
2461 }
2462 
2463 /**
2464  * Test IMapper::getFromBufferDescriptorInfo(Crop)
2465  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoCrop)2466 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoCrop) {
2467     auto info = mDummyDescriptorInfo;
2468     info.format = PixelFormat::RGBA_8888;
2469     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
2470 
2471     hidl_vec<uint8_t> vec;
2472     auto err = mGralloc->getFromBufferDescriptorInfo(info, gralloc4::MetadataType_Crop, &vec);
2473     if (err == Error::UNSUPPORTED) {
2474         GTEST_SUCCEED() << "setting this metadata is unsupported";
2475         return;
2476     }
2477     ASSERT_EQ(err, Error::NONE);
2478 
2479     std::vector<aidl::android::hardware::graphics::common::Rect> crops;
2480     ASSERT_EQ(NO_ERROR, gralloc4::decodeCrop(vec, &crops));
2481     EXPECT_EQ(1, crops.size());
2482 }
2483 
2484 /**
2485  * Test IMapper::getFromBufferDescriptorInfo(Dataspace)
2486  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoDataspace)2487 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoDataspace) {
2488     hidl_vec<uint8_t> vec;
2489     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2490                                                      gralloc4::MetadataType_Dataspace, &vec);
2491     if (err == Error::UNSUPPORTED) {
2492         GTEST_SUCCEED() << "setting this metadata is unsupported";
2493         return;
2494     }
2495     ASSERT_EQ(err, Error::NONE);
2496 
2497     Dataspace dataspace = Dataspace::DISPLAY_P3;
2498     ASSERT_EQ(NO_ERROR, gralloc4::decodeDataspace(vec, &dataspace));
2499     EXPECT_EQ(Dataspace::UNKNOWN, dataspace);
2500 }
2501 
2502 /**
2503  * Test IMapper::getFromBufferDescriptorInfo(BlendMode)
2504  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoBlendMode)2505 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoBlendMode) {
2506     hidl_vec<uint8_t> vec;
2507     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2508                                                      gralloc4::MetadataType_BlendMode, &vec);
2509     if (err == Error::UNSUPPORTED) {
2510         GTEST_SUCCEED() << "setting this metadata is unsupported";
2511         return;
2512     }
2513     ASSERT_EQ(err, Error::NONE);
2514 
2515     BlendMode blendMode = BlendMode::COVERAGE;
2516     ASSERT_EQ(NO_ERROR, gralloc4::decodeBlendMode(vec, &blendMode));
2517     EXPECT_EQ(BlendMode::INVALID, blendMode);
2518 }
2519 
2520 /**
2521  * Test IMapper::getFromBufferDescriptorInfo(Smpte2086)
2522  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoSmpte2086)2523 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoSmpte2086) {
2524     hidl_vec<uint8_t> vec;
2525     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2526                                                      gralloc4::MetadataType_Smpte2086, &vec);
2527     if (err == Error::UNSUPPORTED) {
2528         GTEST_SUCCEED() << "setting this metadata is unsupported";
2529         return;
2530     }
2531     ASSERT_EQ(err, Error::NONE);
2532 
2533     std::optional<Smpte2086> smpte2086;
2534     ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2086(vec, &smpte2086));
2535     EXPECT_FALSE(smpte2086.has_value());
2536 }
2537 
2538 /**
2539  * Test IMapper::getFromBufferDescriptorInfo(Cta861_3)
2540  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoCta861_3)2541 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoCta861_3) {
2542     hidl_vec<uint8_t> vec;
2543     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2544                                                      gralloc4::MetadataType_Cta861_3, &vec);
2545     if (err == Error::UNSUPPORTED) {
2546         GTEST_SUCCEED() << "setting this metadata is unsupported";
2547         return;
2548     }
2549     ASSERT_EQ(err, Error::NONE);
2550 
2551     std::optional<Cta861_3> cta861_3;
2552     ASSERT_EQ(NO_ERROR, gralloc4::decodeCta861_3(vec, &cta861_3));
2553     EXPECT_FALSE(cta861_3.has_value());
2554 }
2555 
2556 /**
2557  * Test IMapper::getFromBufferDescriptorInfo(Smpte2094_40)
2558  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoSmpte2094_40)2559 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoSmpte2094_40) {
2560     hidl_vec<uint8_t> vec;
2561     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2562                                                      gralloc4::MetadataType_Smpte2094_40, &vec);
2563     if (err == Error::UNSUPPORTED) {
2564         GTEST_SUCCEED() << "setting this metadata is unsupported";
2565         return;
2566     }
2567     ASSERT_EQ(err, Error::NONE);
2568 
2569     std::optional<std::vector<uint8_t>> smpte2094_40;
2570     ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, &smpte2094_40));
2571     EXPECT_FALSE(smpte2094_40.has_value());
2572 }
2573 
2574 /**
2575  * Test IMapper::getFromBufferDescriptorInfo(Smpte2094_10)
2576  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoSmpte2094_10)2577 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoSmpte2094_10) {
2578     hidl_vec<uint8_t> vec;
2579     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2580                                                      gralloc4::MetadataType_Smpte2094_10, &vec);
2581     if (err == Error::UNSUPPORTED) {
2582         GTEST_SUCCEED() << "setting this metadata is unsupported";
2583         return;
2584     }
2585     ASSERT_EQ(err, Error::NONE);
2586 
2587     std::optional<std::vector<uint8_t>> smpte2094_10;
2588     ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_10(vec, &smpte2094_10));
2589     EXPECT_FALSE(smpte2094_10.has_value());
2590 }
2591 
2592 /**
2593  * Test IMapper::getFromBufferDescriptorInfo(metadata) for unsupported metadata
2594  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoUnsupportedMetadata)2595 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoUnsupportedMetadata) {
2596     MetadataType metadataTypeFake = {"FAKE", 1};
2597 
2598     hidl_vec<uint8_t> vec;
2599     ASSERT_EQ(Error::UNSUPPORTED,
2600               mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo, metadataTypeFake, &vec));
2601     ASSERT_EQ(0, vec.size());
2602 }
2603 
2604 /**
2605  * Test IMapper::getFromBufferDescriptorInfo(metadata) for unsupported standard metadata
2606  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoUnsupportedStandardMetadata)2607 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoUnsupportedStandardMetadata) {
2608     MetadataType metadataTypeFake = {GRALLOC4_STANDARD_METADATA_TYPE, 9999};
2609 
2610     hidl_vec<uint8_t> vec;
2611     ASSERT_EQ(Error::UNSUPPORTED,
2612               mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo, metadataTypeFake, &vec));
2613     ASSERT_EQ(0, vec.size());
2614 }
2615 
2616 /**
2617  * Test IMapper::listSupportedMetadataTypes()
2618  */
TEST_P(GraphicsMapperHidlTest,ListSupportedMetadataTypes)2619 TEST_P(GraphicsMapperHidlTest, ListSupportedMetadataTypes) {
2620     hidl_vec<IMapper::MetadataTypeDescription> descriptions;
2621     mGralloc->getMapper()->listSupportedMetadataTypes(
2622             [&](const auto& tmpError, const auto& tmpDescriptions) {
2623                 ASSERT_EQ(Error::NONE, tmpError);
2624                 descriptions = tmpDescriptions;
2625             });
2626 
2627     std::set<StandardMetadataType> foundMetadataTypes;
2628 
2629     std::set<StandardMetadataType> notSettableMetadataTypes{
2630             StandardMetadataType::BUFFER_ID,   StandardMetadataType::NAME,
2631             StandardMetadataType::WIDTH,       StandardMetadataType::HEIGHT,
2632             StandardMetadataType::LAYER_COUNT, StandardMetadataType::PIXEL_FORMAT_REQUESTED,
2633             StandardMetadataType::USAGE};
2634 
2635     ASSERT_LE(sRequiredMetadataTypes.size(), descriptions.size());
2636 
2637     for (const auto& description : descriptions) {
2638         const auto& metadataType = description.metadataType;
2639 
2640         if (!gralloc4::isStandardMetadataType(metadataType)) {
2641             EXPECT_GT(description.description.size(), 0);
2642             continue;
2643         }
2644 
2645         StandardMetadataType type = gralloc4::getStandardMetadataTypeValue(metadataType);
2646 
2647         if (sRequiredMetadataTypes.find(type) == sRequiredMetadataTypes.end()) {
2648             continue;
2649         }
2650 
2651         ASSERT_EQ(foundMetadataTypes.find(type), foundMetadataTypes.end());
2652         foundMetadataTypes.insert(type);
2653 
2654         ASSERT_TRUE(description.isGettable);
2655 
2656         if (notSettableMetadataTypes.find(type) != notSettableMetadataTypes.end()) {
2657             ASSERT_FALSE(description.isSettable);
2658         }
2659     }
2660 
2661     ASSERT_EQ(sRequiredMetadataTypes, foundMetadataTypes);
2662 }
2663 
2664 /**
2665  * Test IMapper::dumpBuffer()
2666  */
TEST_P(GraphicsMapperHidlTest,DumpBuffer)2667 TEST_P(GraphicsMapperHidlTest, DumpBuffer) {
2668     const native_handle_t* bufferHandle = nullptr;
2669     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2670     auto buffer = const_cast<native_handle_t*>(bufferHandle);
2671 
2672     IMapper::BufferDump bufferDump;
2673     mGralloc->getMapper()->dumpBuffer(buffer, [&](const auto& tmpError, const auto& tmpBufferDump) {
2674         ASSERT_EQ(Error::NONE, tmpError);
2675         bufferDump = tmpBufferDump;
2676     });
2677 
2678     ASSERT_NO_FATAL_FAILURE(verifyBufferDump(bufferDump, buffer));
2679 }
2680 
2681 /**
2682  * Test IMapper::dumpBuffer() with an invalid buffer
2683  */
TEST_P(GraphicsMapperHidlTest,DumpBufferNullBuffer)2684 TEST_P(GraphicsMapperHidlTest, DumpBufferNullBuffer) {
2685     native_handle_t* bufferHandle = nullptr;
2686     auto buffer = const_cast<native_handle_t*>(bufferHandle);
2687 
2688     mGralloc->getMapper()->dumpBuffer(buffer,
2689                                       [&](const auto& tmpError, const auto& /*tmpBufferDump*/) {
2690                                           ASSERT_EQ(Error::BAD_BUFFER, tmpError);
2691                                       });
2692 }
2693 
2694 /**
2695  * Test IMapper::dumpBuffer() multiple
2696  */
TEST_P(GraphicsMapperHidlTest,DumpBuffers)2697 TEST_P(GraphicsMapperHidlTest, DumpBuffers) {
2698     size_t bufferCount = 10;
2699 
2700     for (int i = 0; i < bufferCount; i++) {
2701         ASSERT_NO_FATAL_FAILURE(mGralloc->allocate(mDummyDescriptorInfo, true));
2702     }
2703 
2704     hidl_vec<IMapper::BufferDump> bufferDump;
2705     mGralloc->getMapper()->dumpBuffers([&](const auto& tmpError, const auto& tmpBufferDump) {
2706         ASSERT_EQ(Error::NONE, tmpError);
2707         bufferDump = tmpBufferDump;
2708     });
2709 
2710     ASSERT_EQ(bufferCount, bufferDump.size());
2711 
2712     for (const auto& dump : bufferDump) {
2713         ASSERT_NO_FATAL_FAILURE(verifyBufferDump(dump));
2714     }
2715 }
2716 
2717 /**
2718  * Test IMapper::getReservedRegion()
2719  */
TEST_P(GraphicsMapperHidlTest,GetReservedRegion)2720 TEST_P(GraphicsMapperHidlTest, GetReservedRegion) {
2721     const native_handle_t* bufferHandle = nullptr;
2722     auto info = mDummyDescriptorInfo;
2723 
2724     const int pageSize = getpagesize();
2725     ASSERT_GE(pageSize, 0);
2726     std::vector<uint64_t> requestedReservedSizes{1, 10, 333, static_cast<uint64_t>(pageSize) / 2,
2727                                                  static_cast<uint64_t>(pageSize)};
2728 
2729     for (auto requestedReservedSize : requestedReservedSizes) {
2730         info.reservedSize = requestedReservedSize;
2731 
2732         ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
2733 
2734         void* reservedRegion = nullptr;
2735         uint64_t reservedSize = 0;
2736         ASSERT_EQ(Error::NONE,
2737                   mGralloc->getReservedRegion(bufferHandle, &reservedRegion, &reservedSize));
2738         ASSERT_NE(nullptr, reservedRegion);
2739         ASSERT_EQ(requestedReservedSize, reservedSize);
2740 
2741         uint8_t testValue = 1;
2742         memset(reservedRegion, testValue, reservedSize);
2743         for (uint64_t i = 0; i < reservedSize; i++) {
2744             ASSERT_EQ(testValue, static_cast<uint8_t*>(reservedRegion)[i]);
2745         }
2746     }
2747 }
2748 
2749 /**
2750  * Test IMapper::getReservedRegion() request over a page
2751  */
TEST_P(GraphicsMapperHidlTest,GetLargeReservedRegion)2752 TEST_P(GraphicsMapperHidlTest, GetLargeReservedRegion) {
2753     const native_handle_t* bufferHandle = nullptr;
2754     auto info = mDummyDescriptorInfo;
2755 
2756     const int pageSize = getpagesize();
2757     ASSERT_GE(pageSize, 0);
2758     std::vector<uint64_t> requestedReservedSizes{static_cast<uint64_t>(pageSize) * 2,
2759                                                  static_cast<uint64_t>(pageSize) * 10,
2760                                                  static_cast<uint64_t>(pageSize) * 1000};
2761 
2762     for (auto requestedReservedSize : requestedReservedSizes) {
2763         info.reservedSize = requestedReservedSize;
2764 
2765         BufferDescriptor descriptor;
2766         ASSERT_NO_FATAL_FAILURE(descriptor = mGralloc->createDescriptor(info));
2767 
2768         Error err = Error::NONE;
2769 
2770         mGralloc->rawAllocate(
2771                 descriptor, 1, [&](const auto& tmpError, const auto&, const auto& tmpBuffers) {
2772                     err = tmpError;
2773                     if (err == Error::NONE) {
2774                         ASSERT_EQ(1, tmpBuffers.size());
2775                         ASSERT_NO_FATAL_FAILURE(bufferHandle =
2776                                                         mGralloc->importBuffer(tmpBuffers[0]));
2777                     }
2778                 });
2779         if (err == Error::UNSUPPORTED) {
2780             continue;
2781         }
2782         ASSERT_EQ(Error::NONE, err);
2783 
2784         void* reservedRegion = nullptr;
2785         uint64_t reservedSize = 0;
2786         err = mGralloc->getReservedRegion(bufferHandle, &reservedRegion, &reservedSize);
2787 
2788         ASSERT_EQ(Error::NONE, err);
2789         ASSERT_NE(nullptr, reservedRegion);
2790         ASSERT_EQ(requestedReservedSize, reservedSize);
2791     }
2792 }
2793 
2794 /**
2795  * Test IMapper::getReservedRegion() across multiple mappers
2796  */
TEST_P(GraphicsMapperHidlTest,GetReservedRegionMultiple)2797 TEST_P(GraphicsMapperHidlTest, GetReservedRegionMultiple) {
2798     const native_handle_t* bufferHandle = nullptr;
2799     auto info = mDummyDescriptorInfo;
2800 
2801     const int pageSize = getpagesize();
2802     ASSERT_GE(pageSize, 0);
2803     info.reservedSize = pageSize;
2804 
2805     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
2806 
2807     void* reservedRegion1 = nullptr;
2808     uint64_t reservedSize1 = 0;
2809     ASSERT_EQ(Error::NONE,
2810               mGralloc->getReservedRegion(bufferHandle, &reservedRegion1, &reservedSize1));
2811     ASSERT_NE(nullptr, reservedRegion1);
2812     ASSERT_EQ(info.reservedSize, reservedSize1);
2813 
2814     std::unique_ptr<Gralloc> anotherGralloc;
2815     ASSERT_NO_FATAL_FAILURE(anotherGralloc = std::make_unique<Gralloc>(std::get<0>(GetParam()),
2816                                                                        std::get<1>(GetParam())));
2817 
2818     void* reservedRegion2 = nullptr;
2819     uint64_t reservedSize2 = 0;
2820     ASSERT_EQ(Error::NONE,
2821               mGralloc->getReservedRegion(bufferHandle, &reservedRegion2, &reservedSize2));
2822     ASSERT_EQ(reservedRegion1, reservedRegion2);
2823     ASSERT_EQ(reservedSize1, reservedSize2);
2824 }
2825 
2826 /**
2827  * Test IMapper::getReservedRegion() with a bad buffer
2828  */
TEST_P(GraphicsMapperHidlTest,GetReservedRegionBadBuffer)2829 TEST_P(GraphicsMapperHidlTest, GetReservedRegionBadBuffer) {
2830     const native_handle_t* bufferHandle = nullptr;
2831 
2832     void* reservedRegion = nullptr;
2833     uint64_t reservedSize = 0;
2834     ASSERT_EQ(Error::BAD_BUFFER,
2835               mGralloc->getReservedRegion(bufferHandle, &reservedRegion, &reservedSize));
2836     ASSERT_EQ(nullptr, reservedRegion);
2837     ASSERT_EQ(0, reservedSize);
2838 }
2839 
2840 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsMapperHidlTest);
2841 
2842 namespace {
getAllocatorInstances()2843 std::vector<std::string> getAllocatorInstances() {
2844     std::vector<std::string> instances;
2845     for (auto halInstance : android::hardware::getAllHalInstanceNames(IAllocator::descriptor)) {
2846         instances.emplace_back(std::move(halInstance));
2847     }
2848 
2849     for (auto aidlInstance : getAidlHalInstanceNames(
2850                  aidl::android::hardware::graphics::allocator::IAllocator::descriptor)) {
2851         instances.emplace_back(std::move(aidlInstance));
2852     }
2853 
2854     return instances;
2855 }
2856 }  // namespace
2857 
2858 INSTANTIATE_TEST_CASE_P(
2859         PerInstance, GraphicsMapperHidlTest,
2860         testing::Combine(
2861                 testing::ValuesIn(getAllocatorInstances()),
2862                 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IMapper::descriptor))),
2863         android::hardware::PrintInstanceTupleNameToString<>);
2864 
2865 }  // namespace
2866 }  // namespace vts
2867 }  // namespace V4_0
2868 }  // namespace mapper
2869 }  // namespace graphics
2870 }  // namespace hardware
2871 }  // namespace android
2872