• 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_keyframe_animation.h"
17 
18 #include "animation/rs_interpolator.h"
19 #include "animation/rs_value_estimator.h"
20 #include "platform/common/rs_log.h"
21 #include "transaction/rs_marshalling_helper.h"
22 
23 namespace OHOS {
24 namespace Rosen {
RSRenderKeyframeAnimation(AnimationId id,const PropertyId & propertyId,const std::shared_ptr<RSRenderPropertyBase> & originValue)25 RSRenderKeyframeAnimation::RSRenderKeyframeAnimation(AnimationId id, const PropertyId& propertyId,
26     const std::shared_ptr<RSRenderPropertyBase>& originValue)
27     : RSRenderPropertyAnimation(id, propertyId, originValue)
28 {}
29 
AddKeyframe(float fraction,const std::shared_ptr<RSRenderPropertyBase> & value,const std::shared_ptr<RSInterpolator> & interpolator)30 void RSRenderKeyframeAnimation::AddKeyframe(float fraction, const std::shared_ptr<RSRenderPropertyBase>& value,
31     const std::shared_ptr<RSInterpolator>& interpolator)
32 {
33     if (fraction < FRACTION_MIN || fraction > FRACTION_MAX) {
34         ROSEN_LOGE("Failed to add key frame, fraction is invalid!");
35         return;
36     }
37 
38     if (IsStarted()) {
39         ROSEN_LOGE("Failed to add key frame, animation has started!");
40         return;
41     }
42 
43     keyframes_.push_back({ fraction, value, interpolator });
44 }
45 
AddKeyframes(const std::vector<std::tuple<float,std::shared_ptr<RSRenderPropertyBase>,std::shared_ptr<RSInterpolator>>> & keyframes)46 void RSRenderKeyframeAnimation::AddKeyframes(const std::vector<std::tuple<float, std::shared_ptr<RSRenderPropertyBase>,
47     std::shared_ptr<RSInterpolator>>>& keyframes)
48 {
49     if (IsStarted()) {
50         ROSEN_LOGE("Failed to add key frame, animation has started!");
51         return;
52     }
53 
54     keyframes_ = keyframes;
55 }
56 
Marshalling(Parcel & parcel) const57 bool RSRenderKeyframeAnimation::Marshalling(Parcel& parcel) const
58 {
59     if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
60         ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, RenderPropertyAnimation failed");
61         return false;
62     }
63     uint32_t size = keyframes_.size();
64     if (!parcel.WriteUint32(size)) {
65         ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write size failed");
66         return false;
67     }
68     for (auto data : keyframes_) {
69         auto& interpolator = std::get<2>(data);
70         if (!(parcel.WriteFloat(std::get<0>(data)) && RSRenderPropertyBase::Marshalling(parcel, std::get<1>(data)) &&
71                 interpolator != nullptr && interpolator->Marshalling(parcel))) {
72             ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write value failed");
73             return false;
74         }
75     }
76     return true;
77 }
78 
Unmarshalling(Parcel & parcel)79 RSRenderKeyframeAnimation* RSRenderKeyframeAnimation::Unmarshalling(Parcel& parcel)
80 {
81     RSRenderKeyframeAnimation* renderKeyframeAnimation = new RSRenderKeyframeAnimation();
82     if (!renderKeyframeAnimation->ParseParam(parcel)) {
83         ROSEN_LOGE("RSRenderKeyframeAnimation::Unmarshalling, ParseParam failed");
84         delete renderKeyframeAnimation;
85         return nullptr;
86     }
87     return renderKeyframeAnimation;
88 }
89 
ParseParam(Parcel & parcel)90 bool RSRenderKeyframeAnimation::ParseParam(Parcel& parcel)
91 {
92     if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
93         ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, RenderPropertyAnimation fail");
94         return false;
95     }
96     uint32_t size = 0;
97     if (!parcel.ReadUint32(size)) {
98         ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Parse Keyframes size fail");
99         return false;
100     }
101     float tupValue0 = 0;
102     keyframes_.clear();
103     for (uint32_t i = 0; i < size; i++) {
104         if (!(parcel.ReadFloat(tupValue0))) {
105             ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling value failed");
106             return false;
107         }
108         std::shared_ptr<RSRenderPropertyBase> tupValue1;
109         if (!RSRenderPropertyBase::Unmarshalling(parcel, tupValue1)) {
110             return false;
111         }
112         std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
113         keyframes_.emplace_back(std::make_tuple(tupValue0, tupValue1, interpolator));
114     }
115     return true;
116 }
117 
OnAnimate(float fraction)118 void RSRenderKeyframeAnimation::OnAnimate(float fraction)
119 {
120     if (keyframes_.empty()) {
121         ROSEN_LOGE("Failed to animate key frame, keyframes is empty!");
122         return;
123     }
124 
125     if (valueEstimator_ == nullptr) {
126         return;
127     }
128     valueEstimator_->UpdateAnimationValue(fraction, GetAdditive());
129 }
130 
InitValueEstimator()131 void RSRenderKeyframeAnimation::InitValueEstimator()
132 {
133     if (valueEstimator_ == nullptr) {
134         valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR);
135     }
136     valueEstimator_->InitKeyframeAnimationValue(property_, keyframes_, lastValue_);
137 }
138 } // namespace Rosen
139 } // namespace OHOS
140