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