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
Marshalling(Parcel & parcel) const28 bool RSStepsInterpolator::Marshalling(Parcel& parcel) const
29 {
30 if (!parcel.WriteUint16(InterpolatorType::STEPS)) {
31 ROSEN_LOGE("StepsInterpolator marshalling write type failed.");
32 return false;
33 }
34 if (!(parcel.WriteInt32(steps_) && parcel.WriteInt32(static_cast<int32_t>(position_)))) {
35 ROSEN_LOGE("StepsInterpolator marshalling write value failed.");
36 return false;
37 }
38 return true;
39 }
40
Unmarshalling(Parcel & parcel)41 RSStepsInterpolator* RSStepsInterpolator::Unmarshalling(Parcel& parcel)
42 {
43 int32_t steps, position;
44 if (!(parcel.ReadInt32(steps) && parcel.ReadInt32(position))) {
45 ROSEN_LOGE("StepsInterpolator unmarshalling failed.");
46 return nullptr;
47 }
48 return new RSStepsInterpolator(steps, static_cast<StepsCurvePosition>(position));
49 }
50
Interpolate(float fraction) const51 float RSStepsInterpolator::Interpolate(float fraction) const
52 {
53 if (fraction < fractionMin || fraction > fractionMax) {
54 ROSEN_LOGE("Fraction is less than 0 or larger than 1, return 1.");
55 return fractionMax;
56 }
57 auto currentStep = static_cast<int32_t>(fraction * steps_);
58 if (position_ == StepsCurvePosition::START) {
59 currentStep++;
60 }
61 return static_cast<float>(currentStep) / steps_;
62 }
63 } // namespace Rosen
64 } // namespace OHOS
65