• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_UI_DISPLAY_SYNC_H
18 
19 #include <unordered_map>
20 #include <string>
21 #include <functional>
22 #include <mutex>
23 #include <iostream>
24 #include <atomic>
25 #include <set>
26 
27 #include "base/memory/ace_type.h"
28 #include "base/utils/utils.h"
29 #include "base/log/log.h"
30 #include "base/utils/base_id.h"
31 #include "base/log/ace_trace.h"
32 #include "ui/animation/frame_rate_range.h"
33 
34 namespace OHOS::Ace {
35 enum class RefreshRateMode : int32_t {
36     REFRESHRATE_MODE_AUTO = -1,
37 };
38 enum class UIObjectType : int32_t {
39     DISPLAYSYNC_OTHERS = -1,
40     DISPLAYSYNC_ANIMATOR = 0,
41     DISPLAYSYNC_XCOMPONENT = 1,
42 };
43 constexpr int32_t INVALID_ANIMATOR_EXPECTED_RATE = -1;
44 
45 class PipelineBase;
46 
47 class DisplaySyncData;
48 using OnFrameCallBack = std::function<void()>;
49 using OnFrameCallBackWithData = std::function<void(const RefPtr<DisplaySyncData>&)>;
50 using OnFrameCallBackWithTimestamp = std::function<void(uint64_t)>;
51 
52 class DisplaySyncData : public AceType {
53     DECLARE_ACE_TYPE(DisplaySyncData, AceType);
54 public:
SetTimestamp(int64_t timestamp)55     void SetTimestamp(int64_t timestamp)
56     {
57         timestamp_ = timestamp;
58     }
59 
GetTimestamp()60     int64_t GetTimestamp() const
61     {
62         return timestamp_;
63     }
64 
SetTargetTimestamp(int64_t targetTimestamp)65     void SetTargetTimestamp(int64_t targetTimestamp)
66     {
67         targetTimestamp_ = targetTimestamp;
68     }
69 
GetTargetTimestamp()70     int64_t GetTargetTimestamp() const
71     {
72         return targetTimestamp_;
73     }
74 
75     OnFrameCallBack onFrame_ = nullptr;
76     OnFrameCallBackWithData onFrameWithData_ = nullptr;
77     OnFrameCallBackWithTimestamp onFrameWithTimestamp_ = nullptr;
78 
79     int64_t timestamp_ = 0;
80     int64_t targetTimestamp_ = 0;
81 
82     int32_t rate_ = 1;
83     bool noSkip_ = true;
84     int32_t count_ = 0;
85     RefPtr<FrameRateRange> rateRange_ = AceType::MakeRefPtr<FrameRateRange>();
86 };
87 
88 class ACE_FORCE_EXPORT UIDisplaySync : public AceType, public BaseId {
89     DECLARE_ACE_TYPE(UIDisplaySync, AceType);
90 public:
91     void AddToPipeline(WeakPtr<PipelineBase>& pipelineContext);
92     void DelFromPipeline(WeakPtr<PipelineBase>& pipelineContext);
93     void AddToPipelineOnContainer();
94     void DelFromPipelineOnContainer();
95     bool IsAddToPipeline(WeakPtr<PipelineBase>& pipelineContext);
96     bool IsOnPipeline();
97     void RequestFrame();
98     void JudgeWhetherRequestFrame();
99 
100     void RegisterOnFrame(OnFrameCallBack&& onFrameCallBack);
101     void RegisterOnFrameWithData(OnFrameCallBackWithData&& onFrameCallBack);
102     void RegisterOnFrameWithTimestamp(OnFrameCallBackWithTimestamp&& onFrameCallBack);
103 
104     void UnregisterOnFrame();
105 
106     void CheckRate(int32_t vsyncRate, int32_t refreshRateMode);
107     void UpdateData(int64_t nanoTimestamp, int32_t vsyncPeriod);
108     void JudgeWhetherSkip();
109     void OnFrame();
110 
111     void SetExpectedFrameRateRange(const FrameRateRange& frameRateRange);
112     bool SetVsyncRate(int32_t vsyncRate);
113     bool IsCommonDivisor(int32_t expectedRate, int32_t vsyncRate);
114 
115     void SetTimestampData(int64_t timestamp);
116     int64_t GetTimestampData() const;
117     void SetTargetTimestampData(int64_t targetTimestamp);
118     int64_t GetTargetTimestampData() const;
119 
120     RefPtr<DisplaySyncData> GetDisplaySyncData() const;
121     int32_t GetAnimatorExpectedRate();
122 
123     void SetRefreshRateMode(int32_t refreshRateMode);
124     int32_t GetRefreshRateMode() const;
125     bool IsAutoRefreshRateMode() const;
126     bool IsNonAutoRefreshRateMode() const;
127 
128     std::vector<int32_t> FindRefreshRateFactors(int32_t refreshRate);
129     int32_t FindMatchedRefreshRate(int32_t vsyncRate, int32_t targetRate);
130     int32_t SearchMatchedRate(int32_t vsyncRate, int32_t iterCount = 1);
131     RefPtr<PipelineBase> GetCurrentContext();
132 
133     UIDisplaySync();
134     UIDisplaySync(UIObjectType uiObjectType);
135     ~UIDisplaySync() noexcept override;
136 
137 private:
138     UIObjectType uiObjectType_ = UIObjectType::DISPLAYSYNC_OTHERS;
139     RefPtr<DisplaySyncData> data_ = AceType::MakeRefPtr<DisplaySyncData>();
140     int32_t sourceVsyncRate_ = 0;
141     bool rateChanged_ = true;
142     int32_t refreshRateMode_ = 0;
143     WeakPtr<PipelineBase> context_;
144     int32_t drawFPS_ = 0;
145     std::unordered_map<int32_t, std::vector<int32_t>> refreshRateToFactorsMap_;
146 };
147 
148 class ACE_FORCE_EXPORT UIXComponentDisplaySync : public UIDisplaySync {
149     DECLARE_ACE_TYPE(UIXComponentDisplaySync, UIDisplaySync);
150 public:
UIXComponentDisplaySync()151     UIXComponentDisplaySync() : UIDisplaySync(UIObjectType::DISPLAYSYNC_XCOMPONENT) {}
152     ~UIXComponentDisplaySync() noexcept override;
153 
154     void NotifyXComponentExpectedFrameRate(const std::string& id);
155     void NotifyXComponentExpectedFrameRate(const std::string& id, int32_t preferred);
156     void NotifyXComponentExpectedFrameRate(const std::string& id,
157         bool isOnTree, const FrameRateRange& expectedFrameRate);
158 
159 private:
160     std::string lastId_;
161     std::optional<FrameRateRange> lastFrameRateRange_;
162 };
163 } // namespace OHOS::Ace
164 
165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_UI_DISPLAY_SYNC_H
166