Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 19 | #include "Scheduler.h" |
| 20 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 21 | #include <algorithm> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 22 | #include <cinttypes> |
| 23 | #include <cstdint> |
| 24 | #include <memory> |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 25 | #include <numeric> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 26 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 27 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 28 | #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h> |
| 29 | #include <android/hardware/configstore/1.2/ISurfaceFlingerConfigs.h> |
| 30 | #include <configstore/Utils.h> |
| 31 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 32 | #include <cutils/properties.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 33 | #include <gui/ISurfaceComposer.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 34 | #include <ui/DisplayStatInfo.h> |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 35 | #include <utils/Timers.h> |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 36 | #include <utils/Trace.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 37 | |
| 38 | #include "DispSync.h" |
| 39 | #include "DispSyncSource.h" |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 40 | #include "EventControlThread.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 41 | #include "EventThread.h" |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 42 | #include "IdleTimer.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 43 | #include "InjectVSyncSource.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 44 | #include "SchedulerUtils.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 48 | using namespace android::hardware::configstore; |
| 49 | using namespace android::hardware::configstore::V1_0; |
| 50 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 51 | #define RETURN_VALUE_IF_INVALID(value) \ |
| 52 | if (handle == nullptr || mConnections.count(handle->id) == 0) return value |
| 53 | #define RETURN_IF_INVALID() \ |
| 54 | if (handle == nullptr || mConnections.count(handle->id) == 0) return |
| 55 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 56 | std::atomic<int64_t> Scheduler::sNextId = 0; |
| 57 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 58 | Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function) |
| 59 | : mHasSyncFramework( |
| 60 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasSyncFramework>(true)), |
| 61 | mDispSyncPresentTimeOffset( |
| 62 | getInt64<ISurfaceFlingerConfigs, |
| 63 | &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(0)), |
| 64 | mPrimaryHWVsyncEnabled(false), |
| 65 | mHWVsyncAvailable(false) { |
| 66 | // Note: We create a local temporary with the real DispSync implementation |
| 67 | // type temporarily so we can initialize it with the configured values, |
| 68 | // before storing it for more generic use using the interface type. |
| 69 | auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync"); |
| 70 | primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset); |
| 71 | mPrimaryDispSync = std::move(primaryDispSync); |
| 72 | mEventControlThread = std::make_unique<impl::EventControlThread>(function); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 73 | |
| 74 | char value[PROPERTY_VALUE_MAX]; |
| 75 | property_get("debug.sf.set_idle_timer_ms", value, "0"); |
| 76 | mSetIdleTimerMs = atoi(value); |
| 77 | |
| 78 | if (mSetIdleTimerMs > 0) { |
| 79 | mIdleTimer = |
| 80 | std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs), |
| 81 | [this] { expiredTimerCallback(); }); |
| 82 | mIdleTimer->start(); |
| 83 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 86 | Scheduler::~Scheduler() = default; |
| 87 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 88 | sp<Scheduler::ConnectionHandle> Scheduler::createConnection( |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 89 | const std::string& connectionName, int64_t phaseOffsetNs, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 90 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 91 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 92 | const int64_t id = sNextId++; |
| 93 | ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id); |
| 94 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 95 | std::unique_ptr<EventThread> eventThread = |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 96 | makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, resyncCallback, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 97 | interceptCallback); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 98 | auto connection = std::make_unique<Connection>(new ConnectionHandle(id), |
| 99 | eventThread->createEventConnection(), |
| 100 | std::move(eventThread)); |
| 101 | mConnections.insert(std::make_pair(id, std::move(connection))); |
| 102 | return mConnections[id]->handle; |
| 103 | } |
| 104 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 105 | std::unique_ptr<EventThread> Scheduler::makeEventThread( |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 106 | const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 107 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 108 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 109 | const std::string sourceName = connectionName + "Source"; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 110 | std::unique_ptr<VSyncSource> eventThreadSource = |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 111 | std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str()); |
| 112 | const std::string threadName = connectionName + "Thread"; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 113 | return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback, |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 114 | interceptCallback, [this] { resetIdleTimer(); }, |
| 115 | threadName.c_str()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 118 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
| 119 | const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 120 | RETURN_VALUE_IF_INVALID(nullptr); |
| 121 | return mConnections[handle->id]->thread->createEventConnection(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 125 | RETURN_VALUE_IF_INVALID(nullptr); |
| 126 | return mConnections[handle->id]->thread.get(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 129 | sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 130 | RETURN_VALUE_IF_INVALID(nullptr); |
| 131 | return mConnections[handle->id]->eventConnection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle, |
| 135 | EventThread::DisplayType displayType, bool connected) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 136 | RETURN_IF_INVALID(); |
| 137 | mConnections[handle->id]->thread->onHotplugReceived(displayType, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 141 | RETURN_IF_INVALID(); |
| 142 | mConnections[handle->id]->thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 146 | RETURN_IF_INVALID(); |
| 147 | mConnections[handle->id]->thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 150 | void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 151 | RETURN_IF_INVALID(); |
| 152 | mConnections.at(handle->id)->thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 156 | RETURN_IF_INVALID(); |
| 157 | mConnections[handle->id]->thread->setPhaseOffset(phaseOffset); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 158 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 159 | |
| 160 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) { |
| 161 | stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0); |
| 162 | stats->vsyncPeriod = mPrimaryDispSync->getPeriod(); |
| 163 | } |
| 164 | |
| 165 | void Scheduler::enableHardwareVsync() { |
| 166 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 167 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
| 168 | mPrimaryDispSync->beginResync(); |
| 169 | mEventControlThread->setVsyncEnabled(true); |
| 170 | mPrimaryHWVsyncEnabled = true; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void Scheduler::disableHardwareVsync(bool makeUnavailable) { |
| 175 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 176 | if (mPrimaryHWVsyncEnabled) { |
| 177 | mEventControlThread->setVsyncEnabled(false); |
| 178 | mPrimaryDispSync->endResync(); |
| 179 | mPrimaryHWVsyncEnabled = false; |
| 180 | } |
| 181 | if (makeUnavailable) { |
| 182 | mHWVsyncAvailable = false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void Scheduler::setVsyncPeriod(const nsecs_t period) { |
| 187 | mPrimaryDispSync->reset(); |
| 188 | mPrimaryDispSync->setPeriod(period); |
| 189 | enableHardwareVsync(); |
| 190 | } |
| 191 | |
| 192 | void Scheduler::addResyncSample(const nsecs_t timestamp) { |
| 193 | bool needsHwVsync = false; |
| 194 | { // Scope for the lock |
| 195 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 196 | if (mPrimaryHWVsyncEnabled) { |
| 197 | needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (needsHwVsync) { |
| 202 | enableHardwareVsync(); |
| 203 | } else { |
| 204 | disableHardwareVsync(false); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
| 209 | if (mPrimaryDispSync->addPresentFence(fenceTime)) { |
| 210 | enableHardwareVsync(); |
| 211 | } else { |
| 212 | disableHardwareVsync(false); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void Scheduler::setIgnorePresentFences(bool ignore) { |
| 217 | mPrimaryDispSync->setIgnorePresentFences(ignore); |
| 218 | } |
| 219 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 220 | void Scheduler::makeHWSyncAvailable(bool makeAvailable) { |
| 221 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 222 | mHWVsyncAvailable = makeAvailable; |
| 223 | } |
| 224 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 225 | void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp, |
| 226 | const std::string layerName) { |
| 227 | // This is V1 logic. It calculates the average FPS based on the timestamp frequency |
| 228 | // regardless of which layer the timestamp came from. |
| 229 | // For now, the averages and FPS are recorded in the systrace. |
| 230 | determineTimestampAverage(isAutoTimestamp, framePresentTime); |
| 231 | |
| 232 | // This is V2 logic. It calculates the average and median timestamp difference based on the |
| 233 | // individual layer history. The results are recorded in the systrace. |
| 234 | determineLayerTimestampStats(layerName, framePresentTime); |
| 235 | } |
| 236 | |
| 237 | void Scheduler::incrementFrameCounter() { |
| 238 | mLayerHistory.incrementCounter(); |
| 239 | } |
| 240 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 241 | void Scheduler::updateFrameSkipping(const int64_t skipCount) { |
| 242 | ATRACE_INT("FrameSkipCount", skipCount); |
| 243 | if (mSkipCount != skipCount) { |
| 244 | // Only update DispSync if it hasn't been updated yet. |
| 245 | mPrimaryDispSync->setRefreshSkipCount(skipCount); |
| 246 | mSkipCount = skipCount; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void Scheduler::determineLayerTimestampStats(const std::string layerName, |
| 251 | const nsecs_t framePresentTime) { |
| 252 | mLayerHistory.insert(layerName, framePresentTime); |
| 253 | std::vector<int64_t> differencesMs; |
| 254 | |
| 255 | // Traverse through the layer history, and determine the differences in present times. |
| 256 | nsecs_t newestPresentTime = framePresentTime; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 257 | std::string differencesText = ""; |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 258 | for (int i = 1; i < mLayerHistory.getSize(); i++) { |
| 259 | std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i); |
| 260 | for (auto layer : layers) { |
| 261 | if (layer.first != layerName) { |
| 262 | continue; |
| 263 | } |
| 264 | int64_t differenceMs = (newestPresentTime - layer.second) / 1000000; |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 265 | // Dismiss noise. |
| 266 | if (differenceMs > 10 && differenceMs < 60) { |
| 267 | differencesMs.push_back(differenceMs); |
| 268 | } |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 269 | IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 270 | newestPresentTime = layer.second; |
| 271 | } |
| 272 | } |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 273 | ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str()); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 274 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 275 | if (!differencesMs.empty()) { |
| 276 | // Mean/Average is a good indicator for when 24fps videos are playing, because the frames |
| 277 | // come in 33, and 49 ms intervals with occasional 41ms. |
| 278 | const int64_t meanMs = scheduler::calculate_mean(differencesMs); |
| 279 | const auto tagMean = "TimestampMean_" + layerName; |
| 280 | ATRACE_INT(tagMean.c_str(), meanMs); |
| 281 | |
| 282 | // Mode and median are good indicators for 30 and 60 fps videos, because the majority of |
| 283 | // frames come in 16, or 33 ms intervals. |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 284 | const auto tagMedian = "TimestampMedian_" + layerName; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 285 | ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs)); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 286 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 287 | const auto tagMode = "TimestampMode_" + layerName; |
| 288 | ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs)); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 289 | } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) { |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 293 | ATRACE_INT("AutoTimestamp", isAutoTimestamp); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 294 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 295 | // Video does not have timestamp automatically set, so we discard timestamps that are |
| 296 | // coming in from other sources for now. |
| 297 | if (isAutoTimestamp) { |
| 298 | return; |
| 299 | } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 300 | int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000; |
| 301 | mPreviousFrameTimestamp = framePresentTime; |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 302 | |
| 303 | if (differenceMs < 10 || differenceMs > 100) { |
| 304 | // Dismiss noise. |
| 305 | return; |
| 306 | } |
| 307 | ATRACE_INT("TimestampDiff", differenceMs); |
| 308 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 309 | mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs; |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 310 | mCounter++; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 311 | int64_t mean = scheduler::calculate_mean(mTimeDifferences); |
| 312 | ATRACE_INT("AutoTimestampMean", mean); |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 313 | |
| 314 | // TODO(b/113612090): This are current numbers from trial and error while running videos |
| 315 | // from YouTube at 24, 30, and 60 fps. |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 316 | if (mean > 14 && mean < 18) { |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 317 | ATRACE_INT("FPS", 60); |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 318 | } else if (mean > 31 && mean < 34) { |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 319 | ATRACE_INT("FPS", 30); |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 320 | return; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 321 | } else if (mean > 39 && mean < 42) { |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 322 | ATRACE_INT("FPS", 24); |
| 323 | } |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 326 | void Scheduler::resetIdleTimer() { |
| 327 | if (mIdleTimer) { |
| 328 | mIdleTimer->reset(); |
| 329 | ATRACE_INT("ExpiredIdleTimer", 0); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void Scheduler::expiredTimerCallback() { |
| 334 | // TODO(b/113612090): Each time a timer expired, we should record the information into |
| 335 | // a circular buffer. Once this has happened a given amount (TBD) of times, we can comfortably |
| 336 | // say that the device is sitting in idle. |
| 337 | ATRACE_INT("ExpiredIdleTimer", 1); |
| 338 | } |
| 339 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 340 | } // namespace android |