• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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     /**
367     * @tc.number: SUB_Driver_Display_HEBC_0100
368     * @tc.desc: Apply for a hebc format buffer
369     * @tc.size: MediumTest
370     * @tc.type: Function
371     */
372     // num42
373     {
374         .width = ALLOC_SIZE_1080,
375         .height = ALLOC_SIZE_1920,
376         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_WRITE,
377         .format = PIXEL_FMT_RGBA_1010102
378     },
379     //HBM_USE_CPU_HW_BOTH
380     // num43
381     {
382         .width = ALLOC_SIZE_1080,
383         .height = ALLOC_SIZE_1920,
384         .usage = HBM_USE_CPU_HW_BOTH | HBM_USE_CPU_WRITE,
385         .format = PIXEL_FMT_RGBA_1010102
386     },
387 #endif // DISPLAY_COMMUNITY
388 };
389 
SetUp()390 void DisplayBufferUt::SetUp()
391 {
392     displayBuffer_ = IDisplayBuffer::Get();
393     if (displayBuffer_ == nullptr) {
394         HDF_LOGE("IDisplayBuffer get failed");
395         ASSERT_TRUE(0);
396     }
397 }
398 
TearDown()399 void DisplayBufferUt::TearDown()
400 {
401 }
402 
MetadataTest(BufferHandle & handle)403 void DisplayBufferUt::MetadataTest(BufferHandle& handle)
404 {
405     int32_t ret = displayBuffer_->RegisterBuffer(handle);
406     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
407 
408     uint32_t key = 1;
409     std::vector<uint8_t> values = {1, 2, 3};
410     std::vector<uint32_t> keys = {};
411     std::vector<uint8_t> rets = {};
412     ret = displayBuffer_->SetMetadata(handle, key, values);
413     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
414     ret = displayBuffer_->GetMetadata(handle, key, rets);
415     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
416     if (ret != DISPLAY_NOT_SUPPORT) {
417         EXPECT_TRUE(rets == values);
418     }
419 
420     ret = displayBuffer_->ListMetadataKeys(handle, keys);
421     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
422     if (ret != DISPLAY_NOT_SUPPORT) {
423         EXPECT_TRUE(keys.size() == 1 && keys[0] == key);
424     }
425 
426     ret = displayBuffer_->EraseMetadataKey(handle, key);
427     EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
428     if (ret != DISPLAY_NOT_SUPPORT) {
429         rets = {};
430         ret = displayBuffer_->GetMetadata(handle, key, rets);
431         EXPECT_TRUE(ret != DISPLAY_SUCCESS);
432     }
433 }
434 
AllocMemTest(AllocInfo & info)435 int32_t DisplayBufferUt::AllocMemTest(AllocInfo& info)
436 {
437     int ret;
438     BufferHandle *buffer = nullptr;
439     const int TEST_COUNT = 40; // test 40 times
440     for (int i = 0; i < TEST_COUNT; i++) {
441         ret = displayBuffer_->AllocMem(info, buffer);
442         if (ret == DISPLAY_NOT_SUPPORT) {
443             HDF_LOGE("%{public}s: AllocMem not support, ret=%{public}d", __func__, ret);
444             return DISPLAY_SUCCESS;
445         }
446         if (ret != DISPLAY_SUCCESS || buffer == nullptr) {
447             HDF_LOGE("AllocMem failed");
448             return ret;
449         }
450         MetadataTest(*buffer);
451         void *vAddr = displayBuffer_->Mmap(*buffer);
452         if (vAddr == nullptr) {
453             HDF_LOGE("Mmap failed");
454             displayBuffer_->FreeMem(*buffer);
455             return DISPLAY_FAILURE;
456         }
457         if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
458             ret = displayBuffer_->InvalidateCache(*buffer);
459             if (ret != DISPLAY_SUCCESS) {
460                 HDF_LOGE("InvalidateCache failed");
461                 displayBuffer_->Unmap(*buffer);
462                 displayBuffer_->FreeMem(*buffer);
463                 return ret;
464             }
465         }
466         if (memset_s(vAddr, buffer->size, 0, buffer->size) != EOK) {
467             HDF_LOGE("Insufficient memory");
468             displayBuffer_->Unmap(*buffer);
469             displayBuffer_->FreeMem(*buffer);
470             return DISPLAY_NOMEM;
471         }
472         if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
473             ret = displayBuffer_->FlushCache(*buffer);
474             if (ret != DISPLAY_SUCCESS) {
475                 HDF_LOGE("FlushCache failed");
476                 displayBuffer_->Unmap(*buffer);
477                 displayBuffer_->FreeMem(*buffer);
478                 return ret;
479             }
480         }
481         displayBuffer_->Unmap(*buffer);
482         displayBuffer_->FreeMem(*buffer);
483     }
484     return DISPLAY_SUCCESS;
485 }
486 
TEST_P(DisplayBufferUt,DisplayBufferUt)487 TEST_P(DisplayBufferUt, DisplayBufferUt)
488 {
489     AllocInfo params = GetParam();
490     int ret = AllocMemTest(params);
491     ASSERT_TRUE(ret == DISPLAY_SUCCESS);
492 }
493 
494 /**
495  * @tc.number: SUB_Driver_Display_HDI_0100-2100
496  * @tc.desc: Set different information testing memory interfaces
497  * @tc.size: MediumTest
498  * @tc.type: Function
499  */
500 INSTANTIATE_TEST_SUITE_P(AllocTest, DisplayBufferUt, ::testing::ValuesIn(DISPLAY_BUFFER_TEST_SETS));
501 } // OHOS
502 } // HDI
503 } // DISPLAY
504 } // TEST
505