blob: 3e5800e2a2b6cbe5ee9b6090b42d79d3375c0e71 [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 {
31public:
32
33 enum TransactionStart {
34 EARLY,
35 NORMAL
36 };
37
38 // Sets the phase offsets
39 //
40 // early: the phase offset when waking up early. May be the same as late, in which case we don't
41 // shift offsets.
42 // late: the regular sf phase offset.
43 void setPhaseOffsets(nsecs_t early, nsecs_t late) {
44 mEarlyPhaseOffset = early;
45 mLatePhaseOffset = late;
46 mPhaseOffset = late;
47 }
48
49 nsecs_t getEarlyPhaseOffset() const {
50 return mEarlyPhaseOffset;
51 }
52
53 void setEventThread(EventThread* eventThread) {
54 mEventThread = eventThread;
55 }
56
57 void setTransactionStart(TransactionStart transactionStart) {
Jorim Jaggif15c3be2018-04-12 12:56:58 +010058 // An early transaction stays an early transaction.
59 if (transactionStart == mTransactionStart || mTransactionStart == TransactionStart::EARLY) {
60 return;
61 }
Dan Stoza2713c302018-03-28 17:07:36 -070062 mTransactionStart = transactionStart;
63 updatePhaseOffsets();
64 }
65
Jorim Jaggif15c3be2018-04-12 12:56:58 +010066 void onTransactionHandled() {
67 if (mTransactionStart == TransactionStart::NORMAL) return;
68 mTransactionStart = TransactionStart::NORMAL;
69 updatePhaseOffsets();
70 }
71
Dan Stoza2713c302018-03-28 17:07:36 -070072 void setLastFrameUsedRenderEngine(bool re) {
73 if (re == mLastFrameUsedRenderEngine) return;
74 mLastFrameUsedRenderEngine = re;
75 updatePhaseOffsets();
76 }
77
78private:
79
80 void updatePhaseOffsets() {
81
82 // Do not change phase offsets if disabled.
83 if (mEarlyPhaseOffset == mLatePhaseOffset) return;
84
85 if (mTransactionStart == TransactionStart::EARLY || mLastFrameUsedRenderEngine) {
86 if (mPhaseOffset != mEarlyPhaseOffset) {
87 if (mEventThread) {
88 mEventThread->setPhaseOffset(mEarlyPhaseOffset);
89 }
90 mPhaseOffset = mEarlyPhaseOffset;
91 }
92 } else {
93 if (mPhaseOffset != mLatePhaseOffset) {
94 if (mEventThread) {
95 mEventThread->setPhaseOffset(mLatePhaseOffset);
96 }
97 mPhaseOffset = mLatePhaseOffset;
98 }
99 }
100 }
101
102 nsecs_t mLatePhaseOffset = 0;
103 nsecs_t mEarlyPhaseOffset = 0;
104 EventThread* mEventThread = nullptr;
105 std::atomic<nsecs_t> mPhaseOffset = 0;
106 std::atomic<TransactionStart> mTransactionStart = TransactionStart::NORMAL;
107 std::atomic<bool> mLastFrameUsedRenderEngine = false;
108};
109
110} // namespace android