blob: 1f821be50485d4ca7c6cb6322c5cfd966c1b65b7 [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 Abraham8287e852020-08-12 14:44:58 -070049VsyncModulator::VsyncConfigOpt VsyncModulator::setTransactionSchedule(
50 TransactionSchedule schedule) {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070051 switch (schedule) {
52 case Schedule::EarlyStart:
53 ALOGW_IF(mExplicitEarlyWakeup, "%s: Duplicate EarlyStart", __FUNCTION__);
Ady Abrahambf1349c2020-06-12 14:26:18 -070054 mExplicitEarlyWakeup = true;
55 break;
Dominik Laskowski08d05c22020-07-22 00:05:08 -070056 case Schedule::EarlyEnd:
57 ALOGW_IF(!mExplicitEarlyWakeup, "%s: Unexpected EarlyEnd", __FUNCTION__);
Ady Abrahambf1349c2020-06-12 14:26:18 -070058 mExplicitEarlyWakeup = false;
59 break;
Dominik Laskowski08d05c22020-07-22 00:05:08 -070060 case Schedule::Early:
61 case Schedule::Late:
62 // No change to mExplicitEarlyWakeup for non-explicit states.
Ady Abrahambf1349c2020-06-12 14:26:18 -070063 break;
64 }
65
66 if (mTraceDetailedInfo) {
67 ATRACE_INT("mExplicitEarlyWakeup", mExplicitEarlyWakeup);
68 }
69
Dominik Laskowski08d05c22020-07-22 00:05:08 -070070 if (!mExplicitEarlyWakeup && (schedule == Schedule::Early || schedule == Schedule::EarlyEnd)) {
71 mEarlyTransactionFrames = MIN_EARLY_TRANSACTION_FRAMES;
72 mEarlyTransactionStartTime = mNow();
Alec Mourib488afa2019-05-23 10:22:24 -070073 }
74
75 // An early transaction stays an early transaction.
Dominik Laskowski08d05c22020-07-22 00:05:08 -070076 if (schedule == mTransactionSchedule || mTransactionSchedule == Schedule::EarlyEnd) {
77 return std::nullopt;
Alec Mourib488afa2019-05-23 10:22:24 -070078 }
Dominik Laskowski08d05c22020-07-22 00:05:08 -070079 mTransactionSchedule = schedule;
Ady Abraham8287e852020-08-12 14:44:58 -070080 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -070081}
82
Ady Abraham8287e852020-08-12 14:44:58 -070083VsyncModulator::VsyncConfigOpt VsyncModulator::onTransactionCommit() {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070084 mLastTransactionCommitTime = mNow();
85 if (mTransactionSchedule == Schedule::Late) return std::nullopt;
86 mTransactionSchedule = Schedule::Late;
Ady Abraham8287e852020-08-12 14:44:58 -070087 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -070088}
89
Ady Abraham8287e852020-08-12 14:44:58 -070090VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeInitiated() {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070091 if (mRefreshRateChangePending) return std::nullopt;
Alec Mourib488afa2019-05-23 10:22:24 -070092 mRefreshRateChangePending = true;
Ady Abraham8287e852020-08-12 14:44:58 -070093 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -070094}
95
Ady Abraham8287e852020-08-12 14:44:58 -070096VsyncModulator::VsyncConfigOpt VsyncModulator::onRefreshRateChangeCompleted() {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070097 if (!mRefreshRateChangePending) return std::nullopt;
Alec Mourib488afa2019-05-23 10:22:24 -070098 mRefreshRateChangePending = false;
Ady Abraham8287e852020-08-12 14:44:58 -070099 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -0700100}
101
Ady Abraham8287e852020-08-12 14:44:58 -0700102VsyncModulator::VsyncConfigOpt VsyncModulator::onDisplayRefresh(bool usedGpuComposition) {
Alec Mourib488afa2019-05-23 10:22:24 -0700103 bool updateOffsetsNeeded = false;
Alec Mourif792cfe2020-06-05 13:11:23 -0700104
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700105 if (mEarlyTransactionStartTime.load() + MIN_EARLY_TRANSACTION_TIME <=
106 mLastTransactionCommitTime.load()) {
107 if (mEarlyTransactionFrames > 0) {
108 mEarlyTransactionFrames--;
Alec Mourif792cfe2020-06-05 13:11:23 -0700109 updateOffsetsNeeded = true;
110 }
Alec Mourib488afa2019-05-23 10:22:24 -0700111 }
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700112 if (usedGpuComposition) {
113 mEarlyGpuFrames = MIN_EARLY_GPU_FRAMES;
Alec Mourib488afa2019-05-23 10:22:24 -0700114 updateOffsetsNeeded = true;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700115 } else if (mEarlyGpuFrames > 0) {
116 mEarlyGpuFrames--;
Alec Mourib488afa2019-05-23 10:22:24 -0700117 updateOffsetsNeeded = true;
118 }
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700119
120 if (!updateOffsetsNeeded) return std::nullopt;
Ady Abraham8287e852020-08-12 14:44:58 -0700121 return updateVsyncConfig();
Alec Mourib488afa2019-05-23 10:22:24 -0700122}
123
Ady Abraham8287e852020-08-12 14:44:58 -0700124VsyncModulator::VsyncConfig VsyncModulator::getVsyncConfig() const {
Alec Mourid7599d82019-05-22 19:58:00 -0700125 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -0700126 return mVsyncConfig;
Alec Mourid7599d82019-05-22 19:58:00 -0700127}
128
Ady Abraham8287e852020-08-12 14:44:58 -0700129const VsyncModulator::VsyncConfig& VsyncModulator::getNextVsyncConfig() const {
Alec Mourib488afa2019-05-23 10:22:24 -0700130 // Early offsets are used if we're in the middle of a refresh rate
131 // change, or if we recently begin a transaction.
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700132 if (mExplicitEarlyWakeup || mTransactionSchedule == Schedule::EarlyEnd ||
133 mEarlyTransactionFrames > 0 || mRefreshRateChangePending) {
Ady Abraham8287e852020-08-12 14:44:58 -0700134 return mVsyncConfigSet.early;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700135 } else if (mEarlyGpuFrames > 0) {
Ady Abraham8287e852020-08-12 14:44:58 -0700136 return mVsyncConfigSet.earlyGpu;
Alec Mourib488afa2019-05-23 10:22:24 -0700137 } else {
Ady Abraham8287e852020-08-12 14:44:58 -0700138 return mVsyncConfigSet.late;
Alec Mourib488afa2019-05-23 10:22:24 -0700139 }
140}
141
Ady Abraham8287e852020-08-12 14:44:58 -0700142VsyncModulator::VsyncConfig VsyncModulator::updateVsyncConfig() {
Alec Mourid7599d82019-05-22 19:58:00 -0700143 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -0700144 return updateVsyncConfigLocked();
Alec Mourid7599d82019-05-22 19:58:00 -0700145}
146
Ady Abraham8287e852020-08-12 14:44:58 -0700147VsyncModulator::VsyncConfig VsyncModulator::updateVsyncConfigLocked() {
148 const VsyncConfig& offsets = getNextVsyncConfig();
149 mVsyncConfig = offsets;
Ady Abraham11b6a702019-06-27 11:24:19 -0700150
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700151 if (mTraceDetailedInfo) {
Ady Abraham8287e852020-08-12 14:44:58 -0700152 const bool isEarly = &offsets == &mVsyncConfigSet.early;
153 const bool isEarlyGpu = &offsets == &mVsyncConfigSet.earlyGpu;
154 const bool isLate = &offsets == &mVsyncConfigSet.late;
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700155
156 ATRACE_INT("Vsync-EarlyOffsetsOn", isEarly);
157 ATRACE_INT("Vsync-EarlyGpuOffsetsOn", isEarlyGpu);
158 ATRACE_INT("Vsync-LateOffsetsOn", isLate);
Alec Mourid7599d82019-05-22 19:58:00 -0700159 }
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700160
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700161 return offsets;
Alec Mourid7599d82019-05-22 19:58:00 -0700162}
163
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700164} // namespace android::scheduler