| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2019 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 | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 19 | #include <unordered_map> | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 20 |  | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 21 | #include <utils/Timers.h> | 
|  | 22 |  | 
|  | 23 | #include "Fps.h" | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 24 | #include "RefreshRateConfigs.h" | 
| Dominik Laskowski | a93a531 | 2020-07-23 15:10:03 -0700 | [diff] [blame] | 25 | #include "VsyncModulator.h" | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 26 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 27 | namespace android::scheduler { | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 28 |  | 
|  | 29 | /* | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 30 | * This class encapsulates vsync configurations for different refresh rates. Depending | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 31 | * on what refresh rate we are using, and wheter we are composing in GL, | 
|  | 32 | * different offsets will help us with latency. This class keeps track of | 
|  | 33 | * which mode the device is on, and returns approprate offsets when needed. | 
|  | 34 | */ | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 35 | class VsyncConfiguration { | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 36 | public: | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 37 | using VsyncConfigSet = VsyncModulator::VsyncConfigSet; | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 38 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 39 | virtual ~VsyncConfiguration() = default; | 
|  | 40 | virtual VsyncConfigSet getCurrentConfigs() const = 0; | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 41 | virtual VsyncConfigSet getConfigsForRefreshRate(Fps fps) const = 0; | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 42 |  | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 43 | virtual void setRefreshRateFps(Fps fps) = 0; | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 44 |  | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 45 | virtual void dump(std::string& result) const = 0; | 
|  | 46 | }; | 
|  | 47 |  | 
|  | 48 | namespace impl { | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 49 |  | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 50 | /* | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 51 | * This is a common implementation for both phase offsets and durations. | 
|  | 52 | * PhaseOffsets and WorkDuration derive from this class and implement the | 
|  | 53 | * constructOffsets method | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 54 | */ | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 55 | class VsyncConfiguration : public scheduler::VsyncConfiguration { | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 56 | public: | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 57 | explicit VsyncConfiguration(Fps currentFps); | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 58 |  | 
|  | 59 | // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 60 | VsyncConfigSet getConfigsForRefreshRate(Fps fps) const override; | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 61 |  | 
|  | 62 | // Returns early, early GL, and late offsets for Apps and SF. | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 63 | VsyncConfigSet getCurrentConfigs() const override { | 
|  | 64 | return getConfigsForRefreshRate(mRefreshRateFps); | 
|  | 65 | } | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 66 |  | 
|  | 67 | // This function should be called when the device is switching between different | 
|  | 68 | // refresh rates, to properly update the offsets. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 69 | void setRefreshRateFps(Fps fps) override { mRefreshRateFps = fps; } | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 70 |  | 
|  | 71 | // Returns current offsets in human friendly format. | 
|  | 72 | void dump(std::string& result) const override; | 
|  | 73 |  | 
| Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 74 | protected: | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 75 | void initializeOffsets(const std::vector<Fps>& refreshRates); | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 76 | virtual VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const = 0; | 
|  | 77 |  | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 78 | std::unordered_map<Fps, VsyncConfigSet, std::hash<Fps>, Fps::EqualsInBuckets> mOffsets; | 
|  | 79 | std::atomic<Fps> mRefreshRateFps; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 80 | }; | 
|  | 81 |  | 
|  | 82 | /* | 
|  | 83 | * This is the old implementation of phase offsets and considered as deprecated. | 
|  | 84 | * WorkDuration is the new implementation. | 
|  | 85 | */ | 
|  | 86 | class PhaseOffsets : public VsyncConfiguration { | 
|  | 87 | public: | 
|  | 88 | explicit PhaseOffsets(const scheduler::RefreshRateConfigs&); | 
|  | 89 |  | 
|  | 90 | protected: | 
| Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 91 | // Used for unit tests | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 92 | PhaseOffsets(const std::vector<Fps>& refreshRates, Fps currentFps, nsecs_t vsyncPhaseOffsetNs, | 
|  | 93 | nsecs_t sfVSyncPhaseOffsetNs, std::optional<nsecs_t> earlySfOffsetNs, | 
|  | 94 | std::optional<nsecs_t> earlyGpuSfOffsetNs, std::optional<nsecs_t> earlyAppOffsetNs, | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 95 | std::optional<nsecs_t> earlyGpuAppOffsetNs, nsecs_t highFpsVsyncPhaseOffsetNs, | 
|  | 96 | nsecs_t highFpsSfVSyncPhaseOffsetNs, std::optional<nsecs_t> highFpsEarlySfOffsetNs, | 
|  | 97 | std::optional<nsecs_t> highFpsEarlyGpuSfOffsetNs, | 
|  | 98 | std::optional<nsecs_t> highFpsEarlyAppOffsetNs, | 
|  | 99 | std::optional<nsecs_t> highFpsEarlyGpuAppOffsetNs, nsecs_t thresholdForNextVsync); | 
|  | 100 |  | 
|  | 101 | private: | 
|  | 102 | VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override; | 
|  | 103 |  | 
|  | 104 | VsyncConfigSet getDefaultOffsets(nsecs_t vsyncPeriod) const; | 
|  | 105 | VsyncConfigSet getHighFpsOffsets(nsecs_t vsyncPeriod) const; | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 106 |  | 
| Ady Abraham | c6c8182 | 2020-04-28 10:28:00 -0700 | [diff] [blame] | 107 | const nsecs_t mVSyncPhaseOffsetNs; | 
|  | 108 | const nsecs_t mSfVSyncPhaseOffsetNs; | 
|  | 109 | const std::optional<nsecs_t> mEarlySfOffsetNs; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 110 | const std::optional<nsecs_t> mEarlyGpuSfOffsetNs; | 
| Ady Abraham | c6c8182 | 2020-04-28 10:28:00 -0700 | [diff] [blame] | 111 | const std::optional<nsecs_t> mEarlyAppOffsetNs; | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 112 | const std::optional<nsecs_t> mEarlyGpuAppOffsetNs; | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 113 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 114 | const nsecs_t mHighFpsVSyncPhaseOffsetNs; | 
|  | 115 | const nsecs_t mHighFpsSfVSyncPhaseOffsetNs; | 
|  | 116 | const std::optional<nsecs_t> mHighFpsEarlySfOffsetNs; | 
|  | 117 | const std::optional<nsecs_t> mHighFpsEarlyGpuSfOffsetNs; | 
|  | 118 | const std::optional<nsecs_t> mHighFpsEarlyAppOffsetNs; | 
|  | 119 | const std::optional<nsecs_t> mHighFpsEarlyGpuAppOffsetNs; | 
|  | 120 |  | 
|  | 121 | const nsecs_t mThresholdForNextVsync; | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 122 | }; | 
|  | 123 |  | 
|  | 124 | /* | 
|  | 125 | * Class that encapsulates the phase offsets for SurfaceFlinger and App. | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 126 | * The offsets are calculated from durations for each one of the (late, early, earlyGpu) | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 127 | * offset types. | 
|  | 128 | */ | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 129 | class WorkDuration : public VsyncConfiguration { | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 130 | public: | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 131 | explicit WorkDuration(const scheduler::RefreshRateConfigs&); | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 132 |  | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 133 | protected: | 
|  | 134 | // Used for unit tests | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 135 | WorkDuration(const std::vector<Fps>& refreshRates, Fps currentFps, nsecs_t sfDuration, | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 136 | nsecs_t appDuration, nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration, | 
|  | 137 | nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration); | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 138 |  | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 139 | private: | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 140 | VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override; | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 141 |  | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 142 | const nsecs_t mSfDuration; | 
|  | 143 | const nsecs_t mAppDuration; | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 144 |  | 
| Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 145 | const nsecs_t mSfEarlyDuration; | 
|  | 146 | const nsecs_t mAppEarlyDuration; | 
|  | 147 |  | 
| Ady Abraham | 8287e85 | 2020-08-12 14:44:58 -0700 | [diff] [blame] | 148 | const nsecs_t mSfEarlyGpuDuration; | 
|  | 149 | const nsecs_t mAppEarlyGpuDuration; | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 150 | }; | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 151 |  | 
| Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 152 | } // namespace impl | 
|  | 153 | } // namespace android::scheduler |