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 IMapper::lock and IMapper::unlock with no CPU usage requested.
723  */
TEST_P(GraphicsMapperHidlTest,LockUnlockNoCPUUsage)724 TEST_P(GraphicsMapperHidlTest, LockUnlockNoCPUUsage) {
725     const auto& info = mDummyDescriptorInfo;
726 
727     const native_handle_t* bufferHandle;
728     uint32_t stride;
729     ASSERT_NO_FATAL_FAILURE(
730             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
731 
732     // lock buffer with 0 usage
733     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
734                                static_cast<int32_t>(info.height)};
735 
736     hidl_handle acquireFenceHandle;
737 
738     auto buffer = const_cast<native_handle_t*>(bufferHandle);
739     mGralloc->getMapper()->lock(buffer, 0, region, acquireFenceHandle,
740                                 [&](const auto& tmpError, const auto& /*tmpData*/) {
741                                     EXPECT_EQ(Error::BAD_VALUE, tmpError)
742                                             << "Locking with 0 access succeeded";
743                                 });
744 
745     mGralloc->getMapper()->unlock(buffer, [&](const auto& tmpError, const auto&) {
746         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
747                 << "Unlocking not locked buffer succeeded";
748     });
749 
750     mGralloc->freeBuffer(bufferHandle);
751 }
752 
753 /**
754  *  Test multiple operations associated with different color formats
755  */
TEST_P(GraphicsMapperHidlTest,Lock_YCRCB_420_SP)756 TEST_P(GraphicsMapperHidlTest, Lock_YCRCB_420_SP) {
757     auto info = mDummyDescriptorInfo;
758     info.format = PixelFormat::YCRCB_420_SP;
759 
760     const native_handle_t* bufferHandle;
761     uint32_t stride;
762     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(
763                                     info, true, Tolerance::kToleranceUnSupported, &stride));
764     if (bufferHandle == nullptr) {
765         GTEST_SUCCEED() << "YCRCB_420_SP format is unsupported";
766         return;
767     }
768 
769     // lock buffer for writing
770     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
771                                static_cast<int32_t>(info.height)};
772     unique_fd fence;
773     uint8_t* data;
774 
775     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
776                                                                         region, fence.release())));
777 
778     android_ycbcr yCbCr;
779     int64_t hSubsampling = 0;
780     int64_t vSubsampling = 0;
781     ASSERT_NO_FATAL_FAILURE(
782             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
783 
784     constexpr uint32_t kCbCrSubSampleFactor = 2;
785     ASSERT_EQ(kCbCrSubSampleFactor, hSubsampling);
786     ASSERT_EQ(kCbCrSubSampleFactor, vSubsampling);
787 
788     auto cbData = static_cast<uint8_t*>(yCbCr.cb);
789     auto crData = static_cast<uint8_t*>(yCbCr.cr);
790     ASSERT_EQ(crData + 1, cbData);
791     ASSERT_EQ(2, yCbCr.chroma_step);
792 
793     fillYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
794 
795     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
796 
797     // lock again for reading
798     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
799                                                                         region, fence.release())));
800 
801     ASSERT_NO_FATAL_FAILURE(
802             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
803 
804     verifyYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
805 
806     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
807 }
808 
TEST_P(GraphicsMapperHidlTest,YV12SubsampleMetadata)809 TEST_P(GraphicsMapperHidlTest, YV12SubsampleMetadata) {
810     auto info = mDummyDescriptorInfo;
811     info.format = PixelFormat::YV12;
812 
813     const native_handle_t* bufferHandle;
814     uint32_t stride;
815     ASSERT_NO_FATAL_FAILURE(
816             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
817 
818     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
819                                static_cast<int32_t>(info.height)};
820     unique_fd fence;
821     ASSERT_NO_FATAL_FAILURE(mGralloc->lock(bufferHandle, info.usage, region, fence.release()));
822 
823     hidl_vec<uint8_t> vec;
824     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
825     std::vector<PlaneLayout> planeLayouts;
826     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
827 
828     ASSERT_EQ(3, planeLayouts.size());
829 
830     auto yPlane = planeLayouts[0];
831     auto crPlane = planeLayouts[1];
832     auto cbPlane = planeLayouts[2];
833 
834     constexpr uint32_t kCbCrSubSampleFactor = 2;
835     EXPECT_EQ(kCbCrSubSampleFactor, crPlane.horizontalSubsampling);
836     EXPECT_EQ(kCbCrSubSampleFactor, crPlane.verticalSubsampling);
837 
838     EXPECT_EQ(kCbCrSubSampleFactor, cbPlane.horizontalSubsampling);
839     EXPECT_EQ(kCbCrSubSampleFactor, cbPlane.verticalSubsampling);
840 
841     const long chromaSampleWidth = info.width / kCbCrSubSampleFactor;
842     const long chromaSampleHeight = info.height / kCbCrSubSampleFactor;
843 
844     EXPECT_EQ(info.width, yPlane.widthInSamples);
845     EXPECT_EQ(info.height, yPlane.heightInSamples);
846 
847     EXPECT_EQ(chromaSampleWidth, crPlane.widthInSamples);
848     EXPECT_EQ(chromaSampleHeight, crPlane.heightInSamples);
849 
850     EXPECT_EQ(chromaSampleWidth, cbPlane.widthInSamples);
851     EXPECT_EQ(chromaSampleHeight, cbPlane.heightInSamples);
852 
853     EXPECT_LE(crPlane.widthInSamples, crPlane.strideInBytes);
854     EXPECT_LE(cbPlane.widthInSamples, cbPlane.strideInBytes);
855 
856     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
857 }
858 
TEST_P(GraphicsMapperHidlTest,Lock_YV12)859 TEST_P(GraphicsMapperHidlTest, Lock_YV12) {
860     auto info = mDummyDescriptorInfo;
861     info.format = PixelFormat::YV12;
862 
863     const native_handle_t* bufferHandle;
864     uint32_t stride;
865     ASSERT_NO_FATAL_FAILURE(
866             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
867 
868     // lock buffer for writing
869     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
870                                static_cast<int32_t>(info.height)};
871     unique_fd fence;
872     uint8_t* data;
873 
874     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
875                                                                         region, fence.release())));
876 
877     android_ycbcr yCbCr;
878     int64_t hSubsampling = 0;
879     int64_t vSubsampling = 0;
880     ASSERT_NO_FATAL_FAILURE(
881             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
882 
883     constexpr uint32_t kCbCrSubSampleFactor = 2;
884     ASSERT_EQ(kCbCrSubSampleFactor, hSubsampling);
885     ASSERT_EQ(kCbCrSubSampleFactor, vSubsampling);
886 
887     auto cbData = static_cast<uint8_t*>(yCbCr.cb);
888     auto crData = static_cast<uint8_t*>(yCbCr.cr);
889     ASSERT_EQ(crData + yCbCr.cstride * info.height / vSubsampling, cbData);
890     ASSERT_EQ(1, yCbCr.chroma_step);
891 
892     fillYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
893 
894     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
895 
896     // lock again for reading
897     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
898                                                                         region, fence.release())));
899 
900     ASSERT_NO_FATAL_FAILURE(
901             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
902 
903     verifyYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
904 
905     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
906 }
907 
TEST_P(GraphicsMapperHidlTest,Lock_YCBCR_420_888)908 TEST_P(GraphicsMapperHidlTest, Lock_YCBCR_420_888) {
909     auto info = mDummyDescriptorInfo;
910     info.format = PixelFormat::YCBCR_420_888;
911 
912     const native_handle_t* bufferHandle;
913     uint32_t stride;
914     ASSERT_NO_FATAL_FAILURE(
915             bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride));
916 
917     // lock buffer for writing
918     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
919                                static_cast<int32_t>(info.height)};
920     unique_fd fence;
921     uint8_t* data;
922 
923     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
924                                                                         region, fence.release())));
925 
926     android_ycbcr yCbCr;
927     int64_t hSubsampling = 0;
928     int64_t vSubsampling = 0;
929     ASSERT_NO_FATAL_FAILURE(
930             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
931 
932     constexpr uint32_t kCbCrSubSampleFactor = 2;
933     ASSERT_EQ(kCbCrSubSampleFactor, hSubsampling);
934     ASSERT_EQ(kCbCrSubSampleFactor, vSubsampling);
935 
936     fillYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
937 
938     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
939 
940     // lock again for reading
941     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
942                                                                         region, fence.release())));
943 
944     ASSERT_NO_FATAL_FAILURE(
945             getAndroidYCbCr(bufferHandle, data, &yCbCr, &hSubsampling, &vSubsampling));
946 
947     verifyYCbCrData(yCbCr, info.width, info.height, hSubsampling, vSubsampling);
948 
949     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
950 }
951 
TEST_P(GraphicsMapperHidlTest,Lock_RAW10)952 TEST_P(GraphicsMapperHidlTest, Lock_RAW10) {
953     auto info = mDummyDescriptorInfo;
954     info.format = PixelFormat::RAW10;
955 
956     const native_handle_t* bufferHandle;
957     uint32_t stride;
958     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(
959                                     info, true, Tolerance::kToleranceUnSupported, &stride));
960     if (bufferHandle == nullptr) {
961         GTEST_SUCCEED() << "RAW10 format is unsupported";
962         return;
963     }
964 
965     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
966                                static_cast<int32_t>(info.height)};
967     unique_fd fence;
968 
969     ASSERT_NO_FATAL_FAILURE(mGralloc->lock(bufferHandle, info.usage, region, fence.release()));
970 
971     hidl_vec<uint8_t> vec;
972     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
973     std::vector<PlaneLayout> planeLayouts;
974     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
975 
976     ASSERT_EQ(1, planeLayouts.size());
977     auto planeLayout = planeLayouts[0];
978 
979     EXPECT_EQ(0, planeLayout.sampleIncrementInBits);
980     EXPECT_EQ(1, planeLayout.horizontalSubsampling);
981     EXPECT_EQ(1, planeLayout.verticalSubsampling);
982 
983     ASSERT_EQ(1, planeLayout.components.size());
984     auto planeLayoutComponent = planeLayout.components[0];
985 
986     EXPECT_EQ(PlaneLayoutComponentType::RAW,
987               static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value));
988     EXPECT_EQ(0, planeLayoutComponent.offsetInBits % 8);
989 
990     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
991 }
992 
TEST_P(GraphicsMapperHidlTest,Lock_RAW12)993 TEST_P(GraphicsMapperHidlTest, Lock_RAW12) {
994     auto info = mDummyDescriptorInfo;
995     info.format = PixelFormat::RAW12;
996 
997     const native_handle_t* bufferHandle;
998     uint32_t stride;
999     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(
1000                                     info, true, Tolerance::kToleranceUnSupported, &stride));
1001     if (bufferHandle == nullptr) {
1002         GTEST_SUCCEED() << "RAW12 format is unsupported";
1003         return;
1004     }
1005 
1006     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
1007                                static_cast<int32_t>(info.height)};
1008     unique_fd fence;
1009 
1010     ASSERT_NO_FATAL_FAILURE(mGralloc->lock(bufferHandle, info.usage, region, fence.release()));
1011 
1012     hidl_vec<uint8_t> vec;
1013     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
1014     std::vector<PlaneLayout> planeLayouts;
1015     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
1016 
1017     ASSERT_EQ(1, planeLayouts.size());
1018     auto planeLayout = planeLayouts[0];
1019 
1020     EXPECT_EQ(0, planeLayout.sampleIncrementInBits);
1021     EXPECT_EQ(1, planeLayout.horizontalSubsampling);
1022     EXPECT_EQ(1, planeLayout.verticalSubsampling);
1023 
1024     ASSERT_EQ(1, planeLayout.components.size());
1025     auto planeLayoutComponent = planeLayout.components[0];
1026 
1027     EXPECT_EQ(PlaneLayoutComponentType::RAW,
1028               static_cast<PlaneLayoutComponentType>(planeLayoutComponent.type.value));
1029     EXPECT_EQ(0, planeLayoutComponent.offsetInBits % 8);
1030 
1031     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
1032 }
1033 
TEST_P(GraphicsMapperHidlTest,Lock_YCBCR_P010)1034 TEST_P(GraphicsMapperHidlTest, Lock_YCBCR_P010) {
1035     if (base::GetIntProperty("ro.vendor.api_level", __ANDROID_API_FUTURE__) < __ANDROID_API_T__) {
1036         GTEST_SKIP() << "Old vendor grallocs may not support P010";
1037     }
1038     auto info = mDummyDescriptorInfo;
1039     info.format = PixelFormat::YCBCR_P010;
1040 
1041     uint32_t stride;
1042     const native_handle_t* bufferHandle =
1043             mGralloc->allocate(info, true, Tolerance::kToleranceStrict, &stride);
1044 
1045     if (::testing::Test::IsSkipped()) {
1046         GTEST_SKIP();
1047     }
1048 
1049     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
1050                                static_cast<int32_t>(info.height)};
1051     unique_fd fence;
1052     uint8_t* data;
1053 
1054     ASSERT_NO_FATAL_FAILURE(data = static_cast<uint8_t*>(mGralloc->lock(bufferHandle, info.usage,
1055                                                                         region, fence.release())));
1056 
1057     YCbCr yCbCr;
1058     ASSERT_NO_FATAL_FAILURE(yCbCr = getAndroidYCbCr_P010(bufferHandle, data));
1059 
1060     constexpr uint32_t kCbCrSubSampleFactor = 2;
1061     ASSERT_EQ(kCbCrSubSampleFactor, yCbCr.horizontalSubSampling);
1062     ASSERT_EQ(kCbCrSubSampleFactor, yCbCr.verticalSubSampling);
1063 
1064     ASSERT_EQ(0, info.height % 2);
1065 
1066     // fill the data
1067     fillYCbCrData(yCbCr.yCbCr, info.width, info.height, yCbCr.horizontalSubSampling,
1068                   yCbCr.verticalSubSampling);
1069     // verify the YCbCr data
1070     verifyYCbCrData(yCbCr.yCbCr, info.width, info.height, yCbCr.horizontalSubSampling,
1071                     yCbCr.verticalSubSampling);
1072 
1073     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(bufferHandle)));
1074 }
1075 
1076 /**
1077  * Test IMapper::unlock with bad access region
1078  */
TEST_P(GraphicsMapperHidlTest,LockBadAccessRegion)1079 TEST_P(GraphicsMapperHidlTest, LockBadAccessRegion) {
1080     const auto& info = mDummyDescriptorInfo;
1081 
1082     const native_handle_t* bufferHandle;
1083     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
1084 
1085     const IMapper::Rect accessRegion{0, 0, static_cast<int32_t>(info.width * 2),
1086                                      static_cast<int32_t>(info.height * 2)};
1087     int acquireFence = -1;
1088 
1089     NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
1090     hidl_handle acquireFenceHandle;
1091     if (acquireFence >= 0) {
1092         auto h = native_handle_init(acquireFenceStorage, 1, 0);
1093         h->data[0] = acquireFence;
1094         acquireFenceHandle = h;
1095     }
1096 
1097     auto buffer = const_cast<native_handle_t*>(bufferHandle);
1098     mGralloc->getMapper()->lock(buffer, info.usage, accessRegion, acquireFenceHandle,
1099                                 [&](const auto& tmpError, const auto& /*tmpData*/) {
1100                                     EXPECT_EQ(Error::BAD_VALUE, tmpError)
1101                                             << "locking with a bad access region should fail";
1102                                 });
1103 
1104     if (::testing::Test::HasFailure()) {
1105         if (acquireFence >= 0) {
1106             close(acquireFence);
1107         }
1108 
1109         int releaseFence = -1;
1110         ASSERT_NO_FATAL_FAILURE(releaseFence = mGralloc->unlock(bufferHandle));
1111 
1112         if (releaseFence >= 0) {
1113             close(releaseFence);
1114         }
1115     }
1116 }
1117 
1118 /**
1119  * Test IMapper::unlock with invalid buffers.
1120  */
TEST_P(GraphicsMapperHidlTest,UnlockNegative)1121 TEST_P(GraphicsMapperHidlTest, UnlockNegative) {
1122     native_handle_t* invalidHandle = nullptr;
1123     mGralloc->getMapper()->unlock(invalidHandle, [&](const auto& tmpError, const auto&) {
1124         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1125                 << "unlock with nullptr did not fail with BAD_BUFFER";
1126     });
1127 
1128     invalidHandle = native_handle_create(0, 0);
1129     mGralloc->getMapper()->unlock(invalidHandle, [&](const auto& tmpError, const auto&) {
1130         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1131                 << "unlock with invalid handle did not fail with BAD_BUFFER";
1132     });
1133     native_handle_delete(invalidHandle);
1134 
1135     ASSERT_NO_FATAL_FAILURE(invalidHandle = const_cast<native_handle_t*>(
1136                                     mGralloc->allocate(mDummyDescriptorInfo, false)));
1137     mGralloc->getMapper()->unlock(invalidHandle, [&](const auto& tmpError, const auto&) {
1138         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1139                 << "unlock with un-imported handle did not fail with BAD_BUFFER";
1140     });
1141     mGralloc->freeBuffer(invalidHandle);
1142 
1143 // disabled as it fails on many existing drivers
1144 #if 0
1145   ASSERT_NO_FATAL_FAILURE(invalidHandle = const_cast<native_handle_t*>(
1146                               mGralloc->allocate(mDummyDescriptorInfo, true)));
1147   mGralloc->getMapper()->unlock(
1148       invalidHandle, [&](const auto& tmpError, const auto&) {
1149         EXPECT_EQ(Error::BAD_BUFFER, tmpError)
1150             << "unlock with unlocked handle did not fail with BAD_BUFFER";
1151       });
1152   mGralloc->freeBuffer(invalidHandle);
1153 #endif
1154 }
1155 
1156 /**
1157  * Test IMapper::flush and IMapper::reread.
1158  */
TEST_P(GraphicsMapperHidlTest,FlushRereadBasic)1159 TEST_P(GraphicsMapperHidlTest, FlushRereadBasic) {
1160     const auto& info = mDummyDescriptorInfo;
1161 
1162     const native_handle_t* rawHandle;
1163     uint32_t stride;
1164     ASSERT_NO_FATAL_FAILURE(rawHandle = mGralloc->allocate(mDummyDescriptorInfo, false,
1165                                                            Tolerance::kToleranceStrict, &stride));
1166 
1167     const native_handle_t* writeBufferHandle;
1168     const native_handle_t* readBufferHandle;
1169     ASSERT_NO_FATAL_FAILURE(writeBufferHandle = mGralloc->importBuffer(rawHandle));
1170     ASSERT_NO_FATAL_FAILURE(readBufferHandle = mGralloc->importBuffer(rawHandle));
1171 
1172     // lock buffer for writing
1173     const IMapper::Rect region{0, 0, static_cast<int32_t>(info.width),
1174                                static_cast<int32_t>(info.height)};
1175     uint8_t* writeData;
1176     ASSERT_NO_FATAL_FAILURE(
1177             writeData = static_cast<uint8_t*>(mGralloc->lock(
1178                     writeBufferHandle, static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN), region,
1179                     -1)));
1180 
1181     uint8_t* readData;
1182     ASSERT_NO_FATAL_FAILURE(
1183             readData = static_cast<uint8_t*>(mGralloc->lock(
1184                     readBufferHandle, static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN), region,
1185                     -1)));
1186 
1187     fillRGBA8888(writeData, info.height, stride * 4, info.width * 4);
1188 
1189     unique_fd fence;
1190     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->flushLockedBuffer(writeBufferHandle)));
1191     if (fence >= 0) {
1192         ASSERT_EQ(0, sync_wait(fence, 3500));
1193     }
1194 
1195     ASSERT_NO_FATAL_FAILURE(mGralloc->rereadLockedBuffer(readBufferHandle));
1196 
1197     ASSERT_NO_FATAL_FAILURE(
1198             verifyRGBA8888(readBufferHandle, readData, info.height, stride * 4, info.width * 4));
1199 
1200     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(readBufferHandle)));
1201 
1202     ASSERT_NO_FATAL_FAILURE(fence.reset(mGralloc->unlock(writeBufferHandle)));
1203 }
1204 
1205 /**
1206  * Test IMapper::flushLockedBuffer with bad buffer
1207  */
TEST_P(GraphicsMapperHidlTest,FlushLockedBufferBadBuffer)1208 TEST_P(GraphicsMapperHidlTest, FlushLockedBufferBadBuffer) {
1209     ASSERT_NO_FATAL_FAILURE(mGralloc->getMapper()->flushLockedBuffer(
1210             nullptr, [&](const auto& tmpError, const auto& /*tmpReleaseFence*/) {
1211                 ASSERT_EQ(Error::BAD_BUFFER, tmpError);
1212             }));
1213 }
1214 
1215 /**
1216  * Test IMapper::rereadLockedBuffer with bad buffer
1217  */
TEST_P(GraphicsMapperHidlTest,RereadLockedBufferBadBuffer)1218 TEST_P(GraphicsMapperHidlTest, RereadLockedBufferBadBuffer) {
1219     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->getMapper()->rereadLockedBuffer(nullptr));
1220 }
1221 
1222 /**
1223  * Test IMapper::isSupported with required format RGBA_8888
1224  */
TEST_P(GraphicsMapperHidlTest,IsSupportedRGBA8888)1225 TEST_P(GraphicsMapperHidlTest, IsSupportedRGBA8888) {
1226     const auto& info = mDummyDescriptorInfo;
1227     bool supported = false;
1228 
1229     ASSERT_NO_FATAL_FAILURE(supported = mGralloc->isSupported(info));
1230     ASSERT_TRUE(supported);
1231 }
1232 
1233 /**
1234  * Test IMapper::isSupported with required format YV12
1235  */
TEST_P(GraphicsMapperHidlTest,IsSupportedYV12)1236 TEST_P(GraphicsMapperHidlTest, IsSupportedYV12) {
1237     auto info = mDummyDescriptorInfo;
1238     info.format = PixelFormat::YV12;
1239     bool supported = false;
1240 
1241     ASSERT_NO_FATAL_FAILURE(supported = mGralloc->isSupported(info));
1242     ASSERT_TRUE(supported);
1243 }
1244 
1245 /**
1246  * Test IMapper::isSupported with optional format Y16
1247  */
TEST_P(GraphicsMapperHidlTest,IsSupportedY16)1248 TEST_P(GraphicsMapperHidlTest, IsSupportedY16) {
1249     auto info = mDummyDescriptorInfo;
1250     info.format = PixelFormat::Y16;
1251     bool supported = false;
1252 
1253     ASSERT_NO_FATAL_FAILURE(supported = mGralloc->isSupported(info));
1254 }
1255 
1256 /**
1257  * Test IMapper::isSupported with optional format R_8
1258  */
TEST_P(GraphicsMapperHidlTest,IsSupportedR8)1259 TEST_P(GraphicsMapperHidlTest, IsSupportedR8) {
1260     auto info = mDummyDescriptorInfo;
1261     info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(
1262             aidl::android::hardware::graphics::common::PixelFormat::R_8);
1263     bool supported = false;
1264 
1265     supported = mGralloc->isSupportedNoFailure(info);
1266 
1267     if (!supported) {
1268         GTEST_SUCCEED() << "R_8 is optional; unsupported so skipping allocation test";
1269         return;
1270     }
1271 
1272     BufferDescriptor descriptor;
1273     ASSERT_NO_FATAL_FAILURE(descriptor = mGralloc->createDescriptor(info));
1274 
1275     constexpr uint32_t count = 1;
1276     std::vector<const native_handle_t*> bufferHandles;
1277     uint32_t stride;
1278     ASSERT_NO_FATAL_FAILURE(bufferHandles =
1279                                     mGralloc->allocate(descriptor, count, false,
1280                                                        Tolerance::kToleranceStrict, &stride));
1281 
1282     EXPECT_LE(info.width, stride) << "invalid buffer stride";
1283     EXPECT_EQ(1u, bufferHandles.size());
1284 
1285     for (auto bufferHandle : bufferHandles) {
1286         mGralloc->freeBuffer(bufferHandle);
1287     }
1288 }
1289 
1290 /**
1291  * Test IMapper::get(BufferId)
1292  */
TEST_P(GraphicsMapperHidlTest,GetBufferId)1293 TEST_P(GraphicsMapperHidlTest, GetBufferId) {
1294     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_BufferId,
1295             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1296                 uint64_t bufferId = 0;
1297                 ASSERT_EQ(NO_ERROR, gralloc4::decodeBufferId(vec, &bufferId));
1298             });
1299 }
1300 
1301 /**
1302  * Test IMapper::get(Name)
1303  */
TEST_P(GraphicsMapperHidlTest,GetName)1304 TEST_P(GraphicsMapperHidlTest, GetName) {
1305     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Name,
1306             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1307                 std::string name;
1308                 ASSERT_EQ(NO_ERROR, gralloc4::decodeName(vec, &name));
1309                 EXPECT_EQ(info.name, name);
1310             });
1311 }
1312 
1313 /**
1314  * Test IMapper::get(Width)
1315  */
TEST_P(GraphicsMapperHidlTest,GetWidth)1316 TEST_P(GraphicsMapperHidlTest, GetWidth) {
1317     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Width,
1318             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1319                 uint64_t width = 0;
1320                 ASSERT_EQ(NO_ERROR, gralloc4::decodeWidth(vec, &width));
1321                 EXPECT_EQ(info.width, width);
1322             });
1323 }
1324 
1325 /**
1326  * Test IMapper::get(Height)
1327  */
TEST_P(GraphicsMapperHidlTest,GetHeight)1328 TEST_P(GraphicsMapperHidlTest, GetHeight) {
1329     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Height,
1330             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1331                 uint64_t height = 0;
1332                 ASSERT_EQ(NO_ERROR, gralloc4::decodeHeight(vec, &height));
1333                 EXPECT_EQ(info.height, height);
1334             });
1335 }
1336 
1337 /**
1338  * Test IMapper::get(LayerCount)
1339  */
TEST_P(GraphicsMapperHidlTest,GetLayerCount)1340 TEST_P(GraphicsMapperHidlTest, GetLayerCount) {
1341     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_LayerCount,
1342             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1343                 uint64_t layerCount = 0;
1344                 ASSERT_EQ(NO_ERROR, gralloc4::decodeLayerCount(vec, &layerCount));
1345                 EXPECT_EQ(info.layerCount, layerCount);
1346             });
1347 }
1348 
1349 /**
1350  * Test IMapper::get(PixelFormatRequested)
1351  */
TEST_P(GraphicsMapperHidlTest,GetPixelFormatRequested)1352 TEST_P(GraphicsMapperHidlTest, GetPixelFormatRequested) {
1353     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatRequested,
1354             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1355                 PixelFormat pixelFormatRequested = PixelFormat::BLOB;
1356                 ASSERT_EQ(NO_ERROR,
1357                           gralloc4::decodePixelFormatRequested(vec, &pixelFormatRequested));
1358                 EXPECT_EQ(info.format, pixelFormatRequested);
1359             });
1360 }
1361 
1362 /**
1363  * Test IMapper::get(PixelFormatFourCC)
1364  */
TEST_P(GraphicsMapperHidlTest,GetPixelFormatFourCC)1365 TEST_P(GraphicsMapperHidlTest, GetPixelFormatFourCC) {
1366     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatFourCC,
1367             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1368                 uint32_t pixelFormatFourCC = 0;
1369                 ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, &pixelFormatFourCC));
1370             });
1371 }
1372 
1373 /**
1374  * Test IMapper::get(PixelFormatModifier)
1375  */
TEST_P(GraphicsMapperHidlTest,GetPixelFormatModifier)1376 TEST_P(GraphicsMapperHidlTest, GetPixelFormatModifier) {
1377     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatModifier,
1378             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1379                 uint64_t pixelFormatModifier = 0;
1380                 ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatModifier(vec, &pixelFormatModifier));
1381             });
1382 }
1383 
1384 /**
1385  * Test IMapper::get(Usage)
1386  */
TEST_P(GraphicsMapperHidlTest,GetUsage)1387 TEST_P(GraphicsMapperHidlTest, GetUsage) {
1388     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Usage,
1389             [](const IMapper::BufferDescriptorInfo& info, const hidl_vec<uint8_t>& vec) {
1390                 uint64_t usage = 0;
1391                 ASSERT_EQ(NO_ERROR, gralloc4::decodeUsage(vec, &usage));
1392                 EXPECT_EQ(info.usage, usage);
1393             });
1394 }
1395 
1396 /**
1397  * Test IMapper::get(AllocationSize)
1398  */
TEST_P(GraphicsMapperHidlTest,GetAllocationSize)1399 TEST_P(GraphicsMapperHidlTest, GetAllocationSize) {
1400     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_AllocationSize,
1401             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1402                 uint64_t allocationSize = 0;
1403                 ASSERT_EQ(NO_ERROR, gralloc4::decodeAllocationSize(vec, &allocationSize));
1404             });
1405 }
1406 
1407 /**
1408  * Test IMapper::get(ProtectedContent)
1409  */
TEST_P(GraphicsMapperHidlTest,GetProtectedContent)1410 TEST_P(GraphicsMapperHidlTest, GetProtectedContent) {
1411     auto info = mDummyDescriptorInfo;
1412     info.usage = BufferUsage::PROTECTED | BufferUsage::COMPOSER_OVERLAY;
1413 
1414     const native_handle_t* bufferHandle = nullptr;
1415     bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceAllErrors);
1416     if (!bufferHandle) {
1417         GTEST_SUCCEED() << "unable to allocate protected content";
1418         return;
1419     }
1420 
1421     hidl_vec<uint8_t> vec;
1422     ASSERT_EQ(Error::NONE,
1423               mGralloc->get(bufferHandle, gralloc4::MetadataType_ProtectedContent, &vec));
1424 
1425     uint64_t protectedContent = 0;
1426     ASSERT_EQ(NO_ERROR, gralloc4::decodeProtectedContent(vec, &protectedContent));
1427     EXPECT_EQ(1, protectedContent);
1428 }
1429 
1430 /**
1431  * Test IMapper::get(Compression)
1432  */
TEST_P(GraphicsMapperHidlTest,GetCompression)1433 TEST_P(GraphicsMapperHidlTest, GetCompression) {
1434     auto info = mDummyDescriptorInfo;
1435     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
1436 
1437     testGet(info, gralloc4::MetadataType_Compression,
1438             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1439                 ExtendableType compression = gralloc4::Compression_DisplayStreamCompression;
1440                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCompression(vec, &compression));
1441 
1442                 EXPECT_EQ(gralloc4::Compression_None.name, compression.name);
1443                 EXPECT_EQ(gralloc4::Compression_None.value, compression.value);
1444             });
1445 }
1446 
1447 /**
1448  * Test IMapper::get(Interlaced)
1449  */
TEST_P(GraphicsMapperHidlTest,GetInterlaced)1450 TEST_P(GraphicsMapperHidlTest, GetInterlaced) {
1451     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Interlaced,
1452             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1453                 ExtendableType interlaced = gralloc4::Interlaced_TopBottom;
1454                 ASSERT_EQ(NO_ERROR, gralloc4::decodeInterlaced(vec, &interlaced));
1455 
1456                 EXPECT_EQ(gralloc4::Interlaced_None.name, interlaced.name);
1457                 EXPECT_EQ(gralloc4::Interlaced_None.value, interlaced.value);
1458             });
1459 }
1460 
1461 /**
1462  * Test IMapper::get(ChromaSiting)
1463  */
TEST_P(GraphicsMapperHidlTest,GetChromaSiting)1464 TEST_P(GraphicsMapperHidlTest, GetChromaSiting) {
1465     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_ChromaSiting,
1466             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1467                 ExtendableType chromaSiting = gralloc4::ChromaSiting_Unknown;
1468                 ASSERT_EQ(NO_ERROR, gralloc4::decodeChromaSiting(vec, &chromaSiting));
1469 
1470                 EXPECT_EQ(gralloc4::ChromaSiting_None.name, chromaSiting.name);
1471                 EXPECT_EQ(gralloc4::ChromaSiting_None.value, chromaSiting.value);
1472             });
1473 }
1474 
1475 /**
1476  * Test IMapper::get(PlaneLayouts)
1477  */
TEST_P(GraphicsMapperHidlTest,GetPlaneLayouts)1478 TEST_P(GraphicsMapperHidlTest, GetPlaneLayouts) {
1479     const native_handle_t* bufferHandle = nullptr;
1480     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
1481 
1482     hidl_vec<uint8_t> vec;
1483     ASSERT_EQ(Error::NONE, mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
1484 
1485     std::vector<PlaneLayout> planeLayouts;
1486     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
1487 
1488     ASSERT_NO_FATAL_FAILURE(verifyRGBA8888PlaneLayouts(planeLayouts));
1489 }
1490 
1491 /**
1492  * Test IMapper::get(Crop)
1493  */
TEST_P(GraphicsMapperHidlTest,GetCrop)1494 TEST_P(GraphicsMapperHidlTest, GetCrop) {
1495     auto info = mDummyDescriptorInfo;
1496     info.format = PixelFormat::RGBA_8888;
1497     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
1498 
1499     testGet(info, gralloc4::MetadataType_Crop,
1500             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1501                 std::vector<aidl::android::hardware::graphics::common::Rect> crops;
1502                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCrop(vec, &crops));
1503                 EXPECT_EQ(1, crops.size());
1504             });
1505 }
1506 
1507 /**
1508  * Test IMapper::get(Dataspace)
1509  */
TEST_P(GraphicsMapperHidlTest,GetDataspace)1510 TEST_P(GraphicsMapperHidlTest, GetDataspace) {
1511     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Dataspace,
1512             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1513                 Dataspace dataspace = Dataspace::DISPLAY_P3;
1514                 ASSERT_EQ(NO_ERROR, gralloc4::decodeDataspace(vec, &dataspace));
1515                 EXPECT_EQ(Dataspace::UNKNOWN, dataspace);
1516             });
1517 }
1518 
1519 /**
1520  * Test IMapper::get(BlendMode)
1521  */
TEST_P(GraphicsMapperHidlTest,GetBlendMode)1522 TEST_P(GraphicsMapperHidlTest, GetBlendMode) {
1523     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_BlendMode,
1524             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1525                 BlendMode blendMode = BlendMode::NONE;
1526                 ASSERT_EQ(NO_ERROR, gralloc4::decodeBlendMode(vec, &blendMode));
1527                 EXPECT_EQ(BlendMode::INVALID, blendMode);
1528             });
1529 }
1530 
1531 /**
1532  * Test IMapper::get(Smpte2086)
1533  */
TEST_P(GraphicsMapperHidlTest,GetSmpte2086)1534 TEST_P(GraphicsMapperHidlTest, GetSmpte2086) {
1535     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2086,
1536             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1537                 std::optional<Smpte2086> smpte2086;
1538                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2086(vec, &smpte2086));
1539                 EXPECT_FALSE(smpte2086.has_value());
1540             });
1541 }
1542 
1543 /**
1544  * Test IMapper::get(Cta861_3)
1545  */
TEST_P(GraphicsMapperHidlTest,GetCta861_3)1546 TEST_P(GraphicsMapperHidlTest, GetCta861_3) {
1547     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Cta861_3,
1548             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1549                 std::optional<Cta861_3> cta861_3;
1550                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCta861_3(vec, &cta861_3));
1551                 EXPECT_FALSE(cta861_3.has_value());
1552             });
1553 }
1554 
1555 /**
1556  * Test IMapper::get(Smpte2094_40)
1557  */
TEST_P(GraphicsMapperHidlTest,GetSmpte2094_40)1558 TEST_P(GraphicsMapperHidlTest, GetSmpte2094_40) {
1559     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_40,
1560             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1561                 std::optional<std::vector<uint8_t>> smpte2094_40;
1562                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, &smpte2094_40));
1563                 EXPECT_FALSE(smpte2094_40.has_value());
1564             });
1565 }
1566 
1567 /**
1568  * Test IMapper::get(Smpte2094_10)
1569  */
TEST_P(GraphicsMapperHidlTest,GetSmpte2094_10)1570 TEST_P(GraphicsMapperHidlTest, GetSmpte2094_10) {
1571     testGet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_10,
1572             [](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1573                 std::optional<std::vector<uint8_t>> smpte2094_10;
1574                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_10(vec, &smpte2094_10));
1575                 EXPECT_FALSE(smpte2094_10.has_value());
1576             });
1577 }
1578 
1579 /**
1580  * Test IMapper::get(metadata) with a bad buffer
1581  */
TEST_P(GraphicsMapperHidlTest,GetMetadataBadValue)1582 TEST_P(GraphicsMapperHidlTest, GetMetadataBadValue) {
1583     const native_handle_t* bufferHandle = nullptr;
1584     hidl_vec<uint8_t> vec;
1585     ASSERT_EQ(Error::BAD_BUFFER,
1586               mGralloc->get(bufferHandle, gralloc4::MetadataType_BufferId, &vec));
1587     ASSERT_EQ(0, vec.size());
1588     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Name, &vec));
1589     ASSERT_EQ(0, vec.size());
1590     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Width, &vec));
1591     ASSERT_EQ(0, vec.size());
1592     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Height, &vec));
1593     ASSERT_EQ(0, vec.size());
1594     ASSERT_EQ(Error::BAD_BUFFER,
1595               mGralloc->get(bufferHandle, gralloc4::MetadataType_LayerCount, &vec));
1596     ASSERT_EQ(0, vec.size());
1597     ASSERT_EQ(Error::BAD_BUFFER,
1598               mGralloc->get(bufferHandle, gralloc4::MetadataType_PixelFormatRequested, &vec));
1599     ASSERT_EQ(0, vec.size());
1600     ASSERT_EQ(Error::BAD_BUFFER,
1601               mGralloc->get(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC, &vec));
1602     ASSERT_EQ(0, vec.size());
1603     ASSERT_EQ(Error::BAD_BUFFER,
1604               mGralloc->get(bufferHandle, gralloc4::MetadataType_PixelFormatModifier, &vec));
1605     ASSERT_EQ(0, vec.size());
1606     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Usage, &vec));
1607     ASSERT_EQ(0, vec.size());
1608     ASSERT_EQ(Error::BAD_BUFFER,
1609               mGralloc->get(bufferHandle, gralloc4::MetadataType_AllocationSize, &vec));
1610     ASSERT_EQ(0, vec.size());
1611     ASSERT_EQ(Error::BAD_BUFFER,
1612               mGralloc->get(bufferHandle, gralloc4::MetadataType_ProtectedContent, &vec));
1613     ASSERT_EQ(0, vec.size());
1614     ASSERT_EQ(Error::BAD_BUFFER,
1615               mGralloc->get(bufferHandle, gralloc4::MetadataType_Compression, &vec));
1616     ASSERT_EQ(0, vec.size());
1617     ASSERT_EQ(Error::BAD_BUFFER,
1618               mGralloc->get(bufferHandle, gralloc4::MetadataType_Interlaced, &vec));
1619     ASSERT_EQ(0, vec.size());
1620     ASSERT_EQ(Error::BAD_BUFFER,
1621               mGralloc->get(bufferHandle, gralloc4::MetadataType_ChromaSiting, &vec));
1622     ASSERT_EQ(0, vec.size());
1623     ASSERT_EQ(Error::BAD_BUFFER,
1624               mGralloc->get(bufferHandle, gralloc4::MetadataType_PlaneLayouts, &vec));
1625     ASSERT_EQ(0, vec.size());
1626     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->get(bufferHandle, gralloc4::MetadataType_Crop, &vec));
1627     ASSERT_EQ(0, vec.size());
1628     ASSERT_EQ(Error::BAD_BUFFER,
1629               mGralloc->get(bufferHandle, gralloc4::MetadataType_Dataspace, &vec));
1630     ASSERT_EQ(0, vec.size());
1631     ASSERT_EQ(Error::BAD_BUFFER,
1632               mGralloc->get(bufferHandle, gralloc4::MetadataType_BlendMode, &vec));
1633     ASSERT_EQ(0, vec.size());
1634     ASSERT_EQ(Error::BAD_BUFFER,
1635               mGralloc->get(bufferHandle, gralloc4::MetadataType_Smpte2086, &vec));
1636     ASSERT_EQ(0, vec.size());
1637     ASSERT_EQ(Error::BAD_BUFFER,
1638               mGralloc->get(bufferHandle, gralloc4::MetadataType_Cta861_3, &vec));
1639     ASSERT_EQ(0, vec.size());
1640     ASSERT_EQ(Error::BAD_BUFFER,
1641               mGralloc->get(bufferHandle, gralloc4::MetadataType_Smpte2094_40, &vec));
1642     ASSERT_EQ(0, vec.size());
1643     ASSERT_EQ(Error::BAD_BUFFER,
1644               mGralloc->get(bufferHandle, gralloc4::MetadataType_Smpte2094_10, &vec));
1645     ASSERT_EQ(0, vec.size());
1646 }
1647 
1648 /**
1649  * Test IMapper::get(metadata) for unsupported metadata
1650  */
TEST_P(GraphicsMapperHidlTest,GetUnsupportedMetadata)1651 TEST_P(GraphicsMapperHidlTest, GetUnsupportedMetadata) {
1652     const native_handle_t* bufferHandle = nullptr;
1653     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
1654 
1655     MetadataType metadataTypeFake = {"FAKE", 1};
1656 
1657     hidl_vec<uint8_t> vec;
1658     ASSERT_EQ(Error::UNSUPPORTED, mGralloc->get(bufferHandle, metadataTypeFake, &vec));
1659     ASSERT_EQ(0, vec.size());
1660 }
1661 
1662 /**
1663  * Test IMapper::get(metadata) for unsupported standard metadata
1664  */
TEST_P(GraphicsMapperHidlTest,GetUnsupportedStandardMetadata)1665 TEST_P(GraphicsMapperHidlTest, GetUnsupportedStandardMetadata) {
1666     const native_handle_t* bufferHandle = nullptr;
1667     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
1668 
1669     MetadataType metadataTypeFake = {GRALLOC4_STANDARD_METADATA_TYPE, 9999};
1670 
1671     hidl_vec<uint8_t> vec;
1672     ASSERT_EQ(Error::UNSUPPORTED, mGralloc->get(bufferHandle, metadataTypeFake, &vec));
1673     ASSERT_EQ(0, vec.size());
1674 }
1675 
1676 /**
1677  * Test IMapper::set(PixelFormatFourCC)
1678  */
TEST_P(GraphicsMapperHidlTest,SetPixelFormatFourCC)1679 TEST_P(GraphicsMapperHidlTest, SetPixelFormatFourCC) {
1680     uint32_t pixelFormatFourCC = 0x34324142;  // DRM_FORMAT_BGRA8888
1681     hidl_vec<uint8_t> vec;
1682     ASSERT_EQ(NO_ERROR, gralloc4::encodePixelFormatFourCC(pixelFormatFourCC, &vec));
1683 
1684     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatFourCC, vec,
1685             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1686                 uint32_t realPixelFormatFourCC = 0;
1687                 ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, &realPixelFormatFourCC));
1688                 EXPECT_EQ(pixelFormatFourCC, realPixelFormatFourCC);
1689             });
1690 }
1691 
1692 /**
1693  * Test IMapper::set(PixelFormatModifier)
1694  */
TEST_P(GraphicsMapperHidlTest,SetPixelFormatModifier)1695 TEST_P(GraphicsMapperHidlTest, SetPixelFormatModifier) {
1696     uint64_t pixelFormatModifier = 10;
1697     hidl_vec<uint8_t> vec;
1698     ASSERT_EQ(NO_ERROR, gralloc4::encodePixelFormatModifier(pixelFormatModifier, &vec));
1699 
1700     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatModifier, vec,
1701             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1702                 uint64_t realPixelFormatModifier = 0;
1703                 ASSERT_EQ(NO_ERROR,
1704                           gralloc4::decodePixelFormatModifier(vec, &realPixelFormatModifier));
1705                 EXPECT_EQ(pixelFormatModifier, realPixelFormatModifier);
1706             });
1707 }
1708 
1709 /**
1710  * Test IMapper::set(AllocationSize)
1711  */
TEST_P(GraphicsMapperHidlTest,SetAllocationSize)1712 TEST_P(GraphicsMapperHidlTest, SetAllocationSize) {
1713     uint64_t allocationSize = 1000000;
1714     hidl_vec<uint8_t> vec;
1715     ASSERT_EQ(NO_ERROR, gralloc4::encodeAllocationSize(allocationSize, &vec));
1716 
1717     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_AllocationSize, vec,
1718             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1719                 uint64_t realAllocationSize = 0;
1720                 ASSERT_EQ(NO_ERROR, gralloc4::decodeAllocationSize(vec, &realAllocationSize));
1721                 EXPECT_EQ(allocationSize, realAllocationSize);
1722             });
1723 }
1724 
1725 /**
1726  * Test IMapper::set(ProtectedContent)
1727  */
TEST_P(GraphicsMapperHidlTest,SetProtectedContent)1728 TEST_P(GraphicsMapperHidlTest, SetProtectedContent) {
1729     const native_handle_t* bufferHandle = nullptr;
1730     auto info = mDummyDescriptorInfo;
1731     info.usage = BufferUsage::PROTECTED | BufferUsage::COMPOSER_OVERLAY;
1732 
1733     bufferHandle = mGralloc->allocate(info, true, Tolerance::kToleranceAllErrors);
1734     if (!bufferHandle) {
1735         GTEST_SUCCEED() << "unable to allocate protected content";
1736         return;
1737     }
1738 
1739     uint64_t protectedContent = 0;
1740     hidl_vec<uint8_t> vec;
1741     ASSERT_EQ(NO_ERROR, gralloc4::encodeProtectedContent(protectedContent, &vec));
1742 
1743     Error err = mGralloc->set(bufferHandle, gralloc4::MetadataType_ProtectedContent, vec);
1744     ASSERT_EQ(err, Error::UNSUPPORTED);
1745     vec.resize(0);
1746 
1747     uint64_t realProtectedContent = 0;
1748     ASSERT_EQ(Error::NONE,
1749               mGralloc->get(bufferHandle, gralloc4::MetadataType_ProtectedContent, &vec));
1750     ASSERT_EQ(NO_ERROR, gralloc4::decodeProtectedContent(vec, &realProtectedContent));
1751     EXPECT_EQ(1, realProtectedContent);
1752 }
1753 
1754 /**
1755  * Test IMapper::set(Compression)
1756  */
TEST_P(GraphicsMapperHidlTest,SetCompression)1757 TEST_P(GraphicsMapperHidlTest, SetCompression) {
1758     auto info = mDummyDescriptorInfo;
1759     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
1760 
1761     ExtendableType compression = gralloc4::Compression_DisplayStreamCompression;
1762     hidl_vec<uint8_t> vec;
1763     ASSERT_EQ(NO_ERROR, gralloc4::encodeCompression(compression, &vec));
1764 
1765     testSet(info, gralloc4::MetadataType_Compression, vec,
1766             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1767                 ExtendableType realCompression = gralloc4::Compression_None;
1768                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCompression(vec, &realCompression));
1769 
1770                 EXPECT_EQ(compression.name, realCompression.name);
1771                 EXPECT_EQ(compression.value, realCompression.value);
1772             });
1773 }
1774 
1775 /**
1776  * Test IMapper::set(Interlaced)
1777  */
TEST_P(GraphicsMapperHidlTest,SetInterlaced)1778 TEST_P(GraphicsMapperHidlTest, SetInterlaced) {
1779     ExtendableType interlaced = gralloc4::Interlaced_RightLeft;
1780     hidl_vec<uint8_t> vec;
1781     ASSERT_EQ(NO_ERROR, gralloc4::encodeInterlaced(interlaced, &vec));
1782 
1783     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Interlaced, vec,
1784             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1785                 ExtendableType realInterlaced = gralloc4::Interlaced_None;
1786                 ASSERT_EQ(NO_ERROR, gralloc4::decodeInterlaced(vec, &realInterlaced));
1787 
1788                 EXPECT_EQ(interlaced.name, realInterlaced.name);
1789                 EXPECT_EQ(interlaced.value, realInterlaced.value);
1790             });
1791 }
1792 
1793 /**
1794  * Test IMapper::set(ChromaSiting)
1795  */
TEST_P(GraphicsMapperHidlTest,SetChromaSiting)1796 TEST_P(GraphicsMapperHidlTest, SetChromaSiting) {
1797     ExtendableType chromaSiting = gralloc4::ChromaSiting_SitedInterstitial;
1798     hidl_vec<uint8_t> vec;
1799     ASSERT_EQ(NO_ERROR, gralloc4::encodeChromaSiting(chromaSiting, &vec));
1800 
1801     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_ChromaSiting, vec,
1802             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1803                 ExtendableType realChromaSiting = gralloc4::ChromaSiting_None;
1804                 ASSERT_EQ(NO_ERROR, gralloc4::decodeChromaSiting(vec, &realChromaSiting));
1805 
1806                 EXPECT_EQ(chromaSiting.name, realChromaSiting.name);
1807                 EXPECT_EQ(chromaSiting.value, realChromaSiting.value);
1808             });
1809 }
1810 
1811 /**
1812  * Test IMapper::set(PlaneLayouts)
1813  */
TEST_P(GraphicsMapperHidlTest,SetPlaneLayouts)1814 TEST_P(GraphicsMapperHidlTest, SetPlaneLayouts) {
1815     const native_handle_t* bufferHandle = nullptr;
1816     auto info = mDummyDescriptorInfo;
1817     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
1818 
1819     std::vector<PlaneLayout> planeLayouts;
1820     PlaneLayout planeLayoutA;
1821     PlaneLayout planeLayoutRGB;
1822     PlaneLayoutComponent component;
1823 
1824     planeLayoutA.offsetInBytes = 0;
1825     planeLayoutA.sampleIncrementInBits = 8;
1826     planeLayoutA.strideInBytes = info.width + 20;
1827     planeLayoutA.widthInSamples = info.width;
1828     planeLayoutA.heightInSamples = info.height;
1829     planeLayoutA.totalSizeInBytes = planeLayoutA.strideInBytes * info.height;
1830     planeLayoutA.horizontalSubsampling = 1;
1831     planeLayoutA.verticalSubsampling = 1;
1832 
1833     component.type = gralloc4::PlaneLayoutComponentType_A;
1834     component.offsetInBits = 0;
1835     component.sizeInBits = 8;
1836     planeLayoutA.components.push_back(component);
1837 
1838     planeLayouts.push_back(planeLayoutA);
1839 
1840     planeLayoutRGB.offsetInBytes = 0;
1841     planeLayoutRGB.sampleIncrementInBits = 24;
1842     planeLayoutRGB.strideInBytes = info.width + 20;
1843     planeLayoutRGB.widthInSamples = info.width;
1844     planeLayoutRGB.heightInSamples = info.height;
1845     planeLayoutRGB.totalSizeInBytes = planeLayoutRGB.strideInBytes * info.height;
1846     planeLayoutRGB.horizontalSubsampling = 1;
1847     planeLayoutRGB.verticalSubsampling = 1;
1848 
1849     component.type = gralloc4::PlaneLayoutComponentType_R;
1850     planeLayoutRGB.components.push_back(component);
1851     component.type = gralloc4::PlaneLayoutComponentType_G;
1852     planeLayoutRGB.components.push_back(component);
1853     component.type = gralloc4::PlaneLayoutComponentType_B;
1854     planeLayoutRGB.components.push_back(component);
1855 
1856     planeLayouts.push_back(planeLayoutRGB);
1857 
1858     hidl_vec<uint8_t> vec;
1859     ASSERT_EQ(NO_ERROR, gralloc4::encodePlaneLayouts(planeLayouts, &vec));
1860 
1861     Error err = mGralloc->set(bufferHandle, gralloc4::MetadataType_PlaneLayouts, vec);
1862     if (err == Error::UNSUPPORTED) {
1863         GTEST_SUCCEED() << "setting this metadata is unsupported";
1864         return;
1865     }
1866     ASSERT_EQ(err, Error::NONE);
1867 
1868     std::vector<PlaneLayout> realPlaneLayouts;
1869     ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &realPlaneLayouts));
1870 
1871     ASSERT_EQ(planeLayouts.size(), realPlaneLayouts.size());
1872 
1873     for (int i = 0; i < realPlaneLayouts.size(); i++) {
1874         const auto& planeLayout = planeLayouts[i];
1875         const auto& realPlaneLayout = realPlaneLayouts[i];
1876 
1877         EXPECT_EQ(planeLayout.offsetInBytes, realPlaneLayout.offsetInBytes);
1878         EXPECT_EQ(planeLayout.sampleIncrementInBits, realPlaneLayout.sampleIncrementInBits);
1879         EXPECT_EQ(planeLayout.strideInBytes, realPlaneLayout.strideInBytes);
1880         EXPECT_EQ(planeLayout.widthInSamples, realPlaneLayout.widthInSamples);
1881         EXPECT_EQ(planeLayout.heightInSamples, realPlaneLayout.heightInSamples);
1882         EXPECT_LE(planeLayout.totalSizeInBytes, realPlaneLayout.totalSizeInBytes);
1883         EXPECT_EQ(planeLayout.horizontalSubsampling, realPlaneLayout.horizontalSubsampling);
1884         EXPECT_EQ(planeLayout.verticalSubsampling, realPlaneLayout.verticalSubsampling);
1885 
1886         ASSERT_EQ(planeLayout.components.size(), realPlaneLayout.components.size());
1887 
1888         for (int j = 0; j < realPlaneLayout.components.size(); j++) {
1889             const auto& component = planeLayout.components[j];
1890             const auto& realComponent = realPlaneLayout.components[j];
1891 
1892             EXPECT_EQ(component.type.name, realComponent.type.name);
1893             EXPECT_EQ(component.type.value, realComponent.type.value);
1894             EXPECT_EQ(component.sizeInBits, realComponent.sizeInBits);
1895             EXPECT_EQ(component.offsetInBits, realComponent.offsetInBits);
1896         }
1897     }
1898 }
1899 
1900 /**
1901  * Test IMapper::set(Crop)
1902  */
TEST_P(GraphicsMapperHidlTest,SetCrop)1903 TEST_P(GraphicsMapperHidlTest, SetCrop) {
1904     std::vector<aidl::android::hardware::graphics::common::Rect> crops{{0, 0, 32, 32}};
1905     hidl_vec<uint8_t> vec;
1906     ASSERT_EQ(NO_ERROR, gralloc4::encodeCrop(crops, &vec));
1907 
1908     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Crop, vec,
1909             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1910                 std::vector<aidl::android::hardware::graphics::common::Rect> realCrops;
1911                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCrop(vec, &realCrops));
1912                 ASSERT_EQ(1, realCrops.size());
1913                 ASSERT_EQ(crops.front().left, realCrops.front().left);
1914                 ASSERT_EQ(crops.front().top, realCrops.front().top);
1915                 ASSERT_EQ(crops.front().right, realCrops.front().right);
1916                 ASSERT_EQ(crops.front().bottom, realCrops.front().bottom);
1917             });
1918 }
1919 
1920 /**
1921  * Test IMapper::set(Dataspace)
1922  */
TEST_P(GraphicsMapperHidlTest,SetDataspace)1923 TEST_P(GraphicsMapperHidlTest, SetDataspace) {
1924     Dataspace dataspace = Dataspace::SRGB_LINEAR;
1925     hidl_vec<uint8_t> vec;
1926     ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(dataspace, &vec));
1927 
1928     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Dataspace, vec,
1929             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1930                 Dataspace realDataspace = Dataspace::UNKNOWN;
1931                 ASSERT_EQ(NO_ERROR, gralloc4::decodeDataspace(vec, &realDataspace));
1932                 EXPECT_EQ(dataspace, realDataspace);
1933             });
1934 }
1935 
1936 /**
1937  * Test IMapper::set(BlendMode)
1938  */
TEST_P(GraphicsMapperHidlTest,SetBlendMode)1939 TEST_P(GraphicsMapperHidlTest, SetBlendMode) {
1940     BlendMode blendMode = BlendMode::PREMULTIPLIED;
1941     hidl_vec<uint8_t> vec;
1942     ASSERT_EQ(NO_ERROR, gralloc4::encodeBlendMode(blendMode, &vec));
1943 
1944     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_BlendMode, vec,
1945             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1946                 BlendMode realBlendMode = BlendMode::INVALID;
1947                 ASSERT_EQ(NO_ERROR, gralloc4::decodeBlendMode(vec, &realBlendMode));
1948                 EXPECT_EQ(blendMode, realBlendMode);
1949             });
1950 }
1951 
1952 /**
1953  * Test IMapper::set(Smpte2086)
1954  */
TEST_P(GraphicsMapperHidlTest,SetSmpte2086)1955 TEST_P(GraphicsMapperHidlTest, SetSmpte2086) {
1956     /**
1957      * DISPLAY_P3 is a color space that uses the DCI_P3 primaries,
1958      * the D65 white point and the SRGB transfer functions.
1959      * Rendering Intent: Colorimetric
1960      * Primaries:
1961      *                  x       y
1962      *  green           0.265   0.690
1963      *  blue            0.150   0.060
1964      *  red             0.680   0.320
1965      *  white (D65)     0.3127  0.3290
1966      */
1967     Smpte2086 smpte2086;
1968     smpte2086.primaryRed.x = 0.680;
1969     smpte2086.primaryRed.y = 0.320;
1970     smpte2086.primaryGreen.x = 0.265;
1971     smpte2086.primaryGreen.y = 0.690;
1972     smpte2086.primaryBlue.x = 0.150;
1973     smpte2086.primaryBlue.y = 0.060;
1974     smpte2086.whitePoint.x = 0.3127;
1975     smpte2086.whitePoint.y = 0.3290;
1976     smpte2086.maxLuminance = 100.0;
1977     smpte2086.minLuminance = 0.1;
1978 
1979     hidl_vec<uint8_t> vec;
1980     ASSERT_EQ(NO_ERROR, gralloc4::encodeSmpte2086(smpte2086, &vec));
1981 
1982     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2086, vec,
1983             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
1984                 std::optional<Smpte2086> realSmpte2086;
1985                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2086(vec, &realSmpte2086));
1986                 ASSERT_TRUE(realSmpte2086.has_value());
1987                 EXPECT_TRUE(isEqual(smpte2086.primaryRed.x, realSmpte2086->primaryRed.x));
1988                 EXPECT_TRUE(isEqual(smpte2086.primaryRed.y, realSmpte2086->primaryRed.y));
1989                 EXPECT_TRUE(isEqual(smpte2086.primaryGreen.x, realSmpte2086->primaryGreen.x));
1990                 EXPECT_TRUE(isEqual(smpte2086.primaryGreen.y, realSmpte2086->primaryGreen.y));
1991                 EXPECT_TRUE(isEqual(smpte2086.primaryBlue.x, realSmpte2086->primaryBlue.x));
1992                 EXPECT_TRUE(isEqual(smpte2086.primaryBlue.y, realSmpte2086->primaryBlue.y));
1993                 EXPECT_TRUE(isEqual(smpte2086.whitePoint.x, realSmpte2086->whitePoint.x));
1994                 EXPECT_TRUE(isEqual(smpte2086.whitePoint.y, realSmpte2086->whitePoint.y));
1995                 EXPECT_TRUE(isEqual(smpte2086.maxLuminance, realSmpte2086->maxLuminance));
1996                 EXPECT_TRUE(isEqual(smpte2086.minLuminance, realSmpte2086->minLuminance));
1997             });
1998 }
1999 
2000 /**
2001  * Test IMapper::set(Cta8613)
2002  */
TEST_P(GraphicsMapperHidlTest,SetCta861_3)2003 TEST_P(GraphicsMapperHidlTest, SetCta861_3) {
2004     Cta861_3 cta861_3;
2005     cta861_3.maxContentLightLevel = 78.0;
2006     cta861_3.maxFrameAverageLightLevel = 62.0;
2007 
2008     hidl_vec<uint8_t> vec;
2009     ASSERT_EQ(NO_ERROR, gralloc4::encodeCta861_3(cta861_3, &vec));
2010 
2011     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Cta861_3, vec,
2012             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
2013                 std::optional<Cta861_3> realCta861_3;
2014                 ASSERT_EQ(NO_ERROR, gralloc4::decodeCta861_3(vec, &realCta861_3));
2015                 ASSERT_TRUE(realCta861_3.has_value());
2016                 EXPECT_TRUE(
2017                         isEqual(cta861_3.maxContentLightLevel, realCta861_3->maxContentLightLevel));
2018                 EXPECT_TRUE(isEqual(cta861_3.maxFrameAverageLightLevel,
2019                                     realCta861_3->maxFrameAverageLightLevel));
2020             });
2021 }
2022 
2023 /**
2024  * Test IMapper::set(Smpte2094_40)
2025  */
TEST_P(GraphicsMapperHidlTest,SetSmpte2094_40)2026 TEST_P(GraphicsMapperHidlTest, SetSmpte2094_40) {
2027     hidl_vec<uint8_t> vec;
2028 
2029     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_40, vec,
2030             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
2031                 std::optional<std::vector<uint8_t>> realSmpte2094_40;
2032                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, &realSmpte2094_40));
2033                 EXPECT_FALSE(realSmpte2094_40.has_value());
2034             });
2035 }
2036 
2037 /**
2038  * Test IMapper::set(Smpte2094_10)
2039  */
TEST_P(GraphicsMapperHidlTest,SetSmpte2094_10)2040 TEST_P(GraphicsMapperHidlTest, SetSmpte2094_10) {
2041     hidl_vec<uint8_t> vec;
2042 
2043     testSet(mDummyDescriptorInfo, gralloc4::MetadataType_Smpte2094_10, vec,
2044             [&](const IMapper::BufferDescriptorInfo& /*info*/, const hidl_vec<uint8_t>& vec) {
2045                 std::optional<std::vector<uint8_t>> realSmpte2094_10;
2046                 ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_10(vec, &realSmpte2094_10));
2047                 EXPECT_FALSE(realSmpte2094_10.has_value());
2048             });
2049 }
2050 
2051 /**
2052  * Test IMapper::set(metadata) with a bad buffer
2053  */
TEST_P(GraphicsMapperHidlTest,SetMetadataNullBuffer)2054 TEST_P(GraphicsMapperHidlTest, SetMetadataNullBuffer) {
2055     const native_handle_t* bufferHandle = nullptr;
2056     hidl_vec<uint8_t> vec;
2057     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_BufferId, vec));
2058     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Name, vec));
2059     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Width, vec));
2060     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Height, vec));
2061     ASSERT_EQ(Error::BAD_BUFFER,
2062               mGralloc->set(bufferHandle, gralloc4::MetadataType_LayerCount, vec));
2063     ASSERT_EQ(Error::BAD_BUFFER,
2064               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatRequested, vec));
2065     ASSERT_EQ(Error::BAD_BUFFER,
2066               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC, vec));
2067     ASSERT_EQ(Error::BAD_BUFFER,
2068               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatModifier, vec));
2069     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Usage, vec));
2070     ASSERT_EQ(Error::BAD_BUFFER,
2071               mGralloc->set(bufferHandle, gralloc4::MetadataType_AllocationSize, vec));
2072     ASSERT_EQ(Error::BAD_BUFFER,
2073               mGralloc->set(bufferHandle, gralloc4::MetadataType_ProtectedContent, vec));
2074     ASSERT_EQ(Error::BAD_BUFFER,
2075               mGralloc->set(bufferHandle, gralloc4::MetadataType_Compression, vec));
2076     ASSERT_EQ(Error::BAD_BUFFER,
2077               mGralloc->set(bufferHandle, gralloc4::MetadataType_Interlaced, vec));
2078     ASSERT_EQ(Error::BAD_BUFFER,
2079               mGralloc->set(bufferHandle, gralloc4::MetadataType_ChromaSiting, vec));
2080     ASSERT_EQ(Error::BAD_BUFFER,
2081               mGralloc->set(bufferHandle, gralloc4::MetadataType_PlaneLayouts, vec));
2082     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Crop, vec));
2083     ASSERT_EQ(Error::BAD_BUFFER,
2084               mGralloc->set(bufferHandle, gralloc4::MetadataType_Dataspace, vec));
2085     ASSERT_EQ(Error::BAD_BUFFER,
2086               mGralloc->set(bufferHandle, gralloc4::MetadataType_BlendMode, vec));
2087     ASSERT_EQ(Error::BAD_BUFFER,
2088               mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2086, vec));
2089     ASSERT_EQ(Error::BAD_BUFFER, mGralloc->set(bufferHandle, gralloc4::MetadataType_Cta861_3, vec));
2090     ASSERT_EQ(Error::BAD_BUFFER,
2091               mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2094_40, vec));
2092     ASSERT_EQ(Error::BAD_BUFFER,
2093               mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2094_10, vec));
2094 }
2095 
2096 /**
2097  * Test get::metadata with cloned native_handle
2098  */
TEST_P(GraphicsMapperHidlTest,GetMetadataClonedHandle)2099 TEST_P(GraphicsMapperHidlTest, GetMetadataClonedHandle) {
2100     const native_handle_t* bufferHandle = nullptr;
2101     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2102 
2103     const auto dataspace = Dataspace::SRGB_LINEAR;
2104     {
2105         hidl_vec<uint8_t> metadata;
2106         ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(dataspace, &metadata));
2107 
2108         Error err = mGralloc->set(bufferHandle, gralloc4::MetadataType_Dataspace, metadata);
2109         if (err == Error::UNSUPPORTED) {
2110             GTEST_SUCCEED() << "setting this metadata is unsupported";
2111             return;
2112         }
2113         ASSERT_EQ(Error::NONE, err);
2114     }
2115 
2116     const native_handle_t* importedHandle;
2117     {
2118         auto clonedHandle = native_handle_clone(bufferHandle);
2119         ASSERT_NO_FATAL_FAILURE(importedHandle = mGralloc->importBuffer(clonedHandle));
2120         native_handle_close(clonedHandle);
2121         native_handle_delete(clonedHandle);
2122     }
2123 
2124     Dataspace realSpace = Dataspace::UNKNOWN;
2125     {
2126         hidl_vec<uint8_t> metadata;
2127         ASSERT_EQ(Error::NONE,
2128                   mGralloc->get(importedHandle, gralloc4::MetadataType_Dataspace, &metadata));
2129         ASSERT_NO_FATAL_FAILURE(gralloc4::decodeDataspace(metadata, &realSpace));
2130     }
2131 
2132     EXPECT_EQ(dataspace, realSpace);
2133 }
2134 
2135 /**
2136  * Test set::metadata with cloned native_handle
2137  */
TEST_P(GraphicsMapperHidlTest,SetMetadataClonedHandle)2138 TEST_P(GraphicsMapperHidlTest, SetMetadataClonedHandle) {
2139     const native_handle_t* bufferHandle = nullptr;
2140     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2141 
2142     const native_handle_t* importedHandle;
2143     {
2144         auto clonedHandle = native_handle_clone(bufferHandle);
2145         ASSERT_NO_FATAL_FAILURE(importedHandle = mGralloc->importBuffer(clonedHandle));
2146         native_handle_close(clonedHandle);
2147         native_handle_delete(clonedHandle);
2148     }
2149 
2150     const auto dataspace = Dataspace::SRGB_LINEAR;
2151     {
2152         hidl_vec<uint8_t> metadata;
2153         ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(dataspace, &metadata));
2154 
2155         Error err = mGralloc->set(importedHandle, gralloc4::MetadataType_Dataspace, metadata);
2156         if (err == Error::UNSUPPORTED) {
2157             GTEST_SUCCEED() << "setting this metadata is unsupported";
2158             return;
2159         }
2160         ASSERT_EQ(Error::NONE, err);
2161     }
2162 
2163     Dataspace realSpace = Dataspace::UNKNOWN;
2164     {
2165         hidl_vec<uint8_t> metadata;
2166         ASSERT_EQ(Error::NONE,
2167                   mGralloc->get(bufferHandle, gralloc4::MetadataType_Dataspace, &metadata));
2168         ASSERT_NO_FATAL_FAILURE(gralloc4::decodeDataspace(metadata, &realSpace));
2169     }
2170 
2171     EXPECT_EQ(dataspace, realSpace);
2172 }
2173 
2174 /**
2175  * Test IMapper::set(metadata) for constant metadata
2176  */
TEST_P(GraphicsMapperHidlTest,SetConstantMetadata)2177 TEST_P(GraphicsMapperHidlTest, SetConstantMetadata) {
2178     const native_handle_t* bufferHandle = nullptr;
2179     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2180 
2181     uint64_t bufferId = 2;
2182     hidl_vec<uint8_t> bufferIdVec;
2183     ASSERT_EQ(NO_ERROR, gralloc4::encodeBufferId(bufferId, &bufferIdVec));
2184     ASSERT_EQ(Error::BAD_VALUE,
2185               mGralloc->set(bufferHandle, gralloc4::MetadataType_BufferId, bufferIdVec));
2186 
2187     std::string name{"new name"};
2188     hidl_vec<uint8_t> nameVec;
2189     ASSERT_EQ(NO_ERROR, gralloc4::encodeName(name, &nameVec));
2190     ASSERT_EQ(Error::BAD_VALUE, mGralloc->set(bufferHandle, gralloc4::MetadataType_Name, nameVec));
2191 
2192     uint64_t width = 32;
2193     hidl_vec<uint8_t> widthVec;
2194     ASSERT_EQ(NO_ERROR, gralloc4::encodeWidth(width, &widthVec));
2195     ASSERT_EQ(Error::BAD_VALUE,
2196               mGralloc->set(bufferHandle, gralloc4::MetadataType_Width, widthVec));
2197 
2198     uint64_t height = 32;
2199     hidl_vec<uint8_t> heightVec;
2200     ASSERT_EQ(NO_ERROR, gralloc4::encodeHeight(height, &heightVec));
2201     ASSERT_EQ(Error::BAD_VALUE,
2202               mGralloc->set(bufferHandle, gralloc4::MetadataType_Height, heightVec));
2203 
2204     uint64_t layerCount = 2;
2205     hidl_vec<uint8_t> layerCountVec;
2206     ASSERT_EQ(NO_ERROR, gralloc4::encodeLayerCount(layerCount, &layerCountVec));
2207     ASSERT_EQ(Error::BAD_VALUE,
2208               mGralloc->set(bufferHandle, gralloc4::MetadataType_LayerCount, layerCountVec));
2209 
2210     hardware::graphics::common::V1_2::PixelFormat pixelFormatRequested = PixelFormat::BLOB;
2211     hidl_vec<uint8_t> pixelFormatRequestedVec;
2212     ASSERT_EQ(NO_ERROR,
2213               gralloc4::encodePixelFormatRequested(pixelFormatRequested, &pixelFormatRequestedVec));
2214     ASSERT_EQ(Error::BAD_VALUE,
2215               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatRequested,
2216                             pixelFormatRequestedVec));
2217 
2218     uint64_t usage = 0;
2219     hidl_vec<uint8_t> usageVec;
2220     ASSERT_EQ(NO_ERROR, gralloc4::encodeUsage(usage, &usageVec));
2221     ASSERT_EQ(Error::BAD_VALUE,
2222               mGralloc->set(bufferHandle, gralloc4::MetadataType_Usage, usageVec));
2223 }
2224 
2225 /**
2226  * Test IMapper::set(metadata) for bad metadata
2227  */
TEST_P(GraphicsMapperHidlTest,SetBadMetadata)2228 TEST_P(GraphicsMapperHidlTest, SetBadMetadata) {
2229     const native_handle_t* bufferHandle = nullptr;
2230     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2231 
2232     hidl_vec<uint8_t> vec;
2233     ASSERT_EQ(Error::UNSUPPORTED,
2234               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatFourCC, vec));
2235     ASSERT_EQ(Error::UNSUPPORTED,
2236               mGralloc->set(bufferHandle, gralloc4::MetadataType_PixelFormatModifier, vec));
2237     ASSERT_EQ(Error::UNSUPPORTED,
2238               mGralloc->set(bufferHandle, gralloc4::MetadataType_AllocationSize, vec));
2239     ASSERT_EQ(Error::UNSUPPORTED,
2240               mGralloc->set(bufferHandle, gralloc4::MetadataType_ProtectedContent, vec));
2241     ASSERT_EQ(Error::UNSUPPORTED,
2242               mGralloc->set(bufferHandle, gralloc4::MetadataType_Compression, vec));
2243     ASSERT_EQ(Error::UNSUPPORTED,
2244               mGralloc->set(bufferHandle, gralloc4::MetadataType_Interlaced, vec));
2245     ASSERT_EQ(Error::UNSUPPORTED,
2246               mGralloc->set(bufferHandle, gralloc4::MetadataType_ChromaSiting, vec));
2247     ASSERT_EQ(Error::UNSUPPORTED,
2248               mGralloc->set(bufferHandle, gralloc4::MetadataType_PlaneLayouts, vec));
2249     ASSERT_EQ(Error::UNSUPPORTED, mGralloc->set(bufferHandle, gralloc4::MetadataType_Crop, vec));
2250     ASSERT_EQ(Error::UNSUPPORTED,
2251               mGralloc->set(bufferHandle, gralloc4::MetadataType_Dataspace, vec));
2252     ASSERT_EQ(Error::UNSUPPORTED,
2253               mGralloc->set(bufferHandle, gralloc4::MetadataType_BlendMode, vec));
2254 
2255     // Keep optional metadata types below and populate the encoded metadata vec
2256     // with some arbitrary different metadata because the common gralloc4::decode*()
2257     // functions do not distinguish between an empty vec and bad value.
2258     if (base::GetIntProperty("ro.vendor.api_level", __ANDROID_API_FUTURE__) >= __ANDROID_API_T__) {
2259         // Some old grallocs shipped with broken validation.
2260         ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(Dataspace::SRGB_LINEAR, &vec));
2261         ASSERT_EQ(Error::UNSUPPORTED,
2262                   mGralloc->set(bufferHandle, gralloc4::MetadataType_Smpte2086, vec));
2263         ASSERT_EQ(Error::UNSUPPORTED,
2264                   mGralloc->set(bufferHandle, gralloc4::MetadataType_Cta861_3, vec));
2265     }
2266 }
2267 
2268 /**
2269  * Test IMapper::getFromBufferDescriptorInfo(BufferId)
2270  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoBufferId)2271 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoBufferId) {
2272     hidl_vec<uint8_t> vec;
2273     ASSERT_EQ(Error::UNSUPPORTED,
2274               mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2275                                                     gralloc4::MetadataType_BufferId, &vec));
2276 }
2277 
2278 /**
2279  * Test IMapper::getFromBufferDescriptorInfo(Name)
2280  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoName)2281 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoName) {
2282     hidl_vec<uint8_t> vec;
2283     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2284                                    mDummyDescriptorInfo, gralloc4::MetadataType_Name, &vec));
2285 
2286     std::string name;
2287     ASSERT_EQ(NO_ERROR, gralloc4::decodeName(vec, &name));
2288     EXPECT_EQ(mDummyDescriptorInfo.name, name);
2289 }
2290 
2291 /**
2292  * Test IMapper::getFromBufferDescriptorInfo(Width)
2293  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoWidth)2294 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoWidth) {
2295     hidl_vec<uint8_t> vec;
2296     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2297                                    mDummyDescriptorInfo, gralloc4::MetadataType_Width, &vec));
2298 
2299     uint64_t width = 0;
2300     ASSERT_EQ(NO_ERROR, gralloc4::decodeWidth(vec, &width));
2301     EXPECT_EQ(mDummyDescriptorInfo.width, width);
2302 }
2303 
2304 /**
2305  * Test IMapper::getFromBufferDescriptorInfo(Height)
2306  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoHeight)2307 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoHeight) {
2308     hidl_vec<uint8_t> vec;
2309     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2310                                    mDummyDescriptorInfo, gralloc4::MetadataType_Height, &vec));
2311 
2312     uint64_t height = 0;
2313     ASSERT_EQ(NO_ERROR, gralloc4::decodeHeight(vec, &height));
2314     EXPECT_EQ(mDummyDescriptorInfo.height, height);
2315 }
2316 
2317 /**
2318  * Test IMapper::getFromBufferDescriptorInfo(PixelFormatRequested)
2319  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPixelFormatRequested)2320 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPixelFormatRequested) {
2321     hidl_vec<uint8_t> vec;
2322     ASSERT_EQ(Error::NONE,
2323               mGralloc->getFromBufferDescriptorInfo(
2324                       mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatRequested, &vec));
2325 
2326     PixelFormat pixelFormatRequested = PixelFormat::BLOB;
2327     ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatRequested(vec, &pixelFormatRequested));
2328     EXPECT_EQ(mDummyDescriptorInfo.format, pixelFormatRequested);
2329 }
2330 
2331 /**
2332  * Test IMapper::getFromBufferDescriptorInfo(PixelFormatFourCC)
2333  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPixelFormatFourCC)2334 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPixelFormatFourCC) {
2335     hidl_vec<uint8_t> vec;
2336     Error err = mGralloc->getFromBufferDescriptorInfo(
2337             mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatFourCC, &vec);
2338     if (err == Error::UNSUPPORTED) {
2339         GTEST_SUCCEED() << "setting this metadata is unsupported";
2340         return;
2341     }
2342     ASSERT_EQ(err, Error::NONE);
2343 
2344     uint32_t pixelFormatFourCC = 0;
2345     ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, &pixelFormatFourCC));
2346 }
2347 
2348 /**
2349  * Test IMapper::getFromBufferDescriptorInfo(PixelFormatModifier)
2350  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPixelFormatModifier)2351 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPixelFormatModifier) {
2352     hidl_vec<uint8_t> vec;
2353     Error err = mGralloc->getFromBufferDescriptorInfo(
2354             mDummyDescriptorInfo, gralloc4::MetadataType_PixelFormatModifier, &vec);
2355     if (err == Error::UNSUPPORTED) {
2356         GTEST_SUCCEED() << "setting this metadata is unsupported";
2357         return;
2358     }
2359     ASSERT_EQ(err, Error::NONE);
2360 
2361     uint64_t pixelFormatModifier = 0;
2362     ASSERT_EQ(NO_ERROR, gralloc4::decodePixelFormatModifier(vec, &pixelFormatModifier));
2363 }
2364 
2365 /**
2366  * Test IMapper::getFromBufferDescriptorInfo(Usage)
2367  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoUsage)2368 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoUsage) {
2369     hidl_vec<uint8_t> vec;
2370     ASSERT_EQ(Error::NONE, mGralloc->getFromBufferDescriptorInfo(
2371                                    mDummyDescriptorInfo, gralloc4::MetadataType_Usage, &vec));
2372 
2373     uint64_t usage = 0;
2374     ASSERT_EQ(NO_ERROR, gralloc4::decodeUsage(vec, &usage));
2375     EXPECT_EQ(mDummyDescriptorInfo.usage, usage);
2376 }
2377 
2378 /**
2379  * Test IMapper::getFromBufferDescriptorInfo(AllocationSize)
2380  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoAllocationSize)2381 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoAllocationSize) {
2382     hidl_vec<uint8_t> vec;
2383     Error err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2384                                                       gralloc4::MetadataType_AllocationSize, &vec);
2385     if (err == Error::UNSUPPORTED) {
2386         GTEST_SUCCEED() << "setting this metadata is unsupported";
2387         return;
2388     }
2389     ASSERT_EQ(err, Error::NONE);
2390 
2391     uint64_t allocationSize = 0;
2392     ASSERT_EQ(NO_ERROR, gralloc4::decodeAllocationSize(vec, &allocationSize));
2393 }
2394 
2395 /**
2396  * Test IMapper::getFromBufferDescriptorInfo(ProtectedContent)
2397  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoProtectedContent)2398 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoProtectedContent) {
2399     auto info = mDummyDescriptorInfo;
2400     info.usage = BufferUsage::PROTECTED | BufferUsage::COMPOSER_OVERLAY;
2401 
2402     hidl_vec<uint8_t> vec;
2403     auto err = mGralloc->getFromBufferDescriptorInfo(info, gralloc4::MetadataType_ProtectedContent,
2404                                                      &vec);
2405     if (err == Error::UNSUPPORTED) {
2406         GTEST_SUCCEED() << "setting this metadata is unsupported";
2407         return;
2408     }
2409     ASSERT_EQ(err, Error::NONE);
2410 
2411     uint64_t protectedContent = 0;
2412     ASSERT_EQ(NO_ERROR, gralloc4::decodeProtectedContent(vec, &protectedContent));
2413     EXPECT_EQ(1, protectedContent);
2414 }
2415 
2416 /**
2417  * Test IMapper::getFromBufferDescriptorInfo(Compression)
2418  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoCompression)2419 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoCompression) {
2420     auto info = mDummyDescriptorInfo;
2421     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
2422 
2423     hidl_vec<uint8_t> vec;
2424     auto err =
2425             mGralloc->getFromBufferDescriptorInfo(info, gralloc4::MetadataType_Compression, &vec);
2426     if (err == Error::UNSUPPORTED) {
2427         GTEST_SUCCEED() << "setting this metadata is unsupported";
2428         return;
2429     }
2430     ASSERT_EQ(err, Error::NONE);
2431 
2432     ExtendableType compression = gralloc4::Compression_DisplayStreamCompression;
2433     ASSERT_EQ(NO_ERROR, gralloc4::decodeCompression(vec, &compression));
2434 
2435     EXPECT_EQ(gralloc4::Compression_None.name, compression.name);
2436     EXPECT_EQ(gralloc4::Compression_None.value, compression.value);
2437 }
2438 
2439 /**
2440  * Test IMapper::getFromBufferDescriptorInfo(Interlaced)
2441  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoInterlaced)2442 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoInterlaced) {
2443     hidl_vec<uint8_t> vec;
2444     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2445                                                      gralloc4::MetadataType_Interlaced, &vec);
2446     if (err == Error::UNSUPPORTED) {
2447         GTEST_SUCCEED() << "setting this metadata is unsupported";
2448         return;
2449     }
2450     ASSERT_EQ(err, Error::NONE);
2451 
2452     ExtendableType interlaced = gralloc4::Interlaced_TopBottom;
2453     ASSERT_EQ(NO_ERROR, gralloc4::decodeInterlaced(vec, &interlaced));
2454 
2455     EXPECT_EQ(gralloc4::Interlaced_None.name, interlaced.name);
2456     EXPECT_EQ(gralloc4::Interlaced_None.value, interlaced.value);
2457 }
2458 
2459 /**
2460  * Test IMapper::getFromBufferDescriptorInfo(ChromaSiting)
2461  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoChromaSiting)2462 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoChromaSiting) {
2463     hidl_vec<uint8_t> vec;
2464     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2465                                                      gralloc4::MetadataType_ChromaSiting, &vec);
2466     if (err == Error::UNSUPPORTED) {
2467         GTEST_SUCCEED() << "setting this metadata is unsupported";
2468         return;
2469     }
2470     ASSERT_EQ(err, Error::NONE);
2471 
2472     ExtendableType chromaSiting = gralloc4::ChromaSiting_CositedHorizontal;
2473     ASSERT_EQ(NO_ERROR, gralloc4::decodeChromaSiting(vec, &chromaSiting));
2474 
2475     EXPECT_EQ(gralloc4::ChromaSiting_None.name, chromaSiting.name);
2476     EXPECT_EQ(gralloc4::ChromaSiting_None.value, chromaSiting.value);
2477 }
2478 
2479 /**
2480  * Test IMapper::getFromBufferDescriptorInfo(PlaneLayouts)
2481  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoPlaneLayouts)2482 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoPlaneLayouts) {
2483     hidl_vec<uint8_t> vec;
2484     const auto ret = mGralloc->getFromBufferDescriptorInfo(
2485             mDummyDescriptorInfo, gralloc4::MetadataType_PlaneLayouts, &vec);
2486     if (ret == Error::NONE) {
2487         std::vector<PlaneLayout> planeLayouts;
2488         ASSERT_EQ(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
2489         ASSERT_NO_FATAL_FAILURE(verifyRGBA8888PlaneLayouts(planeLayouts));
2490     } else {
2491         ASSERT_EQ(Error::UNSUPPORTED, ret);
2492     }
2493 }
2494 
2495 /**
2496  * Test IMapper::getFromBufferDescriptorInfo(Crop)
2497  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoCrop)2498 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoCrop) {
2499     auto info = mDummyDescriptorInfo;
2500     info.format = PixelFormat::RGBA_8888;
2501     info.usage = static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
2502 
2503     hidl_vec<uint8_t> vec;
2504     auto err = mGralloc->getFromBufferDescriptorInfo(info, gralloc4::MetadataType_Crop, &vec);
2505     if (err == Error::UNSUPPORTED) {
2506         GTEST_SUCCEED() << "setting this metadata is unsupported";
2507         return;
2508     }
2509     ASSERT_EQ(err, Error::NONE);
2510 
2511     std::vector<aidl::android::hardware::graphics::common::Rect> crops;
2512     ASSERT_EQ(NO_ERROR, gralloc4::decodeCrop(vec, &crops));
2513     EXPECT_EQ(1, crops.size());
2514 }
2515 
2516 /**
2517  * Test IMapper::getFromBufferDescriptorInfo(Dataspace)
2518  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoDataspace)2519 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoDataspace) {
2520     hidl_vec<uint8_t> vec;
2521     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2522                                                      gralloc4::MetadataType_Dataspace, &vec);
2523     if (err == Error::UNSUPPORTED) {
2524         GTEST_SUCCEED() << "setting this metadata is unsupported";
2525         return;
2526     }
2527     ASSERT_EQ(err, Error::NONE);
2528 
2529     Dataspace dataspace = Dataspace::DISPLAY_P3;
2530     ASSERT_EQ(NO_ERROR, gralloc4::decodeDataspace(vec, &dataspace));
2531     EXPECT_EQ(Dataspace::UNKNOWN, dataspace);
2532 }
2533 
2534 /**
2535  * Test IMapper::getFromBufferDescriptorInfo(BlendMode)
2536  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoBlendMode)2537 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoBlendMode) {
2538     hidl_vec<uint8_t> vec;
2539     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2540                                                      gralloc4::MetadataType_BlendMode, &vec);
2541     if (err == Error::UNSUPPORTED) {
2542         GTEST_SUCCEED() << "setting this metadata is unsupported";
2543         return;
2544     }
2545     ASSERT_EQ(err, Error::NONE);
2546 
2547     BlendMode blendMode = BlendMode::COVERAGE;
2548     ASSERT_EQ(NO_ERROR, gralloc4::decodeBlendMode(vec, &blendMode));
2549     EXPECT_EQ(BlendMode::INVALID, blendMode);
2550 }
2551 
2552 /**
2553  * Test IMapper::getFromBufferDescriptorInfo(Smpte2086)
2554  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoSmpte2086)2555 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoSmpte2086) {
2556     hidl_vec<uint8_t> vec;
2557     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2558                                                      gralloc4::MetadataType_Smpte2086, &vec);
2559     if (err == Error::UNSUPPORTED) {
2560         GTEST_SUCCEED() << "setting this metadata is unsupported";
2561         return;
2562     }
2563     ASSERT_EQ(err, Error::NONE);
2564 
2565     std::optional<Smpte2086> smpte2086;
2566     ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2086(vec, &smpte2086));
2567     EXPECT_FALSE(smpte2086.has_value());
2568 }
2569 
2570 /**
2571  * Test IMapper::getFromBufferDescriptorInfo(Cta861_3)
2572  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoCta861_3)2573 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoCta861_3) {
2574     hidl_vec<uint8_t> vec;
2575     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2576                                                      gralloc4::MetadataType_Cta861_3, &vec);
2577     if (err == Error::UNSUPPORTED) {
2578         GTEST_SUCCEED() << "setting this metadata is unsupported";
2579         return;
2580     }
2581     ASSERT_EQ(err, Error::NONE);
2582 
2583     std::optional<Cta861_3> cta861_3;
2584     ASSERT_EQ(NO_ERROR, gralloc4::decodeCta861_3(vec, &cta861_3));
2585     EXPECT_FALSE(cta861_3.has_value());
2586 }
2587 
2588 /**
2589  * Test IMapper::getFromBufferDescriptorInfo(Smpte2094_40)
2590  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoSmpte2094_40)2591 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoSmpte2094_40) {
2592     hidl_vec<uint8_t> vec;
2593     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2594                                                      gralloc4::MetadataType_Smpte2094_40, &vec);
2595     if (err == Error::UNSUPPORTED) {
2596         GTEST_SUCCEED() << "setting this metadata is unsupported";
2597         return;
2598     }
2599     ASSERT_EQ(err, Error::NONE);
2600 
2601     std::optional<std::vector<uint8_t>> smpte2094_40;
2602     ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, &smpte2094_40));
2603     EXPECT_FALSE(smpte2094_40.has_value());
2604 }
2605 
2606 /**
2607  * Test IMapper::getFromBufferDescriptorInfo(Smpte2094_10)
2608  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoSmpte2094_10)2609 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoSmpte2094_10) {
2610     hidl_vec<uint8_t> vec;
2611     auto err = mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo,
2612                                                      gralloc4::MetadataType_Smpte2094_10, &vec);
2613     if (err == Error::UNSUPPORTED) {
2614         GTEST_SUCCEED() << "setting this metadata is unsupported";
2615         return;
2616     }
2617     ASSERT_EQ(err, Error::NONE);
2618 
2619     std::optional<std::vector<uint8_t>> smpte2094_10;
2620     ASSERT_EQ(NO_ERROR, gralloc4::decodeSmpte2094_10(vec, &smpte2094_10));
2621     EXPECT_FALSE(smpte2094_10.has_value());
2622 }
2623 
2624 /**
2625  * Test IMapper::getFromBufferDescriptorInfo(metadata) for unsupported metadata
2626  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoUnsupportedMetadata)2627 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoUnsupportedMetadata) {
2628     MetadataType metadataTypeFake = {"FAKE", 1};
2629 
2630     hidl_vec<uint8_t> vec;
2631     ASSERT_EQ(Error::UNSUPPORTED,
2632               mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo, metadataTypeFake, &vec));
2633     ASSERT_EQ(0, vec.size());
2634 }
2635 
2636 /**
2637  * Test IMapper::getFromBufferDescriptorInfo(metadata) for unsupported standard metadata
2638  */
TEST_P(GraphicsMapperHidlTest,GetFromBufferDescriptorInfoUnsupportedStandardMetadata)2639 TEST_P(GraphicsMapperHidlTest, GetFromBufferDescriptorInfoUnsupportedStandardMetadata) {
2640     MetadataType metadataTypeFake = {GRALLOC4_STANDARD_METADATA_TYPE, 9999};
2641 
2642     hidl_vec<uint8_t> vec;
2643     ASSERT_EQ(Error::UNSUPPORTED,
2644               mGralloc->getFromBufferDescriptorInfo(mDummyDescriptorInfo, metadataTypeFake, &vec));
2645     ASSERT_EQ(0, vec.size());
2646 }
2647 
2648 /**
2649  * Test IMapper::listSupportedMetadataTypes()
2650  */
TEST_P(GraphicsMapperHidlTest,ListSupportedMetadataTypes)2651 TEST_P(GraphicsMapperHidlTest, ListSupportedMetadataTypes) {
2652     hidl_vec<IMapper::MetadataTypeDescription> descriptions;
2653     mGralloc->getMapper()->listSupportedMetadataTypes(
2654             [&](const auto& tmpError, const auto& tmpDescriptions) {
2655                 ASSERT_EQ(Error::NONE, tmpError);
2656                 descriptions = tmpDescriptions;
2657             });
2658 
2659     std::set<StandardMetadataType> foundMetadataTypes;
2660 
2661     std::set<StandardMetadataType> notSettableMetadataTypes{
2662             StandardMetadataType::BUFFER_ID,   StandardMetadataType::NAME,
2663             StandardMetadataType::WIDTH,       StandardMetadataType::HEIGHT,
2664             StandardMetadataType::LAYER_COUNT, StandardMetadataType::PIXEL_FORMAT_REQUESTED,
2665             StandardMetadataType::USAGE};
2666 
2667     ASSERT_LE(sRequiredMetadataTypes.size(), descriptions.size());
2668 
2669     for (const auto& description : descriptions) {
2670         const auto& metadataType = description.metadataType;
2671 
2672         if (!gralloc4::isStandardMetadataType(metadataType)) {
2673             EXPECT_GT(description.description.size(), 0);
2674             continue;
2675         }
2676 
2677         StandardMetadataType type = gralloc4::getStandardMetadataTypeValue(metadataType);
2678 
2679         if (sRequiredMetadataTypes.find(type) == sRequiredMetadataTypes.end()) {
2680             continue;
2681         }
2682 
2683         ASSERT_EQ(foundMetadataTypes.find(type), foundMetadataTypes.end());
2684         foundMetadataTypes.insert(type);
2685 
2686         ASSERT_TRUE(description.isGettable);
2687 
2688         if (notSettableMetadataTypes.find(type) != notSettableMetadataTypes.end()) {
2689             ASSERT_FALSE(description.isSettable);
2690         }
2691     }
2692 
2693     ASSERT_EQ(sRequiredMetadataTypes, foundMetadataTypes);
2694 }
2695 
2696 /**
2697  * Test IMapper::dumpBuffer()
2698  */
TEST_P(GraphicsMapperHidlTest,DumpBuffer)2699 TEST_P(GraphicsMapperHidlTest, DumpBuffer) {
2700     const native_handle_t* bufferHandle = nullptr;
2701     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
2702     auto buffer = const_cast<native_handle_t*>(bufferHandle);
2703 
2704     IMapper::BufferDump bufferDump;
2705     mGralloc->getMapper()->dumpBuffer(buffer, [&](const auto& tmpError, const auto& tmpBufferDump) {
2706         ASSERT_EQ(Error::NONE, tmpError);
2707         bufferDump = tmpBufferDump;
2708     });
2709 
2710     ASSERT_NO_FATAL_FAILURE(verifyBufferDump(bufferDump, buffer));
2711 }
2712 
2713 /**
2714  * Test IMapper::dumpBuffer() with an invalid buffer
2715  */
TEST_P(GraphicsMapperHidlTest,DumpBufferNullBuffer)2716 TEST_P(GraphicsMapperHidlTest, DumpBufferNullBuffer) {
2717     native_handle_t* bufferHandle = nullptr;
2718     auto buffer = const_cast<native_handle_t*>(bufferHandle);
2719 
2720     mGralloc->getMapper()->dumpBuffer(buffer,
2721                                       [&](const auto& tmpError, const auto& /*tmpBufferDump*/) {
2722                                           ASSERT_EQ(Error::BAD_BUFFER, tmpError);
2723                                       });
2724 }
2725 
2726 /**
2727  * Test IMapper::dumpBuffer() multiple
2728  */
TEST_P(GraphicsMapperHidlTest,DumpBuffers)2729 TEST_P(GraphicsMapperHidlTest, DumpBuffers) {
2730     size_t bufferCount = 10;
2731 
2732     for (int i = 0; i < bufferCount; i++) {
2733         ASSERT_NO_FATAL_FAILURE(mGralloc->allocate(mDummyDescriptorInfo, true));
2734     }
2735 
2736     hidl_vec<IMapper::BufferDump> bufferDump;
2737     mGralloc->getMapper()->dumpBuffers([&](const auto& tmpError, const auto& tmpBufferDump) {
2738         ASSERT_EQ(Error::NONE, tmpError);
2739         bufferDump = tmpBufferDump;
2740     });
2741 
2742     ASSERT_EQ(bufferCount, bufferDump.size());
2743 
2744     for (const auto& dump : bufferDump) {
2745         ASSERT_NO_FATAL_FAILURE(verifyBufferDump(dump));
2746     }
2747 }
2748 
2749 /**
2750  * Test IMapper::getReservedRegion()
2751  */
TEST_P(GraphicsMapperHidlTest,GetReservedRegion)2752 TEST_P(GraphicsMapperHidlTest, GetReservedRegion) {
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{1, 10, 333, static_cast<uint64_t>(pageSize) / 2,
2759                                                  static_cast<uint64_t>(pageSize)};
2760 
2761     for (auto requestedReservedSize : requestedReservedSizes) {
2762         info.reservedSize = requestedReservedSize;
2763 
2764         ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
2765 
2766         void* reservedRegion = nullptr;
2767         uint64_t reservedSize = 0;
2768         ASSERT_EQ(Error::NONE,
2769                   mGralloc->getReservedRegion(bufferHandle, &reservedRegion, &reservedSize));
2770         ASSERT_NE(nullptr, reservedRegion);
2771         ASSERT_EQ(requestedReservedSize, reservedSize);
2772 
2773         uint8_t testValue = 1;
2774         memset(reservedRegion, testValue, reservedSize);
2775         for (uint64_t i = 0; i < reservedSize; i++) {
2776             ASSERT_EQ(testValue, static_cast<uint8_t*>(reservedRegion)[i]);
2777         }
2778     }
2779 }
2780 
2781 /**
2782  * Test IMapper::getReservedRegion() request over a page
2783  */
TEST_P(GraphicsMapperHidlTest,GetLargeReservedRegion)2784 TEST_P(GraphicsMapperHidlTest, GetLargeReservedRegion) {
2785     const native_handle_t* bufferHandle = nullptr;
2786     auto info = mDummyDescriptorInfo;
2787 
2788     const int pageSize = getpagesize();
2789     ASSERT_GE(pageSize, 0);
2790     std::vector<uint64_t> requestedReservedSizes{static_cast<uint64_t>(pageSize) * 2,
2791                                                  static_cast<uint64_t>(pageSize) * 10,
2792                                                  static_cast<uint64_t>(pageSize) * 1000};
2793 
2794     for (auto requestedReservedSize : requestedReservedSizes) {
2795         info.reservedSize = requestedReservedSize;
2796 
2797         BufferDescriptor descriptor;
2798         ASSERT_NO_FATAL_FAILURE(descriptor = mGralloc->createDescriptor(info));
2799 
2800         Error err = Error::NONE;
2801 
2802         mGralloc->rawAllocate(
2803                 descriptor, 1, [&](const auto& tmpError, const auto&, const auto& tmpBuffers) {
2804                     err = tmpError;
2805                     if (err == Error::NONE) {
2806                         ASSERT_EQ(1, tmpBuffers.size());
2807                         ASSERT_NO_FATAL_FAILURE(bufferHandle =
2808                                                         mGralloc->importBuffer(tmpBuffers[0]));
2809                     }
2810                 });
2811         if (err == Error::UNSUPPORTED) {
2812             continue;
2813         }
2814         ASSERT_EQ(Error::NONE, err);
2815 
2816         void* reservedRegion = nullptr;
2817         uint64_t reservedSize = 0;
2818         err = mGralloc->getReservedRegion(bufferHandle, &reservedRegion, &reservedSize);
2819 
2820         ASSERT_EQ(Error::NONE, err);
2821         ASSERT_NE(nullptr, reservedRegion);
2822         ASSERT_EQ(requestedReservedSize, reservedSize);
2823     }
2824 }
2825 
2826 /**
2827  * Test IMapper::getReservedRegion() across multiple mappers
2828  */
TEST_P(GraphicsMapperHidlTest,GetReservedRegionMultiple)2829 TEST_P(GraphicsMapperHidlTest, GetReservedRegionMultiple) {
2830     const native_handle_t* bufferHandle = nullptr;
2831     auto info = mDummyDescriptorInfo;
2832 
2833     const int pageSize = getpagesize();
2834     ASSERT_GE(pageSize, 0);
2835     info.reservedSize = pageSize;
2836 
2837     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(info, true));
2838 
2839     void* reservedRegion1 = nullptr;
2840     uint64_t reservedSize1 = 0;
2841     ASSERT_EQ(Error::NONE,
2842               mGralloc->getReservedRegion(bufferHandle, &reservedRegion1, &reservedSize1));
2843     ASSERT_NE(nullptr, reservedRegion1);
2844     ASSERT_EQ(info.reservedSize, reservedSize1);
2845 
2846     std::unique_ptr<Gralloc> anotherGralloc;
2847     ASSERT_NO_FATAL_FAILURE(anotherGralloc = std::make_unique<Gralloc>(std::get<0>(GetParam()),
2848                                                                        std::get<1>(GetParam())));
2849 
2850     void* reservedRegion2 = nullptr;
2851     uint64_t reservedSize2 = 0;
2852     ASSERT_EQ(Error::NONE,
2853               mGralloc->getReservedRegion(bufferHandle, &reservedRegion2, &reservedSize2));
2854     ASSERT_EQ(reservedRegion1, reservedRegion2);
2855     ASSERT_EQ(reservedSize1, reservedSize2);
2856 }
2857 
2858 /**
2859  * Test IMapper::getReservedRegion() with a bad buffer
2860  */
TEST_P(GraphicsMapperHidlTest,GetReservedRegionBadBuffer)2861 TEST_P(GraphicsMapperHidlTest, GetReservedRegionBadBuffer) {
2862     const native_handle_t* bufferHandle = nullptr;
2863 
2864     void* reservedRegion = nullptr;
2865     uint64_t reservedSize = 0;
2866     ASSERT_EQ(Error::BAD_BUFFER,
2867               mGralloc->getReservedRegion(bufferHandle, &reservedRegion, &reservedSize));
2868     ASSERT_EQ(nullptr, reservedRegion);
2869     ASSERT_EQ(0, reservedSize);
2870 }
2871 
2872 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(GraphicsMapperHidlTest);
2873 
2874 namespace {
getAllocatorInstances()2875 std::vector<std::string> getAllocatorInstances() {
2876     std::vector<std::string> instances;
2877     for (auto halInstance : android::hardware::getAllHalInstanceNames(IAllocator::descriptor)) {
2878         instances.emplace_back(std::move(halInstance));
2879     }
2880 
2881     for (auto aidlInstance : getAidlHalInstanceNames(
2882                  aidl::android::hardware::graphics::allocator::IAllocator::descriptor)) {
2883         instances.emplace_back(std::move(aidlInstance));
2884     }
2885 
2886     return instances;
2887 }
2888 }  // namespace
2889 
2890 INSTANTIATE_TEST_CASE_P(
2891         PerInstance, GraphicsMapperHidlTest,
2892         testing::Combine(
2893                 testing::ValuesIn(getAllocatorInstances()),
2894                 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IMapper::descriptor))),
2895         android::hardware::PrintInstanceTupleNameToString<>);
2896 
2897 }  // namespace
2898 }  // namespace vts
2899 }  // namespace V4_0
2900 }  // namespace mapper
2901 }  // namespace graphics
2902 }  // namespace hardware
2903 }  // namespace android
2904