blob: 7dfad43f7060f3eb2dd5abebe311a2f1985b9ab3 [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
70 void setTransactionStart(TransactionStart transactionStart) {
Jorim Jaggi90535212018-05-23 23:44:06 +020071 if (transactionStart == TransactionStart::EARLY) {
72 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT;
73 }
74
Jorim Jaggif15c3be2018-04-12 12:56:58 +010075 // An early transaction stays an early transaction.
76 if (transactionStart == mTransactionStart || mTransactionStart == TransactionStart::EARLY) {
77 return;
78 }
Dan Stoza2713c302018-03-28 17:07:36 -070079 mTransactionStart = transactionStart;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020080 updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -070081 }
82
Jorim Jaggif15c3be2018-04-12 12:56:58 +010083 void onTransactionHandled() {
84 if (mTransactionStart == TransactionStart::NORMAL) return;
85 mTransactionStart = TransactionStart::NORMAL;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020086 updateOffsets();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010087 }
88
Jorim Jaggi90535212018-05-23 23:44:06 +020089 void onRefreshed(bool usedRenderEngine) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020090 bool updateOffsetsNeeded = false;
Jorim Jaggi90535212018-05-23 23:44:06 +020091 if (mRemainingEarlyFrameCount > 0) {
92 mRemainingEarlyFrameCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020093 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +020094 }
95 if (usedRenderEngine != mLastFrameUsedRenderEngine) {
96 mLastFrameUsedRenderEngine = usedRenderEngine;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020097 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +020098 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020099 if (updateOffsetsNeeded) {
100 updateOffsets();
Jorim Jaggi90535212018-05-23 23:44:06 +0200101 }
Dan Stoza2713c302018-03-28 17:07:36 -0700102 }
103
104private:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200105 void updateOffsets() {
106 const Offsets desired = getOffsets();
107 const Offsets current = mOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -0700108
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200109 bool changed = false;
110 if (desired.sf != current.sf) {
111 mSfEventThread->setPhaseOffset(desired.sf);
112 changed = true;
113 }
114 if (desired.app != current.app) {
115 mAppEventThread->setPhaseOffset(desired.app);
116 changed = true;
117 }
Dan Stoza2713c302018-03-28 17:07:36 -0700118
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200119 if (changed) {
120 mOffsets = desired;
Dan Stoza2713c302018-03-28 17:07:36 -0700121 }
122 }
123
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200124 Offsets getOffsets() {
125 if (mTransactionStart == TransactionStart::EARLY || mRemainingEarlyFrameCount > 0) {
126 return mEarlyOffsets;
127 } else if (mLastFrameUsedRenderEngine) {
128 return mEarlyGlOffsets;
129 } else {
130 return mLateOffsets;
131 }
Jorim Jaggi90535212018-05-23 23:44:06 +0200132 }
133
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200134 Offsets mLateOffsets;
135 Offsets mEarlyOffsets;
136 Offsets mEarlyGlOffsets;
137
138 EventThread* mSfEventThread = nullptr;
139 EventThread* mAppEventThread = nullptr;
140
141 std::atomic<Offsets> mOffsets;
142
Dan Stoza2713c302018-03-28 17:07:36 -0700143 std::atomic<TransactionStart> mTransactionStart = TransactionStart::NORMAL;
144 std::atomic<bool> mLastFrameUsedRenderEngine = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200145 std::atomic<int> mRemainingEarlyFrameCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700146};
147
148} // namespace android