blob: a120e976f02345a0882b5a22e55676c3a3ef6c31 [file] [log] [blame]
Ana Krulec757f63a2019-01-25 10:46:18 -08001/*
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 Mourid7599d82019-05-22 19:58:00 -070019#include <unordered_map>
Ana Krulec757f63a2019-01-25 10:46:18 -080020
Marin Shalamanove8a663d2020-11-24 17:48:00 +010021#include <utils/Timers.h>
22
23#include "Fps.h"
Ana Krulec757f63a2019-01-25 10:46:18 -080024#include "RefreshRateConfigs.h"
Dominik Laskowskia93a5312020-07-23 15:10:03 -070025#include "VsyncModulator.h"
Ana Krulec757f63a2019-01-25 10:46:18 -080026
Dominik Laskowskieddeda12019-07-19 11:54:13 -070027namespace android::scheduler {
Ana Krulec757f63a2019-01-25 10:46:18 -080028
29/*
Ady Abraham8287e852020-08-12 14:44:58 -070030 * This class encapsulates vsync configurations for different refresh rates. Depending
Ana Krulec757f63a2019-01-25 10:46:18 -080031 * 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 Abraham8287e852020-08-12 14:44:58 -070035class VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080036public:
Ady Abraham8287e852020-08-12 14:44:58 -070037 using VsyncConfigSet = VsyncModulator::VsyncConfigSet;
Ana Krulec757f63a2019-01-25 10:46:18 -080038
Ady Abraham8287e852020-08-12 14:44:58 -070039 virtual ~VsyncConfiguration() = default;
40 virtual VsyncConfigSet getCurrentConfigs() const = 0;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010041 virtual VsyncConfigSet getConfigsForRefreshRate(Fps fps) const = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070042
Marin Shalamanove8a663d2020-11-24 17:48:00 +010043 virtual void setRefreshRateFps(Fps fps) = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070044
Ana Krulec757f63a2019-01-25 10:46:18 -080045 virtual void dump(std::string& result) const = 0;
46};
47
48namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070049
Ady Abraham9e16a482019-12-03 17:19:41 -080050/*
Ady Abraham8287e852020-08-12 14:44:58 -070051 * 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 Abraham9e16a482019-12-03 17:19:41 -080054 */
Ady Abraham8287e852020-08-12 14:44:58 -070055class VsyncConfiguration : public scheduler::VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080056public:
Marin Shalamanove8a663d2020-11-24 17:48:00 +010057 explicit VsyncConfiguration(Fps currentFps);
Ady Abraham9e16a482019-12-03 17:19:41 -080058
59 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Marin Shalamanove8a663d2020-11-24 17:48:00 +010060 VsyncConfigSet getConfigsForRefreshRate(Fps fps) const override;
Ady Abraham9e16a482019-12-03 17:19:41 -080061
62 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham8287e852020-08-12 14:44:58 -070063 VsyncConfigSet getCurrentConfigs() const override {
64 return getConfigsForRefreshRate(mRefreshRateFps);
65 }
Ady Abraham9e16a482019-12-03 17:19:41 -080066
67 // This function should be called when the device is switching between different
68 // refresh rates, to properly update the offsets.
Marin Shalamanove8a663d2020-11-24 17:48:00 +010069 void setRefreshRateFps(Fps fps) override { mRefreshRateFps = fps; }
Ady Abraham9e16a482019-12-03 17:19:41 -080070
71 // Returns current offsets in human friendly format.
72 void dump(std::string& result) const override;
73
Ady Abraham60120a02020-03-23 11:23:26 -070074protected:
Marin Shalamanove8a663d2020-11-24 17:48:00 +010075 void initializeOffsets(const std::vector<Fps>& refreshRates);
Ady Abraham8287e852020-08-12 14:44:58 -070076 virtual VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const = 0;
77
Marin Shalamanove8a663d2020-11-24 17:48:00 +010078 std::unordered_map<Fps, VsyncConfigSet, std::hash<Fps>, Fps::EqualsInBuckets> mOffsets;
79 std::atomic<Fps> mRefreshRateFps;
Ady Abraham8287e852020-08-12 14:44:58 -070080};
81
82/*
83 * This is the old implementation of phase offsets and considered as deprecated.
84 * WorkDuration is the new implementation.
85 */
86class PhaseOffsets : public VsyncConfiguration {
87public:
88 explicit PhaseOffsets(const scheduler::RefreshRateConfigs&);
89
90protected:
Ady Abraham60120a02020-03-23 11:23:26 -070091 // Used for unit tests
Marin Shalamanove8a663d2020-11-24 17:48:00 +010092 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 Abraham8287e852020-08-12 14:44:58 -070095 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
101private:
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 Abraham9e16a482019-12-03 17:19:41 -0800106
Ady Abrahamc6c81822020-04-28 10:28:00 -0700107 const nsecs_t mVSyncPhaseOffsetNs;
108 const nsecs_t mSfVSyncPhaseOffsetNs;
109 const std::optional<nsecs_t> mEarlySfOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700110 const std::optional<nsecs_t> mEarlyGpuSfOffsetNs;
Ady Abrahamc6c81822020-04-28 10:28:00 -0700111 const std::optional<nsecs_t> mEarlyAppOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700112 const std::optional<nsecs_t> mEarlyGpuAppOffsetNs;
Ady Abraham9e16a482019-12-03 17:19:41 -0800113
Ady Abraham8287e852020-08-12 14:44:58 -0700114 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 Abraham9e16a482019-12-03 17:19:41 -0800122};
123
124/*
125 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
Ady Abraham8287e852020-08-12 14:44:58 -0700126 * The offsets are calculated from durations for each one of the (late, early, earlyGpu)
Ady Abraham9e16a482019-12-03 17:19:41 -0800127 * offset types.
128 */
Ady Abraham8287e852020-08-12 14:44:58 -0700129class WorkDuration : public VsyncConfiguration {
Ady Abraham9e16a482019-12-03 17:19:41 -0800130public:
Ady Abraham8287e852020-08-12 14:44:58 -0700131 explicit WorkDuration(const scheduler::RefreshRateConfigs&);
Ana Krulec757f63a2019-01-25 10:46:18 -0800132
Ady Abraham9e16a482019-12-03 17:19:41 -0800133protected:
134 // Used for unit tests
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100135 WorkDuration(const std::vector<Fps>& refreshRates, Fps currentFps, nsecs_t sfDuration,
Ady Abraham8287e852020-08-12 14:44:58 -0700136 nsecs_t appDuration, nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration,
137 nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration);
Ady Abraham9e16a482019-12-03 17:19:41 -0800138
Ana Krulec757f63a2019-01-25 10:46:18 -0800139private:
Ady Abraham8287e852020-08-12 14:44:58 -0700140 VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
Ana Krulec757f63a2019-01-25 10:46:18 -0800141
Ady Abraham9e16a482019-12-03 17:19:41 -0800142 const nsecs_t mSfDuration;
143 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700144
Ady Abraham9e16a482019-12-03 17:19:41 -0800145 const nsecs_t mSfEarlyDuration;
146 const nsecs_t mAppEarlyDuration;
147
Ady Abraham8287e852020-08-12 14:44:58 -0700148 const nsecs_t mSfEarlyGpuDuration;
149 const nsecs_t mAppEarlyGpuDuration;
Ana Krulec757f63a2019-01-25 10:46:18 -0800150};
Ana Krulec757f63a2019-01-25 10:46:18 -0800151
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700152} // namespace impl
153} // namespace android::scheduler