Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 19 | #include <chrono> |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 20 | #include <mutex> |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 21 | #include <optional> |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 22 | #include <unordered_set> |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 23 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 24 | #include <android-base/thread_annotations.h> |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 25 | #include <binder/IBinder.h> |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 26 | #include <utils/Timers.h> |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 27 | |
Angel Aguayo | b398ee2 | 2021-10-14 00:39:15 +0000 | [diff] [blame] | 28 | #include "../WpHash.h" |
| 29 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 30 | namespace android::scheduler { |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 31 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 32 | // 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. |
| 35 | enum class TransactionSchedule { |
| 36 | Late, // Default. |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 37 | EarlyStart, |
| 38 | EarlyEnd |
| 39 | }; |
Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 40 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 41 | // Modulates VSYNC phase depending on transaction schedule and refresh rate changes. |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 42 | class VsyncModulator : public IBinder::DeathRecipient { |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 43 | public: |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 44 | // 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 Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 54 | // 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 Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 60 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 61 | 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 Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 70 | using VsyncConfigOpt = std::optional<VsyncConfig>; |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 71 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 72 | 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 Abraham | caba298 | 2021-06-16 16:45:04 -0700 | [diff] [blame] | 76 | std::chrono::nanoseconds hwcMinWorkDuration; // Used for calculating the |
| 77 | // earliest present time |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 78 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 79 | bool operator==(const VsyncConfigSet& other) const { |
Ady Abraham | caba298 | 2021-06-16 16:45:04 -0700 | [diff] [blame] | 80 | return early == other.early && earlyGpu == other.earlyGpu && late == other.late && |
| 81 | hwcMinWorkDuration == other.hwcMinWorkDuration; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 84 | bool operator!=(const VsyncConfigSet& other) const { return !(*this == other); } |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 87 | using Clock = std::chrono::steady_clock; |
| 88 | using TimePoint = Clock::time_point; |
| 89 | using Now = TimePoint (*)(); |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 90 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 91 | explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now); |
Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 92 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 93 | VsyncConfig getVsyncConfig() const EXCLUDES(mMutex); |
Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 94 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 95 | [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex); |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 96 | |
| 97 | // Changes offsets in response to transaction flags or commit. |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 98 | [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule, |
| 99 | const sp<IBinder>& = {}) EXCLUDES(mMutex); |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 100 | [[nodiscard]] VsyncConfigOpt onTransactionCommit(); |
Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 101 | |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 102 | // Called when we send a refresh rate change to hardware composer, so that |
| 103 | // we can move into early offsets. |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 104 | [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated(); |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 105 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 106 | // Called when we detect from VSYNC signals that the refresh rate changed. |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 107 | // This way we can move out of early offsets if no longer necessary. |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 108 | [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted(); |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 109 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 110 | [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition); |
Ady Abraham | be0f948 | 2019-04-24 15:41:53 -0700 | [diff] [blame] | 111 | |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 112 | [[nodiscard]] bool isVsyncConfigDefault() const; |
| 113 | |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 114 | protected: |
| 115 | // Called from unit tests as well |
| 116 | void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex); |
| 117 | |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 118 | private: |
Ady Abraham | 2739e83 | 2022-02-14 17:42:00 -0800 | [diff] [blame] | 119 | enum class VsyncConfigType { Early, EarlyGpu, Late }; |
| 120 | |
| 121 | VsyncConfigType getNextVsyncConfigType() const REQUIRES(mMutex); |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 122 | const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex); |
| 123 | [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex); |
| 124 | [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex); |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 125 | |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 126 | mutable std::mutex mMutex; |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 127 | VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex); |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 128 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 129 | VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late}; |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 130 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 131 | using Schedule = TransactionSchedule; |
| 132 | std::atomic<Schedule> mTransactionSchedule = Schedule::Late; |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 133 | |
Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 134 | std::unordered_set<wp<IBinder>, WpHash> mEarlyWakeupRequests GUARDED_BY(mMutex); |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 135 | 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 Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 146 | } // namespace android::scheduler |