• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef USE_M133_SKIA
69     skOptions.forceUnoptimized = options.forceNoInline;
70 #else
71     skOptions.forceNoInline = options.forceNoInline;
72 #endif
73     auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl.c_str()), skOptions);
74     skRuntimeEffect_ = effect;
75 }
76 
InitForShader(const std::string & sl)77 void SkiaRuntimeEffect::InitForShader(const std::string& sl)
78 {
79     std::string sksl;
80     std::string glsl = sl;
81     GlslToSksl(sksl, glsl);
82 
83     auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl.c_str()));
84     skRuntimeEffect_ = effect;
85 }
86 
InitForES3Shader(const std::string & sl)87 void SkiaRuntimeEffect::InitForES3Shader(const std::string& sl)
88 {
89     std::string sksl;
90     std::string glsl = sl;
91     GlslToSksl(sksl, glsl);
92 
93     auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl.c_str()), SkRuntimeEffectPriv::ES3Options());
94     skRuntimeEffect_ = effect;
95 }
96 
InitForBlender(const std::string & sl)97 void SkiaRuntimeEffect::InitForBlender(const std::string& sl)
98 {
99     std::string sksl;
100     std::string glsl(sl);
101     GlslToSksl(sksl, glsl);
102     auto [effect, err] = SkRuntimeEffect::MakeForBlender(SkString(sksl.c_str()));
103     skRuntimeEffect_ = effect;
104 }
105 
MakeShader(std::shared_ptr<Data> uniforms,std::shared_ptr<ShaderEffect> children[],size_t childCount,const Matrix * localMatrix,bool isOpaque)106 std::shared_ptr<ShaderEffect> SkiaRuntimeEffect::MakeShader(std::shared_ptr<Data> uniforms,
107     std::shared_ptr<ShaderEffect> children[], size_t childCount, const Matrix* localMatrix,
108     bool isOpaque)
109 {
110     std::shared_ptr<ShaderEffect> shader = std::make_shared<ShaderEffect>();
111     if (skRuntimeEffect_ != nullptr) {
112         auto data = uniforms ? uniforms->GetImpl<SkiaData>()->GetSkData() : SkData::MakeEmpty();
113         sk_sp<SkShader> skChildren[childCount];
114         for (size_t i = 0; i < childCount; ++i) {
115             auto skShaderImpl = children[i]->GetImpl<SkiaShaderEffect>();
116             if (skShaderImpl) {
117                 skChildren[i] = sk_sp<SkShader>(skShaderImpl->GetShader());
118             }
119         }
120 #ifdef USE_M133_SKIA
121         sk_sp<SkShader> skShader = skRuntimeEffect_->makeShader(data, skChildren,
122             childCount, localMatrix ? &localMatrix->GetImpl<SkiaMatrix>()->ExportSkiaMatrix() : nullptr);
123 #else
124         sk_sp<SkShader> skShader = skRuntimeEffect_->makeShader(data, skChildren,
125             childCount, localMatrix ? &localMatrix->GetImpl<SkiaMatrix>()->ExportSkiaMatrix() : nullptr,
126             isOpaque);
127 #endif
128         shader->GetImpl<SkiaShaderEffect>()->SetSkShader(skShader);
129     }
130     return shader;
131 }
132 
GetRuntimeEffect() const133 sk_sp<SkRuntimeEffect> SkiaRuntimeEffect::GetRuntimeEffect() const
134 {
135     return skRuntimeEffect_;
136 }
137 
SetRuntimeEffect(const sk_sp<SkRuntimeEffect> & skRuntimeEffect)138 void SkiaRuntimeEffect::SetRuntimeEffect(const sk_sp<SkRuntimeEffect>& skRuntimeEffect)
139 {
140     skRuntimeEffect_ = skRuntimeEffect;
141 }
142 } // namespace Drawing
143 } // namespace Rosen
144 } // namespace OHOS