| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 19 | #include <chrono> | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 20 | #include <mutex> | 
|  | 21 |  | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 22 | #include "Scheduler.h" | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 23 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 24 | namespace android::scheduler { | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | /* | 
|  | 27 | * Modulates the vsync-offsets depending on current SurfaceFlinger state. | 
|  | 28 | */ | 
|  | 29 | class VSyncModulator { | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 30 | private: | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 31 | // 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 Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 34 | static constexpr int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2; | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 35 |  | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 36 | // 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 Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 39 | static constexpr int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2; | 
|  | 40 |  | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 41 | // Margin used to account for potential data races | 
|  | 42 | static const constexpr std::chrono::nanoseconds MARGIN_FOR_TX_APPLY = 1ms; | 
|  | 43 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 44 | public: | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 45 | // Wrapper for a collection of surfaceflinger/app offsets for a particular | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 46 | // configuration. | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 47 | struct Offsets { | 
|  | 48 | nsecs_t sf; | 
|  | 49 | nsecs_t app; | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | bool operator==(const Offsets& other) const { return sf == other.sf && app == other.app; } | 
|  | 52 |  | 
|  | 53 | bool operator!=(const Offsets& other) const { return !(*this == other); } | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 54 | }; | 
|  | 55 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 56 | struct OffsetsConfig { | 
|  | 57 | Offsets early;   // For transactions with the eEarlyWakeup flag. | 
|  | 58 | Offsets earlyGl; // As above but while compositing with GL. | 
|  | 59 | Offsets late;    // Default. | 
|  | 60 |  | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 61 | bool operator==(const OffsetsConfig& other) const { | 
|  | 62 | return early == other.early && earlyGl == other.earlyGl && late == other.late; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | bool operator!=(const OffsetsConfig& other) const { return !(*this == other); } | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 66 | }; | 
|  | 67 |  | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 68 | VSyncModulator(IPhaseOffsetControl&, ConnectionHandle appConnectionHandle, | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 69 | ConnectionHandle sfConnectionHandle, const OffsetsConfig&); | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 70 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 71 | void setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex); | 
| Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 72 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 73 | // Signals that a transaction has started, and changes offsets accordingly. | 
|  | 74 | void setTransactionStart(Scheduler::TransactionStart transactionStart); | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 75 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 76 | // Signals that a transaction has been completed, so that we can finish | 
|  | 77 | // special handling for a transaction. | 
|  | 78 | void onTransactionHandled(); | 
| Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 79 |  | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 80 | // Called when we send a refresh rate change to hardware composer, so that | 
|  | 81 | // we can move into early offsets. | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 82 | void onRefreshRateChangeInitiated(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 83 |  | 
|  | 84 | // Called when we detect from vsync signals that the refresh rate changed. | 
|  | 85 | // This way we can move out of early offsets if no longer necessary. | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 86 | void onRefreshRateChangeCompleted(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 87 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 88 | // Called when the display is presenting a new frame. usedRenderEngine | 
|  | 89 | // should be set to true if RenderEngine was involved with composing the new | 
|  | 90 | // frame. | 
|  | 91 | void onRefreshed(bool usedRenderEngine); | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 92 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 93 | // Returns the offsets that we are currently using | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 94 | Offsets getOffsets() const EXCLUDES(mMutex); | 
| Ady Abraham | be0f948 | 2019-04-24 15:41:53 -0700 | [diff] [blame] | 95 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 96 | private: | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 97 | friend class VSyncModulatorTest; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 98 | // Returns the next offsets that we should be using | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 99 | const Offsets& getNextOffsets() const REQUIRES(mMutex); | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 100 | // Updates offsets and persists them into the scheduler framework. | 
|  | 101 | void updateOffsets() EXCLUDES(mMutex); | 
|  | 102 | void updateOffsetsLocked() REQUIRES(mMutex); | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 103 |  | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 104 | IPhaseOffsetControl& mPhaseOffsetControl; | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 105 | const ConnectionHandle mAppConnectionHandle; | 
|  | 106 | const ConnectionHandle mSfConnectionHandle; | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 107 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 108 | mutable std::mutex mMutex; | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 109 | OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex); | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 110 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 111 | Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late}; | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 112 |  | 
| Ana Krulec | 7ecce8c | 2018-10-12 13:44:41 -0700 | [diff] [blame] | 113 | std::atomic<Scheduler::TransactionStart> mTransactionStart = | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 114 | Scheduler::TransactionStart::Normal; | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 115 | std::atomic<bool> mRefreshRateChangePending = false; | 
| Ady Abraham | bf1349c | 2020-06-12 14:26:18 -0700 | [diff] [blame] | 116 | std::atomic<bool> mExplicitEarlyWakeup = false; | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 117 | std::atomic<int> mRemainingEarlyFrameCount = 0; | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 118 | std::atomic<int> mRemainingRenderEngineUsageCount = 0; | 
| Alec Mouri | f792cfe | 2020-06-05 13:11:23 -0700 | [diff] [blame] | 119 | std::atomic<std::chrono::steady_clock::time_point> mEarlyTxnStartTime = {}; | 
|  | 120 | std::atomic<std::chrono::steady_clock::time_point> mTxnAppliedTime = {}; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | bool mTraceDetailedInfo = false; | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 123 | }; | 
|  | 124 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 125 | } // namespace android::scheduler |