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_KEYFRAME_ANIMATION_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_KEYFRAME_ANIMATION_H 18 19 #include "animation/rs_interpolator.h" 20 #include "animation/rs_render_property_animation.h" 21 #include "animation/rs_value_estimator.h" 22 #include "platform/common/rs_log.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 template<typename T> 27 class RSRenderKeyframeAnimation : public RSRenderPropertyAnimation<T> { 28 public: RSRenderKeyframeAnimation(AnimationId id,const RSAnimatableProperty & property,const T & originValue)29 RSRenderKeyframeAnimation(AnimationId id, const RSAnimatableProperty& property, const T& originValue) 30 : RSRenderPropertyAnimation<T>(id, property, originValue) 31 {} ~RSRenderKeyframeAnimation()32 ~RSRenderKeyframeAnimation() {} 33 AddKeyframe(float fraction,const T & value,const std::shared_ptr<RSInterpolator> & interpolator)34 void AddKeyframe(float fraction, const T& value, 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 (RSRenderAnimation::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,T,std::shared_ptr<RSInterpolator>>> & keyframes)49 void AddKeyframes(const std::vector<std::tuple<float, T, std::shared_ptr<RSInterpolator>>>& keyframes) 50 { 51 if (RSRenderAnimation::IsStarted()) { 52 ROSEN_LOGE("Failed to add key frame, animation has started!"); 53 return; 54 } 55 56 keyframes_ = keyframes; 57 } 58 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)59 bool Marshalling(Parcel& parcel) const override 60 { 61 if (!RSRenderPropertyAnimation<T>::Marshalling(parcel)) { 62 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, RenderPropertyAnimation failed"); 63 return false; 64 } 65 uint32_t size = keyframes_.size(); 66 if (!parcel.WriteUint32(size)) { 67 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write size failed"); 68 return false; 69 } 70 for (auto data : keyframes_) { 71 if (!(parcel.WriteFloat(std::get<0>(data)) && RSMarshallingHelper::Marshalling(parcel, std::get<1>(data)) && 72 std::get<2>(data)->Marshalling(parcel))) { 73 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write value failed"); 74 return false; 75 } 76 } 77 return true; 78 } Unmarshalling(Parcel & parcel)79 static RSRenderKeyframeAnimation* Unmarshalling(Parcel& parcel) 80 { 81 RSRenderKeyframeAnimation* renderKeyframeAnimation = new RSRenderKeyframeAnimation<T>(); 82 if (!renderKeyframeAnimation->ParseParam(parcel)) { 83 ROSEN_LOGE("RSRenderKeyframeAnimation::Unmarshalling, ParseParam failed"); 84 delete renderKeyframeAnimation; 85 return nullptr; 86 } 87 return renderKeyframeAnimation; 88 } 89 #endif 90 protected: OnAnimate(float fraction)91 void OnAnimate(float fraction) override 92 { 93 if (keyframes_.empty()) { 94 ROSEN_LOGE("Failed to animate key frame, keyframes is empty!"); 95 return; 96 } 97 98 float preKeyframeFraction = std::get<0>(keyframes_.front()); 99 auto preKeyframeValue = std::get<1>(keyframes_.front()); 100 for (const auto& keyframe : keyframes_) { 101 // the index of tuple 102 float keyframeFraction = std::get<0>(keyframe); 103 auto keyframeValue = std::get<1>(keyframe); 104 auto keyframeInterpolator = std::get<2>(keyframe); 105 if (fraction <= keyframeFraction) { 106 if (ROSEN_EQ(keyframeFraction, preKeyframeFraction)) { 107 continue; 108 } 109 110 float intervalFraction = (fraction - preKeyframeFraction) / (keyframeFraction - preKeyframeFraction); 111 auto interpolationValue = RSValueEstimator::Estimate( 112 keyframeInterpolator->Interpolate(intervalFraction), preKeyframeValue, keyframeValue); 113 RSRenderPropertyAnimation<T>::SetAnimationValue(interpolationValue); 114 break; 115 } 116 117 preKeyframeFraction = keyframeFraction; 118 preKeyframeValue = keyframeValue; 119 } 120 } 121 122 private: 123 RSRenderKeyframeAnimation() = default; 124 #ifdef ROSEN_OHOS ParseParam(Parcel & parcel)125 bool ParseParam(Parcel& parcel) override 126 { 127 if (!RSRenderPropertyAnimation<T>::ParseParam(parcel)) { 128 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, RenderPropertyAnimation fail"); 129 return false; 130 } 131 uint32_t size = 0; 132 if (!parcel.ReadUint32(size)) { 133 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Parse Keyframes size fail"); 134 return false; 135 } 136 float tupValue0 = 0; 137 T tupValue1; 138 std::shared_ptr<RSInterpolator> interpolator; 139 keyframes_.clear(); 140 for (u_int32_t i = 0; i < size; i++) { 141 if (!(parcel.ReadFloat(tupValue0) && RSMarshallingHelper::Unmarshalling(parcel, tupValue1))) { 142 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling value failed"); 143 return false; 144 } 145 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel)); 146 keyframes_.emplace_back(std::make_tuple(tupValue0, tupValue1, interpolator)); 147 } 148 return true; 149 } 150 #endif 151 std::vector<std::tuple<float, T, std::shared_ptr<RSInterpolator>>> keyframes_; 152 }; 153 } // namespace Rosen 154 } // namespace OHOS 155 156 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_KEYFRAME_ANIMATION_H 157