blob: b6cb373b7ec28ca7ff8be5f541644a9510e2b19f [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 Laskowski933f8de2023-01-20 13:15:51 -050023#include <android-base/thread_annotations.h>
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070024#include <ftl/small_map.h>
Marin Shalamanove8a663d2020-11-24 17:48:00 +010025#include <utils/Timers.h>
26
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080027#include <scheduler/Fps.h>
Dominik Laskowski933f8de2023-01-20 13:15:51 -050028#include <scheduler/VsyncConfig.h>
Ana Krulec757f63a2019-01-25 10:46:18 -080029
Dominik Laskowskieddeda12019-07-19 11:54:13 -070030namespace android::scheduler {
Ana Krulec757f63a2019-01-25 10:46:18 -080031
32/*
Ady Abraham8287e852020-08-12 14:44:58 -070033 * This class encapsulates vsync configurations for different refresh rates. Depending
Ana Krulec757f63a2019-01-25 10:46:18 -080034 * on what refresh rate we are using, and wheter we are composing in GL,
35 * different offsets will help us with latency. This class keeps track of
36 * which mode the device is on, and returns approprate offsets when needed.
37 */
Ady Abraham8287e852020-08-12 14:44:58 -070038class VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080039public:
Ady Abraham8287e852020-08-12 14:44:58 -070040 virtual ~VsyncConfiguration() = default;
41 virtual VsyncConfigSet getCurrentConfigs() const = 0;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010042 virtual VsyncConfigSet getConfigsForRefreshRate(Fps fps) const = 0;
Marin Shalamanov526c3382020-12-10 15:22:29 +010043 virtual void reset() = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070044
Marin Shalamanove8a663d2020-11-24 17:48:00 +010045 virtual void setRefreshRateFps(Fps fps) = 0;
Ana Krulec757f63a2019-01-25 10:46:18 -080046 virtual void dump(std::string& result) const = 0;
47};
48
49namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070050
Ady Abraham9e16a482019-12-03 17:19:41 -080051/*
Ady Abraham8287e852020-08-12 14:44:58 -070052 * This is a common implementation for both phase offsets and durations.
53 * PhaseOffsets and WorkDuration derive from this class and implement the
54 * constructOffsets method
Ady Abraham9e16a482019-12-03 17:19:41 -080055 */
Ady Abraham8287e852020-08-12 14:44:58 -070056class VsyncConfiguration : public scheduler::VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080057public:
Marin Shalamanove8a663d2020-11-24 17:48:00 +010058 explicit VsyncConfiguration(Fps currentFps);
Ady Abraham9e16a482019-12-03 17:19:41 -080059
60 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Marin Shalamanov526c3382020-12-10 15:22:29 +010061 VsyncConfigSet getConfigsForRefreshRate(Fps fps) const override EXCLUDES(mLock);
Ady Abraham9e16a482019-12-03 17:19:41 -080062
63 // Returns early, early GL, and late offsets for Apps and SF.
Marin Shalamanov526c3382020-12-10 15:22:29 +010064 VsyncConfigSet getCurrentConfigs() const override EXCLUDES(mLock) {
65 std::lock_guard lock(mLock);
66 return getConfigsForRefreshRateLocked(mRefreshRateFps);
67 }
68
69 // Cleans the internal cache.
70 void reset() override EXCLUDES(mLock) {
71 std::lock_guard lock(mLock);
72 mOffsetsCache.clear();
Ady Abraham8287e852020-08-12 14:44:58 -070073 }
Ady Abraham9e16a482019-12-03 17:19:41 -080074
75 // This function should be called when the device is switching between different
76 // refresh rates, to properly update the offsets.
Marin Shalamanov526c3382020-12-10 15:22:29 +010077 void setRefreshRateFps(Fps fps) override EXCLUDES(mLock) {
78 std::lock_guard lock(mLock);
79 mRefreshRateFps = fps;
80 }
Ady Abraham9e16a482019-12-03 17:19:41 -080081
82 // Returns current offsets in human friendly format.
83 void dump(std::string& result) const override;
84
Ady Abraham60120a02020-03-23 11:23:26 -070085protected:
Dominik Laskowski933f8de2023-01-20 13:15:51 -050086 virtual VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const = 0;
Ady Abraham8287e852020-08-12 14:44:58 -070087
Marin Shalamanov526c3382020-12-10 15:22:29 +010088 VsyncConfigSet getConfigsForRefreshRateLocked(Fps fps) const REQUIRES(mLock);
89
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070090 mutable ftl::SmallMap<Fps, VsyncConfigSet, 2, FpsApproxEqual> mOffsetsCache GUARDED_BY(mLock);
91 Fps mRefreshRateFps GUARDED_BY(mLock);
Marin Shalamanov526c3382020-12-10 15:22:29 +010092 mutable std::mutex mLock;
Ady Abraham8287e852020-08-12 14:44:58 -070093};
94
95/*
96 * This is the old implementation of phase offsets and considered as deprecated.
97 * WorkDuration is the new implementation.
98 */
99class PhaseOffsets : public VsyncConfiguration {
100public:
Marin Shalamanov526c3382020-12-10 15:22:29 +0100101 explicit PhaseOffsets(Fps currentRefreshRate);
Ady Abraham8287e852020-08-12 14:44:58 -0700102
103protected:
Ady Abraham60120a02020-03-23 11:23:26 -0700104 // Used for unit tests
Marin Shalamanov526c3382020-12-10 15:22:29 +0100105 PhaseOffsets(Fps currentRefreshRate, nsecs_t vsyncPhaseOffsetNs, nsecs_t sfVSyncPhaseOffsetNs,
106 std::optional<nsecs_t> earlySfOffsetNs, std::optional<nsecs_t> earlyGpuSfOffsetNs,
107 std::optional<nsecs_t> earlyAppOffsetNs,
Ady Abraham8287e852020-08-12 14:44:58 -0700108 std::optional<nsecs_t> earlyGpuAppOffsetNs, nsecs_t highFpsVsyncPhaseOffsetNs,
109 nsecs_t highFpsSfVSyncPhaseOffsetNs, std::optional<nsecs_t> highFpsEarlySfOffsetNs,
110 std::optional<nsecs_t> highFpsEarlyGpuSfOffsetNs,
111 std::optional<nsecs_t> highFpsEarlyAppOffsetNs,
Ady Abrahamcaba2982021-06-16 16:45:04 -0700112 std::optional<nsecs_t> highFpsEarlyGpuAppOffsetNs, nsecs_t thresholdForNextVsync,
113 nsecs_t hwcMinWorkDuration);
Ady Abraham8287e852020-08-12 14:44:58 -0700114
115private:
Dominik Laskowski933f8de2023-01-20 13:15:51 -0500116 VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
Ady Abraham8287e852020-08-12 14:44:58 -0700117
118 VsyncConfigSet getDefaultOffsets(nsecs_t vsyncPeriod) const;
119 VsyncConfigSet getHighFpsOffsets(nsecs_t vsyncPeriod) const;
Ady Abraham9e16a482019-12-03 17:19:41 -0800120
Ady Abrahamc6c81822020-04-28 10:28:00 -0700121 const nsecs_t mVSyncPhaseOffsetNs;
122 const nsecs_t mSfVSyncPhaseOffsetNs;
123 const std::optional<nsecs_t> mEarlySfOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700124 const std::optional<nsecs_t> mEarlyGpuSfOffsetNs;
Ady Abrahamc6c81822020-04-28 10:28:00 -0700125 const std::optional<nsecs_t> mEarlyAppOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700126 const std::optional<nsecs_t> mEarlyGpuAppOffsetNs;
Ady Abraham9e16a482019-12-03 17:19:41 -0800127
Ady Abraham8287e852020-08-12 14:44:58 -0700128 const nsecs_t mHighFpsVSyncPhaseOffsetNs;
129 const nsecs_t mHighFpsSfVSyncPhaseOffsetNs;
130 const std::optional<nsecs_t> mHighFpsEarlySfOffsetNs;
131 const std::optional<nsecs_t> mHighFpsEarlyGpuSfOffsetNs;
132 const std::optional<nsecs_t> mHighFpsEarlyAppOffsetNs;
133 const std::optional<nsecs_t> mHighFpsEarlyGpuAppOffsetNs;
134
135 const nsecs_t mThresholdForNextVsync;
Ady Abrahamcaba2982021-06-16 16:45:04 -0700136 const nsecs_t mHwcMinWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -0800137};
138
139/*
140 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
Ady Abraham8287e852020-08-12 14:44:58 -0700141 * The offsets are calculated from durations for each one of the (late, early, earlyGpu)
Ady Abraham9e16a482019-12-03 17:19:41 -0800142 * offset types.
143 */
Ady Abraham8287e852020-08-12 14:44:58 -0700144class WorkDuration : public VsyncConfiguration {
Ady Abraham9e16a482019-12-03 17:19:41 -0800145public:
ramindania04b8a52023-08-07 18:49:47 -0700146 explicit WorkDuration(Fps currentRefreshRate);
Ana Krulec757f63a2019-01-25 10:46:18 -0800147
Ady Abraham9e16a482019-12-03 17:19:41 -0800148protected:
149 // Used for unit tests
Marin Shalamanov526c3382020-12-10 15:22:29 +0100150 WorkDuration(Fps currentFps, nsecs_t sfDuration, nsecs_t appDuration, nsecs_t sfEarlyDuration,
Ady Abrahamcaba2982021-06-16 16:45:04 -0700151 nsecs_t appEarlyDuration, nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration,
152 nsecs_t hwcMinWorkDuration);
Ady Abraham9e16a482019-12-03 17:19:41 -0800153
Ana Krulec757f63a2019-01-25 10:46:18 -0800154private:
Dominik Laskowski933f8de2023-01-20 13:15:51 -0500155 VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
Ana Krulec757f63a2019-01-25 10:46:18 -0800156
Ady Abraham9e16a482019-12-03 17:19:41 -0800157 const nsecs_t mSfDuration;
158 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700159
Ady Abraham9e16a482019-12-03 17:19:41 -0800160 const nsecs_t mSfEarlyDuration;
161 const nsecs_t mAppEarlyDuration;
162
Ady Abraham8287e852020-08-12 14:44:58 -0700163 const nsecs_t mSfEarlyGpuDuration;
164 const nsecs_t mAppEarlyGpuDuration;
Ady Abrahamcaba2982021-06-16 16:45:04 -0700165
166 const nsecs_t mHwcMinWorkDuration;
Ana Krulec757f63a2019-01-25 10:46:18 -0800167};
Ana Krulec757f63a2019-01-25 10:46:18 -0800168
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700169} // namespace impl
170} // namespace android::scheduler