blob: 40a992cf01dd8b76cb2ee7f5aaae01d04f0a7426 [file] [log] [blame]
Alec Mourib488afa2019-05-23 10:22:24 -07001/*
2 * Copyright 2019 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Alec Mourid7599d82019-05-22 19:58:00 -070021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Alec Mourib488afa2019-05-23 10:22:24 -070023#include "VSyncModulator.h"
24
Alec Mourid7599d82019-05-22 19:58:00 -070025#include <cutils/properties.h>
26#include <utils/Trace.h>
27
Alec Mourib488afa2019-05-23 10:22:24 -070028#include <cinttypes>
29#include <mutex>
30
Dominik Laskowskieddeda12019-07-19 11:54:13 -070031namespace android::scheduler {
Alec Mourib488afa2019-05-23 10:22:24 -070032
Dominik Laskowskieddeda12019-07-19 11:54:13 -070033VSyncModulator::VSyncModulator(Scheduler& scheduler,
Dominik Laskowski98041832019-08-01 18:35:59 -070034 Scheduler::ConnectionHandle appConnectionHandle,
35 Scheduler::ConnectionHandle sfConnectionHandle,
Dominik Laskowskieddeda12019-07-19 11:54:13 -070036 const OffsetsConfig& config)
37 : mScheduler(scheduler),
38 mAppConnectionHandle(appConnectionHandle),
39 mSfConnectionHandle(sfConnectionHandle),
40 mOffsetsConfig(config) {
Alec Mourid7599d82019-05-22 19:58:00 -070041 char value[PROPERTY_VALUE_MAX];
42 property_get("debug.sf.vsync_trace_detailed_info", value, "0");
43 mTraceDetailedInfo = atoi(value);
Alec Mourid7599d82019-05-22 19:58:00 -070044}
45
Dominik Laskowskieddeda12019-07-19 11:54:13 -070046void VSyncModulator::setPhaseOffsets(const OffsetsConfig& config) {
Alec Mourid7599d82019-05-22 19:58:00 -070047 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskieddeda12019-07-19 11:54:13 -070048 mOffsetsConfig = config;
Alec Mourid7599d82019-05-22 19:58:00 -070049 updateOffsetsLocked();
Alec Mourib488afa2019-05-23 10:22:24 -070050}
51
52void VSyncModulator::setTransactionStart(Scheduler::TransactionStart transactionStart) {
53 if (transactionStart == Scheduler::TransactionStart::EARLY) {
54 mRemainingEarlyFrameCount = MIN_EARLY_FRAME_COUNT_TRANSACTION;
55 }
56
57 // An early transaction stays an early transaction.
58 if (transactionStart == mTransactionStart ||
59 mTransactionStart == Scheduler::TransactionStart::EARLY) {
60 return;
61 }
62 mTransactionStart = transactionStart;
63 updateOffsets();
64}
65
66void VSyncModulator::onTransactionHandled() {
67 if (mTransactionStart == Scheduler::TransactionStart::NORMAL) return;
68 mTransactionStart = Scheduler::TransactionStart::NORMAL;
69 updateOffsets();
70}
71
72void VSyncModulator::onRefreshRateChangeInitiated() {
73 if (mRefreshRateChangePending) {
74 return;
75 }
76 mRefreshRateChangePending = true;
77 updateOffsets();
78}
79
80void VSyncModulator::onRefreshRateChangeCompleted() {
81 if (!mRefreshRateChangePending) {
82 return;
83 }
84 mRefreshRateChangePending = false;
85 updateOffsets();
86}
87
88void VSyncModulator::onRefreshed(bool usedRenderEngine) {
89 bool updateOffsetsNeeded = false;
90 if (mRemainingEarlyFrameCount > 0) {
91 mRemainingEarlyFrameCount--;
92 updateOffsetsNeeded = true;
93 }
94 if (usedRenderEngine) {
95 mRemainingRenderEngineUsageCount = MIN_EARLY_GL_FRAME_COUNT_TRANSACTION;
96 updateOffsetsNeeded = true;
97 } else if (mRemainingRenderEngineUsageCount > 0) {
98 mRemainingRenderEngineUsageCount--;
99 updateOffsetsNeeded = true;
100 }
101 if (updateOffsetsNeeded) {
102 updateOffsets();
103 }
104}
105
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700106VSyncModulator::Offsets VSyncModulator::getOffsets() const {
Alec Mourid7599d82019-05-22 19:58:00 -0700107 std::lock_guard<std::mutex> lock(mMutex);
Alec Mourib656aed2019-06-21 16:27:05 -0700108 return mOffsets;
Alec Mourid7599d82019-05-22 19:58:00 -0700109}
110
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700111const VSyncModulator::Offsets& VSyncModulator::getNextOffsets() const {
Alec Mourib488afa2019-05-23 10:22:24 -0700112 // Early offsets are used if we're in the middle of a refresh rate
113 // change, or if we recently begin a transaction.
114 if (mTransactionStart == Scheduler::TransactionStart::EARLY || mRemainingEarlyFrameCount > 0 ||
115 mRefreshRateChangePending) {
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700116 return mOffsetsConfig.early;
Alec Mourib488afa2019-05-23 10:22:24 -0700117 } else if (mRemainingRenderEngineUsageCount > 0) {
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700118 return mOffsetsConfig.earlyGl;
Alec Mourib488afa2019-05-23 10:22:24 -0700119 } else {
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700120 return mOffsetsConfig.late;
Alec Mourib488afa2019-05-23 10:22:24 -0700121 }
122}
123
124void VSyncModulator::updateOffsets() {
Alec Mourid7599d82019-05-22 19:58:00 -0700125 std::lock_guard<std::mutex> lock(mMutex);
126 updateOffsetsLocked();
127}
128
129void VSyncModulator::updateOffsetsLocked() {
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700130 const Offsets& offsets = getNextOffsets();
Alec Mourib488afa2019-05-23 10:22:24 -0700131
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700132 mScheduler.setPhaseOffset(mSfConnectionHandle, offsets.sf);
133 mScheduler.setPhaseOffset(mAppConnectionHandle, offsets.app);
Alec Mourib488afa2019-05-23 10:22:24 -0700134
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700135 mOffsets = offsets;
Ady Abraham11b6a702019-06-27 11:24:19 -0700136
Alec Mourid7599d82019-05-22 19:58:00 -0700137 if (!mTraceDetailedInfo) {
138 return;
139 }
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700140
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700141 const bool isEarly = &offsets == &mOffsetsConfig.early;
142 const bool isEarlyGl = &offsets == &mOffsetsConfig.earlyGl;
143 const bool isLate = &offsets == &mOffsetsConfig.late;
144
Ady Abraham2139f732019-11-13 18:56:40 -0800145 ATRACE_INT("Vsync-EarlyOffsetsOn", isEarly);
146 ATRACE_INT("Vsync-EarlyGLOffsetsOn", isEarlyGl);
147 ATRACE_INT("Vsync-LateOffsetsOn", isLate);
Alec Mourid7599d82019-05-22 19:58:00 -0700148}
149
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700150} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800151
152// TODO(b/129481165): remove the #pragma below and fix conversion issues
153#pragma clang diagnostic pop // ignored "-Wconversion"