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