blob: ab678c98c5b6a2544c498861e787ec4c85f2bf30 [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>
21
Ana Krulec757f63a2019-01-25 10:46:18 -080022#include "Scheduler.h"
Dan Stoza2713c302018-03-28 17:07:36 -070023
Dominik Laskowskieddeda12019-07-19 11:54:13 -070024namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070025
26/*
27 * Modulates the vsync-offsets depending on current SurfaceFlinger state.
28 */
29class VSyncModulator {
Jorim Jaggi90535212018-05-23 23:44:06 +020030private:
Alec Mouri754c98a2019-03-18 18:53:42 -070031 // Number of frames we'll keep the early phase offsets once they are activated for a
32 // transaction. This acts as a low-pass filter in case the client isn't quick enough in
33 // sending new transactions.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070034 static constexpr int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2;
Jorim Jaggi90535212018-05-23 23:44:06 +020035
Alec Mouri5a102102019-05-23 07:14:20 -070036 // Number of frames we'll keep the early gl phase offsets once they are activated.
37 // This acts as a low-pass filter to avoid scenarios where we rapidly
38 // switch in and out of gl composition.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070039 static constexpr int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2;
40
Ady Abrahambf1349c2020-06-12 14:26:18 -070041 // Margin used to account for potential data races
42 static const constexpr std::chrono::nanoseconds MARGIN_FOR_TX_APPLY = 1ms;
43
Dan Stoza2713c302018-03-28 17:07:36 -070044public:
Alec Mourib488afa2019-05-23 10:22:24 -070045 // Wrapper for a collection of surfaceflinger/app offsets for a particular
Dominik Laskowskieddeda12019-07-19 11:54:13 -070046 // configuration.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020047 struct Offsets {
48 nsecs_t sf;
49 nsecs_t app;
Ady Abraham9e16a482019-12-03 17:19:41 -080050
51 bool operator==(const Offsets& other) const { return sf == other.sf && app == other.app; }
52
53 bool operator!=(const Offsets& other) const { return !(*this == other); }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020054 };
55
Dominik Laskowskieddeda12019-07-19 11:54:13 -070056 struct OffsetsConfig {
57 Offsets early; // For transactions with the eEarlyWakeup flag.
58 Offsets earlyGl; // As above but while compositing with GL.
59 Offsets late; // Default.
60
Ady Abraham9e16a482019-12-03 17:19:41 -080061 bool operator==(const OffsetsConfig& other) const {
62 return early == other.early && earlyGl == other.earlyGl && late == other.late;
63 }
64
65 bool operator!=(const OffsetsConfig& other) const { return !(*this == other); }
Alec Mourid7599d82019-05-22 19:58:00 -070066 };
67
Ady Abrahambf1349c2020-06-12 14:26:18 -070068 VSyncModulator(IPhaseOffsetControl&, ConnectionHandle appConnectionHandle,
Dominik Laskowski98041832019-08-01 18:35:59 -070069 ConnectionHandle sfConnectionHandle, const OffsetsConfig&);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020070
Dominik Laskowskieddeda12019-07-19 11:54:13 -070071 void setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex);
Ana Krulec12096d02018-09-07 14:54:14 -070072
Alec Mourib488afa2019-05-23 10:22:24 -070073 // Signals that a transaction has started, and changes offsets accordingly.
74 void setTransactionStart(Scheduler::TransactionStart transactionStart);
Jorim Jaggi90535212018-05-23 23:44:06 +020075
Alec Mourib488afa2019-05-23 10:22:24 -070076 // Signals that a transaction has been completed, so that we can finish
77 // special handling for a transaction.
78 void onTransactionHandled();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010079
Alec Mouri754c98a2019-03-18 18:53:42 -070080 // Called when we send a refresh rate change to hardware composer, so that
81 // we can move into early offsets.
Alec Mourib488afa2019-05-23 10:22:24 -070082 void onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070083
84 // Called when we detect from vsync signals that the refresh rate changed.
85 // This way we can move out of early offsets if no longer necessary.
Alec Mourib488afa2019-05-23 10:22:24 -070086 void onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070087
Alec Mourib488afa2019-05-23 10:22:24 -070088 // Called when the display is presenting a new frame. usedRenderEngine
89 // should be set to true if RenderEngine was involved with composing the new
90 // frame.
91 void onRefreshed(bool usedRenderEngine);
Alec Mouri5a102102019-05-23 07:14:20 -070092
Alec Mourid7599d82019-05-22 19:58:00 -070093 // Returns the offsets that we are currently using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070094 Offsets getOffsets() const EXCLUDES(mMutex);
Ady Abrahambe0f9482019-04-24 15:41:53 -070095
Dan Stoza2713c302018-03-28 17:07:36 -070096private:
Ady Abrahambf1349c2020-06-12 14:26:18 -070097 friend class VSyncModulatorTest;
Alec Mourid7599d82019-05-22 19:58:00 -070098 // Returns the next offsets that we should be using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070099 const Offsets& getNextOffsets() const REQUIRES(mMutex);
Alec Mourid7599d82019-05-22 19:58:00 -0700100 // Updates offsets and persists them into the scheduler framework.
101 void updateOffsets() EXCLUDES(mMutex);
102 void updateOffsetsLocked() REQUIRES(mMutex);
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700103
Ady Abrahambf1349c2020-06-12 14:26:18 -0700104 IPhaseOffsetControl& mPhaseOffsetControl;
Dominik Laskowski98041832019-08-01 18:35:59 -0700105 const ConnectionHandle mAppConnectionHandle;
106 const ConnectionHandle mSfConnectionHandle;
Dan Stoza2713c302018-03-28 17:07:36 -0700107
Alec Mourid7599d82019-05-22 19:58:00 -0700108 mutable std::mutex mMutex;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700109 OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200110
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700111 Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200112
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700113 std::atomic<Scheduler::TransactionStart> mTransactionStart =
Ady Abrahambf1349c2020-06-12 14:26:18 -0700114 Scheduler::TransactionStart::Normal;
Alec Mouri754c98a2019-03-18 18:53:42 -0700115 std::atomic<bool> mRefreshRateChangePending = false;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700116 std::atomic<bool> mExplicitEarlyWakeup = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200117 std::atomic<int> mRemainingEarlyFrameCount = 0;
Alec Mouri5a102102019-05-23 07:14:20 -0700118 std::atomic<int> mRemainingRenderEngineUsageCount = 0;
Alec Mourif792cfe2020-06-05 13:11:23 -0700119 std::atomic<std::chrono::steady_clock::time_point> mEarlyTxnStartTime = {};
120 std::atomic<std::chrono::steady_clock::time_point> mTxnAppliedTime = {};
Alec Mourid7599d82019-05-22 19:58:00 -0700121
122 bool mTraceDetailedInfo = false;
Dan Stoza2713c302018-03-28 17:07:36 -0700123};
124
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700125} // namespace android::scheduler