File: VSyncModulator.cpp

package info (click to toggle)
android-platform-frameworks-native 1%3A10.0.0%2Br36-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 25,828 kB
  • sloc: cpp: 252,025; xml: 52,812; ansic: 26,775; java: 5,107; python: 1,887; sh: 266; asm: 105; makefile: 23
file content (164 lines) | stat: -rw-r--r-- 5,554 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define ATRACE_TAG ATRACE_TAG_GRAPHICS

#include "VSyncModulator.h"

#include <cutils/properties.h>
#include <utils/Trace.h>

#include <cinttypes>
#include <mutex>

namespace android {

using RefreshRateType = scheduler::RefreshRateConfigs::RefreshRateType;
VSyncModulator::VSyncModulator() {
    char value[PROPERTY_VALUE_MAX];
    property_get("debug.sf.vsync_trace_detailed_info", value, "0");
    mTraceDetailedInfo = atoi(value);
    // Populate the offset map with some default offsets.
    const Offsets defaultOffsets = {RefreshRateType::DEFAULT, 0, 0};
    setPhaseOffsets(defaultOffsets, defaultOffsets, defaultOffsets, 0);
}

void VSyncModulator::setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late,
                                     nsecs_t thresholdForNextVsync) {
    std::lock_guard<std::mutex> lock(mMutex);
    mOffsetMap.insert_or_assign(OffsetType::Early, early);
    mOffsetMap.insert_or_assign(OffsetType::EarlyGl, earlyGl);
    mOffsetMap.insert_or_assign(OffsetType::Late, late);
    mThresholdForNextVsync = thresholdForNextVsync;
    updateOffsetsLocked();
}

void VSyncModulator::setTransactionStart(Scheduler::TransactionStart transactionStart) {
    if (transactionStart == Scheduler::TransactionStart::EARLY) {
        mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT_TRANSACTION;
    }

    // An early transaction stays an early transaction.
    if (transactionStart == mTransactionStart ||
        mTransactionStart == Scheduler::TransactionStart::EARLY) {
        return;
    }
    mTransactionStart = transactionStart;
    updateOffsets();
}

void VSyncModulator::onTransactionHandled() {
    if (mTransactionStart == Scheduler::TransactionStart::NORMAL) return;
    mTransactionStart = Scheduler::TransactionStart::NORMAL;
    updateOffsets();
}

void VSyncModulator::onRefreshRateChangeInitiated() {
    if (mRefreshRateChangePending) {
        return;
    }
    mRefreshRateChangePending = true;
    updateOffsets();
}

void VSyncModulator::onRefreshRateChangeCompleted() {
    if (!mRefreshRateChangePending) {
        return;
    }
    mRefreshRateChangePending = false;
    updateOffsets();
}

void VSyncModulator::onRefreshed(bool usedRenderEngine) {
    bool updateOffsetsNeeded = false;
    if (mRemainingEarlyFrameCount > 0) {
        mRemainingEarlyFrameCount--;
        updateOffsetsNeeded = true;
    }
    if (usedRenderEngine) {
        mRemainingRenderEngineUsageCount = MIN_EARLY_GL_FRAME_COUNT_TRANSACTION;
        updateOffsetsNeeded = true;
    } else if (mRemainingRenderEngineUsageCount > 0) {
        mRemainingRenderEngineUsageCount--;
        updateOffsetsNeeded = true;
    }
    if (updateOffsetsNeeded) {
        updateOffsets();
    }
}

VSyncModulator::Offsets VSyncModulator::getOffsets() {
    std::lock_guard<std::mutex> lock(mMutex);
    return mOffsets;
}

VSyncModulator::Offsets VSyncModulator::getNextOffsets() {
    return mOffsetMap.at(getNextOffsetType());
}

VSyncModulator::OffsetType VSyncModulator::getNextOffsetType() {
    // Early offsets are used if we're in the middle of a refresh rate
    // change, or if we recently begin a transaction.
    if (mTransactionStart == Scheduler::TransactionStart::EARLY || mRemainingEarlyFrameCount > 0 ||
        mRefreshRateChangePending) {
        return OffsetType::Early;
    } else if (mRemainingRenderEngineUsageCount > 0) {
        return OffsetType::EarlyGl;
    } else {
        return OffsetType::Late;
    }
}

void VSyncModulator::updateOffsets() {
    std::lock_guard<std::mutex> lock(mMutex);
    updateOffsetsLocked();
}

void VSyncModulator::updateOffsetsLocked() {
    const Offsets desired = getNextOffsets();

    if (mSfConnectionHandle != nullptr) {
        mScheduler->setPhaseOffset(mSfConnectionHandle, desired.sf);
    }

    if (mAppConnectionHandle != nullptr) {
        mScheduler->setPhaseOffset(mAppConnectionHandle, desired.app);
    }

    flushOffsets();
}

void VSyncModulator::flushOffsets() {
    OffsetType type = getNextOffsetType();
    mOffsets = mOffsetMap.at(type);
    if (!mTraceDetailedInfo) {
        return;
    }
    ATRACE_INT("Vsync-EarlyOffsetsOn",
               mOffsets.fpsMode == RefreshRateType::DEFAULT && type == OffsetType::Early);
    ATRACE_INT("Vsync-EarlyGLOffsetsOn",
               mOffsets.fpsMode == RefreshRateType::DEFAULT && type == OffsetType::EarlyGl);
    ATRACE_INT("Vsync-LateOffsetsOn",
               mOffsets.fpsMode == RefreshRateType::DEFAULT && type == OffsetType::Late);
    ATRACE_INT("Vsync-HighFpsEarlyOffsetsOn",
               mOffsets.fpsMode == RefreshRateType::PERFORMANCE && type == OffsetType::Early);
    ATRACE_INT("Vsync-HighFpsEarlyGLOffsetsOn",
               mOffsets.fpsMode == RefreshRateType::PERFORMANCE && type == OffsetType::EarlyGl);
    ATRACE_INT("Vsync-HighFpsLateOffsetsOn",
               mOffsets.fpsMode == RefreshRateType::PERFORMANCE && type == OffsetType::Late);
}

} // namespace android