| 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 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 19 | #include <mutex> | 
|  | 20 |  | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 21 | #include "Scheduler.h" | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 22 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 23 | namespace android::scheduler { | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 24 |  | 
|  | 25 | /* | 
|  | 26 | * Modulates the vsync-offsets depending on current SurfaceFlinger state. | 
|  | 27 | */ | 
|  | 28 | class VSyncModulator { | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 29 | private: | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 30 | // 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 Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 33 | static constexpr int MIN_EARLY_FRAME_COUNT_TRANSACTION = 2; | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 34 |  | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 35 | // 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 Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 38 | static constexpr int MIN_EARLY_GL_FRAME_COUNT_TRANSACTION = 2; | 
|  | 39 |  | 
|  | 40 | using RefreshRateType = RefreshRateConfigs::RefreshRateType; | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 41 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 42 | public: | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 43 | // Wrapper for a collection of surfaceflinger/app offsets for a particular | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 44 | // configuration. | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 45 | struct Offsets { | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 46 | RefreshRateType fpsMode; | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 47 | nsecs_t sf; | 
|  | 48 | nsecs_t app; | 
|  | 49 | }; | 
|  | 50 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 51 | 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 Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 57 | }; | 
|  | 58 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 59 | VSyncModulator(Scheduler&, ConnectionHandle appConnectionHandle, | 
|  | 60 | ConnectionHandle sfConnectionHandle, const OffsetsConfig&); | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 61 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 62 | void setPhaseOffsets(const OffsetsConfig&) EXCLUDES(mMutex); | 
| Ana Krulec | 12096d0 | 2018-09-07 14:54:14 -0700 | [diff] [blame] | 63 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 64 | // Signals that a transaction has started, and changes offsets accordingly. | 
|  | 65 | void setTransactionStart(Scheduler::TransactionStart transactionStart); | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 66 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 67 | // Signals that a transaction has been completed, so that we can finish | 
|  | 68 | // special handling for a transaction. | 
|  | 69 | void onTransactionHandled(); | 
| Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 70 |  | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 71 | // Called when we send a refresh rate change to hardware composer, so that | 
|  | 72 | // we can move into early offsets. | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 73 | void onRefreshRateChangeInitiated(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 74 |  | 
|  | 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 Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 77 | void onRefreshRateChangeCompleted(); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 78 |  | 
| Alec Mouri | b488afa | 2019-05-23 10:22:24 -0700 | [diff] [blame] | 79 | // 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 Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 83 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 84 | // Returns the offsets that we are currently using | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 85 | Offsets getOffsets() const EXCLUDES(mMutex); | 
| Ady Abraham | be0f948 | 2019-04-24 15:41:53 -0700 | [diff] [blame] | 86 |  | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 87 | private: | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 88 | // Returns the next offsets that we should be using | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 89 | const Offsets& getNextOffsets() const REQUIRES(mMutex); | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 90 | // Updates offsets and persists them into the scheduler framework. | 
|  | 91 | void updateOffsets() EXCLUDES(mMutex); | 
|  | 92 | void updateOffsetsLocked() REQUIRES(mMutex); | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 93 |  | 
|  | 94 | Scheduler& mScheduler; | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 95 | const ConnectionHandle mAppConnectionHandle; | 
|  | 96 | const ConnectionHandle mSfConnectionHandle; | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 97 |  | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 98 | mutable std::mutex mMutex; | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 99 | OffsetsConfig mOffsetsConfig GUARDED_BY(mMutex); | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 100 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 101 | Offsets mOffsets GUARDED_BY(mMutex){mOffsetsConfig.late}; | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 102 |  | 
| Ana Krulec | 7ecce8c | 2018-10-12 13:44:41 -0700 | [diff] [blame] | 103 | std::atomic<Scheduler::TransactionStart> mTransactionStart = | 
|  | 104 | Scheduler::TransactionStart::NORMAL; | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 105 | std::atomic<bool> mRefreshRateChangePending = false; | 
| Jorim Jaggi | 9053521 | 2018-05-23 23:44:06 +0200 | [diff] [blame] | 106 | std::atomic<int> mRemainingEarlyFrameCount = 0; | 
| Alec Mouri | 5a10210 | 2019-05-23 07:14:20 -0700 | [diff] [blame] | 107 | std::atomic<int> mRemainingRenderEngineUsageCount = 0; | 
| Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | bool mTraceDetailedInfo = false; | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 110 | }; | 
|  | 111 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 112 | } // namespace android::scheduler |