blob: 81a7864cdb7a91cd700910b7bdc3c14fb8e6a000 [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
19#include <utils/Errors.h>
20
Ana Krulec757f63a2019-01-25 10:46:18 -080021#include <cinttypes>
Dan Stoza2713c302018-03-28 17:07:36 -070022#include <mutex>
23
Ana Krulec757f63a2019-01-25 10:46:18 -080024#include "Scheduler.h"
Dan Stoza2713c302018-03-28 17:07:36 -070025
26namespace android {
27
28/*
29 * Modulates the vsync-offsets depending on current SurfaceFlinger state.
30 */
31class VSyncModulator {
Jorim Jaggi90535212018-05-23 23:44:06 +020032private:
Alec Mouri754c98a2019-03-18 18:53:42 -070033 // Number of frames we'll keep the early phase offsets once they are activated for a
34 // transaction. This acts as a low-pass filter in case the client isn't quick enough in
35 // sending new transactions.
36 const int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2;
Jorim Jaggi90535212018-05-23 23:44:06 +020037
Dan Stoza2713c302018-03-28 17:07:36 -070038public:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020039 struct Offsets {
40 nsecs_t sf;
41 nsecs_t app;
42 };
43
Dan Stoza2713c302018-03-28 17:07:36 -070044 // Sets the phase offsets
45 //
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020046 // sfEarly: The phase offset when waking up SF early, which happens when marking a transaction
47 // as early. May be the same as late, in which case we don't shift offsets.
48 // sfEarlyGl: Like sfEarly, but only if we used GL composition. If we use both GL composition
49 // and the transaction was marked as early, we'll use sfEarly.
50 // sfLate: The regular SF vsync phase offset.
51 // appEarly: Like sfEarly, but for the app-vsync
52 // appEarlyGl: Like sfEarlyGl, but for the app-vsync.
53 // appLate: The regular app vsync phase offset.
54 void setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late) {
55 mEarlyOffsets = early;
56 mEarlyGlOffsets = earlyGl;
57 mLateOffsets = late;
Ady Abrahamf6656352019-02-14 16:29:49 -080058
59 if (mSfConnectionHandle && late.sf != mOffsets.load().sf) {
60 mScheduler->setPhaseOffset(mSfConnectionHandle, late.sf);
61 }
62
63 if (mAppConnectionHandle && late.app != mOffsets.load().app) {
64 mScheduler->setPhaseOffset(mAppConnectionHandle, late.app);
65 }
66
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020067 mOffsets = late;
Dan Stoza2713c302018-03-28 17:07:36 -070068 }
69
Ana Krulecfefcb582018-08-07 14:22:37 -070070 Offsets getEarlyOffsets() const { return mEarlyOffsets; }
Dan Stoza2713c302018-03-28 17:07:36 -070071
Ana Krulecfefcb582018-08-07 14:22:37 -070072 Offsets getEarlyGlOffsets() const { return mEarlyGlOffsets; }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020073
74 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
75 mSfEventThread = sfEventThread;
76 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070077 }
78
Ana Krulec12096d02018-09-07 14:54:14 -070079 void setSchedulerAndHandles(Scheduler* scheduler,
80 Scheduler::ConnectionHandle* appConnectionHandle,
81 Scheduler::ConnectionHandle* sfConnectionHandle) {
82 mScheduler = scheduler;
83 mAppConnectionHandle = appConnectionHandle;
84 mSfConnectionHandle = sfConnectionHandle;
85 }
86
Ana Krulec7ecce8c2018-10-12 13:44:41 -070087 void setTransactionStart(Scheduler::TransactionStart transactionStart) {
88 if (transactionStart == Scheduler::TransactionStart::EARLY) {
Alec Mouri754c98a2019-03-18 18:53:42 -070089 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT_TRANSACTION;
Jorim Jaggi90535212018-05-23 23:44:06 +020090 }
91
Jorim Jaggif15c3be2018-04-12 12:56:58 +010092 // An early transaction stays an early transaction.
Ana Krulec7ecce8c2018-10-12 13:44:41 -070093 if (transactionStart == mTransactionStart ||
94 mTransactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggif15c3be2018-04-12 12:56:58 +010095 return;
96 }
Dan Stoza2713c302018-03-28 17:07:36 -070097 mTransactionStart = transactionStart;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020098 updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -070099 }
100
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100101 void onTransactionHandled() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700102 if (mTransactionStart == Scheduler::TransactionStart::NORMAL) return;
103 mTransactionStart = Scheduler::TransactionStart::NORMAL;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200104 updateOffsets();
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100105 }
106
Alec Mouri754c98a2019-03-18 18:53:42 -0700107 // Called when we send a refresh rate change to hardware composer, so that
108 // we can move into early offsets.
109 void onRefreshRateChangeInitiated() {
110 if (mRefreshRateChangePending) {
111 return;
112 }
113 mRefreshRateChangePending = true;
114 updateOffsets();
115 }
116
117 // Called when we detect from vsync signals that the refresh rate changed.
118 // This way we can move out of early offsets if no longer necessary.
119 void onRefreshRateChangeDetected() {
120 if (!mRefreshRateChangePending) {
121 return;
122 }
123 mRefreshRateChangePending = false;
124 updateOffsets();
125 }
126
Jorim Jaggi90535212018-05-23 23:44:06 +0200127 void onRefreshed(bool usedRenderEngine) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200128 bool updateOffsetsNeeded = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200129 if (mRemainingEarlyFrameCount > 0) {
130 mRemainingEarlyFrameCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200131 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200132 }
133 if (usedRenderEngine != mLastFrameUsedRenderEngine) {
134 mLastFrameUsedRenderEngine = usedRenderEngine;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200135 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200136 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200137 if (updateOffsetsNeeded) {
138 updateOffsets();
Jorim Jaggi90535212018-05-23 23:44:06 +0200139 }
Dan Stoza2713c302018-03-28 17:07:36 -0700140 }
141
Ady Abrahambe0f9482019-04-24 15:41:53 -0700142 Offsets getOffsets() {
143 // Early offsets are used if we're in the middle of a refresh rate
144 // change, or if we recently begin a transaction.
145 if (mTransactionStart == Scheduler::TransactionStart::EARLY ||
146 mRemainingEarlyFrameCount > 0 || mRefreshRateChangePending) {
147 return mEarlyOffsets;
148 } else if (mLastFrameUsedRenderEngine) {
149 return mEarlyGlOffsets;
150 } else {
151 return mLateOffsets;
152 }
153 }
154
Dan Stoza2713c302018-03-28 17:07:36 -0700155private:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200156 void updateOffsets() {
157 const Offsets desired = getOffsets();
158 const Offsets current = mOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -0700159
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200160 bool changed = false;
161 if (desired.sf != current.sf) {
Ana Krulec12096d02018-09-07 14:54:14 -0700162 if (mSfConnectionHandle != nullptr) {
163 mScheduler->setPhaseOffset(mSfConnectionHandle, desired.sf);
164 } else {
165 mSfEventThread->setPhaseOffset(desired.sf);
166 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200167 changed = true;
168 }
169 if (desired.app != current.app) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800170 if (mAppConnectionHandle != nullptr) {
Ana Krulec12096d02018-09-07 14:54:14 -0700171 mScheduler->setPhaseOffset(mAppConnectionHandle, desired.app);
172 } else {
173 mAppEventThread->setPhaseOffset(desired.app);
174 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200175 changed = true;
176 }
Dan Stoza2713c302018-03-28 17:07:36 -0700177
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200178 if (changed) {
179 mOffsets = desired;
Dan Stoza2713c302018-03-28 17:07:36 -0700180 }
181 }
182
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200183 Offsets mLateOffsets;
184 Offsets mEarlyOffsets;
185 Offsets mEarlyGlOffsets;
186
187 EventThread* mSfEventThread = nullptr;
188 EventThread* mAppEventThread = nullptr;
189
Ana Krulec12096d02018-09-07 14:54:14 -0700190 Scheduler* mScheduler = nullptr;
191 Scheduler::ConnectionHandle* mAppConnectionHandle = nullptr;
192 Scheduler::ConnectionHandle* mSfConnectionHandle = nullptr;
193
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200194 std::atomic<Offsets> mOffsets;
195
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700196 std::atomic<Scheduler::TransactionStart> mTransactionStart =
197 Scheduler::TransactionStart::NORMAL;
Dan Stoza2713c302018-03-28 17:07:36 -0700198 std::atomic<bool> mLastFrameUsedRenderEngine = false;
Alec Mouri754c98a2019-03-18 18:53:42 -0700199 std::atomic<bool> mRefreshRateChangePending = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200200 std::atomic<int> mRemainingEarlyFrameCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700201};
202
203} // namespace android