1 /*
2  * Copyright (c) 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 
16 #include <cstddef>
17 #include "gtest/gtest.h"
18 #include "skia_adapter/skia_gpu_context.h"
19 #include "text/font.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class SkiaGPUContextTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31     void SetUp() override;
32     void TearDown() override;
33 };
34 
SetUpTestCase()35 void SkiaGPUContextTest::SetUpTestCase() {}
TearDownTestCase()36 void SkiaGPUContextTest::TearDownTestCase() {}
SetUp()37 void SkiaGPUContextTest::SetUp() {}
TearDown()38 void SkiaGPUContextTest::TearDown() {}
39 
40 #ifdef RS_ENABLE_VK
41 /**
42  * @tc.name: BuildFromVK001
43  * @tc.desc: Test BuildFromVK
44  * @tc.type: FUNC
45  * @tc.require: I91F9L
46  */
47 HWTEST_F(SkiaGPUContextTest, BuildFromVK001, TestSize.Level1)
48 {
49     auto gpuContext = std::make_shared<SkiaGPUContext>();
50     ASSERT_TRUE(gpuContext != nullptr);
51     GrVkBackendContext grVkBackendContext;
52     gpuContext->BuildFromVK(grVkBackendContext);
53 }
54 
55 /**
56  * @tc.name: storeVkPipelineCacheData001
57  * @tc.desc: Test storeVkPipelineCacheData
58  * @tc.type: FUNC
59  * @tc.require: I91F9L
60  */
61 HWTEST_F(SkiaGPUContextTest, storeVkPipelineCacheData001, TestSize.Level1)
62 {
63     auto gpuContext = std::make_shared<SkiaGPUContext>();
64     ASSERT_TRUE(gpuContext != nullptr);
65     gpuContext->storeVkPipelineCacheData();
66     GrMockOptions options;
67     gpuContext->SetGrContext(GrDirectContext::MakeMock(&options));
68     gpuContext->storeVkPipelineCacheData();
69 }
70 #endif
71 
72 /**
73  * @tc.name: GetResourceCacheUsage001
74  * @tc.desc: Test GetResourceCacheUsage
75  * @tc.type: FUNC
76  * @tc.require: I91F9L
77  */
78 HWTEST_F(SkiaGPUContextTest, GetResourceCacheUsage001, TestSize.Level1)
79 {
80     auto gpuContext = std::make_shared<SkiaGPUContext>();
81     ASSERT_TRUE(gpuContext != nullptr);
82     gpuContext->GetResourceCacheUsage(nullptr, nullptr);
83     GrMockOptions options;
84     gpuContext->SetGrContext(GrDirectContext::MakeMock(&options));
85     int resourceCount = 1;
86     size_t resourceBytes = 1;
87     gpuContext->GetResourceCacheUsage(&resourceCount, &resourceBytes);
88 }
89 
90 /**
91  * @tc.name: FreeGpuResources001
92  * @tc.desc: Test FreeGpuResources
93  * @tc.type: FUNC
94  * @tc.require: I91F9L
95  */
96 HWTEST_F(SkiaGPUContextTest, FreeGpuResources001, TestSize.Level1)
97 {
98     auto gpuContext = std::make_shared<SkiaGPUContext>();
99     ASSERT_TRUE(gpuContext != nullptr);
100     gpuContext->FreeGpuResources();
101 }
102 
103 /**
104  * @tc.name: DumpGpuStats001
105  * @tc.desc: Test DumpGpuStats
106  * @tc.type: FUNC
107  * @tc.require: I91F9L
108  */
109 HWTEST_F(SkiaGPUContextTest, DumpGpuStats001, TestSize.Level1)
110 {
111     auto gpuContext = std::make_shared<SkiaGPUContext>();
112     ASSERT_TRUE(gpuContext != nullptr);
113     std::string out = "11";
114     gpuContext->DumpGpuStats(out);
115 }
116 
117 /**
118  * @tc.name: ReleaseResourcesAndAbandonContext001
119  * @tc.desc: Test ReleaseResourcesAndAbandonContext
120  * @tc.type: FUNC
121  * @tc.require: I91F9L
122  */
123 HWTEST_F(SkiaGPUContextTest, ReleaseResourcesAndAbandonContext001, TestSize.Level1)
124 {
125     auto gpuContext = std::make_shared<SkiaGPUContext>();
126     ASSERT_TRUE(gpuContext != nullptr);
127     gpuContext->ReleaseResourcesAndAbandonContext();
128     GrMockOptions options;
129     gpuContext->SetGrContext(GrDirectContext::MakeMock(&options));
130     gpuContext->ReleaseResourcesAndAbandonContext();
131 }
132 
133 /**
134  * @tc.name: PurgeUnlockedResources001
135  * @tc.desc: Test PurgeUnlockedResources
136  * @tc.type: FUNC
137  * @tc.require: I91F9L
138  */
139 HWTEST_F(SkiaGPUContextTest, PurgeUnlockedResources001, TestSize.Level1)
140 {
141     auto gpuContext = std::make_shared<SkiaGPUContext>();
142     ASSERT_TRUE(gpuContext != nullptr);
143     gpuContext->PurgeUnlockedResources(true);
144 }
145 
146 /**
147  * @tc.name: PurgeUnlockedResourcesByTag001
148  * @tc.desc: Test PurgeUnlockedResourcesByTag
149  * @tc.type: FUNC
150  * @tc.require: I91F9L
151  */
152 HWTEST_F(SkiaGPUContextTest, PurgeUnlockedResourcesByTag001, TestSize.Level1)
153 {
154     auto gpuContext = std::make_shared<SkiaGPUContext>();
155     ASSERT_TRUE(gpuContext != nullptr);
156     GPUResourceTag tag;
157     gpuContext->PurgeUnlockedResourcesByTag(true, tag);
158     GrMockOptions options;
159     gpuContext->SetGrContext(GrDirectContext::MakeMock(&options));
160     gpuContext->PurgeUnlockedResourcesByTag(true, tag);
161 }
162 
163 /**
164  * @tc.name: PurgeUnlockedResourcesByPid001
165  * @tc.desc: Test PurgeUnlockedResourcesByPid
166  * @tc.type: FUNC
167  * @tc.require: I91F9L
168  */
169 HWTEST_F(SkiaGPUContextTest, PurgeUnlockedResourcesByPid001, TestSize.Level1)
170 {
171     auto gpuContext = std::make_shared<SkiaGPUContext>();
172     ASSERT_TRUE(gpuContext != nullptr);
173     GPUResourceTag tag;
174     std::set<pid_t> pidset;
175     gpuContext->PurgeUnlockedResourcesByPid(true, pidset);
176     GrMockOptions options;
177     gpuContext->SetGrContext(GrDirectContext::MakeMock(&options));
178     gpuContext->PurgeUnlockedResourcesByPid(true, pidset);
179 }
180 
181 /**
182  * @tc.name: PurgeUnlockAndSafeCacheGpuResources001
183  * @tc.desc: Test PurgeUnlockAndSafeCacheGpuResources
184  * @tc.type: FUNC
185  * @tc.require: I91F9L
186  */
187 HWTEST_F(SkiaGPUContextTest, PurgeUnlockAndSafeCacheGpuResources001, TestSize.Level1)
188 {
189     auto gpuContext = std::make_shared<SkiaGPUContext>();
190     ASSERT_TRUE(gpuContext != nullptr);
191     gpuContext->PurgeUnlockAndSafeCacheGpuResources();
192 }
193 
194 /**
195  * @tc.name: Submit001
196  * @tc.desc: Test Submit
197  * @tc.type: FUNC
198  * @tc.require: I91F9L
199  */
200 HWTEST_F(SkiaGPUContextTest, Submit001, TestSize.Level1)
201 {
202     auto gpuContext = std::make_shared<SkiaGPUContext>();
203     ASSERT_TRUE(gpuContext != nullptr);
204     gpuContext->Submit();
205 }
206 
207 /**
208  * @tc.name: ReleaseByTag001
209  * @tc.desc: Test ReleaseByTag
210  * @tc.type: FUNC
211  * @tc.require: I91F9L
212  */
213 HWTEST_F(SkiaGPUContextTest, ReleaseByTag001, TestSize.Level1)
214 {
215     auto gpuContext = std::make_shared<SkiaGPUContext>();
216     ASSERT_TRUE(gpuContext != nullptr);
217     GPUResourceTag tag;
218     gpuContext->ReleaseByTag(tag);
219     GrMockOptions options;
220     gpuContext->SetGrContext(GrDirectContext::MakeMock(&options));
221 }
222 
223 /**
224  * @tc.name: DumpMemoryStatisticsByTag001
225  * @tc.desc: Test DumpMemoryStatisticsByTag
226  * @tc.type: FUNC
227  * @tc.require: I91F9L
228  */
229 HWTEST_F(SkiaGPUContextTest, DumpMemoryStatisticsByTag001, TestSize.Level1)
230 {
231     auto gpuContext = std::make_shared<SkiaGPUContext>();
232     ASSERT_TRUE(gpuContext != nullptr);
233     GPUResourceTag tag;
234     gpuContext->DumpMemoryStatisticsByTag(nullptr, tag);
235     gpuContext->SetGrContext(nullptr);
236     gpuContext->DumpMemoryStatisticsByTag(nullptr, tag);
237 }
238 
239 /**
240  * @tc.name: DumpMemoryStatistics001
241  * @tc.desc: Test DumpMemoryStatistics
242  * @tc.type: FUNC
243  * @tc.require: I91F9L
244  */
245 HWTEST_F(SkiaGPUContextTest, DumpMemoryStatistics001, TestSize.Level1)
246 {
247     auto gpuContext = std::make_shared<SkiaGPUContext>();
248     ASSERT_TRUE(gpuContext != nullptr);
249     gpuContext->DumpMemoryStatistics(nullptr);
250 }
251 
252 /**
253  * @tc.name: SetCurrentGpuResourceTag001
254  * @tc.desc: Test SetCurrentGpuResourceTag
255  * @tc.type: FUNC
256  * @tc.require: I91F9L
257  */
258 HWTEST_F(SkiaGPUContextTest, SetCurrentGpuResourceTag001, TestSize.Level1)
259 {
260     auto gpuContext = std::make_shared<SkiaGPUContext>();
261     ASSERT_TRUE(gpuContext != nullptr);
262     GPUResourceTag gPUResourceTag;
263     gpuContext->SetCurrentGpuResourceTag(gPUResourceTag);
264 }
265 } // namespace Drawing
266 } // namespace Rosen
267 } // namespace OHOS