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