1 /*
2 * Copyright (c) 2021-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_value_estimator.h"
17
18 #include "common/rs_common_def.h"
19 #include "platform/common/rs_log.h"
20 #include "modifier/rs_render_property.h"
21
22 namespace OHOS {
23 namespace Rosen {
Estimate(float fraction,const Quaternion & startValue,const Quaternion & endValue)24 Quaternion RSValueEstimator::Estimate(float fraction,
25 const Quaternion& startValue, const Quaternion& endValue)
26 {
27 auto value = startValue;
28 return value.Slerp(endValue, fraction);
29 }
30
Estimate(float fraction,const std::shared_ptr<RSFilter> & startValue,const std::shared_ptr<RSFilter> & endValue)31 std::shared_ptr<RSFilter> RSValueEstimator::Estimate(
32 float fraction, const std::shared_ptr<RSFilter>& startValue, const std::shared_ptr<RSFilter>& endValue)
33 {
34 if ((startValue == nullptr || !startValue->IsValid()) && (endValue == nullptr || !endValue->IsValid())) {
35 return endValue;
36 }
37
38 if (startValue == nullptr || !startValue->IsValid()) {
39 return endValue * fraction;
40 }
41
42 if (endValue == nullptr || !endValue->IsValid()) {
43 return (fraction < 0.5f) ? startValue * (1.0f - fraction * 2) : endValue;
44 }
45
46 if (startValue->GetFilterType() == endValue->GetFilterType()) {
47 return startValue * (1.0f - fraction) + endValue * fraction;
48 } else {
49 return (fraction < 0.5f) ? startValue * (1.0f - fraction * 2) : endValue * (fraction * 2 - 1.0f);
50 }
51 }
52
53 template<>
EstimateFraction(const std::shared_ptr<RSInterpolator> & interpolator)54 float RSCurveValueEstimator<float>::EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator)
55 {
56 float start = FRACTION_MIN;
57 float end = FRACTION_MAX;
58 auto byValue = endValue_ - startValue_;
59 while (end > start + EPSILON) {
60 float mid = (start + end) / 2.0f;
61 float fraction = interpolator->Interpolate(mid);
62 auto interpolationValue = startValue_ * (1.0f - fraction) + endValue_ * fraction;
63 if (lastValue_ < interpolationValue) {
64 (byValue > 0) ? (end = mid) : (start = mid);
65 } else {
66 (byValue > 0) ? (start = mid) : (end = mid);
67 }
68
69 if (std::abs(lastValue_ - interpolationValue) <= EPSILON) {
70 return mid;
71 }
72 }
73
74 return FRACTION_MIN;
75 }
76
77 template class RSCurveValueEstimator<float>;
78 } // namespace Rosen
79 } // namespace OHOS
80