blob: f920bd20495e8c47a484476455323235d1976347 [file] [log] [blame]
Dan Stoza2713c302018-03-28 17:07:36 -07001/*
2 * Copyright 2018 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
17#pragma once
18
Alec Mourif792cfe2020-06-05 13:11:23 -070019#include <chrono>
Dan Stoza2713c302018-03-28 17:07:36 -070020#include <mutex>
Dominik Laskowski08d05c22020-07-22 00:05:08 -070021#include <optional>
Dan Stoza2713c302018-03-28 17:07:36 -070022
Dominik Laskowski08d05c22020-07-22 00:05:08 -070023#include <android-base/thread_annotations.h>
24#include <utils/Timers.h>
Dan Stoza2713c302018-03-28 17:07:36 -070025
Dominik Laskowskieddeda12019-07-19 11:54:13 -070026namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070027
Dominik Laskowski08d05c22020-07-22 00:05:08 -070028// State machine controlled by transaction flags. VsyncModulator switches to early phase offsets
29// when a transaction is flagged EarlyStart or Early, lasting until an EarlyEnd transaction or a
30// fixed number of frames, respectively.
31enum class TransactionSchedule {
32 Late, // Default.
33 Early, // Deprecated.
34 EarlyStart,
35 EarlyEnd
36};
Jorim Jaggi90535212018-05-23 23:44:06 +020037
Dominik Laskowski08d05c22020-07-22 00:05:08 -070038// Modulates VSYNC phase depending on transaction schedule and refresh rate changes.
39class VsyncModulator {
Dan Stoza2713c302018-03-28 17:07:36 -070040public:
Dominik Laskowski08d05c22020-07-22 00:05:08 -070041 // Number of frames to keep early offsets after an early transaction or GPU composition.
42 // This acts as a low-pass filter in case subsequent transactions are delayed, or if the
43 // composition strategy alternates on subsequent frames.
44 static constexpr int MIN_EARLY_TRANSACTION_FRAMES = 2;
45 static constexpr int MIN_EARLY_GPU_FRAMES = 2;
46
47 // Duration to delay the MIN_EARLY_TRANSACTION_FRAMES countdown after an early transaction.
48 // This may keep early offsets for an extra frame, but avoids a race with transaction commit.
49 static const std::chrono::nanoseconds MIN_EARLY_TRANSACTION_TIME;
50
51 // Phase offsets for SF and app deadlines from VSYNC.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020052 struct Offsets {
53 nsecs_t sf;
54 nsecs_t app;
Ady Abraham9e16a482019-12-03 17:19:41 -080055
56 bool operator==(const Offsets& other) const { return sf == other.sf && app == other.app; }
Ady Abraham9e16a482019-12-03 17:19:41 -080057 bool operator!=(const Offsets& other) const { return !(*this == other); }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020058 };
59
Dominik Laskowski08d05c22020-07-22 00:05:08 -070060 using OffsetsOpt = std::optional<Offsets>;
61
Dominik Laskowskieddeda12019-07-19 11:54:13 -070062 struct OffsetsConfig {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070063 Offsets early; // Used for early transactions, and during refresh rate change.
64 Offsets earlyGpu; // Used during GPU composition.
65 Offsets late; // Default.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070066
Ady Abraham9e16a482019-12-03 17:19:41 -080067 bool operator==(const OffsetsConfig& other) const {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070068 return early == other.early && earlyGpu == other.earlyGpu && late == other.late;
Ady Abraham9e16a482019-12-03 17:19:41 -080069 }
70
71 bool operator!=(const OffsetsConfig& other) const { return !(*this == other); }
Alec Mourid7599d82019-05-22 19:58:00 -070072 };
73
Dominik Laskowski08d05c22020-07-22 00:05:08 -070074 using Clock = std::chrono::steady_clock;
75 using TimePoint = Clock::time_point;
76 using Now = TimePoint (*)();
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020077
Dominik Laskowski08d05c22020-07-22 00:05:08 -070078 explicit VsyncModulator(const OffsetsConfig&, Now = Clock::now);
Ana Krulec12096d02018-09-07 14:54:14 -070079
Dominik Laskowski08d05c22020-07-22 00:05:08 -070080 Offsets getOffsets() const EXCLUDES(mMutex);
Jorim Jaggi90535212018-05-23 23:44:06 +020081
Dominik Laskowski08d05c22020-07-22 00:05:08 -070082 [[nodiscard]] Offsets setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex);
83
84 // Changes offsets in response to transaction flags or commit.
85 [[nodiscard]] OffsetsOpt setTransactionSchedule(TransactionSchedule);
86 [[nodiscard]] OffsetsOpt onTransactionCommit();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010087
Alec Mouri754c98a2019-03-18 18:53:42 -070088 // Called when we send a refresh rate change to hardware composer, so that
89 // we can move into early offsets.
Dominik Laskowski08d05c22020-07-22 00:05:08 -070090 [[nodiscard]] OffsetsOpt onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070091
Dominik Laskowski08d05c22020-07-22 00:05:08 -070092 // Called when we detect from VSYNC signals that the refresh rate changed.
Alec Mouri754c98a2019-03-18 18:53:42 -070093 // This way we can move out of early offsets if no longer necessary.
Dominik Laskowski08d05c22020-07-22 00:05:08 -070094 [[nodiscard]] OffsetsOpt onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070095
Dominik Laskowski08d05c22020-07-22 00:05:08 -070096 [[nodiscard]] OffsetsOpt onDisplayRefresh(bool usedGpuComposition);
Ady Abrahambe0f9482019-04-24 15:41:53 -070097
Dan Stoza2713c302018-03-28 17:07:36 -070098private:
Dominik Laskowskieddeda12019-07-19 11:54:13 -070099 const Offsets& getNextOffsets() const REQUIRES(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700100 [[nodiscard]] Offsets updateOffsets() EXCLUDES(mMutex);
101 [[nodiscard]] Offsets updateOffsetsLocked() REQUIRES(mMutex);
Dan Stoza2713c302018-03-28 17:07:36 -0700102
Alec Mourid7599d82019-05-22 19:58:00 -0700103 mutable std::mutex mMutex;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700104 OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200105
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700106 Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200107
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700108 using Schedule = TransactionSchedule;
109 std::atomic<Schedule> mTransactionSchedule = Schedule::Late;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700110 std::atomic<bool> mExplicitEarlyWakeup = false;
Alec Mourid7599d82019-05-22 19:58:00 -0700111
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700112 std::atomic<bool> mRefreshRateChangePending = false;
113
114 std::atomic<int> mEarlyTransactionFrames = 0;
115 std::atomic<int> mEarlyGpuFrames = 0;
116 std::atomic<TimePoint> mEarlyTransactionStartTime = TimePoint();
117 std::atomic<TimePoint> mLastTransactionCommitTime = TimePoint();
118
119 const Now mNow;
120 const bool mTraceDetailedInfo;
Dan Stoza2713c302018-03-28 17:07:36 -0700121};
122
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700123} // namespace android::scheduler