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