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 MetadataHelperTest : 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 };
41
SetUpTestCase()42 void MetadataHelperTest::SetUpTestCase()
43 {
44 buffer_ = new SurfaceBufferImpl(0);
45 auto ret = buffer_->Alloc(requestConfig);
46 ASSERT_EQ(ret, GSERROR_OK);
47 }
48
49 /*
50 * Function: MetadataHelperTest
51 * Type: Function
52 * Rank: Important(2)
53 * EnvConditions: N/A
54 * CaseDescription: test ConvertColorSpaceTypeToInfo
55 */
56 HWTEST_F(MetadataHelperTest, ConvertColorSpaceTypeToInfoTest, Function | SmallTest | Level2)
57 {
58 CM_ColorSpaceInfo colorSpaceInfo;
59 ASSERT_EQ(MetadataHelper::ConvertColorSpaceTypeToInfo(CM_SRGB_FULL, colorSpaceInfo), GSERROR_OK);
60
61 ASSERT_EQ(colorSpaceInfo.primaries, COLORPRIMARIES_SRGB);
62 ASSERT_EQ(colorSpaceInfo.transfunc, TRANSFUNC_SRGB);
63 ASSERT_EQ(colorSpaceInfo.matrix, MATRIX_BT601_N);
64 ASSERT_EQ(colorSpaceInfo.range, RANGE_FULL);
65 }
66
67 /*
68 * Function: MetadataHelperTest
69 * Type: Function
70 * Rank: Important(2)
71 * EnvConditions: N/A
72 * CaseDescription: test ConvertColorSpaceInfoToType
73 */
74 HWTEST_F(MetadataHelperTest, ConvertColorSpaceInfoToTypeTest, Function | SmallTest | Level2)
75 {
76 CM_ColorSpaceInfo colorSpaceInfo = {
77 .primaries = COLORPRIMARIES_SRGB,
78 .transfunc = TRANSFUNC_SRGB,
79 .matrix = MATRIX_BT601_N,
80 .range = RANGE_FULL,
81 };
82 CM_ColorSpaceType colorSpaceType;
83 ASSERT_EQ(MetadataHelper::ConvertColorSpaceInfoToType(colorSpaceInfo, colorSpaceType), GSERROR_OK);
84
85 ASSERT_EQ(colorSpaceType, CM_SRGB_FULL);
86 }
87
88 /*
89 * Function: MetadataHelperTest
90 * Type: Function
91 * Rank: Important(2)
92 * EnvConditions: N/A
93 * CaseDescription: test SetColorSpaceInfo and GetColorSpaceInfo
94 */
95 HWTEST_F(MetadataHelperTest, ColorSpaceInfoTest, Function | SmallTest | Level2)
96 {
97 CM_ColorSpaceInfo infoSet = {
98 .primaries = COLORPRIMARIES_SRGB,
99 .transfunc = TRANSFUNC_SRGB,
100 .matrix = MATRIX_BT709,
101 .range = RANGE_FULL,
102 };
103
104 auto retSet = MetadataHelper::SetColorSpaceInfo(buffer_, infoSet);
105 ASSERT_TRUE(retSet == GSERROR_OK || GSErrorStr(retSet) == "<500 api call failed>with low error <Not supported>");
106
107 CM_ColorSpaceInfo infoGet;
108 auto retGet = MetadataHelper::GetColorSpaceInfo(buffer_, infoGet);
109 ASSERT_TRUE(retGet == GSERROR_OK || GSErrorStr(retGet) == "<500 api call failed>with low error <Not supported>");
110
111 if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
112 ASSERT_EQ(infoSet.primaries, infoGet.primaries);
113 ASSERT_EQ(infoSet.transfunc, infoGet.transfunc);
114 ASSERT_EQ(infoSet.matrix, infoGet.matrix);
115 ASSERT_EQ(infoSet.range, infoGet.range);
116 }
117 }
118
119 /*
120 * Function: MetadataHelperTest
121 * Type: Function
122 * Rank: Important(2)
123 * EnvConditions: N/A
124 * CaseDescription: test SetColorSpaceType and GetColorSpaceType
125 */
126 HWTEST_F(MetadataHelperTest, ColorSpaceTypeTest, Function | SmallTest | Level2)
127 {
128 auto retSet = MetadataHelper::SetColorSpaceType(buffer_, CM_SRGB_FULL);
129 ASSERT_TRUE(retSet == GSERROR_OK || GSErrorStr(retSet) == "<500 api call failed>with low error <Not supported>");
130
131 CM_ColorSpaceType colorSpaceType;
132 auto retGet = MetadataHelper::GetColorSpaceType(buffer_, colorSpaceType);
133 ASSERT_TRUE(retGet == GSERROR_OK || GSErrorStr(retGet) == "<500 api call failed>with low error <Not supported>");
134
135 if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
136 ASSERT_EQ(colorSpaceType, CM_SRGB_FULL);
137 }
138 }
139
140 /*
141 * Function: MetadataHelperTest
142 * Type: Function
143 * Rank: Important(2)
144 * EnvConditions: N/A
145 * CaseDescription: test SetHDRMetadataType and GetHDRMetadataType
146 */
147 HWTEST_F(MetadataHelperTest, HDRMetadataTypeTest, Function | SmallTest | Level2)
148 {
149 auto retSet = MetadataHelper::SetHDRMetadataType(buffer_, CM_VIDEO_HDR_VIVID);
150 ASSERT_TRUE(retSet == GSERROR_OK || GSErrorStr(retSet) == "<500 api call failed>with low error <Not supported>");
151
152 CM_HDR_Metadata_Type hdrMetadataType;
153 auto retGet = MetadataHelper::GetHDRMetadataType(buffer_, hdrMetadataType);
154 ASSERT_TRUE(retGet == GSERROR_OK || GSErrorStr(retGet) == "<500 api call failed>with low error <Not supported>");
155
156 if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
157 ASSERT_EQ(hdrMetadataType, CM_VIDEO_HDR_VIVID);
158 }
159 }
160
161 /*
162 * Function: MetadataHelperTest
163 * Type: Function
164 * Rank: Important(2)
165 * EnvConditions: N/A
166 * CaseDescription: test SetHDRStaticMetadata and GetHDRStaticMetadata
167 */
168 HWTEST_F(MetadataHelperTest, HDRStaticMetadataTest, Function | SmallTest | Level2)
169 {
170 HdrStaticMetadata metadataSet = {
171 .smpte2086 = {
172 .displayPrimaryRed = {0.1f, 0.1f},
173 .displayPrimaryGreen = {0.2f, 0.2f},
174 .displayPrimaryBlue = {0.3f, 0.3f},
175 .whitePoint = {0.4f, 0.4f},
176 .maxLuminance = 1000.0f,
177 .minLuminance = 0.1f,
178 },
179 .cta861 = {
180 .maxContentLightLevel = 500.0f,
181 .maxFrameAverageLightLevel = 300.0f,
182 },
183 };
184
185 auto retSet = MetadataHelper::SetHDRStaticMetadata(buffer_, metadataSet);
186 ASSERT_TRUE(retSet == GSERROR_OK || GSErrorStr(retSet) == "<500 api call failed>with low error <Not supported>");
187
188 HdrStaticMetadata metadataGet;
189 auto retGet = MetadataHelper::GetHDRStaticMetadata(buffer_, metadataGet);
190 ASSERT_TRUE(retGet == GSERROR_OK || GSErrorStr(retGet) == "<500 api call failed>with low error <Not supported>");
191
192 if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
193 ASSERT_EQ(metadataSet.smpte2086.displayPrimaryRed.x, metadataGet.smpte2086.displayPrimaryRed.x);
194 ASSERT_EQ(metadataSet.smpte2086.displayPrimaryRed.y, metadataGet.smpte2086.displayPrimaryRed.y);
195 ASSERT_EQ(metadataSet.smpte2086.displayPrimaryGreen.x, metadataGet.smpte2086.displayPrimaryGreen.x);
196 ASSERT_EQ(metadataSet.smpte2086.displayPrimaryGreen.y, metadataGet.smpte2086.displayPrimaryGreen.y);
197 ASSERT_EQ(metadataSet.smpte2086.displayPrimaryBlue.x, metadataGet.smpte2086.displayPrimaryBlue.x);
198 ASSERT_EQ(metadataSet.smpte2086.displayPrimaryBlue.y, metadataGet.smpte2086.displayPrimaryBlue.y);
199 ASSERT_EQ(metadataSet.smpte2086.whitePoint.x, metadataGet.smpte2086.whitePoint.x);
200 ASSERT_EQ(metadataSet.smpte2086.whitePoint.y, metadataGet.smpte2086.whitePoint.y);
201 ASSERT_EQ(metadataSet.smpte2086.maxLuminance, metadataGet.smpte2086.maxLuminance);
202 ASSERT_EQ(metadataSet.smpte2086.minLuminance, metadataGet.smpte2086.minLuminance);
203 ASSERT_EQ(metadataSet.cta861.maxContentLightLevel, metadataGet.cta861.maxContentLightLevel);
204 ASSERT_EQ(metadataSet.cta861.maxFrameAverageLightLevel, metadataGet.cta861.maxFrameAverageLightLevel);
205 }
206 }
207
208 /*
209 * Function: MetadataHelperTest
210 * Type: Function
211 * Rank: Important(2)
212 * EnvConditions: N/A
213 * CaseDescription: test SetHDRDynamicMetadata and GetHDRDynamicMetadata
214 */
215 HWTEST_F(MetadataHelperTest, HDRDynamicMetadataTest, Function | SmallTest | Level2)
216 {
217 std::vector<uint8_t> metadataSet{1, 18, 119, 33, 196, 253, 112, 171, 74, 230, 99, 23, 0, 244, 82, 138, 13, 158, 100,
218 41, 50, 189, 111, 144, 3, 153, 75, 210, 243, 237, 19, 12, 128};
219
220 auto retSet = MetadataHelper::SetHDRDynamicMetadata(buffer_, metadataSet);
221 ASSERT_TRUE(retSet == GSERROR_OK || GSErrorStr(retSet) == "<500 api call failed>with low error <Not supported>");
222
223 std::vector<uint8_t> metadataGet;
224 auto retGet = MetadataHelper::GetHDRDynamicMetadata(buffer_, metadataGet);
225 ASSERT_TRUE(retGet == GSERROR_OK || GSErrorStr(retGet) == "<500 api call failed>with low error <Not supported>");
226
227 if (retSet == GSERROR_OK && retGet == GSERROR_OK) {
228 ASSERT_EQ(metadataSet.size(), metadataGet.size());
229 for (uint32_t i = 0; i < metadataSet.size(); i++) {
230 ASSERT_EQ(metadataSet[i], metadataGet[i]);
231 }
232 }
233 }
234 }