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