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