blob: 704a5d5626974b06664e8b93c88f0e74e9afc02e [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
Dan Stoza2713c302018-03-28 17:07:36 -070019#include <mutex>
20
Ana Krulec757f63a2019-01-25 10:46:18 -080021#include "Scheduler.h"
Dan Stoza2713c302018-03-28 17:07:36 -070022
Dominik Laskowskieddeda12019-07-19 11:54:13 -070023namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070024
25/*
26 * Modulates the vsync-offsets depending on current SurfaceFlinger state.
27 */
28class VSyncModulator {
Jorim Jaggi90535212018-05-23 23:44:06 +020029private:
Alec Mouri754c98a2019-03-18 18:53:42 -070030 // Number of frames we'll keep the early phase offsets once they are activated for a
31 // transaction. This acts as a low-pass filter in case the client isn't quick enough in
32 // sending new transactions.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070033 static constexpr int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2;
Jorim Jaggi90535212018-05-23 23:44:06 +020034
Alec Mouri5a102102019-05-23 07:14:20 -070035 // Number of frames we'll keep the early gl phase offsets once they are activated.
36 // This acts as a low-pass filter to avoid scenarios where we rapidly
37 // switch in and out of gl composition.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070038 static constexpr int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2;
39
Dan Stoza2713c302018-03-28 17:07:36 -070040public:
Alec Mourib488afa2019-05-23 10:22:24 -070041 // Wrapper for a collection of surfaceflinger/app offsets for a particular
Dominik Laskowskieddeda12019-07-19 11:54:13 -070042 // configuration.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020043 struct Offsets {
44 nsecs_t sf;
45 nsecs_t app;
Ady Abraham9e16a482019-12-03 17:19:41 -080046
47 bool operator==(const Offsets& other) const { return sf == other.sf && app == other.app; }
48
49 bool operator!=(const Offsets& other) const { return !(*this == other); }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020050 };
51
Dominik Laskowskieddeda12019-07-19 11:54:13 -070052 struct OffsetsConfig {
53 Offsets early; // For transactions with the eEarlyWakeup flag.
54 Offsets earlyGl; // As above but while compositing with GL.
55 Offsets late; // Default.
56
Ady Abraham9e16a482019-12-03 17:19:41 -080057 bool operator==(const OffsetsConfig& other) const {
58 return early == other.early && earlyGl == other.earlyGl && late == other.late;
59 }
60
61 bool operator!=(const OffsetsConfig& other) const { return !(*this == other); }
Alec Mourid7599d82019-05-22 19:58:00 -070062 };
63
Dominik Laskowski98041832019-08-01 18:35:59 -070064 VSyncModulator(Scheduler&, ConnectionHandle appConnectionHandle,
65 ConnectionHandle sfConnectionHandle, const OffsetsConfig&);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020066
Dominik Laskowskieddeda12019-07-19 11:54:13 -070067 void setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex);
Ana Krulec12096d02018-09-07 14:54:14 -070068
Alec Mourib488afa2019-05-23 10:22:24 -070069 // Signals that a transaction has started, and changes offsets accordingly.
70 void setTransactionStart(Scheduler::TransactionStart transactionStart);
Jorim Jaggi90535212018-05-23 23:44:06 +020071
Alec Mourib488afa2019-05-23 10:22:24 -070072 // Signals that a transaction has been completed, so that we can finish
73 // special handling for a transaction.
74 void onTransactionHandled();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010075
Alec Mouri754c98a2019-03-18 18:53:42 -070076 // Called when we send a refresh rate change to hardware composer, so that
77 // we can move into early offsets.
Alec Mourib488afa2019-05-23 10:22:24 -070078 void onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070079
80 // Called when we detect from vsync signals that the refresh rate changed.
81 // This way we can move out of early offsets if no longer necessary.
Alec Mourib488afa2019-05-23 10:22:24 -070082 void onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070083
Alec Mourib488afa2019-05-23 10:22:24 -070084 // Called when the display is presenting a new frame. usedRenderEngine
85 // should be set to true if RenderEngine was involved with composing the new
86 // frame.
87 void onRefreshed(bool usedRenderEngine);
Alec Mouri5a102102019-05-23 07:14:20 -070088
Alec Mourid7599d82019-05-22 19:58:00 -070089 // Returns the offsets that we are currently using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070090 Offsets getOffsets() const EXCLUDES(mMutex);
Ady Abrahambe0f9482019-04-24 15:41:53 -070091
Dan Stoza2713c302018-03-28 17:07:36 -070092private:
Alec Mourid7599d82019-05-22 19:58:00 -070093 // Returns the next offsets that we should be using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070094 const Offsets& getNextOffsets() const REQUIRES(mMutex);
Alec Mourid7599d82019-05-22 19:58:00 -070095 // Updates offsets and persists them into the scheduler framework.
96 void updateOffsets() EXCLUDES(mMutex);
97 void updateOffsetsLocked() REQUIRES(mMutex);
Dominik Laskowskieddeda12019-07-19 11:54:13 -070098
99 Scheduler& mScheduler;
Dominik Laskowski98041832019-08-01 18:35:59 -0700100 const ConnectionHandle mAppConnectionHandle;
101 const ConnectionHandle mSfConnectionHandle;
Dan Stoza2713c302018-03-28 17:07:36 -0700102
Alec Mourid7599d82019-05-22 19:58:00 -0700103 mutable std::mutex mMutex;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700104 OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200105
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700106 Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200107
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700108 std::atomic<Scheduler::TransactionStart> mTransactionStart =
109 Scheduler::TransactionStart::NORMAL;
Alec Mouri754c98a2019-03-18 18:53:42 -0700110 std::atomic<bool> mRefreshRateChangePending = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200111 std::atomic<int> mRemainingEarlyFrameCount = 0;
Alec Mouri5a102102019-05-23 07:14:20 -0700112 std::atomic<int> mRemainingRenderEngineUsageCount = 0;
Alec Mourid7599d82019-05-22 19:58:00 -0700113
114 bool mTraceDetailedInfo = false;
Dan Stoza2713c302018-03-28 17:07:36 -0700115};
116
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700117} // namespace android::scheduler