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> |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 31 | #include <system/window.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 32 | #include <ui/DisplayStatInfo.h> |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 33 | #include <utils/Timers.h> |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 34 | #include <utils/Trace.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 35 | |
| 36 | #include "DispSync.h" |
| 37 | #include "DispSyncSource.h" |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 38 | #include "EventControlThread.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 39 | #include "EventThread.h" |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 40 | #include "IdleTimer.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 41 | #include "InjectVSyncSource.h" |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 42 | #include "LayerInfo.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 43 | #include "SchedulerUtils.h" |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 44 | #include "SurfaceFlingerProperties.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 48 | using namespace android::hardware::configstore; |
| 49 | using namespace android::hardware::configstore::V1_0; |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 50 | using namespace android::sysprop; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 51 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 52 | #define RETURN_VALUE_IF_INVALID(value) \ |
| 53 | if (handle == nullptr || mConnections.count(handle->id) == 0) return value |
| 54 | #define RETURN_IF_INVALID() \ |
| 55 | if (handle == nullptr || mConnections.count(handle->id) == 0) return |
| 56 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 57 | std::atomic<int64_t> Scheduler::sNextId = 0; |
| 58 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 59 | Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function, |
| 60 | const scheduler::RefreshRateConfigs& refreshRateConfig) |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 61 | : mHasSyncFramework(running_without_sync_framework(true)), |
| 62 | mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)), |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 63 | mPrimaryHWVsyncEnabled(false), |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 64 | mHWVsyncAvailable(false), |
| 65 | mRefreshRateConfigs(refreshRateConfig) { |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 66 | // Note: We create a local temporary with the real DispSync implementation |
| 67 | // type temporarily so we can initialize it with the configured values, |
| 68 | // before storing it for more generic use using the interface type. |
| 69 | auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync"); |
| 70 | primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset); |
| 71 | mPrimaryDispSync = std::move(primaryDispSync); |
| 72 | mEventControlThread = std::make_unique<impl::EventControlThread>(function); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 73 | |
Ady Abraham | be59c0d | 2019-03-05 13:01:13 -0800 | [diff] [blame] | 74 | mSetIdleTimerMs = set_idle_timer_ms(0); |
| 75 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 76 | char value[PROPERTY_VALUE_MAX]; |
Ana Krulec | a5bdd9d | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 77 | property_get("debug.sf.set_idle_timer_ms", value, "0"); |
Ady Abraham | be59c0d | 2019-03-05 13:01:13 -0800 | [diff] [blame] | 78 | int int_value = atoi(value); |
| 79 | if (int_value) { |
| 80 | mSetIdleTimerMs = atoi(value); |
| 81 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 82 | |
| 83 | if (mSetIdleTimerMs > 0) { |
| 84 | mIdleTimer = |
| 85 | std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs), |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 86 | [this] { resetTimerCallback(); }, |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 87 | [this] { expiredTimerCallback(); }); |
| 88 | mIdleTimer->start(); |
| 89 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Lloyd Pique | 1f9f1a4 | 2019-01-31 13:04:00 -0800 | [diff] [blame] | 92 | Scheduler::~Scheduler() { |
| 93 | // Ensure the IdleTimer thread is joined before we start destroying state. |
| 94 | mIdleTimer.reset(); |
| 95 | } |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 96 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 97 | sp<Scheduler::ConnectionHandle> Scheduler::createConnection( |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 98 | const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 99 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 100 | const int64_t id = sNextId++; |
| 101 | ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id); |
| 102 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 103 | std::unique_ptr<EventThread> eventThread = |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 104 | makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 105 | std::move(interceptCallback)); |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 106 | |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 107 | auto eventThreadConnection = |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 108 | createConnectionInternal(eventThread.get(), std::move(resyncCallback)); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 109 | mConnections.emplace(id, |
| 110 | std::make_unique<Connection>(new ConnectionHandle(id), |
| 111 | eventThreadConnection, |
| 112 | std::move(eventThread))); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 113 | return mConnections[id]->handle; |
| 114 | } |
| 115 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 116 | std::unique_ptr<EventThread> Scheduler::makeEventThread( |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 117 | const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 118 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 119 | std::unique_ptr<VSyncSource> eventThreadSource = |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 120 | std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName); |
| 121 | return std::make_unique<impl::EventThread>(std::move(eventThreadSource), |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 122 | std::move(interceptCallback), connectionName); |
| 123 | } |
| 124 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 125 | sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread, |
| 126 | ResyncCallback&& resyncCallback) { |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 127 | return eventThread->createEventConnection(std::move(resyncCallback), |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 128 | [this] { resetIdleTimer(); }); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 131 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 132 | const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 133 | RETURN_VALUE_IF_INVALID(nullptr); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 134 | return createConnectionInternal(mConnections[handle->id]->thread.get(), |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 135 | std::move(resyncCallback)); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 139 | RETURN_VALUE_IF_INVALID(nullptr); |
| 140 | return mConnections[handle->id]->thread.get(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 143 | sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 144 | RETURN_VALUE_IF_INVALID(nullptr); |
| 145 | return mConnections[handle->id]->eventConnection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle, |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 149 | PhysicalDisplayId displayId, bool connected) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 150 | RETURN_IF_INVALID(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 151 | mConnections[handle->id]->thread->onHotplugReceived(displayId, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 155 | RETURN_IF_INVALID(); |
| 156 | mConnections[handle->id]->thread->onScreenAcquired(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 160 | RETURN_IF_INVALID(); |
| 161 | mConnections[handle->id]->thread->onScreenReleased(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 164 | void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId, |
| 165 | int32_t configId) { |
| 166 | RETURN_IF_INVALID(); |
| 167 | mConnections[handle->id]->thread->onConfigChanged(displayId, configId); |
| 168 | } |
| 169 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 170 | void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 171 | RETURN_IF_INVALID(); |
| 172 | mConnections.at(handle->id)->thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 176 | RETURN_IF_INVALID(); |
| 177 | mConnections[handle->id]->thread->setPhaseOffset(phaseOffset); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 178 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 179 | |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 180 | void Scheduler::pauseVsyncCallback(const android::sp<android::Scheduler::ConnectionHandle>& handle, |
| 181 | bool pause) { |
| 182 | RETURN_IF_INVALID(); |
| 183 | mConnections[handle->id]->thread->pauseVsyncCallback(pause); |
| 184 | } |
| 185 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 186 | void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) { |
| 187 | stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0); |
| 188 | stats->vsyncPeriod = mPrimaryDispSync->getPeriod(); |
| 189 | } |
| 190 | |
| 191 | void Scheduler::enableHardwareVsync() { |
| 192 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 193 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
| 194 | mPrimaryDispSync->beginResync(); |
| 195 | mEventControlThread->setVsyncEnabled(true); |
| 196 | mPrimaryHWVsyncEnabled = true; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void Scheduler::disableHardwareVsync(bool makeUnavailable) { |
| 201 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 202 | if (mPrimaryHWVsyncEnabled) { |
| 203 | mEventControlThread->setVsyncEnabled(false); |
| 204 | mPrimaryDispSync->endResync(); |
| 205 | mPrimaryHWVsyncEnabled = false; |
| 206 | } |
| 207 | if (makeUnavailable) { |
| 208 | mHWVsyncAvailable = false; |
| 209 | } |
| 210 | } |
| 211 | |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 212 | void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) { |
| 213 | { |
| 214 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 215 | if (makeAvailable) { |
| 216 | mHWVsyncAvailable = makeAvailable; |
| 217 | } else if (!mHWVsyncAvailable) { |
| 218 | // Hardware vsync is not currently available, so abort the resync |
| 219 | // attempt for now |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (period <= 0) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | setVsyncPeriod(period); |
| 229 | } |
| 230 | |
| 231 | ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) { |
| 232 | std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState; |
| 233 | return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() { |
| 234 | if (const auto vsync = ptr.lock()) { |
| 235 | vsync->resync(getVsyncPeriod); |
| 236 | } |
| 237 | }; |
| 238 | } |
| 239 | |
| 240 | void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) { |
| 241 | static constexpr nsecs_t kIgnoreDelay = ms2ns(500); |
| 242 | |
| 243 | const nsecs_t now = systemTime(); |
| 244 | const nsecs_t last = lastResyncTime.exchange(now); |
| 245 | |
| 246 | if (now - last > kIgnoreDelay) { |
| 247 | scheduler.resyncToHardwareVsync(false, getVsyncPeriod()); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void Scheduler::setRefreshSkipCount(int count) { |
| 252 | mPrimaryDispSync->setRefreshSkipCount(count); |
| 253 | } |
| 254 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 255 | void Scheduler::setVsyncPeriod(const nsecs_t period) { |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 256 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 257 | mPrimaryDispSync->reset(); |
| 258 | mPrimaryDispSync->setPeriod(period); |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 259 | |
| 260 | if (!mPrimaryHWVsyncEnabled) { |
| 261 | mPrimaryDispSync->beginResync(); |
| 262 | mEventControlThread->setVsyncEnabled(true); |
| 263 | mPrimaryHWVsyncEnabled = true; |
| 264 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void Scheduler::addResyncSample(const nsecs_t timestamp) { |
| 268 | bool needsHwVsync = false; |
| 269 | { // Scope for the lock |
| 270 | std::lock_guard<std::mutex> lock(mHWVsyncLock); |
| 271 | if (mPrimaryHWVsyncEnabled) { |
| 272 | needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (needsHwVsync) { |
| 277 | enableHardwareVsync(); |
| 278 | } else { |
| 279 | disableHardwareVsync(false); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { |
| 284 | if (mPrimaryDispSync->addPresentFence(fenceTime)) { |
| 285 | enableHardwareVsync(); |
| 286 | } else { |
| 287 | disableHardwareVsync(false); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void Scheduler::setIgnorePresentFences(bool ignore) { |
| 292 | mPrimaryDispSync->setIgnorePresentFences(ignore); |
| 293 | } |
| 294 | |
Ady Abraham | c3e2131 | 2019-02-07 14:30:23 -0800 | [diff] [blame] | 295 | nsecs_t Scheduler::expectedPresentTime() { |
| 296 | return mPrimaryDispSync->expectedPresentTime(); |
| 297 | } |
| 298 | |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 299 | void Scheduler::dumpPrimaryDispSync(std::string& result) const { |
| 300 | mPrimaryDispSync->dump(result); |
| 301 | } |
| 302 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 303 | std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer( |
| 304 | const std::string name) { |
| 305 | RefreshRateType refreshRateType = RefreshRateType::PERFORMANCE; |
| 306 | |
| 307 | const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType); |
| 308 | const uint32_t fps = (refreshRate) ? refreshRate->fps : 0; |
| 309 | return mLayerHistory.createLayer(name, fps); |
| 310 | } |
| 311 | |
| 312 | void Scheduler::addLayerPresentTime( |
| 313 | const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, |
| 314 | nsecs_t presentTime) { |
| 315 | mLayerHistory.insert(layerHandle, presentTime); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Kevin DuBois | 413287f | 2019-02-25 08:46:47 -0800 | [diff] [blame] | 318 | void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) { |
| 319 | fn(*mPrimaryDispSync); |
| 320 | } |
| 321 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 322 | void Scheduler::updateFpsBasedOnContent() { |
| 323 | uint32_t refreshRate = std::round(mLayerHistory.getDesiredRefreshRate()); |
| 324 | ATRACE_INT("ContentFPS", refreshRate); |
| 325 | if (refreshRate > 0) { |
| 326 | contentChangeRefreshRate(ContentFeatureState::CONTENT_DETECTION_ON, refreshRate); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 327 | } else { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 328 | contentChangeRefreshRate(ContentFeatureState::CONTENT_DETECTION_OFF, 0); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 329 | } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Ana Krulec | 8d3e4f3 | 2019-03-05 10:40:33 -0800 | [diff] [blame] | 332 | void Scheduler::setChangeRefreshRateCallback( |
| 333 | const ChangeRefreshRateCallback& changeRefreshRateCallback) { |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 334 | std::lock_guard<std::mutex> lock(mCallbackLock); |
Ana Krulec | 8d3e4f3 | 2019-03-05 10:40:33 -0800 | [diff] [blame] | 335 | mChangeRefreshRateCallback = changeRefreshRateCallback; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 338 | void Scheduler::updateFrameSkipping(const int64_t skipCount) { |
| 339 | ATRACE_INT("FrameSkipCount", skipCount); |
| 340 | if (mSkipCount != skipCount) { |
| 341 | // Only update DispSync if it hasn't been updated yet. |
| 342 | mPrimaryDispSync->setRefreshSkipCount(skipCount); |
| 343 | mSkipCount = skipCount; |
| 344 | } |
| 345 | } |
| 346 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 347 | void Scheduler::resetIdleTimer() { |
| 348 | if (mIdleTimer) { |
| 349 | mIdleTimer->reset(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | void Scheduler::resetTimerCallback() { |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 354 | // We do not notify the applications about config changes when idle timer is reset. |
| 355 | timerChangeRefreshRate(IdleTimerState::RESET); |
| 356 | ATRACE_INT("ExpiredIdleTimer", 0); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void Scheduler::expiredTimerCallback() { |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 360 | // We do not notify the applications about config changes when idle timer expires. |
| 361 | timerChangeRefreshRate(IdleTimerState::EXPIRED); |
| 362 | ATRACE_INT("ExpiredIdleTimer", 1); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 365 | std::string Scheduler::doDump() { |
| 366 | std::ostringstream stream; |
| 367 | stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl; |
| 368 | return stream.str(); |
| 369 | } |
| 370 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 371 | void Scheduler::contentChangeRefreshRate(ContentFeatureState contentFeatureState, |
| 372 | uint32_t refreshRate) { |
| 373 | RefreshRateType newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 374 | { |
| 375 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 376 | mCurrentContentFeatureState = contentFeatureState; |
| 377 | mContentRefreshRate = refreshRate; |
| 378 | newRefreshRateType = calculateRefreshRateType(); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 379 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 380 | changeRefreshRate(newRefreshRateType, ConfigEvent::Changed); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 384 | RefreshRateType newRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 385 | { |
| 386 | std::lock_guard<std::mutex> lock(mFeatureStateLock); |
| 387 | mCurrentIdleTimerState = idleTimerState; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 388 | newRefreshRateType = calculateRefreshRateType(); |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 389 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame^] | 390 | changeRefreshRate(newRefreshRateType, ConfigEvent::None); |
| 391 | } |
| 392 | |
| 393 | Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() { |
| 394 | // First check if timer has expired as it means there is no new content on the screen |
| 395 | if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) { |
| 396 | return RefreshRateType::DEFAULT; |
| 397 | } |
| 398 | |
| 399 | // If content detection is off we choose performance as we don't know the content fps |
| 400 | if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) { |
| 401 | return RefreshRateType::PERFORMANCE; |
| 402 | } |
| 403 | |
| 404 | // Content detection is on, find the appropriate refresh rate |
| 405 | // Start with the smallest refresh rate which is greater than the content |
| 406 | auto iter = mRefreshRateConfigs.getRefreshRates().cbegin(); |
| 407 | RefreshRateType currRefreshRateType = iter->first; |
| 408 | while (iter != mRefreshRateConfigs.getRefreshRates().cend()) { |
| 409 | if (iter->second->fps >= mContentRefreshRate) { |
| 410 | currRefreshRateType = iter->first; |
| 411 | break; |
| 412 | } |
| 413 | ++iter; |
| 414 | } |
| 415 | |
| 416 | if (iter == mRefreshRateConfigs.getRefreshRates().cend()) { |
| 417 | return RefreshRateType::PERFORMANCE; |
| 418 | } |
| 419 | |
| 420 | // TODO(b/129874336): This logic is sub-optimal for content refresh rate that aligns better |
| 421 | // with a higher refresh rate. For example for 45fps we should choose 90Hz config. |
| 422 | |
| 423 | return currRefreshRateType; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) { |
| 427 | std::lock_guard<std::mutex> lock(mCallbackLock); |
| 428 | if (mChangeRefreshRateCallback) { |
| 429 | mChangeRefreshRateCallback(refreshRateType, configEvent); |
| 430 | } |
| 431 | } |
| 432 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 433 | } // namespace android |