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