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