1 /*
2 * Copyright (c) 2021 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 #include "vsync_sampler.h"
17 #include <cmath>
18 #include "vsync_generator.h"
19
20 namespace OHOS {
21 namespace Rosen {
22 namespace impl {
23 std::once_flag VSyncSampler::createFlag_;
24 sptr<OHOS::Rosen::VSyncSampler> VSyncSampler::instance_ = nullptr;
25
26 namespace {
27 constexpr double PI = 3.1415926;
28 constexpr int64_t g_errorThreshold = 40000000000; // 200 usec squared
29 constexpr int32_t INVAILD_TIMESTAMP = -1;
30 constexpr int32_t MINES_SAMPLE_NUMS = 3;
31 }
GetInstance()32 sptr<OHOS::Rosen::VSyncSampler> VSyncSampler::GetInstance() noexcept
33 {
34 std::call_once(createFlag_, []() {
35 auto vsyncSampler = new VSyncSampler();
36 instance_ = vsyncSampler;
37 });
38
39 return instance_;
40 }
41
VSyncSampler()42 VSyncSampler::VSyncSampler()
43 : period_(0), phase_(0), referenceTime_(0),
44 error_(0), firstSampleIndex_(0), numSamples_(0),
45 modeUpdated_(false)
46 {
47 }
48
Reset()49 void VSyncSampler::Reset()
50 {
51 std::lock_guard<std::mutex> lock(mutex_);
52 period_ = 0;
53 phase_ = 0;
54 referenceTime_ = 0;
55 error_ = 0;
56 firstSampleIndex_ = 0;
57 numSamples_ = 0;
58 modeUpdated_ = false;
59 hardwareVSyncStatus_ = true;
60 }
61
ResetErrorLocked()62 void VSyncSampler::ResetErrorLocked()
63 {
64 presentFenceTimeOffset_ = 0;
65 error_ = 0;
66 for (uint32_t i = 0; i < NUM_PRESENT; i++) {
67 presentFenceTime_[i] = INVAILD_TIMESTAMP;
68 }
69 }
70
BeginSample()71 void VSyncSampler::BeginSample()
72 {
73 std::lock_guard<std::mutex> lock(mutex_);
74 numSamples_ = 0;
75 modeUpdated_ = false;
76 hardwareVSyncStatus_ = true;
77 }
78
SetHardwareVSyncStatus(bool enabled)79 void VSyncSampler::SetHardwareVSyncStatus(bool enabled)
80 {
81 std::lock_guard<std::mutex> lock(mutex_);
82 hardwareVSyncStatus_ = enabled;
83 }
84
GetHardwareVSyncStatus() const85 bool VSyncSampler::GetHardwareVSyncStatus() const
86 {
87 std::lock_guard<std::mutex> lock(mutex_);
88 return hardwareVSyncStatus_;
89 }
90
AddSample(int64_t timeStamp)91 bool VSyncSampler::AddSample(int64_t timeStamp)
92 {
93 std::lock_guard<std::mutex> lock(mutex_);
94 if (numSamples_ == 0) {
95 phase_ = 0;
96 referenceTime_ = timeStamp;
97 CreateVSyncGenerator()->UpdateMode(period_, phase_, referenceTime_);
98 }
99
100 if (numSamples_ < MAX_SAMPLES - 1) {
101 numSamples_++;
102 } else {
103 firstSampleIndex_ = (firstSampleIndex_ + 1) % MAX_SAMPLES;
104 }
105
106 uint32_t index = (firstSampleIndex_ + numSamples_ - 1) % MAX_SAMPLES;
107 samples_[index] = timeStamp;
108
109 UpdateModeLocked();
110
111 if (numResyncSamplesSincePresent_++ > MAX_SAMPLES_WITHOUT_PRESENT) {
112 ResetErrorLocked();
113 }
114
115 // 1/2 just a empirical value
116 bool ret = modeUpdated_ & (error_ < g_errorThreshold / 2);
117 return !ret;
118 }
119
120
UpdateModeLocked()121 void VSyncSampler::UpdateModeLocked()
122 {
123 if (numSamples_ >= MIN_SAMPLES_FOR_UPDATE) {
124 int64_t sum = 0;
125 int64_t min = INT64_MAX;
126 int64_t max = 0;
127 for (uint32_t i = 1; i < numSamples_; i++) {
128 int64_t prevSample = samples_[(firstSampleIndex_ + i - 1 + MAX_SAMPLES) % MAX_SAMPLES];
129 int64_t currentSample = samples_[(firstSampleIndex_ + i) % MAX_SAMPLES];
130 int64_t diff = currentSample - prevSample;
131 min = min < diff ? min : diff;
132 max = max > diff ? max : diff;
133 sum += diff;
134 }
135 sum -= min;
136 sum -= max;
137
138 period_ = sum / (numSamples_ - MINES_SAMPLE_NUMS);
139
140 double scale = 2.0 * PI / period_;
141 double deltaAvgX = 0;
142 double deltaAvgY = 0;
143 for (uint32_t i = 1; i < numSamples_; i++) {
144 double delta = (samples_[(firstSampleIndex_ + i) % MAX_SAMPLES] - referenceTime_) % period_ * scale;
145 deltaAvgX += cos(delta);
146 deltaAvgY += sin(delta);
147 }
148
149 deltaAvgX /= double(numSamples_ - 1);
150 deltaAvgY /= double(numSamples_ - 1);
151
152 phase_ = int64_t(::atan2(deltaAvgY, deltaAvgX) / scale);
153
154 modeUpdated_ = true;
155 CreateVSyncGenerator()->UpdateMode(period_, phase_, referenceTime_);
156 }
157 }
158
UpdateErrorLocked()159 void VSyncSampler::UpdateErrorLocked()
160 {
161 if (!modeUpdated_) {
162 return;
163 }
164
165 int numErrSamples = 0;
166 int64_t sqErrSum = 0;
167
168 for (uint32_t i = 0; i < NUM_PRESENT; i++) {
169 int64_t t = presentFenceTime_[i];
170 if (t <= 0) {
171 continue;
172 }
173
174 int64_t sample = t - referenceTime_;
175 if (sample <= phase_) {
176 continue;
177 }
178
179 int64_t sampleErr = (sample - phase_) % period_;
180 // 1/2 just a empirical value
181 if (sampleErr > period_ / 2) {
182 sampleErr -= period_;
183 }
184 sqErrSum += sampleErr * sampleErr;
185 numErrSamples++;
186 }
187
188 if (numErrSamples > 0) {
189 error_ = sqErrSum / numErrSamples;
190 } else {
191 error_ = 0;
192 }
193 }
194
AddPresentFenceTime(int64_t timestamp)195 bool VSyncSampler::AddPresentFenceTime(int64_t timestamp)
196 {
197 std::lock_guard<std::mutex> lock(mutex_);
198 presentFenceTime_[presentFenceTimeOffset_] = timestamp;
199 presentFenceTimeOffset_ = (presentFenceTimeOffset_ + 1) % NUM_PRESENT;
200 numResyncSamplesSincePresent_ = 0;
201
202 UpdateErrorLocked();
203
204 return !modeUpdated_ || error_ > g_errorThreshold;
205 }
206
GetPeriod() const207 int64_t VSyncSampler::GetPeriod() const
208 {
209 std::lock_guard<std::mutex> lock(mutex_);
210 return period_;
211 }
212
GetPhase() const213 int64_t VSyncSampler::GetPhase() const
214 {
215 std::lock_guard<std::mutex> lock(mutex_);
216 return phase_;
217 }
218
GetRefrenceTime() const219 int64_t VSyncSampler::GetRefrenceTime() const
220 {
221 std::lock_guard<std::mutex> lock(mutex_);
222 return referenceTime_;
223 }
224
~VSyncSampler()225 VSyncSampler::~VSyncSampler()
226 {
227 }
228 } // namespace impl
229
CreateVSyncSampler()230 sptr<VSyncSampler> CreateVSyncSampler()
231 {
232 return impl::VSyncSampler::GetInstance();
233 }
234 }
235 }
236