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