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 "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_0;
30 using namespace OHOS::HDI::Display::Buffer::V1_1;
31 using OHOS::HDI::Display::Buffer::V1_0::AllocInfo;
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
MetadataTest(BufferHandle & handle)221 void DisplayBufferUt::MetadataTest(BufferHandle& handle)
222 {
223 int32_t ret = displayBuffer_->RegisterBuffer(handle);
224 EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
225
226 uint32_t key = 1;
227 std::vector<uint8_t> values = {1, 2, 3};
228 std::vector<uint32_t> keys = {};
229 std::vector<uint8_t> rets = {};
230 ret = displayBuffer_->SetMetadata(handle, key, values);
231 EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
232 ret = displayBuffer_->GetMetadata(handle, key, rets);
233 EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
234 if (ret != DISPLAY_NOT_SUPPORT) {
235 EXPECT_TRUE(rets == values);
236 }
237
238 ret = displayBuffer_->ListMetadataKeys(handle, keys);
239 EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
240 if (ret != DISPLAY_NOT_SUPPORT) {
241 EXPECT_TRUE(keys.size() == 1 && keys[0] == key);
242 }
243
244 ret = displayBuffer_->EraseMetadataKey(handle, key);
245 EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
246 if (ret != DISPLAY_NOT_SUPPORT) {
247 rets = {};
248 ret = displayBuffer_->GetMetadata(handle, key, rets);
249 EXPECT_TRUE(ret != DISPLAY_SUCCESS);
250 }
251 }
252
AllocMemTest(AllocInfo & info)253 int32_t DisplayBufferUt::AllocMemTest(AllocInfo& info)
254 {
255 int ret;
256 BufferHandle *buffer = nullptr;
257 const int TEST_COUNT = 40; // test 40 times
258 for (int i = 0; i < TEST_COUNT; i++) {
259 ret = displayBuffer_->AllocMem(info, buffer);
260 if (ret == DISPLAY_NOT_SUPPORT) {
261 HDF_LOGE("%{public}s: AllocMem not support, ret=%{public}d", __func__, ret);
262 return DISPLAY_SUCCESS;
263 }
264 if (ret != DISPLAY_SUCCESS || buffer == nullptr) {
265 HDF_LOGE("AllocMem failed");
266 return ret;
267 }
268 MetadataTest(*buffer);
269 void *vAddr = displayBuffer_->Mmap(*buffer);
270 if (vAddr == nullptr) {
271 HDF_LOGE("Mmap failed");
272 displayBuffer_->FreeMem(*buffer);
273 return DISPLAY_FAILURE;
274 }
275 if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
276 ret = displayBuffer_->InvalidateCache(*buffer);
277 if (ret != DISPLAY_SUCCESS) {
278 HDF_LOGE("InvalidateCache failed");
279 displayBuffer_->Unmap(*buffer);
280 displayBuffer_->FreeMem(*buffer);
281 return ret;
282 }
283 }
284 if (memset_s(vAddr, buffer->size, 0, buffer->size) != EOK) {
285 HDF_LOGE("Insufficient memory");
286 displayBuffer_->Unmap(*buffer);
287 displayBuffer_->FreeMem(*buffer);
288 return DISPLAY_NOMEM;
289 }
290 if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
291 ret = displayBuffer_->FlushCache(*buffer);
292 if (ret != DISPLAY_SUCCESS) {
293 HDF_LOGE("FlushCache failed");
294 displayBuffer_->Unmap(*buffer);
295 displayBuffer_->FreeMem(*buffer);
296 return ret;
297 }
298 }
299 displayBuffer_->Unmap(*buffer);
300 displayBuffer_->FreeMem(*buffer);
301 }
302 return DISPLAY_SUCCESS;
303 }
304
TEST_P(DisplayBufferUt,DisplayBufferUt)305 TEST_P(DisplayBufferUt, DisplayBufferUt)
306 {
307 AllocInfo params = GetParam();
308 int ret = AllocMemTest(params);
309 ASSERT_TRUE(ret == DISPLAY_SUCCESS);
310 }
311
312 INSTANTIATE_TEST_SUITE_P(AllocTest, DisplayBufferUt, ::testing::ValuesIn(DISPLAY_BUFFER_TEST_SETS));
313 } // OHOS
314 } // HDI
315 } // DISPLAY
316 } // TEST