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