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 #include "display_buffer_ut.h"
16 #include <securec.h>
17 
18 #include "gtest/gtest.h"
19 #include "v1_0/display_buffer_type.h"
20 #include "v1_2/display_composer_type.h"
21 #include "hdf_base.h"
22 #include "hdf_log.h"
23 
24 namespace OHOS {
25 namespace HDI {
26 namespace Display {
27 namespace TEST {
28 using namespace testing::ext;
29 using namespace OHOS::HDI::Display::Composer::V1_2;
30 using namespace OHOS::HDI::Display::Buffer::V1_1;
31 using OHOS::HDI::Display::Buffer::V1_0::AllocInfo;
32 using OHOS::HDI::Display::Composer::V1_2::HBM_USE_MEM_DMA;
33 using OHOS::HDI::Display::Composer::V1_2::HBM_USE_CPU_READ;
34 using OHOS::HDI::Display::Composer::V1_2::HBM_USE_CPU_WRITE;
35 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_RGBX_8888;
36 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_RGBA_8888;
37 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_BGRA_8888;
38 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_YCBCR_420_SP;
39 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_YCRCB_420_SP;
40 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_YCBCR_420_P;
41 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_YCRCB_420_P;
42 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_RGB_888;
43 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_BGRX_8888;
44 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_RGBA_4444;
45 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_RGBX_4444;
46 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_BGRA_4444;
47 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_BGRX_4444;
48 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_BGR_565;
49 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_BGRA_5551;
50 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_BGRX_5551;
51 using OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_RGBA_1010102;
52 #ifndef DISPLAY_TEST_CHK_RETURN
53 #define DISPLAY_TEST_CHK_RETURN(val, ret, ...) \
54     do {                                       \
55         if (val) {                             \
56             __VA_ARGS__;                       \
57             return (ret);                      \
58         }                                      \
59     } while (0)
60 #endif
61 
62 const uint32_t ALLOC_SIZE_1080 = 1080; // alloc size 1080
63 const uint32_t ALLOC_SIZE_1920 = 1920; // alloc size 1920
64 const uint32_t ALLOC_SIZE_1280 = 1280; // alloc size 1280
65 const uint32_t ALLOC_SIZE_720 = 720; // alloc size 720
66 
67 const AllocInfo DISPLAY_BUFFER_TEST_SETS[] = {
68     // num0
69     {
70         .width = ALLOC_SIZE_1920,
71         .height = ALLOC_SIZE_1080,
72         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
73         .format = PIXEL_FMT_RGBX_8888
74     },
75     // num1
76     {
77         .width = ALLOC_SIZE_1080,
78         .height = ALLOC_SIZE_1920,
79         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
80         .format = PIXEL_FMT_RGBX_8888
81     },
82     // num2
83     {
84         .width = ALLOC_SIZE_1280,
85         .height = ALLOC_SIZE_720,
86         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
87         .format = PIXEL_FMT_RGBX_8888
88     },
89     // num3
90     {
91         .width = ALLOC_SIZE_1080,
92         .height = ALLOC_SIZE_1920,
93         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
94         .format = PIXEL_FMT_RGBA_8888
95     },
96     // num4
97     {
98         .width = ALLOC_SIZE_1080,
99         .height = ALLOC_SIZE_1920,
100         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
101         .format = PIXEL_FMT_BGRA_8888
102     },
103     // num5
104     {
105         .width = ALLOC_SIZE_1080,
106         .height = ALLOC_SIZE_1920,
107         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
108         .format = PIXEL_FMT_YCBCR_420_SP
109     },
110     // num6
111     {
112         .width = ALLOC_SIZE_1080,
113         .height = ALLOC_SIZE_1920,
114         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
115         .format = PIXEL_FMT_YCRCB_420_SP
116     },
117     // num7
118     {
119         .width = ALLOC_SIZE_1080,
120         .height = ALLOC_SIZE_1920,
121         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
122         .format = PIXEL_FMT_YCBCR_420_P
123     },
124     // num8
125     {
126         .width = ALLOC_SIZE_1080,
127         .height = ALLOC_SIZE_1920,
128         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
129         .format = PIXEL_FMT_YCRCB_420_P
130     },
131     // num9
132     {
133         .width = ALLOC_SIZE_1080,
134         .height = ALLOC_SIZE_1920,
135         .usage = HBM_USE_MEM_DMA,
136         .format = PIXEL_FMT_RGBX_8888
137     },
138     // num10
139     {
140         .width = ALLOC_SIZE_1080,
141         .height = ALLOC_SIZE_1920,
142         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ,
143         .format = PIXEL_FMT_RGBX_8888
144     },
145     // num11
146     {
147         .width = ALLOC_SIZE_1080,
148         .height = ALLOC_SIZE_1920,
149         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_WRITE,
150         .format = PIXEL_FMT_RGBX_8888
151     },
152     //HBM_USE_CPU_HW_BOTH
153     // num12
154     {
155         .width = ALLOC_SIZE_1920,
156         .height = ALLOC_SIZE_1080,
157         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
158         .format = PIXEL_FMT_RGBX_8888
159     },
160     // num13
161     {
162         .width = ALLOC_SIZE_1080,
163         .height = ALLOC_SIZE_1920,
164         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
165         .format = PIXEL_FMT_RGBX_8888
166     },
167     // num14
168     {
169         .width = ALLOC_SIZE_1280,
170         .height = ALLOC_SIZE_720,
171         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
172         .format = PIXEL_FMT_RGBX_8888
173     },
174     // num15
175     {
176         .width = ALLOC_SIZE_1080,
177         .height = ALLOC_SIZE_1920,
178         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
179         .format = PIXEL_FMT_RGBA_8888
180     },
181     // num16
182     {
183         .width = ALLOC_SIZE_1080,
184         .height = ALLOC_SIZE_1920,
185         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
186         .format = PIXEL_FMT_BGRA_8888
187     },
188     // num17
189     {
190         .width = ALLOC_SIZE_1080,
191         .height = ALLOC_SIZE_1920,
192         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
193         .format = PIXEL_FMT_YCBCR_420_SP
194     },
195     // num18
196     {
197         .width = ALLOC_SIZE_1080,
198         .height = ALLOC_SIZE_1920,
199         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
200         .format = PIXEL_FMT_YCRCB_420_SP
201     },
202     // num19
203     {
204         .width = ALLOC_SIZE_1080,
205         .height = ALLOC_SIZE_1920,
206         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
207         .format = PIXEL_FMT_YCBCR_420_P
208     },
209     // num20
210     {
211         .width = ALLOC_SIZE_1080,
212         .height = ALLOC_SIZE_1920,
213         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
214         .format = PIXEL_FMT_YCRCB_420_P
215     },
216     // num21
217     {
218         .width = ALLOC_SIZE_1080,
219         .height = ALLOC_SIZE_1920,
220         .usage = HBM_USE_CPU_HW_BOTH,
221         .format = PIXEL_FMT_RGBX_8888
222     },
223     // num22
224     {
225         .width = ALLOC_SIZE_1080,
226         .height = ALLOC_SIZE_1920,
227         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ,
228         .format = PIXEL_FMT_RGBX_8888
229     },
230     // num23
231     {
232         .width = ALLOC_SIZE_1080,
233         .height = ALLOC_SIZE_1920,
234         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_WRITE,
235         .format = PIXEL_FMT_RGBX_8888
236     },
237 #ifdef DISPLAY_COMMUNITY
238      // num24
239     {
240         .width = ALLOC_SIZE_1080,
241         .height = ALLOC_SIZE_1920,
242         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
243         .format = PIXEL_FMT_RGB_888
244     },
245     // num25
246     {
247         .width = ALLOC_SIZE_1080,
248         .height = ALLOC_SIZE_1920,
249         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
250         .format = PIXEL_FMT_BGRX_8888
251     },
252     // num26
253     {
254         .width = ALLOC_SIZE_1080,
255         .height = ALLOC_SIZE_1920,
256         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
257         .format = PIXEL_FMT_RGBA_4444
258     },
259     // num27
260     {
261         .width = ALLOC_SIZE_1080,
262         .height = ALLOC_SIZE_1920,
263         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
264         .format = PIXEL_FMT_RGBX_4444
265     },
266     // num28
267     {
268         .width = ALLOC_SIZE_1080,
269         .height = ALLOC_SIZE_1920,
270         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
271         .format = PIXEL_FMT_BGRA_4444
272     },
273     // num29
274     {
275         .width = ALLOC_SIZE_1080,
276         .height = ALLOC_SIZE_1920,
277         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
278         .format = PIXEL_FMT_BGRX_4444
279     },
280     // num30
281     {
282         .width = ALLOC_SIZE_1080,
283         .height = ALLOC_SIZE_1920,
284         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
285         .format = PIXEL_FMT_BGR_565
286     },
287     // num31
288     {
289         .width = ALLOC_SIZE_1080,
290         .height = ALLOC_SIZE_1920,
291         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
292         .format = PIXEL_FMT_BGRA_5551
293     },
294     // num32
295     {
296         .width = ALLOC_SIZE_1080,
297         .height = ALLOC_SIZE_1920,
298         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
299         .format = PIXEL_FMT_BGRX_5551
300     },
301     //HBM_USE_CPU_HW_BOTH
302     // num33
303     {
304         .width = ALLOC_SIZE_1080,
305         .height = ALLOC_SIZE_1920,
306         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
307         .format = PIXEL_FMT_RGB_888
308     },
309     // num34
310     {
311         .width = ALLOC_SIZE_1080,
312         .height = ALLOC_SIZE_1920,
313         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
314         .format = PIXEL_FMT_BGRX_8888
315     },
316     // num35
317     {
318         .width = ALLOC_SIZE_1080,
319         .height = ALLOC_SIZE_1920,
320         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
321         .format = PIXEL_FMT_RGBA_4444
322     },
323     // num36
324     {
325         .width = ALLOC_SIZE_1080,
326         .height = ALLOC_SIZE_1920,
327         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
328         .format = PIXEL_FMT_RGBX_4444
329     },
330     // num37
331     {
332         .width = ALLOC_SIZE_1080,
333         .height = ALLOC_SIZE_1920,
334         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
335         .format = PIXEL_FMT_BGRA_4444
336     },
337     // num38
338     {
339         .width = ALLOC_SIZE_1080,
340         .height = ALLOC_SIZE_1920,
341         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
342         .format = PIXEL_FMT_BGRX_4444
343     },
344     // num39
345     {
346         .width = ALLOC_SIZE_1080,
347         .height = ALLOC_SIZE_1920,
348         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
349         .format = PIXEL_FMT_BGR_565
350     },
351     // num40
352     {
353         .width = ALLOC_SIZE_1080,
354         .height = ALLOC_SIZE_1920,
355         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
356         .format = PIXEL_FMT_BGRA_5551
357     },
358     // num41
359     {
360         .width = ALLOC_SIZE_1080,
361         .height = ALLOC_SIZE_1920,
362         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
363         .format = PIXEL_FMT_BGRX_5551
364     },
365 #else
366     // num42
367     {
368         .width = ALLOC_SIZE_1080,
369         .height = ALLOC_SIZE_1920,
370         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_WRITE,
371         .format = PIXEL_FMT_RGBA_1010102
372     },
373     //HBM_USE_CPU_HW_BOTH
374     // num43
375     {
376         .width = ALLOC_SIZE_1080,
377         .height = ALLOC_SIZE_1920,
378         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_WRITE,
379         .format = PIXEL_FMT_RGBA_1010102
380     },
381 #endif // DISPLAY_COMMUNITY
382 };
383 
SetUp()384 void DisplayBufferUt::SetUp()
385 {
386     displayBuffer_ = IDisplayBuffer::Get();
387     if (displayBuffer_ == nullptr) {
388         HDF_LOGE("IDisplayBuffer get failed");
389         ASSERT_TRUE(0);
390     }
391 }
392 
TearDown()393 void DisplayBufferUt::TearDown()
394 {
395 }
396 
MetadataTest(BufferHandle & handle)397 void DisplayBufferUt::MetadataTest(BufferHandle& handle)
398 {
399     int32_t ret = displayBuffer_->RegisterBuffer(handle);
400     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
401 
402     uint32_t key = 1;
403     std::vector<uint8_t> values = {1, 2, 3};
404     std::vector<uint32_t> keys = {};
405     std::vector<uint8_t> rets = {};
406     ret = displayBuffer_->SetMetadata(handle, key, values);
407     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
408     ret = displayBuffer_->GetMetadata(handle, key, rets);
409     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
410     if (ret != DISPLAY_NOT_SUPPORT) {
411         EXPECT_TRUE(rets == values);
412     }
413 
414     ret = displayBuffer_->ListMetadataKeys(handle, keys);
415     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
416     if (ret != DISPLAY_NOT_SUPPORT) {
417         EXPECT_TRUE(keys.size() == 1 && keys[0] == key);
418     }
419 
420     ret = displayBuffer_->EraseMetadataKey(handle, key);
421     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
422     if (ret != DISPLAY_NOT_SUPPORT) {
423         rets = {};
424         ret = displayBuffer_->GetMetadata(handle, key, rets);
425         EXPECT_TRUE(ret != DISPLAY_SUCCESS);
426     }
427 }
428 
AllocMemTest(AllocInfo & info)429 int32_t DisplayBufferUt::AllocMemTest(AllocInfo& info)
430 {
431     int ret;
432     BufferHandle *buffer = nullptr;
433     const int TEST_COUNT = 40; // test 40 times
434     for (int i = 0; i < TEST_COUNT; i++) {
435         ret = displayBuffer_->AllocMem(info, buffer);
436         if (ret == DISPLAY_NOT_SUPPORT) {
437             HDF_LOGE("%{public}s: AllocMem not support, ret=%{public}d", __func__, ret);
438             return DISPLAY_SUCCESS;
439         }
440         if (ret != DISPLAY_SUCCESS || buffer == nullptr) {
441             HDF_LOGE("AllocMem failed");
442             return ret;
443         }
444         MetadataTest(*buffer);
445         void *vAddr = displayBuffer_->Mmap(*buffer);
446         if (vAddr == nullptr) {
447             HDF_LOGE("Mmap failed");
448             displayBuffer_->FreeMem(*buffer);
449             return DISPLAY_FAILURE;
450         }
451         if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
452             ret = displayBuffer_->InvalidateCache(*buffer);
453             if (ret != DISPLAY_SUCCESS) {
454                 HDF_LOGE("InvalidateCache failed");
455                 displayBuffer_->Unmap(*buffer);
456                 displayBuffer_->FreeMem(*buffer);
457                 return ret;
458             }
459         }
460         if (memset_s(vAddr, buffer->size, 0, buffer->size) != EOK) {
461             HDF_LOGE("Insufficient memory");
462             displayBuffer_->Unmap(*buffer);
463             displayBuffer_->FreeMem(*buffer);
464             return DISPLAY_NOMEM;
465         }
466         if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
467             ret = displayBuffer_->FlushCache(*buffer);
468             if (ret != DISPLAY_SUCCESS) {
469                 HDF_LOGE("FlushCache failed");
470                 displayBuffer_->Unmap(*buffer);
471                 displayBuffer_->FreeMem(*buffer);
472                 return ret;
473             }
474         }
475         displayBuffer_->Unmap(*buffer);
476         displayBuffer_->FreeMem(*buffer);
477     }
478     return DISPLAY_SUCCESS;
479 }
480 
TEST_P(DisplayBufferUt,DisplayBufferUt)481 TEST_P(DisplayBufferUt, DisplayBufferUt)
482 {
483     AllocInfo params = GetParam();
484     int ret = AllocMemTest(params);
485     ASSERT_TRUE(ret == DISPLAY_SUCCESS);
486 }
487 
488 INSTANTIATE_TEST_SUITE_P(AllocTest, DisplayBufferUt, ::testing::ValuesIn(DISPLAY_BUFFER_TEST_SETS));
489 } // OHOS
490 } // HDI
491 } // DISPLAY
492 } // TEST