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 "draw/surface.h"
19 #include "effect/runtime_effect.h"
20 #include "effect/runtime_shader_builder.h"
21 #include "effect/shader_effect.h"
22 #include "skia_adapter/skia_runtime_shader_builder.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 class SkiaRuntimeShaderBuilderTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36 };
37 
SetUpTestCase()38 void SkiaRuntimeShaderBuilderTest::SetUpTestCase() {}
TearDownTestCase()39 void SkiaRuntimeShaderBuilderTest::TearDownTestCase() {}
SetUp()40 void SkiaRuntimeShaderBuilderTest::SetUp() {}
TearDown()41 void SkiaRuntimeShaderBuilderTest::TearDown() {}
42 
43 /**
44  * @tc.name: MakeShader001
45  * @tc.desc: Test MakeShader
46  * @tc.type: FUNC
47  * @tc.require: I91EQ7
48  */
49 HWTEST_F(SkiaRuntimeShaderBuilderTest, MakeShader001, TestSize.Level1)
50 {
51     std::string shaderString(R"(
52         uniform shader imageInput;
53 
54         half4 main(float2 xy) {
55             half4 c = imageInput.eval(xy);
56             return half4(c.rgb, 1.0);
57         }
58     )");
59     std::shared_ptr<RuntimeEffect> effect = RuntimeEffect::CreateForShader(shaderString);
60     SkiaRuntimeShaderBuilder skiaRuntimeShaderBuilder{effect};
61     Matrix matrix;
62     EXPECT_TRUE(skiaRuntimeShaderBuilder.MakeShader(&matrix, false) != nullptr);
63 }
64 
65 /**
66  * @tc.name: SetUniform001
67  * @tc.desc: Test SetUniform
68  * @tc.type: FUNC
69  * @tc.require: I91EQ7
70  */
71 HWTEST_F(SkiaRuntimeShaderBuilderTest, SetUniform001, TestSize.Level1)
72 {
73     SkiaRuntimeShaderBuilder skiaRuntimeShaderBuilder;
74     skiaRuntimeShaderBuilder.SetUniform("lightPos", 1.0f);
75     skiaRuntimeShaderBuilder.SetUniform("lightPos", 1.0f, 1.0f);
76     skiaRuntimeShaderBuilder.SetUniform("lightPos", 1.0f, 1.0f, 1.0f);
77     skiaRuntimeShaderBuilder.SetUniform("lightPos", 1.0f, 1.0f, 1.0f, 1.0f);
78     Matrix44 matrix;
79     skiaRuntimeShaderBuilder.SetUniform("lightPos", matrix);
80     Matrix matrix2;
81     EXPECT_TRUE(skiaRuntimeShaderBuilder.MakeShader(&matrix2, false) == nullptr);
82 }
83 
84 /**
85  * @tc.name: SetUniformVec4001
86  * @tc.desc: Test SetUniformVec4
87  * @tc.type: FUNC
88  * @tc.require: I91EQ7
89  */
90 HWTEST_F(SkiaRuntimeShaderBuilderTest, SetUniformVec4001, TestSize.Level1)
91 {
92     SkiaRuntimeShaderBuilder skiaRuntimeShaderBuilder;
93     skiaRuntimeShaderBuilder.SetUniformVec4("lightPos", 1.0f, 1.0f, 1.0f, 1.0f);
94     Matrix matrix;
95     EXPECT_TRUE(skiaRuntimeShaderBuilder.MakeShader(&matrix, false) == nullptr);
96 }
97 
98 /**
99  * @tc.name: SetUniformVec4002
100  * @tc.desc: Test SetUniformVec4
101  * @tc.type: FUNC
102  * @tc.require: I91EQ7
103  */
104 HWTEST_F(SkiaRuntimeShaderBuilderTest, SetUniformVec4002, TestSize.Level1)
105 {
106     std::string shaderString(R"(
107         uniform shader imageInput;
108 
109         half4 main(float2 xy) {
110             half4 c = imageInput.eval(xy);
111             return half4(c.rgb, 1.0);
112         }
113     )");
114     std::shared_ptr<RuntimeEffect> effect = RuntimeEffect::CreateForShader(shaderString);
115     SkiaRuntimeShaderBuilder skiaRuntimeShaderBuilder{effect};
116     skiaRuntimeShaderBuilder.SetUniformVec4("lightPos", 1.0f, 1.0f, 1.0f, 1.0f);
117     Matrix matrix;
118     EXPECT_TRUE(skiaRuntimeShaderBuilder.MakeShader(&matrix, false) != nullptr);
119 }
120 } // namespace Drawing
121 } // namespace Rosen
122 } // namespace OHOS