• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #undef LOG_TAG
19 #define LOG_TAG "VSyncReactor"
20 //#define LOG_NDEBUG 0
21 
22 #include <cutils/properties.h>
23 #include <log/log.h>
24 #include <utils/Trace.h>
25 
26 #include "../TracedOrdinal.h"
27 #include "VSyncDispatch.h"
28 #include "VSyncReactor.h"
29 #include "VSyncTracker.h"
30 
31 namespace android::scheduler {
32 
33 using base::StringAppendF;
34 
35 VsyncController::~VsyncController() = default;
36 
now() const37 nsecs_t SystemClock::now() const {
38     return systemTime(SYSTEM_TIME_MONOTONIC);
39 }
40 
VSyncReactor(std::unique_ptr<Clock> clock,VSyncTracker & tracker,size_t pendingFenceLimit,bool supportKernelIdleTimer)41 VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker,
42                            size_t pendingFenceLimit, bool supportKernelIdleTimer)
43       : mClock(std::move(clock)),
44         mTracker(tracker),
45         mPendingLimit(pendingFenceLimit),
46         // TODO(adyabr): change mSupportKernelIdleTimer when the active display changes
47         mSupportKernelIdleTimer(supportKernelIdleTimer) {}
48 
49 VSyncReactor::~VSyncReactor() = default;
50 
addPresentFence(std::shared_ptr<FenceTime> fence)51 bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) {
52     if (!fence) {
53         return false;
54     }
55 
56     nsecs_t const signalTime = fence->getCachedSignalTime();
57     if (signalTime == Fence::SIGNAL_TIME_INVALID) {
58         return true;
59     }
60 
61     std::lock_guard lock(mMutex);
62     if (mExternalIgnoreFences || mInternalIgnoreFences) {
63         return true;
64     }
65 
66     bool timestampAccepted = true;
67     for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
68         auto const time = (*it)->getCachedSignalTime();
69         if (time == Fence::SIGNAL_TIME_PENDING) {
70             it++;
71         } else if (time == Fence::SIGNAL_TIME_INVALID) {
72             it = mUnfiredFences.erase(it);
73         } else {
74             timestampAccepted &= mTracker.addVsyncTimestamp(time);
75 
76             it = mUnfiredFences.erase(it);
77         }
78     }
79 
80     if (signalTime == Fence::SIGNAL_TIME_PENDING) {
81         if (mPendingLimit == mUnfiredFences.size()) {
82             mUnfiredFences.erase(mUnfiredFences.begin());
83         }
84         mUnfiredFences.push_back(std::move(fence));
85     } else {
86         timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
87     }
88 
89     if (!timestampAccepted) {
90         mMoreSamplesNeeded = true;
91         setIgnorePresentFencesInternal(true);
92         mPeriodConfirmationInProgress = true;
93     }
94 
95     return mMoreSamplesNeeded;
96 }
97 
setIgnorePresentFences(bool ignore)98 void VSyncReactor::setIgnorePresentFences(bool ignore) {
99     std::lock_guard lock(mMutex);
100     mExternalIgnoreFences = ignore;
101     updateIgnorePresentFencesInternal();
102 }
103 
setIgnorePresentFencesInternal(bool ignore)104 void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
105     mInternalIgnoreFences = ignore;
106     updateIgnorePresentFencesInternal();
107 }
108 
updateIgnorePresentFencesInternal()109 void VSyncReactor::updateIgnorePresentFencesInternal() {
110     if (mExternalIgnoreFences || mInternalIgnoreFences) {
111         mUnfiredFences.clear();
112     }
113 }
114 
startPeriodTransitionInternal(nsecs_t newPeriod)115 void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
116     ATRACE_CALL();
117     mPeriodConfirmationInProgress = true;
118     mPeriodTransitioningTo = newPeriod;
119     mMoreSamplesNeeded = true;
120     setIgnorePresentFencesInternal(true);
121 }
122 
endPeriodTransition()123 void VSyncReactor::endPeriodTransition() {
124     ATRACE_CALL();
125     mPeriodTransitioningTo.reset();
126     mPeriodConfirmationInProgress = false;
127     mLastHwVsync.reset();
128 }
129 
startPeriodTransition(nsecs_t period)130 void VSyncReactor::startPeriodTransition(nsecs_t period) {
131     ATRACE_INT64("VSR-setPeriod", period);
132     std::lock_guard lock(mMutex);
133     mLastHwVsync.reset();
134 
135     if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod()) {
136         endPeriodTransition();
137         setIgnorePresentFencesInternal(false);
138         mMoreSamplesNeeded = false;
139     } else {
140         startPeriodTransitionInternal(period);
141     }
142 }
143 
periodConfirmed(nsecs_t vsync_timestamp,std::optional<nsecs_t> HwcVsyncPeriod)144 bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
145     if (!mPeriodConfirmationInProgress) {
146         return false;
147     }
148 
149     if (mDisplayPowerMode == hal::PowerMode::DOZE ||
150         mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
151         return true;
152     }
153 
154     if (!mLastHwVsync && !HwcVsyncPeriod) {
155         return false;
156     }
157 
158     const bool periodIsChanging =
159             mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
160     if (mSupportKernelIdleTimer && !periodIsChanging) {
161         // Clear out the Composer-provided period and use the allowance logic below
162         HwcVsyncPeriod = {};
163     }
164 
165     auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
166     static constexpr int allowancePercent = 10;
167     static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
168     auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
169     if (HwcVsyncPeriod) {
170         return std::abs(*HwcVsyncPeriod - period) < allowance;
171     }
172 
173     auto const distance = vsync_timestamp - *mLastHwVsync;
174     return std::abs(distance - period) < allowance;
175 }
176 
addHwVsyncTimestamp(nsecs_t timestamp,std::optional<nsecs_t> hwcVsyncPeriod,bool * periodFlushed)177 bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
178                                        bool* periodFlushed) {
179     assert(periodFlushed);
180 
181     std::lock_guard lock(mMutex);
182     if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
183         ATRACE_NAME("VSR: period confirmed");
184         if (mPeriodTransitioningTo) {
185             mTracker.setPeriod(*mPeriodTransitioningTo);
186             *periodFlushed = true;
187         }
188 
189         if (mLastHwVsync) {
190             mTracker.addVsyncTimestamp(*mLastHwVsync);
191         }
192         mTracker.addVsyncTimestamp(timestamp);
193 
194         endPeriodTransition();
195         mMoreSamplesNeeded = mTracker.needsMoreSamples();
196     } else if (mPeriodConfirmationInProgress) {
197         ATRACE_NAME("VSR: still confirming period");
198         mLastHwVsync = timestamp;
199         mMoreSamplesNeeded = true;
200         *periodFlushed = false;
201     } else {
202         ATRACE_NAME("VSR: adding sample");
203         *periodFlushed = false;
204         mTracker.addVsyncTimestamp(timestamp);
205         mMoreSamplesNeeded = mTracker.needsMoreSamples();
206     }
207 
208     if (!mMoreSamplesNeeded) {
209         setIgnorePresentFencesInternal(false);
210     }
211     return mMoreSamplesNeeded;
212 }
213 
setDisplayPowerMode(hal::PowerMode powerMode)214 void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
215     std::scoped_lock lock(mMutex);
216     mDisplayPowerMode = powerMode;
217 }
218 
dump(std::string & result) const219 void VSyncReactor::dump(std::string& result) const {
220     std::lock_guard lock(mMutex);
221     StringAppendF(&result, "VsyncReactor in use\n");
222     StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
223     StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
224                   mInternalIgnoreFences, mExternalIgnoreFences);
225     StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
226                   mMoreSamplesNeeded, mPeriodConfirmationInProgress);
227     if (mPeriodTransitioningTo) {
228         StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
229     } else {
230         StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
231     }
232 
233     if (mLastHwVsync) {
234         StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
235                       (mClock->now() - *mLastHwVsync) / 1e6f);
236     } else {
237         StringAppendF(&result, "No Last HW vsync\n");
238     }
239 
240     StringAppendF(&result, "VSyncTracker:\n");
241     mTracker.dump(result);
242 }
243 
244 } // namespace android::scheduler
245