blob: 3126debed4b4fa7cd568c26ed3f0600b2f80e5a0 [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) {
58 if (transactionStart == mTransactionStart) return;
59 mTransactionStart = transactionStart;
60 updatePhaseOffsets();
61 }
62
63 void setLastFrameUsedRenderEngine(bool re) {
64 if (re == mLastFrameUsedRenderEngine) return;
65 mLastFrameUsedRenderEngine = re;
66 updatePhaseOffsets();
67 }
68
69private:
70
71 void updatePhaseOffsets() {
72
73 // Do not change phase offsets if disabled.
74 if (mEarlyPhaseOffset == mLatePhaseOffset) return;
75
76 if (mTransactionStart == TransactionStart::EARLY || mLastFrameUsedRenderEngine) {
77 if (mPhaseOffset != mEarlyPhaseOffset) {
78 if (mEventThread) {
79 mEventThread->setPhaseOffset(mEarlyPhaseOffset);
80 }
81 mPhaseOffset = mEarlyPhaseOffset;
82 }
83 } else {
84 if (mPhaseOffset != mLatePhaseOffset) {
85 if (mEventThread) {
86 mEventThread->setPhaseOffset(mLatePhaseOffset);
87 }
88 mPhaseOffset = mLatePhaseOffset;
89 }
90 }
91 }
92
93 nsecs_t mLatePhaseOffset = 0;
94 nsecs_t mEarlyPhaseOffset = 0;
95 EventThread* mEventThread = nullptr;
96 std::atomic<nsecs_t> mPhaseOffset = 0;
97 std::atomic<TransactionStart> mTransactionStart = TransactionStart::NORMAL;
98 std::atomic<bool> mLastFrameUsedRenderEngine = false;
99};
100
101} // namespace android