blob: be0d3348b5479bdc81a800b57e8f060afc704863 [file] [log] [blame]
Dan Stoza2713c302018-03-28 17:07:36 -07001/*
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 Mourif792cfe2020-06-05 13:11:23 -070019#include <chrono>
Dan Stoza2713c302018-03-28 17:07:36 -070020#include <mutex>
Dominik Laskowski08d05c22020-07-22 00:05:08 -070021#include <optional>
Ady Abraham23ea9da2021-07-14 16:32:56 -070022#include <unordered_set>
Dan Stoza2713c302018-03-28 17:07:36 -070023
Dominik Laskowski08d05c22020-07-22 00:05:08 -070024#include <android-base/thread_annotations.h>
Ady Abraham23ea9da2021-07-14 16:32:56 -070025#include <binder/IBinder.h>
Dominik Laskowski08d05c22020-07-22 00:05:08 -070026#include <utils/Timers.h>
Dan Stoza2713c302018-03-28 17:07:36 -070027
Dominik Laskowski1c99a002023-01-20 17:10:36 -050028#include <scheduler/TransactionSchedule.h>
Dominik Laskowski933f8de2023-01-20 13:15:51 -050029#include <scheduler/VsyncConfig.h>
30
Angel Aguayob398ee22021-10-14 00:39:15 +000031#include "../WpHash.h"
32
Dominik Laskowskieddeda12019-07-19 11:54:13 -070033namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070034
Dominik Laskowski08d05c22020-07-22 00:05:08 -070035// Modulates VSYNC phase depending on transaction schedule and refresh rate changes.
Ady Abraham23ea9da2021-07-14 16:32:56 -070036class VsyncModulator : public IBinder::DeathRecipient {
Dan Stoza2713c302018-03-28 17:07:36 -070037public:
Dominik Laskowski08d05c22020-07-22 00:05:08 -070038 // 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 Abraham8287e852020-08-12 14:44:58 -070048 using VsyncConfigOpt = std::optional<VsyncConfig>;
Dominik Laskowski08d05c22020-07-22 00:05:08 -070049
Dominik Laskowski08d05c22020-07-22 00:05:08 -070050 using Clock = std::chrono::steady_clock;
51 using TimePoint = Clock::time_point;
52 using Now = TimePoint (*)();
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020053
Ady Abraham8287e852020-08-12 14:44:58 -070054 explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now);
Ana Krulec12096d02018-09-07 14:54:14 -070055
Dominik Laskowski14956dc2023-02-22 13:43:57 -050056 bool isVsyncConfigEarly() const EXCLUDES(mMutex);
57
Ady Abraham8287e852020-08-12 14:44:58 -070058 VsyncConfig getVsyncConfig() const EXCLUDES(mMutex);
Jorim Jaggi90535212018-05-23 23:44:06 +020059
Dominik Laskowski14956dc2023-02-22 13:43:57 -050060 void cancelRefreshRateChange() { mRefreshRateChangePending = false; }
61
Ady Abraham8287e852020-08-12 14:44:58 -070062 [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -070063
64 // Changes offsets in response to transaction flags or commit.
Ady Abraham23ea9da2021-07-14 16:32:56 -070065 [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule,
66 const sp<IBinder>& = {}) EXCLUDES(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -070067 [[nodiscard]] VsyncConfigOpt onTransactionCommit();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010068
Alec Mouri754c98a2019-03-18 18:53:42 -070069 // Called when we send a refresh rate change to hardware composer, so that
70 // we can move into early offsets.
Ady Abraham8287e852020-08-12 14:44:58 -070071 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070072
Dominik Laskowski08d05c22020-07-22 00:05:08 -070073 // Called when we detect from VSYNC signals that the refresh rate changed.
Alec Mouri754c98a2019-03-18 18:53:42 -070074 // This way we can move out of early offsets if no longer necessary.
Ady Abraham8287e852020-08-12 14:44:58 -070075 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070076
Ady Abraham8287e852020-08-12 14:44:58 -070077 [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition);
Ady Abrahambe0f9482019-04-24 15:41:53 -070078
Ady Abraham23ea9da2021-07-14 16:32:56 -070079protected:
80 // Called from unit tests as well
81 void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex);
82
Dan Stoza2713c302018-03-28 17:07:36 -070083private:
Ady Abraham2739e832022-02-14 17:42:00 -080084 enum class VsyncConfigType { Early, EarlyGpu, Late };
85
86 VsyncConfigType getNextVsyncConfigType() const REQUIRES(mMutex);
Ady Abraham8287e852020-08-12 14:44:58 -070087 const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex);
88 [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex);
89 [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex);
Dan Stoza2713c302018-03-28 17:07:36 -070090
Alec Mourid7599d82019-05-22 19:58:00 -070091 mutable std::mutex mMutex;
Ady Abraham8287e852020-08-12 14:44:58 -070092 VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020093
Ady Abraham8287e852020-08-12 14:44:58 -070094 VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020095
Dominik Laskowski08d05c22020-07-22 00:05:08 -070096 using Schedule = TransactionSchedule;
97 std::atomic<Schedule> mTransactionSchedule = Schedule::Late;
Alec Mourid7599d82019-05-22 19:58:00 -070098
Ady Abraham23ea9da2021-07-14 16:32:56 -070099 std::unordered_set<wp<IBinder>, WpHash> mEarlyWakeupRequests GUARDED_BY(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700100 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;
Dan Stoza2713c302018-03-28 17:07:36 -0700109};
110
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700111} // namespace android::scheduler