blob: 844751270e769a29c6b67f88324d819a7e700f73 [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
Marin Shalamanov526c3382020-12-10 15:22:29 +010019#include <mutex>
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070020#include <optional>
21#include <string>
Ana Krulec757f63a2019-01-25 10:46:18 -080022
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070023#include <ftl/small_map.h>
Marin Shalamanove8a663d2020-11-24 17:48:00 +010024#include <utils/Timers.h>
25
26#include "Fps.h"
Dominik Laskowskia93a5312020-07-23 15:10:03 -070027#include "VsyncModulator.h"
Ana Krulec757f63a2019-01-25 10:46:18 -080028
Dominik Laskowskieddeda12019-07-19 11:54:13 -070029namespace android::scheduler {
Ana Krulec757f63a2019-01-25 10:46:18 -080030
31/*
Ady Abraham8287e852020-08-12 14:44:58 -070032 * This class encapsulates vsync configurations for different refresh rates. Depending
Ana Krulec757f63a2019-01-25 10:46:18 -080033 * on what refresh rate we are using, and wheter we are composing in GL,
34 * different offsets will help us with latency. This class keeps track of
35 * which mode the device is on, and returns approprate offsets when needed.
36 */
Ady Abraham8287e852020-08-12 14:44:58 -070037class VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080038public:
Ady Abraham8287e852020-08-12 14:44:58 -070039 using VsyncConfigSet = VsyncModulator::VsyncConfigSet;
Ana Krulec757f63a2019-01-25 10:46:18 -080040
Ady Abraham8287e852020-08-12 14:44:58 -070041 virtual ~VsyncConfiguration() = default;
42 virtual VsyncConfigSet getCurrentConfigs() const = 0;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010043 virtual VsyncConfigSet getConfigsForRefreshRate(Fps fps) const = 0;
Marin Shalamanov526c3382020-12-10 15:22:29 +010044 virtual void reset() = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070045
Marin Shalamanove8a663d2020-11-24 17:48:00 +010046 virtual void setRefreshRateFps(Fps fps) = 0;
Ana Krulec757f63a2019-01-25 10:46:18 -080047 virtual void dump(std::string& result) const = 0;
48};
49
50namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070051
Ady Abraham9e16a482019-12-03 17:19:41 -080052/*
Ady Abraham8287e852020-08-12 14:44:58 -070053 * This is a common implementation for both phase offsets and durations.
54 * PhaseOffsets and WorkDuration derive from this class and implement the
55 * constructOffsets method
Ady Abraham9e16a482019-12-03 17:19:41 -080056 */
Ady Abraham8287e852020-08-12 14:44:58 -070057class VsyncConfiguration : public scheduler::VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080058public:
Marin Shalamanove8a663d2020-11-24 17:48:00 +010059 explicit VsyncConfiguration(Fps currentFps);
Ady Abraham9e16a482019-12-03 17:19:41 -080060
61 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Marin Shalamanov526c3382020-12-10 15:22:29 +010062 VsyncConfigSet getConfigsForRefreshRate(Fps fps) const override EXCLUDES(mLock);
Ady Abraham9e16a482019-12-03 17:19:41 -080063
64 // Returns early, early GL, and late offsets for Apps and SF.
Marin Shalamanov526c3382020-12-10 15:22:29 +010065 VsyncConfigSet getCurrentConfigs() const override EXCLUDES(mLock) {
66 std::lock_guard lock(mLock);
67 return getConfigsForRefreshRateLocked(mRefreshRateFps);
68 }
69
70 // Cleans the internal cache.
71 void reset() override EXCLUDES(mLock) {
72 std::lock_guard lock(mLock);
73 mOffsetsCache.clear();
Ady Abraham8287e852020-08-12 14:44:58 -070074 }
Ady Abraham9e16a482019-12-03 17:19:41 -080075
76 // This function should be called when the device is switching between different
77 // refresh rates, to properly update the offsets.
Marin Shalamanov526c3382020-12-10 15:22:29 +010078 void setRefreshRateFps(Fps fps) override EXCLUDES(mLock) {
79 std::lock_guard lock(mLock);
80 mRefreshRateFps = fps;
81 }
Ady Abraham9e16a482019-12-03 17:19:41 -080082
83 // Returns current offsets in human friendly format.
84 void dump(std::string& result) const override;
85
Ady Abraham60120a02020-03-23 11:23:26 -070086protected:
Ady Abraham8287e852020-08-12 14:44:58 -070087 virtual VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const = 0;
88
Marin Shalamanov526c3382020-12-10 15:22:29 +010089 VsyncConfigSet getConfigsForRefreshRateLocked(Fps fps) const REQUIRES(mLock);
90
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070091 mutable ftl::SmallMap<Fps, VsyncConfigSet, 2, FpsApproxEqual> mOffsetsCache GUARDED_BY(mLock);
92 Fps mRefreshRateFps GUARDED_BY(mLock);
Marin Shalamanov526c3382020-12-10 15:22:29 +010093 mutable std::mutex mLock;
Ady Abraham8287e852020-08-12 14:44:58 -070094};
95
96/*
97 * This is the old implementation of phase offsets and considered as deprecated.
98 * WorkDuration is the new implementation.
99 */
100class PhaseOffsets : public VsyncConfiguration {
101public:
Marin Shalamanov526c3382020-12-10 15:22:29 +0100102 explicit PhaseOffsets(Fps currentRefreshRate);
Ady Abraham8287e852020-08-12 14:44:58 -0700103
104protected:
Ady Abraham60120a02020-03-23 11:23:26 -0700105 // Used for unit tests
Marin Shalamanov526c3382020-12-10 15:22:29 +0100106 PhaseOffsets(Fps currentRefreshRate, nsecs_t vsyncPhaseOffsetNs, nsecs_t sfVSyncPhaseOffsetNs,
107 std::optional<nsecs_t> earlySfOffsetNs, std::optional<nsecs_t> earlyGpuSfOffsetNs,
108 std::optional<nsecs_t> earlyAppOffsetNs,
Ady Abraham8287e852020-08-12 14:44:58 -0700109 std::optional<nsecs_t> earlyGpuAppOffsetNs, nsecs_t highFpsVsyncPhaseOffsetNs,
110 nsecs_t highFpsSfVSyncPhaseOffsetNs, std::optional<nsecs_t> highFpsEarlySfOffsetNs,
111 std::optional<nsecs_t> highFpsEarlyGpuSfOffsetNs,
112 std::optional<nsecs_t> highFpsEarlyAppOffsetNs,
Ady Abrahamcaba2982021-06-16 16:45:04 -0700113 std::optional<nsecs_t> highFpsEarlyGpuAppOffsetNs, nsecs_t thresholdForNextVsync,
114 nsecs_t hwcMinWorkDuration);
Ady Abraham8287e852020-08-12 14:44:58 -0700115
116private:
117 VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
118
119 VsyncConfigSet getDefaultOffsets(nsecs_t vsyncPeriod) const;
120 VsyncConfigSet getHighFpsOffsets(nsecs_t vsyncPeriod) const;
Ady Abraham9e16a482019-12-03 17:19:41 -0800121
Ady Abrahamc6c81822020-04-28 10:28:00 -0700122 const nsecs_t mVSyncPhaseOffsetNs;
123 const nsecs_t mSfVSyncPhaseOffsetNs;
124 const std::optional<nsecs_t> mEarlySfOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700125 const std::optional<nsecs_t> mEarlyGpuSfOffsetNs;
Ady Abrahamc6c81822020-04-28 10:28:00 -0700126 const std::optional<nsecs_t> mEarlyAppOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700127 const std::optional<nsecs_t> mEarlyGpuAppOffsetNs;
Ady Abraham9e16a482019-12-03 17:19:41 -0800128
Ady Abraham8287e852020-08-12 14:44:58 -0700129 const nsecs_t mHighFpsVSyncPhaseOffsetNs;
130 const nsecs_t mHighFpsSfVSyncPhaseOffsetNs;
131 const std::optional<nsecs_t> mHighFpsEarlySfOffsetNs;
132 const std::optional<nsecs_t> mHighFpsEarlyGpuSfOffsetNs;
133 const std::optional<nsecs_t> mHighFpsEarlyAppOffsetNs;
134 const std::optional<nsecs_t> mHighFpsEarlyGpuAppOffsetNs;
135
136 const nsecs_t mThresholdForNextVsync;
Ady Abrahamcaba2982021-06-16 16:45:04 -0700137 const nsecs_t mHwcMinWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -0800138};
139
140/*
141 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
Ady Abraham8287e852020-08-12 14:44:58 -0700142 * The offsets are calculated from durations for each one of the (late, early, earlyGpu)
Ady Abraham9e16a482019-12-03 17:19:41 -0800143 * offset types.
144 */
Ady Abraham8287e852020-08-12 14:44:58 -0700145class WorkDuration : public VsyncConfiguration {
Ady Abraham9e16a482019-12-03 17:19:41 -0800146public:
Marin Shalamanov526c3382020-12-10 15:22:29 +0100147 explicit WorkDuration(Fps currentRefrshRate);
Ana Krulec757f63a2019-01-25 10:46:18 -0800148
Ady Abraham9e16a482019-12-03 17:19:41 -0800149protected:
150 // Used for unit tests
Marin Shalamanov526c3382020-12-10 15:22:29 +0100151 WorkDuration(Fps currentFps, nsecs_t sfDuration, nsecs_t appDuration, nsecs_t sfEarlyDuration,
Ady Abrahamcaba2982021-06-16 16:45:04 -0700152 nsecs_t appEarlyDuration, nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration,
153 nsecs_t hwcMinWorkDuration);
Ady Abraham9e16a482019-12-03 17:19:41 -0800154
Ana Krulec757f63a2019-01-25 10:46:18 -0800155private:
Ady Abraham8287e852020-08-12 14:44:58 -0700156 VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
Ana Krulec757f63a2019-01-25 10:46:18 -0800157
Ady Abraham9e16a482019-12-03 17:19:41 -0800158 const nsecs_t mSfDuration;
159 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700160
Ady Abraham9e16a482019-12-03 17:19:41 -0800161 const nsecs_t mSfEarlyDuration;
162 const nsecs_t mAppEarlyDuration;
163
Ady Abraham8287e852020-08-12 14:44:58 -0700164 const nsecs_t mSfEarlyGpuDuration;
165 const nsecs_t mAppEarlyGpuDuration;
Ady Abrahamcaba2982021-06-16 16:45:04 -0700166
167 const nsecs_t mHwcMinWorkDuration;
Ana Krulec757f63a2019-01-25 10:46:18 -0800168};
Ana Krulec757f63a2019-01-25 10:46:18 -0800169
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700170} // namespace impl
171} // namespace android::scheduler