1 /*
2  * Copyright (C) 2022 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 #include <gtest/gtest.h>
18 
19 #include <android/hardware/graphics/mapper/utils/IMapperMetadataTypes.h>
20 #include <android/hardware/graphics/mapper/utils/IMapperProvider.h>
21 #include <drm/drm_fourcc.h>
22 #include <gralloctypes/Gralloc4.h>
23 #include <span>
24 #include <vector>
25 
26 using namespace ::android;
27 using namespace ::android::hardware::graphics::mapper;
28 using namespace ::aidl::android::hardware::graphics::common;
29 namespace gralloc4 = ::android::gralloc4;
30 using ::android::hardware::hidl_vec;
31 
32 // These tests are primarily interested in hitting all the different *types* that can be
33 // serialized/deserialized than in exhaustively testing all the StandardMetadataTypes.
34 // Exhaustive testing of the actual metadata types is relegated for IMapper's VTS suite
35 // where meaning & correctness of values are more narrowly defined (eg, read-only values)
36 
37 static constexpr auto HeaderSize = 69;
38 
SkipHeader(std::vector<uint8_t> & buffer)39 static std::span<uint8_t> SkipHeader(std::vector<uint8_t>& buffer) {
40     return std::span<uint8_t>(buffer).subspan(HeaderSize);
41 }
42 
fakePlaneLayouts()43 static std::vector<PlaneLayout> fakePlaneLayouts() {
44     PlaneLayout myPlaneLayout;
45     myPlaneLayout.offsetInBytes = 10;
46     myPlaneLayout.sampleIncrementInBits = 11;
47     myPlaneLayout.strideInBytes = 12;
48     myPlaneLayout.widthInSamples = 13;
49     myPlaneLayout.heightInSamples = 14;
50     myPlaneLayout.totalSizeInBytes = 15;
51     myPlaneLayout.horizontalSubsampling = 16;
52     myPlaneLayout.verticalSubsampling = 17;
53 
54     myPlaneLayout.components.resize(3);
55     for (int i = 0; i < myPlaneLayout.components.size(); i++) {
56         auto& it = myPlaneLayout.components[i];
57         it.type = ExtendableType{"Plane ID", 40 + i};
58         it.offsetInBits = 20 + i;
59         it.sizeInBits = 30 + i;
60     }
61 
62     return std::vector<PlaneLayout>{myPlaneLayout, PlaneLayout{}};
63 }
64 
TEST(Metadata,setGetBufferId)65 TEST(Metadata, setGetBufferId) {
66     using BufferId = StandardMetadata<StandardMetadataType::BUFFER_ID>::value;
67 
68     std::vector<uint8_t> buffer(10000, 0);
69     int64_t* payload = reinterpret_cast<int64_t*>(SkipHeader(buffer).data());
70     *payload = 42;
71 
72     EXPECT_EQ(8 + HeaderSize, BufferId::encode(18, buffer.data(), 0));
73     EXPECT_EQ(42, *payload);
74     EXPECT_EQ(8 + HeaderSize, BufferId::encode(18, buffer.data(), buffer.size()));
75     EXPECT_EQ(18, *payload);
76     EXPECT_FALSE(BufferId::decode(buffer.data(), 0));
77     auto read = BufferId::decode(buffer.data(), buffer.size());
78     EXPECT_TRUE(read.has_value());
79     EXPECT_EQ(18, read.value_or(0));
80 }
81 
TEST(Metadata,setGetDataspace)82 TEST(Metadata, setGetDataspace) {
83     using DataspaceValue = StandardMetadata<StandardMetadataType::DATASPACE>::value;
84     using intType = std::underlying_type_t<Dataspace>;
85     std::vector<uint8_t> buffer(10000, 0);
86     auto data = SkipHeader(buffer);
87 
88     EXPECT_EQ(4 + HeaderSize, DataspaceValue::encode(Dataspace::BT2020, buffer.data(), 0));
89     EXPECT_EQ(0, *reinterpret_cast<intType*>(data.data()));
90     EXPECT_EQ(4 + HeaderSize,
91               DataspaceValue::encode(Dataspace::BT2020, buffer.data(), buffer.size()));
92     EXPECT_EQ(static_cast<intType>(Dataspace::BT2020), *reinterpret_cast<intType*>(data.data()));
93     EXPECT_FALSE(DataspaceValue::decode(buffer.data(), 0));
94     auto read = DataspaceValue::decode(buffer.data(), buffer.size());
95     ASSERT_TRUE(read.has_value());
96     EXPECT_EQ(Dataspace::BT2020, *read);
97 }
98 
TEST(Metadata,setGetValidName)99 TEST(Metadata, setGetValidName) {
100     using NameValue = StandardMetadata<StandardMetadataType::NAME>::value;
101 
102     std::vector<uint8_t> buffer(10000, 'a');
103 
104     // len("Hello") + sizeof(int64)
105     constexpr int expectedSize = 5 + sizeof(int64_t) + HeaderSize;
106     EXPECT_EQ(expectedSize, NameValue::encode("Hello", buffer.data(), buffer.size()));
107     EXPECT_EQ(5, *reinterpret_cast<int64_t*>(SkipHeader(buffer).data()));
108     // Verify didn't write past the end of the desired size
109     EXPECT_EQ('a', buffer[expectedSize]);
110 
111     auto readValue = NameValue::decode(buffer.data(), buffer.size());
112     ASSERT_TRUE(readValue.has_value());
113     EXPECT_EQ(5, readValue->length());
114     EXPECT_EQ("Hello", *readValue);
115 }
116 
TEST(Metadata,setGetInvalidName)117 TEST(Metadata, setGetInvalidName) {
118     using NameValue = StandardMetadata<StandardMetadataType::NAME>::value;
119 
120     std::vector<uint8_t> buffer;
121     buffer.resize(12 + HeaderSize, 'a');
122     buffer[buffer.size() - 1] = '\0';
123 
124     // len("This is a long string") + sizeof(int64)
125     constexpr int expectedSize = 21 + sizeof(int64_t) + HeaderSize;
126     EXPECT_EQ(expectedSize,
127               NameValue::encode("This is a long string", buffer.data(), buffer.size()));
128     EXPECT_EQ(21, *reinterpret_cast<int64_t*>(SkipHeader(buffer).data()));
129 
130     auto readValue = NameValue::decode(buffer.data(), buffer.size());
131     EXPECT_FALSE(readValue.has_value());
132     readValue = NameValue::decode(buffer.data(), 0);
133     ASSERT_FALSE(readValue.has_value());
134 }
135 
TEST(Metadata,wouldOverflowName)136 TEST(Metadata, wouldOverflowName) {
137     using NameValue = StandardMetadata<StandardMetadataType::NAME>::value;
138     std::vector<uint8_t> buffer(10000, 0);
139 
140     // int_max + sizeof(int64) overflows int32
141     std::string_view bad_string{"badbeef", std::numeric_limits<int32_t>::max()};
142     EXPECT_EQ(-AIMAPPER_ERROR_BAD_VALUE,
143               NameValue::encode(bad_string, buffer.data(), buffer.size()));
144 
145     // check barely overflows
146     bad_string = std::string_view{"badbeef", std::numeric_limits<int32_t>::max() - 7};
147     EXPECT_EQ(-AIMAPPER_ERROR_BAD_VALUE,
148               NameValue::encode(bad_string, buffer.data(), buffer.size()));
149 }
150 
TEST(Metadata,setGetMismatchedWidthHight)151 TEST(Metadata, setGetMismatchedWidthHight) {
152     // Validates that the header is properly validated on decode
153     using WidthValue = StandardMetadata<StandardMetadataType::WIDTH>::value;
154     using HeightValue = StandardMetadata<StandardMetadataType::HEIGHT>::value;
155     std::vector<uint8_t> buffer(10000, 0);
156 
157     EXPECT_EQ(8 + HeaderSize, WidthValue::encode(100, buffer.data(), buffer.size()));
158     EXPECT_EQ(100, *reinterpret_cast<uint64_t*>(SkipHeader(buffer).data()));
159     auto read = WidthValue::decode(buffer.data(), buffer.size());
160     ASSERT_TRUE(read.has_value());
161     EXPECT_EQ(100, *read);
162     read = HeightValue::decode(buffer.data(), buffer.size());
163     EXPECT_FALSE(read.has_value());
164 }
165 
TEST(Metadata,setGetCompression)166 TEST(Metadata, setGetCompression) {
167     using CompressionValue = StandardMetadata<StandardMetadataType::COMPRESSION>::value;
168     ExtendableType myCompression{"bestest_compression_ever", 42};
169     std::vector<uint8_t> buffer(10000, 0);
170     const int expectedSize =
171             myCompression.name.length() + sizeof(int64_t) + sizeof(int64_t) + HeaderSize;
172     EXPECT_EQ(expectedSize, CompressionValue::encode(myCompression, buffer.data(), 0));
173     EXPECT_EQ(0, buffer[0]);
174     EXPECT_EQ(expectedSize, CompressionValue::encode(myCompression, buffer.data(), buffer.size()));
175     EXPECT_EQ(myCompression.name.length(), *reinterpret_cast<int64_t*>(SkipHeader(buffer).data()));
176     EXPECT_FALSE(CompressionValue::decode(buffer.data(), 0).has_value());
177     auto read = CompressionValue::decode(buffer.data(), buffer.size());
178     ASSERT_TRUE(read.has_value());
179     EXPECT_EQ(myCompression, read.value());
180 }
181 
TEST(Metadata,setGetPlaneLayout)182 TEST(Metadata, setGetPlaneLayout) {
183     using PlaneLayoutValue = StandardMetadata<StandardMetadataType::PLANE_LAYOUTS>::value;
184 
185     std::vector<PlaneLayout> layouts = fakePlaneLayouts();
186 
187     std::vector<uint8_t> buffer(10000, 0);
188     constexpr int componentSize = 8 + (4 * sizeof(int64_t));
189     constexpr int firstLayoutSize = (8 + 1) * sizeof(int64_t) + (3 * componentSize);
190     constexpr int secondLayoutSize = (8 + 1) * sizeof(int64_t);
191     constexpr int expectedSize = firstLayoutSize + secondLayoutSize + sizeof(int64_t) + HeaderSize;
192     EXPECT_EQ(expectedSize, PlaneLayoutValue::encode(layouts, buffer.data(), 0));
193     EXPECT_EQ(0, buffer[0]);
194     EXPECT_EQ(expectedSize, PlaneLayoutValue::encode(layouts, buffer.data(), buffer.size()));
195     int64_t* payload = reinterpret_cast<int64_t*>(SkipHeader(buffer).data());
196     EXPECT_EQ(3, payload[1]);
197     EXPECT_EQ(8, payload[2]);
198     EXPECT_EQ(40, payload[4]);
199     EXPECT_EQ(31, payload[11]);
200     EXPECT_EQ(22, payload[15]);
201     EXPECT_EQ(10, payload[17]);
202     EXPECT_EQ(11, payload[18]);
203     EXPECT_FALSE(PlaneLayoutValue::decode(buffer.data(), 0).has_value());
204     auto read = PlaneLayoutValue::decode(buffer.data(), buffer.size());
205     ASSERT_TRUE(read.has_value());
206     EXPECT_EQ(layouts, *read);
207 }
208 
TEST(Metadata,setGetRects)209 TEST(Metadata, setGetRects) {
210     using RectsValue = StandardMetadata<StandardMetadataType::CROP>::value;
211     std::vector<uint8_t> buffer(10000, 0);
212     std::vector<Rect> cropRects{2};
213     cropRects[0] = Rect{10, 11, 12, 13};
214     cropRects[1] = Rect{20, 21, 22, 23};
215 
216     constexpr int expectedSize = sizeof(int64_t) + (8 * sizeof(int32_t)) + HeaderSize;
217     EXPECT_EQ(expectedSize, RectsValue::encode(cropRects, buffer.data(), buffer.size()));
218     EXPECT_EQ(2, reinterpret_cast<int64_t*>(SkipHeader(buffer).data())[0]);
219     EXPECT_EQ(10, reinterpret_cast<int32_t*>(SkipHeader(buffer).data())[2]);
220     auto read = RectsValue::decode(buffer.data(), buffer.size());
221     ASSERT_TRUE(read.has_value());
222     EXPECT_EQ(cropRects.size(), read->size());
223     EXPECT_EQ(cropRects, *read);
224 }
225 
TEST(Metadata,setGetSmpte2086)226 TEST(Metadata, setGetSmpte2086) {
227     using Smpte2086Value = StandardMetadata<StandardMetadataType::SMPTE2086>::value;
228     Smpte2086 source;
229     source.minLuminance = 12.335f;
230     source.maxLuminance = 452.889f;
231     source.whitePoint = XyColor{-6.f, -9.f};
232     source.primaryRed = XyColor{.1f, .2f};
233     source.primaryGreen = XyColor{.3f, .4f};
234     source.primaryBlue = XyColor{.5f, .6f};
235 
236     constexpr int expectedSize = 10 * sizeof(float) + HeaderSize;
237     std::vector<uint8_t> buffer(10000, 0);
238     EXPECT_EQ(expectedSize, Smpte2086Value::encode(source, buffer.data(), buffer.size()));
239     auto read = Smpte2086Value::decode(buffer.data(), buffer.size());
240     ASSERT_TRUE(read.has_value());
241     ASSERT_TRUE(read->has_value());
242     EXPECT_EQ(source, read->value());
243 
244     // A valid encoding of a nullopt
245     read = Smpte2086Value::decode(nullptr, 0);
246     ASSERT_TRUE(read.has_value());
247     EXPECT_FALSE(read->has_value());
248 }
249 
TEST(Metadata,setGetCta861_3)250 TEST(Metadata, setGetCta861_3) {
251     using Cta861_3Value = StandardMetadata<StandardMetadataType::CTA861_3>::value;
252     Cta861_3 source;
253     source.maxFrameAverageLightLevel = 244.55f;
254     source.maxContentLightLevel = 202.202f;
255 
256     constexpr int expectedSize = 2 * sizeof(float) + HeaderSize;
257     std::vector<uint8_t> buffer(10000, 0);
258     EXPECT_EQ(expectedSize, Cta861_3Value::encode(source, buffer.data(), buffer.size()));
259     auto read = Cta861_3Value::decode(buffer.data(), buffer.size());
260     ASSERT_TRUE(read.has_value());
261     ASSERT_TRUE(read->has_value());
262     EXPECT_EQ(source, read->value());
263 
264     // A valid encoding of a nullopt
265     read = Cta861_3Value::decode(nullptr, 0);
266     ASSERT_TRUE(read.has_value());
267     EXPECT_FALSE(read->has_value());
268 }
269 
TEST(Metadata,setGetSmpte2094_10)270 TEST(Metadata, setGetSmpte2094_10) {
271     using SMPTE2094_10Value = StandardMetadata<StandardMetadataType::SMPTE2094_10>::value;
272 
273     std::vector<uint8_t> buffer(10000, 0);
274     EXPECT_EQ(0, SMPTE2094_10Value::encode(std::nullopt, buffer.data(), buffer.size()));
275     auto read = SMPTE2094_10Value::decode(buffer.data(), 0);
276     ASSERT_TRUE(read.has_value());
277     EXPECT_FALSE(read->has_value());
278 
279     const std::vector<uint8_t> emptyBuffer;
280     EXPECT_EQ(sizeof(int64_t) + HeaderSize,
281               SMPTE2094_10Value::encode(emptyBuffer, buffer.data(), buffer.size()));
282     read = SMPTE2094_10Value::decode(buffer.data(), buffer.size());
283     ASSERT_TRUE(read.has_value());
284     ASSERT_TRUE(read->has_value());
285     EXPECT_EQ(0, read->value().size());
286 
287     const std::vector<uint8_t> simpleBuffer{0, 1, 2, 3, 4, 5};
288     EXPECT_EQ(sizeof(int64_t) + 6 + HeaderSize,
289               SMPTE2094_10Value::encode(simpleBuffer, buffer.data(), buffer.size()));
290     read = SMPTE2094_10Value::decode(buffer.data(), buffer.size());
291     ASSERT_TRUE(read.has_value());
292     ASSERT_TRUE(read->has_value());
293     EXPECT_EQ(6, read->value().size());
294     EXPECT_EQ(simpleBuffer, read->value());
295 }
296 
TEST(MetadataProvider,bufferId)297 TEST(MetadataProvider, bufferId) {
298     using BufferId = StandardMetadata<StandardMetadataType::BUFFER_ID>::value;
299     std::vector<uint8_t> buffer(10000, 0);
300     int result = provideStandardMetadata(StandardMetadataType::BUFFER_ID, buffer.data(),
301                                          buffer.size(), []<StandardMetadataType T>(auto&& provide) {
302                                              if constexpr (T == StandardMetadataType::BUFFER_ID) {
303                                                  return provide(42);
304                                              }
305                                              return 0;
306                                          });
307 
308     EXPECT_EQ(8 + HeaderSize, result);
309     auto read = BufferId::decode(buffer.data(), buffer.size());
310     EXPECT_EQ(42, read.value_or(0));
311 }
312 
TEST(MetadataProvider,allJumpsWork)313 TEST(MetadataProvider, allJumpsWork) {
314     const auto& values = ndk::internal::enum_values<StandardMetadataType>;
315     auto get = [](StandardMetadataType type) -> int {
316         return provideStandardMetadata(type, nullptr, 0, []<StandardMetadataType T>(auto&&) {
317             return static_cast<int>(T) + 100;
318         });
319     };
320 
321     for (auto& type : values) {
322         const int expected = type == StandardMetadataType::INVALID ? -AIMAPPER_ERROR_UNSUPPORTED
323                                                                    : static_cast<int>(type) + 100;
324         EXPECT_EQ(expected, get(type));
325     }
326 }
327 
TEST(MetadataProvider,invalid)328 TEST(MetadataProvider, invalid) {
329     int result = provideStandardMetadata(StandardMetadataType::INVALID, nullptr, 0,
330                                          []<StandardMetadataType T>(auto&&) { return 10; });
331 
332     EXPECT_EQ(-AIMAPPER_ERROR_UNSUPPORTED, result);
333 }
334 
TEST(MetadataProvider,outOfBounds)335 TEST(MetadataProvider, outOfBounds) {
336     int result = provideStandardMetadata(static_cast<StandardMetadataType>(-1), nullptr, 0,
337                                          []<StandardMetadataType T>(auto&&) { return 10; });
338     EXPECT_EQ(-AIMAPPER_ERROR_UNSUPPORTED, result) << "-1 should have resulted in UNSUPPORTED";
339 
340     result = provideStandardMetadata(static_cast<StandardMetadataType>(100), nullptr, 0,
341                                      []<StandardMetadataType T>(auto&&) { return 10; });
342     EXPECT_EQ(-AIMAPPER_ERROR_UNSUPPORTED, result)
343             << "100 (out of range) should have resulted in UNSUPPORTED";
344 }
345 
346 template <StandardMetadataType T>
encode(const typename StandardMetadata<T>::value_type & value)347 std::vector<uint8_t> encode(const typename StandardMetadata<T>::value_type& value) {
348     using Value = typename StandardMetadata<T>::value;
349 
350     int desiredSize = Value::encode(value, nullptr, 0);
351     EXPECT_GE(desiredSize, 0);
352     std::vector<uint8_t> buffer;
353     buffer.resize(desiredSize);
354     EXPECT_EQ(desiredSize, Value::encode(value, buffer.data(), buffer.size()));
355     return buffer;
356 }
357 
TEST(MetadataGralloc4Interop,BufferId)358 TEST(MetadataGralloc4Interop, BufferId) {
359     auto mpbuf = encode<StandardMetadataType::BUFFER_ID>(42);
360     hidl_vec<uint8_t> g4buf;
361     ASSERT_EQ(NO_ERROR, gralloc4::encodeBufferId(42, &g4buf));
362     EXPECT_EQ(g4buf, mpbuf);
363 }
364 
TEST(MetadataGralloc4Interop,Name)365 TEST(MetadataGralloc4Interop, Name) {
366     auto mpbuf = encode<StandardMetadataType::NAME>("Hello, Interop!");
367     hidl_vec<uint8_t> g4buf;
368     ASSERT_EQ(NO_ERROR, gralloc4::encodeName("Hello, Interop!", &g4buf));
369     EXPECT_EQ(g4buf, mpbuf);
370 }
371 
TEST(MetadataGralloc4Interop,Width)372 TEST(MetadataGralloc4Interop, Width) {
373     auto mpbuf = encode<StandardMetadataType::WIDTH>(128);
374     hidl_vec<uint8_t> g4buf;
375     ASSERT_EQ(NO_ERROR, gralloc4::encodeWidth(128, &g4buf));
376     EXPECT_EQ(g4buf, mpbuf);
377 }
378 
TEST(MetadataGralloc4Interop,Height)379 TEST(MetadataGralloc4Interop, Height) {
380     auto mpbuf = encode<StandardMetadataType::HEIGHT>(64);
381     hidl_vec<uint8_t> g4buf;
382     ASSERT_EQ(NO_ERROR, gralloc4::encodeHeight(64, &g4buf));
383     EXPECT_EQ(g4buf, mpbuf);
384 }
385 
TEST(MetadataGralloc4Interop,LayerCount)386 TEST(MetadataGralloc4Interop, LayerCount) {
387     auto mpbuf = encode<StandardMetadataType::LAYER_COUNT>(3);
388     hidl_vec<uint8_t> g4buf;
389     ASSERT_EQ(NO_ERROR, gralloc4::encodeLayerCount(3, &g4buf));
390     EXPECT_EQ(g4buf, mpbuf);
391 }
392 
TEST(MetadataGralloc4Interop,PixelFormatRequested)393 TEST(MetadataGralloc4Interop, PixelFormatRequested) {
394     auto mpbuf = encode<StandardMetadataType::PIXEL_FORMAT_REQUESTED>(PixelFormat::RGBX_8888);
395     hidl_vec<uint8_t> g4buf;
396     ASSERT_EQ(NO_ERROR, gralloc4::encodePixelFormatRequested(
397                                 hardware::graphics::common::V1_2::PixelFormat::RGBX_8888, &g4buf));
398     EXPECT_EQ(g4buf, mpbuf);
399 }
400 
TEST(MetadataGralloc4Interop,PixelFormatFourcc)401 TEST(MetadataGralloc4Interop, PixelFormatFourcc) {
402     auto mpbuf = encode<StandardMetadataType::PIXEL_FORMAT_FOURCC>(DRM_FORMAT_ABGR8888);
403     hidl_vec<uint8_t> g4buf;
404     ASSERT_EQ(NO_ERROR, gralloc4::encodePixelFormatFourCC(DRM_FORMAT_ABGR8888, &g4buf));
405     EXPECT_EQ(g4buf, mpbuf);
406 }
407 
TEST(MetadataGralloc4Interop,PixelFormatModifier)408 TEST(MetadataGralloc4Interop, PixelFormatModifier) {
409     auto mpbuf = encode<StandardMetadataType::PIXEL_FORMAT_MODIFIER>(123456);
410     hidl_vec<uint8_t> g4buf;
411     ASSERT_EQ(NO_ERROR, gralloc4::encodePixelFormatModifier(123456, &g4buf));
412     EXPECT_EQ(g4buf, mpbuf);
413 }
414 
TEST(MetadataGralloc4Interop,Usage)415 TEST(MetadataGralloc4Interop, Usage) {
416     auto mpbuf = encode<StandardMetadataType::USAGE>(BufferUsage::COMPOSER_OVERLAY);
417     hidl_vec<uint8_t> g4buf;
418     ASSERT_EQ(NO_ERROR,
419               gralloc4::encodeUsage(
420                       static_cast<uint64_t>(
421                               hardware::graphics::common::V1_2::BufferUsage::COMPOSER_OVERLAY),
422                       &g4buf));
423     EXPECT_EQ(g4buf, mpbuf);
424 }
425 
TEST(MetadataGralloc4Interop,AllocationSize)426 TEST(MetadataGralloc4Interop, AllocationSize) {
427     auto mpbuf = encode<StandardMetadataType::ALLOCATION_SIZE>(10200);
428     hidl_vec<uint8_t> g4buf;
429     ASSERT_EQ(NO_ERROR, gralloc4::encodeAllocationSize(10200, &g4buf));
430     EXPECT_EQ(g4buf, mpbuf);
431 }
432 
TEST(MetadataGralloc4Interop,ProtectedContent)433 TEST(MetadataGralloc4Interop, ProtectedContent) {
434     auto mpbuf = encode<StandardMetadataType::PROTECTED_CONTENT>(1);
435     hidl_vec<uint8_t> g4buf;
436     ASSERT_EQ(NO_ERROR, gralloc4::encodeProtectedContent(1, &g4buf));
437     EXPECT_EQ(g4buf, mpbuf);
438 }
439 
TEST(MetadataGralloc4Interop,Compression)440 TEST(MetadataGralloc4Interop, Compression) {
441     auto mpbuf = encode<StandardMetadataType::COMPRESSION>(
442             gralloc4::Compression_DisplayStreamCompression);
443     hidl_vec<uint8_t> g4buf;
444     ASSERT_EQ(NO_ERROR,
445               gralloc4::encodeCompression(gralloc4::Compression_DisplayStreamCompression, &g4buf));
446     EXPECT_EQ(g4buf, mpbuf);
447 }
448 
TEST(MetadataGralloc4Interop,Interlaced)449 TEST(MetadataGralloc4Interop, Interlaced) {
450     auto mpbuf = encode<StandardMetadataType::INTERLACED>(gralloc4::Interlaced_TopBottom);
451     hidl_vec<uint8_t> g4buf;
452     ASSERT_EQ(NO_ERROR, gralloc4::encodeInterlaced(gralloc4::Interlaced_TopBottom, &g4buf));
453     EXPECT_EQ(g4buf, mpbuf);
454 }
455 
TEST(MetadataGralloc4Interop,ChromeSitting)456 TEST(MetadataGralloc4Interop, ChromeSitting) {
457     auto mpbuf =
458             encode<StandardMetadataType::CHROMA_SITING>(gralloc4::ChromaSiting_SitedInterstitial);
459     hidl_vec<uint8_t> g4buf;
460     ASSERT_EQ(NO_ERROR,
461               gralloc4::encodeChromaSiting(gralloc4::ChromaSiting_SitedInterstitial, &g4buf));
462     EXPECT_EQ(g4buf, mpbuf);
463 }
464 
TEST(MetadataGralloc4Interop,PlaneLayouts)465 TEST(MetadataGralloc4Interop, PlaneLayouts) {
466     auto mpbuf = encode<StandardMetadataType::PLANE_LAYOUTS>(fakePlaneLayouts());
467     hidl_vec<uint8_t> g4buf;
468     ASSERT_EQ(NO_ERROR, gralloc4::encodePlaneLayouts(fakePlaneLayouts(), &g4buf));
469     EXPECT_EQ(g4buf, mpbuf);
470 }
471 
TEST(MetadataGralloc4Interop,Crop)472 TEST(MetadataGralloc4Interop, Crop) {
473     std::vector<Rect> cropRects{Rect{10, 11, 12, 13}, Rect{20, 21, 22, 23}};
474     auto mpbuf = encode<StandardMetadataType::CROP>(cropRects);
475     hidl_vec<uint8_t> g4buf;
476     ASSERT_EQ(NO_ERROR, gralloc4::encodeCrop(cropRects, &g4buf));
477     EXPECT_EQ(g4buf, mpbuf);
478 }
479 
TEST(MetadataGralloc4Interop,Dataspace)480 TEST(MetadataGralloc4Interop, Dataspace) {
481     auto mpbuf = encode<StandardMetadataType::DATASPACE>(Dataspace::DISPLAY_P3);
482     hidl_vec<uint8_t> g4buf;
483     ASSERT_EQ(NO_ERROR, gralloc4::encodeDataspace(Dataspace::DISPLAY_P3, &g4buf));
484     EXPECT_EQ(g4buf, mpbuf);
485 }
486 
TEST(MetadataGralloc4Interop,BlendMode)487 TEST(MetadataGralloc4Interop, BlendMode) {
488     auto mpbuf = encode<StandardMetadataType::BLEND_MODE>(BlendMode::PREMULTIPLIED);
489     hidl_vec<uint8_t> g4buf;
490     ASSERT_EQ(NO_ERROR, gralloc4::encodeBlendMode(BlendMode::PREMULTIPLIED, &g4buf));
491     EXPECT_EQ(g4buf, mpbuf);
492 }
493 
TEST(MetadataGralloc4Interop,Smpte2086)494 TEST(MetadataGralloc4Interop, Smpte2086) {
495     Smpte2086 hdrdata{XyColor{.1f, .2f}, XyColor{.3f, .4f}, XyColor{.5f, .6f},
496                       XyColor{.7f, .8f}, 452.889f,          12.335f};
497 
498     auto mpbuf = encode<StandardMetadataType::SMPTE2086>(hdrdata);
499     hidl_vec<uint8_t> g4buf;
500     ASSERT_EQ(NO_ERROR, gralloc4::encodeSmpte2086(hdrdata, &g4buf));
501     EXPECT_EQ(g4buf, mpbuf);
502 }
503 
TEST(MetadataGralloc4Interop,Cta861_3)504 TEST(MetadataGralloc4Interop, Cta861_3) {
505     Cta861_3 hdrdata{302.202f, 244.55f};
506     auto mpbuf = encode<StandardMetadataType::CTA861_3>(hdrdata);
507     hidl_vec<uint8_t> g4buf;
508     ASSERT_EQ(NO_ERROR, gralloc4::encodeCta861_3(hdrdata, &g4buf));
509     EXPECT_EQ(g4buf, mpbuf);
510 }
511 
TEST(MetadataGralloc4Interop,Smpte2094_10)512 TEST(MetadataGralloc4Interop, Smpte2094_10) {
513     auto mpbuf = encode<StandardMetadataType::SMPTE2094_10>(std::nullopt);
514     hidl_vec<uint8_t> g4buf;
515     ASSERT_EQ(NO_ERROR, gralloc4::encodeSmpte2094_10(std::nullopt, &g4buf));
516     EXPECT_EQ(g4buf, mpbuf);
517 
518     std::vector<uint8_t> hdrdata{1, 2, 3, 4, 5, 6};
519     mpbuf = encode<StandardMetadataType::SMPTE2094_10>(hdrdata);
520     ASSERT_EQ(NO_ERROR, gralloc4::encodeSmpte2094_10(hdrdata, &g4buf));
521     EXPECT_EQ(g4buf, mpbuf);
522 }
523 
TEST(MetadataGralloc4Interop,Smpte2094_40)524 TEST(MetadataGralloc4Interop, Smpte2094_40) {
525     auto mpbuf = encode<StandardMetadataType::SMPTE2094_40>(std::nullopt);
526     hidl_vec<uint8_t> g4buf;
527     ASSERT_EQ(NO_ERROR, gralloc4::encodeSmpte2094_40(std::nullopt, &g4buf));
528     EXPECT_EQ(g4buf, mpbuf);
529 
530     std::vector<uint8_t> hdrdata{1, 2, 3, 4, 5, 6};
531     mpbuf = encode<StandardMetadataType::SMPTE2094_40>(hdrdata);
532     ASSERT_EQ(NO_ERROR, gralloc4::encodeSmpte2094_40(hdrdata, &g4buf));
533     EXPECT_EQ(g4buf, mpbuf);
534 }
535