1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 
18 #include "metadata_helper.h"
19 #include "surface_buffer_impl.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::HDI::Display::Graphic::Common::V1_0;
23 
24 namespace OHOS {
25 class MetadataManagerTest : public testing::Test {
26 public:
27     static void SetUpTestCase();
TearDownTestCase()28     static void TearDownTestCase() {}
29 
30     static inline BufferRequestConfig requestConfig = {
31         .width = 0x100,
32         .height = 0x100,
33         .strideAlignment = 0x8,
34         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
35         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
36         .timeout = 0,
37         .colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB,
38     };
39     static inline sptr<SurfaceBuffer> buffer_ = nullptr;
40     static inline sptr<SurfaceBuffer> nullBuffer_ = nullptr;
41 };
42 
SetUpTestCase()43 void MetadataManagerTest::SetUpTestCase()
44 {
45     buffer_ = new SurfaceBufferImpl(0);
46     auto ret = buffer_->Alloc(requestConfig);
47     ASSERT_EQ(ret, GSERROR_OK);
48 }
49 
50 /*
51 * Function: MetadataManagerTest
52 * Type: Function
53 * Rank: Important(2)
54 * EnvConditions: N/A
55 * CaseDescription: test ConvertMetadataToVec
56 */
57 HWTEST_F(MetadataManagerTest, ConvertMetadataToVecTest, Function | SmallTest | Level2)
58 {
59     uint32_t metadata = 0;
60     std::vector<uint8_t> vec;
61     ASSERT_EQ(MetadataHelper::ConvertMetadataToVec(metadata, vec), GSERROR_OK);
62 
63     ASSERT_EQ(vec.size(), 4);
64     for (uint32_t i = 0; i < vec.size(); ++i) {
65         ASSERT_EQ(vec[i], 0);
66     }
67 }
68 
69 /*
70 * Function: MetadataManagerTest
71 * Type: Function
72 * Rank: Important(2)
73 * EnvConditions: N/A
74 * CaseDescription: test ConvertVecToMetadata
75 */
76 HWTEST_F(MetadataManagerTest, ConvertVecToMetadataTest, Function | SmallTest | Level2)
77 {
78     std::vector<uint8_t> vec;
79     uint32_t metadata = 1;
80     ASSERT_EQ(MetadataHelper::ConvertVecToMetadata(vec, metadata), GSERROR_NOT_SUPPORT);
81 
82     vec.assign(4, 0);
83     ASSERT_EQ(MetadataHelper::ConvertVecToMetadata(vec, metadata), GSERROR_OK);
84     ASSERT_EQ(metadata, 0);
85 }
86 
87 /*
88 * Function: MetadataManagerTest
89 * Type: Function
90 * Rank: Important(2)
91 * EnvConditions: N/A
92 * CaseDescription: test ConvertColorSpaceTypeToInfo
93 */
94 HWTEST_F(MetadataManagerTest, ConvertColorSpaceTypeToInfoTest, Function | SmallTest | Level2)
95 {
96     CM_ColorSpaceInfo colorSpaceInfo;
97     ASSERT_EQ(MetadataHelper::ConvertColorSpaceTypeToInfo(CM_SRGB_FULL, colorSpaceInfo), GSERROR_OK);
98 
99     ASSERT_EQ(colorSpaceInfo.primaries, COLORPRIMARIES_SRGB);
100     ASSERT_EQ(colorSpaceInfo.transfunc, TRANSFUNC_SRGB);
101     ASSERT_EQ(colorSpaceInfo.matrix, MATRIX_BT601_N);
102     ASSERT_EQ(colorSpaceInfo.range, RANGE_FULL);
103 }
104 
105 /*
106 * Function: MetadataManagerTest
107 * Type: Function
108 * Rank: Important(2)
109 * EnvConditions: N/A
110 * CaseDescription: test ConvertColorSpaceInfoToType
111 */
112 HWTEST_F(MetadataManagerTest, ConvertColorSpaceInfoToTypeTest, Function | SmallTest | Level2)
113 {
114     CM_ColorSpaceInfo colorSpaceInfo = {
115         .primaries = COLORPRIMARIES_SRGB,
116         .transfunc = TRANSFUNC_SRGB,
117         .matrix = MATRIX_BT601_N,
118         .range = RANGE_FULL,
119     };
120     CM_ColorSpaceType colorSpaceType;
121     ASSERT_EQ(MetadataHelper::ConvertColorSpaceInfoToType(colorSpaceInfo, colorSpaceType), GSERROR_OK);
122 
123     ASSERT_EQ(colorSpaceType, CM_SRGB_FULL);
124 }
125 
126 /*
127 * Function: MetadataManagerTest
128 * Type: Function
129 * Rank: Important(2)
130 * EnvConditions: N/A
131 * CaseDescription: test SetColorSpaceInfo and GetColorSpaceInfo
132 */
133 HWTEST_F(MetadataManagerTest, ColorSpaceInfoTest, Function | SmallTest | Level2)
134 {
135     CM_ColorSpaceInfo infoSet = {
136         .primaries = COLORPRIMARIES_SRGB,
137         .transfunc = TRANSFUNC_SRGB,
138         .matrix = MATRIX_BT709,
139         .range = RANGE_FULL,
140     };
141 
142     auto retSet = MetadataHelper::SetColorSpaceInfo(buffer_, infoSet);
143     ASSERT_TRUE(retSet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
144 
145     CM_ColorSpaceInfo infoGet;
146     auto retGet = MetadataHelper::GetColorSpaceInfo(buffer_, infoGet);
147     ASSERT_TRUE(retGet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
148 
149     if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
150         ASSERT_EQ(infoSet.primaries, infoGet.primaries);
151         ASSERT_EQ(infoSet.transfunc, infoGet.transfunc);
152         ASSERT_EQ(infoSet.matrix, infoGet.matrix);
153         ASSERT_EQ(infoSet.range, infoGet.range);
154     }
155 
156     ASSERT_EQ(MetadataHelper::SetColorSpaceInfo(nullBuffer_, infoSet), GSERROR_NO_BUFFER);
157     ASSERT_EQ(MetadataHelper::GetColorSpaceInfo(nullBuffer_, infoGet), GSERROR_NO_BUFFER);
158 }
159 
160 /*
161 * Function: MetadataManagerTest
162 * Type: Function
163 * Rank: Important(2)
164 * EnvConditions: N/A
165 * CaseDescription: test SetColorSpaceType and GetColorSpaceType
166 */
167 HWTEST_F(MetadataManagerTest, ColorSpaceTypeTest, Function | SmallTest | Level2)
168 {
169     auto retSet = MetadataHelper::SetColorSpaceType(buffer_, CM_SRGB_FULL);
170     ASSERT_TRUE(retSet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
171 
172     CM_ColorSpaceType colorSpaceType;
173     auto retGet = MetadataHelper::GetColorSpaceType(buffer_, colorSpaceType);
174     ASSERT_TRUE(retGet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
175 
176     if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
177         ASSERT_EQ(colorSpaceType, CM_SRGB_FULL);
178     }
179 
180     ASSERT_EQ(MetadataHelper::SetColorSpaceType(nullBuffer_, CM_SRGB_FULL), GSERROR_NO_BUFFER);
181     ASSERT_EQ(MetadataHelper::GetColorSpaceType(nullBuffer_, colorSpaceType), GSERROR_NO_BUFFER);
182 }
183 
184 /*
185 * Function: MetadataManagerTest
186 * Type: Function
187 * Rank: Important(2)
188 * EnvConditions: N/A
189 * CaseDescription: test SetHDRMetadataType and GetHDRMetadataType
190 */
191 HWTEST_F(MetadataManagerTest, HDRMetadataTypeTest, Function | SmallTest | Level2)
192 {
193     auto retSet = MetadataHelper::SetHDRMetadataType(buffer_, CM_VIDEO_HDR_VIVID);
194     ASSERT_TRUE(retSet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
195 
196     CM_HDR_Metadata_Type hdrMetadataType;
197     auto retGet = MetadataHelper::GetHDRMetadataType(buffer_, hdrMetadataType);
198     ASSERT_TRUE(retGet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
199 
200     if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
201         ASSERT_EQ(hdrMetadataType, CM_VIDEO_HDR_VIVID);
202     }
203 
204     ASSERT_EQ(MetadataHelper::SetHDRMetadataType(nullBuffer_, CM_VIDEO_HDR_VIVID), GSERROR_NO_BUFFER);
205     ASSERT_EQ(MetadataHelper::GetHDRMetadataType(nullBuffer_, hdrMetadataType), GSERROR_NO_BUFFER);
206 }
207 
208 /*
209 * Function: MetadataManagerTest
210 * Type: Function
211 * Rank: Important(2)
212 * EnvConditions: N/A
213 * CaseDescription: test SetHDRStaticMetadata and GetHDRStaticMetadata
214 */
215 HWTEST_F(MetadataManagerTest, HDRStaticMetadataTest, Function | SmallTest | Level2)
216 {
217     HdrStaticMetadata metadataSet = {
218         .smpte2086 = {
219             .displayPrimaryRed = {0.1f, 0.1f},
220             .displayPrimaryGreen = {0.2f, 0.2f},
221             .displayPrimaryBlue = {0.3f, 0.3f},
222             .whitePoint = {0.4f, 0.4f},
223             .maxLuminance = 1000.0f,
224             .minLuminance = 0.1f,
225         },
226         .cta861 = {
227             .maxContentLightLevel = 500.0f,
228             .maxFrameAverageLightLevel = 300.0f,
229         },
230     };
231 
232     auto retSet = MetadataHelper::SetHDRStaticMetadata(buffer_, metadataSet);
233     ASSERT_TRUE(retSet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
234 
235     HdrStaticMetadata metadataGet;
236     auto retGet = MetadataHelper::GetHDRStaticMetadata(buffer_, metadataGet);
237     ASSERT_TRUE(retGet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
238 
239     if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
240         ASSERT_EQ(metadataSet.smpte2086.displayPrimaryRed.x, metadataGet.smpte2086.displayPrimaryRed.x);
241         ASSERT_EQ(metadataSet.smpte2086.displayPrimaryRed.y, metadataGet.smpte2086.displayPrimaryRed.y);
242         ASSERT_EQ(metadataSet.smpte2086.displayPrimaryGreen.x, metadataGet.smpte2086.displayPrimaryGreen.x);
243         ASSERT_EQ(metadataSet.smpte2086.displayPrimaryGreen.y, metadataGet.smpte2086.displayPrimaryGreen.y);
244         ASSERT_EQ(metadataSet.smpte2086.displayPrimaryBlue.x, metadataGet.smpte2086.displayPrimaryBlue.x);
245         ASSERT_EQ(metadataSet.smpte2086.displayPrimaryBlue.y, metadataGet.smpte2086.displayPrimaryBlue.y);
246         ASSERT_EQ(metadataSet.smpte2086.whitePoint.x, metadataGet.smpte2086.whitePoint.x);
247         ASSERT_EQ(metadataSet.smpte2086.whitePoint.y, metadataGet.smpte2086.whitePoint.y);
248         ASSERT_EQ(metadataSet.smpte2086.maxLuminance, metadataGet.smpte2086.maxLuminance);
249         ASSERT_EQ(metadataSet.smpte2086.minLuminance, metadataGet.smpte2086.minLuminance);
250         ASSERT_EQ(metadataSet.cta861.maxContentLightLevel, metadataGet.cta861.maxContentLightLevel);
251         ASSERT_EQ(metadataSet.cta861.maxFrameAverageLightLevel, metadataGet.cta861.maxFrameAverageLightLevel);
252     }
253 
254     ASSERT_EQ(MetadataHelper::SetHDRStaticMetadata(nullBuffer_, metadataSet), GSERROR_NO_BUFFER);
255     ASSERT_EQ(MetadataHelper::GetHDRStaticMetadata(nullBuffer_, metadataGet), GSERROR_NO_BUFFER);
256 }
257 
258 /*
259 * Function: MetadataManagerTest
260 * Type: Function
261 * Rank: Important(2)
262 * EnvConditions: N/A
263 * CaseDescription: test SetHDRDynamicMetadata and GetHDRDynamicMetadata
264 */
265 HWTEST_F(MetadataManagerTest, HDRDynamicMetadataTest, Function | SmallTest | Level2)
266 {
267     std::vector<uint8_t> metadataSet{1, 18, 119, 33, 196, 253, 112, 171, 74, 230, 99, 23, 0, 244, 82, 138, 13, 158, 100,
268         41, 50, 189, 111, 144, 3, 153, 75, 210, 243, 237, 19, 12, 128};
269 
270     auto retSet = MetadataHelper::SetHDRDynamicMetadata(buffer_, metadataSet);
271     ASSERT_TRUE(retSet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
272 
273     std::vector<uint8_t> metadataGet;
274     auto retGet = MetadataHelper::GetHDRDynamicMetadata(buffer_, metadataGet);
275     ASSERT_TRUE(retGet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
276 
277     if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
278         ASSERT_EQ(metadataSet.size(), metadataGet.size());
279         for (uint32_t i = 0; i < metadataSet.size(); i++) {
280             ASSERT_EQ(metadataSet[i], metadataGet[i]);
281         }
282     }
283 
284     ASSERT_EQ(MetadataHelper::SetHDRDynamicMetadata(nullBuffer_, metadataSet), GSERROR_NO_BUFFER);
285     ASSERT_EQ(MetadataHelper::GetHDRDynamicMetadata(nullBuffer_, metadataGet), GSERROR_NO_BUFFER);
286 }
287 
288 /*
289 * Function: MetadataManagerTest
290 * Type: Function
291 * Rank: Important(2)
292 * EnvConditions: N/A
293 * CaseDescription: test SetHDRStaticMetadata and GetHDRStaticMetadata
294 */
295 HWTEST_F(MetadataManagerTest, HDRStaticMetadataVecTest, Function | SmallTest | Level2)
296 {
297     HdrStaticMetadata metadata = {
298         .smpte2086 = {
299             .displayPrimaryRed = {0.1f, 0.1f},
300             .displayPrimaryGreen = {0.2f, 0.2f},
301             .displayPrimaryBlue = {0.3f, 0.3f},
302             .whitePoint = {0.4f, 0.4f},
303             .maxLuminance = 1000.0f,
304             .minLuminance = 0.1f,
305         },
306         .cta861 = {
307             .maxContentLightLevel = 500.0f,
308             .maxFrameAverageLightLevel = 300.0f,
309         },
310     };
311 
312     std::vector<uint8_t> metadataSet;
313     ASSERT_EQ(MetadataHelper::ConvertMetadataToVec(metadata, metadataSet), GSERROR_OK);
314 
315     auto retSet = MetadataHelper::SetHDRStaticMetadata(buffer_, metadataSet);
316     ASSERT_TRUE(retSet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
317 
318     std::vector<uint8_t> metadataGet;
319     auto retGet = MetadataHelper::GetHDRStaticMetadata(buffer_, metadataGet);
320     ASSERT_TRUE(retGet == GSERROR_OK || retSet == GSERROR_HDI_ERROR);
321 
322     if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
323         ASSERT_EQ(metadataSet.size(), metadataGet.size());
324         for (uint32_t i = 0; i < metadataSet.size(); i++) {
325             ASSERT_EQ(metadataSet[i], metadataGet[i]);
326         }
327     }
328 
329     ASSERT_EQ(MetadataHelper::SetHDRStaticMetadata(nullBuffer_, metadataSet), GSERROR_NO_BUFFER);
330     ASSERT_EQ(MetadataHelper::GetHDRStaticMetadata(nullBuffer_, metadataGet), GSERROR_NO_BUFFER);
331 }
332 }