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