blob: 63c0feb9ccf41d356e95d0b056a62af56e55dc9c [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
Dan Stoza2713c302018-03-28 17:07:36 -070019#include <mutex>
20
Ana Krulec757f63a2019-01-25 10:46:18 -080021#include "Scheduler.h"
Dan Stoza2713c302018-03-28 17:07:36 -070022
Dominik Laskowskieddeda12019-07-19 11:54:13 -070023namespace android::scheduler {
Dan Stoza2713c302018-03-28 17:07:36 -070024
25/*
26 * Modulates the vsync-offsets depending on current SurfaceFlinger state.
27 */
28class VSyncModulator {
Jorim Jaggi90535212018-05-23 23:44:06 +020029private:
Alec Mouri754c98a2019-03-18 18:53:42 -070030 // Number of frames we'll keep the early phase offsets once they are activated for a
31 // transaction. This acts as a low-pass filter in case the client isn't quick enough in
32 // sending new transactions.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070033 static constexpr int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2;
Jorim Jaggi90535212018-05-23 23:44:06 +020034
Alec Mouri5a102102019-05-23 07:14:20 -070035 // Number of frames we'll keep the early gl phase offsets once they are activated.
36 // This acts as a low-pass filter to avoid scenarios where we rapidly
37 // switch in and out of gl composition.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070038 static constexpr int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2;
39
Dan Stoza2713c302018-03-28 17:07:36 -070040public:
Alec Mourib488afa2019-05-23 10:22:24 -070041 // Wrapper for a collection of surfaceflinger/app offsets for a particular
Dominik Laskowskieddeda12019-07-19 11:54:13 -070042 // configuration.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020043 struct Offsets {
44 nsecs_t sf;
45 nsecs_t app;
46 };
47
Dominik Laskowskieddeda12019-07-19 11:54:13 -070048 struct OffsetsConfig {
49 Offsets early; // For transactions with the eEarlyWakeup flag.
50 Offsets earlyGl; // As above but while compositing with GL.
51 Offsets late; // Default.
52
53 nsecs_t thresholdForNextVsync;
Alec Mourid7599d82019-05-22 19:58:00 -070054 };
55
Dominik Laskowski98041832019-08-01 18:35:59 -070056 VSyncModulator(Scheduler&, ConnectionHandle appConnectionHandle,
57 ConnectionHandle sfConnectionHandle, const OffsetsConfig&);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020058
Dominik Laskowskieddeda12019-07-19 11:54:13 -070059 void setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex);
Ana Krulec12096d02018-09-07 14:54:14 -070060
Alec Mourib488afa2019-05-23 10:22:24 -070061 // Signals that a transaction has started, and changes offsets accordingly.
62 void setTransactionStart(Scheduler::TransactionStart transactionStart);
Jorim Jaggi90535212018-05-23 23:44:06 +020063
Alec Mourib488afa2019-05-23 10:22:24 -070064 // Signals that a transaction has been completed, so that we can finish
65 // special handling for a transaction.
66 void onTransactionHandled();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010067
Alec Mouri754c98a2019-03-18 18:53:42 -070068 // Called when we send a refresh rate change to hardware composer, so that
69 // we can move into early offsets.
Alec Mourib488afa2019-05-23 10:22:24 -070070 void onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070071
72 // Called when we detect from vsync signals that the refresh rate changed.
73 // This way we can move out of early offsets if no longer necessary.
Alec Mourib488afa2019-05-23 10:22:24 -070074 void onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070075
Alec Mourib488afa2019-05-23 10:22:24 -070076 // Called when the display is presenting a new frame. usedRenderEngine
77 // should be set to true if RenderEngine was involved with composing the new
78 // frame.
79 void onRefreshed(bool usedRenderEngine);
Alec Mouri5a102102019-05-23 07:14:20 -070080
Alec Mourid7599d82019-05-22 19:58:00 -070081 // Returns the offsets that we are currently using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070082 Offsets getOffsets() const EXCLUDES(mMutex);
Ady Abrahambe0f9482019-04-24 15:41:53 -070083
Dan Stoza2713c302018-03-28 17:07:36 -070084private:
Alec Mourid7599d82019-05-22 19:58:00 -070085 // Returns the next offsets that we should be using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070086 const Offsets& getNextOffsets() const REQUIRES(mMutex);
Alec Mourid7599d82019-05-22 19:58:00 -070087 // Updates offsets and persists them into the scheduler framework.
88 void updateOffsets() EXCLUDES(mMutex);
89 void updateOffsetsLocked() REQUIRES(mMutex);
Dominik Laskowskieddeda12019-07-19 11:54:13 -070090
91 Scheduler& mScheduler;
Dominik Laskowski98041832019-08-01 18:35:59 -070092 const ConnectionHandle mAppConnectionHandle;
93 const ConnectionHandle mSfConnectionHandle;
Dan Stoza2713c302018-03-28 17:07:36 -070094
Alec Mourid7599d82019-05-22 19:58:00 -070095 mutable std::mutex mMutex;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070096 OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020097
Dominik Laskowskieddeda12019-07-19 11:54:13 -070098 Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020099
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700100 std::atomic<Scheduler::TransactionStart> mTransactionStart =
101 Scheduler::TransactionStart::NORMAL;
Alec Mouri754c98a2019-03-18 18:53:42 -0700102 std::atomic<bool> mRefreshRateChangePending = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200103 std::atomic<int> mRemainingEarlyFrameCount = 0;
Alec Mouri5a102102019-05-23 07:14:20 -0700104 std::atomic<int> mRemainingRenderEngineUsageCount = 0;
Alec Mourid7599d82019-05-22 19:58:00 -0700105
106 bool mTraceDetailedInfo = false;
Dan Stoza2713c302018-03-28 17:07:36 -0700107};
108
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700109} // namespace android::scheduler