| 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 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 28 | namespace android::scheduler { | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 29 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 30 | // State machine controlled by transaction flags. VsyncModulator switches to early phase offsets | 
|  | 31 | // when a transaction is flagged EarlyStart or Early, lasting until an EarlyEnd transaction or a | 
|  | 32 | // fixed number of frames, respectively. | 
|  | 33 | enum class TransactionSchedule { | 
|  | 34 | Late,  // Default. | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 35 | EarlyStart, | 
|  | 36 | EarlyEnd | 
|  | 37 | }; | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 38 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 39 | // Modulates VSYNC phase depending on transaction schedule and refresh rate changes. | 
| Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 40 | class VsyncModulator : public IBinder::DeathRecipient { | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 41 | public: | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 42 | // Number of frames to keep early offsets after an early transaction or GPU composition. | 
|  | 43 | // This acts as a low-pass filter in case subsequent transactions are delayed, or if the | 
|  | 44 | // composition strategy alternates on subsequent frames. | 
|  | 45 | static constexpr int MIN_EARLY_TRANSACTION_FRAMES = 2; | 
|  | 46 | static constexpr int MIN_EARLY_GPU_FRAMES = 2; | 
|  | 47 |  | 
|  | 48 | // Duration to delay the MIN_EARLY_TRANSACTION_FRAMES countdown after an early transaction. | 
|  | 49 | // This may keep early offsets for an extra frame, but avoids a race with transaction commit. | 
|  | 50 | static const std::chrono::nanoseconds MIN_EARLY_TRANSACTION_TIME; | 
|  | 51 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 52 | // Phase offsets and work durations for SF and app deadlines from VSYNC. | 
|  | 53 | struct VsyncConfig { | 
|  | 54 | nsecs_t sfOffset; | 
|  | 55 | nsecs_t appOffset; | 
|  | 56 | std::chrono::nanoseconds sfWorkDuration; | 
|  | 57 | std::chrono::nanoseconds appWorkDuration; | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 58 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 59 | bool operator==(const VsyncConfig& other) const { | 
|  | 60 | return sfOffset == other.sfOffset && appOffset == other.appOffset && | 
|  | 61 | sfWorkDuration == other.sfWorkDuration && | 
|  | 62 | appWorkDuration == other.appWorkDuration; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | bool operator!=(const VsyncConfig& other) const { return !(*this == other); } | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 66 | }; | 
|  | 67 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 68 | using VsyncConfigOpt = std::optional<VsyncConfig>; | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 69 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 70 | struct VsyncConfigSet { | 
|  | 71 | VsyncConfig early;    // Used for early transactions, and during refresh rate change. | 
|  | 72 | VsyncConfig earlyGpu; // Used during GPU composition. | 
|  | 73 | VsyncConfig late;     // Default. | 
| Ady Abraham | caba298 | 2021-06-16 16:45:04 -0700 | [diff] [blame] | 74 | std::chrono::nanoseconds hwcMinWorkDuration; // Used for calculating the | 
|  | 75 | // earliest present time | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 76 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 77 | bool operator==(const VsyncConfigSet& other) const { | 
| Ady Abraham | caba298 | 2021-06-16 16:45:04 -0700 | [diff] [blame] | 78 | return early == other.early && earlyGpu == other.earlyGpu && late == other.late && | 
|  | 79 | hwcMinWorkDuration == other.hwcMinWorkDuration; | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 82 | bool operator!=(const VsyncConfigSet& other) const { return !(*this == other); } | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 83 | }; | 
|  | 84 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 85 | using Clock = std::chrono::steady_clock; | 
|  | 86 | using TimePoint = Clock::time_point; | 
|  | 87 | using Now = TimePoint (*)(); | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 88 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 89 | explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now); | 
| Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 90 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 91 | VsyncConfig getVsyncConfig() const EXCLUDES(mMutex); | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 92 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 93 | [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex); | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | // Changes offsets in response to transaction flags or commit. | 
| Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 96 | [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule, | 
|  | 97 | const sp<IBinder>& = {}) EXCLUDES(mMutex); | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 98 | [[nodiscard]] VsyncConfigOpt onTransactionCommit(); | 
| Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 99 |  | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 100 | // Called when we send a refresh rate change to hardware composer, so that | 
|  | 101 | // we can move into early offsets. | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 102 | [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 103 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 104 | // Called when we detect from VSYNC signals that the refresh rate changed. | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 105 | // 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] | 106 | [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 107 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 108 | [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition); | 
| Ady Abraham | be0f948 | 2019-04-24 15:41:53 -0700 | [diff] [blame] | 109 |  | 
| Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 110 | protected: | 
|  | 111 | // Called from unit tests as well | 
|  | 112 | void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex); | 
|  | 113 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 114 | private: | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 115 | const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex); | 
|  | 116 | [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex); | 
|  | 117 | [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex); | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 118 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 119 | mutable std::mutex mMutex; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 120 | VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex); | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 121 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 122 | VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late}; | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 123 |  | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 124 | using Schedule = TransactionSchedule; | 
|  | 125 | std::atomic<Schedule> mTransactionSchedule = Schedule::Late; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 126 |  | 
| Ady Abraham | 23ea9da | 2021-07-14 16:32:56 -0700 | [diff] [blame] | 127 | struct WpHash { | 
|  | 128 | size_t operator()(const wp<IBinder>& p) const { | 
|  | 129 | return std::hash<IBinder*>()(p.unsafe_get()); | 
|  | 130 | } | 
|  | 131 | }; | 
|  | 132 |  | 
|  | 133 | std::unordered_set<wp<IBinder>, WpHash> mEarlyWakeupRequests GUARDED_BY(mMutex); | 
| Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 134 | std::atomic<bool> mRefreshRateChangePending = false; | 
|  | 135 |  | 
|  | 136 | std::atomic<int> mEarlyTransactionFrames = 0; | 
|  | 137 | std::atomic<int> mEarlyGpuFrames = 0; | 
|  | 138 | std::atomic<TimePoint> mEarlyTransactionStartTime = TimePoint(); | 
|  | 139 | std::atomic<TimePoint> mLastTransactionCommitTime = TimePoint(); | 
|  | 140 |  | 
|  | 141 | const Now mNow; | 
|  | 142 | const bool mTraceDetailedInfo; | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 143 | }; | 
|  | 144 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 145 | } // namespace android::scheduler |