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 19 #include <chrono> 20 #include <mutex> 21 #include <optional> 22 #include <unordered_set> 23 24 #include <android-base/thread_annotations.h> 25 #include <binder/IBinder.h> 26 #include <utils/Timers.h> 27 28 #include "../WpHash.h" 29 30 namespace android::scheduler { 31 32 // State machine controlled by transaction flags. VsyncModulator switches to early phase offsets 33 // when a transaction is flagged EarlyStart or Early, lasting until an EarlyEnd transaction or a 34 // fixed number of frames, respectively. 35 enum class TransactionSchedule { 36 Late, // Default. 37 EarlyStart, 38 EarlyEnd 39 }; 40 41 // Modulates VSYNC phase depending on transaction schedule and refresh rate changes. 42 class VsyncModulator : public IBinder::DeathRecipient { 43 public: 44 // Number of frames to keep early offsets after an early transaction or GPU composition. 45 // This acts as a low-pass filter in case subsequent transactions are delayed, or if the 46 // composition strategy alternates on subsequent frames. 47 static constexpr int MIN_EARLY_TRANSACTION_FRAMES = 2; 48 static constexpr int MIN_EARLY_GPU_FRAMES = 2; 49 50 // Duration to delay the MIN_EARLY_TRANSACTION_FRAMES countdown after an early transaction. 51 // This may keep early offsets for an extra frame, but avoids a race with transaction commit. 52 static const std::chrono::nanoseconds MIN_EARLY_TRANSACTION_TIME; 53 54 // Phase offsets and work durations for SF and app deadlines from VSYNC. 55 struct VsyncConfig { 56 nsecs_t sfOffset; 57 nsecs_t appOffset; 58 std::chrono::nanoseconds sfWorkDuration; 59 std::chrono::nanoseconds appWorkDuration; 60 61 bool operator==(const VsyncConfig& other) const { 62 return sfOffset == other.sfOffset && appOffset == other.appOffset && 63 sfWorkDuration == other.sfWorkDuration && 64 appWorkDuration == other.appWorkDuration; 65 } 66 67 bool operator!=(const VsyncConfig& other) const { return !(*this == other); } 68 }; 69 70 using VsyncConfigOpt = std::optional<VsyncConfig>; 71 72 struct VsyncConfigSet { 73 VsyncConfig early; // Used for early transactions, and during refresh rate change. 74 VsyncConfig earlyGpu; // Used during GPU composition. 75 VsyncConfig late; // Default. 76 std::chrono::nanoseconds hwcMinWorkDuration; // Used for calculating the 77 // earliest present time 78 79 bool operator==(const VsyncConfigSet& other) const { 80 return early == other.early && earlyGpu == other.earlyGpu && late == other.late && 81 hwcMinWorkDuration == other.hwcMinWorkDuration; 82 } 83 84 bool operator!=(const VsyncConfigSet& other) const { return !(*this == other); } 85 }; 86 87 using Clock = std::chrono::steady_clock; 88 using TimePoint = Clock::time_point; 89 using Now = TimePoint (*)(); 90 91 explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now); 92 93 VsyncConfig getVsyncConfig() const EXCLUDES(mMutex); 94 95 [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex); 96 97 // Changes offsets in response to transaction flags or commit. 98 [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule, 99 const sp<IBinder>& = {}) EXCLUDES(mMutex); 100 [[nodiscard]] VsyncConfigOpt onTransactionCommit(); 101 102 // Called when we send a refresh rate change to hardware composer, so that 103 // we can move into early offsets. 104 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated(); 105 106 // Called when we detect from VSYNC signals that the refresh rate changed. 107 // This way we can move out of early offsets if no longer necessary. 108 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted(); 109 110 [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition); 111 112 [[nodiscard]] bool isVsyncConfigDefault() const; 113 114 protected: 115 // Called from unit tests as well 116 void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex); 117 118 private: 119 enum class VsyncConfigType { Early, EarlyGpu, Late }; 120 121 VsyncConfigType getNextVsyncConfigType() const REQUIRES(mMutex); 122 const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex); 123 [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex); 124 [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex); 125 126 mutable std::mutex mMutex; 127 VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex); 128 GUARDED_BY(mMutex)129 VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late}; 130 131 using Schedule = TransactionSchedule; 132 std::atomic<Schedule> mTransactionSchedule = Schedule::Late; 133 134 std::unordered_set<wp<IBinder>, WpHash> mEarlyWakeupRequests GUARDED_BY(mMutex); 135 std::atomic<bool> mRefreshRateChangePending = false; 136 137 std::atomic<int> mEarlyTransactionFrames = 0; 138 std::atomic<int> mEarlyGpuFrames = 0; 139 std::atomic<TimePoint> mEarlyTransactionStartTime = TimePoint(); 140 std::atomic<TimePoint> mLastTransactionCommitTime = TimePoint(); 141 142 const Now mNow; 143 const bool mTraceDetailedInfo; 144 }; 145 146 } // namespace android::scheduler 147