• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROPERTY_ANIMATION_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROPERTY_ANIMATION_H
18 
19 #include "animation/rs_property_accessors.h"
20 #include "animation/rs_render_animation.h"
21 #include "common/rs_common_def.h"
22 #include "pipeline/rs_canvas_render_node.h"
23 #include "platform/common/rs_log.h"
24 #include "transaction/rs_marshalling_helper.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 template<typename T>
29 class RSRenderPropertyAnimation : public RSRenderAnimation {
30 public:
31     virtual ~RSRenderPropertyAnimation() = default;
32 
GetProperty()33     RSAnimatableProperty GetProperty() const override
34     {
35         return property_;
36     }
37 
SetAdditive(bool isAdditive)38     void SetAdditive(bool isAdditive)
39     {
40         if (IsStarted()) {
41             ROSEN_LOGE("Failed to set additive, animation has started!");
42             return;
43         }
44 
45         isAdditive_ = isAdditive;
46     }
47 
GetAdditive()48     bool GetAdditive()
49     {
50         return isAdditive_;
51     }
52 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel)53     bool Marshalling(Parcel& parcel) const override
54     {
55         if (!RSRenderAnimation::Marshalling(parcel)) {
56             ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, RenderAnimation failed");
57             return false;
58         }
59         if (!parcel.WriteInt32(static_cast<std::underlying_type<RSAnimatableProperty>::type>(property_))) {
60             ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write type failed");
61             return false;
62         }
63         if (!(RSMarshallingHelper::Marshalling(parcel, originValue_) &&
64                 RSMarshallingHelper::Marshalling(parcel, isAdditive_))) {
65             ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write value failed");
66             return false;
67         }
68         return true;
69     }
70 #endif
71 protected:
RSRenderPropertyAnimation(AnimationId id,const RSAnimatableProperty & property,const T & originValue)72     RSRenderPropertyAnimation(AnimationId id, const RSAnimatableProperty& property, const T& originValue)
73         : RSRenderAnimation(id), property_(property), originValue_(originValue), lastValue_(originValue)
74     {
75         propertyAccessor_ = std::static_pointer_cast<RSPropertyAccessors<T>>(
76             RSBasePropertyAccessors::GetAccessor(property));
77     }
78     RSRenderPropertyAnimation() =default;
79 #ifdef ROSEN_OHOS
ParseParam(Parcel & parcel)80     bool ParseParam(Parcel& parcel) override
81     {
82         if (!RSRenderAnimation::ParseParam(parcel)) {
83             ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, RenderAnimation failed");
84             return false;
85         }
86 
87         int32_t property = 0;
88         if (!(parcel.ReadInt32(property) && RSMarshallingHelper::Unmarshalling(parcel, originValue_) &&
89                 RSMarshallingHelper::Unmarshalling(parcel, isAdditive_))) {
90             ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, Unmarshalling failed");
91             return false;
92         }
93         lastValue_ = originValue_;
94         property_ = static_cast<RSAnimatableProperty>(property);
95         propertyAccessor_ = std::static_pointer_cast<RSPropertyAccessors<T>>(
96             RSBasePropertyAccessors::GetAccessor(property_));
97 
98         return true;
99     }
100 #endif
SetPropertyValue(const T & value)101     void SetPropertyValue(const T& value)
102     {
103         auto target = GetTarget();
104         if (target == nullptr || propertyAccessor_->getter_ == nullptr) {
105             ROSEN_LOGE("Failed to set property value, target is null!");
106             return;
107         }
108         (target->GetMutableRenderProperties().*propertyAccessor_->setter_)(value);
109     }
110 
GetPropertyValue()111     auto GetPropertyValue() const
112     {
113         auto target = GetTarget();
114         if (target == nullptr || propertyAccessor_->getter_ == nullptr) {
115             ROSEN_LOGE("Failed to get property value, target is null!");
116             return lastValue_;
117         }
118 
119         return (target->GetRenderProperties().*propertyAccessor_->getter_)();
120     }
121 
GetOriginValue()122     auto GetOriginValue() const
123     {
124         return originValue_;
125     }
126 
GetLastValue()127     auto GetLastValue() const
128     {
129         return lastValue_;
130     }
131 
SetAnimationValue(const T & value)132     void SetAnimationValue(const T& value)
133     {
134         T animationValue;
135         if (GetAdditive()) {
136             animationValue = GetPropertyValue() + value - lastValue_;
137         } else {
138             animationValue = value;
139         }
140 
141         lastValue_ = value;
142         SetPropertyValue(animationValue);
143     }
144 
OnRemoveOnCompletion()145     void OnRemoveOnCompletion() override
146     {
147         T backwardValue;
148         if (GetAdditive()) {
149             backwardValue = GetPropertyValue() + GetOriginValue() - lastValue_;
150         } else {
151             backwardValue = GetOriginValue();
152         }
153 
154         SetPropertyValue(backwardValue);
155     }
156 
157 private:
158     RSAnimatableProperty property_ { RSAnimatableProperty::INVALID };
159     T originValue_;
160     T lastValue_;
161     bool isAdditive_ { true };
162     std::shared_ptr<RSPropertyAccessors<T>> propertyAccessor_;
163 };
164 } // namespace Rosen
165 } // namespace OHOS
166 
167 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROPERTY_ANIMATION_H
168