| 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> | 
|  | 25 | #include <log/log.h> | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 26 | #include <utils/Trace.h> | 
|  | 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 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 43 | VsyncModulator::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 | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 49 | VsyncModulator::VsyncConfigOpt VsyncModulator::setTransactionSchedule( | 
|  | 50 | TransactionSchedule schedule) { | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 51 | switch (schedule) { | 
|  | 52 | case Schedule::EarlyStart: | 
| Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 53 | ALOGW_IF(mEarlyWakeup, "%s: Duplicate EarlyStart", __FUNCTION__); | 
|  | 54 | mEarlyWakeup = true; | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 55 | break; | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 56 | case Schedule::EarlyEnd: | 
| Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 57 | ALOGW_IF(!mEarlyWakeup, "%s: Unexpected EarlyEnd", __FUNCTION__); | 
|  | 58 | mEarlyWakeup = false; | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 59 | break; | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 60 | case Schedule::Late: | 
| Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 61 | // No change to mEarlyWakeup for non-explicit states. | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 62 | break; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | if (mTraceDetailedInfo) { | 
| Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 66 | ATRACE_INT("mEarlyWakeup", mEarlyWakeup); | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 69 | if (!mEarlyWakeup && schedule == Schedule::EarlyEnd) { | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 70 | mEarlyTransactionFrames = MIN_EARLY_TRANSACTION_FRAMES; | 
|  | 71 | mEarlyTransactionStartTime = mNow(); | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | // An early transaction stays an early transaction. | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 75 | if (schedule == mTransactionSchedule || mTransactionSchedule == Schedule::EarlyEnd) { | 
|  | 76 | return std::nullopt; | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 77 | } | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 78 | mTransactionSchedule = schedule; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 79 | return updateVsyncConfig(); | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 82 | VsyncModulator::VsyncConfigOpt VsyncModulator::onTransactionCommit() { | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 83 | mLastTransactionCommitTime = mNow(); | 
|  | 84 | if (mTransactionSchedule == Schedule::Late) return std::nullopt; | 
|  | 85 | mTransactionSchedule = Schedule::Late; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 86 | return updateVsyncConfig(); | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 89 | VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeInitiated() { | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 90 | if (mRefreshRateChangePending) return std::nullopt; | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 91 | mRefreshRateChangePending = true; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 92 | return updateVsyncConfig(); | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 95 | VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeCompleted() { | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 96 | if (!mRefreshRateChangePending) return std::nullopt; | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 97 | mRefreshRateChangePending = false; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 98 | return updateVsyncConfig(); | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 101 | VsyncModulator::VsyncConfigOpt VsyncModulator::onDisplayRefresh(bool usedGpuComposition) { | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 102 | bool updateOffsetsNeeded = false; | 
| Alec Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 103 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 104 | if (mEarlyTransactionStartTime.load() + MIN_EARLY_TRANSACTION_TIME <= | 
|  | 105 | mLastTransactionCommitTime.load()) { | 
|  | 106 | if (mEarlyTransactionFrames > 0) { | 
|  | 107 | mEarlyTransactionFrames--; | 
| Alec Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 108 | updateOffsetsNeeded = true; | 
|  | 109 | } | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 110 | } | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 111 | if (usedGpuComposition) { | 
|  | 112 | mEarlyGpuFrames = MIN_EARLY_GPU_FRAMES; | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 113 | updateOffsetsNeeded = true; | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 114 | } else if (mEarlyGpuFrames > 0) { | 
|  | 115 | mEarlyGpuFrames--; | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 116 | updateOffsetsNeeded = true; | 
|  | 117 | } | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | if (!updateOffsetsNeeded) return std::nullopt; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 120 | return updateVsyncConfig(); | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 123 | VsyncModulator::VsyncConfig VsyncModulator::getVsyncConfig() const { | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 124 | std::lock_guard<std::mutex> lock(mMutex); | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 125 | return mVsyncConfig; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 128 | const VsyncModulator::VsyncConfig& VsyncModulator::getNextVsyncConfig() const { | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 129 | // Early offsets are used if we're in the middle of a refresh rate | 
|  | 130 | // change, or if we recently begin a transaction. | 
| Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 131 | if (mEarlyWakeup || mTransactionSchedule == Schedule::EarlyEnd || mEarlyTransactionFrames > 0 || | 
|  | 132 | mRefreshRateChangePending) { | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 133 | return mVsyncConfigSet.early; | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 134 | } else if (mEarlyGpuFrames > 0) { | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 135 | return mVsyncConfigSet.earlyGpu; | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 136 | } else { | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 137 | return mVsyncConfigSet.late; | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 141 | VsyncModulator::VsyncConfig VsyncModulator::updateVsyncConfig() { | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 142 | std::lock_guard<std::mutex> lock(mMutex); | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 143 | return updateVsyncConfigLocked(); | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 146 | VsyncModulator::VsyncConfig VsyncModulator::updateVsyncConfigLocked() { | 
|  | 147 | const VsyncConfig& offsets = getNextVsyncConfig(); | 
|  | 148 | mVsyncConfig = offsets; | 
| Ady Abraham | 11b6a70 | 2019-06-27 11:24:19 -0700 | [diff] [blame] | 149 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 150 | if (mTraceDetailedInfo) { | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 151 | const bool isEarly = &offsets == &mVsyncConfigSet.early; | 
|  | 152 | const bool isEarlyGpu = &offsets == &mVsyncConfigSet.earlyGpu; | 
|  | 153 | const bool isLate = &offsets == &mVsyncConfigSet.late; | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | ATRACE_INT("Vsync-EarlyOffsetsOn", isEarly); | 
|  | 156 | ATRACE_INT("Vsync-EarlyGpuOffsetsOn", isEarlyGpu); | 
|  | 157 | ATRACE_INT("Vsync-LateOffsetsOn", isLate); | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 158 | } | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 159 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 160 | return offsets; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 163 | } // namespace android::scheduler |