1 /*
2  * Copyright (c) 2022-2022 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_steps_interpolator.h"
17 
18 #include <algorithm>
19 #include <cmath>
20 
21 #include "platform/common/rs_log.h"
22 
23 namespace OHOS {
24 namespace Rosen {
RSStepsInterpolator(int32_t steps,StepsCurvePosition position)25 RSStepsInterpolator::RSStepsInterpolator(int32_t steps, StepsCurvePosition position)
26     : steps_(steps <= 0 ? 1 : steps), position_(position)
27 {}
28 
RSStepsInterpolator(uint64_t id,int32_t steps,StepsCurvePosition position)29 RSStepsInterpolator::RSStepsInterpolator(uint64_t id, int32_t steps, StepsCurvePosition position)
30     : RSInterpolator(id), steps_(steps <= 0 ? 1 : steps), position_(position)
31 {}
32 
Marshalling(Parcel & parcel) const33 bool RSStepsInterpolator::Marshalling(Parcel& parcel) const
34 {
35     if (!parcel.WriteUint16(InterpolatorType::STEPS)) {
36         ROSEN_LOGE("StepsInterpolator marshalling write type failed.");
37         return false;
38     }
39     if (!parcel.WriteUint64(id_)) {
40         ROSEN_LOGE("StepsInterpolator marshalling write id failed.");
41         return false;
42     }
43     if (!(parcel.WriteInt32(steps_) && parcel.WriteInt32(static_cast<int32_t>(position_)))) {
44         ROSEN_LOGE("StepsInterpolator marshalling write value failed.");
45         return false;
46     }
47     return true;
48 }
49 
Unmarshalling(Parcel & parcel)50 RSStepsInterpolator* RSStepsInterpolator::Unmarshalling(Parcel& parcel)
51 {
52     uint64_t id = parcel.ReadUint64();
53     int32_t steps = 0;
54     int32_t position = 0;
55     if (!(parcel.ReadInt32(steps) && parcel.ReadInt32(position))) {
56         ROSEN_LOGE("StepsInterpolator unmarshalling failed.");
57         return nullptr;
58     }
59     return new RSStepsInterpolator(id, steps, static_cast<StepsCurvePosition>(position));
60 }
61 
InterpolateImpl(float fraction) const62 float RSStepsInterpolator::InterpolateImpl(float fraction) const
63 {
64     if (fraction < fractionMin || fraction > fractionMax) {
65         ROSEN_LOGE("Fraction is less than 0 or larger than 1, return 1.");
66         return fractionMax;
67     }
68     auto currentStep = static_cast<int32_t>(fraction * steps_);
69     if (position_ == StepsCurvePosition::START) {
70         currentStep++;
71     }
72     if (steps_ == 0) {
73         ROSEN_LOGE("RSStepsInterpolator::Interpolate, steps number is invalid!");
74         return static_cast<float>(currentStep);
75     }
76     // current step should not greater than the total steps number
77     currentStep = std::min(currentStep, steps_);
78     return static_cast<float>(currentStep) / steps_;
79 }
80 } // namespace Rosen
81 } // namespace OHOS
82