blob: 3c5b3f1ff589c097d8ca3e3e6bad2a51319244db [file] [log] [blame]
Kevin DuBoisb2501ba2019-11-12 14:20:29 -08001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Kevin DuBois5988f482020-01-17 09:03:32 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Kevin DuBoisc94ca832019-11-26 12:56:24 -080018#undef LOG_TAG
19#define LOG_TAG "VSyncReactor"
Kevin DuBoisf91e9232019-11-21 10:51:23 -080020//#define LOG_NDEBUG 0
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080021#include "VSyncReactor.h"
Ady Abraham13bc0be2020-02-27 16:48:20 -080022#include <cutils/properties.h>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080023#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080024#include <utils/Trace.h>
Ady Abraham13bc0be2020-02-27 16:48:20 -080025#include "../TracedOrdinal.h"
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080026#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080027#include "VSyncDispatch.h"
28#include "VSyncTracker.h"
29
30namespace android::scheduler {
Ady Abraham5e7371c2020-03-24 14:47:24 -070031using base::StringAppendF;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080032
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080033Clock::~Clock() = default;
Kevin DuBois00287382019-11-19 15:11:55 -080034nsecs_t SystemClock::now() const {
35 return systemTime(SYSTEM_TIME_MONOTONIC);
36}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080037
Ady Abraham8735eac2020-08-12 16:35:04 -070038VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker,
39 size_t pendingFenceLimit, bool supportKernelIdleTimer)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080040 : mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070041 mTracker(tracker),
Ady Abraham13bc0be2020-02-27 16:48:20 -080042 mPendingLimit(pendingFenceLimit),
Dan Stoza027d3652020-05-26 17:26:34 -070043 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080044
Kevin DuBoisf91e9232019-11-21 10:51:23 -080045VSyncReactor::~VSyncReactor() = default;
46
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080047bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
48 if (!fence) {
49 return false;
50 }
51
52 nsecs_t const signalTime = fence->getCachedSignalTime();
53 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
54 return true;
55 }
56
Ady Abraham9c53ee72020-07-22 21:16:18 -070057 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080058 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080059 return true;
60 }
61
Kevin DuBois02d5ed92020-01-27 11:05:46 -080062 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080063 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
64 auto const time = (*it)->getCachedSignalTime();
65 if (time == Fence::SIGNAL_TIME_PENDING) {
66 it++;
67 } else if (time == Fence::SIGNAL_TIME_INVALID) {
68 it = mUnfiredFences.erase(it);
69 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070070 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080071
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080072 it = mUnfiredFences.erase(it);
73 }
74 }
75
76 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
77 if (mPendingLimit == mUnfiredFences.size()) {
78 mUnfiredFences.erase(mUnfiredFences.begin());
79 }
80 mUnfiredFences.push_back(fence);
81 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070082 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080083 }
84
85 if (!timestampAccepted) {
86 mMoreSamplesNeeded = true;
87 setIgnorePresentFencesInternal(true);
88 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080089 }
90
Kevin DuBoisf77025c2019-12-18 16:13:24 -080091 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080092}
93
94void VSyncReactor::setIgnorePresentFences(bool ignoration) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070095 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080096 mExternalIgnoreFences = ignoration;
97 updateIgnorePresentFencesInternal();
98}
99
100void VSyncReactor::setIgnorePresentFencesInternal(bool ignoration) {
101 mInternalIgnoreFences = ignoration;
102 updateIgnorePresentFencesInternal();
103}
104
105void VSyncReactor::updateIgnorePresentFencesInternal() {
106 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800107 mUnfiredFences.clear();
108 }
109}
110
Ady Abraham0ed31c92020-04-16 11:48:45 -0700111nsecs_t VSyncReactor::computeNextRefresh(int periodOffset, nsecs_t now) const {
Ady Abraham5a858552020-03-31 17:54:56 -0700112 auto const currentPeriod = periodOffset ? mTracker.currentPeriod() : 0;
113 return mTracker.nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800114}
115
Ady Abraham0ed31c92020-04-16 11:48:45 -0700116nsecs_t VSyncReactor::expectedPresentTime(nsecs_t now) {
Ady Abraham5a858552020-03-31 17:54:56 -0700117 return mTracker.nextAnticipatedVSyncTimeFrom(now);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800118}
119
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800120void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700121 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800122 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800123 mPeriodTransitioningTo = newPeriod;
124 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800125 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800126}
127
128void VSyncReactor::endPeriodTransition() {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700129 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800130 mPeriodTransitioningTo.reset();
131 mPeriodConfirmationInProgress = false;
132 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800133}
134
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800135void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800136 ATRACE_INT64("VSR-setPeriod", period);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800137 std::lock_guard lk(mMutex);
138 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700139
140 if (!mSupportKernelIdleTimer && period == getPeriod()) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800141 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700142 setIgnorePresentFencesInternal(false);
143 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800144 } else {
145 startPeriodTransition(period);
146 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800147}
148
149nsecs_t VSyncReactor::getPeriod() {
Ady Abraham5a858552020-03-31 17:54:56 -0700150 return mTracker.currentPeriod();
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800151}
152
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800153void VSyncReactor::beginResync() {
Ady Abraham5a858552020-03-31 17:54:56 -0700154 mTracker.resetModel();
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800155}
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800156
Ady Abraham5dee2f12020-02-05 17:49:47 -0800157bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
158 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800159 return false;
160 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800161
Ady Abraham5dee2f12020-02-05 17:49:47 -0800162 if (!mLastHwVsync && !HwcVsyncPeriod) {
163 return false;
164 }
165
Dan Stoza09bf7632020-06-10 14:28:50 -0700166 const bool periodIsChanging =
167 mPeriodTransitioningTo && (*mPeriodTransitioningTo != getPeriod());
168 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700169 // Clear out the Composer-provided period and use the allowance logic below
170 HwcVsyncPeriod = {};
171 }
172
Ady Abraham5dee2f12020-02-05 17:49:47 -0800173 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : getPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800174 static constexpr int allowancePercent = 10;
175 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
176 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800177 if (HwcVsyncPeriod) {
178 return std::abs(*HwcVsyncPeriod - period) < allowance;
179 }
180
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800181 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800182 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800183}
184
Ady Abraham5dee2f12020-02-05 17:49:47 -0800185bool VSyncReactor::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
186 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800187 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800188
Ady Abraham9c53ee72020-07-22 21:16:18 -0700189 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800190 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700191 ATRACE_NAME("VSR: period confirmed");
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800192 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700193 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800194 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800195 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700196
197 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700198 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700199 }
Ady Abraham5a858552020-03-31 17:54:56 -0700200 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700201
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800202 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700203 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800204 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700205 ATRACE_NAME("VSR: still confirming period");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800206 mLastHwVsync = timestamp;
207 mMoreSamplesNeeded = true;
208 *periodFlushed = false;
209 } else {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700210 ATRACE_NAME("VSR: adding sample");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800211 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700212 mTracker.addVsyncTimestamp(timestamp);
213 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800214 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800215
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700216 if (!mMoreSamplesNeeded) {
217 setIgnorePresentFencesInternal(false);
218 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800219 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800220}
221
Kevin DuBois00287382019-11-19 15:11:55 -0800222void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700223 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700224 StringAppendF(&result, "VsyncReactor in use\n");
225 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
226 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
227 mInternalIgnoreFences, mExternalIgnoreFences);
228 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
229 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
230 if (mPeriodTransitioningTo) {
231 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
232 } else {
233 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
234 }
235
236 if (mLastHwVsync) {
237 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
238 (mClock->now() - *mLastHwVsync) / 1e6f);
239 } else {
240 StringAppendF(&result, "No Last HW vsync\n");
241 }
242
Ady Abraham5e7371c2020-03-24 14:47:24 -0700243 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700244 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800245}
246
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800247} // namespace android::scheduler