blob: dab2003d912a6677b5b91d8fad6d3c626b2e7f7c [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;
Ady Abrahamf6656352019-02-14 16:29:49 -080057
58 if (mSfConnectionHandle && late.sf != mOffsets.load().sf) {
59 mScheduler->setPhaseOffset(mSfConnectionHandle, late.sf);
60 }
61
62 if (mAppConnectionHandle && late.app != mOffsets.load().app) {
63 mScheduler->setPhaseOffset(mAppConnectionHandle, late.app);
64 }
65
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020066 mOffsets = late;
Dan Stoza2713c302018-03-28 17:07:36 -070067 }
68
Ana Krulecfefcb582018-08-07 14:22:37 -070069 Offsets getEarlyOffsets() const { return mEarlyOffsets; }
Dan Stoza2713c302018-03-28 17:07:36 -070070
Ana Krulecfefcb582018-08-07 14:22:37 -070071 Offsets getEarlyGlOffsets() const { return mEarlyGlOffsets; }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020072
73 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
74 mSfEventThread = sfEventThread;
75 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070076 }
77
Ana Krulec12096d02018-09-07 14:54:14 -070078 void setSchedulerAndHandles(Scheduler* scheduler,
79 Scheduler::ConnectionHandle* appConnectionHandle,
80 Scheduler::ConnectionHandle* sfConnectionHandle) {
81 mScheduler = scheduler;
82 mAppConnectionHandle = appConnectionHandle;
83 mSfConnectionHandle = sfConnectionHandle;
84 }
85
Ana Krulec7ecce8c2018-10-12 13:44:41 -070086 void setTransactionStart(Scheduler::TransactionStart transactionStart) {
87 if (transactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggi90535212018-05-23 23:44:06 +020088 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT;
89 }
90
Jorim Jaggif15c3be2018-04-12 12:56:58 +010091 // An early transaction stays an early transaction.
Ana Krulec7ecce8c2018-10-12 13:44:41 -070092 if (transactionStart == mTransactionStart ||
93 mTransactionStart == Scheduler::TransactionStart::EARLY) {
Jorim Jaggif15c3be2018-04-12 12:56:58 +010094 return;
95 }
Dan Stoza2713c302018-03-28 17:07:36 -070096 mTransactionStart = transactionStart;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020097 updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -070098 }
99
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100100 void onTransactionHandled() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700101 if (mTransactionStart == Scheduler::TransactionStart::NORMAL) return;
102 mTransactionStart = Scheduler::TransactionStart::NORMAL;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200103 updateOffsets();
Jorim Jaggif15c3be2018-04-12 12:56:58 +0100104 }
105
Jorim Jaggi90535212018-05-23 23:44:06 +0200106 void onRefreshed(bool usedRenderEngine) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200107 bool updateOffsetsNeeded = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200108 if (mRemainingEarlyFrameCount > 0) {
109 mRemainingEarlyFrameCount--;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200110 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200111 }
112 if (usedRenderEngine != mLastFrameUsedRenderEngine) {
113 mLastFrameUsedRenderEngine = usedRenderEngine;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200114 updateOffsetsNeeded = true;
Jorim Jaggi90535212018-05-23 23:44:06 +0200115 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200116 if (updateOffsetsNeeded) {
117 updateOffsets();
Jorim Jaggi90535212018-05-23 23:44:06 +0200118 }
Dan Stoza2713c302018-03-28 17:07:36 -0700119 }
120
121private:
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200122 void updateOffsets() {
123 const Offsets desired = getOffsets();
124 const Offsets current = mOffsets;
Dan Stoza2713c302018-03-28 17:07:36 -0700125
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200126 bool changed = false;
127 if (desired.sf != current.sf) {
Ana Krulec12096d02018-09-07 14:54:14 -0700128 if (mSfConnectionHandle != nullptr) {
129 mScheduler->setPhaseOffset(mSfConnectionHandle, desired.sf);
130 } else {
131 mSfEventThread->setPhaseOffset(desired.sf);
132 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200133 changed = true;
134 }
135 if (desired.app != current.app) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800136 if (mAppConnectionHandle != nullptr) {
Ana Krulec12096d02018-09-07 14:54:14 -0700137 mScheduler->setPhaseOffset(mAppConnectionHandle, desired.app);
138 } else {
139 mAppEventThread->setPhaseOffset(desired.app);
140 }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200141 changed = true;
142 }
Dan Stoza2713c302018-03-28 17:07:36 -0700143
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200144 if (changed) {
145 mOffsets = desired;
Dan Stoza2713c302018-03-28 17:07:36 -0700146 }
147 }
148
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200149 Offsets getOffsets() {
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700150 if (mTransactionStart == Scheduler::TransactionStart::EARLY ||
151 mRemainingEarlyFrameCount > 0) {
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200152 return mEarlyOffsets;
153 } else if (mLastFrameUsedRenderEngine) {
154 return mEarlyGlOffsets;
155 } else {
156 return mLateOffsets;
157 }
Jorim Jaggi90535212018-05-23 23:44:06 +0200158 }
159
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200160 Offsets mLateOffsets;
161 Offsets mEarlyOffsets;
162 Offsets mEarlyGlOffsets;
163
164 EventThread* mSfEventThread = nullptr;
165 EventThread* mAppEventThread = nullptr;
166
Ana Krulec12096d02018-09-07 14:54:14 -0700167 Scheduler* mScheduler = nullptr;
168 Scheduler::ConnectionHandle* mAppConnectionHandle = nullptr;
169 Scheduler::ConnectionHandle* mSfConnectionHandle = nullptr;
170
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200171 std::atomic<Offsets> mOffsets;
172
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700173 std::atomic<Scheduler::TransactionStart> mTransactionStart =
174 Scheduler::TransactionStart::NORMAL;
Dan Stoza2713c302018-03-28 17:07:36 -0700175 std::atomic<bool> mLastFrameUsedRenderEngine = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200176 std::atomic<int> mRemainingEarlyFrameCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700177};
178
179} // namespace android