1 /*
2  * Copyright (c) 2022 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/render/render_context.h"
17 
18 #include "core/pipeline_ng/pipeline_context.h"
19 
20 namespace OHOS::Ace::NG {
21 namespace {
RenderFitToString(RenderFit renderFit)22 std::string RenderFitToString(RenderFit renderFit)
23 {
24     static const std::string RenderFitStyles[] = { "RenderFit.CENTER", "RenderFit.TOP", "RenderFit.BOTTOM",
25         "RenderFit.LEFT", "RenderFit.RIGHT", "RenderFit.TOP_LEFT", "RenderFit.TOP_RIGHT", "RenderFit.BOTTOM_LEFT",
26         "RenderFit.BOTTOM_RIGHT", "RenderFit.RESIZE_FILL", "RenderFit.RESIZE_CONTAIN",
27         "RenderFit.RESIZE_CONTAIN_TOP_LEFT", "RenderFit.RESIZE_CONTAIN_BOTTOM_RIGHT", "RenderFit.RESIZE_COVER",
28         "RenderFit.RESIZE_COVER_TOP_LEFT", "RenderFit.RESIZE_COVER_BOTTOM_RIGHT" };
29     return RenderFitStyles[static_cast<int>(renderFit)];
30 }
31 
UseEffectTypeToString(EffectType effectType)32 std::string UseEffectTypeToString(EffectType effectType)
33 {
34     static const std::string UseEffectTypeStyles[] = { "EffectType.DEFAULT", "EffectType.WINDOW_EFFECT" };
35     return UseEffectTypeStyles[static_cast<int>(effectType)];
36 }
37 
38 } // namespace
39 
SetRequestFrame(const std::function<void ()> & requestFrame)40 void RenderContext::SetRequestFrame(const std::function<void()>& requestFrame)
41 {
42     requestFrame_ = requestFrame;
43 }
44 
RequestNextFrame() const45 void RenderContext::RequestNextFrame() const
46 {
47     if (requestFrame_) {
48         requestFrame_();
49         auto node = GetHost();
50         CHECK_NULL_VOID(node);
51         if (node->GetInspectorId().has_value()) {
52             auto pipeline = AceType::DynamicCast<PipelineContext>(PipelineBase::GetCurrentContext());
53             CHECK_NULL_VOID(pipeline);
54             pipeline->SetNeedRenderNode(WeakPtr<FrameNode>(node));
55         }
56     }
57 }
58 
SetHostNode(const WeakPtr<FrameNode> & host)59 void RenderContext::SetHostNode(const WeakPtr<FrameNode>& host)
60 {
61     host_ = host;
62 }
63 
GetHost() const64 RefPtr<FrameNode> RenderContext::GetHost() const
65 {
66     return host_.Upgrade();
67 }
68 
GetUnsafeHost() const69 FrameNode* RenderContext::GetUnsafeHost() const
70 {
71     return UnsafeRawPtr(host_);
72 }
73 
SetSharedTransitionOptions(const std::shared_ptr<SharedTransitionOption> & option)74 void RenderContext::SetSharedTransitionOptions(const std::shared_ptr<SharedTransitionOption>& option)
75 {
76     sharedTransitionOption_ = option;
77 }
78 
GetSharedTransitionOption() const79 const std::shared_ptr<SharedTransitionOption>& RenderContext::GetSharedTransitionOption() const
80 {
81     return sharedTransitionOption_;
82 }
83 
SetShareId(const ShareId & shareId)84 void RenderContext::SetShareId(const ShareId& shareId)
85 {
86     shareId_ = shareId;
87 }
88 
GetShareId() const89 const ShareId& RenderContext::GetShareId() const
90 {
91     return shareId_;
92 }
93 
HasSharedTransition() const94 bool RenderContext::HasSharedTransition() const
95 {
96     return !shareId_.empty();
97 }
98 
HasSharedTransitionOption() const99 bool RenderContext::HasSharedTransitionOption() const
100 {
101     return sharedTransitionOption_ != nullptr;
102 }
103 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const104 void RenderContext::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
105 {
106     ACE_PROPERTY_TO_JSON_VALUE(propBorder_, BorderProperty);
107     ACE_PROPERTY_TO_JSON_VALUE(propOuterBorder_, OuterBorderProperty);
108     ACE_PROPERTY_TO_JSON_VALUE(propPointLight_, PointLightProperty);
109     ACE_PROPERTY_TO_JSON_VALUE(propBdImage_, BorderImageProperty);
110     ACE_PROPERTY_TO_JSON_VALUE(propOverlay_, OverlayProperty);
111     ACE_PROPERTY_TO_JSON_VALUE(propPositionProperty_, RenderPositionProperty);
112     ACE_PROPERTY_TO_JSON_VALUE(propBackground_, BackgroundProperty);
113     ACE_PROPERTY_TO_JSON_VALUE(propForeground_, ForegroundProperty);
114     ACE_PROPERTY_TO_JSON_VALUE(propGraphics_, GraphicsProperty);
115     ACE_PROPERTY_TO_JSON_VALUE(propGradient_, GradientProperty);
116     ACE_PROPERTY_TO_JSON_VALUE(propTransform_, TransformProperty);
117     ACE_PROPERTY_TO_JSON_VALUE(propClip_, ClipProperty);
118     ACE_PROPERTY_TO_JSON_VALUE(GetBackBlurStyle(), BlurStyleOption);
119     ACE_PROPERTY_TO_JSON_VALUE(GetBackgroundEffect(), EffectOption);
120     if (filter.IsFastFilter()) {
121         ObscuredToJsonValue(json, filter);
122         return;
123     }
124     if (propTransformMatrix_.has_value()) {
125         auto jsonValue = JsonUtil::Create(true);
126         jsonValue->Put("type", "matrix");
127         auto matrixString = propTransformMatrix_->ToString();
128         while (matrixString.find("\n") != std::string::npos) {
129             auto num = matrixString.find("\n");
130             matrixString.replace(num, 1, "");
131         }
132         jsonValue->Put("matrix", matrixString.c_str());
133         json->PutExtAttr("transform", jsonValue, filter);
134     } else {
135         json->PutExtAttr("transform", JsonUtil::Create(true), filter);
136     }
137     json->PutExtAttr("backgroundColor",
138         propBackgroundColor_.value_or(Color::TRANSPARENT).ColorToString().c_str(), filter);
139     json->PutExtAttr("zIndex", propZIndex_.value_or(0), filter);
140     json->PutExtAttr("opacity", propOpacity_.value_or(1), filter);
141     if (propProgressMask_.has_value() && propProgressMask_.value()) {
142         json->PutExtAttr("total", propProgressMask_.value()->GetMaxValue(), filter);
143         json->PutExtAttr("updateProgress", propProgressMask_.value()->GetValue(), filter);
144         json->PutExtAttr("updateColor", propProgressMask_.value()->GetColor().ColorToString().c_str(), filter);
145         json->PutExtAttr("enableBreathe", propProgressMask_.value()->GetEnableBreathe(), filter);
146     }
147     json->PutExtAttr("lightUpEffect", propLightUpEffect_.value_or(0.0), filter);
148     json->PutExtAttr("sphericalEffect", propSphericalEffect_.value_or(0.0), filter);
149     auto pixStretchEffectOption = propPixelStretchEffect_.value_or(PixStretchEffectOption());
150     auto pixelJsonValue = JsonUtil::Create(true);
151     pixelJsonValue->Put("left", pixStretchEffectOption.left.ToString().c_str());
152     pixelJsonValue->Put("right", pixStretchEffectOption.right.ToString().c_str());
153     pixelJsonValue->Put("top", pixStretchEffectOption.top.ToString().c_str());
154     pixelJsonValue->Put("bottom", pixStretchEffectOption.bottom.ToString().c_str());
155     json->PutExtAttr("pixelStretchEffect", pixelJsonValue, filter);
156     json->PutExtAttr("foregroundColor",
157         propForegroundColor_.value_or(Color::FOREGROUND).ColorToString().c_str(), filter);
158     if (propClickEffectLevel_.has_value()) {
159         auto clickEffectJsonValue = JsonUtil::Create(true);
160         clickEffectJsonValue->Put("level", std::to_string((int)propClickEffectLevel_.value().level).c_str());
161         clickEffectJsonValue->Put("scale", std::to_string((float)propClickEffectLevel_.value().scaleNumber).c_str());
162         json->PutExtAttr("clickEffect", clickEffectJsonValue, filter);
163     }
164     ObscuredToJsonValue(json, filter);
165     json->PutExtAttr("renderGroup", propRenderGroup_.value_or(false) ? "true" : "false", filter);
166     json->PutExtAttr("renderFit", RenderFitToString(propRenderFit_.value_or(RenderFit::TOP_LEFT)).c_str(), filter);
167     json->PutExtAttr("useShadowBatching", propUseShadowBatching_.value_or(false) ? "true" : "false", filter);
168     json->PutExtAttr("useEffect", propUseEffect_.value_or(false) ? "true" : "false", filter);
169     json->PutExtAttr("useEffectType",
170         UseEffectTypeToString(propUseEffectType_.value_or(EffectType::DEFAULT)).c_str(), filter);
171 }
172 
ObscuredToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const173 void RenderContext::ObscuredToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
174 {
175     /* no fixed attr below, just return */
176     if (filter.IsFastFilter()) {
177         return;
178     }
179     auto jsonObscuredArray = JsonUtil::CreateArray(true);
180     std::vector<ObscuredReasons> obscuredReasons = propObscured_.value_or(std::vector<ObscuredReasons>());
181     for (size_t i = 0; i < obscuredReasons.size(); i++) {
182         auto index = std::to_string(i);
183         auto value = std::to_string(static_cast<int32_t>(obscuredReasons[i]));
184         jsonObscuredArray->Put(index.c_str(), value.c_str());
185     }
186     json->PutExtAttr("obscured", jsonObscuredArray, filter);
187 }
188 
FromJson(const std::unique_ptr<JsonValue> & json)189 void RenderContext::FromJson(const std::unique_ptr<JsonValue>& json)
190 {
191     auto borderRadius = json->GetValue("borderRadius");
192     BorderRadiusProperty brp;
193     brp.radiusTopLeft = Dimension::FromString(borderRadius->GetString("topLeft"));
194     brp.radiusTopRight = Dimension::FromString(borderRadius->GetString("topRight"));
195     brp.radiusBottomLeft = Dimension::FromString(borderRadius->GetString("bottomLeft"));
196     brp.radiusBottomRight = Dimension::FromString(borderRadius->GetString("bottomRight"));
197     UpdateBorderRadius(brp);
198     UpdateBackgroundColor(Color::ColorFromString(json->GetString("backgroundColor")));
199     auto clip = json->GetString("clip");
200     if (clip == "true" || clip == "false") {
201         UpdateClipEdge(clip == "true" ? true : false);
202     } else {
203         LOGE("UITree |ERROR| invalid clip=%{public}s", clip.c_str());
204     }
205 }
206 } // namespace OHOS::Ace::NG
207