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_0/display_composer_type.h"
21#include "hdf_base.h"
22#include "hdf_log.h"
23
24namespace OHOS {
25namespace HDI {
26namespace Display {
27namespace TEST {
28using namespace testing::ext;
29using namespace OHOS::HDI::Display::Composer::V1_0;
30using namespace OHOS::HDI::Display::Buffer::V1_1;
31using 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
42const uint32_t ALLOC_SIZE_1080 = 1080; // alloc size 1080
43const uint32_t ALLOC_SIZE_1920 = 1920; // alloc size 1920
44const uint32_t ALLOC_SIZE_1280 = 1280; // alloc size 1280
45const uint32_t ALLOC_SIZE_720 = 720; // alloc size 720
46
47const 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    /**
199    * @tc.number: SUB_Driver_Display_HEBC_0100
200    * @tc.desc: Apply for a hebc format buffer
201    * @tc.size: MediumTest
202    * @tc.type: Function
203    */
204    {
205        .width = ALLOC_SIZE_1080,
206        .height = ALLOC_SIZE_1920,
207        .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_WRITE,
208        .format = PIXEL_FMT_RGBA_1010102
209    },
210#endif // DISPLAY_COMMUNITY
211};
212
213void DisplayBufferUt::SetUp()
214{
215    displayBuffer_ = IDisplayBuffer::Get();
216    if (displayBuffer_ == nullptr) {
217        HDF_LOGE("IDisplayBuffer get failed");
218        ASSERT_TRUE(0);
219    }
220}
221
222void DisplayBufferUt::TearDown()
223{
224}
225
226void DisplayBufferUt::MetadataTest(BufferHandle& handle)
227{
228    int32_t ret = displayBuffer_->RegisterBuffer(handle);
229    EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
230
231    uint32_t key = 1;
232    std::vector<uint8_t> values = {1, 2, 3};
233    std::vector<uint32_t> keys = {};
234    std::vector<uint8_t> rets = {};
235    ret = displayBuffer_->SetMetadata(handle, key, values);
236    EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
237    ret = displayBuffer_->GetMetadata(handle, key, rets);
238    EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
239    if (ret != DISPLAY_NOT_SUPPORT) {
240        EXPECT_TRUE(rets == values);
241    }
242
243    ret = displayBuffer_->ListMetadataKeys(handle, keys);
244    EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
245    if (ret != DISPLAY_NOT_SUPPORT) {
246        EXPECT_TRUE(keys.size() == 1 && keys[0] == key);
247    }
248
249    ret = displayBuffer_->EraseMetadataKey(handle, key);
250    EXPECT_TRUE(ret == DISPLAY_NOT_SUPPORT || ret == DISPLAY_SUCCESS);
251    if (ret != DISPLAY_NOT_SUPPORT) {
252        rets = {};
253        ret = displayBuffer_->GetMetadata(handle, key, rets);
254        EXPECT_TRUE(ret != DISPLAY_SUCCESS);
255    }
256}
257
258int32_t DisplayBufferUt::AllocMemTest(AllocInfo& info)
259{
260    int ret;
261    BufferHandle *buffer = nullptr;
262    const int TEST_COUNT = 40; // test 40 times
263    for (int i = 0; i < TEST_COUNT; i++) {
264        ret = displayBuffer_->AllocMem(info, buffer);
265        if (ret == DISPLAY_NOT_SUPPORT) {
266            HDF_LOGE("%{public}s: AllocMem not support, ret=%{public}d", __func__, ret);
267            return DISPLAY_SUCCESS;
268        }
269        if (ret != DISPLAY_SUCCESS || buffer == nullptr) {
270            HDF_LOGE("AllocMem failed");
271            return ret;
272        }
273        MetadataTest(*buffer);
274        void *vAddr = displayBuffer_->Mmap(*buffer);
275        if (vAddr == nullptr) {
276            HDF_LOGE("Mmap failed");
277            displayBuffer_->FreeMem(*buffer);
278            return DISPLAY_FAILURE;
279        }
280        if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
281            ret = displayBuffer_->InvalidateCache(*buffer);
282            if (ret != DISPLAY_SUCCESS) {
283                HDF_LOGE("InvalidateCache failed");
284                displayBuffer_->Unmap(*buffer);
285                displayBuffer_->FreeMem(*buffer);
286                return ret;
287            }
288        }
289        if (memset_s(vAddr, buffer->size, 0, buffer->size) != EOK) {
290            HDF_LOGE("Insufficient memory");
291            displayBuffer_->Unmap(*buffer);
292            displayBuffer_->FreeMem(*buffer);
293            return DISPLAY_NOMEM;
294        }
295        if (info.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
296            ret = displayBuffer_->FlushCache(*buffer);
297            if (ret != DISPLAY_SUCCESS) {
298                HDF_LOGE("FlushCache failed");
299                displayBuffer_->Unmap(*buffer);
300                displayBuffer_->FreeMem(*buffer);
301                return ret;
302            }
303        }
304        displayBuffer_->Unmap(*buffer);
305        displayBuffer_->FreeMem(*buffer);
306    }
307    return DISPLAY_SUCCESS;
308}
309
310TEST_P(DisplayBufferUt, DisplayBufferUt)
311{
312    AllocInfo params = GetParam();
313    int ret = AllocMemTest(params);
314    ASSERT_TRUE(ret == DISPLAY_SUCCESS);
315}
316
317/**
318 * @tc.number: SUB_Driver_Display_HDI_0100-2100
319 * @tc.desc: Set different information testing memory interfaces
320 * @tc.size: MediumTest
321 * @tc.type: Function
322 */
323INSTANTIATE_TEST_SUITE_P(AllocTest, DisplayBufferUt, ::testing::ValuesIn(DISPLAY_BUFFER_TEST_SETS));
324} // OHOS
325} // HDI
326} // DISPLAY
327} // TEST
328