blob: e395b4204602e45a69ad2dae0ade077ef397db29 [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/*
27 * This class encapsulates offsets for different refresh rates. Depending
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 Abraham9e16a482019-12-03 17:19:41 -080032class PhaseConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080033public:
Dominik Laskowskieddeda12019-07-19 11:54:13 -070034 using Offsets = VSyncModulator::OffsetsConfig;
Ana Krulec757f63a2019-01-25 10:46:18 -080035
Ady Abraham9e16a482019-12-03 17:19:41 -080036 virtual ~PhaseConfiguration();
Dominik Laskowskieddeda12019-07-19 11:54:13 -070037
Ana Krulec757f63a2019-01-25 10:46:18 -080038 virtual Offsets getCurrentOffsets() const = 0;
Ady Abraham2139f732019-11-13 18:56:40 -080039 virtual Offsets getOffsetsForRefreshRate(float fps) const = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070040
Ady Abraham2139f732019-11-13 18:56:40 -080041 virtual void setRefreshRateFps(float fps) = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070042
Ana Krulec757f63a2019-01-25 10:46:18 -080043 virtual void dump(std::string& result) const = 0;
44};
45
46namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070047
Ady Abraham9e16a482019-12-03 17:19:41 -080048/*
49 * This is the old implementation of phase offsets and considered as deprecated.
50 * PhaseDurations is the new implementation.
51 */
52class PhaseOffsets : public scheduler::PhaseConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080053public:
Ady Abraham9e16a482019-12-03 17:19:41 -080054 PhaseOffsets(const scheduler::RefreshRateConfigs&);
55
56 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Ady Abraham090d42c2020-01-08 12:08:11 -080057 Offsets getOffsetsForRefreshRate(float fps) const override;
Ady Abraham9e16a482019-12-03 17:19:41 -080058
59 // Returns early, early GL, and late offsets for Apps and SF.
60 Offsets getCurrentOffsets() const override { return getOffsetsForRefreshRate(mRefreshRateFps); }
61
62 // This function should be called when the device is switching between different
63 // refresh rates, to properly update the offsets.
64 void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; }
65
66 // Returns current offsets in human friendly format.
67 void dump(std::string& result) const override;
68
Ady Abraham60120a02020-03-23 11:23:26 -070069protected:
70 // Used for unit tests
71 PhaseOffsets(const std::vector<float>& refreshRates, float currentFps,
Ady Abrahamc6c81822020-04-28 10:28:00 -070072 nsecs_t vsyncPhaseOffsetNs, nsecs_t sfVSyncPhaseOffsetNs,
73 std::optional<nsecs_t> earlySfOffsetNs, std::optional<nsecs_t> earlyGlSfOffsetNs,
74 std::optional<nsecs_t> earlyAppOffsetNs, std::optional<nsecs_t> earlyGlAppOffsetNs,
Ady Abraham60120a02020-03-23 11:23:26 -070075 nsecs_t thresholdForNextVsync);
Ady Abraham090d42c2020-01-08 12:08:11 -080076 std::unordered_map<float, Offsets> initializeOffsets(
Ady Abraham60120a02020-03-23 11:23:26 -070077 const std::vector<float>& refreshRates) const;
Sushil Chauhanbdb9e6d2020-03-19 16:15:46 -070078 Offsets getDefaultOffsets(nsecs_t vsyncPeriod) const;
79 Offsets getHighFpsOffsets(nsecs_t vsyncPeriod) const;
80 Offsets getPhaseOffsets(float fps, nsecs_t vsyncPeriod) const;
Ady Abraham9e16a482019-12-03 17:19:41 -080081
Ady Abrahamc6c81822020-04-28 10:28:00 -070082 const nsecs_t mVSyncPhaseOffsetNs;
83 const nsecs_t mSfVSyncPhaseOffsetNs;
84 const std::optional<nsecs_t> mEarlySfOffsetNs;
85 const std::optional<nsecs_t> mEarlyGlSfOffsetNs;
86 const std::optional<nsecs_t> mEarlyAppOffsetNs;
87 const std::optional<nsecs_t> mEarlyGlAppOffsetNs;
Ady Abraham9e16a482019-12-03 17:19:41 -080088 const nsecs_t mThresholdForNextVsync;
89 const std::unordered_map<float, Offsets> mOffsets;
90
91 std::atomic<float> mRefreshRateFps;
92};
93
94/*
95 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
96 * The offsets are calculated from durations for each one of the (late, early, earlyGL)
97 * offset types.
98 */
99class PhaseDurations : public scheduler::PhaseConfiguration {
100public:
101 PhaseDurations(const scheduler::RefreshRateConfigs&);
Ana Krulec757f63a2019-01-25 10:46:18 -0800102
Ady Abraham796beb02019-04-11 15:23:07 -0700103 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Ady Abraham2139f732019-11-13 18:56:40 -0800104 Offsets getOffsetsForRefreshRate(float fps) const override;
Ady Abraham796beb02019-04-11 15:23:07 -0700105
Ana Krulec757f63a2019-01-25 10:46:18 -0800106 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham2139f732019-11-13 18:56:40 -0800107 Offsets getCurrentOffsets() const override { return getOffsetsForRefreshRate(mRefreshRateFps); }
Ana Krulec757f63a2019-01-25 10:46:18 -0800108
109 // This function should be called when the device is switching between different
110 // refresh rates, to properly update the offsets.
Ady Abraham2139f732019-11-13 18:56:40 -0800111 void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; }
Ana Krulec757f63a2019-01-25 10:46:18 -0800112
113 // Returns current offsets in human friendly format.
114 void dump(std::string& result) const override;
115
Ady Abraham9e16a482019-12-03 17:19:41 -0800116protected:
117 // Used for unit tests
118 PhaseDurations(const std::vector<float>& refreshRates, float currentFps, nsecs_t sfDuration,
119 nsecs_t appDuration, nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration,
120 nsecs_t sfEarlyGlDuration, nsecs_t appEarlyGlDuration);
121
Ana Krulec757f63a2019-01-25 10:46:18 -0800122private:
Ady Abraham090d42c2020-01-08 12:08:11 -0800123 std::unordered_map<float, Offsets> initializeOffsets(const std::vector<float>&) const;
Ady Abrahamdfa37362020-01-21 18:02:39 -0800124 PhaseDurations::Offsets constructOffsets(nsecs_t vsyncDuration) const;
Ana Krulec757f63a2019-01-25 10:46:18 -0800125
Ady Abraham9e16a482019-12-03 17:19:41 -0800126 const nsecs_t mSfDuration;
127 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700128
Ady Abraham9e16a482019-12-03 17:19:41 -0800129 const nsecs_t mSfEarlyDuration;
130 const nsecs_t mAppEarlyDuration;
131
132 const nsecs_t mSfEarlyGlDuration;
133 const nsecs_t mAppEarlyGlDuration;
134
135 const std::unordered_map<float, Offsets> mOffsets;
136
137 std::atomic<float> mRefreshRateFps;
Ana Krulec757f63a2019-01-25 10:46:18 -0800138};
Ana Krulec757f63a2019-01-25 10:46:18 -0800139
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700140} // namespace impl
141} // namespace android::scheduler