blob: c27a25d738488440bb006ca89360d87b545cd4e5 [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
21#include "RefreshRateConfigs.h"
Dominik Laskowskia93a5312020-07-23 15:10:03 -070022#include "VsyncModulator.h"
Ana Krulec757f63a2019-01-25 10:46:18 -080023
Dominik Laskowskieddeda12019-07-19 11:54:13 -070024namespace android::scheduler {
Ana Krulec757f63a2019-01-25 10:46:18 -080025
26/*
Ady Abraham8287e852020-08-12 14:44:58 -070027 * This class encapsulates vsync configurations for different refresh rates. Depending
Ana Krulec757f63a2019-01-25 10:46:18 -080028 * 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 Abraham8287e852020-08-12 14:44:58 -070032class VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080033public:
Ady Abraham8287e852020-08-12 14:44:58 -070034 using VsyncConfigSet = VsyncModulator::VsyncConfigSet;
Ana Krulec757f63a2019-01-25 10:46:18 -080035
Ady Abraham8287e852020-08-12 14:44:58 -070036 virtual ~VsyncConfiguration() = default;
37 virtual VsyncConfigSet getCurrentConfigs() const = 0;
38 virtual VsyncConfigSet getConfigsForRefreshRate(float fps) const = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070039
Ady Abraham2139f732019-11-13 18:56:40 -080040 virtual void setRefreshRateFps(float fps) = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070041
Ana Krulec757f63a2019-01-25 10:46:18 -080042 virtual void dump(std::string& result) const = 0;
43};
44
45namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070046
Ady Abraham9e16a482019-12-03 17:19:41 -080047/*
Ady Abraham8287e852020-08-12 14:44:58 -070048 * 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 Abraham9e16a482019-12-03 17:19:41 -080051 */
Ady Abraham8287e852020-08-12 14:44:58 -070052class VsyncConfiguration : public scheduler::VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080053public:
Ady Abraham8287e852020-08-12 14:44:58 -070054 explicit VsyncConfiguration(float currentFps);
Ady Abraham9e16a482019-12-03 17:19:41 -080055
56 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Ady Abraham8287e852020-08-12 14:44:58 -070057 VsyncConfigSet getConfigsForRefreshRate(float fps) const override;
Ady Abraham9e16a482019-12-03 17:19:41 -080058
59 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham8287e852020-08-12 14:44:58 -070060 VsyncConfigSet getCurrentConfigs() const override {
61 return getConfigsForRefreshRate(mRefreshRateFps);
62 }
Ady Abraham9e16a482019-12-03 17:19:41 -080063
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 Abraham60120a02020-03-23 11:23:26 -070071protected:
Ady Abraham8287e852020-08-12 14:44:58 -070072 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 */
83class PhaseOffsets : public VsyncConfiguration {
84public:
85 explicit PhaseOffsets(const scheduler::RefreshRateConfigs&);
86
87protected:
Ady Abraham60120a02020-03-23 11:23:26 -070088 // Used for unit tests
89 PhaseOffsets(const std::vector<float>& refreshRates, float currentFps,
Ady Abrahamc6c81822020-04-28 10:28:00 -070090 nsecs_t vsyncPhaseOffsetNs, nsecs_t sfVSyncPhaseOffsetNs,
Ady Abraham8287e852020-08-12 14:44:58 -070091 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
99private:
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 Abraham9e16a482019-12-03 17:19:41 -0800104
Ady Abrahamc6c81822020-04-28 10:28:00 -0700105 const nsecs_t mVSyncPhaseOffsetNs;
106 const nsecs_t mSfVSyncPhaseOffsetNs;
107 const std::optional<nsecs_t> mEarlySfOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700108 const std::optional<nsecs_t> mEarlyGpuSfOffsetNs;
Ady Abrahamc6c81822020-04-28 10:28:00 -0700109 const std::optional<nsecs_t> mEarlyAppOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700110 const std::optional<nsecs_t> mEarlyGpuAppOffsetNs;
Ady Abraham9e16a482019-12-03 17:19:41 -0800111
Ady Abraham8287e852020-08-12 14:44:58 -0700112 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 Abraham9e16a482019-12-03 17:19:41 -0800120};
121
122/*
123 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
Ady Abraham8287e852020-08-12 14:44:58 -0700124 * The offsets are calculated from durations for each one of the (late, early, earlyGpu)
Ady Abraham9e16a482019-12-03 17:19:41 -0800125 * offset types.
126 */
Ady Abraham8287e852020-08-12 14:44:58 -0700127class WorkDuration : public VsyncConfiguration {
Ady Abraham9e16a482019-12-03 17:19:41 -0800128public:
Ady Abraham8287e852020-08-12 14:44:58 -0700129 explicit WorkDuration(const scheduler::RefreshRateConfigs&);
Ana Krulec757f63a2019-01-25 10:46:18 -0800130
Ady Abraham9e16a482019-12-03 17:19:41 -0800131protected:
132 // Used for unit tests
Ady Abraham8287e852020-08-12 14:44:58 -0700133 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 Abraham9e16a482019-12-03 17:19:41 -0800136
Ana Krulec757f63a2019-01-25 10:46:18 -0800137private:
Ady Abraham8287e852020-08-12 14:44:58 -0700138 VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
Ana Krulec757f63a2019-01-25 10:46:18 -0800139
Ady Abraham9e16a482019-12-03 17:19:41 -0800140 const nsecs_t mSfDuration;
141 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700142
Ady Abraham9e16a482019-12-03 17:19:41 -0800143 const nsecs_t mSfEarlyDuration;
144 const nsecs_t mAppEarlyDuration;
145
Ady Abraham8287e852020-08-12 14:44:58 -0700146 const nsecs_t mSfEarlyGpuDuration;
147 const nsecs_t mAppEarlyGpuDuration;
Ana Krulec757f63a2019-01-25 10:46:18 -0800148};
Ana Krulec757f63a2019-01-25 10:46:18 -0800149
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700150} // namespace impl
151} // namespace android::scheduler