blob: d7ec733f54b4821bebaca068618db7d191297ced [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:
Jorim Jaggi90535212018-05-23 23:44:06 +020033 // Number of frames we'll keep the early phase offsets once they are activated. This acts as a
34 // low-pass filter in case the client isn't quick enough in sending new transactions.
35 const int MIN_EARLY_FRAME_COUNT = 2;
36
Dan Stoza2713c302018-03-28 17:07:36 -070037public:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020038 struct Offsets {
39 nsecs_t sf;
40 nsecs_t app;
41 };
42
Dan Stoza2713c302018-03-28 17:07:36 -070043 // Sets the phase offsets
44 //
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020045 // sfEarly: The phase offset when waking up SF early, which happens when marking a transaction
46 // as early. May be the same as late, in which case we don't shift offsets.
47 // sfEarlyGl: Like sfEarly, but only if we used GL composition. If we use both GL composition
48 // and the transaction was marked as early, we'll use sfEarly.
49 // sfLate: The regular SF vsync phase offset.
50 // appEarly: Like sfEarly, but for the app-vsync
51 // appEarlyGl: Like sfEarlyGl, but for the app-vsync.
52 // appLate: The regular app vsync phase offset.
53 void setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late) {
54 mEarlyOffsets = early;
55 mEarlyGlOffsets = earlyGl;
56 mLateOffsets = late;
57 mOffsets = late;
Dan Stoza2713c302018-03-28 17:07:36 -070058 }
59
Ana Krulecfefcb582018-08-07 14:22:37 -070060 Offsets getEarlyOffsets() const { return mEarlyOffsets; }
Dan Stoza2713c302018-03-28 17:07:36 -070061
Ana Krulecfefcb582018-08-07 14:22:37 -070062 Offsets getEarlyGlOffsets() const { return mEarlyGlOffsets; }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020063
64 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
65 mSfEventThread = sfEventThread;
66 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070067 }
68
Ana Krulec12096d02018-09-07 14:54:14 -070069 void setSchedulerAndHandles(Scheduler* scheduler,
70 Scheduler::ConnectionHandle* appConnectionHandle,
71 Scheduler::ConnectionHandle* sfConnectionHandle) {
72 mScheduler = scheduler;
73 mAppConnectionHandle = appConnectionHandle;
74 mSfConnectionHandle = sfConnectionHandle;
75 }
76
Ana Krulec7ecce8c2018-10-12 13:44:41 -070077 void setTransactionStart(Scheduler::TransactionStart transactionStart) {
78 if (transactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggi90535212018-05-23 23:44:06 +020079 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT;
80 }
81
Jorim Jaggif15c3be2018-04-12 12:56:58 +010082 // An early transaction stays an early transaction.
Ana Krulec7ecce8c2018-10-12 13:44:41 -070083 if (transactionStart == mTransactionStart ||
84 mTransactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggif15c3be2018-04-12 12:56:58 +010085 return;
86 }
Dan Stoza2713c302018-03-28 17:07:36 -070087 mTransactionStart = transactionStart;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020088 updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -070089 }
90
Jorim Jaggif15c3be2018-04-12 12:56:58 +010091 void onTransactionHandled() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -070092 if (mTransactionStart == Scheduler::TransactionStart::NORMAL) return;
93 mTransactionStart = Scheduler::TransactionStart::NORMAL;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020094 updateOffsets();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010095 }
96
Jorim Jaggi90535212018-05-23 23:44:06 +020097 void onRefreshed(bool usedRenderEngine) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020098 bool updateOffsetsNeeded = false;
Jorim Jaggi90535212018-05-23 23:44:06 +020099 if (mRemainingEarlyFrameCount > 0) {
100 mRemainingEarlyFrameCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200101 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200102 }
103 if (usedRenderEngine != mLastFrameUsedRenderEngine) {
104 mLastFrameUsedRenderEngine = usedRenderEngine;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200105 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200106 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200107 if (updateOffsetsNeeded) {
108 updateOffsets();
Jorim Jaggi90535212018-05-23 23:44:06 +0200109 }
Dan Stoza2713c302018-03-28 17:07:36 -0700110 }
111
112private:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200113 void updateOffsets() {
114 const Offsets desired = getOffsets();
115 const Offsets current = mOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -0700116
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200117 bool changed = false;
118 if (desired.sf != current.sf) {
Ana Krulec12096d02018-09-07 14:54:14 -0700119 if (mSfConnectionHandle != nullptr) {
120 mScheduler->setPhaseOffset(mSfConnectionHandle, desired.sf);
121 } else {
122 mSfEventThread->setPhaseOffset(desired.sf);
123 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200124 changed = true;
125 }
126 if (desired.app != current.app) {
Ana Krulec12096d02018-09-07 14:54:14 -0700127 if (mSfConnectionHandle != nullptr) {
128 mScheduler->setPhaseOffset(mAppConnectionHandle, desired.app);
129 } else {
130 mAppEventThread->setPhaseOffset(desired.app);
131 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200132 changed = true;
133 }
Dan Stoza2713c302018-03-28 17:07:36 -0700134
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200135 if (changed) {
136 mOffsets = desired;
Dan Stoza2713c302018-03-28 17:07:36 -0700137 }
138 }
139
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200140 Offsets getOffsets() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700141 if (mTransactionStart == Scheduler::TransactionStart::EARLY ||
142 mRemainingEarlyFrameCount > 0) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200143 return mEarlyOffsets;
144 } else if (mLastFrameUsedRenderEngine) {
145 return mEarlyGlOffsets;
146 } else {
147 return mLateOffsets;
148 }
Jorim Jaggi90535212018-05-23 23:44:06 +0200149 }
150
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200151 Offsets mLateOffsets;
152 Offsets mEarlyOffsets;
153 Offsets mEarlyGlOffsets;
154
155 EventThread* mSfEventThread = nullptr;
156 EventThread* mAppEventThread = nullptr;
157
Ana Krulec12096d02018-09-07 14:54:14 -0700158 Scheduler* mScheduler = nullptr;
159 Scheduler::ConnectionHandle* mAppConnectionHandle = nullptr;
160 Scheduler::ConnectionHandle* mSfConnectionHandle = nullptr;
161
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200162 std::atomic<Offsets> mOffsets;
163
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700164 std::atomic<Scheduler::TransactionStart> mTransactionStart =
165 Scheduler::TransactionStart::NORMAL;
Dan Stoza2713c302018-03-28 17:07:36 -0700166 std::atomic<bool> mLastFrameUsedRenderEngine = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200167 std::atomic<int> mRemainingEarlyFrameCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700168};
169
170} // namespace android