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 } // namespace
32
SetRequestFrame(const std::function<void ()> & requestFrame)33 void RenderContext::SetRequestFrame(const std::function<void()>& requestFrame)
34 {
35 requestFrame_ = requestFrame;
36 }
37
RequestNextFrame() const38 void RenderContext::RequestNextFrame() const
39 {
40 if (requestFrame_) {
41 requestFrame_();
42 auto node = GetHost();
43 CHECK_NULL_VOID(node);
44 if (node->GetInspectorId().has_value()) {
45 auto pipeline = AceType::DynamicCast<PipelineContext>(PipelineBase::GetCurrentContext());
46 CHECK_NULL_VOID(pipeline);
47 pipeline->SetNeedRenderNode(WeakPtr<FrameNode>(node));
48 }
49 }
50 }
51
SetHostNode(const WeakPtr<FrameNode> & host)52 void RenderContext::SetHostNode(const WeakPtr<FrameNode>& host)
53 {
54 host_ = host;
55 }
56
GetHost() const57 RefPtr<FrameNode> RenderContext::GetHost() const
58 {
59 return host_.Upgrade();
60 }
61
GetUnsafeHost() const62 FrameNode* RenderContext::GetUnsafeHost() const
63 {
64 return UnsafeRawPtr(host_);
65 }
66
SetSharedTransitionOptions(const std::shared_ptr<SharedTransitionOption> & option)67 void RenderContext::SetSharedTransitionOptions(const std::shared_ptr<SharedTransitionOption>& option)
68 {
69 sharedTransitionOption_ = option;
70 }
71
GetSharedTransitionOption() const72 const std::shared_ptr<SharedTransitionOption>& RenderContext::GetSharedTransitionOption() const
73 {
74 return sharedTransitionOption_;
75 }
76
SetShareId(const ShareId & shareId)77 void RenderContext::SetShareId(const ShareId& shareId)
78 {
79 shareId_ = shareId;
80 }
81
GetShareId() const82 const ShareId& RenderContext::GetShareId() const
83 {
84 return shareId_;
85 }
86
HasSharedTransition() const87 bool RenderContext::HasSharedTransition() const
88 {
89 return !shareId_.empty();
90 }
91
HasSharedTransitionOption() const92 bool RenderContext::HasSharedTransitionOption() const
93 {
94 return sharedTransitionOption_ != nullptr;
95 }
96
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const97 void RenderContext::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
98 {
99 ACE_PROPERTY_TO_JSON_VALUE(propBorder_, BorderProperty);
100 ACE_PROPERTY_TO_JSON_VALUE(propOuterBorder_, OuterBorderProperty);
101 ACE_PROPERTY_TO_JSON_VALUE(propPointLight_, PointLightProperty);
102 ACE_PROPERTY_TO_JSON_VALUE(propBdImage_, BorderImageProperty);
103 ACE_PROPERTY_TO_JSON_VALUE(propOverlay_, OverlayProperty);
104 ACE_PROPERTY_TO_JSON_VALUE(propPositionProperty_, RenderPositionProperty);
105 ACE_PROPERTY_TO_JSON_VALUE(propBackground_, BackgroundProperty);
106 ACE_PROPERTY_TO_JSON_VALUE(propForeground_, ForegroundProperty);
107 ACE_PROPERTY_TO_JSON_VALUE(propGraphics_, GraphicsProperty);
108 ACE_PROPERTY_TO_JSON_VALUE(propGradient_, GradientProperty);
109 ACE_PROPERTY_TO_JSON_VALUE(propTransform_, TransformProperty);
110 ACE_PROPERTY_TO_JSON_VALUE(propClip_, ClipProperty);
111 ACE_PROPERTY_TO_JSON_VALUE(GetBackBlurStyle(), BlurStyleOption);
112 ACE_PROPERTY_TO_JSON_VALUE(GetBackgroundEffect(), EffectOption);
113 if (filter.IsFastFilter()) {
114 ObscuredToJsonValue(json, filter);
115 return;
116 }
117 if (propTransformMatrix_.has_value()) {
118 auto jsonValue = JsonUtil::Create(true);
119 jsonValue->Put("type", "matrix");
120 auto matrixString = propTransformMatrix_->ToString();
121 while (matrixString.find("\n") != std::string::npos) {
122 auto num = matrixString.find("\n");
123 matrixString.replace(num, 1, "");
124 }
125 jsonValue->Put("matrix", matrixString.c_str());
126 json->PutExtAttr("transform", jsonValue, filter);
127 } else {
128 json->PutExtAttr("transform", JsonUtil::Create(true), filter);
129 }
130 json->PutExtAttr("backgroundColor",
131 propBackgroundColor_.value_or(Color::TRANSPARENT).ColorToString().c_str(), filter);
132 json->PutExtAttr("zIndex", propZIndex_.value_or(0), filter);
133 json->PutExtAttr("opacity", propOpacity_.value_or(1), filter);
134 if (propProgressMask_.has_value() && propProgressMask_.value()) {
135 json->PutExtAttr("total", propProgressMask_.value()->GetMaxValue(), filter);
136 json->PutExtAttr("updateProgress", propProgressMask_.value()->GetValue(), filter);
137 json->PutExtAttr("updateColor", propProgressMask_.value()->GetColor().ColorToString().c_str(), filter);
138 json->PutExtAttr("enableBreathe", propProgressMask_.value()->GetEnableBreathe(), filter);
139 }
140 json->PutExtAttr("lightUpEffect", propLightUpEffect_.value_or(0.0), filter);
141 json->PutExtAttr("sphericalEffect", propSphericalEffect_.value_or(0.0), filter);
142 auto pixStretchEffectOption = propPixelStretchEffect_.value_or(PixStretchEffectOption());
143 auto pixelJsonValue = JsonUtil::Create(true);
144 pixelJsonValue->Put("left", pixStretchEffectOption.left.ToString().c_str());
145 pixelJsonValue->Put("right", pixStretchEffectOption.right.ToString().c_str());
146 pixelJsonValue->Put("top", pixStretchEffectOption.top.ToString().c_str());
147 pixelJsonValue->Put("bottom", pixStretchEffectOption.bottom.ToString().c_str());
148 json->PutExtAttr("pixelStretchEffect", pixelJsonValue, filter);
149 json->PutExtAttr("foregroundColor",
150 propForegroundColor_.value_or(Color::FOREGROUND).ColorToString().c_str(), filter);
151 if (propClickEffectLevel_.has_value()) {
152 auto clickEffectJsonValue = JsonUtil::Create(true);
153 clickEffectJsonValue->Put("level", std::to_string((int)propClickEffectLevel_.value().level).c_str());
154 clickEffectJsonValue->Put("scale", std::to_string((float)propClickEffectLevel_.value().scaleNumber).c_str());
155 json->PutExtAttr("clickEffect", clickEffectJsonValue, filter);
156 }
157 ObscuredToJsonValue(json, filter);
158 json->PutExtAttr("renderGroup", propRenderGroup_.value_or(false) ? "true" : "false", filter);
159 json->PutExtAttr("renderFit", RenderFitToString(propRenderFit_.value_or(RenderFit::TOP_LEFT)).c_str(), filter);
160 json->PutExtAttr("useShadowBatching", propUseShadowBatching_.value_or(false) ? "true" : "false", filter);
161 }
162
ObscuredToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const163 void RenderContext::ObscuredToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
164 {
165 /* no fixed attr below, just return */
166 if (filter.IsFastFilter()) {
167 return;
168 }
169 auto jsonObscuredArray = JsonUtil::CreateArray(true);
170 std::vector<ObscuredReasons> obscuredReasons = propObscured_.value_or(std::vector<ObscuredReasons>());
171 for (size_t i = 0; i < obscuredReasons.size(); i++) {
172 auto index = std::to_string(i);
173 auto value = std::to_string(static_cast<int32_t>(obscuredReasons[i]));
174 jsonObscuredArray->Put(index.c_str(), value.c_str());
175 }
176 json->PutExtAttr("obscured", jsonObscuredArray, filter);
177 }
178
FromJson(const std::unique_ptr<JsonValue> & json)179 void RenderContext::FromJson(const std::unique_ptr<JsonValue>& json)
180 {
181 auto borderRadius = json->GetValue("borderRadius");
182 BorderRadiusProperty brp;
183 brp.radiusTopLeft = Dimension::FromString(borderRadius->GetString("topLeft"));
184 brp.radiusTopRight = Dimension::FromString(borderRadius->GetString("topRight"));
185 brp.radiusBottomLeft = Dimension::FromString(borderRadius->GetString("bottomLeft"));
186 brp.radiusBottomRight = Dimension::FromString(borderRadius->GetString("bottomRight"));
187 UpdateBorderRadius(brp);
188 UpdateBackgroundColor(Color::ColorFromString(json->GetString("backgroundColor")));
189 auto clip = json->GetString("clip");
190 if (clip == "true" || clip == "false") {
191 UpdateClipEdge(clip == "true" ? true : false);
192 } else {
193 LOGE("UITree |ERROR| invalid clip=%{public}s", clip.c_str());
194 }
195 }
196 } // namespace OHOS::Ace::NG
197