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