blob: ea8ca4c8b90778e7da910573f600883ec21da36e [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
21#include <mutex>
22
23using namespace android::surfaceflinger;
24
25namespace android {
26
27/*
28 * Modulates the vsync-offsets depending on current SurfaceFlinger state.
29 */
30class VSyncModulator {
Jorim Jaggi90535212018-05-23 23:44:06 +020031private:
Jorim Jaggi90535212018-05-23 23:44:06 +020032 // Number of frames we'll keep the early phase offsets once they are activated. This acts as a
33 // low-pass filter in case the client isn't quick enough in sending new transactions.
34 const int MIN_EARLY_FRAME_COUNT = 2;
35
Dan Stoza2713c302018-03-28 17:07:36 -070036public:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020037 struct Offsets {
38 nsecs_t sf;
39 nsecs_t app;
40 };
41
Ana Krulecfefcb582018-08-07 14:22:37 -070042 enum TransactionStart { EARLY, NORMAL };
Dan Stoza2713c302018-03-28 17:07:36 -070043
44 // 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;
58 mOffsets = late;
Dan Stoza2713c302018-03-28 17:07:36 -070059 }
60
Ana Krulecfefcb582018-08-07 14:22:37 -070061 Offsets getEarlyOffsets() const { return mEarlyOffsets; }
Dan Stoza2713c302018-03-28 17:07:36 -070062
Ana Krulecfefcb582018-08-07 14:22:37 -070063 Offsets getEarlyGlOffsets() const { return mEarlyGlOffsets; }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020064
65 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
66 mSfEventThread = sfEventThread;
67 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070068 }
69
Ana Krulec12096d02018-09-07 14:54:14 -070070 void setSchedulerAndHandles(Scheduler* scheduler,
71 Scheduler::ConnectionHandle* appConnectionHandle,
72 Scheduler::ConnectionHandle* sfConnectionHandle) {
73 mScheduler = scheduler;
74 mAppConnectionHandle = appConnectionHandle;
75 mSfConnectionHandle = sfConnectionHandle;
76 }
77
Dan Stoza2713c302018-03-28 17:07:36 -070078 void setTransactionStart(TransactionStart transactionStart) {
Jorim Jaggi90535212018-05-23 23:44:06 +020079 if (transactionStart == TransactionStart::EARLY) {
80 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT;
81 }
82
Jorim Jaggif15c3be2018-04-12 12:56:58 +010083 // An early transaction stays an early transaction.
84 if (transactionStart == mTransactionStart || mTransactionStart == TransactionStart::EARLY) {
85 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() {
92 if (mTransactionStart == TransactionStart::NORMAL) return;
93 mTransactionStart = 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() {
141 if (mTransactionStart == TransactionStart::EARLY || mRemainingEarlyFrameCount > 0) {
142 return mEarlyOffsets;
143 } else if (mLastFrameUsedRenderEngine) {
144 return mEarlyGlOffsets;
145 } else {
146 return mLateOffsets;
147 }
Jorim Jaggi90535212018-05-23 23:44:06 +0200148 }
149
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200150 Offsets mLateOffsets;
151 Offsets mEarlyOffsets;
152 Offsets mEarlyGlOffsets;
153
154 EventThread* mSfEventThread = nullptr;
155 EventThread* mAppEventThread = nullptr;
156
Ana Krulec12096d02018-09-07 14:54:14 -0700157 Scheduler* mScheduler = nullptr;
158 Scheduler::ConnectionHandle* mAppConnectionHandle = nullptr;
159 Scheduler::ConnectionHandle* mSfConnectionHandle = nullptr;
160
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200161 std::atomic<Offsets> mOffsets;
162
Dan Stoza2713c302018-03-28 17:07:36 -0700163 std::atomic<TransactionStart> mTransactionStart = TransactionStart::NORMAL;
164 std::atomic<bool> mLastFrameUsedRenderEngine = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200165 std::atomic<int> mRemainingEarlyFrameCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700166};
167
168} // namespace android