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> |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 22 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
| 24 | #include <utils/Timers.h> |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 25 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 26 | namespace android::scheduler { |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 27 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 28 | // 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. |
| 31 | enum class TransactionSchedule { |
| 32 | Late, // Default. |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 33 | EarlyStart, |
| 34 | EarlyEnd |
| 35 | }; |
Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 36 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 37 | // Modulates VSYNC phase depending on transaction schedule and refresh rate changes. |
| 38 | class VsyncModulator { |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 39 | public: |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 40 | // 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 Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 50 | // 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 Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 56 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 57 | 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 Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 66 | using VsyncConfigOpt = std::optional<VsyncConfig>; |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 67 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 68 | 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 Abraham | caba298 | 2021-06-16 16:45:04 -0700 | [diff] [blame^] | 72 | std::chrono::nanoseconds hwcMinWorkDuration; // Used for calculating the |
| 73 | // earliest present time |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 74 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 75 | bool operator==(const VsyncConfigSet& other) const { |
Ady Abraham | caba298 | 2021-06-16 16:45:04 -0700 | [diff] [blame^] | 76 | return early == other.early && earlyGpu == other.earlyGpu && late == other.late && |
| 77 | hwcMinWorkDuration == other.hwcMinWorkDuration; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 80 | bool operator!=(const VsyncConfigSet& other) const { return !(*this == other); } |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 83 | using Clock = std::chrono::steady_clock; |
| 84 | using TimePoint = Clock::time_point; |
| 85 | using Now = TimePoint (*)(); |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 86 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 87 | explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now); |
Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 88 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 89 | VsyncConfig getVsyncConfig() const EXCLUDES(mMutex); |
Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 90 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 91 | [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex); |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 92 | |
| 93 | // Changes offsets in response to transaction flags or commit. |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 94 | [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule); |
| 95 | [[nodiscard]] VsyncConfigOpt onTransactionCommit(); |
Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 96 | |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 97 | // Called when we send a refresh rate change to hardware composer, so that |
| 98 | // we can move into early offsets. |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 99 | [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated(); |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 100 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 101 | // Called when we detect from VSYNC signals that the refresh rate changed. |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 102 | // 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] | 103 | [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted(); |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 104 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 105 | [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition); |
Ady Abraham | be0f948 | 2019-04-24 15:41:53 -0700 | [diff] [blame] | 106 | |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 107 | private: |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 108 | const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex); |
| 109 | [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex); |
| 110 | [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex); |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 111 | |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 112 | mutable std::mutex mMutex; |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 113 | VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex); |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 114 | |
Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 115 | VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late}; |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 116 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 117 | using Schedule = TransactionSchedule; |
| 118 | std::atomic<Schedule> mTransactionSchedule = Schedule::Late; |
Ady Abraham | 8cbd307 | 2021-03-15 16:39:06 -0700 | [diff] [blame] | 119 | std::atomic<bool> mEarlyWakeup = false; |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 120 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame] | 121 | 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 Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 132 | } // namespace android::scheduler |