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 "core/components_ng/svg/parse/svg_fe_flood.h"
17 
18 #include "2d_graphics/include/effect/shader_effect.h"
19 
20 #include "base/utils/utils.h"
21 #include "core/components/declaration/svg/svg_fe_flood_declaration.h"
22 #include "core/components_ng/render/drawing.h"
23 
24 namespace OHOS::Ace::NG {
25 
Create()26 RefPtr<SvgNode> SvgFeFlood::Create()
27 {
28     return AceType::MakeRefPtr<SvgFeFlood>();
29 }
30 
SvgFeFlood()31 SvgFeFlood::SvgFeFlood() : SvgFe() {}
32 
OnAsImageFilter(std::shared_ptr<RSImageFilter> & imageFilter,const SvgColorInterpolationType & srcColor,SvgColorInterpolationType & currentColor,std::unordered_map<std::string,std::shared_ptr<RSImageFilter>> & resultHash) const33 void SvgFeFlood::OnAsImageFilter(std::shared_ptr<RSImageFilter>& imageFilter,
34     const SvgColorInterpolationType& srcColor, SvgColorInterpolationType& currentColor,
35     std::unordered_map<std::string, std::shared_ptr<RSImageFilter>>& resultHash) const
36 {
37     imageFilter = MakeImageFilter(feAttr_.in, imageFilter, resultHash);
38 
39     auto floodColor = feFloodAttr_.floodColor;
40     auto floodOpacity = feFloodAttr_.floodOpacity;
41 
42     floodColor = floodColor.ChangeOpacity(floodOpacity);
43     auto shaderFilter = RSRecordingShaderEffect::CreateColorShader(floodColor.GetValue());
44     CHECK_NULL_VOID(shaderFilter);
45     Rect effectFilterArea = effectFilterArea_;
46 
47     imageFilter = RSRecordingImageFilter::CreateShaderImageFilter(shaderFilter,
48         { effectFilterArea.Left(), effectFilterArea.Top(), effectFilterArea.Right(), effectFilterArea.Bottom() });
49 
50     ConverImageFilterColor(imageFilter, srcColor, currentColor);
51     RegisterResult(feAttr_.result, imageFilter, resultHash);
52 }
53 
ParseAndSetSpecializedAttr(const std::string & name,const std::string & value)54 bool SvgFeFlood::ParseAndSetSpecializedAttr(const std::string& name, const std::string& value)
55 {
56     static const LinearMapNode<void (*)(const std::string&, SvgFeFloodAttribute&)> attrs[] = {
57         { DOM_SVG_FE_FLOOD_COLOR,
58             [](const std::string& val, SvgFeFloodAttribute& attr) {
59                 attr.floodColor = SvgAttributesParser::GetColor(val);
60             } },
61         { DOM_SVG_FE_FLOOD_OPACITY,
62             [](const std::string& val, SvgFeFloodAttribute& attr) {
63                 attr.floodOpacity = SvgAttributesParser::ParseDouble(val);
64             } },
65     };
66     auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), name.c_str());
67     if (attrIter != -1) {
68         attrs[attrIter].value(value, feFloodAttr_);
69         return true;
70     }
71     return SvgFe::ParseAndSetSpecializedAttr(name, value);
72 }
73 
74 } // namespace OHOS::Ace::NG
75