blob: 355a14adf833e343b63196be7b8b5ca18f4d40a2 [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>
Dan Stoza2713c302018-03-28 17:07:36 -070022
Dominik Laskowski08d05c22020-07-22 00:05:08 -070023#include <android-base/thread_annotations.h>
24#include <utils/Timers.h>
Dan Stoza2713c302018-03-28 17:07:36 -070025
Dominik Laskowskieddeda12019-07-19 11:54:13 -070026namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070027
Dominik Laskowski08d05c22020-07-22 00:05:08 -070028// State machine controlled by transaction flags. VsyncModulator switches to early phase offsets
29// when a transaction is flagged EarlyStart or Early, lasting until an EarlyEnd transaction or a
30// fixed number of frames, respectively.
31enum class TransactionSchedule {
32 Late, // Default.
33 Early, // Deprecated.
34 EarlyStart,
35 EarlyEnd
36};
Jorim Jaggi90535212018-05-23 23:44:06 +020037
Dominik Laskowski08d05c22020-07-22 00:05:08 -070038// Modulates VSYNC phase depending on transaction schedule and refresh rate changes.
39class VsyncModulator {
Dan Stoza2713c302018-03-28 17:07:36 -070040public:
Dominik Laskowski08d05c22020-07-22 00:05:08 -070041 // Number of frames to keep early offsets after an early transaction or GPU composition.
42 // This acts as a low-pass filter in case subsequent transactions are delayed, or if the
43 // composition strategy alternates on subsequent frames.
44 static constexpr int MIN_EARLY_TRANSACTION_FRAMES = 2;
45 static constexpr int MIN_EARLY_GPU_FRAMES = 2;
46
47 // Duration to delay the MIN_EARLY_TRANSACTION_FRAMES countdown after an early transaction.
48 // This may keep early offsets for an extra frame, but avoids a race with transaction commit.
49 static const std::chrono::nanoseconds MIN_EARLY_TRANSACTION_TIME;
50
Ady Abraham8287e852020-08-12 14:44:58 -070051 // Phase offsets and work durations for SF and app deadlines from VSYNC.
52 struct VsyncConfig {
53 nsecs_t sfOffset;
54 nsecs_t appOffset;
55 std::chrono::nanoseconds sfWorkDuration;
56 std::chrono::nanoseconds appWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -080057
Ady Abraham8287e852020-08-12 14:44:58 -070058 bool operator==(const VsyncConfig& other) const {
59 return sfOffset == other.sfOffset && appOffset == other.appOffset &&
60 sfWorkDuration == other.sfWorkDuration &&
61 appWorkDuration == other.appWorkDuration;
62 }
63
64 bool operator!=(const VsyncConfig& other) const { return !(*this == other); }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020065 };
66
Ady Abraham8287e852020-08-12 14:44:58 -070067 using VsyncConfigOpt = std::optional<VsyncConfig>;
Dominik Laskowski08d05c22020-07-22 00:05:08 -070068
Ady Abraham8287e852020-08-12 14:44:58 -070069 struct VsyncConfigSet {
70 VsyncConfig early; // Used for early transactions, and during refresh rate change.
71 VsyncConfig earlyGpu; // Used during GPU composition.
72 VsyncConfig late; // Default.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070073
Ady Abraham8287e852020-08-12 14:44:58 -070074 bool operator==(const VsyncConfigSet& other) const {
Dominik Laskowski08d05c22020-07-22 00:05:08 -070075 return early == other.early && earlyGpu == other.earlyGpu && late == other.late;
Ady Abraham9e16a482019-12-03 17:19:41 -080076 }
77
Ady Abraham8287e852020-08-12 14:44:58 -070078 bool operator!=(const VsyncConfigSet& other) const { return !(*this == other); }
Alec Mourid7599d82019-05-22 19:58:00 -070079 };
80
Dominik Laskowski08d05c22020-07-22 00:05:08 -070081 using Clock = std::chrono::steady_clock;
82 using TimePoint = Clock::time_point;
83 using Now = TimePoint (*)();
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020084
Ady Abraham8287e852020-08-12 14:44:58 -070085 explicit VsyncModulator(const VsyncConfigSet&, Now = Clock::now);
Ana Krulec12096d02018-09-07 14:54:14 -070086
Ady Abraham8287e852020-08-12 14:44:58 -070087 VsyncConfig getVsyncConfig() const EXCLUDES(mMutex);
Jorim Jaggi90535212018-05-23 23:44:06 +020088
Ady Abraham8287e852020-08-12 14:44:58 -070089 [[nodiscard]] VsyncConfig setVsyncConfigSet(const VsyncConfigSet&) EXCLUDES(mMutex);
Dominik Laskowski08d05c22020-07-22 00:05:08 -070090
91 // Changes offsets in response to transaction flags or commit.
Ady Abraham8287e852020-08-12 14:44:58 -070092 [[nodiscard]] VsyncConfigOpt setTransactionSchedule(TransactionSchedule);
93 [[nodiscard]] VsyncConfigOpt onTransactionCommit();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010094
Alec Mouri754c98a2019-03-18 18:53:42 -070095 // Called when we send a refresh rate change to hardware composer, so that
96 // we can move into early offsets.
Ady Abraham8287e852020-08-12 14:44:58 -070097 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070098
Dominik Laskowski08d05c22020-07-22 00:05:08 -070099 // Called when we detect from VSYNC signals that the refresh rate changed.
Alec Mouri754c98a2019-03-18 18:53:42 -0700100 // This way we can move out of early offsets if no longer necessary.
Ady Abraham8287e852020-08-12 14:44:58 -0700101 [[nodiscard]] VsyncConfigOpt onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -0700102
Ady Abraham8287e852020-08-12 14:44:58 -0700103 [[nodiscard]] VsyncConfigOpt onDisplayRefresh(bool usedGpuComposition);
Ady Abrahambe0f9482019-04-24 15:41:53 -0700104
Dan Stoza2713c302018-03-28 17:07:36 -0700105private:
Ady Abraham8287e852020-08-12 14:44:58 -0700106 const VsyncConfig& getNextVsyncConfig() const REQUIRES(mMutex);
107 [[nodiscard]] VsyncConfig updateVsyncConfig() EXCLUDES(mMutex);
108 [[nodiscard]] VsyncConfig updateVsyncConfigLocked() REQUIRES(mMutex);
Dan Stoza2713c302018-03-28 17:07:36 -0700109
Alec Mourid7599d82019-05-22 19:58:00 -0700110 mutable std::mutex mMutex;
Ady Abraham8287e852020-08-12 14:44:58 -0700111 VsyncConfigSet mVsyncConfigSet GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200112
Ady Abraham8287e852020-08-12 14:44:58 -0700113 VsyncConfig mVsyncConfig GUARDED_BY(mMutex){mVsyncConfigSet.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200114
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700115 using Schedule = TransactionSchedule;
116 std::atomic<Schedule> mTransactionSchedule = Schedule::Late;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700117 std::atomic<bool> mExplicitEarlyWakeup = false;
Alec Mourid7599d82019-05-22 19:58:00 -0700118
Dominik Laskowski08d05c22020-07-22 00:05:08 -0700119 std::atomic<bool> mRefreshRateChangePending = false;
120
121 std::atomic<int> mEarlyTransactionFrames = 0;
122 std::atomic<int> mEarlyGpuFrames = 0;
123 std::atomic<TimePoint> mEarlyTransactionStartTime = TimePoint();
124 std::atomic<TimePoint> mLastTransactionCommitTime = TimePoint();
125
126 const Now mNow;
127 const bool mTraceDetailedInfo;
Dan Stoza2713c302018-03-28 17:07:36 -0700128};
129
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700130} // namespace android::scheduler