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