blob: 9410768b2d2533c12cdaafb6162d732dd2d89ca5 [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.
Dominik Laskowski08d05c22020-07-22 00:05:08 -070033 EarlyStart,
34 EarlyEnd
35};
Jorim Jaggi90535212018-05-23 23:44:06 +020036
Dominik Laskowski08d05c22020-07-22 00:05:08 -070037// Modulates VSYNC phase depending on transaction schedule and refresh rate changes.
38class VsyncModulator {
Dan Stoza2713c302018-03-28 17:07:36 -070039public:
Dominik Laskowski08d05c22020-07-22 00:05:08 -070040 // Number of frames to keep early offsets after an early transaction or GPU composition.
41 // This acts as a low-pass filter in case subsequent transactions are delayed, or if the
42 // composition strategy alternates on subsequent frames.
43 static constexpr int MIN_EARLY_TRANSACTION_FRAMES = 2;
44 static constexpr int MIN_EARLY_GPU_FRAMES = 2;
45
46 // Duration to delay the MIN_EARLY_TRANSACTION_FRAMES countdown after an early transaction.
47 // This may keep early offsets for an extra frame, but avoids a race with transaction commit.
48 static const std::chrono::nanoseconds MIN_EARLY_TRANSACTION_TIME;
49
Ady Abraham8287e852020-08-12 14:44:58 -070050 // Phase offsets and work durations for SF and app deadlines from VSYNC.
51 struct VsyncConfig {
52 nsecs_t sfOffset;
53 nsecs_t appOffset;
54 std::chrono::nanoseconds sfWorkDuration;
55 std::chrono::nanoseconds appWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -080056
Ady Abraham8287e852020-08-12 14:44:58 -070057 bool operator==(const VsyncConfig& other) const {
58 return sfOffset == other.sfOffset && appOffset == other.appOffset &&
59 sfWorkDuration == other.sfWorkDuration &&
60 appWorkDuration == other.appWorkDuration;
61 }
62
63 bool operator!=(const VsyncConfig& other) const { return !(*this == other); }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020064 };
65
Ady Abraham8287e852020-08-12 14:44:58 -070066 using VsyncConfigOpt = std::optional<VsyncConfig>;
Dominik Laskowski08d05c22020-07-22 00:05:08 -070067
Ady Abraham8287e852020-08-12 14:44:58 -070068 struct VsyncConfigSet {
69 VsyncConfig early; // Used for early transactions, and during refresh rate change.
70 VsyncConfig earlyGpu; // Used during GPU composition.
71 VsyncConfig late; // Default.
Ady Abrahamcaba2982021-06-16 16:45:04 -070072 std::chrono::nanoseconds hwcMinWorkDuration; // Used for calculating the
73 // earliest present time
Dominik Laskowskieddeda12019-07-19 11:54:13 -070074
Ady Abraham8287e852020-08-12 14:44:58 -070075 bool operator==(const VsyncConfigSet& other) const {
Ady Abrahamcaba2982021-06-16 16:45:04 -070076 return early == other.early && earlyGpu == other.earlyGpu && late == other.late &&
77 hwcMinWorkDuration == other.hwcMinWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -080078 }
79
Ady Abraham8287e852020-08-12 14:44:58 -070080 bool operator!=(const VsyncConfigSet& other) const { return !(*this == other); }
Alec Mourid7599d82019-05-22 19:58:00 -070081 };
82
Dominik Laskowski08d05c22020-07-22 00:05:08 -070083 using Clock = std::chrono::steady_clock;
84 using TimePoint = Clock::time_point;
85 using Now = TimePoint (*)();
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020086
Ady Abraham8287e852020-08-12 14:44:58 -070087 explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now);
Ana Krulec12096d02018-09-07 14:54:14 -070088
Ady Abraham8287e852020-08-12 14:44:58 -070089 VsyncConfig getVsyncConfig() const EXCLUDES(mMutex);
Jorim Jaggi90535212018-05-23 23:44:06 +020090
Ady Abraham8287e852020-08-12 14:44:58 -070091 [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -070092
93 // Changes offsets in response to transaction flags or commit.
Ady Abraham8287e852020-08-12 14:44:58 -070094 [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule);
95 [[nodiscard]] VsyncConfigOpt onTransactionCommit();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010096
Alec Mouri754c98a2019-03-18 18:53:42 -070097 // Called when we send a refresh rate change to hardware composer, so that
98 // we can move into early offsets.
Ady Abraham8287e852020-08-12 14:44:58 -070099 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -0700100
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700101 // Called when we detect from VSYNC signals that the refresh rate changed.
Alec Mouri754c98a2019-03-18 18:53:42 -0700102 // This way we can move out of early offsets if no longer necessary.
Ady Abraham8287e852020-08-12 14:44:58 -0700103 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -0700104
Ady Abraham8287e852020-08-12 14:44:58 -0700105 [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition);
Ady Abrahambe0f9482019-04-24 15:41:53 -0700106
Dan Stoza2713c302018-03-28 17:07:36 -0700107private:
Ady Abraham8287e852020-08-12 14:44:58 -0700108 const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex);
109 [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex);
110 [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex);
Dan Stoza2713c302018-03-28 17:07:36 -0700111
Alec Mourid7599d82019-05-22 19:58:00 -0700112 mutable std::mutex mMutex;
Ady Abraham8287e852020-08-12 14:44:58 -0700113 VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200114
Ady Abraham8287e852020-08-12 14:44:58 -0700115 VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200116
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700117 using Schedule = TransactionSchedule;
118 std::atomic<Schedule> mTransactionSchedule = Schedule::Late;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700119 std::atomic<bool> mEarlyWakeup = false;
Alec Mourid7599d82019-05-22 19:58:00 -0700120
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700121 std::atomic<bool> mRefreshRateChangePending = false;
122
123 std::atomic<int> mEarlyTransactionFrames = 0;
124 std::atomic<int> mEarlyGpuFrames = 0;
125 std::atomic<TimePoint> mEarlyTransactionStartTime = TimePoint();
126 std::atomic<TimePoint> mLastTransactionCommitTime = TimePoint();
127
128 const Now mNow;
129 const bool mTraceDetailedInfo;
Dan Stoza2713c302018-03-28 17:07:36 -0700130};
131
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700132} // namespace android::scheduler