blob: 677862e4fe0e9763f3a92dec73dfa34238453b2d [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
Ana Krulec757f63a2019-01-25 10:46:18 -080019#include <cinttypes>
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
24namespace android {
25
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.
34 const 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.
39 const 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
43 // configuration .
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020044 struct Offsets {
45 nsecs_t sf;
46 nsecs_t app;
47 };
48
Dan Stoza2713c302018-03-28 17:07:36 -070049 // Sets the phase offsets
50 //
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020051 // sfEarly: The phase offset when waking up SF early, which happens when marking a transaction
52 // as early. May be the same as late, in which case we don't shift offsets.
53 // sfEarlyGl: Like sfEarly, but only if we used GL composition. If we use both GL composition
54 // and the transaction was marked as early, we'll use sfEarly.
55 // sfLate: The regular SF vsync phase offset.
56 // appEarly: Like sfEarly, but for the app-vsync
57 // appEarlyGl: Like sfEarlyGl, but for the app-vsync.
58 // appLate: The regular app vsync phase offset.
Ady Abraham45e4e362019-06-07 18:20:51 -070059 void setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late,
Alec Mourib488afa2019-05-23 10:22:24 -070060 nsecs_t thresholdForNextVsync);
Ady Abrahamf6656352019-02-14 16:29:49 -080061
Alec Mourib488afa2019-05-23 10:22:24 -070062 // Returns the configured early offsets.
Ana Krulecfefcb582018-08-07 14:22:37 -070063 Offsets getEarlyOffsets() const { return mEarlyOffsets; }
Dan Stoza2713c302018-03-28 17:07:36 -070064
Alec Mourib488afa2019-05-23 10:22:24 -070065 // Returns the configured early gl offsets.
Ana Krulecfefcb582018-08-07 14:22:37 -070066 Offsets getEarlyGlOffsets() const { return mEarlyGlOffsets; }
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020067
Alec Mourib488afa2019-05-23 10:22:24 -070068 // Sets handles to the SF and app event threads.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +020069 void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) {
70 mSfEventThread = sfEventThread;
71 mAppEventThread = appEventThread;
Dan Stoza2713c302018-03-28 17:07:36 -070072 }
73
Alec Mourib488afa2019-05-23 10:22:24 -070074 // Sets the scheduler and vsync connection handlers.
Ana Krulec12096d02018-09-07 14:54:14 -070075 void setSchedulerAndHandles(Scheduler* scheduler,
76 Scheduler::ConnectionHandle* appConnectionHandle,
77 Scheduler::ConnectionHandle* sfConnectionHandle) {
78 mScheduler = scheduler;
79 mAppConnectionHandle = appConnectionHandle;
80 mSfConnectionHandle = sfConnectionHandle;
81 }
82
Alec Mourib488afa2019-05-23 10:22:24 -070083 // Signals that a transaction has started, and changes offsets accordingly.
84 void setTransactionStart(Scheduler::TransactionStart transactionStart);
Jorim Jaggi90535212018-05-23 23:44:06 +020085
Alec Mourib488afa2019-05-23 10:22:24 -070086 // Signals that a transaction has been completed, so that we can finish
87 // special handling for a transaction.
88 void onTransactionHandled();
Jorim Jaggif15c3be2018-04-12 12:56:58 +010089
Alec Mouri754c98a2019-03-18 18:53:42 -070090 // Called when we send a refresh rate change to hardware composer, so that
91 // we can move into early offsets.
Alec Mourib488afa2019-05-23 10:22:24 -070092 void onRefreshRateChangeInitiated();
Alec Mouri754c98a2019-03-18 18:53:42 -070093
94 // Called when we detect from vsync signals that the refresh rate changed.
95 // This way we can move out of early offsets if no longer necessary.
Alec Mourib488afa2019-05-23 10:22:24 -070096 void onRefreshRateChangeCompleted();
Alec Mouri754c98a2019-03-18 18:53:42 -070097
Alec Mourib488afa2019-05-23 10:22:24 -070098 // Called when the display is presenting a new frame. usedRenderEngine
99 // should be set to true if RenderEngine was involved with composing the new
100 // frame.
101 void onRefreshed(bool usedRenderEngine);
Alec Mouri5a102102019-05-23 07:14:20 -0700102
Alec Mourib488afa2019-05-23 10:22:24 -0700103 // Returns the offsets that should be used.
104 Offsets getOffsets();
Ady Abrahambe0f9482019-04-24 15:41:53 -0700105
Dan Stoza2713c302018-03-28 17:07:36 -0700106private:
Alec Mourib488afa2019-05-23 10:22:24 -0700107 void updateOffsets();
Dan Stoza2713c302018-03-28 17:07:36 -0700108
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200109 Offsets mLateOffsets;
110 Offsets mEarlyOffsets;
111 Offsets mEarlyGlOffsets;
Ady Abraham45e4e362019-06-07 18:20:51 -0700112 nsecs_t mThresholdForNextVsync;
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200113
114 EventThread* mSfEventThread = nullptr;
115 EventThread* mAppEventThread = nullptr;
116
Ana Krulec12096d02018-09-07 14:54:14 -0700117 Scheduler* mScheduler = nullptr;
118 Scheduler::ConnectionHandle* mAppConnectionHandle = nullptr;
119 Scheduler::ConnectionHandle* mSfConnectionHandle = nullptr;
120
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200121 std::atomic<Offsets> mOffsets;
122
Ana Krulec7ecce8c2018-10-12 13:44:41 -0700123 std::atomic<Scheduler::TransactionStart> mTransactionStart =
124 Scheduler::TransactionStart::NORMAL;
Alec Mouri754c98a2019-03-18 18:53:42 -0700125 std::atomic<bool> mRefreshRateChangePending = false;
Jorim Jaggi90535212018-05-23 23:44:06 +0200126 std::atomic<int> mRemainingEarlyFrameCount = 0;
Alec Mouri5a102102019-05-23 07:14:20 -0700127 std::atomic<int> mRemainingRenderEngineUsageCount = 0;
Dan Stoza2713c302018-03-28 17:07:36 -0700128};
129
130} // namespace android