• 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 
26 #include "base/memory/ace_type.h"
27 #include "base/utils/utils.h"
28 #include "base/log/log.h"
29 #include "base/utils/base_id.h"
30 #include "base/log/ace_trace.h"
31 
32 namespace OHOS::Ace {
33 enum class RefreshRateMode : int32_t {
34     REFRESHRATE_MODE_AUTO = -1,
35     REFRESHRATE_MODE_NULL = 0,
36     REFRESHRATE_MODE_LOW = 1,
37     REFRESHRATE_MODE_MEDIUM,
38     REFRESHRATE_MODE_HIGH,
39 };
40 class PipelineBase;
41 
42 class FrameRateRange : public AceType {
DECLARE_ACE_TYPE(FrameRateRange,AceType)43     DECLARE_ACE_TYPE(FrameRateRange, AceType)
44 public:
45     FrameRateRange() : min_(0), max_(0), preferred_(0) {}
46 
FrameRateRange(int min,int max,int preferred)47     FrameRateRange(int min, int max, int preferred) : min_(min), max_(max), preferred_(preferred) {}
48 
IsZero()49     bool IsZero() const
50     {
51         return this->preferred_ == 0;
52     }
53 
IsValid()54     bool IsValid() const
55     {
56         return !this->IsZero() && this->min_ <= this->preferred_ && this->preferred_ <= this->max_ &&
57             this->min_ >= 0 && this->max_ <= rangeMaxRefreshrate;
58     }
59 
IsDynamic()60     bool IsDynamic() const
61     {
62         return IsValid() && this->min_ != this->max_;
63     }
64 
Reset()65     void Reset()
66     {
67         this->min_ = 0;
68         this->max_ = 0;
69         this->preferred_ = 0;
70     }
71 
Set(int min,int max,int preferred)72     void Set(int min, int max, int preferred)
73     {
74         this->min_ = min;
75         this->max_ = max;
76         this->preferred_ = preferred;
77     }
78 
Merge(const FrameRateRange & other)79     void Merge(const FrameRateRange& other)
80     {
81         if (this->preferred_ < other.preferred_) {
82             this->Set(other.min_, other.max_, other.preferred_);
83         }
84     }
85 
86     bool operator==(const FrameRateRange& other)
87     {
88         return this->min_ == other.min_ && this->max_ == other.max_ &&
89             this->preferred_ == other.preferred_;
90     }
91 
92     bool operator!=(const FrameRateRange& other)
93     {
94         return this->min_ != other.min_ || this->max_ != other.max_ ||
95             this->preferred_ != other.preferred_;
96     }
97 
98     int min_ = 0;
99     int max_ = 0;
100     int preferred_ = 0;
101     const int32_t rangeMaxRefreshrate = 144;
102 };
103 
104 class DisplaySyncData;
105 using OnFrameCallBack = std::function<void()>;
106 using OnFrameCallBackWithData = std::function<void(const RefPtr<DisplaySyncData>&)>;
107 using OnFrameCallBackWithTimestamp = std::function<void(uint64_t)>;
108 
109 class DisplaySyncData : public AceType {
DECLARE_ACE_TYPE(DisplaySyncData,AceType)110     DECLARE_ACE_TYPE(DisplaySyncData, AceType)
111 public:
112     void SetTimestamp(uint64_t timestamp)
113     {
114         timestamp_ = timestamp;
115     }
116 
GetTimestamp()117     uint64_t GetTimestamp() const
118     {
119         return timestamp_;
120     }
121 
SetTargetTimestamp(uint64_t targetTimestamp)122     void SetTargetTimestamp(uint64_t targetTimestamp)
123     {
124         targetTimestamp_ = targetTimestamp;
125     }
126 
GetTargetTimestamp()127     uint64_t GetTargetTimestamp() const
128     {
129         return targetTimestamp_;
130     }
131 
132     OnFrameCallBack onFrame_ = nullptr;
133     OnFrameCallBackWithData onFrameWithData_ = nullptr;
134     OnFrameCallBackWithTimestamp onFrameWithTimestamp_ = nullptr;
135 
136     uint64_t timestamp_ = 0;
137     uint64_t targetTimestamp_ = 0;
138 
139     int32_t rate_ = 1;
140     bool noSkip_ = true;
141     int32_t count_ = 0;
142     RefPtr<FrameRateRange> rateRange_ = AceType::MakeRefPtr<FrameRateRange>();
143 };
144 
145 class ACE_FORCE_EXPORT UIDisplaySync : public AceType, public BaseId {
146     DECLARE_ACE_TYPE(UIDisplaySync, AceType)
147 public:
148     void AddToPipeline(WeakPtr<PipelineBase>& pipelineContext);
149     void DelFromPipeline(WeakPtr<PipelineBase>& pipelineContext);
150     void AddToPipelineOnContainer();
151     void DelFromPipelineOnContainer();
152     bool IsAddToPipeline(WeakPtr<PipelineBase>& pipelineContext);
153     bool IsOnPipeline();
154     void RequestFrame();
155     void JudgeWhetherRequestFrame();
156 
157     void RegisterOnFrame(OnFrameCallBack&& onFrameCallBack);
158     void RegisterOnFrameWithData(OnFrameCallBackWithData&& onFrameCallBack);
159     void RegisterOnFrameWithTimestamp(OnFrameCallBackWithTimestamp&& onFrameCallBack);
160 
161     void UnRegisterOnFrame();
162 
163     void CheckRate(int32_t vsyncRate, int32_t refreshRateMode);
164     void UpdateData(uint64_t nanoTimestamp, int32_t vsyncPeriod);
165     void JudgeWhetherSkip();
166     void OnFrame();
167 
168     void SetExpectedFrameRateRange(FrameRateRange&& frameRateRange);
169     bool SetVsyncRate(int32_t vsyncRate);
170     bool IsCommonDivisor(int32_t expectedRate, int32_t vsyncRate);
171 
172     void SetTimestampData(uint64_t timestamp);
173     uint64_t GetTimestampData() const;
174     void SetTargetTimestampData(uint64_t targetTimestamp);
175     uint64_t GetTargetTimestampData() const;
176 
177     RefPtr<DisplaySyncData> GetDisplaySyncData() const;
178 
179     void SetRefreshRateMode(int32_t refreshRateMode);
180     int32_t GetRefreshRateMode() const;
181     bool IsAutoRefreshRateMode() const;
182     bool IsNonAutoRefreshRateMode() const;
183 
184     UIDisplaySync();
185     ~UIDisplaySync() noexcept override;
186 
187 private:
188     RefPtr<DisplaySyncData> data_ = AceType::MakeRefPtr<DisplaySyncData>();
189     int32_t sourceVsyncRate_ = 0;
190     bool rateChanged_ = true;
191     int32_t refreshRateMode_ = 0;
192 };
193 } // namespace OHOS::Ace
194 
195 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_UI_DISPLAY_SYNC_H
196