blob: 636c8c8b28f455d21378cd24d86f84270bde0d27 [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
40 using RefreshRateType = RefreshRateConfigs::RefreshRateType;
Alec Mouri5a102102019-05-23 07:14:20 -070041
Dan Stoza2713c302018-03-28 17:07:36 -070042public:
Alec Mourib488afa2019-05-23 10:22:24 -070043 // Wrapper for a collection of surfaceflinger/app offsets for a particular
Dominik Laskowskieddeda12019-07-19 11:54:13 -070044 // configuration.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020045 struct Offsets {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070046 RefreshRateType fpsMode;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020047 nsecs_t sf;
48 nsecs_t app;
49 };
50
Dominik Laskowskieddeda12019-07-19 11:54:13 -070051 struct OffsetsConfig {
52 Offsets early; // For transactions with the eEarlyWakeup flag.
53 Offsets earlyGl; // As above but while compositing with GL.
54 Offsets late; // Default.
55
56 nsecs_t thresholdForNextVsync;
Alec Mourid7599d82019-05-22 19:58:00 -070057 };
58
Dominik Laskowskieddeda12019-07-19 11:54:13 -070059 VSyncModulator(Scheduler&, const sp<Scheduler::ConnectionHandle>& appConnectionHandle,
60 const sp<Scheduler::ConnectionHandle>& sfConnectionHandle, const OffsetsConfig&);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020061
Dominik Laskowskieddeda12019-07-19 11:54:13 -070062 void setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex);
Ana Krulec12096d02018-09-07 14:54:14 -070063
Alec Mourib488afa2019-05-23 10:22:24 -070064 // Signals that a transaction has started, and changes offsets accordingly.
65 void setTransactionStart(Scheduler::TransactionStart transactionStart);
Jorim Jaggi90535212018-05-23 23:44:06 +020066
Alec Mourib488afa2019-05-23 10:22:24 -070067 // Signals that a transaction has been completed, so that we can finish
68 // special handling for a transaction.
69 void onTransactionHandled();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010070
Alec Mouri754c98a2019-03-18 18:53:42 -070071 // Called when we send a refresh rate change to hardware composer, so that
72 // we can move into early offsets.
Alec Mourib488afa2019-05-23 10:22:24 -070073 void onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070074
75 // Called when we detect from vsync signals that the refresh rate changed.
76 // This way we can move out of early offsets if no longer necessary.
Alec Mourib488afa2019-05-23 10:22:24 -070077 void onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070078
Alec Mourib488afa2019-05-23 10:22:24 -070079 // Called when the display is presenting a new frame. usedRenderEngine
80 // should be set to true if RenderEngine was involved with composing the new
81 // frame.
82 void onRefreshed(bool usedRenderEngine);
Alec Mouri5a102102019-05-23 07:14:20 -070083
Alec Mourid7599d82019-05-22 19:58:00 -070084 // Returns the offsets that we are currently using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070085 Offsets getOffsets() const EXCLUDES(mMutex);
Ady Abrahambe0f9482019-04-24 15:41:53 -070086
Dan Stoza2713c302018-03-28 17:07:36 -070087private:
Alec Mourid7599d82019-05-22 19:58:00 -070088 // Returns the next offsets that we should be using
Dominik Laskowskieddeda12019-07-19 11:54:13 -070089 const Offsets& getNextOffsets() const REQUIRES(mMutex);
Alec Mourid7599d82019-05-22 19:58:00 -070090 // Updates offsets and persists them into the scheduler framework.
91 void updateOffsets() EXCLUDES(mMutex);
92 void updateOffsetsLocked() REQUIRES(mMutex);
Dominik Laskowskieddeda12019-07-19 11:54:13 -070093
94 Scheduler& mScheduler;
95 const sp<Scheduler::ConnectionHandle> mAppConnectionHandle;
96 const sp<Scheduler::ConnectionHandle> mSfConnectionHandle;
Dan Stoza2713c302018-03-28 17:07:36 -070097
Alec Mourid7599d82019-05-22 19:58:00 -070098 mutable std::mutex mMutex;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070099 OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex);
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200100
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700101 Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late};
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200102
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700103 std::atomic<Scheduler::TransactionStart> mTransactionStart =
104 Scheduler::TransactionStart::NORMAL;
Alec Mouri754c98a2019-03-18 18:53:42 -0700105 std::atomic<bool> mRefreshRateChangePending = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200106 std::atomic<int> mRemainingEarlyFrameCount = 0;
Alec Mouri5a102102019-05-23 07:14:20 -0700107 std::atomic<int> mRemainingRenderEngineUsageCount = 0;
Alec Mourid7599d82019-05-22 19:58:00 -0700108
109 bool mTraceDetailedInfo = false;
Dan Stoza2713c302018-03-28 17:07:36 -0700110};
111
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700112} // namespace android::scheduler