blob: 7b1bdfd36bbac8afffa9ad64ad2fa70232a80a1f [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"
22#include "VSyncModulator.h"
23
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
69private:
Ady Abraham090d42c2020-01-08 12:08:11 -080070 std::unordered_map<float, Offsets> initializeOffsets(
Ady Abraham9e16a482019-12-03 17:19:41 -080071 const scheduler::RefreshRateConfigs&) const;
72 Offsets getDefaultOffsets(nsecs_t vsyncDuration) const;
73 Offsets getHighFpsOffsets(nsecs_t vsyncDuration) const;
74
75 const nsecs_t mThresholdForNextVsync;
76 const std::unordered_map<float, Offsets> mOffsets;
77
78 std::atomic<float> mRefreshRateFps;
79};
80
81/*
82 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
83 * The offsets are calculated from durations for each one of the (late, early, earlyGL)
84 * offset types.
85 */
86class PhaseDurations : public scheduler::PhaseConfiguration {
87public:
88 PhaseDurations(const scheduler::RefreshRateConfigs&);
Ana Krulec757f63a2019-01-25 10:46:18 -080089
Ady Abraham796beb02019-04-11 15:23:07 -070090 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Ady Abraham2139f732019-11-13 18:56:40 -080091 Offsets getOffsetsForRefreshRate(float fps) const override;
Ady Abraham796beb02019-04-11 15:23:07 -070092
Ana Krulec757f63a2019-01-25 10:46:18 -080093 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham2139f732019-11-13 18:56:40 -080094 Offsets getCurrentOffsets() const override { return getOffsetsForRefreshRate(mRefreshRateFps); }
Ana Krulec757f63a2019-01-25 10:46:18 -080095
96 // This function should be called when the device is switching between different
97 // refresh rates, to properly update the offsets.
Ady Abraham2139f732019-11-13 18:56:40 -080098 void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; }
Ana Krulec757f63a2019-01-25 10:46:18 -080099
100 // Returns current offsets in human friendly format.
101 void dump(std::string& result) const override;
102
Ady Abraham9e16a482019-12-03 17:19:41 -0800103protected:
104 // Used for unit tests
105 PhaseDurations(const std::vector<float>& refreshRates, float currentFps, nsecs_t sfDuration,
106 nsecs_t appDuration, nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration,
107 nsecs_t sfEarlyGlDuration, nsecs_t appEarlyGlDuration);
108
Ana Krulec757f63a2019-01-25 10:46:18 -0800109private:
Ady Abraham090d42c2020-01-08 12:08:11 -0800110 std::unordered_map<float, Offsets> initializeOffsets(const std::vector<float>&) const;
Ana Krulec757f63a2019-01-25 10:46:18 -0800111
Ady Abraham9e16a482019-12-03 17:19:41 -0800112 const nsecs_t mSfDuration;
113 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700114
Ady Abraham9e16a482019-12-03 17:19:41 -0800115 const nsecs_t mSfEarlyDuration;
116 const nsecs_t mAppEarlyDuration;
117
118 const nsecs_t mSfEarlyGlDuration;
119 const nsecs_t mAppEarlyGlDuration;
120
121 const std::unordered_map<float, Offsets> mOffsets;
122
123 std::atomic<float> mRefreshRateFps;
Ana Krulec757f63a2019-01-25 10:46:18 -0800124};
Ana Krulec757f63a2019-01-25 10:46:18 -0800125
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700126} // namespace impl
127} // namespace android::scheduler