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_VALUE_ESTIMATOR_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_VALUE_ESTIMATOR_H 18 19 #include <memory> 20 21 #include "animation/rs_animation_common.h" 22 #include "animation/rs_interpolator.h" 23 #include "common/rs_color.h" 24 #include "common/rs_matrix3.h" 25 #include "common/rs_vector2.h" 26 #include "common/rs_vector4.h" 27 #include "render/rs_filter.h" 28 29 namespace OHOS { 30 namespace Rosen { 31 class RSValueEstimator { 32 public: 33 template<typename T> Estimate(float fraction,const T & startValue,const T & endValue)34 static T Estimate(float fraction, const T& startValue, const T& endValue) 35 { 36 return startValue * (1.0f - fraction) + endValue * fraction; 37 } 38 39 static Quaternion Estimate(float fraction, const Quaternion& startValue, const Quaternion& endValue); 40 41 static std::shared_ptr<RSFilter> Estimate( 42 float fraction, const std::shared_ptr<RSFilter>& startValue, const std::shared_ptr<RSFilter>& endValue); 43 44 template<typename T> EstimateFraction(const std::shared_ptr<RSInterpolator> & interpolator,const T & value,const T & startValue,const T & endValue)45 static float EstimateFraction( 46 const std::shared_ptr<RSInterpolator>& interpolator, const T& value, const T& startValue, const T& endValue) 47 { 48 float start = FRACTION_MIN; 49 float end = FRACTION_MAX; 50 auto byValue = endValue - startValue; 51 while (end > start + EPSILON) { 52 float mid = (start + end) / 2.0f; 53 float fraction = interpolator->Interpolate(mid); 54 auto interpolationValue = Estimate(fraction, startValue, endValue); 55 if (value < interpolationValue) { 56 (byValue > 0) ? (end = mid) : (start = mid); 57 } else { 58 (byValue > 0) ? (start = mid) : (end = mid); 59 } 60 61 if (std::abs(value - interpolationValue) <= EPSILON) { 62 return mid; 63 } 64 } 65 66 return FRACTION_MIN; 67 } 68 69 static float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator, const Vector2f& value, 70 const Vector2f& startValue, const Vector2f& endValue); 71 72 static float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator, const Vector4f& value, 73 const Vector4f& startValue, const Vector4f& endValue); 74 75 static float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator, const Quaternion& value, 76 const Quaternion& startValue, const Quaternion& endValue); 77 78 static float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator, const RSColor& value, 79 const RSColor& startValue, const RSColor& endValue); 80 81 static float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator, const Matrix3f& value, 82 const Matrix3f& startValue, const Matrix3f& endValue); 83 84 static float EstimateFraction(const std::shared_ptr<RSInterpolator>& interpolator, 85 const std::shared_ptr<RSFilter>& value, const std::shared_ptr<RSFilter>& startValue, 86 const std::shared_ptr<RSFilter>& endValue); 87 }; 88 } // namespace Rosen 89 } // namespace OHOS 90 91 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_VALUE_ESTIMATOR_H 92