• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include "ge_shader.h"
18 #include "ge_log.h"
19 #include "draw/canvas.h"
20 #include "image/bitmap.h"
21 #include "utils/rect.h"
22 #include "draw/color.h"
23 #include "draw/path.h"
24 #include "ge_shader_filter_params.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS::Rosen;
29 using namespace Drawing;
30 
31 namespace OHOS {
32 namespace Rosen {
33 class GEShaderEffectTest : public Test {
34 public:
SetUpTestCase()35     static void SetUpTestCase()
36     {}
TearDownTestCase()37     static void TearDownTestCase()
38     {}
SetUp()39     void SetUp() override
40     {}
TearDown()41     void TearDown() override
42     {}
43 
44     static inline Canvas canvas_;
45     static inline Rect rect_{0.0, 0.0, 100.0, 100.0};
46 };
47 
48 using CacheDataType = std::shared_ptr<Image>;
49 class GETestShader : public GEShader {
50 public:
MakeDrawingShader(const Rect & rect,float progress)51     void MakeDrawingShader(const Rect& rect, float progress) override
52     {
53         static constexpr char prog[] = R"(
54             vec4 main(float2 fragCoord)
55             {
56                 return vec4(0.0, 0.0, 0.0, 1.0);
57             }
58         )";
59         auto testShaderEffect = RuntimeEffect::CreateForShader(prog);
60         auto build = std::make_shared<RuntimeShaderBuilder>(testShaderEffect);
61         drShader_ = build->MakeShader(nullptr, false);
62     }
63 
64 protected:
Preprocess(Canvas & canvas,const Rect & rect)65     void Preprocess(Canvas& canvas, const Rect& rect) override
66     {
67         if (cacheAnyPtr_ == nullptr) {
68             CacheDataType cacheData = nullptr;
69             cacheAnyPtr_ = std::make_shared<std::any>(std::make_any<CacheDataType>(cacheData));
70         }
71     }
72 };
73 
74 HWTEST_F(GEShaderEffectTest, GEShaderEffectTest_01, TestSize.Level1)
75 {
76     auto testShader = std::make_shared<GETestShader>();
77     uint32_t hash = 0;
78     EXPECT_EQ(testShader->Hash(), hash);
79     testShader->MakeDrawingShader(rect_, -1.f);
80     EXPECT_NE(testShader->GetDrawingShader(), nullptr);
81     std::shared_ptr<std::any> cachPtr = nullptr;
82     testShader->SetCache(cachPtr);
83     EXPECT_EQ(testShader->GetCache(), nullptr);
84     testShader->DrawShader(canvas_, rect_);
85     EXPECT_NE(testShader->GetCache(), nullptr);
86 }
87 }
88 }