1 /*
2  * Copyright (c) 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_UI_DISPLAY_SYNC_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_UI_DISPLAY_SYNC_MANAGER_H
18 
19 #include <unordered_map>
20 #include <mutex>
21 #include <queue>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/utils/utils.h"
25 #include "base/log/log.h"
26 #include "base/utils/base_id.h"
27 #include "base/log/ace_trace.h"
28 #include "base/utils/system_properties.h"
29 #include "ui_display_sync.h"
30 
31 namespace OHOS::Ace {
32 using IdType = uint64_t;
33 using IdToDisplaySyncMap = std::unordered_map<IdType, WeakPtr<UIDisplaySync>>;
34 
35 class ACE_FORCE_EXPORT UIDisplaySyncManager : public AceType {
36     DECLARE_ACE_TYPE(UIDisplaySyncManager, AceType)
37 public:
38     bool AddDisplaySync(const RefPtr<UIDisplaySync>& displaySync);
39     bool RemoveDisplaySync(const RefPtr<UIDisplaySync>& displaySync);
40     bool HasDisplaySync(const RefPtr<UIDisplaySync>& displaySync);
41 
42     void DispatchFunc(int64_t nanoTimestamp);
43 
44     bool SetVsyncRate(int32_t vsyncRate);
45     int32_t GetVsyncRate() const;
46     bool SetVsyncPeriod(int64_t vsyncPeriod);
47     int64_t GetVsyncPeriod() const;
48     bool SetRefreshRateMode(int32_t refreshRateMode);
49     int32_t GetRefreshRateMode() const;
50     int32_t GetDisplaySyncRate() const;
51     IdToDisplaySyncMap GetUIDisplaySyncMap() const;
52     void CheckSkipEnableProperty();
53     bool IsSupportSkip() const;
54     bool IsAutoRefreshRateMode() const;
55     bool IsNonAutoRefreshRateMode() const;
56 
57     std::set<int32_t> FindRefreshRateFactors(int32_t refreshRate);
58     int32_t FindMatchedRefreshRate(int32_t target);
59     void FindAllRefreshRateFactors();
60 
61     int32_t GetAnimatorRate();
62     bool IsAnimatorStopped();
63 
64     UIDisplaySyncManager();
65     ~UIDisplaySyncManager() noexcept override;
66 
67 private:
68     static const std::vector<int32_t> REFRESH_RATE_LIST;
69     static constexpr int32_t ERROR_DELTA = 3;
70     static constexpr float SECOND_IN_NANO = 1000000000.0f;
71 
72     int32_t sourceVsyncRate_ = 0;
73     int64_t vsyncPeriod_ = 0;
74     int32_t refreshRateMode_ = 0;
75     RefPtr<FrameRateRange> displaySyncRange_ = AceType::MakeRefPtr<FrameRateRange>();
76 
77     IdToDisplaySyncMap uiDisplaySyncMap_;
78     std::once_flag isEnablePropertyFlag_;
79     bool supportSkipEnabled_ = true;
80     std::vector<int32_t> refreshRateFactors_;
81     std::once_flag computeFactorsFlag_;
82     std::priority_queue<int32_t> maxAnimatorRateHap_;
83 };
84 } // namespace OHOS::Ace
85 
86 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_UI_DISPLAY_SYNC_MANAGER_H
87