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