| 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 |  | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 19 | #include <cinttypes> | 
| 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 |  | 
|  | 24 | namespace android { | 
|  | 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. | 
|  | 34 | const 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. | 
|  | 39 | const int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2; | 
|  | 40 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 41 | public: | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 42 | VSyncModulator(); | 
|  | 43 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 44 | // Wrapper for a collection of surfaceflinger/app offsets for a particular | 
|  | 45 | // configuration . | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 46 | struct Offsets { | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 47 | scheduler::RefreshRateConfigs::RefreshRateType fpsMode; | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 48 | nsecs_t sf; | 
|  | 49 | nsecs_t app; | 
|  | 50 | }; | 
|  | 51 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 52 | enum class OffsetType { | 
|  | 53 | Early, | 
|  | 54 | EarlyGl, | 
|  | 55 | Late, | 
|  | 56 | }; | 
|  | 57 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 58 | // Sets the phase offsets | 
|  | 59 | // | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 60 | // sfEarly: The phase offset when waking up SF early, which happens when marking a transaction | 
|  | 61 | //          as early. May be the same as late, in which case we don't shift offsets. | 
|  | 62 | // sfEarlyGl: Like sfEarly, but only if we used GL composition. If we use both GL composition | 
|  | 63 | //            and the transaction was marked as early, we'll use sfEarly. | 
|  | 64 | // sfLate: The regular SF vsync phase offset. | 
|  | 65 | // appEarly: Like sfEarly, but for the app-vsync | 
|  | 66 | // appEarlyGl: Like sfEarlyGl, but for the app-vsync. | 
|  | 67 | // appLate: The regular app vsync phase offset. | 
| Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 68 | void setPhaseOffsets(Offsets early, Offsets earlyGl, Offsets late, | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 69 | nsecs_t thresholdForNextVsync) EXCLUDES(mMutex); | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 70 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 71 | // Sets handles to the SF and app event threads. | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 72 | void setEventThreads(EventThread* sfEventThread, EventThread* appEventThread) { | 
|  | 73 | mSfEventThread = sfEventThread; | 
|  | 74 | mAppEventThread = appEventThread; | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 77 | // Sets the scheduler and vsync connection handlers. | 
| Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 78 | void setSchedulerAndHandles(Scheduler* scheduler, | 
|  | 79 | Scheduler::ConnectionHandle* appConnectionHandle, | 
|  | 80 | Scheduler::ConnectionHandle* sfConnectionHandle) { | 
|  | 81 | mScheduler = scheduler; | 
|  | 82 | mAppConnectionHandle = appConnectionHandle; | 
|  | 83 | mSfConnectionHandle = sfConnectionHandle; | 
|  | 84 | } | 
|  | 85 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 86 | // Signals that a transaction has started, and changes offsets accordingly. | 
|  | 87 | void setTransactionStart(Scheduler::TransactionStart transactionStart); | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 88 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 89 | // Signals that a transaction has been completed, so that we can finish | 
|  | 90 | // special handling for a transaction. | 
|  | 91 | void onTransactionHandled(); | 
| Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 92 |  | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 93 | // Called when we send a refresh rate change to hardware composer, so that | 
|  | 94 | // we can move into early offsets. | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 95 | void onRefreshRateChangeInitiated(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 96 |  | 
|  | 97 | // Called when we detect from vsync signals that the refresh rate changed. | 
|  | 98 | // 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] | 99 | void onRefreshRateChangeCompleted(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 100 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 101 | // Called when the display is presenting a new frame. usedRenderEngine | 
|  | 102 | // should be set to true if RenderEngine was involved with composing the new | 
|  | 103 | // frame. | 
|  | 104 | void onRefreshed(bool usedRenderEngine); | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 105 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 106 | // Returns the offsets that we are currently using | 
|  | 107 | Offsets getOffsets() EXCLUDES(mMutex); | 
| Ady Abraham | be0f948 | 2019-04-24 15:41:53 -0700 | [diff] [blame] | 108 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 109 | private: | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 110 | // Returns the next offsets that we should be using | 
|  | 111 | Offsets getNextOffsets() REQUIRES(mMutex); | 
|  | 112 | // Returns the next offset type that we should use. | 
|  | 113 | OffsetType getNextOffsetType(); | 
|  | 114 | // Updates offsets and persists them into the scheduler framework. | 
|  | 115 | void updateOffsets() EXCLUDES(mMutex); | 
|  | 116 | void updateOffsetsLocked() REQUIRES(mMutex); | 
| Alec Mouri | b656aed | 2019-06-21 16:27:05 -0700 | [diff] [blame] | 117 | // Updates the internal offsets and offset type. | 
|  | 118 | void flushOffsets() REQUIRES(mMutex); | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 119 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 120 | mutable std::mutex mMutex; | 
|  | 121 | std::unordered_map<OffsetType, Offsets> mOffsetMap GUARDED_BY(mMutex); | 
| Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 122 | nsecs_t mThresholdForNextVsync; | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 123 |  | 
|  | 124 | EventThread* mSfEventThread = nullptr; | 
|  | 125 | EventThread* mAppEventThread = nullptr; | 
|  | 126 |  | 
| Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 127 | Scheduler* mScheduler = nullptr; | 
|  | 128 | Scheduler::ConnectionHandle* mAppConnectionHandle = nullptr; | 
|  | 129 | Scheduler::ConnectionHandle* mSfConnectionHandle = nullptr; | 
|  | 130 |  | 
| Alec Mouri | b656aed | 2019-06-21 16:27:05 -0700 | [diff] [blame] | 131 | Offsets mOffsets GUARDED_BY(mMutex) = {Scheduler::RefreshRateType::DEFAULT, 0, 0}; | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 132 |  | 
| Ana Krulec | 7ecce8c | 2018-10-12 13:44:41 -0700 | [diff] [blame] | 133 | std::atomic<Scheduler::TransactionStart> mTransactionStart = | 
|  | 134 | Scheduler::TransactionStart::NORMAL; | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 135 | std::atomic<bool> mRefreshRateChangePending = false; | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 136 | std::atomic<int> mRemainingEarlyFrameCount = 0; | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 137 | std::atomic<int> mRemainingRenderEngineUsageCount = 0; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 138 |  | 
|  | 139 | bool mTraceDetailedInfo = false; | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 140 | }; | 
|  | 141 |  | 
|  | 142 | } // namespace android |