• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "animation/rs_render_property_animation.h"
17 
18 #include "modifier/rs_render_property.h"
19 #include "pipeline/rs_canvas_render_node.h"
20 #include "platform/common/rs_log.h"
21 #include "transaction/rs_marshalling_helper.h"
22 
23 namespace OHOS {
24 namespace Rosen {
RSRenderPropertyAnimation(AnimationId id,const PropertyId & propertyId,const std::shared_ptr<RSRenderPropertyBase> & originValue)25 RSRenderPropertyAnimation::RSRenderPropertyAnimation(AnimationId id, const PropertyId& propertyId,
26     const std::shared_ptr<RSRenderPropertyBase>& originValue) : RSRenderAnimation(id), propertyId_(propertyId),
27     originValue_(originValue->Clone()), lastValue_(originValue->Clone())
28 {}
29 
DumpAnimationType(std::string & out) const30 void RSRenderPropertyAnimation::DumpAnimationType(std::string& out) const
31 {
32     out += "Type:RSRenderPropertyAnimation";
33 }
34 
GetPropertyId() const35 PropertyId RSRenderPropertyAnimation::GetPropertyId() const
36 {
37     return propertyId_;
38 }
39 
SetAdditive(bool isAdditive)40 void RSRenderPropertyAnimation::SetAdditive(bool isAdditive)
41 {
42     if (IsStarted()) {
43         ROSEN_LOGE("Failed to set additive, animation has started!");
44         return;
45     }
46 
47     isAdditive_ = isAdditive;
48 }
49 
GetAdditive()50 bool RSRenderPropertyAnimation::GetAdditive()
51 {
52     return isAdditive_;
53 }
54 
AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase> & property)55 void RSRenderPropertyAnimation::AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase>& property)
56 {
57     property_ = property;
58     if (property_ == nullptr) {
59         return;
60     }
61     InitValueEstimator();
62     if (originValue_ != nullptr) {
63         property_->SetPropertyType(originValue_->GetPropertyType());
64     }
65 }
66 
Marshalling(Parcel & parcel) const67 bool RSRenderPropertyAnimation::Marshalling(Parcel& parcel) const
68 {
69     if (!RSRenderAnimation::Marshalling(parcel)) {
70         ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, RenderAnimation failed");
71         return false;
72     }
73     if (!parcel.WriteUint64(propertyId_)) {
74         ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write PropertyId failed");
75         return false;
76     }
77     if (!(RSMarshallingHelper::Marshalling(parcel, isAdditive_) &&
78             RSRenderPropertyBase::Marshalling(parcel, originValue_))) {
79         ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write value failed");
80         return false;
81     }
82     return true;
83 }
84 
ParseParam(Parcel & parcel)85 bool RSRenderPropertyAnimation::ParseParam(Parcel& parcel)
86 {
87     if (!RSRenderAnimation::ParseParam(parcel)) {
88         ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, RenderAnimation failed");
89         return false;
90     }
91 
92     if (!(parcel.ReadUint64(propertyId_) && RSMarshallingHelper::Unmarshalling(parcel, isAdditive_))) {
93         ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, Unmarshalling failed");
94         return false;
95     }
96     if (!RSRenderPropertyBase::Unmarshalling(parcel, originValue_)) {
97         return false;
98     }
99     if (originValue_ == nullptr) {
100         ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, originValue_ is nullptr!");
101         return false;
102     }
103     lastValue_ = originValue_->Clone();
104 
105     return true;
106 }
107 
SetPropertyValue(const std::shared_ptr<RSRenderPropertyBase> & value)108 void RSRenderPropertyAnimation::SetPropertyValue(const std::shared_ptr<RSRenderPropertyBase>& value)
109 {
110     if (property_ != nullptr) {
111         property_->SetValue(value);
112     }
113 }
114 
GetPropertyValue() const115 const std::shared_ptr<RSRenderPropertyBase> RSRenderPropertyAnimation::GetPropertyValue() const
116 {
117     if (property_ != nullptr) {
118         return property_->Clone();
119     }
120 
121     return lastValue_->Clone();
122 }
123 
GetOriginValue() const124 const std::shared_ptr<RSRenderPropertyBase>& RSRenderPropertyAnimation::GetOriginValue() const
125 {
126     return originValue_;
127 }
128 
GetLastValue() const129 const std::shared_ptr<RSRenderPropertyBase>& RSRenderPropertyAnimation::GetLastValue() const
130 {
131     return lastValue_;
132 }
133 
SetAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & value)134 void RSRenderPropertyAnimation::SetAnimationValue(const std::shared_ptr<RSRenderPropertyBase>& value)
135 {
136     SetPropertyValue(GetAnimationValue(value));
137 }
138 
GetAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & value)139 const std::shared_ptr<RSRenderPropertyBase> RSRenderPropertyAnimation::GetAnimationValue(
140     const std::shared_ptr<RSRenderPropertyBase>& value)
141 {
142     std::shared_ptr<RSRenderPropertyBase> animationValue;
143     if (GetAdditive()) {
144         animationValue = GetPropertyValue() + value - lastValue_;
145     } else {
146         animationValue = value->Clone();
147     }
148 
149     lastValue_ = value->Clone();
150     return animationValue;
151 }
152 
OnRemoveOnCompletion()153 void RSRenderPropertyAnimation::OnRemoveOnCompletion()
154 {
155     std::shared_ptr<RSRenderPropertyBase> backwardValue;
156     if (GetAdditive()) {
157         backwardValue = GetPropertyValue() + GetOriginValue() - lastValue_;
158     } else {
159         backwardValue = GetOriginValue();
160     }
161 
162     SetPropertyValue(backwardValue);
163 }
164 
RecordLastAnimateValue()165 void RSRenderPropertyAnimation::RecordLastAnimateValue()
166 {
167     if (!RSRenderAnimation::isCalcAnimateVelocity_) {
168         return;
169     }
170     animateVelocity_.reset();
171     lastAnimateValue_.reset();
172     if (property_ != nullptr) {
173         lastAnimateValue_ = property_->Clone();
174     }
175 }
176 
UpdateAnimateVelocity(float frameInterval)177 void RSRenderPropertyAnimation::UpdateAnimateVelocity(float frameInterval)
178 {
179     if (!RSRenderAnimation::isCalcAnimateVelocity_ ||
180         !lastAnimateValue_ || !property_ || ROSEN_EQ<float>(frameInterval, 0)) {
181         return;
182     }
183     if (property_->GetPropertyUnit() > RSPropertyUnit::UNKNOWN) {
184         auto currAnimateValue = property_->Clone();
185         animateVelocity_ = (currAnimateValue - lastAnimateValue_) * (1 / frameInterval);
186     }
187 }
188 } // namespace Rosen
189 } // namespace OHOS
190