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