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 "modifier/rs_showing_properties_freezer.h"
17
18 #include <securec.h>
19
20 #include "modifier/rs_modifier_type.h"
21 #include "modifier/rs_property_modifier.h"
22 #include "pipeline/rs_node_map.h"
23 #include "ui/rs_node.h"
24 #include "platform/common/rs_log.h"
25
26 namespace OHOS {
27 namespace Rosen {
RSShowingPropertiesFreezer(NodeId id,std::shared_ptr<RSUIContext> rsUIContext)28 RSShowingPropertiesFreezer::RSShowingPropertiesFreezer(NodeId id, std::shared_ptr<RSUIContext> rsUIContext)
29 : id_(id), rsUIContext_(rsUIContext)
30 {}
31
32 template<typename T, RSModifierType Type>
GetPropertyImpl() const33 std::optional<T> RSShowingPropertiesFreezer::GetPropertyImpl() const
34 {
35 auto node = rsUIContext_.lock() ? rsUIContext_.lock()->GetNodeMap().GetNode<RSNode>(id_)
36 : RSNodeMap::Instance().GetNode<RSNode>(id_);
37 if (node == nullptr) {
38 return std::nullopt;
39 }
40 std::unique_lock<std::recursive_mutex> lock(node->propertyMutex_);
41 auto iter = node->propertyModifiers_.find(Type);
42 if (iter == node->propertyModifiers_.end()) {
43 ROSEN_LOGE(
44 "RSShowingPropertiesFreezer::GetPropertyImpl Type %{public}d failed, no such property!",
45 static_cast<int>(Type));
46 return std::nullopt;
47 }
48 auto property = std::static_pointer_cast<RSAnimatableProperty<T>>(iter->second->GetProperty());
49 if (property == nullptr) {
50 ROSEN_LOGE(
51 "RSShowingPropertiesFreezer::GetPropertyImpl Type %{public}d failed, property is null!",
52 static_cast<int>(Type));
53 return std::nullopt;
54 }
55 bool success = property->GetShowingValueAndCancelAnimation();
56 if (!success) {
57 ROSEN_LOGE("RSShowingPropertiesFreezer::GetPropertyImpl Type %{public}d failed, cancel animation failed!",
58 static_cast<int>(Type));
59 return std::nullopt;
60 }
61 return property->Get();
62 }
63
GetBounds() const64 std::optional<Vector4f> RSShowingPropertiesFreezer::GetBounds() const
65 {
66 return GetPropertyImpl<Vector4f, RSModifierType::BOUNDS>();
67 }
68
GetFrame() const69 std::optional<Vector4f> RSShowingPropertiesFreezer::GetFrame() const
70 {
71 return GetPropertyImpl<Vector4f, RSModifierType::FRAME>();
72 }
73
GetPositionZ() const74 std::optional<float> RSShowingPropertiesFreezer::GetPositionZ() const
75 {
76 return GetPropertyImpl<float, RSModifierType::POSITION_Z>();
77 }
78
GetPivot() const79 std::optional<Vector2f> RSShowingPropertiesFreezer::GetPivot() const
80 {
81 return GetPropertyImpl<Vector2f, RSModifierType::PIVOT>();
82 }
83
GetPivotZ() const84 std::optional<float> RSShowingPropertiesFreezer::GetPivotZ() const
85 {
86 return GetPropertyImpl<float, RSModifierType::PIVOT_Z>();
87 }
88
GetQuaternion() const89 std::optional<Quaternion> RSShowingPropertiesFreezer::GetQuaternion() const
90 {
91 return GetPropertyImpl<Quaternion, RSModifierType::QUATERNION>();
92 }
93
GetRotation() const94 std::optional<float> RSShowingPropertiesFreezer::GetRotation() const
95 {
96 return GetPropertyImpl<float, RSModifierType::ROTATION>();
97 }
98
GetRotationX() const99 std::optional<float> RSShowingPropertiesFreezer::GetRotationX() const
100 {
101 return GetPropertyImpl<float, RSModifierType::ROTATION_X>();
102 }
103
GetRotationY() const104 std::optional<float> RSShowingPropertiesFreezer::GetRotationY() const
105 {
106 return GetPropertyImpl<float, RSModifierType::ROTATION_Y>();
107 }
108
GetCameraDistance() const109 std::optional<float> RSShowingPropertiesFreezer::GetCameraDistance() const
110 {
111 return GetPropertyImpl<float, RSModifierType::CAMERA_DISTANCE>();
112 }
113
GetTranslate() const114 std::optional<Vector2f> RSShowingPropertiesFreezer::GetTranslate() const
115 {
116 return GetPropertyImpl<Vector2f, RSModifierType::TRANSLATE>();
117 }
118
GetTranslateZ() const119 std::optional<float> RSShowingPropertiesFreezer::GetTranslateZ() const
120 {
121 return GetPropertyImpl<float, RSModifierType::TRANSLATE_Z>();
122 }
123
GetScale() const124 std::optional<Vector2f> RSShowingPropertiesFreezer::GetScale() const
125 {
126 return GetPropertyImpl<Vector2f, RSModifierType::SCALE>();
127 }
128
GetScaleZ() const129 std::optional<float> RSShowingPropertiesFreezer::GetScaleZ() const
130 {
131 return GetPropertyImpl<float, RSModifierType::SCALE_Z>();
132 }
133
GetSkew() const134 std::optional<Vector3f> RSShowingPropertiesFreezer::GetSkew() const
135 {
136 return GetPropertyImpl<Vector3f, RSModifierType::SKEW>();
137 }
138
GetPersp() const139 std::optional<Vector4f> RSShowingPropertiesFreezer::GetPersp() const
140 {
141 return GetPropertyImpl<Vector4f, RSModifierType::PERSP>();
142 }
143
GetAlpha() const144 std::optional<float> RSShowingPropertiesFreezer::GetAlpha() const
145 {
146 return GetPropertyImpl<float, RSModifierType::ALPHA>();
147 }
148
GetCornerRadius() const149 std::optional<Vector4f> RSShowingPropertiesFreezer::GetCornerRadius() const
150 {
151 return GetPropertyImpl<Vector4f, RSModifierType::CORNER_RADIUS>();
152 }
153
GetForegroundColor() const154 std::optional<Color> RSShowingPropertiesFreezer::GetForegroundColor() const
155 {
156 return GetPropertyImpl<Color, RSModifierType::FOREGROUND_COLOR>();
157 }
158
GetBackgroundColor() const159 std::optional<Color> RSShowingPropertiesFreezer::GetBackgroundColor() const
160 {
161 return GetPropertyImpl<Color, RSModifierType::BACKGROUND_COLOR>();
162 }
163
GetSurfaceBgColor() const164 std::optional<Color> RSShowingPropertiesFreezer::GetSurfaceBgColor() const
165 {
166 return GetPropertyImpl<Color, RSModifierType::SURFACE_BG_COLOR>();
167 }
168
GetBgImageWidth() const169 std::optional<float> RSShowingPropertiesFreezer::GetBgImageWidth() const
170 {
171 return GetPropertyImpl<float, RSModifierType::BG_IMAGE_WIDTH>();
172 }
173
GetBgImageHeight() const174 std::optional<float> RSShowingPropertiesFreezer::GetBgImageHeight() const
175 {
176 return GetPropertyImpl<float, RSModifierType::BG_IMAGE_HEIGHT>();
177 }
178
GetBgImagePositionX() const179 std::optional<float> RSShowingPropertiesFreezer::GetBgImagePositionX() const
180 {
181 return GetPropertyImpl<float, RSModifierType::BG_IMAGE_POSITION_X>();
182 }
183
GetBgImagePositionY() const184 std::optional<float> RSShowingPropertiesFreezer::GetBgImagePositionY() const
185 {
186 return GetPropertyImpl<float, RSModifierType::BG_IMAGE_POSITION_Y>();
187 }
188
GetBorderColor() const189 std::optional<Vector4<Color>> RSShowingPropertiesFreezer::GetBorderColor() const
190 {
191 return GetPropertyImpl<Vector4<Color>, RSModifierType::BORDER_COLOR>();
192 }
193
GetBorderWidth() const194 std::optional<Vector4f> RSShowingPropertiesFreezer::GetBorderWidth() const
195 {
196 return GetPropertyImpl<Vector4f, RSModifierType::BORDER_WIDTH>();
197 }
198
GetBackgroundFilter() const199 std::optional<std::shared_ptr<RSFilter>> RSShowingPropertiesFreezer::GetBackgroundFilter() const
200 {
201 return GetPropertyImpl<std::shared_ptr<RSFilter>, RSModifierType::BACKGROUND_FILTER>();
202 }
203
GetFilter() const204 std::optional<std::shared_ptr<RSFilter>> RSShowingPropertiesFreezer::GetFilter() const
205 {
206 return GetPropertyImpl<std::shared_ptr<RSFilter>, RSModifierType::FILTER>();
207 }
208
GetShadowColor() const209 std::optional<Color> RSShowingPropertiesFreezer::GetShadowColor() const
210 {
211 return GetPropertyImpl<Color, RSModifierType::SHADOW_COLOR>();
212 }
213
GetShadowOffsetX() const214 std::optional<float> RSShowingPropertiesFreezer::GetShadowOffsetX() const
215 {
216 return GetPropertyImpl<float, RSModifierType::SHADOW_OFFSET_X>();
217 }
218
GetShadowOffsetY() const219 std::optional<float> RSShowingPropertiesFreezer::GetShadowOffsetY() const
220 {
221 return GetPropertyImpl<float, RSModifierType::SHADOW_OFFSET_Y>();
222 }
223
GetShadowAlpha() const224 std::optional<float> RSShowingPropertiesFreezer::GetShadowAlpha() const
225 {
226 return GetPropertyImpl<float, RSModifierType::SHADOW_ALPHA>();
227 }
228
GetShadowElevation() const229 std::optional<float> RSShowingPropertiesFreezer::GetShadowElevation() const
230 {
231 return GetPropertyImpl<float, RSModifierType::SHADOW_ELEVATION>();
232 }
233
GetShadowRadius() const234 std::optional<float> RSShowingPropertiesFreezer::GetShadowRadius() const
235 {
236 return GetPropertyImpl<float, RSModifierType::SHADOW_RADIUS>();
237 }
238
GetSpherizeDegree() const239 std::optional<float> RSShowingPropertiesFreezer::GetSpherizeDegree() const
240 {
241 return GetPropertyImpl<float, RSModifierType::SPHERIZE>();
242 }
243
GetAttractionFractionValue() const244 std::optional<float> RSShowingPropertiesFreezer::GetAttractionFractionValue() const
245 {
246 return GetPropertyImpl<float, RSModifierType::ATTRACTION_FRACTION>();
247 }
248
GetAttractionDstPointValue() const249 std::optional<Vector2f> RSShowingPropertiesFreezer::GetAttractionDstPointValue() const
250 {
251 return GetPropertyImpl<Vector2f, RSModifierType::ATTRACTION_DSTPOINT>();
252 }
253
GetLightUpEffectDegree() const254 std::optional<float> RSShowingPropertiesFreezer::GetLightUpEffectDegree() const
255 {
256 return GetPropertyImpl<float, RSModifierType::LIGHT_UP_EFFECT>();
257 }
258
GetDynamicDimDegree() const259 std::optional<float> RSShowingPropertiesFreezer::GetDynamicDimDegree() const
260 {
261 return GetPropertyImpl<float, RSModifierType::DYNAMIC_DIM_DEGREE>();
262 }
263 } // namespace Rosen
264 } // namespace OHOS
265