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 | |
| 51 | // Phase offsets for SF and app deadlines from VSYNC. |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 52 | struct Offsets { |
| 53 | nsecs_t sf; |
| 54 | nsecs_t app; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 55 | |
| 56 | bool operator==(const Offsets& other) const { return sf == other.sf && app == other.app; } |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 57 | bool operator!=(const Offsets& other) const { return !(*this == other); } |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 60 | using OffsetsOpt = std::optional<Offsets>; |
| 61 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 62 | struct OffsetsConfig { |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 63 | Offsets early; // Used for early transactions, and during refresh rate change. |
| 64 | Offsets earlyGpu; // Used during GPU composition. |
| 65 | Offsets late; // Default. |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 66 | |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 67 | bool operator==(const OffsetsConfig& other) const { |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 68 | return early == other.early && earlyGpu == other.earlyGpu && late == other.late; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool operator!=(const OffsetsConfig& other) const { return !(*this == other); } |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 74 | using Clock = std::chrono::steady_clock; |
| 75 | using TimePoint = Clock::time_point; |
| 76 | using Now = TimePoint (*)(); |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 77 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 78 | explicit VsyncModulator(const OffsetsConfig&, Now = Clock::now); |
Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 79 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 80 | Offsets getOffsets() const EXCLUDES(mMutex); |
Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 81 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 82 | [[nodiscard]] Offsets setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex); |
| 83 | |
| 84 | // Changes offsets in response to transaction flags or commit. |
| 85 | [[nodiscard]] OffsetsOpt setTransactionSchedule(TransactionSchedule); |
| 86 | [[nodiscard]] OffsetsOpt onTransactionCommit(); |
Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 87 | |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 88 | // Called when we send a refresh rate change to hardware composer, so that |
| 89 | // we can move into early offsets. |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 90 | [[nodiscard]] OffsetsOpt onRefreshRateChangeInitiated(); |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 91 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 92 | // Called when we detect from VSYNC signals that the refresh rate changed. |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 93 | // This way we can move out of early offsets if no longer necessary. |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 94 | [[nodiscard]] OffsetsOpt onRefreshRateChangeCompleted(); |
Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 95 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 96 | [[nodiscard]] OffsetsOpt onDisplayRefresh(bool usedGpuComposition); |
Ady Abraham | be0f948 | 2019-04-24 15:41:53 -0700 | [diff] [blame] | 97 | |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 98 | private: |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 99 | const Offsets& getNextOffsets() const REQUIRES(mMutex); |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 100 | [[nodiscard]] Offsets updateOffsets() EXCLUDES(mMutex); |
| 101 | [[nodiscard]] Offsets updateOffsetsLocked() REQUIRES(mMutex); |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 102 | |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 103 | mutable std::mutex mMutex; |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 104 | OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex); |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 105 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 106 | Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late}; |
Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 107 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 108 | using Schedule = TransactionSchedule; |
| 109 | std::atomic<Schedule> mTransactionSchedule = Schedule::Late; |
Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 110 | std::atomic<bool> mExplicitEarlyWakeup = false; |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 111 | |
Dominik Laskowski | 08d05c2 | 2020-07-22 00:05:08 -0700 | [diff] [blame^] | 112 | std::atomic<bool> mRefreshRateChangePending = false; |
| 113 | |
| 114 | std::atomic<int> mEarlyTransactionFrames = 0; |
| 115 | std::atomic<int> mEarlyGpuFrames = 0; |
| 116 | std::atomic<TimePoint> mEarlyTransactionStartTime = TimePoint(); |
| 117 | std::atomic<TimePoint> mLastTransactionCommitTime = TimePoint(); |
| 118 | |
| 119 | const Now mNow; |
| 120 | const bool mTraceDetailedInfo; |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 123 | } // namespace android::scheduler |