1 /* 2 * Copyright (c) 2024 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 #ifndef RENDER_SERVICE_BASE_COMMON_RS_THRESHOLD_DETECTOR_H 16 #define RENDER_SERVICE_BASE_COMMON_RS_THRESHOLD_DETECTOR_H 17 18 #include "common/rs_common_def.h" 19 #include "common/rs_macros.h" 20 21 namespace OHOS { 22 namespace Rosen { 23 template<class T> 24 class RSB_EXPORT RSThresholdDetector final { 25 public: 26 using DetectCallBack = std::function<void(const T value, bool isHigh)>; 27 /* 28 * @brief if value > thresholdHigh, callback once and high flag. Recover when value <= thresholdLow. 29 * @param thresholdLow Low threshold. 30 * @param thresholdHigh High threshold. 31 */ RSThresholdDetector(T thresholdLow,T thresholdHigh)32 RSThresholdDetector(T thresholdLow, T thresholdHigh) 33 :thresholdLow_(thresholdLow), thresholdHigh_(thresholdHigh), inThresHoldBand_(false) {} 34 virtual ~RSThresholdDetector() = default; Detect(const T value,const DetectCallBack & callback)35 void Detect(const T value, const DetectCallBack& callback) 36 { 37 if (value > thresholdHigh_ && !inThresHoldBand_) { 38 inThresHoldBand_ = true; 39 callback(value, true); 40 } 41 if (value <= thresholdLow_ && inThresHoldBand_) { 42 inThresHoldBand_ = false; 43 callback(value, false); 44 } 45 } 46 private: 47 T thresholdLow_; 48 T thresholdHigh_; 49 bool inThresHoldBand_; // used to save previous data status 50 }; 51 } // namespace Rosen 52 } // namespace OHOS 53 #endif // RENDER_SERVICE_BASE_COMMON_RS_THRESHOLD_DETECTOR_H 54