• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_0/display_composer_type.h"
21 #include "v1_0/include/idisplay_buffer.h"
22 #include "hdf_base.h"
23 #include "hdf_log.h"
24 
25 namespace OHOS {
26 namespace HDI {
27 namespace Display {
28 namespace TEST {
29 using namespace OHOS::HDI::Display::Composer::V1_0;
30 using namespace OHOS::HDI::Display::Buffer::V1_0;
31 
32 #ifndef DISPLAY_TEST_CHK_RETURN
33 #define DISPLAY_TEST_CHK_RETURN(val, ret, ...) \
34     do {                                       \
35         if (val) {                             \
36             __VA_ARGS__;                       \
37             return (ret);                      \
38         }                                      \
39     } while (0)
40 #endif
41 
42 const uint32_t ALLOC_SIZE_1080 = 1080; // alloc size 1080
43 const uint32_t ALLOC_SIZE_1920 = 1920; // alloc size 1920
44 const uint32_t ALLOC_SIZE_1280 = 1280; // alloc size 1280
45 const uint32_t ALLOC_SIZE_720 = 720; // alloc size 720
46 
47 const AllocInfo DISPLAY_BUFFER_TEST_SETS[] = {
48     // num0
49     {
50         .width = ALLOC_SIZE_1920,
51         .height = ALLOC_SIZE_1080,
52         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
53         .format = PIXEL_FMT_RGBX_8888
54     },
55     // num1
56     {
57         .width = ALLOC_SIZE_1080,
58         .height = ALLOC_SIZE_1920,
59         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
60         .format = PIXEL_FMT_RGBX_8888
61     },
62     // num2
63     {
64         .width = ALLOC_SIZE_1280,
65         .height = ALLOC_SIZE_720,
66         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
67         .format = PIXEL_FMT_RGBX_8888
68     },
69     // num3
70     {
71         .width = ALLOC_SIZE_1080,
72         .height = ALLOC_SIZE_1920,
73         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
74         .format = PIXEL_FMT_RGBA_8888
75     },
76     // num4
77     {
78         .width = ALLOC_SIZE_1080,
79         .height = ALLOC_SIZE_1920,
80         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
81         .format = PIXEL_FMT_BGRA_8888
82     },
83 
84     // num5
85     {
86         .width = ALLOC_SIZE_1080,
87         .height = ALLOC_SIZE_1920,
88         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
89         .format = PIXEL_FMT_YCBCR_420_SP
90     },
91     // num6
92     {
93         .width = ALLOC_SIZE_1080,
94         .height = ALLOC_SIZE_1920,
95         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
96         .format = PIXEL_FMT_YCRCB_420_SP
97     },
98     // num7
99     {
100         .width = ALLOC_SIZE_1080,
101         .height = ALLOC_SIZE_1920,
102         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
103         .format = PIXEL_FMT_YCBCR_420_P
104     },
105     // num8
106     {
107         .width = ALLOC_SIZE_1080,
108         .height = ALLOC_SIZE_1920,
109         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
110         .format = PIXEL_FMT_YCRCB_420_P
111     },
112     // num9
113     {
114         .width = ALLOC_SIZE_1080,
115         .height = ALLOC_SIZE_1920,
116         .usage = HBM_USE_MEM_DMA,
117         .format = PIXEL_FMT_RGBX_8888
118     },
119     // num10
120     {
121         .width = ALLOC_SIZE_1080,
122         .height = ALLOC_SIZE_1920,
123         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ,
124         .format = PIXEL_FMT_RGBX_8888
125     },
126     // num11
127     {
128         .width = ALLOC_SIZE_1080,
129         .height = ALLOC_SIZE_1920,
130         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_WRITE,
131         .format = PIXEL_FMT_RGBX_8888
132     },
133 #ifdef DISPLAY_COMMUNITY
134      // num12
135     {
136         .width = ALLOC_SIZE_1080,
137         .height = ALLOC_SIZE_1920,
138         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
139         .format = PIXEL_FMT_RGB_888
140     },
141     // num13
142     {
143         .width = ALLOC_SIZE_1080,
144         .height = ALLOC_SIZE_1920,
145         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
146         .format = PIXEL_FMT_BGRX_8888
147     },
148     // num14
149     {
150         .width = ALLOC_SIZE_1080,
151         .height = ALLOC_SIZE_1920,
152         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
153         .format = PIXEL_FMT_RGBA_4444
154     },
155     // num15
156     {
157         .width = ALLOC_SIZE_1080,
158         .height = ALLOC_SIZE_1920,
159         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
160         .format = PIXEL_FMT_RGBX_4444
161     },
162     // num16
163     {
164         .width = ALLOC_SIZE_1080,
165         .height = ALLOC_SIZE_1920,
166         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
167         .format = PIXEL_FMT_BGRA_4444
168     },
169     // num17
170     {
171         .width = ALLOC_SIZE_1080,
172         .height = ALLOC_SIZE_1920,
173         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
174         .format = PIXEL_FMT_BGRX_4444
175     },
176     // num18
177     {
178         .width = ALLOC_SIZE_1080,
179         .height = ALLOC_SIZE_1920,
180         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
181         .format = PIXEL_FMT_BGR_565
182     },
183     // num19
184     {
185         .width = ALLOC_SIZE_1080,
186         .height = ALLOC_SIZE_1920,
187         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
188         .format = PIXEL_FMT_BGRA_5551
189     },
190     // num20
191     {
192         .width = ALLOC_SIZE_1080,
193         .height = ALLOC_SIZE_1920,
194         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
195         .format = PIXEL_FMT_BGRX_5551
196     },
197 #else
198     // num21
199     {
200         .width = ALLOC_SIZE_1080,
201         .height = ALLOC_SIZE_1920,
202         .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_WRITE,
203         .format = PIXEL_FMT_RGBA_1010102
204     },
205 #endif // DISPLAY_COMMUNITY
206 };
207 
SetUp()208 void DisplayBufferUt::SetUp()
209 {
210     displayBuffer_ = IDisplayBuffer::Get();
211     if (displayBuffer_ == nullptr) {
212         HDF_LOGE("IDisplayBuffer get failed");
213         ASSERT_TRUE(0);
214     }
215 }
216 
TearDown()217 void DisplayBufferUt::TearDown()
218 {
219 }
220 
AllocMemTest(AllocInfo & info)221 int32_t DisplayBufferUt::AllocMemTest(AllocInfo& info)
222 {
223     int ret;
224     BufferHandle *buffer = nullptr;
225     const int TEST_COUNT = 40; // test 40 times
226     for (int i = 0; i < TEST_COUNT; i++) {
227         ret = displayBuffer_->AllocMem(info, buffer);
228         if (ret != DISPLAY_SUCCESS || buffer == nullptr) {
229             HDF_LOGE("AllocMem failed");
230             return ret;
231         }
232         void *vAddr = displayBuffer_->Mmap(*buffer);
233         if (vAddr == nullptr) {
234             HDF_LOGE("Mmap failed");
235             displayBuffer_->FreeMem(*buffer);
236             return DISPLAY_FAILURE;
237         }
238 
239         if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
240             ret = displayBuffer_->InvalidateCache(*buffer);
241             if (ret != DISPLAY_SUCCESS) {
242                 HDF_LOGE("InvalidateCache failed");
243                 displayBuffer_->Unmap(*buffer);
244                 displayBuffer_->FreeMem(*buffer);
245                 return ret;
246             }
247         }
248         if (memset_s(vAddr, buffer->size, 0, buffer->size) != EOK) {
249             HDF_LOGE("Insufficient memory");
250             displayBuffer_->Unmap(*buffer);
251             displayBuffer_->FreeMem(*buffer);
252             return DISPLAY_NOMEM;
253         }
254 
255         if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
256             ret = displayBuffer_->FlushCache(*buffer);
257             if (ret != DISPLAY_SUCCESS) {
258                 HDF_LOGE("FlushCache failed");
259                 displayBuffer_->Unmap(*buffer);
260                 displayBuffer_->FreeMem(*buffer);
261                 return ret;
262             }
263         }
264         displayBuffer_->Unmap(*buffer);
265         displayBuffer_->FreeMem(*buffer);
266     }
267     return DISPLAY_SUCCESS;
268 }
269 
TEST_P(DisplayBufferUt,DisplayBufferUt)270 TEST_P(DisplayBufferUt, DisplayBufferUt)
271 {
272     AllocInfo params = GetParam();
273     int ret = AllocMemTest(params);
274     ASSERT_TRUE(ret == DISPLAY_SUCCESS);
275 }
276 
277 INSTANTIATE_TEST_SUITE_P(AllocTest, DisplayBufferUt, ::testing::ValuesIn(DISPLAY_BUFFER_TEST_SETS));
278 } // OHOS
279 } // HDI
280 } // DISPLAY
281 } // TEST