blob: 245db0f5c7bad3e3416a62a3164826822a2354a8 [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
Ady Abraham8287e852020-08-12 14:44:58 -070043VsyncModulator::VsyncConfig 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);
56 token->linkToDeath(this);
57 } 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) {
63 token->unlinkToDeath(this);
64 } 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
Ady Abraham8287e852020-08-12 14:44:58 -0700132VsyncModulator::VsyncConfig 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 Abraham8287e852020-08-12 14:44:58 -0700137const VsyncModulator::VsyncConfig& VsyncModulator::getNextVsyncConfig() const {
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 Abraham8287e852020-08-12 14:44:58 -0700142 return mVsyncConfigSet.early;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700143 } else if (mEarlyGpuFrames > 0) {
Ady Abraham8287e852020-08-12 14:44:58 -0700144 return mVsyncConfigSet.earlyGpu;
Alec Mourib488afa2019-05-23 10:22:24 -0700145 } else {
Ady Abraham8287e852020-08-12 14:44:58 -0700146 return mVsyncConfigSet.late;
Alec Mourib488afa2019-05-23 10:22:24 -0700147 }
148}
149
Ady Abraham8287e852020-08-12 14:44:58 -0700150VsyncModulator::VsyncConfig VsyncModulator::updateVsyncConfig() {
Alec Mourid7599d82019-05-22 19:58:00 -0700151 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -0700152 return updateVsyncConfigLocked();
Alec Mourid7599d82019-05-22 19:58:00 -0700153}
154
Ady Abraham8287e852020-08-12 14:44:58 -0700155VsyncModulator::VsyncConfig VsyncModulator::updateVsyncConfigLocked() {
156 const VsyncConfig& offsets = getNextVsyncConfig();
157 mVsyncConfig = offsets;
Ady Abraham11b6a702019-06-27 11:24:19 -0700158
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700159 if (mTraceDetailedInfo) {
Ady Abraham8287e852020-08-12 14:44:58 -0700160 const bool isEarly = &offsets == &mVsyncConfigSet.early;
161 const bool isEarlyGpu = &offsets == &mVsyncConfigSet.earlyGpu;
162 const bool isLate = &offsets == &mVsyncConfigSet.late;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700163
164 ATRACE_INT("Vsync-EarlyOffsetsOn", isEarly);
165 ATRACE_INT("Vsync-EarlyGpuOffsetsOn", isEarlyGpu);
166 ATRACE_INT("Vsync-LateOffsetsOn", isLate);
Alec Mourid7599d82019-05-22 19:58:00 -0700167 }
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700168
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700169 return offsets;
Alec Mourid7599d82019-05-22 19:58:00 -0700170}
171
Ady Abraham23ea9da2021-07-14 16:32:56 -0700172void VsyncModulator::binderDied(const wp<IBinder>& who) {
173 std::lock_guard<std::mutex> lock(mMutex);
174 mEarlyWakeupRequests.erase(who);
175
176 static_cast<void>(updateVsyncConfigLocked());
177}
178
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700179} // namespace android::scheduler