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
GetPropertyId() const30 PropertyId RSRenderPropertyAnimation::GetPropertyId() const
31 {
32 return propertyId_;
33 }
34
SetAdditive(bool isAdditive)35 void RSRenderPropertyAnimation::SetAdditive(bool isAdditive)
36 {
37 if (IsStarted()) {
38 ROSEN_LOGE("Failed to set additive, animation has started!");
39 return;
40 }
41
42 isAdditive_ = isAdditive;
43 }
44
GetAdditive()45 bool RSRenderPropertyAnimation::GetAdditive()
46 {
47 return isAdditive_;
48 }
49
AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase> & property)50 void RSRenderPropertyAnimation::AttachRenderProperty(const std::shared_ptr<RSRenderPropertyBase>& property)
51 {
52 property_ = property;
53 if (property_ == nullptr) {
54 return;
55 }
56 InitValueEstimator();
57 if (originValue_ != nullptr) {
58 property_->SetPropertyType(originValue_->GetPropertyType());
59 }
60 }
61
Marshalling(Parcel & parcel) const62 bool RSRenderPropertyAnimation::Marshalling(Parcel& parcel) const
63 {
64 if (!RSRenderAnimation::Marshalling(parcel)) {
65 ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, RenderAnimation failed");
66 return false;
67 }
68 if (!parcel.WriteUint64(propertyId_)) {
69 ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write PropertyId failed");
70 return false;
71 }
72 if (!(RSMarshallingHelper::Marshalling(parcel, isAdditive_) &&
73 RSRenderPropertyBase::Marshalling(parcel, originValue_))) {
74 ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write value failed");
75 return false;
76 }
77 return true;
78 }
79
ParseParam(Parcel & parcel)80 bool RSRenderPropertyAnimation::ParseParam(Parcel& parcel)
81 {
82 if (!RSRenderAnimation::ParseParam(parcel)) {
83 ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, RenderAnimation failed");
84 return false;
85 }
86
87 if (!(parcel.ReadUint64(propertyId_) && RSMarshallingHelper::Unmarshalling(parcel, isAdditive_))) {
88 ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, Unmarshalling failed");
89 return false;
90 }
91 if (!RSRenderPropertyBase::Unmarshalling(parcel, originValue_)) {
92 return false;
93 }
94 lastValue_ = originValue_->Clone();
95
96 return true;
97 }
98
SetPropertyValue(const std::shared_ptr<RSRenderPropertyBase> & value)99 void RSRenderPropertyAnimation::SetPropertyValue(const std::shared_ptr<RSRenderPropertyBase>& value)
100 {
101 if (property_ != nullptr) {
102 property_->SetValue(value);
103 }
104 }
105
GetPropertyValue() const106 const std::shared_ptr<RSRenderPropertyBase> RSRenderPropertyAnimation::GetPropertyValue() const
107 {
108 if (property_ != nullptr) {
109 return property_->Clone();
110 }
111
112 return lastValue_->Clone();
113 }
114
GetOriginValue() const115 const std::shared_ptr<RSRenderPropertyBase>& RSRenderPropertyAnimation::GetOriginValue() const
116 {
117 return originValue_;
118 }
119
GetLastValue() const120 const std::shared_ptr<RSRenderPropertyBase>& RSRenderPropertyAnimation::GetLastValue() const
121 {
122 return lastValue_;
123 }
124
SetAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & value)125 void RSRenderPropertyAnimation::SetAnimationValue(const std::shared_ptr<RSRenderPropertyBase>& value)
126 {
127 SetPropertyValue(GetAnimationValue(value));
128 }
129
GetAnimationValue(const std::shared_ptr<RSRenderPropertyBase> & value)130 const std::shared_ptr<RSRenderPropertyBase> RSRenderPropertyAnimation::GetAnimationValue(
131 const std::shared_ptr<RSRenderPropertyBase>& value)
132 {
133 std::shared_ptr<RSRenderPropertyBase> animationValue;
134 if (GetAdditive()) {
135 animationValue = GetPropertyValue() + value - lastValue_;
136 } else {
137 animationValue = value->Clone();
138 }
139
140 lastValue_ = value->Clone();
141 return animationValue;
142 }
143
OnRemoveOnCompletion()144 void RSRenderPropertyAnimation::OnRemoveOnCompletion()
145 {
146 std::shared_ptr<RSRenderPropertyBase> backwardValue;
147 if (GetAdditive()) {
148 backwardValue = GetPropertyValue() + GetOriginValue() - lastValue_;
149 } else {
150 backwardValue = GetOriginValue();
151 }
152
153 SetPropertyValue(backwardValue);
154 }
155 } // namespace Rosen
156 } // namespace OHOS
157