• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "shader_cache.h"
17 
18 #include <cstring>
19 
20 #include <gtest/gtest.h>
21 #include "platform/common/rs_system_properties.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 class ShaderCacheTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 };
35 
SetUpTestCase()36 void ShaderCacheTest::SetUpTestCase() {}
TearDownTestCase()37 void ShaderCacheTest::TearDownTestCase() {}
SetUp()38 void ShaderCacheTest::SetUp() {}
TearDown()39 void ShaderCacheTest::TearDown() {}
40 
41 /**
42  * @tc.name: instance_test_001
43  * @tc.desc: Verify the instanciation of ShaderCache
44  * @tc.type: FUNC
45  * @tc.require:
46  * @tc.author:
47  */
48 HWTEST_F(ShaderCacheTest, instance_test_001, TestSize.Level1)
49 {
50 #ifdef ACE_ENABLE_GL
51     if (RSSystemProperties::IsUseVulkan()) {
52         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
53         return;
54     }
55     GTEST_LOG_(INFO) << "ShaderCacheTest instance_test_001 start";
56     /**
57      * @tc.steps: step1. initialize a shader cache with empty cache directory
58      */
59     auto &cache = ShaderCache::Instance();
60     std::string testedFileDir = "";
61     cache.SetFilePath(testedFileDir);
62     const char* identity = nullptr;
63     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
64     /**
65      * @tc.steps: step2. test initialization function
66      */
67     cache.InitShaderCache(identity, 0, false);
68     EXPECT_EQ(nullptr, cache.Load(*fakeData));
69 #endif
70 }
71 
72 /**
73  * @tc.name: initialization_test_001
74  * @tc.desc: Verify the initialization function of shader cache
75  * @tc.type: FUNC
76  * @tc.require:
77  * @tc.author:
78  */
79 HWTEST_F(ShaderCacheTest, initialization_test_001, TestSize.Level1)
80 {
81 #ifdef ACE_ENABLE_GL
82     if (RSSystemProperties::IsUseVulkan()) {
83         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
84         return;
85     }
86     GTEST_LOG_(INFO) << "ShaderCacheTest initialization_test_001 start";
87     /**
88      * @tc.steps: step1. initialize a shader cache instance and prepare parameters
89      */
90     auto &cache = ShaderCache::Instance();
91     const char* identity = nullptr;
92     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
93     /**
94      * @tc.steps: step2. test initialization function
95      */
96     cache.InitShaderCache(identity, 0, false);
97     EXPECT_EQ(nullptr, cache.Load(*fakeData));
98 #endif
99 }
100 
101 
102 /**
103  * @tc.name: initialization_test_002
104  * @tc.desc: Verify the file directory setting function of shader cache
105  * @tc.type: FUNC
106  * @tc.require:
107  * @tc.author:
108  */
109 HWTEST_F(ShaderCacheTest, initialization_test_002, TestSize.Level1)
110 {
111 #ifdef ACE_ENABLE_GL
112     if (RSSystemProperties::IsUseVulkan()) {
113         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
114         return;
115     }
116     GTEST_LOG_(INFO) << "ShaderCacheTest initialization_test_002 start";
117     /**
118      * @tc.steps: step1. initialize a shader cache instance and set the file dir
119      */
120     auto &cache = ShaderCache::Instance();
121     std::string testedFileDir = "TemporalFilePath";
122     cache.SetFilePath(testedFileDir);
123     const char* identity = nullptr;
124     const char* fakeBuffer = "testStr";
125     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
126     fakeData->BuildWithCopy(fakeBuffer, 8);
127     /**
128      * @tc.steps: step2. test if the file direction is correctly set
129      */
130     cache.InitShaderCache(identity, 0, false);
131     EXPECT_EQ(nullptr, cache.Load(*fakeData));
132 #endif
133 }
134 
135 /**
136  * @tc.name: store_test_001
137  * @tc.desc: Verify the store function of shader cache with a zero-length
138  * @tc.type: FUNC
139  * @tc.require:
140  * @tc.author:
141  */
142 HWTEST_F(ShaderCacheTest, store_test_001, TestSize.Level1)
143 {
144 #ifdef ACE_ENABLE_GL
145     if (RSSystemProperties::IsUseVulkan()) {
146         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
147         return;
148     }
149     GTEST_LOG_(INFO) << "ShaderCacheTest store_test_001 start";
150     /**
151      * @tc.steps: step1. initialize a shader cache instance and prepare parameters
152      */
153     auto &cache = ShaderCache::Instance();
154     std::string testedFileDir = "TemporalFilePath";
155     cache.SetFilePath(testedFileDir);
156     const char* identity = nullptr;
157     std::shared_ptr<Drawing::Data> fakeKey = std::make_shared<Drawing::Data>();
158     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
159     /**
160      * @tc.steps: step2. test the store function with given parameters
161      */
162     cache.InitShaderCache(identity, 0, false);
163     cache.Store(*fakeKey, *fakeData);
164     EXPECT_EQ(nullptr, cache.Load(*fakeKey));
165 #endif
166 }
167 
168 /**
169  * @tc.name: store_test_002
170  * @tc.desc: Verify the store function of shader cache with uninitialized status
171  * @tc.type: FUNC
172  * @tc.require:
173  * @tc.author:
174  */
175 HWTEST_F(ShaderCacheTest, store_test_002, TestSize.Level1)
176 {
177 #ifdef ACE_ENABLE_GL
178     if (RSSystemProperties::IsUseVulkan()) {
179         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
180         return;
181     }
182     GTEST_LOG_(INFO) << "ShaderCacheTest store_test_002 start";
183     /**
184      * @tc.steps: step1. initialize a shader cache instance and prepare parameters
185      */
186     auto &cache = ShaderCache::Instance();
187     std::string testedFileDir = "";
188     cache.SetFilePath(testedFileDir);
189     const char* identity = nullptr;
190     std::shared_ptr<Drawing::Data> fakeKey = std::make_shared<Drawing::Data>();
191     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
192     /**
193      * @tc.steps: step2. test the store function with given parameters
194      */
195     cache.InitShaderCache(identity, 0, false);
196     cache.Store(*fakeKey, *fakeData);
197     EXPECT_EQ(nullptr, cache.Load(*fakeKey));
198 #endif
199 }
200 
201 /**
202  * @tc.name: writing_test_001
203  * @tc.desc: Verify the writeToDisk function with a zero-length hashID
204  * @tc.type: FUNC
205  * @tc.require:
206  * @tc.author:
207  */
208 HWTEST_F(ShaderCacheTest, writing_test_001, TestSize.Level1)
209 {
210 #ifdef ACE_ENABLE_GL
211     if (RSSystemProperties::IsUseVulkan()) {
212         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
213         return;
214     }
215     GTEST_LOG_(INFO) << "ShaderCacheTest writing_test_001 start";
216     /**
217      * @tc.steps: step1. initialize a shader cache instance and prepare parameters
218      */
219     auto &cache = ShaderCache::Instance();
220     std::string testedFileDir = "TemporalFilePath";
221     cache.SetFilePath(testedFileDir);
222     const char* identity = nullptr;
223     const char* fakeBuffer = "testStr";
224     std::shared_ptr<Drawing::Data> fakeKey = std::make_shared<Drawing::Data>();
225     fakeKey->BuildWithCopy(fakeBuffer, 8);
226     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
227     fakeData->BuildWithCopy(fakeBuffer, 8);
228     /**
229      * @tc.steps: step2. verify the empty result of writeToDisk function
230      */
231     cache.InitShaderCache(identity, 0, false);
232     cache.Store(*fakeKey, *fakeData);
233     EXPECT_EQ(fakeData->GetSize(), cache.Load(*fakeKey)->GetSize());
234 
235 #endif
236 }
237 
238 /**
239  * @tc.name: writing_test_002
240  * @tc.desc: Verify the writeToDisk function with a non-zero-length hashID
241  * @tc.type: FUNC
242  * @tc.require:
243  * @tc.author:
244  */
245 HWTEST_F(ShaderCacheTest, writing_test_002, TestSize.Level1)
246 {
247 #ifdef ACE_ENABLE_GL
248     if (RSSystemProperties::IsUseVulkan()) {
249         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
250         return;
251     }
252     GTEST_LOG_(INFO) << "ShaderCacheTest writing_test_002 start";
253     /**
254      * @tc.steps: step1. initialize a shader cache instance and prepare parameters
255      */
256     auto &cache = ShaderCache::Instance();
257     std::string testedFileDir = "TemporalFilePath";
258     cache.SetFilePath(testedFileDir);
259     const char* identity = "testIdentity";
260     const char* fakeBuffer = "testStr";
261     std::shared_ptr<Drawing::Data> fakeKey = std::make_shared<Drawing::Data>();
262     fakeKey->BuildWithCopy(fakeBuffer, 8);
263     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
264     fakeData->BuildWithCopy(fakeBuffer, 8);
265     /**
266      * @tc.steps: step2. verify the non-empty result of writeToDisk function
267      */
268     cache.InitShaderCache(identity, 16, false);
269     cache.Store(*fakeKey, *fakeData);
270     EXPECT_EQ(fakeData->GetSize(), cache.Load(*fakeKey)->GetSize());
271 #endif
272 }
273 
274 /**
275  * @tc.name: load_test_001
276  * @tc.desc: Imporve Coverage
277  * @tc.type: FUNC
278  * @tc.require:
279  * @tc.author:
280  */
281 HWTEST_F(ShaderCacheTest, load_test_001, TestSize.Level1)
282 {
283     auto &cache = ShaderCache::Instance();
284     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
285     EXPECT_EQ(nullptr, cache.Load(*fakeData));
286 }
287 
288 /**
289  * @tc.name: querryShaderNum_test_001
290  * @tc.desc: Imporve Coverage
291  * @tc.type: FUNC
292  * @tc.require:
293  * @tc.author:
294  */
295 HWTEST_F(ShaderCacheTest, querryShaderNum_test_001, TestSize.Level1)
296 {
297     auto &cache = ShaderCache::Instance();
298     cache.QuerryShaderNum();
299     const char* identity = nullptr;
300     cache.InitShaderCache(identity, 0, false);
301     const char* testKey1 = "testKey1";
302     const char* testValue1 = "testValue1";
303     cache.cacheData_->Rewrite(testKey1, 8, testValue1, 8);
304     EXPECT_NE(0, cache.QuerryShaderNum());
305 }
306 
307 /**
308  * @tc.name: storeShader_test_001
309  * @tc.desc: Imporve Coverage
310  * @tc.type: FUNC
311  * @tc.require:
312  * @tc.author:
313  */
314 HWTEST_F(ShaderCacheTest, storeShader_test_001, TestSize.Level1)
315 {
316     auto &cache = ShaderCache::Instance();
317     std::string testedFileDir = "testFilePath";
318     cache.SetFilePath(testedFileDir);
319     const char* identity = nullptr;
320     std::shared_ptr<Drawing::Data> fakeKey = std::make_shared<Drawing::Data>();
321     std::shared_ptr<Drawing::Data> fakeData = std::make_shared<Drawing::Data>();
322     cache.InitShaderCache(identity, 0, false);
323     cache.Store(*fakeKey, *fakeData);
324     EXPECT_EQ(nullptr, cache.Load(*fakeKey));
325     cache.cacheData_ = nullptr;
326     cache.Store(*fakeKey, *fakeData);
327     cache.Load(*fakeKey);
328 }
329 
330 /**
331  * @tc.name: InitShaderCacheTest
332  * @tc.desc: Imporve Coverage
333  * @tc.type: FUNC
334  * @tc.require:
335  * @tc.author:
336  */
337 HWTEST_F(ShaderCacheTest, InitShaderCacheTest, TestSize.Level1)
338 {
339     auto &cache = ShaderCache::Instance();
340     cache.filePath_ = "";
341     cache.InitShaderCache(nullptr, 0, false);
342 }
343 
344 /**
345  * @tc.name: StoreTest
346  * @tc.desc: Imporve Coverage
347  * @tc.type: FUNC
348  * @tc.require:
349  * @tc.author:
350  */
351 HWTEST_F(ShaderCacheTest, StoreTest, TestSize.Level1)
352 {
353     auto &cache = ShaderCache::Instance();
354     cache.initialized_ = false;
355     Drawing::Data key;
356     Drawing::Data value;
357     cache.Store(key, value);
358 }
359 } // namespace Rosen
360 } // namespace OHOS