• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Gralloc4Test"
18 
19 #include <limits>
20 
21 #include <gralloctypes/Gralloc4.h>
22 
23 #include <gtest/gtest.h>
24 
25 using android::hardware::hidl_vec;
26 
27 using android::hardware::graphics::common::V1_2::PixelFormat;
28 using android::hardware::graphics::common::V1_2::BufferUsage;
29 
30 using aidl::android::hardware::graphics::common::BlendMode;
31 using aidl::android::hardware::graphics::common::ChromaSiting;
32 using aidl::android::hardware::graphics::common::Compression;
33 using aidl::android::hardware::graphics::common::Cta861_3;
34 using aidl::android::hardware::graphics::common::Dataspace;
35 using aidl::android::hardware::graphics::common::ExtendableType;
36 using aidl::android::hardware::graphics::common::Interlaced;
37 using aidl::android::hardware::graphics::common::PlaneLayout;
38 using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
39 using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
40 using aidl::android::hardware::graphics::common::Rect;
41 using aidl::android::hardware::graphics::common::Smpte2086;
42 using aidl::android::hardware::graphics::common::StandardMetadataType;
43 using aidl::android::hardware::graphics::common::XyColor;
44 
45 using BufferDescriptorInfo = android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
46 using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
47 
48 namespace android {
49 
50 template<class T>
51 using EncodeFunction = status_t(*)(T, hidl_vec<uint8_t>*);
52 
53 template<class T>
54 using EncodeConstFunction = status_t(*)(const T&, hidl_vec<uint8_t>*);
55 
56 template<class T>
57 using EncodeMetadataTypeFunction = status_t(*)(const MetadataType&, T, hidl_vec<uint8_t>*);
58 
59 template<class T>
60 using EncodeMetadataTypeConstFunction = status_t(*)(const MetadataType&, const T&, hidl_vec<uint8_t>*);
61 
62 template<class T>
63 using EncodeOptionalFunction = status_t(*)(const std::optional<T>&, hidl_vec<uint8_t>*);
64 
65 template<class T>
66 using DecodeFunction = status_t(*)(const hidl_vec<uint8_t>&, T*);
67 
68 template<class T>
69 using DecodeMetadataTypeFunction = status_t(*)(const MetadataType&, const hidl_vec<uint8_t>&, T*);
70 
71 template<class T>
72 using DecodeOptionalFunction = status_t(*)(const hidl_vec<uint8_t>&, std::optional<T>*);
73 
74 template<class T>
testHelper(const T & input,EncodeFunction<T> encode,DecodeFunction<T> decode)75 void testHelper(const T& input, EncodeFunction<T> encode, DecodeFunction<T> decode) {
76     hidl_vec<uint8_t> vec;
77     T output;
78     ASSERT_EQ(NO_ERROR, encode(input, &vec));
79     ASSERT_EQ(NO_ERROR, decode(vec, &output));
80     ASSERT_EQ(input, output);
81 }
82 
83 template<class T>
testHelperConst(const T & input,EncodeConstFunction<T> encode,DecodeFunction<T> decode)84 void testHelperConst(const T& input, EncodeConstFunction<T> encode, DecodeFunction<T> decode) {
85     hidl_vec<uint8_t> vec;
86     T output;
87     ASSERT_EQ(NO_ERROR, encode(input, &vec));
88     ASSERT_EQ(NO_ERROR, decode(vec, &output));
89     ASSERT_EQ(input, output);
90 }
91 
92 template<class T>
testHelperMetadataType(const T & input,EncodeMetadataTypeFunction<T> encode,DecodeMetadataTypeFunction<T> decode)93 void testHelperMetadataType(const T& input, EncodeMetadataTypeFunction<T> encode, DecodeMetadataTypeFunction<T> decode) {
94     hidl_vec<uint8_t> vec;
95     MetadataType metadataType{"vendor.mycompanyname.graphics.common.MetadataType", 0};
96     T output;
97     ASSERT_EQ(NO_ERROR, encode(metadataType, input, &vec));
98     ASSERT_EQ(NO_ERROR, decode(metadataType, vec, &output));
99     ASSERT_EQ(input, output);
100 }
101 
102 template<class T>
testHelperMetadataTypeConst(const T & input,EncodeMetadataTypeConstFunction<T> encode,DecodeMetadataTypeFunction<T> decode)103 void testHelperMetadataTypeConst(const T& input, EncodeMetadataTypeConstFunction<T> encode, DecodeMetadataTypeFunction<T> decode) {
104     hidl_vec<uint8_t> vec;
105     MetadataType metadataType{"vendor.mycompanyname.graphics.common.MetadataType", 0};
106     T output;
107     ASSERT_EQ(NO_ERROR, encode(metadataType, input, &vec));
108     ASSERT_EQ(NO_ERROR, decode(metadataType, vec, &output));
109     ASSERT_EQ(input, output);
110 }
111 
112 template<class T>
testHelperStableAidlType(const T & input,EncodeConstFunction<T> encode,DecodeFunction<T> decode)113 void testHelperStableAidlType(const T& input, EncodeConstFunction<T> encode, DecodeFunction<T> decode) {
114     hidl_vec<uint8_t> vec;
115     T output;
116     ASSERT_EQ(NO_ERROR, encode(input, &vec));
117     ASSERT_EQ(NO_ERROR, decode(vec, &output));
118     ASSERT_TRUE(input == output);
119 }
120 
121 template<class T>
testHelperStableAidlTypeOptional(const std::optional<T> & input,EncodeOptionalFunction<T> encode,DecodeOptionalFunction<T> decode)122 void testHelperStableAidlTypeOptional(const std::optional<T>& input, EncodeOptionalFunction<T> encode,
123                                       DecodeOptionalFunction<T> decode) {
124     hidl_vec<uint8_t> vec;
125     std::optional<T> tmp = input;
126     std::optional<T> output;
127     ASSERT_EQ(NO_ERROR, encode(tmp, &vec));
128     ASSERT_EQ(NO_ERROR, decode(vec, &output));
129     ASSERT_EQ(tmp.has_value(), output.has_value());
130     if (!tmp.has_value()) {
131         return;
132     }
133     ASSERT_TRUE(*tmp == *output);
134 }
135 
136 class Gralloc4TestUint32 : public testing::TestWithParam<uint32_t> { };
137 
138 INSTANTIATE_TEST_CASE_P(
139         Gralloc4TestUint32Params, Gralloc4TestUint32,
140         ::testing::Values(0, -1, 1, 5, 100, 0xFF, std::numeric_limits<uint32_t>::min(),
141                           std::numeric_limits<uint32_t>::max()));
142 
TEST_P(Gralloc4TestUint32,Uint32)143 TEST_P(Gralloc4TestUint32, Uint32) {
144     ASSERT_NO_FATAL_FAILURE(testHelperMetadataType(GetParam(), gralloc4::encodeUint32, gralloc4::decodeUint32));
145 }
146 
TEST_P(Gralloc4TestUint32,PixelFormatFourCC)147 TEST_P(Gralloc4TestUint32, PixelFormatFourCC) {
148     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodePixelFormatFourCC, gralloc4::decodePixelFormatFourCC));
149 }
150 
151 class Gralloc4TestInt32 : public testing::TestWithParam<int32_t> { };
152 
153 INSTANTIATE_TEST_CASE_P(
154         Gralloc4TestInt32Params, Gralloc4TestInt32,
155         ::testing::Values(0, 1, 5, 100, 0xFF, std::numeric_limits<int32_t>::min(),
156                           std::numeric_limits<int32_t>::max()));
157 
TEST_P(Gralloc4TestInt32,Int32)158 TEST_P(Gralloc4TestInt32, Int32) {
159     ASSERT_NO_FATAL_FAILURE(testHelperMetadataType(GetParam(), gralloc4::encodeInt32, gralloc4::decodeInt32));
160 }
161 
162 class Gralloc4TestUint64 : public testing::TestWithParam<uint64_t> { };
163 
164 INSTANTIATE_TEST_CASE_P(
165         Gralloc4TestUint64Params, Gralloc4TestUint64,
166         ::testing::Values(0, -1, 1, 5, 100, 0xFF, std::numeric_limits<uint64_t>::min(),
167                           std::numeric_limits<uint64_t>::max()));
168 
TEST_P(Gralloc4TestUint64,Uint64)169 TEST_P(Gralloc4TestUint64, Uint64) {
170     ASSERT_NO_FATAL_FAILURE(testHelperMetadataType(GetParam(), gralloc4::encodeUint64, gralloc4::decodeUint64));
171 }
172 
TEST_P(Gralloc4TestUint64,BufferId)173 TEST_P(Gralloc4TestUint64, BufferId) {
174     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodeBufferId, gralloc4::decodeBufferId));
175 }
176 
TEST_P(Gralloc4TestUint64,Width)177 TEST_P(Gralloc4TestUint64, Width) {
178     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodeWidth, gralloc4::decodeWidth));
179 }
180 
TEST_P(Gralloc4TestUint64,Height)181 TEST_P(Gralloc4TestUint64, Height) {
182     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodeHeight, gralloc4::decodeHeight));
183 }
184 
TEST_P(Gralloc4TestUint64,LayerCount)185 TEST_P(Gralloc4TestUint64, LayerCount) {
186     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodeLayerCount, gralloc4::decodeLayerCount));
187 }
188 
TEST_P(Gralloc4TestUint64,PixelFormatModifier)189 TEST_P(Gralloc4TestUint64, PixelFormatModifier) {
190     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodePixelFormatModifier, gralloc4::decodePixelFormatModifier));
191 }
192 
TEST_P(Gralloc4TestUint64,Usage)193 TEST_P(Gralloc4TestUint64, Usage) {
194     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodeUsage, gralloc4::decodeUsage));
195 }
196 
TEST_P(Gralloc4TestUint64,AllocationSize)197 TEST_P(Gralloc4TestUint64, AllocationSize) {
198     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodeAllocationSize, gralloc4::decodeAllocationSize));
199 }
200 
TEST_P(Gralloc4TestUint64,ProtectedContent)201 TEST_P(Gralloc4TestUint64, ProtectedContent) {
202     ASSERT_NO_FATAL_FAILURE(testHelper(GetParam(), gralloc4::encodeProtectedContent, gralloc4::decodeProtectedContent));
203 }
204 
205 class Gralloc4TestInt64 : public testing::TestWithParam<int64_t> { };
206 
207 INSTANTIATE_TEST_CASE_P(
208         Gralloc4TestInt64Params, Gralloc4TestInt64,
209         ::testing::Values(0, 1, 5, 100, 0xFF, std::numeric_limits<int64_t>::min(),
210                           std::numeric_limits<int64_t>::max()));
211 
TEST_P(Gralloc4TestInt64,Int64)212 TEST_P(Gralloc4TestInt64, Int64) {
213     ASSERT_NO_FATAL_FAILURE(testHelperMetadataType(GetParam(), gralloc4::encodeInt64, gralloc4::decodeInt64));
214 }
215 
216 class Gralloc4TestFloat : public testing::TestWithParam<float> { };
217 
218 INSTANTIATE_TEST_CASE_P(
219         Gralloc4TestFloatParams, Gralloc4TestFloat,
220         ::testing::Values(0.0, 1.999999, 5.5, 100.1, 1234.5678, std::numeric_limits<float>::min(),
221                           std::numeric_limits<float>::max()));
222 
TEST_P(Gralloc4TestFloat,Float)223 TEST_P(Gralloc4TestFloat, Float) {
224     ASSERT_NO_FATAL_FAILURE(testHelperMetadataType(GetParam(), gralloc4::encodeFloat, gralloc4::decodeFloat));
225 }
226 
227 class Gralloc4TestDouble : public testing::TestWithParam<double> { };
228 
229 INSTANTIATE_TEST_CASE_P(
230         Gralloc4TestDoubleParams, Gralloc4TestDouble,
231         ::testing::Values(0.0, 1.999999, 5.5, 100.1, 1234.5678, std::numeric_limits<double>::min(),
232                           std::numeric_limits<double>::max()));
233 
TEST_P(Gralloc4TestDouble,Double)234 TEST_P(Gralloc4TestDouble, Double) {
235     ASSERT_NO_FATAL_FAILURE(testHelperMetadataType(GetParam(), gralloc4::encodeDouble, gralloc4::decodeDouble));
236 }
237 
238 class Gralloc4TestString : public testing::TestWithParam<std::string> { };
239 
240 INSTANTIATE_TEST_CASE_P(
241         Gralloc4TestStringParams, Gralloc4TestString,
242         ::testing::Values("name", "aaaaa", "", "abcdefghijklmnopqrstuvwxyz", "0xFF"));
243 
TEST_P(Gralloc4TestString,String)244 TEST_P(Gralloc4TestString, String) {
245     ASSERT_NO_FATAL_FAILURE(testHelperMetadataTypeConst(GetParam(), gralloc4::encodeString, gralloc4::decodeString));
246 }
247 
TEST_P(Gralloc4TestString,Name)248 TEST_P(Gralloc4TestString, Name) {
249     ASSERT_NO_FATAL_FAILURE(testHelperConst(GetParam(), gralloc4::encodeName, gralloc4::decodeName));
250 }
251 
252 class Gralloc4TestPixelFormat : public testing::TestWithParam<PixelFormat> { };
253 
254 INSTANTIATE_TEST_CASE_P(
255         Gralloc4TestPixelFormatParams, Gralloc4TestPixelFormat,
256         ::testing::Values(PixelFormat::RGBA_8888, PixelFormat::BLOB,
257                           PixelFormat::IMPLEMENTATION_DEFINED, PixelFormat::YCBCR_420_888,
258                           PixelFormat::YV12));
259 
TEST_P(Gralloc4TestPixelFormat,PixelFormatRequested)260 TEST_P(Gralloc4TestPixelFormat, PixelFormatRequested) {
261     ASSERT_NO_FATAL_FAILURE(testHelperConst(GetParam(), gralloc4::encodePixelFormatRequested, gralloc4::decodePixelFormatRequested));
262 }
263 
264 class Gralloc4TestCompression : public testing::TestWithParam<ExtendableType> { };
265 
266 INSTANTIATE_TEST_CASE_P(
267         Gralloc4TestCompressionParams, Gralloc4TestCompression,
268         ::testing::Values(gralloc4::Compression_None, gralloc4::Compression_DisplayStreamCompression,
269             ExtendableType{"", 0},
270             ExtendableType{"vendor.mycompanyname.graphics.common.Compression", 0xFF},
271             ExtendableType{"vendor.mycompanyname.graphics.common.Compression", std::numeric_limits<int64_t>::max()}));
272 
TEST_P(Gralloc4TestCompression,Compression)273 TEST_P(Gralloc4TestCompression, Compression) {
274     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlType(GetParam(), gralloc4::encodeCompression, gralloc4::decodeCompression));
275 }
276 
277 class Gralloc4TestInterlaced : public testing::TestWithParam<ExtendableType> { };
278 
279 INSTANTIATE_TEST_CASE_P(
280         Gralloc4TestInterlacedParams, Gralloc4TestInterlaced,
281         ::testing::Values(gralloc4::Interlaced_None, gralloc4::Interlaced_TopBottom,
282             gralloc4::Interlaced_RightLeft,
283             ExtendableType{"", 0},
284             ExtendableType{"vendor.mycompanyname.graphics.common.Interlaced", 0xFF},
285             ExtendableType{"vendor.mycompanyname.graphics.common.Interlaced", std::numeric_limits<int64_t>::max()}));
286 
TEST_P(Gralloc4TestInterlaced,Interlaced)287 TEST_P(Gralloc4TestInterlaced, Interlaced) {
288     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlType(GetParam(), gralloc4::encodeInterlaced, gralloc4::decodeInterlaced));
289 }
290 
291 class Gralloc4TestChromaSiting : public testing::TestWithParam<ExtendableType> { };
292 
293 INSTANTIATE_TEST_CASE_P(
294         Gralloc4TestChromaSitingParams, Gralloc4TestChromaSiting,
295         ::testing::Values(gralloc4::ChromaSiting_None, gralloc4::ChromaSiting_Unknown,
296             gralloc4::ChromaSiting_SitedInterstitial, gralloc4::ChromaSiting_CositedHorizontal,
297             ExtendableType{"", 0},
298             ExtendableType{"vendor.mycompanyname.graphics.common.ChromaSiting", 0xFF},
299             ExtendableType{"vendor.mycompanyname.graphics.common.ChromaSiting", std::numeric_limits<int64_t>::max()}));
300 
TEST_P(Gralloc4TestChromaSiting,ChromaSiting)301 TEST_P(Gralloc4TestChromaSiting, ChromaSiting) {
302     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlType(GetParam(), gralloc4::encodeChromaSiting, gralloc4::decodeChromaSiting));
303 }
304 
305 class Gralloc4TestPlaneLayouts : public testing::Test { };
306 
TEST_F(Gralloc4TestPlaneLayouts,PlaneLayouts)307 TEST_F(Gralloc4TestPlaneLayouts, PlaneLayouts) {
308     uint32_t width = 64;
309     uint32_t height = 64;
310 
311     std::vector<PlaneLayout> planeLayouts;
312     PlaneLayout planeLayoutA;
313     PlaneLayout planeLayoutRGB;
314     PlaneLayoutComponent component;
315 
316     planeLayoutA.offsetInBytes = 0;
317     planeLayoutA.sampleIncrementInBits = 8;
318     planeLayoutA.strideInBytes = width + 20;
319     planeLayoutA.widthInSamples = width;
320     planeLayoutA.heightInSamples = height;
321     planeLayoutA.totalSizeInBytes = planeLayoutA.strideInBytes * height;
322     planeLayoutA.horizontalSubsampling = 1;
323     planeLayoutA.verticalSubsampling = 1;
324 
325     component.type = gralloc4::PlaneLayoutComponentType_A;
326     component.offsetInBits = 0;
327     component.sizeInBits = 8;
328     planeLayoutA.components.push_back(component);
329 
330     planeLayouts.push_back(planeLayoutA);
331 
332     planeLayoutRGB.offsetInBytes = 0;
333     planeLayoutRGB.sampleIncrementInBits = 32;
334     planeLayoutRGB.strideInBytes = width + 20;
335     planeLayoutRGB.widthInSamples = width;
336     planeLayoutRGB.heightInSamples = height;
337     planeLayoutRGB.totalSizeInBytes = planeLayoutRGB.strideInBytes * height;
338     planeLayoutRGB.horizontalSubsampling = 1;
339     planeLayoutRGB.verticalSubsampling = 1;
340     component.type = gralloc4::PlaneLayoutComponentType_R;
341     planeLayoutRGB.components.push_back(component);
342     component.type = gralloc4::PlaneLayoutComponentType_G;
343     planeLayoutRGB.components.push_back(component);
344     component.type = gralloc4::PlaneLayoutComponentType_B;
345     planeLayoutRGB.components.push_back(component);
346 
347     planeLayouts.push_back(planeLayoutRGB);
348 
349     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlType(planeLayouts, gralloc4::encodePlaneLayouts, gralloc4::decodePlaneLayouts));
350 }
351 
352 class Gralloc4TestCrop : public testing::Test { };
353 
TEST_F(Gralloc4TestCrop,Crop)354 TEST_F(Gralloc4TestCrop, Crop) {
355     std::vector<Rect> crops;
356     Rect crop1, crop2, crop3;
357 
358     crop1.left = 0;
359     crop1.top = 0;
360     crop1.right = 64;
361     crop1.bottom = 64;
362     crops.push_back(crop1);
363 
364     crop2.left = std::numeric_limits<int32_t>::min();
365     crop2.top = 0xFF;
366     crop2.right = std::numeric_limits<int32_t>::max();
367     crop2.bottom = 0xFFFF;
368     crops.push_back(crop2);
369 
370     crop3.left = 0;
371     crop3.top = 0;
372     crop3.right = -1;
373     crop3.bottom = -1;
374     crops.push_back(crop3);
375 
376     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlType(crops, gralloc4::encodeCrop, gralloc4::decodeCrop));
377 }
378 
379 class Gralloc4TestDataspace : public testing::TestWithParam<Dataspace> { };
380 
381 INSTANTIATE_TEST_CASE_P(
382         Gralloc4TestDataspaceParams, Gralloc4TestDataspace,
383         ::testing::Values(Dataspace::UNKNOWN, Dataspace::ARBITRARY, Dataspace::DISPLAY_P3,
384                           Dataspace::ADOBE_RGB));
385 
TEST_P(Gralloc4TestDataspace,DataspaceRequested)386 TEST_P(Gralloc4TestDataspace, DataspaceRequested) {
387     ASSERT_NO_FATAL_FAILURE(testHelperConst(GetParam(), gralloc4::encodeDataspace, gralloc4::decodeDataspace));
388 }
389 
390 class Gralloc4TestBlendMode : public testing::TestWithParam<BlendMode> { };
391 
392 INSTANTIATE_TEST_CASE_P(
393         Gralloc4TestBlendModeParams, Gralloc4TestBlendMode,
394         ::testing::Values(BlendMode::INVALID, BlendMode::NONE, BlendMode::PREMULTIPLIED,
395                           BlendMode::COVERAGE));
396 
TEST_P(Gralloc4TestBlendMode,BlendMode)397 TEST_P(Gralloc4TestBlendMode, BlendMode) {
398     ASSERT_NO_FATAL_FAILURE(testHelperConst(GetParam(), gralloc4::encodeBlendMode, gralloc4::decodeBlendMode));
399 }
400 
401 class Gralloc4TestSmpte2086 : public testing::TestWithParam<std::optional<Smpte2086>> { };
402 
403 INSTANTIATE_TEST_CASE_P(
404         Gralloc4TestSmpte2086Params, Gralloc4TestSmpte2086,
405         ::testing::Values(std::optional<Smpte2086>(Smpte2086{XyColor{0.680, 0.320},
406                                                              XyColor{0.265, 0.690},
407                                                              XyColor{0.150, 0.060},
408                                                              XyColor{0.3127, 0.3290},
409                                                              100.0, 0.1}),
410                           std::optional<Smpte2086>(Smpte2086{XyColor{-1.0, 100.0},
411                                                              XyColor{0xFF, -0xFF},
412                                                              XyColor{999.9, 0.0},
413                                                              XyColor{0.0, -1.0},
414                                                              -0.1, -100.0}),
415                           std::nullopt));
416 
TEST_P(Gralloc4TestSmpte2086,Smpte2086)417 TEST_P(Gralloc4TestSmpte2086, Smpte2086) {
418     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlTypeOptional(GetParam(), gralloc4::encodeSmpte2086, gralloc4::decodeSmpte2086));
419 }
420 
421 class Gralloc4TestCta861_3 : public testing::TestWithParam<std::optional<Cta861_3>> { };
422 
423 INSTANTIATE_TEST_CASE_P(
424         Gralloc4TestCta861_3Params, Gralloc4TestCta861_3,
425         ::testing::Values(std::optional<Cta861_3>(Cta861_3{78.0, 62.0}),
426                           std::optional<Cta861_3>(Cta861_3{10.0, 10.0}),
427                           std::optional<Cta861_3>(Cta861_3{0.0, 0.0}),
428                           std::optional<Cta861_3>(Cta861_3{std::numeric_limits<float>::min(), std::numeric_limits<float>::min()}),
429                           std::optional<Cta861_3>(Cta861_3{std::numeric_limits<float>::max(), std::numeric_limits<float>::max()}),
430                           std::nullopt));
431 
TEST_P(Gralloc4TestCta861_3,Cta861_3)432 TEST_P(Gralloc4TestCta861_3, Cta861_3) {
433     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlTypeOptional(GetParam(), gralloc4::encodeCta861_3, gralloc4::decodeCta861_3));
434 }
435 
436 class Gralloc4TestSmpte2094_40 : public testing::TestWithParam<std::optional<std::vector<uint8_t>>> { };
437 
438 INSTANTIATE_TEST_CASE_P(
439         Gralloc4TestSmpte2094_40Params, Gralloc4TestSmpte2094_40,
440         ::testing::Values(std::optional<std::vector<uint8_t>>({}),
441                           std::optional<std::vector<uint8_t>>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
442                           std::optional<std::vector<uint8_t>>({std::numeric_limits<uint8_t>::min(),
443                                                                std::numeric_limits<uint8_t>::min() + 1,
444                                                                std::numeric_limits<uint8_t>::min() + 2,
445                                                                std::numeric_limits<uint8_t>::min() + 3,
446                                                                std::numeric_limits<uint8_t>::min() + 4}),
447                           std::optional<std::vector<uint8_t>>({std::numeric_limits<uint8_t>::max(),
448                                                                std::numeric_limits<uint8_t>::max() - 1,
449                                                                std::numeric_limits<uint8_t>::max() - 2,
450                                                                std::numeric_limits<uint8_t>::max() - 3,
451                                                                std::numeric_limits<uint8_t>::max() - 4}),
452                           std::nullopt));
453 
TEST_P(Gralloc4TestSmpte2094_40,Smpte2094_40)454 TEST_P(Gralloc4TestSmpte2094_40, Smpte2094_40) {
455     ASSERT_NO_FATAL_FAILURE(testHelperStableAidlTypeOptional(GetParam(), gralloc4::encodeSmpte2094_40, gralloc4::decodeSmpte2094_40));
456 }
457 
458 class Gralloc4TestBufferDescriptorInfo : public testing::TestWithParam<BufferDescriptorInfo> { };
459 
460 INSTANTIATE_TEST_CASE_P(
461         Gralloc4TestBufferDescriptorInfoParams, Gralloc4TestBufferDescriptorInfo,
462         ::testing::Values(BufferDescriptorInfo{"BufferName", 64, 64, 1,
463                 PixelFormat::RGBA_8888,
464                 static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN),
465                 1024}));
466 
TEST_P(Gralloc4TestBufferDescriptorInfo,BufferDescriptorInfo)467 TEST_P(Gralloc4TestBufferDescriptorInfo, BufferDescriptorInfo) {
468     ASSERT_NO_FATAL_FAILURE(testHelperConst(GetParam(), gralloc4::encodeBufferDescriptorInfo, gralloc4::decodeBufferDescriptorInfo));
469 }
470 
471 class Gralloc4TestErrors : public testing::Test { };
472 
TEST_F(Gralloc4TestErrors,Gralloc4TestEncodeNull)473 TEST_F(Gralloc4TestErrors, Gralloc4TestEncodeNull) {
474     ASSERT_NE(NO_ERROR, gralloc4::encodeBufferId(0, nullptr));
475     ASSERT_NE(NO_ERROR, gralloc4::encodeName("", nullptr));
476     ASSERT_NE(NO_ERROR, gralloc4::encodeWidth(0, nullptr));
477     ASSERT_NE(NO_ERROR, gralloc4::encodeHeight(0, nullptr));
478     ASSERT_NE(NO_ERROR, gralloc4::encodeLayerCount(0, nullptr));
479     ASSERT_NE(NO_ERROR, gralloc4::encodePixelFormatRequested(PixelFormat::RGBA_8888, nullptr));
480     ASSERT_NE(NO_ERROR, gralloc4::encodePixelFormatFourCC(0, nullptr));
481     ASSERT_NE(NO_ERROR, gralloc4::encodePixelFormatModifier(0, nullptr));
482     ASSERT_NE(NO_ERROR, gralloc4::encodeUsage(0, nullptr));
483     ASSERT_NE(NO_ERROR, gralloc4::encodeAllocationSize(0, nullptr));
484     ASSERT_NE(NO_ERROR, gralloc4::encodeProtectedContent(0, nullptr));
485     ASSERT_NE(NO_ERROR, gralloc4::encodeCompression(gralloc4::Compression_None, nullptr));
486     ASSERT_NE(NO_ERROR, gralloc4::encodeInterlaced(gralloc4::Interlaced_None, nullptr));
487     ASSERT_NE(NO_ERROR, gralloc4::encodeChromaSiting(gralloc4::ChromaSiting_None, nullptr));
488     ASSERT_NE(NO_ERROR, gralloc4::encodePlaneLayouts({}, nullptr));
489     ASSERT_NE(NO_ERROR, gralloc4::encodeDataspace(Dataspace::UNKNOWN, nullptr));
490     ASSERT_NE(NO_ERROR, gralloc4::encodeBlendMode(BlendMode::NONE, nullptr));
491     ASSERT_NE(NO_ERROR, gralloc4::encodeSmpte2086({{}}, nullptr));
492     ASSERT_NE(NO_ERROR, gralloc4::encodeCta861_3({{}}, nullptr));
493     ASSERT_NE(NO_ERROR, gralloc4::encodeSmpte2094_40({{}}, nullptr));
494 }
495 
TEST_F(Gralloc4TestErrors,Gralloc4TestDecodeNull)496 TEST_F(Gralloc4TestErrors, Gralloc4TestDecodeNull) {
497     hidl_vec<uint8_t> vec;
498 
499     ASSERT_NE(NO_ERROR, gralloc4::decodeBufferId(vec, nullptr));
500     ASSERT_NE(NO_ERROR, gralloc4::decodeName(vec, nullptr));
501     ASSERT_NE(NO_ERROR, gralloc4::decodeWidth(vec, nullptr));
502     ASSERT_NE(NO_ERROR, gralloc4::decodeHeight(vec, nullptr));
503     ASSERT_NE(NO_ERROR, gralloc4::decodeLayerCount(vec, nullptr));
504     ASSERT_NE(NO_ERROR, gralloc4::decodePixelFormatRequested(vec, nullptr));
505     ASSERT_NE(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, nullptr));
506     ASSERT_NE(NO_ERROR, gralloc4::decodePixelFormatModifier(vec, nullptr));
507     ASSERT_NE(NO_ERROR, gralloc4::decodeUsage(vec, nullptr));
508     ASSERT_NE(NO_ERROR, gralloc4::decodeAllocationSize(vec, nullptr));
509     ASSERT_NE(NO_ERROR, gralloc4::decodeProtectedContent(vec, nullptr));
510     ASSERT_NE(NO_ERROR, gralloc4::decodeCompression(vec, nullptr));
511     ASSERT_NE(NO_ERROR, gralloc4::decodeInterlaced(vec, nullptr));
512     ASSERT_NE(NO_ERROR, gralloc4::decodeChromaSiting(vec, nullptr));
513     ASSERT_NE(NO_ERROR, gralloc4::decodePlaneLayouts(vec, nullptr));
514     ASSERT_NE(NO_ERROR, gralloc4::decodeDataspace(vec, nullptr));
515     ASSERT_NE(NO_ERROR, gralloc4::decodeBlendMode(vec, nullptr));
516     ASSERT_NE(NO_ERROR, gralloc4::decodeSmpte2086(vec, nullptr));
517     ASSERT_NE(NO_ERROR, gralloc4::decodeCta861_3(vec, nullptr));
518     ASSERT_NE(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, nullptr));
519 }
520 
TEST_F(Gralloc4TestErrors,Gralloc4TestDecodeBadVec)521 TEST_F(Gralloc4TestErrors, Gralloc4TestDecodeBadVec) {
522     hidl_vec<uint8_t> vec = { 0 };
523 
524     uint64_t bufferId, width, height, layerCount, pixelFormatModifier, usage, allocationSize,
525             protectedContent;
526     std::string name;
527     PixelFormat pixelFormatRequested;
528     uint32_t pixelFormatFourCC;
529     ExtendableType compression, interlaced, chromaSiting;
530     std::vector<PlaneLayout> planeLayouts;
531     Dataspace dataspace;
532     BlendMode blendMode;
533     std::optional<Smpte2086> smpte2086;
534     std::optional<Cta861_3> cta861_3;
535     std::optional<std::vector<uint8_t>> smpte2094_40;
536 
537     ASSERT_NE(NO_ERROR, gralloc4::decodeBufferId(vec, &bufferId));
538     ASSERT_NE(NO_ERROR, gralloc4::decodeName(vec, &name));
539     ASSERT_NE(NO_ERROR, gralloc4::decodeWidth(vec, &width));
540     ASSERT_NE(NO_ERROR, gralloc4::decodeHeight(vec, &height));
541     ASSERT_NE(NO_ERROR, gralloc4::decodeLayerCount(vec, &layerCount));
542     ASSERT_NE(NO_ERROR, gralloc4::decodePixelFormatRequested(vec, &pixelFormatRequested));
543     ASSERT_NE(NO_ERROR, gralloc4::decodePixelFormatFourCC(vec, &pixelFormatFourCC));
544     ASSERT_NE(NO_ERROR, gralloc4::decodePixelFormatModifier(vec, &pixelFormatModifier));
545     ASSERT_NE(NO_ERROR, gralloc4::decodeUsage(vec, &usage));
546     ASSERT_NE(NO_ERROR, gralloc4::decodeAllocationSize(vec, &allocationSize));
547     ASSERT_NE(NO_ERROR, gralloc4::decodeProtectedContent(vec, &protectedContent));
548     ASSERT_NE(NO_ERROR, gralloc4::decodeCompression(vec, &compression));
549     ASSERT_NE(NO_ERROR, gralloc4::decodeInterlaced(vec, &interlaced));
550     ASSERT_NE(NO_ERROR, gralloc4::decodeChromaSiting(vec, &chromaSiting));
551     ASSERT_NE(NO_ERROR, gralloc4::decodePlaneLayouts(vec, &planeLayouts));
552     ASSERT_NE(NO_ERROR, gralloc4::decodeDataspace(vec, &dataspace));
553     ASSERT_NE(NO_ERROR, gralloc4::decodeBlendMode(vec, &blendMode));
554     ASSERT_NE(NO_ERROR, gralloc4::decodeSmpte2086(vec, &smpte2086));
555     ASSERT_NE(NO_ERROR, gralloc4::decodeCta861_3(vec, &cta861_3));
556     ASSERT_NE(NO_ERROR, gralloc4::decodeSmpte2094_40(vec, &smpte2094_40));
557 }
558 
559 class Gralloc4TestHelpers : public testing::Test { };
560 
TEST_F(Gralloc4TestHelpers,Gralloc4TestIsStandard)561 TEST_F(Gralloc4TestHelpers, Gralloc4TestIsStandard) {
562     ASSERT_TRUE(gralloc4::isStandardMetadataType(gralloc4::MetadataType_BufferId));
563     ASSERT_TRUE(gralloc4::isStandardCompression(gralloc4::Compression_None));
564     ASSERT_TRUE(gralloc4::isStandardInterlaced(gralloc4::Interlaced_None));
565     ASSERT_TRUE(gralloc4::isStandardChromaSiting(gralloc4::ChromaSiting_None));
566     ASSERT_TRUE(gralloc4::isStandardPlaneLayoutComponentType(gralloc4::PlaneLayoutComponentType_Y));
567 }
568 
TEST_F(Gralloc4TestHelpers,Gralloc4TestIsNotStandard)569 TEST_F(Gralloc4TestHelpers, Gralloc4TestIsNotStandard) {
570     ASSERT_FALSE(gralloc4::isStandardMetadataType({"vendor.mycompanyname.graphics.common.MetadataType", 0}));
571     ASSERT_FALSE(gralloc4::isStandardCompression({"vendor.mycompanyname.graphics.common.Compression", 0}));
572     ASSERT_FALSE(gralloc4::isStandardInterlaced({"vendor.mycompanyname.graphics.common.Interlaced", 0}));
573     ASSERT_FALSE(gralloc4::isStandardChromaSiting({"vendor.mycompanyname.graphics.common.ChromaSiting", 0}));
574     ASSERT_FALSE(gralloc4::isStandardPlaneLayoutComponentType({"vendor.mycompanyname.graphics.common.PlaneLayoutComponentType", 0}));
575 }
576 
TEST_F(Gralloc4TestHelpers,Gralloc4TestGetStandardValue)577 TEST_F(Gralloc4TestHelpers, Gralloc4TestGetStandardValue) {
578     ASSERT_EQ(StandardMetadataType::BUFFER_ID, gralloc4::getStandardMetadataTypeValue(gralloc4::MetadataType_BufferId));
579     ASSERT_EQ(Compression::NONE, gralloc4::getStandardCompressionValue(gralloc4::Compression_None));
580     ASSERT_EQ(Interlaced::NONE, gralloc4::getStandardInterlacedValue(gralloc4::Interlaced_None));
581     ASSERT_EQ(ChromaSiting::NONE, gralloc4::getStandardChromaSitingValue(gralloc4::ChromaSiting_None));
582     ASSERT_EQ(PlaneLayoutComponentType::Y, gralloc4::getStandardPlaneLayoutComponentTypeValue(gralloc4::PlaneLayoutComponentType_Y));
583 }
584 
585 } // namespace android
586