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 "animation/rs_property_animation.h"
17
18 #include "animation/rs_animation_trace_utils.h"
19 #include "animation/rs_render_animation.h"
20 #include "modifier/rs_modifier.h"
21 #include "modifier/rs_property.h"
22 #include "ui/rs_node.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 static constexpr int NUMBER_FOR_HALF = 2;
27
RSPropertyAnimation(std::shared_ptr<RSPropertyBase> property)28 RSPropertyAnimation::RSPropertyAnimation(std::shared_ptr<RSPropertyBase> property) : property_(property)
29 {
30 InitAdditiveMode();
31 }
32
SetIsCustom(const bool isCustom)33 void RSPropertyAnimation::SetIsCustom(const bool isCustom)
34 {
35 isCustom_ = isCustom;
36 }
37
SetAdditive(bool isAdditive)38 void RSPropertyAnimation::SetAdditive(bool isAdditive)
39 {
40 isAdditive_ = isAdditive;
41 }
42
GetAdditive() const43 bool RSPropertyAnimation::GetAdditive() const
44 {
45 return isAdditive_;
46 }
47
GetOriginValue() const48 const std::shared_ptr<RSPropertyBase> RSPropertyAnimation::GetOriginValue() const
49 {
50 return originValue_;
51 }
52
SetPropertyValue(const std::shared_ptr<RSPropertyBase> & value)53 void RSPropertyAnimation::SetPropertyValue(const std::shared_ptr<RSPropertyBase>& value)
54 {
55 if (property_ != nullptr) {
56 property_->SetValue(value);
57 }
58 }
59
GetPropertyValue() const60 const std::shared_ptr<RSPropertyBase> RSPropertyAnimation::GetPropertyValue() const
61 {
62 if (property_ != nullptr) {
63 return property_->Clone();
64 }
65
66 return nullptr;
67 }
68
GetPropertyId() const69 PropertyId RSPropertyAnimation::GetPropertyId() const
70 {
71 if (property_ != nullptr) {
72 return property_->GetId();
73 }
74
75 return {};
76 }
77
OnStart()78 void RSPropertyAnimation::OnStart()
79 {
80 if (!hasOriginValue_) {
81 originValue_ = GetPropertyValue();
82 hasOriginValue_ = true;
83 }
84 InitInterpolationValue();
85 }
86
SetOriginValue(const std::shared_ptr<RSPropertyBase> & originValue)87 void RSPropertyAnimation::SetOriginValue(const std::shared_ptr<RSPropertyBase>& originValue)
88 {
89 if (!hasOriginValue_ && originValue != nullptr) {
90 originValue_ = originValue->Clone();
91 hasOriginValue_ = true;
92 }
93 }
94
InitInterpolationValue()95 void RSPropertyAnimation::InitInterpolationValue()
96 {
97 if (isDelta_) {
98 startValue_ = originValue_->Clone();
99 endValue_ = originValue_ + byValue_;
100 } else {
101 byValue_ = endValue_ - startValue_;
102 }
103 }
104
OnUpdateStagingValue(bool isFirstStart)105 void RSPropertyAnimation::OnUpdateStagingValue(bool isFirstStart)
106 {
107 auto startValue = startValue_;
108 auto endValue = endValue_;
109 if (!GetDirection()) {
110 std::swap(startValue, endValue);
111 }
112 auto byValue = endValue - startValue;
113 auto targetValue = endValue;
114 if (isFirstStart) {
115 if (GetAutoReverse() && GetRepeatCount() % NUMBER_FOR_HALF == 0) {
116 targetValue = startValue;
117 } else {
118 targetValue = endValue;
119 }
120 } else {
121 auto currentValue = GetPropertyValue();
122 if (GetAutoReverse() && GetRepeatCount() % NUMBER_FOR_HALF == 0) {
123 targetValue = IsReversed() ? currentValue + byValue : currentValue - byValue;
124 } else {
125 targetValue = IsReversed() ? currentValue - byValue : currentValue + byValue;
126 }
127 }
128
129 SetPropertyValue(targetValue);
130 }
131
UpdateStagingValueOnInteractiveFinish(RSInteractiveAnimationPosition pos)132 void RSPropertyAnimation::UpdateStagingValueOnInteractiveFinish(RSInteractiveAnimationPosition pos)
133 {
134 auto targetValue = endValue_;
135 if (pos ==RSInteractiveAnimationPosition::START) {
136 targetValue = startValue_;
137 } else if (pos ==RSInteractiveAnimationPosition::END) {
138 targetValue = endValue_;
139 } else if (pos ==RSInteractiveAnimationPosition::CURRENT) {
140 if (IsUiAnimation() && property_ != nullptr) {
141 property_->SetValueFromRender(property_->GetRenderProperty());
142 return;
143 }
144 }
145 SetPropertyValue(targetValue);
146 }
147
SetPropertyOnAllAnimationFinish()148 void RSPropertyAnimation::SetPropertyOnAllAnimationFinish()
149 {
150 if (property_ != nullptr) {
151 property_->UpdateOnAllAnimationFinish();
152 }
153 }
154
InitAdditiveMode()155 void RSPropertyAnimation::InitAdditiveMode()
156 {
157 if (property_ == nullptr) {
158 return;
159 }
160
161 switch (property_->type_) {
162 case RSModifierType::QUATERNION:
163 SetAdditive(false);
164 break;
165 default:
166 break;
167 }
168 }
169
DumpAnimationInfo(std::string & dumpInfo) const170 void RSPropertyAnimation::DumpAnimationInfo(std::string& dumpInfo) const
171 {
172 dumpInfo += ", isCustom:" + std::to_string(isCustom_);
173 RSRenderPropertyType type = RSRenderPropertyType::INVALID;
174 if (property_) {
175 type = property_->GetPropertyType();
176 dumpInfo += ", ModifierType: " + std::to_string(static_cast<int16_t>(property_->type_));
177 }
178 if (startValue_) {
179 dumpInfo += ", StartValue: " +
180 RSAnimationTraceUtils::GetInstance().ParseRenderPropertyVaule(startValue_->GetRenderProperty(), type);
181 }
182 if (endValue_) {
183 dumpInfo += ", EndValue: " +
184 RSAnimationTraceUtils::GetInstance().ParseRenderPropertyVaule(endValue_->GetRenderProperty(), type);
185 }
186 }
187 } // namespace Rosen
188 } // namespace OHOS
189