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); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 76 | mSupportKernelTimer = support_kernel_idle_timer(false); |
Ady Abraham | be59c0d | 2019-03-05 13:01:13 -0800 | [diff] [blame] | 77 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 78 | mSetTouchTimerMs = set_touch_timer_ms(0); |
| 79 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 80 | char value[PROPERTY_VALUE_MAX]; |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 81 | property_get("debug.sf.set_idle_timer_ms", value, "0"); |
Ady Abraham | be59c0d | 2019-03-05 13:01:13 -0800 | [diff] [blame] | 82 | int int_value = atoi(value); |
| 83 | if (int_value) { |
| 84 | mSetIdleTimerMs = atoi(value); |
| 85 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 86 | |
| 87 | if (mSetIdleTimerMs > 0) { |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 88 | if (mSupportKernelTimer) { |
| 89 | mIdleTimer = |
| 90 | std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds( |
| 91 | mSetIdleTimerMs), |
| 92 | [this] { resetKernelTimerCallback(); }, |
| 93 | [this] { |
| 94 | expiredKernelTimerCallback(); |
| 95 | }); |
| 96 | } else { |
| 97 | mIdleTimer = std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds( |
| 98 | mSetIdleTimerMs), |
| 99 | [this] { resetTimerCallback(); }, |
| 100 | [this] { expiredTimerCallback(); }); |
| 101 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 102 | mIdleTimer->start(); |
| 103 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 104 | |
| 105 | if (mSetTouchTimerMs > 0) { |
| 106 | // Touch events are coming to SF every 100ms, so the timer needs to be higher than that |
| 107 | mTouchTimer = |
| 108 | std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetTouchTimerMs), |
| 109 | [this] { resetTouchTimerCallback(); }, |
| 110 | [this] { expiredTouchTimerCallback(); }); |
| 111 | mTouchTimer->start(); |
| 112 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Lloyd Pique | 1f9f1a4 | 2019-01-31 13:04:00 -0800 | [diff] [blame] | 115 | Scheduler::~Scheduler() { |
| 116 | // Ensure the IdleTimer thread is joined before we start destroying state. |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 117 | mTouchTimer.reset(); |
Lloyd Pique | 1f9f1a4 | 2019-01-31 13:04:00 -0800 | [diff] [blame] | 118 | mIdleTimer.reset(); |
| 119 | } |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 120 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 121 | sp<Scheduler::ConnectionHandle> Scheduler::createConnection( |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 122 | const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync, |
| 123 | ResyncCallback resyncCallback, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 124 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 125 | const int64_t id = sNextId++; |
| 126 | ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id); |
| 127 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 128 | std::unique_ptr<EventThread> eventThread = |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 129 | makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 130 | offsetThresholdForNextVsync, std::move(interceptCallback)); |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 131 | |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 132 | auto eventThreadConnection = |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 133 | createConnectionInternal(eventThread.get(), std::move(resyncCallback), |
| 134 | ISurfaceComposer::eConfigChangedSuppress); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 135 | mConnections.emplace(id, |
| 136 | std::make_unique<Connection>(new ConnectionHandle(id), |
| 137 | eventThreadConnection, |
| 138 | std::move(eventThread))); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 139 | return mConnections[id]->handle; |
| 140 | } |
| 141 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 142 | std::unique_ptr<EventThread> Scheduler::makeEventThread( |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 143 | const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs, |
| 144 | nsecs_t offsetThresholdForNextVsync, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 145 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 146 | std::unique_ptr<VSyncSource> eventThreadSource = |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 147 | std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, offsetThresholdForNextVsync, |
| 148 | true, connectionName); |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 149 | return std::make_unique<impl::EventThread>(std::move(eventThreadSource), |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 150 | std::move(interceptCallback), connectionName); |
| 151 | } |
| 152 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 153 | sp<EventThreadConnection> Scheduler::createConnectionInternal( |
| 154 | EventThread* eventThread, ResyncCallback&& resyncCallback, |
| 155 | ISurfaceComposer::ConfigChanged configChanged) { |
| 156 | return eventThread->createEventConnection(std::move(resyncCallback), configChanged); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 159 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 160 | const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback, |
| 161 | ISurfaceComposer::ConfigChanged configChanged) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 162 | RETURN_VALUE_IF_INVALID(nullptr); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 163 | return createConnectionInternal(mConnections[handle->id]->thread.get(), |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 164 | std::move(resyncCallback), configChanged); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 168 | RETURN_VALUE_IF_INVALID(nullptr); |
| 169 | return mConnections[handle->id]->thread.get(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 172 | sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 173 | RETURN_VALUE_IF_INVALID(nullptr); |
| 174 | return mConnections[handle->id]->eventConnection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle, |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 178 | PhysicalDisplayId displayId, bool connected) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 179 | RETURN_IF_INVALID(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 180 | mConnections[handle->id]->thread->onHotplugReceived(displayId, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 184 | RETURN_IF_INVALID(); |
| 185 | mConnections[handle->id]->thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 189 | RETURN_IF_INVALID(); |
| 190 | mConnections[handle->id]->thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 193 | void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId, |
| 194 | int32_t configId) { |
| 195 | RETURN_IF_INVALID(); |
| 196 | mConnections[handle->id]->thread->onConfigChanged(displayId, configId); |
| 197 | } |
| 198 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 199 | void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 200 | RETURN_IF_INVALID(); |
| 201 | mConnections.at(handle->id)->thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 205 | RETURN_IF_INVALID(); |
| 206 | mConnections[handle->id]->thread->setPhaseOffset(phaseOffset); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 207 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 208 | |
| 209 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) { |
| 210 | stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0); |
| 211 | stats->vsyncPeriod = mPrimaryDispSync->getPeriod(); |
| 212 | } |
| 213 | |
| 214 | void Scheduler::enableHardwareVsync() { |
| 215 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 216 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
| 217 | mPrimaryDispSync->beginResync(); |
| 218 | mEventControlThread->setVsyncEnabled(true); |
| 219 | mPrimaryHWVsyncEnabled = true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void Scheduler::disableHardwareVsync(bool makeUnavailable) { |
| 224 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 225 | if (mPrimaryHWVsyncEnabled) { |
| 226 | mEventControlThread->setVsyncEnabled(false); |
| 227 | mPrimaryDispSync->endResync(); |
| 228 | mPrimaryHWVsyncEnabled = false; |
| 229 | } |
| 230 | if (makeUnavailable) { |
| 231 | mHWVsyncAvailable = false; |
| 232 | } |
| 233 | } |
| 234 | |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 235 | void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) { |
| 236 | { |
| 237 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 238 | if (makeAvailable) { |
| 239 | mHWVsyncAvailable = makeAvailable; |
| 240 | } else if (!mHWVsyncAvailable) { |
| 241 | // Hardware vsync is not currently available, so abort the resync |
| 242 | // attempt for now |
| 243 | return; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (period <= 0) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | setVsyncPeriod(period); |
| 252 | } |
| 253 | |
| 254 | ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) { |
| 255 | std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState; |
| 256 | return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() { |
| 257 | if (const auto vsync = ptr.lock()) { |
| 258 | vsync->resync(getVsyncPeriod); |
| 259 | } |
| 260 | }; |
| 261 | } |
| 262 | |
| 263 | void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) { |
| 264 | static constexpr nsecs_t kIgnoreDelay = ms2ns(500); |
| 265 | |
| 266 | const nsecs_t now = systemTime(); |
| 267 | const nsecs_t last = lastResyncTime.exchange(now); |
| 268 | |
| 269 | if (now - last > kIgnoreDelay) { |
| 270 | scheduler.resyncToHardwareVsync(false, getVsyncPeriod()); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void Scheduler::setRefreshSkipCount(int count) { |
| 275 | mPrimaryDispSync->setRefreshSkipCount(count); |
| 276 | } |
| 277 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 278 | void Scheduler::setVsyncPeriod(const nsecs_t period) { |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 279 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 280 | mPrimaryDispSync->setPeriod(period); |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 281 | |
| 282 | if (!mPrimaryHWVsyncEnabled) { |
| 283 | mPrimaryDispSync->beginResync(); |
| 284 | mEventControlThread->setVsyncEnabled(true); |
| 285 | mPrimaryHWVsyncEnabled = true; |
| 286 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Alec Mouri | f8e689c | 2019-05-20 18:32:22 -0700 | [diff] [blame] | 289 | void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 290 | bool needsHwVsync = false; |
Alec Mouri | f8e689c | 2019-05-20 18:32:22 -0700 | [diff] [blame] | 291 | *periodFlushed = false; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 292 | { // Scope for the lock |
| 293 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 294 | if (mPrimaryHWVsyncEnabled) { |
Alec Mouri | f8e689c | 2019-05-20 18:32:22 -0700 | [diff] [blame] | 295 | needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | if (needsHwVsync) { |
| 300 | enableHardwareVsync(); |
| 301 | } else { |
| 302 | disableHardwareVsync(false); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
| 307 | if (mPrimaryDispSync->addPresentFence(fenceTime)) { |
| 308 | enableHardwareVsync(); |
| 309 | } else { |
| 310 | disableHardwareVsync(false); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void Scheduler::setIgnorePresentFences(bool ignore) { |
| 315 | mPrimaryDispSync->setIgnorePresentFences(ignore); |
| 316 | } |
| 317 | |
Ady Abraham | 8fe1102 | 2019-06-12 17:11:12 -0700 | [diff] [blame] | 318 | nsecs_t Scheduler::getDispSyncExpectedPresentTime() { |
Ady Abraham | c3e2131 | 2019-02-07 14:30:23 -0800 | [diff] [blame] | 319 | return mPrimaryDispSync->expectedPresentTime(); |
| 320 | } |
| 321 | |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 322 | void Scheduler::dumpPrimaryDispSync(std::string& result) const { |
| 323 | mPrimaryDispSync->dump(result); |
| 324 | } |
| 325 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 326 | std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer( |
Ady Abraham | 8f1ee7f | 2019-04-05 10:32:50 -0700 | [diff] [blame] | 327 | std::string const& name, int windowType) { |
| 328 | RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER) |
| 329 | ? RefreshRateType::DEFAULT |
| 330 | : RefreshRateType::PERFORMANCE; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 331 | |
| 332 | const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType); |
Ana Krulec | ad083c4 | 2019-06-26 16:28:08 -0700 | [diff] [blame] | 333 | const uint32_t performanceFps = (refreshRate) ? refreshRate->fps : 0; |
| 334 | |
| 335 | const auto defaultRefreshRate = mRefreshRateConfigs.getRefreshRate(RefreshRateType::DEFAULT); |
| 336 | const uint32_t defaultFps = (defaultRefreshRate) ? defaultRefreshRate->fps : 0; |
| 337 | return mLayerHistory.createLayer(name, defaultFps, performanceFps); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 340 | void Scheduler::addLayerPresentTimeAndHDR( |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 341 | const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 342 | nsecs_t presentTime, bool isHDR) { |
| 343 | mLayerHistory.insert(layerHandle, presentTime, isHDR); |
| 344 | } |
| 345 | |
| 346 | void Scheduler::setLayerVisibility( |
| 347 | const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) { |
| 348 | mLayerHistory.setVisibility(layerHandle, visible); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 351 | void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) { |
| 352 | fn(*mPrimaryDispSync); |
| 353 | } |
| 354 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 355 | void Scheduler::updateFpsBasedOnContent() { |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 356 | auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR(); |
| 357 | const uint32_t refreshRateRound = std::round(refreshRate); |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 358 | RefreshRateType newRefreshRateType; |
| 359 | { |
| 360 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 361 | if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) { |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 362 | return; |
| 363 | } |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 364 | mContentRefreshRate = refreshRateRound; |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 365 | ATRACE_INT("ContentFPS", mContentRefreshRate); |
| 366 | |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 367 | mIsHDRContent = isHDR; |
| 368 | ATRACE_INT("ContentHDR", mIsHDRContent); |
| 369 | |
| 370 | mCurrentContentFeatureState = refreshRateRound > 0 |
| 371 | ? ContentFeatureState::CONTENT_DETECTION_ON |
| 372 | : ContentFeatureState::CONTENT_DETECTION_OFF; |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 373 | newRefreshRateType = calculateRefreshRateType(); |
| 374 | if (mRefreshRateType == newRefreshRateType) { |
| 375 | return; |
| 376 | } |
| 377 | mRefreshRateType = newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 378 | } |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 379 | changeRefreshRate(newRefreshRateType, ConfigEvent::Changed); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Ana Krulec | 8d3e4f3 | 2019-03-05 10:40:33 -0800 | [diff] [blame] | 382 | void Scheduler::setChangeRefreshRateCallback( |
| 383 | const ChangeRefreshRateCallback& changeRefreshRateCallback) { |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 384 | std::lock_guard<std::mutex> lock(mCallbackLock); |
Ana Krulec | 8d3e4f3 | 2019-03-05 10:40:33 -0800 | [diff] [blame] | 385 | mChangeRefreshRateCallback = changeRefreshRateCallback; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 388 | void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) { |
| 389 | std::lock_guard<std::mutex> lock(mCallbackLock); |
| 390 | mGetVsyncPeriod = getVsyncPeriod; |
| 391 | } |
| 392 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 393 | void Scheduler::updateFrameSkipping(const int64_t skipCount) { |
| 394 | ATRACE_INT("FrameSkipCount", skipCount); |
| 395 | if (mSkipCount != skipCount) { |
| 396 | // Only update DispSync if it hasn't been updated yet. |
| 397 | mPrimaryDispSync->setRefreshSkipCount(skipCount); |
| 398 | mSkipCount = skipCount; |
| 399 | } |
| 400 | } |
| 401 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 402 | void Scheduler::resetIdleTimer() { |
| 403 | if (mIdleTimer) { |
| 404 | mIdleTimer->reset(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 408 | void Scheduler::notifyTouchEvent() { |
| 409 | if (mTouchTimer) { |
| 410 | mTouchTimer->reset(); |
| 411 | } |
| 412 | |
| 413 | if (mSupportKernelTimer) { |
| 414 | resetIdleTimer(); |
| 415 | } |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 416 | |
| 417 | // Touch event will boost the refresh rate to performance. |
| 418 | // Clear Layer History to get fresh FPS detection |
| 419 | mLayerHistory.clearHistory(); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 422 | void Scheduler::resetTimerCallback() { |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 423 | timerChangeRefreshRate(IdleTimerState::RESET); |
| 424 | ATRACE_INT("ExpiredIdleTimer", 0); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 427 | void Scheduler::resetKernelTimerCallback() { |
| 428 | ATRACE_INT("ExpiredKernelIdleTimer", 0); |
| 429 | std::lock_guard<std::mutex> lock(mCallbackLock); |
| 430 | if (mGetVsyncPeriod) { |
Alec Mouri | f8e689c | 2019-05-20 18:32:22 -0700 | [diff] [blame] | 431 | resyncToHardwareVsync(true, mGetVsyncPeriod()); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 435 | void Scheduler::expiredTimerCallback() { |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 436 | timerChangeRefreshRate(IdleTimerState::EXPIRED); |
| 437 | ATRACE_INT("ExpiredIdleTimer", 1); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 440 | void Scheduler::resetTouchTimerCallback() { |
| 441 | // We do not notify the applications about config changes when idle timer is reset. |
| 442 | touchChangeRefreshRate(TouchState::ACTIVE); |
| 443 | ATRACE_INT("TouchState", 1); |
| 444 | } |
| 445 | |
| 446 | void Scheduler::expiredTouchTimerCallback() { |
| 447 | // We do not notify the applications about config changes when idle timer expires. |
| 448 | touchChangeRefreshRate(TouchState::INACTIVE); |
| 449 | ATRACE_INT("TouchState", 0); |
| 450 | } |
| 451 | |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 452 | void Scheduler::expiredKernelTimerCallback() { |
| 453 | ATRACE_INT("ExpiredKernelIdleTimer", 1); |
| 454 | // Disable HW Vsync if the timer expired, as we don't need it |
| 455 | // enabled if we're not pushing frames. |
| 456 | disableHardwareVsync(false); |
| 457 | } |
| 458 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 459 | std::string Scheduler::doDump() { |
| 460 | std::ostringstream stream; |
| 461 | stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 462 | stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 463 | return stream.str(); |
| 464 | } |
| 465 | |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 466 | void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 467 | RefreshRateType newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 468 | { |
| 469 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 470 | if (mCurrentIdleTimerState == idleTimerState) { |
| 471 | return; |
| 472 | } |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 473 | mCurrentIdleTimerState = idleTimerState; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 474 | newRefreshRateType = calculateRefreshRateType(); |
Ady Abraham | 6398a0a | 2019-04-18 19:30:44 -0700 | [diff] [blame] | 475 | if (mRefreshRateType == newRefreshRateType) { |
| 476 | return; |
| 477 | } |
| 478 | mRefreshRateType = newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 479 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 480 | changeRefreshRate(newRefreshRateType, ConfigEvent::None); |
| 481 | } |
| 482 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 483 | void Scheduler::touchChangeRefreshRate(TouchState touchState) { |
| 484 | ConfigEvent event = ConfigEvent::None; |
| 485 | RefreshRateType newRefreshRateType; |
| 486 | { |
| 487 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 488 | if (mCurrentTouchState == touchState) { |
| 489 | return; |
| 490 | } |
| 491 | mCurrentTouchState = touchState; |
| 492 | newRefreshRateType = calculateRefreshRateType(); |
| 493 | if (mRefreshRateType == newRefreshRateType) { |
| 494 | return; |
| 495 | } |
| 496 | mRefreshRateType = newRefreshRateType; |
| 497 | // Send an event in case that content detection is on as touch has a higher priority |
| 498 | if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) { |
| 499 | event = ConfigEvent::Changed; |
| 500 | } |
| 501 | } |
| 502 | changeRefreshRate(newRefreshRateType, event); |
| 503 | } |
| 504 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 505 | Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() { |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 506 | // HDR content is not supported on PERFORMANCE mode |
| 507 | if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 508 | return RefreshRateType::DEFAULT; |
| 509 | } |
| 510 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 511 | // As long as touch is active we want to be in performance mode |
| 512 | if (mCurrentTouchState == TouchState::ACTIVE) { |
| 513 | return RefreshRateType::PERFORMANCE; |
| 514 | } |
| 515 | |
| 516 | // If timer has expired as it means there is no new content on the screen |
| 517 | if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) { |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 518 | return RefreshRateType::DEFAULT; |
| 519 | } |
| 520 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 521 | // If content detection is off we choose performance as we don't know the content fps |
| 522 | if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) { |
| 523 | return RefreshRateType::PERFORMANCE; |
| 524 | } |
| 525 | |
Wei Wang | 09be73f | 2019-07-02 14:29:18 -0700 | [diff] [blame^] | 526 | // Content detection is on, find the appropriate refresh rate with minimal error |
| 527 | auto iter = min_element(mRefreshRateConfigs.getRefreshRates().cbegin(), |
| 528 | mRefreshRateConfigs.getRefreshRates().cend(), |
| 529 | [rate = mContentRefreshRate](const auto& l, const auto& r) -> bool { |
| 530 | return std::abs(l.second->fps - static_cast<float>(rate)) < |
| 531 | std::abs(r.second->fps - static_cast<float>(rate)); |
| 532 | }); |
| 533 | RefreshRateType currRefreshRateType = iter->first; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 534 | |
Ady Abraham | 85b3f01 | 2019-04-08 11:04:14 -0700 | [diff] [blame] | 535 | // Some content aligns better on higher refresh rate. For example for 45fps we should choose |
| 536 | // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't |
| 537 | // align well with both |
Wei Wang | 09be73f | 2019-07-02 14:29:18 -0700 | [diff] [blame^] | 538 | constexpr float MARGIN = 0.05f; |
Ady Abraham | 85b3f01 | 2019-04-08 11:04:14 -0700 | [diff] [blame] | 539 | float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps / |
| 540 | float(mContentRefreshRate); |
| 541 | if (std::abs(std::round(ratio) - ratio) > MARGIN) { |
| 542 | while (iter != mRefreshRateConfigs.getRefreshRates().cend()) { |
| 543 | ratio = iter->second->fps / float(mContentRefreshRate); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 544 | |
Ady Abraham | 85b3f01 | 2019-04-08 11:04:14 -0700 | [diff] [blame] | 545 | if (std::abs(std::round(ratio) - ratio) <= MARGIN) { |
| 546 | currRefreshRateType = iter->first; |
| 547 | break; |
| 548 | } |
| 549 | ++iter; |
| 550 | } |
| 551 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 552 | |
| 553 | return currRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) { |
| 557 | std::lock_guard<std::mutex> lock(mCallbackLock); |
| 558 | if (mChangeRefreshRateCallback) { |
| 559 | mChangeRefreshRateCallback(refreshRateType, configEvent); |
| 560 | } |
| 561 | } |
| 562 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 563 | } // namespace android |