• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
16 #include "../GrallocAllocTest.h"
17 #include <securec.h>
18 #include "gtest/gtest.h"
19 #include "display_gralloc.h"
20 #include "../../common/display_test.h"
21 
22 namespace {
23 struct AllocTestPrms {
24     AllocInfo allocInfo;
25     int32_t expectStride;
26     int32_t expectSize;
27 };
28 
29 class GrallocAllocTest : public ::testing::TestWithParam<AllocTestPrms> {
30 protected:
31     virtual void SetUp();
32     virtual void TearDown();
33     int32_t AllocMemTest(AllocTestPrms &info);
34     GrallocFuncs *mGrallocFuncs = nullptr;
35 };
36 }
37 
38 namespace {
ALIGN_UP(int x,int a)39 int ALIGN_UP(int x, int a)
40 {
41     return ((x + (a-1)) / a) * a;
42 }
43 const int WIDTH_ALIGN = 8U;
44 const AllocTestPrms GRALLOC_TEST_SETS[] = {
45     // num0
46     // SUB_DriverSystem_DisplayHdi_0050
47     {
48         .allocInfo = {
49             .width = 1920,
50             .height = 1080,
51             .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
52             .format = PIXEL_FMT_RGBX_8888
53         },
54         .expectStride = 1920 * 4,
55         .expectSize = 1920 * 1080 * 4
56     },
57     // num1
58     // SUB_DriverSystem_DisplayHdi_0060
59     {
60         .allocInfo = {
61             .width = 1080,
62             .height = 1920,
63             .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
64             .format = PIXEL_FMT_RGBX_8888
65             },
66         .expectStride = 4352,
67         .expectSize = 8355840
68     },
69     // num2
70     // SUB_DriverSystem_DisplayHdi_0070
71     {
72         .allocInfo = {
73             .width = 1280,
74             .height = 720,
75             .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
76             .format = PIXEL_FMT_RGBX_8888
77         },
78         .expectStride = 1280 * 4,
79         .expectSize = 1280 * 720 * 4
80     },
81     // num3
82     // SUB_DriverSystem_DisplayHdi_0080
83     {
84         .allocInfo = {
85             .width = 1080,
86             .height = 1920,
87             .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
88             .format = PIXEL_FMT_RGBA_8888
89             },
90         .expectStride = 4352,
91         .expectSize = 8355840
92     },
93     // num5
94     // SUB_DriverSystem_DisplayHdi_0100
95     {
96         .allocInfo = {
97             .width = 1080,
98             .height = 1920,
99             .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
100             .format = PIXEL_FMT_BGRA_8888
101             },
102         .expectStride = 4352,
103         .expectSize = 8355840
104     },
105     // num18
106     // SUB_DriverSystem_DisplayHdi_0230
107     {
108         .allocInfo = {
109             .width = 1080,
110             .height = 1920,
111             .usage = HBM_USE_MEM_DMA,
112             .format = PIXEL_FMT_RGBX_8888
113         },
114         .expectStride = 4352,
115         .expectSize = 8355840
116     },
117     // num19
118     // SUB_DriverSystem_DisplayHdi_0240
119     {
120         .allocInfo = {
121             .width = 1080,
122             .height = 1920,
123             .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ,
124             .format = PIXEL_FMT_RGBX_8888
125         },
126         .expectStride = 4352, // expectStride
127         .expectSize = 8355840 // expectSize
128     },
129     // num20
130     // SUB_DriverSystem_DisplayHdi_0250
131     {
132         .allocInfo = {
133             .width = 1080,
134             .height = 1920,
135             .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_WRITE,
136             .format = PIXEL_FMT_RGBX_8888
137         },
138         .expectStride = 4352, // expectStride
139         .expectSize = 8355840 // expectSize
140     },
141 };
142 
CheckBufferHandle(AllocTestPrms & info,BufferHandle & buffer)143 static bool CheckBufferHandle(AllocTestPrms &info, BufferHandle &buffer)
144 {
145     if (buffer.stride != (ALIGN_UP(info.expectStride, WIDTH_ALIGN))) {
146         DISPLAY_TEST_LOGE("stride check failed stride %d, expect stride %d ", buffer.stride, info.expectStride);
147         DISPLAY_TEST_LOGE("stride check failed format %d width %d, height %d ", info.allocInfo.format,
148             info.allocInfo.width, info.allocInfo.height);
149         return false;
150     }
151 
152     if (buffer.size != info.expectSize) {
153         DISPLAY_TEST_LOGE("size check failed size %d, expect size %d ", buffer.size, info.expectSize);
154         DISPLAY_TEST_LOGE("stride check failed format %d width %d, height %d ", info.allocInfo.format,
155             info.allocInfo.width, info.allocInfo.height);
156         return false;
157     }
158     return true;
159 }
160 
SetUp()161 void GrallocAllocTest::SetUp()
162 {
163     if (GrallocInitialize(&mGrallocFuncs) != DISPLAY_SUCCESS) {
164         DISPLAY_TEST_LOGE("DisplayInit failure\n");
165         ASSERT_TRUE(0);
166     }
167 }
168 
TearDown()169 void GrallocAllocTest::TearDown()
170 {
171     if (GrallocUninitialize(mGrallocFuncs) != DISPLAY_SUCCESS) {
172         DISPLAY_TEST_LOGE("DisplayUninit failure\n");
173         ASSERT_TRUE(0);
174     }
175 }
176 
AllocMemTest(AllocTestPrms & info)177 int32_t GrallocAllocTest::AllocMemTest(AllocTestPrms &info)
178 {
179     int ret;
180     BufferHandle *buffer = nullptr;
181     const int testCount = 21; // test 40 times
182     for (int i = 0; i < testCount; i++) {
183         ret = mGrallocFuncs->AllocMem(&info.allocInfo, &buffer);
184         if (ret != DISPLAY_SUCCESS) {
185             return ret;
186         }
187         void *vAddr = mGrallocFuncs->Mmap(buffer);
188         if (vAddr == nullptr) {
189             return DISPLAY_FAILURE;
190         }
191 
192         if (info.allocInfo.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
193             ret = mGrallocFuncs->InvalidateCache(buffer);
194             if (ret != DISPLAY_SUCCESS) {
195                 return ret;
196             }
197         }
198         if (memset_s(vAddr, buffer->size, 0, buffer->size) != EOK) {
199             return DISPLAY_NOMEM;
200         }
201         DISPLAY_TEST_CHK_RETURN(!CheckBufferHandle(info, *buffer), DISPLAY_FAILURE,
202             DISPLAY_TEST_LOGE("buffer check failed"));
203         if (info.allocInfo.usage & (HBM_USE_CPU_READ | HBM_USE_CPU_WRITE)) {
204             ret = mGrallocFuncs->FlushCache(buffer);
205             if (ret != DISPLAY_SUCCESS) {
206                 return ret;
207             }
208         }
209         mGrallocFuncs->Unmap((buffer));
210         mGrallocFuncs->FreeMem(buffer);
211     }
212 
213     return DISPLAY_SUCCESS;
214 }
215 
TEST(GrallocAllocTest,NULLPTR)216 TEST(GrallocAllocTest, NULLPTR)
217 {
218     int ret = GrallocInitialize(nullptr);
219     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
220 
221     GrallocFuncs *grallocFuncs;
222     AllocInfo allocInfo;
223     BufferHandle *hdl;
224     ret = GrallocInitialize(&grallocFuncs);
225     ASSERT_TRUE(ret == DISPLAY_SUCCESS);
226     ret = grallocFuncs->AllocMem(nullptr, nullptr);
227     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
228     ret = grallocFuncs->AllocMem(&allocInfo, nullptr);
229     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
230     ret = grallocFuncs->AllocMem(nullptr, &hdl);
231     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
232     ret = grallocFuncs->InvalidateCache(nullptr);
233     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
234     ret = grallocFuncs->FlushCache(nullptr);
235     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
236     grallocFuncs->FreeMem(nullptr);
237     void *vAddr = grallocFuncs->Mmap(nullptr);
238     ASSERT_TRUE(vAddr == nullptr);
239     ret = grallocFuncs->Unmap(nullptr);
240     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
241     ret = GrallocUninitialize(nullptr);
242     ASSERT_TRUE(ret != DISPLAY_SUCCESS);
243 
244     ret = GrallocUninitialize(grallocFuncs);
245     ASSERT_TRUE(ret == DISPLAY_SUCCESS);
246 }
247 
TEST_P(GrallocAllocTest,GrallocAlloc)248 TEST_P(GrallocAllocTest, GrallocAlloc)
249 {
250     AllocTestPrms params = GetParam();
251     int ret = AllocMemTest(params);
252     ASSERT_TRUE(ret == DISPLAY_SUCCESS);
253 }
254 INSTANTIATE_TEST_SUITE_P(AllocTest, GrallocAllocTest, ::testing::ValuesIn(GRALLOC_TEST_SETS));
255 }