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> |
Ady Abraham | 8f1ee7f | 2019-04-05 10:32:50 -0700 | [diff] [blame] | 31 | #include <input/InputWindow.h> |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 32 | #include <system/window.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" |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 43 | #include "LayerInfo.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 44 | #include "SchedulerUtils.h" |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 45 | #include "SurfaceFlingerProperties.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 49 | using namespace android::hardware::configstore; |
| 50 | using namespace android::hardware::configstore::V1_0; |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 51 | using namespace android::sysprop; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 52 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 53 | #define RETURN_VALUE_IF_INVALID(value) \ |
| 54 | if (handle == nullptr || mConnections.count(handle->id) == 0) return value |
| 55 | #define RETURN_IF_INVALID() \ |
| 56 | if (handle == nullptr || mConnections.count(handle->id) == 0) return |
| 57 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 58 | std::atomic<int64_t> Scheduler::sNextId = 0; |
| 59 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 60 | Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function, |
| 61 | const scheduler::RefreshRateConfigs& refreshRateConfig) |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 62 | : mHasSyncFramework(running_without_sync_framework(true)), |
| 63 | mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)), |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 64 | mPrimaryHWVsyncEnabled(false), |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 65 | mHWVsyncAvailable(false), |
| 66 | mRefreshRateConfigs(refreshRateConfig) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 67 | // Note: We create a local temporary with the real DispSync implementation |
| 68 | // type temporarily so we can initialize it with the configured values, |
| 69 | // before storing it for more generic use using the interface type. |
| 70 | auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync"); |
| 71 | primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset); |
| 72 | mPrimaryDispSync = std::move(primaryDispSync); |
| 73 | mEventControlThread = std::make_unique<impl::EventControlThread>(function); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 74 | |
Ady Abraham | be59c0d | 2019-03-05 13:01:13 -0800 | [diff] [blame] | 75 | mSetIdleTimerMs = set_idle_timer_ms(0); |
| 76 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 77 | char value[PROPERTY_VALUE_MAX]; |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 78 | property_get("debug.sf.set_idle_timer_ms", value, "0"); |
Ady Abraham | be59c0d | 2019-03-05 13:01:13 -0800 | [diff] [blame] | 79 | int int_value = atoi(value); |
| 80 | if (int_value) { |
| 81 | mSetIdleTimerMs = atoi(value); |
| 82 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 83 | |
| 84 | if (mSetIdleTimerMs > 0) { |
| 85 | mIdleTimer = |
| 86 | std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs), |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 87 | [this] { resetTimerCallback(); }, |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 88 | [this] { expiredTimerCallback(); }); |
| 89 | mIdleTimer->start(); |
| 90 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Lloyd Pique | 1f9f1a4 | 2019-01-31 13:04:00 -0800 | [diff] [blame] | 93 | Scheduler::~Scheduler() { |
| 94 | // Ensure the IdleTimer thread is joined before we start destroying state. |
| 95 | mIdleTimer.reset(); |
| 96 | } |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 97 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 98 | sp<Scheduler::ConnectionHandle> Scheduler::createConnection( |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 99 | const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 100 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 101 | const int64_t id = sNextId++; |
| 102 | ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id); |
| 103 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 104 | std::unique_ptr<EventThread> eventThread = |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 105 | makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 106 | std::move(interceptCallback)); |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 107 | |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 108 | auto eventThreadConnection = |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 109 | createConnectionInternal(eventThread.get(), std::move(resyncCallback)); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 110 | mConnections.emplace(id, |
| 111 | std::make_unique<Connection>(new ConnectionHandle(id), |
| 112 | eventThreadConnection, |
| 113 | std::move(eventThread))); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 114 | return mConnections[id]->handle; |
| 115 | } |
| 116 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 117 | std::unique_ptr<EventThread> Scheduler::makeEventThread( |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 118 | const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 119 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 120 | std::unique_ptr<VSyncSource> eventThreadSource = |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 121 | std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName); |
| 122 | return std::make_unique<impl::EventThread>(std::move(eventThreadSource), |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 123 | std::move(interceptCallback), connectionName); |
| 124 | } |
| 125 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 126 | sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread, |
| 127 | ResyncCallback&& resyncCallback) { |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 128 | return eventThread->createEventConnection(std::move(resyncCallback), |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 129 | [this] { resetIdleTimer(); }); |
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( |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 133 | const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 134 | RETURN_VALUE_IF_INVALID(nullptr); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 135 | return createConnectionInternal(mConnections[handle->id]->thread.get(), |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 136 | std::move(resyncCallback)); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 140 | RETURN_VALUE_IF_INVALID(nullptr); |
| 141 | return mConnections[handle->id]->thread.get(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 144 | sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 145 | RETURN_VALUE_IF_INVALID(nullptr); |
| 146 | return mConnections[handle->id]->eventConnection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle, |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 150 | PhysicalDisplayId displayId, bool connected) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 151 | RETURN_IF_INVALID(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 152 | mConnections[handle->id]->thread->onHotplugReceived(displayId, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 156 | RETURN_IF_INVALID(); |
| 157 | mConnections[handle->id]->thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 161 | RETURN_IF_INVALID(); |
| 162 | mConnections[handle->id]->thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 165 | void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId, |
| 166 | int32_t configId) { |
| 167 | RETURN_IF_INVALID(); |
| 168 | mConnections[handle->id]->thread->onConfigChanged(displayId, configId); |
| 169 | } |
| 170 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 171 | void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 172 | RETURN_IF_INVALID(); |
| 173 | mConnections.at(handle->id)->thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 177 | RETURN_IF_INVALID(); |
| 178 | mConnections[handle->id]->thread->setPhaseOffset(phaseOffset); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 179 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 180 | |
| 181 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) { |
| 182 | stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0); |
| 183 | stats->vsyncPeriod = mPrimaryDispSync->getPeriod(); |
| 184 | } |
| 185 | |
| 186 | void Scheduler::enableHardwareVsync() { |
| 187 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 188 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
| 189 | mPrimaryDispSync->beginResync(); |
| 190 | mEventControlThread->setVsyncEnabled(true); |
| 191 | mPrimaryHWVsyncEnabled = true; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void Scheduler::disableHardwareVsync(bool makeUnavailable) { |
| 196 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 197 | if (mPrimaryHWVsyncEnabled) { |
| 198 | mEventControlThread->setVsyncEnabled(false); |
| 199 | mPrimaryDispSync->endResync(); |
| 200 | mPrimaryHWVsyncEnabled = false; |
| 201 | } |
| 202 | if (makeUnavailable) { |
| 203 | mHWVsyncAvailable = false; |
| 204 | } |
| 205 | } |
| 206 | |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 207 | void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) { |
| 208 | { |
| 209 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 210 | if (makeAvailable) { |
| 211 | mHWVsyncAvailable = makeAvailable; |
| 212 | } else if (!mHWVsyncAvailable) { |
| 213 | // Hardware vsync is not currently available, so abort the resync |
| 214 | // attempt for now |
| 215 | return; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (period <= 0) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | setVsyncPeriod(period); |
| 224 | } |
| 225 | |
| 226 | ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) { |
| 227 | std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState; |
| 228 | return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() { |
| 229 | if (const auto vsync = ptr.lock()) { |
| 230 | vsync->resync(getVsyncPeriod); |
| 231 | } |
| 232 | }; |
| 233 | } |
| 234 | |
| 235 | void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) { |
| 236 | static constexpr nsecs_t kIgnoreDelay = ms2ns(500); |
| 237 | |
| 238 | const nsecs_t now = systemTime(); |
| 239 | const nsecs_t last = lastResyncTime.exchange(now); |
| 240 | |
| 241 | if (now - last > kIgnoreDelay) { |
| 242 | scheduler.resyncToHardwareVsync(false, getVsyncPeriod()); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void Scheduler::setRefreshSkipCount(int count) { |
| 247 | mPrimaryDispSync->setRefreshSkipCount(count); |
| 248 | } |
| 249 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 250 | void Scheduler::setVsyncPeriod(const nsecs_t period) { |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 251 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 252 | mPrimaryDispSync->setPeriod(period); |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 253 | |
| 254 | if (!mPrimaryHWVsyncEnabled) { |
| 255 | mPrimaryDispSync->beginResync(); |
| 256 | mEventControlThread->setVsyncEnabled(true); |
| 257 | mPrimaryHWVsyncEnabled = true; |
| 258 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 261 | void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodChanged) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 262 | bool needsHwVsync = false; |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 263 | *periodChanged = false; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 264 | { // Scope for the lock |
| 265 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 266 | if (mPrimaryHWVsyncEnabled) { |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 267 | needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodChanged); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | if (needsHwVsync) { |
| 272 | enableHardwareVsync(); |
| 273 | } else { |
| 274 | disableHardwareVsync(false); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
| 279 | if (mPrimaryDispSync->addPresentFence(fenceTime)) { |
| 280 | enableHardwareVsync(); |
| 281 | } else { |
| 282 | disableHardwareVsync(false); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void Scheduler::setIgnorePresentFences(bool ignore) { |
| 287 | mPrimaryDispSync->setIgnorePresentFences(ignore); |
| 288 | } |
| 289 | |
Ady Abraham | c3e2131 | 2019-02-07 14:30:23 -0800 | [diff] [blame] | 290 | nsecs_t Scheduler::expectedPresentTime() { |
| 291 | return mPrimaryDispSync->expectedPresentTime(); |
| 292 | } |
| 293 | |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 294 | void Scheduler::dumpPrimaryDispSync(std::string& result) const { |
| 295 | mPrimaryDispSync->dump(result); |
| 296 | } |
| 297 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 298 | std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer( |
Ady Abraham | 8f1ee7f | 2019-04-05 10:32:50 -0700 | [diff] [blame] | 299 | std::string const& name, int windowType) { |
| 300 | RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER) |
| 301 | ? RefreshRateType::DEFAULT |
| 302 | : RefreshRateType::PERFORMANCE; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 303 | |
| 304 | const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType); |
| 305 | const uint32_t fps = (refreshRate) ? refreshRate->fps : 0; |
| 306 | return mLayerHistory.createLayer(name, fps); |
| 307 | } |
| 308 | |
| 309 | void Scheduler::addLayerPresentTime( |
| 310 | const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, |
| 311 | nsecs_t presentTime) { |
| 312 | mLayerHistory.insert(layerHandle, presentTime); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 315 | void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) { |
| 316 | fn(*mPrimaryDispSync); |
| 317 | } |
| 318 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 319 | void Scheduler::updateFpsBasedOnContent() { |
| 320 | uint32_t refreshRate = std::round(mLayerHistory.getDesiredRefreshRate()); |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame^] | 321 | RefreshRateType newRefreshRateType; |
| 322 | { |
| 323 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 324 | if (mContentRefreshRate == refreshRate) { |
| 325 | return; |
| 326 | } |
| 327 | mContentRefreshRate = refreshRate; |
| 328 | ATRACE_INT("ContentFPS", mContentRefreshRate); |
| 329 | |
| 330 | mCurrentContentFeatureState = refreshRate > 0 ? ContentFeatureState::CONTENT_DETECTION_ON |
| 331 | : ContentFeatureState::CONTENT_DETECTION_OFF; |
| 332 | newRefreshRateType = calculateRefreshRateType(); |
| 333 | if (mRefreshRateType == newRefreshRateType) { |
| 334 | return; |
| 335 | } |
| 336 | mRefreshRateType = newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 337 | } |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame^] | 338 | changeRefreshRate(newRefreshRateType, ConfigEvent::Changed); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Ana Krulec | 8d3e4f3 | 2019-03-05 10:40:33 -0800 | [diff] [blame] | 341 | void Scheduler::setChangeRefreshRateCallback( |
| 342 | const ChangeRefreshRateCallback& changeRefreshRateCallback) { |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 343 | std::lock_guard<std::mutex> lock(mCallbackLock); |
Ana Krulec | 8d3e4f3 | 2019-03-05 10:40:33 -0800 | [diff] [blame] | 344 | mChangeRefreshRateCallback = changeRefreshRateCallback; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 347 | void Scheduler::updateFrameSkipping(const int64_t skipCount) { |
| 348 | ATRACE_INT("FrameSkipCount", skipCount); |
| 349 | if (mSkipCount != skipCount) { |
| 350 | // Only update DispSync if it hasn't been updated yet. |
| 351 | mPrimaryDispSync->setRefreshSkipCount(skipCount); |
| 352 | mSkipCount = skipCount; |
| 353 | } |
| 354 | } |
| 355 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 356 | void Scheduler::resetIdleTimer() { |
| 357 | if (mIdleTimer) { |
| 358 | mIdleTimer->reset(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | void Scheduler::resetTimerCallback() { |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 363 | // We do not notify the applications about config changes when idle timer is reset. |
| 364 | timerChangeRefreshRate(IdleTimerState::RESET); |
| 365 | ATRACE_INT("ExpiredIdleTimer", 0); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void Scheduler::expiredTimerCallback() { |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 369 | // We do not notify the applications about config changes when idle timer expires. |
| 370 | timerChangeRefreshRate(IdleTimerState::EXPIRED); |
| 371 | ATRACE_INT("ExpiredIdleTimer", 1); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 374 | std::string Scheduler::doDump() { |
| 375 | std::ostringstream stream; |
| 376 | stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl; |
| 377 | return stream.str(); |
| 378 | } |
| 379 | |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 380 | void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 381 | RefreshRateType newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 382 | { |
| 383 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame^] | 384 | if (mCurrentIdleTimerState == idleTimerState) { |
| 385 | return; |
| 386 | } |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 387 | mCurrentIdleTimerState = idleTimerState; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 388 | newRefreshRateType = calculateRefreshRateType(); |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame^] | 389 | if (mRefreshRateType == newRefreshRateType) { |
| 390 | return; |
| 391 | } |
| 392 | mRefreshRateType = newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 393 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 394 | changeRefreshRate(newRefreshRateType, ConfigEvent::None); |
| 395 | } |
| 396 | |
| 397 | Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() { |
| 398 | // First check if timer has expired as it means there is no new content on the screen |
| 399 | if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) { |
| 400 | return RefreshRateType::DEFAULT; |
| 401 | } |
| 402 | |
| 403 | // If content detection is off we choose performance as we don't know the content fps |
| 404 | if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) { |
| 405 | return RefreshRateType::PERFORMANCE; |
| 406 | } |
| 407 | |
| 408 | // Content detection is on, find the appropriate refresh rate |
Ady Abraham | 85b3f01 | 2019-04-08 11:04:14 -0700 | [diff] [blame] | 409 | // Start with the smallest refresh rate which is within a margin of the content |
| 410 | RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE; |
| 411 | constexpr float MARGIN = 0.05f; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 412 | auto iter = mRefreshRateConfigs.getRefreshRates().cbegin(); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 413 | while (iter != mRefreshRateConfigs.getRefreshRates().cend()) { |
Ady Abraham | 85b3f01 | 2019-04-08 11:04:14 -0700 | [diff] [blame] | 414 | if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 415 | currRefreshRateType = iter->first; |
| 416 | break; |
| 417 | } |
| 418 | ++iter; |
| 419 | } |
| 420 | |
Ady Abraham | 85b3f01 | 2019-04-08 11:04:14 -0700 | [diff] [blame] | 421 | // Some content aligns better on higher refresh rate. For example for 45fps we should choose |
| 422 | // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't |
| 423 | // align well with both |
| 424 | float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps / |
| 425 | float(mContentRefreshRate); |
| 426 | if (std::abs(std::round(ratio) - ratio) > MARGIN) { |
| 427 | while (iter != mRefreshRateConfigs.getRefreshRates().cend()) { |
| 428 | ratio = iter->second->fps / float(mContentRefreshRate); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 429 | |
Ady Abraham | 85b3f01 | 2019-04-08 11:04:14 -0700 | [diff] [blame] | 430 | if (std::abs(std::round(ratio) - ratio) <= MARGIN) { |
| 431 | currRefreshRateType = iter->first; |
| 432 | break; |
| 433 | } |
| 434 | ++iter; |
| 435 | } |
| 436 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 437 | |
| 438 | return currRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) { |
| 442 | std::lock_guard<std::mutex> lock(mCallbackLock); |
| 443 | if (mChangeRefreshRateCallback) { |
| 444 | mChangeRefreshRateCallback(refreshRateType, configEvent); |
| 445 | } |
| 446 | } |
| 447 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 448 | } // namespace android |