blob: c10efdee21515195a4a34b0e29d4b9eaf1703c81 [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.
57 Offsets getOffsetsForRefreshRate(float fps) const override {
58 const auto iter = mOffsets.find(fps);
59 LOG_ALWAYS_FATAL_IF(iter == mOffsets.end());
60 return iter->second;
61 }
62
63 // Returns early, early GL, and late offsets for Apps and SF.
64 Offsets getCurrentOffsets() const override { return getOffsetsForRefreshRate(mRefreshRateFps); }
65
66 // This function should be called when the device is switching between different
67 // refresh rates, to properly update the offsets.
68 void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; }
69
70 // Returns current offsets in human friendly format.
71 void dump(std::string& result) const override;
72
73private:
74 std::unordered_map<float, PhaseOffsets::Offsets> initializeOffsets(
75 const scheduler::RefreshRateConfigs&) const;
76 Offsets getDefaultOffsets(nsecs_t vsyncDuration) const;
77 Offsets getHighFpsOffsets(nsecs_t vsyncDuration) const;
78
79 const nsecs_t mThresholdForNextVsync;
80 const std::unordered_map<float, Offsets> mOffsets;
81
82 std::atomic<float> mRefreshRateFps;
83};
84
85/*
86 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
87 * The offsets are calculated from durations for each one of the (late, early, earlyGL)
88 * offset types.
89 */
90class PhaseDurations : public scheduler::PhaseConfiguration {
91public:
92 PhaseDurations(const scheduler::RefreshRateConfigs&);
Ana Krulec757f63a2019-01-25 10:46:18 -080093
Ady Abraham796beb02019-04-11 15:23:07 -070094 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Ady Abraham2139f732019-11-13 18:56:40 -080095 Offsets getOffsetsForRefreshRate(float fps) const override;
Ady Abraham796beb02019-04-11 15:23:07 -070096
Ana Krulec757f63a2019-01-25 10:46:18 -080097 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham2139f732019-11-13 18:56:40 -080098 Offsets getCurrentOffsets() const override { return getOffsetsForRefreshRate(mRefreshRateFps); }
Ana Krulec757f63a2019-01-25 10:46:18 -080099
100 // This function should be called when the device is switching between different
101 // refresh rates, to properly update the offsets.
Ady Abraham2139f732019-11-13 18:56:40 -0800102 void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; }
Ana Krulec757f63a2019-01-25 10:46:18 -0800103
104 // Returns current offsets in human friendly format.
105 void dump(std::string& result) const override;
106
Ady Abraham9e16a482019-12-03 17:19:41 -0800107protected:
108 // Used for unit tests
109 PhaseDurations(const std::vector<float>& refreshRates, float currentFps, nsecs_t sfDuration,
110 nsecs_t appDuration, nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration,
111 nsecs_t sfEarlyGlDuration, nsecs_t appEarlyGlDuration);
112
Ana Krulec757f63a2019-01-25 10:46:18 -0800113private:
Ady Abraham9e16a482019-12-03 17:19:41 -0800114 std::unordered_map<float, PhaseDurations::Offsets> initializeOffsets(
115 const std::vector<float>&) const;
Ana Krulec757f63a2019-01-25 10:46:18 -0800116
Ady Abraham9e16a482019-12-03 17:19:41 -0800117 const nsecs_t mSfDuration;
118 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700119
Ady Abraham9e16a482019-12-03 17:19:41 -0800120 const nsecs_t mSfEarlyDuration;
121 const nsecs_t mAppEarlyDuration;
122
123 const nsecs_t mSfEarlyGlDuration;
124 const nsecs_t mAppEarlyGlDuration;
125
126 const std::unordered_map<float, Offsets> mOffsets;
127
128 std::atomic<float> mRefreshRateFps;
Ana Krulec757f63a2019-01-25 10:46:18 -0800129};
Ana Krulec757f63a2019-01-25 10:46:18 -0800130
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700131} // namespace impl
132} // namespace android::scheduler