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