blob: e071a599c97dedd5961d8c7798161da5eee79406 [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:
32
33 // 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:
38
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020039 struct Offsets {
40 nsecs_t sf;
41 nsecs_t app;
42 };
43
Dan Stoza2713c302018-03-28 17:07:36 -070044 enum TransactionStart {
45 EARLY,
46 NORMAL
47 };
48
49 // 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.
59 void setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late) {
60 mEarlyOffsets = early;
61 mEarlyGlOffsets = earlyGl;
62 mLateOffsets = late;
63 mOffsets = late;
Dan Stoza2713c302018-03-28 17:07:36 -070064 }
65
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020066 Offsets getEarlyOffsets() const {
67 return mEarlyOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -070068 }
69
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020070 Offsets getEarlyGlOffsets() const {
71 return mEarlyGlOffsets;
72 }
73
74 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
75 mSfEventThread = sfEventThread;
76 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070077 }
78
79 void setTransactionStart(TransactionStart transactionStart) {
Jorim Jaggi90535212018-05-23 23:44:06 +020080
81 if (transactionStart == TransactionStart::EARLY) {
82 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT;
83 }
84
Jorim Jaggif15c3be2018-04-12 12:56:58 +010085 // An early transaction stays an early transaction.
86 if (transactionStart == mTransactionStart || mTransactionStart == TransactionStart::EARLY) {
87 return;
88 }
Dan Stoza2713c302018-03-28 17:07:36 -070089 mTransactionStart = transactionStart;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020090 updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -070091 }
92
Jorim Jaggif15c3be2018-04-12 12:56:58 +010093 void onTransactionHandled() {
94 if (mTransactionStart == TransactionStart::NORMAL) return;
95 mTransactionStart = TransactionStart::NORMAL;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020096 updateOffsets();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010097 }
98
Jorim Jaggi90535212018-05-23 23:44:06 +020099 void onRefreshed(bool usedRenderEngine) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200100 bool updateOffsetsNeeded = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200101 if (mRemainingEarlyFrameCount > 0) {
102 mRemainingEarlyFrameCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200103 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200104 }
105 if (usedRenderEngine != mLastFrameUsedRenderEngine) {
106 mLastFrameUsedRenderEngine = usedRenderEngine;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200107 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200108 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200109 if (updateOffsetsNeeded) {
110 updateOffsets();
Jorim Jaggi90535212018-05-23 23:44:06 +0200111 }
Dan Stoza2713c302018-03-28 17:07:36 -0700112 }
113
114private:
115
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200116 void updateOffsets() {
117 const Offsets desired = getOffsets();
118 const Offsets current = mOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -0700119
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200120 bool changed = false;
121 if (desired.sf != current.sf) {
122 mSfEventThread->setPhaseOffset(desired.sf);
123 changed = true;
124 }
125 if (desired.app != current.app) {
126 mAppEventThread->setPhaseOffset(desired.app);
127 changed = true;
128 }
Dan Stoza2713c302018-03-28 17:07:36 -0700129
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200130 if (changed) {
131 mOffsets = desired;
Dan Stoza2713c302018-03-28 17:07:36 -0700132 }
133 }
134
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200135 Offsets getOffsets() {
136 if (mTransactionStart == TransactionStart::EARLY || mRemainingEarlyFrameCount > 0) {
137 return mEarlyOffsets;
138 } else if (mLastFrameUsedRenderEngine) {
139 return mEarlyGlOffsets;
140 } else {
141 return mLateOffsets;
142 }
Jorim Jaggi90535212018-05-23 23:44:06 +0200143 }
144
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200145 Offsets mLateOffsets;
146 Offsets mEarlyOffsets;
147 Offsets mEarlyGlOffsets;
148
149 EventThread* mSfEventThread = nullptr;
150 EventThread* mAppEventThread = nullptr;
151
152 std::atomic<Offsets> mOffsets;
153
Dan Stoza2713c302018-03-28 17:07:36 -0700154 std::atomic<TransactionStart> mTransactionStart = TransactionStart::NORMAL;
155 std::atomic<bool> mLastFrameUsedRenderEngine = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200156 std::atomic<int> mRemainingEarlyFrameCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700157};
158
159} // namespace android