blob: dde0c57f722c690a27f80eb9040ca5e7cba7ee0f [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
Dan Stoza2713c302018-03-28 17:07:36 -070042 // Sets the phase offsets
43 //
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020044 // sfEarly: The phase offset when waking up SF early, which happens when marking a transaction
45 // as early. May be the same as late, in which case we don't shift offsets.
46 // sfEarlyGl: Like sfEarly, but only if we used GL composition. If we use both GL composition
47 // and the transaction was marked as early, we'll use sfEarly.
48 // sfLate: The regular SF vsync phase offset.
49 // appEarly: Like sfEarly, but for the app-vsync
50 // appEarlyGl: Like sfEarlyGl, but for the app-vsync.
51 // appLate: The regular app vsync phase offset.
52 void setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late) {
53 mEarlyOffsets = early;
54 mEarlyGlOffsets = earlyGl;
55 mLateOffsets = late;
56 mOffsets = late;
Dan Stoza2713c302018-03-28 17:07:36 -070057 }
58
Ana Krulecfefcb582018-08-07 14:22:37 -070059 Offsets getEarlyOffsets() const { return mEarlyOffsets; }
Dan Stoza2713c302018-03-28 17:07:36 -070060
Ana Krulecfefcb582018-08-07 14:22:37 -070061 Offsets getEarlyGlOffsets() const { return mEarlyGlOffsets; }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020062
63 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
64 mSfEventThread = sfEventThread;
65 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070066 }
67
Ana Krulec12096d02018-09-07 14:54:14 -070068 void setSchedulerAndHandles(Scheduler* scheduler,
69 Scheduler::ConnectionHandle* appConnectionHandle,
70 Scheduler::ConnectionHandle* sfConnectionHandle) {
71 mScheduler = scheduler;
72 mAppConnectionHandle = appConnectionHandle;
73 mSfConnectionHandle = sfConnectionHandle;
74 }
75
Ana Krulec7ecce8c2018-10-12 13:44:41 -070076 void setTransactionStart(Scheduler::TransactionStart transactionStart) {
77 if (transactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggi90535212018-05-23 23:44:06 +020078 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT;
79 }
80
Jorim Jaggif15c3be2018-04-12 12:56:58 +010081 // An early transaction stays an early transaction.
Ana Krulec7ecce8c2018-10-12 13:44:41 -070082 if (transactionStart == mTransactionStart ||
83 mTransactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggif15c3be2018-04-12 12:56:58 +010084 return;
85 }
Dan Stoza2713c302018-03-28 17:07:36 -070086 mTransactionStart = transactionStart;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020087 updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -070088 }
89
Jorim Jaggif15c3be2018-04-12 12:56:58 +010090 void onTransactionHandled() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -070091 if (mTransactionStart == Scheduler::TransactionStart::NORMAL) return;
92 mTransactionStart = Scheduler::TransactionStart::NORMAL;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020093 updateOffsets();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010094 }
95
Jorim Jaggi90535212018-05-23 23:44:06 +020096 void onRefreshed(bool usedRenderEngine) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020097 bool updateOffsetsNeeded = false;
Jorim Jaggi90535212018-05-23 23:44:06 +020098 if (mRemainingEarlyFrameCount > 0) {
99 mRemainingEarlyFrameCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200100 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200101 }
102 if (usedRenderEngine != mLastFrameUsedRenderEngine) {
103 mLastFrameUsedRenderEngine = usedRenderEngine;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200104 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200105 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200106 if (updateOffsetsNeeded) {
107 updateOffsets();
Jorim Jaggi90535212018-05-23 23:44:06 +0200108 }
Dan Stoza2713c302018-03-28 17:07:36 -0700109 }
110
111private:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200112 void updateOffsets() {
113 const Offsets desired = getOffsets();
114 const Offsets current = mOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -0700115
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200116 bool changed = false;
117 if (desired.sf != current.sf) {
Ana Krulec12096d02018-09-07 14:54:14 -0700118 if (mSfConnectionHandle != nullptr) {
119 mScheduler->setPhaseOffset(mSfConnectionHandle, desired.sf);
120 } else {
121 mSfEventThread->setPhaseOffset(desired.sf);
122 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200123 changed = true;
124 }
125 if (desired.app != current.app) {
Ana Krulec12096d02018-09-07 14:54:14 -0700126 if (mSfConnectionHandle != nullptr) {
127 mScheduler->setPhaseOffset(mAppConnectionHandle, desired.app);
128 } else {
129 mAppEventThread->setPhaseOffset(desired.app);
130 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200131 changed = true;
132 }
Dan Stoza2713c302018-03-28 17:07:36 -0700133
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200134 if (changed) {
135 mOffsets = desired;
Dan Stoza2713c302018-03-28 17:07:36 -0700136 }
137 }
138
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200139 Offsets getOffsets() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700140 if (mTransactionStart == Scheduler::TransactionStart::EARLY ||
141 mRemainingEarlyFrameCount > 0) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200142 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
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700163 std::atomic<Scheduler::TransactionStart> mTransactionStart =
164 Scheduler::TransactionStart::NORMAL;
Dan Stoza2713c302018-03-28 17:07:36 -0700165 std::atomic<bool> mLastFrameUsedRenderEngine = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200166 std::atomic<int> mRemainingEarlyFrameCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700167};
168
169} // namespace android