blob: 73a1fb9816dc660446ca55fcae5c1ff61384a400 [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
Alec Mouri5a102102019-05-23 07:14:20 -070038 // Number of frames we'll keep the early gl phase offsets once they are activated.
39 // This acts as a low-pass filter to avoid scenarios where we rapidly
40 // switch in and out of gl composition.
41 const int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2;
42
Dan Stoza2713c302018-03-28 17:07:36 -070043public:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020044 struct Offsets {
45 nsecs_t sf;
46 nsecs_t app;
47 };
48
Dan Stoza2713c302018-03-28 17:07:36 -070049 // Sets the phase offsets
50 //
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020051 // sfEarly: The phase offset when waking up SF early, which happens when marking a transaction
52 // as early. May be the same as late, in which case we don't shift offsets.
53 // sfEarlyGl: Like sfEarly, but only if we used GL composition. If we use both GL composition
54 // and the transaction was marked as early, we'll use sfEarly.
55 // sfLate: The regular SF vsync phase offset.
56 // appEarly: Like sfEarly, but for the app-vsync
57 // appEarlyGl: Like sfEarlyGl, but for the app-vsync.
58 // appLate: The regular app vsync phase offset.
Ady Abraham45e4e362019-06-07 18:20:51 -070059 void setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late,
60 nsecs_t thresholdForNextVsync) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020061 mEarlyOffsets = early;
62 mEarlyGlOffsets = earlyGl;
63 mLateOffsets = late;
Ady Abraham45e4e362019-06-07 18:20:51 -070064 mThresholdForNextVsync = thresholdForNextVsync;
Ady Abrahamf6656352019-02-14 16:29:49 -080065
66 if (mSfConnectionHandle && late.sf != mOffsets.load().sf) {
67 mScheduler->setPhaseOffset(mSfConnectionHandle, late.sf);
68 }
69
70 if (mAppConnectionHandle && late.app != mOffsets.load().app) {
71 mScheduler->setPhaseOffset(mAppConnectionHandle, late.app);
72 }
73
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020074 mOffsets = late;
Dan Stoza2713c302018-03-28 17:07:36 -070075 }
76
Ana Krulecfefcb582018-08-07 14:22:37 -070077 Offsets getEarlyOffsets() const { return mEarlyOffsets; }
Dan Stoza2713c302018-03-28 17:07:36 -070078
Ana Krulecfefcb582018-08-07 14:22:37 -070079 Offsets getEarlyGlOffsets() const { return mEarlyGlOffsets; }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020080
81 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
82 mSfEventThread = sfEventThread;
83 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070084 }
85
Ana Krulec12096d02018-09-07 14:54:14 -070086 void setSchedulerAndHandles(Scheduler* scheduler,
87 Scheduler::ConnectionHandle* appConnectionHandle,
88 Scheduler::ConnectionHandle* sfConnectionHandle) {
89 mScheduler = scheduler;
90 mAppConnectionHandle = appConnectionHandle;
91 mSfConnectionHandle = sfConnectionHandle;
92 }
93
Ana Krulec7ecce8c2018-10-12 13:44:41 -070094 void setTransactionStart(Scheduler::TransactionStart transactionStart) {
95 if (transactionStart == Scheduler::TransactionStart::EARLY) {
Alec Mouri754c98a2019-03-18 18:53:42 -070096 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT_TRANSACTION;
Jorim Jaggi90535212018-05-23 23:44:06 +020097 }
98
Jorim Jaggif15c3be2018-04-12 12:56:58 +010099 // An early transaction stays an early transaction.
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700100 if (transactionStart == mTransactionStart ||
101 mTransactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100102 return;
103 }
Dan Stoza2713c302018-03-28 17:07:36 -0700104 mTransactionStart = transactionStart;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200105 updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -0700106 }
107
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100108 void onTransactionHandled() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700109 if (mTransactionStart == Scheduler::TransactionStart::NORMAL) return;
110 mTransactionStart = Scheduler::TransactionStart::NORMAL;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200111 updateOffsets();
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100112 }
113
Alec Mouri754c98a2019-03-18 18:53:42 -0700114 // Called when we send a refresh rate change to hardware composer, so that
115 // we can move into early offsets.
116 void onRefreshRateChangeInitiated() {
117 if (mRefreshRateChangePending) {
118 return;
119 }
120 mRefreshRateChangePending = true;
121 updateOffsets();
122 }
123
124 // Called when we detect from vsync signals that the refresh rate changed.
125 // This way we can move out of early offsets if no longer necessary.
Alec Mourif8e689c2019-05-20 18:32:22 -0700126 void onRefreshRateChangeCompleted() {
Alec Mouri754c98a2019-03-18 18:53:42 -0700127 if (!mRefreshRateChangePending) {
128 return;
129 }
130 mRefreshRateChangePending = false;
131 updateOffsets();
132 }
133
Jorim Jaggi90535212018-05-23 23:44:06 +0200134 void onRefreshed(bool usedRenderEngine) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200135 bool updateOffsetsNeeded = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200136 if (mRemainingEarlyFrameCount > 0) {
137 mRemainingEarlyFrameCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200138 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200139 }
Alec Mouri5a102102019-05-23 07:14:20 -0700140 if (usedRenderEngine) {
141 mRemainingRenderEngineUsageCount = MIN_EARLY_GL_FRAME_COUNT_TRANSACTION;
142 updateOffsetsNeeded = true;
143 } else if (mRemainingRenderEngineUsageCount > 0) {
144 mRemainingRenderEngineUsageCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200145 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200146 }
Alec Mouri5a102102019-05-23 07:14:20 -0700147
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200148 if (updateOffsetsNeeded) {
149 updateOffsets();
Jorim Jaggi90535212018-05-23 23:44:06 +0200150 }
Dan Stoza2713c302018-03-28 17:07:36 -0700151 }
152
Ady Abrahambe0f9482019-04-24 15:41:53 -0700153 Offsets getOffsets() {
154 // Early offsets are used if we're in the middle of a refresh rate
155 // change, or if we recently begin a transaction.
156 if (mTransactionStart == Scheduler::TransactionStart::EARLY ||
157 mRemainingEarlyFrameCount > 0 || mRefreshRateChangePending) {
158 return mEarlyOffsets;
Alec Mouri5a102102019-05-23 07:14:20 -0700159 } else if (mRemainingRenderEngineUsageCount > 0) {
Ady Abrahambe0f9482019-04-24 15:41:53 -0700160 return mEarlyGlOffsets;
161 } else {
162 return mLateOffsets;
163 }
164 }
165
Dan Stoza2713c302018-03-28 17:07:36 -0700166private:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200167 void updateOffsets() {
168 const Offsets desired = getOffsets();
169 const Offsets current = mOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -0700170
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200171 bool changed = false;
172 if (desired.sf != current.sf) {
Ana Krulec12096d02018-09-07 14:54:14 -0700173 if (mSfConnectionHandle != nullptr) {
174 mScheduler->setPhaseOffset(mSfConnectionHandle, desired.sf);
175 } else {
176 mSfEventThread->setPhaseOffset(desired.sf);
177 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200178 changed = true;
179 }
180 if (desired.app != current.app) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800181 if (mAppConnectionHandle != nullptr) {
Ana Krulec12096d02018-09-07 14:54:14 -0700182 mScheduler->setPhaseOffset(mAppConnectionHandle, desired.app);
183 } else {
184 mAppEventThread->setPhaseOffset(desired.app);
185 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200186 changed = true;
187 }
Dan Stoza2713c302018-03-28 17:07:36 -0700188
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200189 if (changed) {
190 mOffsets = desired;
Dan Stoza2713c302018-03-28 17:07:36 -0700191 }
192 }
193
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200194 Offsets mLateOffsets;
195 Offsets mEarlyOffsets;
196 Offsets mEarlyGlOffsets;
Ady Abraham45e4e362019-06-07 18:20:51 -0700197 nsecs_t mThresholdForNextVsync;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200198
199 EventThread* mSfEventThread = nullptr;
200 EventThread* mAppEventThread = nullptr;
201
Ana Krulec12096d02018-09-07 14:54:14 -0700202 Scheduler* mScheduler = nullptr;
203 Scheduler::ConnectionHandle* mAppConnectionHandle = nullptr;
204 Scheduler::ConnectionHandle* mSfConnectionHandle = nullptr;
205
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200206 std::atomic<Offsets> mOffsets;
207
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700208 std::atomic<Scheduler::TransactionStart> mTransactionStart =
209 Scheduler::TransactionStart::NORMAL;
Alec Mouri754c98a2019-03-18 18:53:42 -0700210 std::atomic<bool> mRefreshRateChangePending = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200211 std::atomic<int> mRemainingEarlyFrameCount = 0;
Alec Mouri5a102102019-05-23 07:14:20 -0700212 std::atomic<int> mRemainingRenderEngineUsageCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700213};
214
215} // namespace android