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