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 #include "render/rs_material_filter.h"
22
23 namespace OHOS {
24 namespace Rosen {
Estimate(float fraction,const Quaternion & startValue,const Quaternion & endValue)25 Quaternion RSValueEstimator::Estimate(float fraction,
26 const Quaternion& startValue, const Quaternion& endValue)
27 {
28 auto value = startValue;
29 return value.Slerp(endValue, fraction);
30 }
31
Estimate(float fraction,const std::shared_ptr<RSFilter> & startValue,const std::shared_ptr<RSFilter> & endValue)32 std::shared_ptr<RSFilter> RSValueEstimator::Estimate(
33 float fraction, const std::shared_ptr<RSFilter>& startValue, const std::shared_ptr<RSFilter>& endValue)
34 {
35 if ((startValue == nullptr || !startValue->IsValid()) && (endValue == nullptr || !endValue->IsValid())) {
36 return endValue;
37 }
38
39 if (startValue == nullptr || !startValue->IsValid()) {
40 if (endValue->GetFilterType() == RSFilter::MATERIAL) {
41 auto material = std::static_pointer_cast<RSMaterialFilter>(endValue)->TransformFilter(fraction);
42 return material;
43 }
44 return endValue * fraction;
45 }
46
47 if (endValue == nullptr || !endValue->IsValid()) {
48 if (startValue->GetFilterType() == RSFilter::MATERIAL) {
49 auto material =
50 std::static_pointer_cast<RSMaterialFilter>(startValue)->TransformFilter(1.0f - fraction);
51 return material;
52 }
53 return startValue * (1.0f - fraction);
54 }
55
56 if (startValue->GetFilterType() == endValue->GetFilterType()) {
57 return startValue * (1.0f - fraction) + endValue * fraction;
58 } else {
59 return (fraction < 0.5f) ? startValue * (1.0f - fraction * 2) : endValue * (fraction * 2 - 1.0f);
60 }
61 }
62
63 template<>
EstimateFraction(const std::shared_ptr<RSInterpolator> & interpolator)64 float RSCurveValueEstimator<float>::EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator)
65 {
66 if (interpolator == nullptr) {
67 ROSEN_LOGD("Interpolator is null, return FRACTION_MIN.");
68 return FRACTION_MIN;
69 }
70 float start = FRACTION_MIN;
71 float end = FRACTION_MAX;
72 auto byValue = endValue_ - startValue_;
73 while (end > start + EPSILON) {
74 float mid = (start + end) / 2.0f;
75 float fraction = interpolator->Interpolate(mid);
76 auto interpolationValue = startValue_ * (1.0f - fraction) + endValue_ * fraction;
77 if (lastValue_ < interpolationValue) {
78 (byValue > 0) ? (end = mid) : (start = mid);
79 } else {
80 (byValue > 0) ? (start = mid) : (end = mid);
81 }
82
83 if (std::abs(lastValue_ - interpolationValue) <= EPSILON) {
84 return mid;
85 }
86 }
87
88 return FRACTION_MIN;
89 }
90
91 template class RSCurveValueEstimator<float>;
92 } // namespace Rosen
93 } // namespace OHOS
94