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