1 /*
2 * Copyright (c) 2021-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 #include <securec.h>
16 #include <gtest/gtest.h>
17 #include <surface.h>
18 #include <surface_buffer_impl.h>
19 #include <buffer_utils.h>
20 #include <metadata_helper.h>
21 #include "v1_1/buffer_handle_meta_key_type.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS::Rosen {
27 class SurfaceBufferImplTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31
32 static inline BufferRequestConfig requestConfig = {
33 .width = 0x100,
34 .height = 0x100,
35 .strideAlignment = 0x8,
36 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
37 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
38 .timeout = 0,
39 .colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3,
40 };
41 static inline sptr<SurfaceBuffer> buffer = nullptr;
42 static inline int32_t val32 = 0;
43 static inline int64_t val64 = 0;
44 };
45
SetUpTestCase()46 void SurfaceBufferImplTest::SetUpTestCase()
47 {
48 buffer = nullptr;
49 val32 = 0;
50 val64 = 0;
51 }
52
TearDownTestCase()53 void SurfaceBufferImplTest::TearDownTestCase()
54 {
55 buffer = nullptr;
56 }
57
58 /*
59 * Function: GetSeqNum
60 * Type: Function
61 * Rank: Important(2)
62 * EnvConditions: N/A
63 * CaseDescription: 1. new SurfaceBufferImpl and GetSeqNum
64 * 2. new SurfaceBufferImpl again and check GetSeqNum = oldSeq + 1
65 * 3. set and verify the value of parameter isConsumerAttachBufferFlag_ is false
66 * 4. set and verify the value of parameter isConsumerAttachBufferFlag_ is true
67 */
68 HWTEST_F(SurfaceBufferImplTest, NewSeqIncrease001, Function | MediumTest | Level2)
69 {
70 buffer = new SurfaceBufferImpl();
71 int oldSeq = buffer->GetSeqNum();
72
73 buffer = new SurfaceBufferImpl();
74 ASSERT_EQ(oldSeq + 1, buffer->GetSeqNum());
75
76 buffer->SetConsumerAttachBufferFlag(false);
77 ASSERT_EQ(buffer->GetConsumerAttachBufferFlag(), false);
78 buffer->SetConsumerAttachBufferFlag(true);
79 ASSERT_EQ(buffer->GetConsumerAttachBufferFlag(), true);
80 }
81
82 /*
83 * Function: check buffer state
84 * Type: Function
85 * Rank: Important(2)
86 * EnvConditions: N/A
87 * CaseDescription: 1. check buffer state, such as bufferhandle, virAddr, fileDescriptor and size
88 */
89 HWTEST_F(SurfaceBufferImplTest, State001, Function | MediumTest | Level2)
90 {
91 ASSERT_EQ(buffer->GetBufferHandle(), nullptr);
92 ASSERT_EQ(buffer->GetVirAddr(), nullptr);
93 ASSERT_EQ(buffer->GetFileDescriptor(), -1);
94 ASSERT_EQ(buffer->GetSize(), 0u);
95 }
96
97 /*
98 * Function: check buffer state
99 * Type: Function
100 * Rank: Important(2)
101 * EnvConditions: N/A
102 * CaseDescription: 1. call GetBufferHandle and Alloc
103 * 2. check buffer state, such as bufferhandle, virAddr and size
104 * 3. call Free
105 * 4. check ret
106 */
107 HWTEST_F(SurfaceBufferImplTest, State002, Function | MediumTest | Level2)
108 {
109 ASSERT_EQ(buffer->GetBufferHandle(), nullptr);
110 ASSERT_EQ(buffer->GetPhyAddr(), 0);
111 ASSERT_EQ(buffer->GetStride(), -1);
112 vector<uint32_t> keys;
113 ASSERT_EQ(buffer->ListMetadataKeys(keys), GSERROR_NOT_INIT);
114 ASSERT_EQ(buffer->EraseMetadataKey(1), GSERROR_NOT_INIT);
115 GSError ret = buffer->Alloc(requestConfig);
116 ASSERT_EQ(ret, OHOS::GSERROR_OK);
117
118 ASSERT_NE(buffer->GetBufferHandle(), nullptr);
119 ASSERT_NE(buffer->GetVirAddr(), nullptr);
120 ASSERT_NE(buffer->GetSize(), 0u);
121 ASSERT_EQ(buffer->GetFormat(), GRAPHIC_PIXEL_FMT_RGBA_8888);
122 ASSERT_EQ(buffer->GetUsage(), BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA);
123 ASSERT_EQ(buffer->GetSurfaceBufferColorGamut(), GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3);
124
125 buffer->SetBufferHandle(nullptr);
126 }
127
128 /*
129 * Function: parcel
130 * Type: Function
131 * Rank: Important(2)
132 * EnvConditions: N/A
133 * CaseDescription: 1. new SurfaceBufferImpl and Alloc
134 * 2. call Set data interface
135 * 3. call WriteSurfaceBufferImpl and ReadSurfaceBufferImpl
136 * 4. call Get data interface
137 * 5. check ret
138 */
139 HWTEST_F(SurfaceBufferImplTest, Parcel001, Function | MediumTest | Level2)
140 {
141 sptr<SurfaceBuffer> sbi = new SurfaceBufferImpl(0);
142 auto sret = sbi->Alloc(requestConfig);
143 ASSERT_EQ(sret, OHOS::GSERROR_OK);
144
145 MessageParcel parcel;
146 WriteSurfaceBufferImpl(parcel, sbi->GetSeqNum(), sbi);
147
148 sptr<SurfaceBuffer> buffer = nullptr;
149 uint32_t seq;
150 ReadSurfaceBufferImpl(parcel, seq, buffer);
151 ASSERT_NE(buffer, nullptr);
152 }
153
154 /*
155 * Function: Create
156 * Type: Function
157 * Rank: Important(2)
158 * EnvConditions: N/A
159 * CaseDescription: 1. Call SurfaceBuffer::Create()
160 * 2. check ret
161 */
162 HWTEST_F(SurfaceBufferImplTest, Create001, Function | MediumTest | Level2)
163 {
164 sptr<SurfaceBuffer> buffer = SurfaceBuffer::Create();
165 ASSERT_NE(buffer, nullptr);
166 }
167
168 /*
169 * Function: Set/Get/List/Erase Metadata
170 * Type: Function
171 * Rank: Important(2)
172 * EnvConditions: N/A
173 * CaseDescription: 1. new SurfaceBufferImpl and Alloc
174 * 2. call Set Metadata interface
175 * 3. call Get Metadata interface
176 * 4. check ret
177 * 5. call List Metadata keys interface
178 * 6. check ret
179 * 7. call Erase Metadata key interface
180 * 8. call List Metadata keys interface again
181 * 9. check ret
182 */
183 HWTEST_F(SurfaceBufferImplTest, Metadata001, Function | MediumTest | Level2)
184 {
185 using namespace HDI::Display::Graphic::Common::V1_0;
186
187 sptr<SurfaceBuffer> sbi = new SurfaceBufferImpl(0);
188 auto sret = sbi->Alloc(requestConfig);
189 ASSERT_EQ(sret, OHOS::GSERROR_OK);
190
191 uint32_t metadataKey = 2;
192
193 uint32_t setMetadata = 4260097;
194 std::vector<uint8_t> setData;
195 ASSERT_EQ(MetadataHelper::ConvertMetadataToVec(setMetadata, setData), OHOS::GSERROR_OK);
196 ASSERT_EQ(sbi->SetMetadata(0, setData), GSERROR_INVALID_ARGUMENTS);
197 ASSERT_EQ(sbi->SetMetadata(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END, setData), GSERROR_INVALID_ARGUMENTS);
198 sret = sbi->SetMetadata(metadataKey, setData);
199 ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
200
201 std::vector<uint8_t> getData;
202 ASSERT_EQ(sbi->GetMetadata(0, getData), GSERROR_INVALID_ARGUMENTS);
203 ASSERT_EQ(sbi->GetMetadata(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END, getData), GSERROR_INVALID_ARGUMENTS);
204 sret = sbi->GetMetadata(metadataKey, getData);
205 ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
206
207 if (sret == OHOS::GSERROR_OK) {
208 uint32_t getMetadata;
209 ASSERT_EQ(MetadataHelper::ConvertVecToMetadata(getData, getMetadata), OHOS::GSERROR_OK);
210 ASSERT_EQ(setMetadata, getMetadata);
211 }
212
213 std::vector<uint32_t> keys;
214
215 sret = sbi->ListMetadataKeys(keys);
216 ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
217 if (sret == OHOS::GSERROR_OK) {
218 ASSERT_EQ(sret, OHOS::GSERROR_OK);
219 ASSERT_EQ(keys.size(), 1);
220 ASSERT_EQ(keys[0], metadataKey);
221 }
222
223 ASSERT_EQ(sbi->EraseMetadataKey(0), GSERROR_INVALID_ARGUMENTS);
224 ASSERT_EQ(sbi->EraseMetadataKey(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END), GSERROR_INVALID_ARGUMENTS);
225 sret = sbi->EraseMetadataKey(metadataKey);
226 ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
227
228 sret = sbi->ListMetadataKeys(keys);
229 ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
230 if (sret == OHOS::GSERROR_OK) {
231 ASSERT_EQ(keys.size(), 0);
232 }
233 }
234
235 /*
236 * Function: SetMetadata
237 * Type: Function
238 * Rank: Important(2)
239 * EnvConditions: N/A
240 * CaseDescription: 1. new SurfaceBufferImpl and Alloc
241 * 2. call Set Metadata interface with disbale cache
242 3. check ret and metaDataCache_ should be empty
243 */
244 HWTEST_F(SurfaceBufferImplTest, Metadata002, Function | MediumTest | Level2)
245 {
246 using namespace HDI::Display::Graphic::Common::V1_0;
247
248 sptr<SurfaceBufferImpl> sbi = new SurfaceBufferImpl(0);
249 auto sret = sbi->Alloc(requestConfig);
250 ASSERT_EQ(sret, OHOS::GSERROR_OK);
251
252 uint32_t metadataKey = 2;
253
254 uint32_t setMetadata = 4260097;
255 std::vector<uint8_t> setData;
256 ASSERT_EQ(MetadataHelper::ConvertMetadataToVec(setMetadata, setData), OHOS::GSERROR_OK);
257 ASSERT_EQ(sbi->SetMetadata(0, setData, false), GSERROR_INVALID_ARGUMENTS);
258 ASSERT_EQ(sbi->SetMetadata(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END, setData, false),
259 GSERROR_INVALID_ARGUMENTS);
260 sret = sbi->SetMetadata(metadataKey, setData, false);
261 ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
262 if (sret == OHOS::GSERROR_OK) {
263 ASSERT_TRUE(sbi->metaDataCache_.empty());
264 }
265 }
266
267 /*
268 * Function: SetMetadata
269 * Type: Function
270 * Rank: Important(2)
271 * EnvConditions: N/A
272 * CaseDescription: 1. new SurfaceBufferImpl and Alloc
273 * 2. call Set Metadata interface with enable cache
274 3. check ret and metaDataCache_ size be 1
275 */
276 HWTEST_F(SurfaceBufferImplTest, Metadata003, Function | MediumTest | Level2)
277 {
278 using namespace HDI::Display::Graphic::Common::V1_0;
279
280 sptr<SurfaceBufferImpl> sbi = new SurfaceBufferImpl(0);
281 auto sret = sbi->Alloc(requestConfig);
282 ASSERT_EQ(sret, OHOS::GSERROR_OK);
283
284 uint32_t metadataKey = 2;
285
286 uint32_t setMetadata = 4260097;
287 std::vector<uint8_t> setData;
288 ASSERT_EQ(MetadataHelper::ConvertMetadataToVec(setMetadata, setData), OHOS::GSERROR_OK);
289 ASSERT_EQ(sbi->SetMetadata(0, setData, true), GSERROR_INVALID_ARGUMENTS);
290 ASSERT_EQ(sbi->SetMetadata(HDI::Display::Graphic::Common::V1_1::ATTRKEY_END, setData, true),
291 GSERROR_INVALID_ARGUMENTS);
292 sret = sbi->SetMetadata(metadataKey, setData, true);
293 ASSERT_TRUE(sret == OHOS::GSERROR_OK || sret == GSERROR_HDI_ERROR);
294 if (sret == OHOS::GSERROR_OK) {
295 ASSERT_TRUE(sbi->metaDataCache_.size() == 1);
296 }
297 }
298
299
300 /*
301 * Function: BufferRequestConfig
302 * Type: Function
303 * Rank: Important(2)
304 * EnvConditions: N/A
305 * CaseDescription: 1. new SurfaceBufferImpl
306 * 2. call SetBufferRequestConfig interface using requestConfig and check ret
307 * 3. call GetBufferRequestConfig interface using requestConfig and check ret
308 * 4. call WriteBufferRequestConfig interface and check ret
309 * 5. call ReadBufferRequestConfig interface and check ret
310 */
311 HWTEST_F(SurfaceBufferImplTest, BufferRequestConfig001, Function | MediumTest | Level2)
312 {
313 buffer = new SurfaceBufferImpl();
314 MessageParcel parcel;
315 buffer->SetBufferRequestConfig(requestConfig);
316 ASSERT_EQ(buffer->GetBufferRequestConfig(), requestConfig);
317 ASSERT_EQ(buffer->WriteBufferRequestConfig(parcel), GSERROR_OK);
318 ASSERT_EQ(buffer->ReadBufferRequestConfig(parcel), GSERROR_OK);
319 }
320
321 /*
322 * Function: EglData
323 * Type: Function
324 * Rank: Important(2)
325 * EnvConditions: N/A
326 * CaseDescription: 1. new SurfaceBufferImpl
327 * 2. call SetEglData
328 * 3. call GetEglData and check ret
329 */
330 HWTEST_F(SurfaceBufferImplTest, EglData001, Function | MediumTest | Level2)
331 {
332 buffer = new SurfaceBufferImpl();
333 sptr<EglData> data = nullptr;
334 buffer->SetEglData(data);
335 ASSERT_EQ(buffer->GetEglData(), nullptr);
336 }
337
338 /*
339 * Function: BufferWrapper
340 * Type: Function
341 * Rank: Important(2)
342 * EnvConditions: N/A
343 * CaseDescription: 1. new SurfaceBufferImpl
344 * 2. call SetBufferWrapper
345 * 3. call GetBufferWrapper and check ret
346 */
347 HWTEST_F(SurfaceBufferImplTest, BufferWrapper001, Function | MediumTest | Level2)
348 {
349 buffer = new SurfaceBufferImpl();
350 BufferWrapper wrapper = {};
351 buffer->SetBufferWrapper(wrapper);
352 buffer->GetBufferWrapper();
353 }
354
355 /*
356 * Function: SetSurfaceBufferScalingMode&GetSurfaceBufferScalingMode
357 * Type: Function
358 * Rank: Important(2)
359 * EnvConditions: N/A
360 * CaseDescription: 1. new SurfaceBufferImpl
361 * 2. call GetSurfaceBufferScalingMode and check default is SCALING_MODE_SCALE_TO_WINDOW
362 * 3. call SetSurfaceBufferScalingMode and GetSurfaceBufferScalingMode and check ret
363 * 4. repeatly call SetSurfaceBufferScalingMode and GetSurfaceBufferScalingMode and check ret
364 */
365 HWTEST_F(SurfaceBufferImplTest, SurfaceBufferScalingMode001, Function | MediumTest | Level2)
366 {
367 buffer = new SurfaceBufferImpl();
368 ASSERT_EQ(buffer->GetSurfaceBufferScalingMode(), ScalingMode::SCALING_MODE_SCALE_TO_WINDOW);
369 buffer->SetSurfaceBufferScalingMode(ScalingMode::SCALING_MODE_SCALE_CROP);
370 ASSERT_EQ(buffer->GetSurfaceBufferScalingMode(), ScalingMode::SCALING_MODE_SCALE_CROP);
371 buffer->SetSurfaceBufferScalingMode(ScalingMode::SCALING_MODE_NO_SCALE_CROP);
372 ASSERT_EQ(buffer->GetSurfaceBufferScalingMode(), ScalingMode::SCALING_MODE_NO_SCALE_CROP);
373 }
374 }
375