1 /* 2 * Copyright (c) 2021-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 #ifndef DISPLAYMGR_GRADUAL_ANIMATOR_H 17 #define DISPLAYMGR_GRADUAL_ANIMATOR_H 18 19 #include <memory> 20 #include <string> 21 #include <cstdint> 22 #include <iosfwd> 23 #include <event_handler.h> 24 #include <event_runner.h> 25 #include "inner_event.h" 26 27 namespace OHOS { 28 namespace DisplayPowerMgr { 29 class AnimateCallback { 30 public: 31 virtual ~AnimateCallback() = default; 32 virtual void OnStart() = 0; 33 virtual void OnChanged(uint32_t currentValue) = 0; 34 virtual void OnEnd() = 0; 35 virtual void DiscountBrightness(double discount) = 0; 36 }; 37 38 class GradualAnimator : public std::enable_shared_from_this<GradualAnimator> { 39 public: 40 GradualAnimator(const std::string& name, std::shared_ptr<AnimateCallback>& callback); 41 virtual ~GradualAnimator() = default; 42 void StartAnimation(uint32_t from, uint32_t to, uint32_t duration); 43 void StopAnimation(); 44 bool IsAnimating() const; 45 uint32_t GetAnimationUpdateTime() const; 46 private: 47 static const uint32_t DEFAULT_UPDATE_TIME = 30; 48 static const int32_t STRIDE_ABSOLUTE_MIN = 1; 49 50 void NextStep(); 51 52 std::string name_; 53 std::shared_ptr<AnimateCallback> callback_; 54 std::atomic_bool animating_ = false; 55 std::atomic_uint32_t fromBrightness_; 56 std::atomic_uint32_t toBrightness_; 57 std::atomic_uint32_t duration_; 58 std::atomic_uint32_t updateTime_; 59 std::atomic_uint32_t totalSteps_; 60 std::atomic_int32_t stride_; 61 std::atomic_uint32_t currentBrightness_; 62 std::atomic_uint32_t currentStep_; 63 }; 64 } // namespace DisplayPowerMgr 65 } // namespace OHOS 66 #endif // DISPLAYMGR_GRADUAL_ANIMATOR_H