blob: d777ef99ae5577964e6a2f51496ce2b82db1f0f6 [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
Alec Mourif792cfe2020-06-05 13:11:23 -070019#include <chrono>
Dan Stoza2713c302018-03-28 17:07:36 -070020#include <mutex>
21
Ana Krulec757f63a2019-01-25 10:46:18 -080022#include "Scheduler.h"
Dan Stoza2713c302018-03-28 17:07:36 -070023
Dominik Laskowskieddeda12019-07-19 11:54:13 -070024namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070025
26/*
27 * Modulates the vsync-offsets depending on current SurfaceFlinger state.
28 */
29class VSyncModulator {
Jorim Jaggi90535212018-05-23 23:44:06 +020030private:
Alec Mouri754c98a2019-03-18 18:53:42 -070031 // Number of frames we'll keep the early phase offsets once they are activated for a
32 // transaction. This acts as a low-pass filter in case the client isn't quick enough in
33 // sending new transactions.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070034 static constexpr int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2;
Jorim Jaggi90535212018-05-23 23:44:06 +020035
Alec Mouri5a102102019-05-23 07:14:20 -070036 // Number of frames we'll keep the early gl phase offsets once they are activated.
37 // This acts as a low-pass filter to avoid scenarios where we rapidly
38 // switch in and out of gl composition.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070039 static constexpr int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2;
40
Dan Stoza2713c302018-03-28 17:07:36 -070041public:
Alec Mourib488afa2019-05-23 10:22:24 -070042 // Wrapper for a collection of surfaceflinger/app offsets for a particular
Dominik Laskowskieddeda12019-07-19 11:54:13 -070043 // configuration.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020044 struct Offsets {
45 nsecs_t sf;
46 nsecs_t app;
Ady Abraham9e16a482019-12-03 17:19:41 -080047
48 bool operator==(const Offsets& other) const { return sf == other.sf && app == other.app; }
49
50 bool operator!=(const Offsets& other) const { return !(*this == other); }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020051 };
52
Dominik Laskowskieddeda12019-07-19 11:54:13 -070053 struct OffsetsConfig {
54 Offsets early; // For transactions with the eEarlyWakeup flag.
55 Offsets earlyGl; // As above but while compositing with GL.
56 Offsets late; // Default.
57
Ady Abraham9e16a482019-12-03 17:19:41 -080058 bool operator==(const OffsetsConfig& other) const {
59 return early == other.early && earlyGl == other.earlyGl && late == other.late;
60 }
61
62 bool operator!=(const OffsetsConfig& other) const { return !(*this == other); }
Alec Mourid7599d82019-05-22 19:58:00 -070063 };
64
Dominik Laskowski98041832019-08-01 18:35:59 -070065 VSyncModulator(Scheduler&, ConnectionHandle appConnectionHandle,
66 ConnectionHandle sfConnectionHandle, const OffsetsConfig&);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020067
Dominik Laskowskieddeda12019-07-19 11:54:13 -070068 void setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex);
Ana Krulec12096d02018-09-07 14:54:14 -070069
Alec Mourib488afa2019-05-23 10:22:24 -070070 // Signals that a transaction has started, and changes offsets accordingly.
71 void setTransactionStart(Scheduler::TransactionStart transactionStart);
Jorim Jaggi90535212018-05-23 23:44:06 +020072
Alec Mourib488afa2019-05-23 10:22:24 -070073 // Signals that a transaction has been completed, so that we can finish
74 // special handling for a transaction.
75 void onTransactionHandled();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010076
Alec Mouri754c98a2019-03-18 18:53:42 -070077 // Called when we send a refresh rate change to hardware composer, so that
78 // we can move into early offsets.
Alec Mourib488afa2019-05-23 10:22:24 -070079 void onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070080
81 // Called when we detect from vsync signals that the refresh rate changed.
82 // This way we can move out of early offsets if no longer necessary.
Alec Mourib488afa2019-05-23 10:22:24 -070083 void onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070084
Alec Mourib488afa2019-05-23 10:22:24 -070085 // Called when the display is presenting a new frame. usedRenderEngine
86 // should be set to true if RenderEngine was involved with composing the new
87 // frame.
88 void onRefreshed(bool usedRenderEngine);
Alec Mouri5a102102019-05-23 07:14:20 -070089
Alec Mourid7599d82019-05-22 19:58:00 -070090 // Returns the offsets that we are currently using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070091 Offsets getOffsets() const EXCLUDES(mMutex);
Ady Abrahambe0f9482019-04-24 15:41:53 -070092
Dan Stoza2713c302018-03-28 17:07:36 -070093private:
Alec Mourid7599d82019-05-22 19:58:00 -070094 // Returns the next offsets that we should be using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070095 const Offsets& getNextOffsets() const REQUIRES(mMutex);
Alec Mourid7599d82019-05-22 19:58:00 -070096 // Updates offsets and persists them into the scheduler framework.
97 void updateOffsets() EXCLUDES(mMutex);
98 void updateOffsetsLocked() REQUIRES(mMutex);
Dominik Laskowskieddeda12019-07-19 11:54:13 -070099
100 Scheduler& mScheduler;
Dominik Laskowski98041832019-08-01 18:35:59 -0700101 const ConnectionHandle mAppConnectionHandle;
102 const ConnectionHandle mSfConnectionHandle;
Dan Stoza2713c302018-03-28 17:07:36 -0700103
Alec Mourid7599d82019-05-22 19:58:00 -0700104 mutable std::mutex mMutex;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700105 OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200106
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700107 Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200108
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700109 std::atomic<Scheduler::TransactionStart> mTransactionStart =
110 Scheduler::TransactionStart::NORMAL;
Alec Mouri754c98a2019-03-18 18:53:42 -0700111 std::atomic<bool> mRefreshRateChangePending = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200112 std::atomic<int> mRemainingEarlyFrameCount = 0;
Alec Mouri5a102102019-05-23 07:14:20 -0700113 std::atomic<int> mRemainingRenderEngineUsageCount = 0;
Alec Mourif792cfe2020-06-05 13:11:23 -0700114 std::atomic<std::chrono::steady_clock::time_point> mEarlyTxnStartTime = {};
115 std::atomic<std::chrono::steady_clock::time_point> mTxnAppliedTime = {};
Alec Mourid7599d82019-05-22 19:58:00 -0700116
117 bool mTraceDetailedInfo = false;
Dan Stoza2713c302018-03-28 17:07:36 -0700118};
119
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700120} // namespace android::scheduler