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