blob: 537cae1d7c61dda87e3553a1f7cc67a99d688edb [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>
Ady Abraham23ea9da2021-07-14 16:32:56 -070022#include <unordered_set>
Dan Stoza2713c302018-03-28 17:07:36 -070023
Dominik Laskowski08d05c22020-07-22 00:05:08 -070024#include <android-base/thread_annotations.h>
Ady Abraham23ea9da2021-07-14 16:32:56 -070025#include <binder/IBinder.h>
Dominik Laskowski08d05c22020-07-22 00:05:08 -070026#include <utils/Timers.h>
Dan Stoza2713c302018-03-28 17:07:36 -070027
Angel Aguayob398ee22021-10-14 00:39:15 +000028#include "../WpHash.h"
29
Dominik Laskowskieddeda12019-07-19 11:54:13 -070030namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070031
Dominik Laskowski08d05c22020-07-22 00:05:08 -070032// State machine controlled by transaction flags. VsyncModulator switches to early phase offsets
33// when a transaction is flagged EarlyStart or Early, lasting until an EarlyEnd transaction or a
34// fixed number of frames, respectively.
35enum class TransactionSchedule {
36 Late, // Default.
Dominik Laskowski08d05c22020-07-22 00:05:08 -070037 EarlyStart,
38 EarlyEnd
39};
Jorim Jaggi90535212018-05-23 23:44:06 +020040
Dominik Laskowski08d05c22020-07-22 00:05:08 -070041// Modulates VSYNC phase depending on transaction schedule and refresh rate changes.
Ady Abraham23ea9da2021-07-14 16:32:56 -070042class VsyncModulator : public IBinder::DeathRecipient {
Dan Stoza2713c302018-03-28 17:07:36 -070043public:
Dominik Laskowski08d05c22020-07-22 00:05:08 -070044 // Number of frames to keep early offsets after an early transaction or GPU composition.
45 // This acts as a low-pass filter in case subsequent transactions are delayed, or if the
46 // composition strategy alternates on subsequent frames.
47 static constexpr int MIN_EARLY_TRANSACTION_FRAMES = 2;
48 static constexpr int MIN_EARLY_GPU_FRAMES = 2;
49
50 // Duration to delay the MIN_EARLY_TRANSACTION_FRAMES countdown after an early transaction.
51 // This may keep early offsets for an extra frame, but avoids a race with transaction commit.
52 static const std::chrono::nanoseconds MIN_EARLY_TRANSACTION_TIME;
53
Ady Abraham8287e852020-08-12 14:44:58 -070054 // Phase offsets and work durations for SF and app deadlines from VSYNC.
55 struct VsyncConfig {
56 nsecs_t sfOffset;
57 nsecs_t appOffset;
58 std::chrono::nanoseconds sfWorkDuration;
59 std::chrono::nanoseconds appWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -080060
Ady Abraham8287e852020-08-12 14:44:58 -070061 bool operator==(const VsyncConfig& other) const {
62 return sfOffset == other.sfOffset && appOffset == other.appOffset &&
63 sfWorkDuration == other.sfWorkDuration &&
64 appWorkDuration == other.appWorkDuration;
65 }
66
67 bool operator!=(const VsyncConfig& other) const { return !(*this == other); }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020068 };
69
Ady Abraham8287e852020-08-12 14:44:58 -070070 using VsyncConfigOpt = std::optional<VsyncConfig>;
Dominik Laskowski08d05c22020-07-22 00:05:08 -070071
Ady Abraham8287e852020-08-12 14:44:58 -070072 struct VsyncConfigSet {
73 VsyncConfig early; // Used for early transactions, and during refresh rate change.
74 VsyncConfig earlyGpu; // Used during GPU composition.
75 VsyncConfig late; // Default.
Ady Abrahamcaba2982021-06-16 16:45:04 -070076 std::chrono::nanoseconds hwcMinWorkDuration; // Used for calculating the
77 // earliest present time
Dominik Laskowskieddeda12019-07-19 11:54:13 -070078
Ady Abraham8287e852020-08-12 14:44:58 -070079 bool operator==(const VsyncConfigSet& other) const {
Ady Abrahamcaba2982021-06-16 16:45:04 -070080 return early == other.early && earlyGpu == other.earlyGpu && late == other.late &&
81 hwcMinWorkDuration == other.hwcMinWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -080082 }
83
Ady Abraham8287e852020-08-12 14:44:58 -070084 bool operator!=(const VsyncConfigSet& other) const { return !(*this == other); }
Alec Mourid7599d82019-05-22 19:58:00 -070085 };
86
Dominik Laskowski08d05c22020-07-22 00:05:08 -070087 using Clock = std::chrono::steady_clock;
88 using TimePoint = Clock::time_point;
89 using Now = TimePoint (*)();
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020090
Ady Abraham8287e852020-08-12 14:44:58 -070091 explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now);
Ana Krulec12096d02018-09-07 14:54:14 -070092
Ady Abraham8287e852020-08-12 14:44:58 -070093 VsyncConfig getVsyncConfig() const EXCLUDES(mMutex);
Jorim Jaggi90535212018-05-23 23:44:06 +020094
Ady Abraham8287e852020-08-12 14:44:58 -070095 [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -070096
97 // Changes offsets in response to transaction flags or commit.
Ady Abraham23ea9da2021-07-14 16:32:56 -070098 [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule,
99 const sp<IBinder>& = {}) EXCLUDES(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -0700100 [[nodiscard]] VsyncConfigOpt onTransactionCommit();
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100101
Alec Mouri754c98a2019-03-18 18:53:42 -0700102 // Called when we send a refresh rate change to hardware composer, so that
103 // we can move into early offsets.
Ady Abraham8287e852020-08-12 14:44:58 -0700104 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -0700105
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700106 // Called when we detect from VSYNC signals that the refresh rate changed.
Alec Mouri754c98a2019-03-18 18:53:42 -0700107 // This way we can move out of early offsets if no longer necessary.
Ady Abraham8287e852020-08-12 14:44:58 -0700108 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -0700109
Ady Abraham8287e852020-08-12 14:44:58 -0700110 [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition);
Ady Abrahambe0f9482019-04-24 15:41:53 -0700111
Ady Abraham2739e832022-02-14 17:42:00 -0800112 [[nodiscard]] bool isVsyncConfigDefault() const;
113
Ady Abraham23ea9da2021-07-14 16:32:56 -0700114protected:
115 // Called from unit tests as well
116 void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex);
117
Dan Stoza2713c302018-03-28 17:07:36 -0700118private:
Ady Abraham2739e832022-02-14 17:42:00 -0800119 enum class VsyncConfigType { Early, EarlyGpu, Late };
120
121 VsyncConfigType getNextVsyncConfigType() const REQUIRES(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -0700122 const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex);
123 [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex);
124 [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex);
Dan Stoza2713c302018-03-28 17:07:36 -0700125
Alec Mourid7599d82019-05-22 19:58:00 -0700126 mutable std::mutex mMutex;
Ady Abraham8287e852020-08-12 14:44:58 -0700127 VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200128
Ady Abraham8287e852020-08-12 14:44:58 -0700129 VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200130
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700131 using Schedule = TransactionSchedule;
132 std::atomic<Schedule> mTransactionSchedule = Schedule::Late;
Alec Mourid7599d82019-05-22 19:58:00 -0700133
Ady Abraham23ea9da2021-07-14 16:32:56 -0700134 std::unordered_set<wp<IBinder>, WpHash> mEarlyWakeupRequests GUARDED_BY(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700135 std::atomic<bool> mRefreshRateChangePending = false;
136
137 std::atomic<int> mEarlyTransactionFrames = 0;
138 std::atomic<int> mEarlyGpuFrames = 0;
139 std::atomic<TimePoint> mEarlyTransactionStartTime = TimePoint();
140 std::atomic<TimePoint> mLastTransactionCommitTime = TimePoint();
141
142 const Now mNow;
143 const bool mTraceDetailedInfo;
Dan Stoza2713c302018-03-28 17:07:36 -0700144};
145
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700146} // namespace android::scheduler