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