Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "VsyncModulator" |
| 21 | |
Dominik Laskowski | a93a531 | 2020-07-23 15:10:03 -0700 | [diff] [blame] | 22 | #include "VsyncModulator.h" |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 23 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 24 | #include <common/trace.h> |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 25 | #include <log/log.h> |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 26 | |
Alec Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 27 | #include <chrono> |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 28 | #include <cinttypes> |
| 29 | #include <mutex> |
| 30 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 31 | using namespace std::chrono_literals; |
| 32 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 33 | namespace android::scheduler { |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 34 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 35 | const std::chrono::nanoseconds VsyncModulator::MIN_EARLY_TRANSACTION_TIME = 1ms; |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 36 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 37 | VsyncModulator::VsyncModulator(const VsyncConfigSet& config, Now now) |
| 38 | : mVsyncConfigSet(config), |
Surbhi Kadam | 56d89e9 | 2024-09-06 17:00:13 +0000 | [diff] [blame] | 39 | mNow(now) {} |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 40 | |
Dominik Laskowski | 933f8de | 2023-01-20 13:15:51 -0500 | [diff] [blame] | 41 | VsyncConfig VsyncModulator::setVsyncConfigSet(const VsyncConfigSet& config) { |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 42 | std::lock_guard<std::mutex> lock(mMutex); |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 43 | mVsyncConfigSet = config; |
| 44 | return updateVsyncConfigLocked(); |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 47 | VsyncModulator::VsyncConfigOpt VsyncModulator::setTransactionSchedule(TransactionSchedule schedule, |
| 48 | const sp<IBinder>& token) { |
| 49 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 50 | switch (schedule) { |
| 51 | case Schedule::EarlyStart: |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 52 | if (token) { |
| 53 | mEarlyWakeupRequests.emplace(token); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 54 | token->linkToDeath(sp<DeathRecipient>::fromExisting(this)); |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 55 | } else { |
| 56 | ALOGW("%s: EarlyStart requested without a valid token", __func__); |
| 57 | } |
Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 58 | break; |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 59 | case Schedule::EarlyEnd: { |
| 60 | if (token && mEarlyWakeupRequests.erase(token) > 0) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 61 | token->unlinkToDeath(sp<DeathRecipient>::fromExisting(this)); |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 62 | } else { |
| 63 | ALOGW("%s: Unexpected EarlyEnd", __func__); |
| 64 | } |
Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 65 | break; |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 66 | } |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 67 | case Schedule::Late: |
Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 68 | // No change to mEarlyWakeup for non-explicit states. |
Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 69 | break; |
| 70 | } |
| 71 | |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 72 | if (mEarlyWakeupRequests.empty() && schedule == Schedule::EarlyEnd) { |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 73 | mEarlyTransactionFrames = MIN_EARLY_TRANSACTION_FRAMES; |
| 74 | mEarlyTransactionStartTime = mNow(); |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // An early transaction stays an early transaction. |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 78 | if (schedule == mTransactionSchedule || mTransactionSchedule == Schedule::EarlyEnd) { |
| 79 | return std::nullopt; |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 80 | } |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 81 | mTransactionSchedule = schedule; |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 82 | return updateVsyncConfigLocked(); |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 85 | VsyncModulator::VsyncConfigOpt VsyncModulator::onTransactionCommit() { |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 86 | mLastTransactionCommitTime = mNow(); |
| 87 | if (mTransactionSchedule == Schedule::Late) return std::nullopt; |
| 88 | mTransactionSchedule = Schedule::Late; |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 89 | return updateVsyncConfig(); |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 92 | VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeInitiated() { |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 93 | if (mRefreshRateChangePending) return std::nullopt; |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 94 | mRefreshRateChangePending = true; |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 95 | return updateVsyncConfig(); |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 98 | VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeCompleted() { |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 99 | if (!mRefreshRateChangePending) return std::nullopt; |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 100 | mRefreshRateChangePending = false; |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 101 | return updateVsyncConfig(); |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 104 | VsyncModulator::VsyncConfigOpt VsyncModulator::onDisplayRefresh(bool usedGpuComposition) { |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 105 | bool updateOffsetsNeeded = false; |
Alec Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 106 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 107 | if (mEarlyTransactionStartTime.load() + MIN_EARLY_TRANSACTION_TIME <= |
| 108 | mLastTransactionCommitTime.load()) { |
| 109 | if (mEarlyTransactionFrames > 0) { |
| 110 | mEarlyTransactionFrames--; |
Alec Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 111 | updateOffsetsNeeded = true; |
| 112 | } |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 113 | } |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 114 | if (usedGpuComposition) { |
| 115 | mEarlyGpuFrames = MIN_EARLY_GPU_FRAMES; |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 116 | updateOffsetsNeeded = true; |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 117 | } else if (mEarlyGpuFrames > 0) { |
| 118 | mEarlyGpuFrames--; |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 119 | updateOffsetsNeeded = true; |
| 120 | } |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 121 | |
| 122 | if (!updateOffsetsNeeded) return std::nullopt; |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 123 | return updateVsyncConfig(); |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Dominik Laskowski | 933f8de | 2023-01-20 13:15:51 -0500 | [diff] [blame] | 126 | VsyncConfig VsyncModulator::getVsyncConfig() const { |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 127 | std::lock_guard<std::mutex> lock(mMutex); |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 128 | return mVsyncConfig; |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 131 | auto VsyncModulator::getNextVsyncConfigType() const -> VsyncConfigType { |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 132 | // Early offsets are used if we're in the middle of a refresh rate |
| 133 | // change, or if we recently begin a transaction. |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 134 | if (!mEarlyWakeupRequests.empty() || mTransactionSchedule == Schedule::EarlyEnd || |
| 135 | mEarlyTransactionFrames > 0 || mRefreshRateChangePending) { |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 136 | return VsyncConfigType::Early; |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 137 | } else if (mEarlyGpuFrames > 0) { |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 138 | return VsyncConfigType::EarlyGpu; |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 139 | } else { |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 140 | return VsyncConfigType::Late; |
| 141 | } |
| 142 | } |
| 143 | |
Dominik Laskowski | 933f8de | 2023-01-20 13:15:51 -0500 | [diff] [blame] | 144 | const VsyncConfig& VsyncModulator::getNextVsyncConfig() const { |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 145 | switch (getNextVsyncConfigType()) { |
| 146 | case VsyncConfigType::Early: |
| 147 | return mVsyncConfigSet.early; |
| 148 | case VsyncConfigType::EarlyGpu: |
| 149 | return mVsyncConfigSet.earlyGpu; |
| 150 | case VsyncConfigType::Late: |
| 151 | return mVsyncConfigSet.late; |
Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Dominik Laskowski | 933f8de | 2023-01-20 13:15:51 -0500 | [diff] [blame] | 155 | VsyncConfig VsyncModulator::updateVsyncConfig() { |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 156 | std::lock_guard<std::mutex> lock(mMutex); |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 157 | return updateVsyncConfigLocked(); |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Dominik Laskowski | 933f8de | 2023-01-20 13:15:51 -0500 | [diff] [blame] | 160 | VsyncConfig VsyncModulator::updateVsyncConfigLocked() { |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 161 | const VsyncConfig& offsets = getNextVsyncConfig(); |
| 162 | mVsyncConfig = offsets; |
Ady Abraham | 11b6a70 | 2019-06-27 11:24:19 -0700 | [diff] [blame] | 163 | |
Surbhi Kadam | 56d89e9 | 2024-09-06 17:00:13 +0000 | [diff] [blame] | 164 | // Trace config type |
| 165 | SFTRACE_INT("Vsync-Early", &mVsyncConfig == &mVsyncConfigSet.early); |
| 166 | SFTRACE_INT("Vsync-EarlyGpu", &mVsyncConfig == &mVsyncConfigSet.earlyGpu); |
| 167 | SFTRACE_INT("Vsync-Late", &mVsyncConfig == &mVsyncConfigSet.late); |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 168 | |
Surbhi Kadam | 56d89e9 | 2024-09-06 17:00:13 +0000 | [diff] [blame] | 169 | // Trace early vsync conditions |
| 170 | SFTRACE_INT("EarlyWakeupRequests", |
| 171 | static_cast<int>(mEarlyWakeupRequests.size())); |
| 172 | SFTRACE_INT("EarlyTransactionFrames", mEarlyTransactionFrames); |
| 173 | SFTRACE_INT("RefreshRateChangePending", mRefreshRateChangePending); |
| 174 | |
| 175 | // Trace early gpu conditions |
| 176 | SFTRACE_INT("EarlyGpuFrames", mEarlyGpuFrames); |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 177 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 178 | return offsets; |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 181 | void VsyncModulator::binderDied(const wp<IBinder>& who) { |
| 182 | std::lock_guard<std::mutex> lock(mMutex); |
| 183 | mEarlyWakeupRequests.erase(who); |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 184 | static_cast<void>(updateVsyncConfigLocked()); |
| 185 | } |
| 186 | |
Dominik Laskowski | 14956dc | 2023-02-22 13:43:57 -0500 | [diff] [blame] | 187 | bool VsyncModulator::isVsyncConfigEarly() const { |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 188 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 14956dc | 2023-02-22 13:43:57 -0500 | [diff] [blame] | 189 | return getNextVsyncConfigType() != VsyncConfigType::Late; |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 192 | } // namespace android::scheduler |