blob: 586357f50adfef8ac6dcaa81a8b7e1ef0b7a9fe4 [file] [log] [blame]
Alec Mourib488afa2019-05-23 10:22:24 -07001/*
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 Mourid7599d82019-05-22 19:58:00 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Dominik Laskowski08d05c22020-07-22 00:05:08 -070019#undef LOG_TAG
20#define LOG_TAG "VsyncModulator"
21
Dominik Laskowskia93a5312020-07-23 15:10:03 -070022#include "VsyncModulator.h"
Alec Mourib488afa2019-05-23 10:22:24 -070023
Dominik Laskowski08d05c22020-07-22 00:05:08 -070024#include <android-base/properties.h>
25#include <log/log.h>
Alec Mourid7599d82019-05-22 19:58:00 -070026#include <utils/Trace.h>
27
Alec Mourif792cfe2020-06-05 13:11:23 -070028#include <chrono>
Alec Mourib488afa2019-05-23 10:22:24 -070029#include <cinttypes>
30#include <mutex>
31
Dominik Laskowski08d05c22020-07-22 00:05:08 -070032using namespace std::chrono_literals;
33
Dominik Laskowskieddeda12019-07-19 11:54:13 -070034namespace android::scheduler {
Alec Mourib488afa2019-05-23 10:22:24 -070035
Dominik Laskowski08d05c22020-07-22 00:05:08 -070036const std::chrono::nanoseconds VsyncModulator::MIN_EARLY_TRANSACTION_TIME = 1ms;
Alec Mourid7599d82019-05-22 19:58:00 -070037
Ady Abraham8287e852020-08-12 14:44:58 -070038VsyncModulator::VsyncModulator(const VsyncConfigSet& config, Now now)
39 : mVsyncConfigSet(config),
Dominik Laskowski08d05c22020-07-22 00:05:08 -070040 mNow(now),
41 mTraceDetailedInfo(base::GetBoolProperty("debug.sf.vsync_trace_detailed_info", false)) {}
42
Dominik Laskowski933f8de2023-01-20 13:15:51 -050043VsyncConfig VsyncModulator::setVsyncConfigSet(const VsyncConfigSet& config) {
Alec Mourid7599d82019-05-22 19:58:00 -070044 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -070045 mVsyncConfigSet = config;
46 return updateVsyncConfigLocked();
Alec Mourib488afa2019-05-23 10:22:24 -070047}
48
Ady Abraham23ea9da2021-07-14 16:32:56 -070049VsyncModulator::VsyncConfigOpt VsyncModulator::setTransactionSchedule(TransactionSchedule schedule,
50 const sp<IBinder>& token) {
51 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -070052 switch (schedule) {
53 case Schedule::EarlyStart:
Ady Abraham23ea9da2021-07-14 16:32:56 -070054 if (token) {
55 mEarlyWakeupRequests.emplace(token);
Ady Abrahamd11bade2022-08-01 16:18:03 -070056 token->linkToDeath(sp<DeathRecipient>::fromExisting(this));
Ady Abraham23ea9da2021-07-14 16:32:56 -070057 } else {
58 ALOGW("%s: EarlyStart requested without a valid token", __func__);
59 }
Ady Abrahambf1349c2020-06-12 14:26:18 -070060 break;
Ady Abraham23ea9da2021-07-14 16:32:56 -070061 case Schedule::EarlyEnd: {
62 if (token && mEarlyWakeupRequests.erase(token) > 0) {
Ady Abrahamd11bade2022-08-01 16:18:03 -070063 token->unlinkToDeath(sp<DeathRecipient>::fromExisting(this));
Ady Abraham23ea9da2021-07-14 16:32:56 -070064 } else {
65 ALOGW("%s: Unexpected EarlyEnd", __func__);
66 }
Ady Abrahambf1349c2020-06-12 14:26:18 -070067 break;
Ady Abraham23ea9da2021-07-14 16:32:56 -070068 }
Dominik Laskowski08d05c22020-07-22 00:05:08 -070069 case Schedule::Late:
Ady Abraham8cbd3072021-03-15 16:39:06 -070070 // No change to mEarlyWakeup for non-explicit states.
Ady Abrahambf1349c2020-06-12 14:26:18 -070071 break;
72 }
73
74 if (mTraceDetailedInfo) {
Ady Abraham23ea9da2021-07-14 16:32:56 -070075 ATRACE_INT("mEarlyWakeup", static_cast<int>(mEarlyWakeupRequests.size()));
Ady Abrahambf1349c2020-06-12 14:26:18 -070076 }
77
Ady Abraham23ea9da2021-07-14 16:32:56 -070078 if (mEarlyWakeupRequests.empty() && schedule == Schedule::EarlyEnd) {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070079 mEarlyTransactionFrames = MIN_EARLY_TRANSACTION_FRAMES;
80 mEarlyTransactionStartTime = mNow();
Alec Mourib488afa2019-05-23 10:22:24 -070081 }
82
83 // An early transaction stays an early transaction.
Dominik Laskowski08d05c22020-07-22 00:05:08 -070084 if (schedule == mTransactionSchedule || mTransactionSchedule == Schedule::EarlyEnd) {
85 return std::nullopt;
Alec Mourib488afa2019-05-23 10:22:24 -070086 }
Dominik Laskowski08d05c22020-07-22 00:05:08 -070087 mTransactionSchedule = schedule;
Ady Abraham23ea9da2021-07-14 16:32:56 -070088 return updateVsyncConfigLocked();
Alec Mourib488afa2019-05-23 10:22:24 -070089}
90
Ady Abraham8287e852020-08-12 14:44:58 -070091VsyncModulator::VsyncConfigOpt VsyncModulator::onTransactionCommit() {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070092 mLastTransactionCommitTime = mNow();
93 if (mTransactionSchedule == Schedule::Late) return std::nullopt;
94 mTransactionSchedule = Schedule::Late;
Ady Abraham8287e852020-08-12 14:44:58 -070095 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -070096}
97
Ady Abraham8287e852020-08-12 14:44:58 -070098VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeInitiated() {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070099 if (mRefreshRateChangePending) return std::nullopt;
Alec Mourib488afa2019-05-23 10:22:24 -0700100 mRefreshRateChangePending = true;
Ady Abraham8287e852020-08-12 14:44:58 -0700101 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -0700102}
103
Ady Abraham8287e852020-08-12 14:44:58 -0700104VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeCompleted() {
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700105 if (!mRefreshRateChangePending) return std::nullopt;
Alec Mourib488afa2019-05-23 10:22:24 -0700106 mRefreshRateChangePending = false;
Ady Abraham8287e852020-08-12 14:44:58 -0700107 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -0700108}
109
Ady Abraham8287e852020-08-12 14:44:58 -0700110VsyncModulator::VsyncConfigOpt VsyncModulator::onDisplayRefresh(bool usedGpuComposition) {
Alec Mourib488afa2019-05-23 10:22:24 -0700111 bool updateOffsetsNeeded = false;
Alec Mourif792cfe2020-06-05 13:11:23 -0700112
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700113 if (mEarlyTransactionStartTime.load() + MIN_EARLY_TRANSACTION_TIME <=
114 mLastTransactionCommitTime.load()) {
115 if (mEarlyTransactionFrames > 0) {
116 mEarlyTransactionFrames--;
Alec Mourif792cfe2020-06-05 13:11:23 -0700117 updateOffsetsNeeded = true;
118 }
Alec Mourib488afa2019-05-23 10:22:24 -0700119 }
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700120 if (usedGpuComposition) {
121 mEarlyGpuFrames = MIN_EARLY_GPU_FRAMES;
Alec Mourib488afa2019-05-23 10:22:24 -0700122 updateOffsetsNeeded = true;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700123 } else if (mEarlyGpuFrames > 0) {
124 mEarlyGpuFrames--;
Alec Mourib488afa2019-05-23 10:22:24 -0700125 updateOffsetsNeeded = true;
126 }
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700127
128 if (!updateOffsetsNeeded) return std::nullopt;
Ady Abraham8287e852020-08-12 14:44:58 -0700129 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -0700130}
131
Dominik Laskowski933f8de2023-01-20 13:15:51 -0500132VsyncConfig VsyncModulator::getVsyncConfig() const {
Alec Mourid7599d82019-05-22 19:58:00 -0700133 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -0700134 return mVsyncConfig;
Alec Mourid7599d82019-05-22 19:58:00 -0700135}
136
Ady Abraham2739e832022-02-14 17:42:00 -0800137auto VsyncModulator::getNextVsyncConfigType() const -> VsyncConfigType {
Alec Mourib488afa2019-05-23 10:22:24 -0700138 // Early offsets are used if we're in the middle of a refresh rate
139 // change, or if we recently begin a transaction.
Ady Abraham23ea9da2021-07-14 16:32:56 -0700140 if (!mEarlyWakeupRequests.empty() || mTransactionSchedule == Schedule::EarlyEnd ||
141 mEarlyTransactionFrames > 0 || mRefreshRateChangePending) {
Ady Abraham2739e832022-02-14 17:42:00 -0800142 return VsyncConfigType::Early;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700143 } else if (mEarlyGpuFrames > 0) {
Ady Abraham2739e832022-02-14 17:42:00 -0800144 return VsyncConfigType::EarlyGpu;
Alec Mourib488afa2019-05-23 10:22:24 -0700145 } else {
Ady Abraham2739e832022-02-14 17:42:00 -0800146 return VsyncConfigType::Late;
147 }
148}
149
Dominik Laskowski933f8de2023-01-20 13:15:51 -0500150const VsyncConfig& VsyncModulator::getNextVsyncConfig() const {
Ady Abraham2739e832022-02-14 17:42:00 -0800151 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 Mourib488afa2019-05-23 10:22:24 -0700158 }
159}
160
Dominik Laskowski933f8de2023-01-20 13:15:51 -0500161VsyncConfig VsyncModulator::updateVsyncConfig() {
Alec Mourid7599d82019-05-22 19:58:00 -0700162 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -0700163 return updateVsyncConfigLocked();
Alec Mourid7599d82019-05-22 19:58:00 -0700164}
165
Dominik Laskowski933f8de2023-01-20 13:15:51 -0500166VsyncConfig VsyncModulator::updateVsyncConfigLocked() {
Ady Abraham8287e852020-08-12 14:44:58 -0700167 const VsyncConfig& offsets = getNextVsyncConfig();
168 mVsyncConfig = offsets;
Ady Abraham11b6a702019-06-27 11:24:19 -0700169
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700170 if (mTraceDetailedInfo) {
Ady Abraham8287e852020-08-12 14:44:58 -0700171 const bool isEarly = &offsets == &mVsyncConfigSet.early;
172 const bool isEarlyGpu = &offsets == &mVsyncConfigSet.earlyGpu;
173 const bool isLate = &offsets == &mVsyncConfigSet.late;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700174
175 ATRACE_INT("Vsync-EarlyOffsetsOn", isEarly);
176 ATRACE_INT("Vsync-EarlyGpuOffsetsOn", isEarlyGpu);
177 ATRACE_INT("Vsync-LateOffsetsOn", isLate);
Alec Mourid7599d82019-05-22 19:58:00 -0700178 }
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700179
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700180 return offsets;
Alec Mourid7599d82019-05-22 19:58:00 -0700181}
182
Ady Abraham23ea9da2021-07-14 16:32:56 -0700183void 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 Laskowski14956dc2023-02-22 13:43:57 -0500190bool VsyncModulator::isVsyncConfigEarly() const {
Ady Abraham2739e832022-02-14 17:42:00 -0800191 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski14956dc2023-02-22 13:43:57 -0500192 return getNextVsyncConfigType() != VsyncConfigType::Late;
Ady Abraham2739e832022-02-14 17:42:00 -0800193}
194
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700195} // namespace android::scheduler