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 <scheduler/TransactionSchedule.h> 29 #include <scheduler/VsyncConfig.h> 30 31 #include "../WpHash.h" 32 33 namespace android::scheduler { 34 35 // Modulates VSYNC phase depending on transaction schedule and refresh rate changes. 36 class VsyncModulator : public IBinder::DeathRecipient { 37 public: 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 48 using VsyncConfigOpt = std::optional<VsyncConfig>; 49 50 using Clock = std::chrono::steady_clock; 51 using TimePoint = Clock::time_point; 52 using Now = TimePoint (*)(); 53 54 explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now); 55 56 bool isVsyncConfigEarly() const EXCLUDES(mMutex); 57 58 VsyncConfig getVsyncConfig() const EXCLUDES(mMutex); 59 cancelRefreshRateChange()60 void cancelRefreshRateChange() { mRefreshRateChangePending = false; } 61 62 [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex); 63 64 // Changes offsets in response to transaction flags or commit. 65 [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule, 66 const sp<IBinder>& = {}) EXCLUDES(mMutex); 67 [[nodiscard]] VsyncConfigOpt onTransactionCommit(); 68 69 // Called when we send a refresh rate change to hardware composer, so that 70 // we can move into early offsets. 71 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated(); 72 73 // Called when we detect from VSYNC signals that the refresh rate changed. 74 // This way we can move out of early offsets if no longer necessary. 75 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted(); 76 77 [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition); 78 79 protected: 80 // Called from unit tests as well 81 void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex); 82 83 private: 84 enum class VsyncConfigType { Early, EarlyGpu, Late }; 85 86 VsyncConfigType getNextVsyncConfigType() const REQUIRES(mMutex); 87 const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex); 88 [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex); 89 [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex); 90 91 mutable std::mutex mMutex; 92 VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex); 93 GUARDED_BY(mMutex)94 VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late}; 95 96 using Schedule = TransactionSchedule; 97 std::atomic<Schedule> mTransactionSchedule = Schedule::Late; 98 99 std::unordered_set<wp<IBinder>, WpHash> mEarlyWakeupRequests GUARDED_BY(mMutex); 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; 108 const bool mTraceDetailedInfo; 109 }; 110 111 } // namespace android::scheduler 112