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