1 /*
2  * Copyright (c) 2023 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 "skia_runtime_effect.h"
17 #include <regex>
18 #include <fstream>
19 #include "effect/runtime_effect.h"
20 
21 #include "skia_adapter/skia_data.h"
22 #include "skia_adapter/skia_matrix.h"
23 #include "skia_shader_effect.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
GlslToSksl(std::string & sksl,std::string & glsl)28 void SkiaRuntimeEffect::GlslToSksl(std::string& sksl, std::string& glsl)
29 {
30     auto fixup = [&glsl](const char* input, const char* replacement) {
31         glsl = std::regex_replace(glsl, std::regex(input), replacement);
32     };
33     std::vector<std::string> childNames;
34 
35     std::string pattern(R"(uniform\s+shader\s+(.*);)");
36     std::regex functionRegex(pattern);
37     std::smatch match;
38     std::string::const_iterator searchStart(glsl.cbegin());
39     while (std::regex_search(searchStart, glsl.cend(), match, functionRegex)) {
40         childNames.emplace_back(match[1].str());
41         searchStart = match.suffix().first;
42     }
43 
44     for (const auto& childName : childNames) {
45         std::string pattern(childName + R"(\((.*?)\))");
46         std::string replacement = childName + ".eval($1)";
47         fixup(pattern.c_str(), replacement.c_str());
48     }
49     // change "uniform sampler2D inputTexture" to "uniform shader inputTexture"
50     fixup(R"(\buniform\s+sampler2D\b)", "uniform shader ");
51     // change "texture(inputTexture, drawing_coord)" to "inputTexture.eval(drawing_coord)"
52     fixup(R"(texture\(\s*(\w+)\s*,\s*(\w+)\s*\))", "$01.eval($02)");
53     // Replace possible f characters after floating point numbers
54     fixup(R"((\d+\.\d+)f)", "$1");
55     sksl += glsl;
56 }
57 
SkiaRuntimeEffect()58 SkiaRuntimeEffect::SkiaRuntimeEffect() noexcept : skRuntimeEffect_(nullptr) {}
59 
InitForShader(const std::string & sl,const RuntimeEffectOptions & options)60 void SkiaRuntimeEffect::InitForShader(const std::string& sl, const RuntimeEffectOptions& options)
61 {
62     std::string sksl;
63     std::string glsl = sl;
64     GlslToSksl(sksl, glsl);
65 
66     SkRuntimeEffect::Options skOptions;
67     skOptions.useAF = options.useAF;
68     skOptions.forceNoInline = options.forceNoInline;
69     auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl.c_str()), skOptions);
70     skRuntimeEffect_ = effect;
71 }
72 
InitForShader(const std::string & sl)73 void SkiaRuntimeEffect::InitForShader(const std::string& sl)
74 {
75     std::string sksl;
76     std::string glsl = sl;
77     GlslToSksl(sksl, glsl);
78 
79     auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl.c_str()));
80     skRuntimeEffect_ = effect;
81 }
82 
InitForES3Shader(const std::string & sl)83 void SkiaRuntimeEffect::InitForES3Shader(const std::string& sl)
84 {
85     std::string sksl;
86     std::string glsl = sl;
87     GlslToSksl(sksl, glsl);
88 
89     auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl.c_str()), SkRuntimeEffectPriv::ES3Options());
90     skRuntimeEffect_ = effect;
91 }
92 
InitForBlender(const std::string & sl)93 void SkiaRuntimeEffect::InitForBlender(const std::string& sl)
94 {
95     std::string sksl;
96     std::string glsl(sl);
97     GlslToSksl(sksl, glsl);
98     auto [effect, err] = SkRuntimeEffect::MakeForBlender(SkString(sksl.c_str()));
99     skRuntimeEffect_ = effect;
100 }
101 
MakeShader(std::shared_ptr<Data> uniforms,std::shared_ptr<ShaderEffect> children[],size_t childCount,const Matrix * localMatrix,bool isOpaque)102 std::shared_ptr<ShaderEffect> SkiaRuntimeEffect::MakeShader(std::shared_ptr<Data> uniforms,
103     std::shared_ptr<ShaderEffect> children[], size_t childCount, const Matrix* localMatrix,
104     bool isOpaque)
105 {
106     std::shared_ptr<ShaderEffect> shader = std::make_shared<ShaderEffect>();
107     if (skRuntimeEffect_ != nullptr) {
108         auto data = uniforms ? uniforms->GetImpl<SkiaData>()->GetSkData() : SkData::MakeEmpty();
109         sk_sp<SkShader> skChildren[childCount];
110         for (size_t i = 0; i < childCount; ++i) {
111             auto skShaderImpl = children[i]->GetImpl<SkiaShaderEffect>();
112             if (skShaderImpl) {
113                 skChildren[i] = sk_sp<SkShader>(skShaderImpl->GetShader());
114             }
115         }
116         sk_sp<SkShader> skShader = skRuntimeEffect_->makeShader(data, skChildren,
117             childCount, localMatrix ? &localMatrix->GetImpl<SkiaMatrix>()->ExportSkiaMatrix() : nullptr,
118             isOpaque);
119         shader->GetImpl<SkiaShaderEffect>()->SetSkShader(skShader);
120     }
121     return shader;
122 }
123 
GetRuntimeEffect() const124 sk_sp<SkRuntimeEffect> SkiaRuntimeEffect::GetRuntimeEffect() const
125 {
126     return skRuntimeEffect_;
127 }
128 
SetRuntimeEffect(const sk_sp<SkRuntimeEffect> & skRuntimeEffect)129 void SkiaRuntimeEffect::SetRuntimeEffect(const sk_sp<SkRuntimeEffect>& skRuntimeEffect)
130 {
131     skRuntimeEffect_ = skRuntimeEffect;
132 }
133 } // namespace Drawing
134 } // namespace Rosen
135 } // namespace OHOS