blob: 02ebd702722ea117cfdb6050bbc5befb7e7177c1 [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
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080026#include <scheduler/Fps.h>
27
Dominik Laskowskia93a5312020-07-23 15:10:03 -070028#include "VsyncModulator.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 using VsyncConfigSet = VsyncModulator::VsyncConfigSet;
Ana Krulec757f63a2019-01-25 10:46:18 -080041
Ady Abraham8287e852020-08-12 14:44:58 -070042 virtual ~VsyncConfiguration() = default;
43 virtual VsyncConfigSet getCurrentConfigs() const = 0;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010044 virtual VsyncConfigSet getConfigsForRefreshRate(Fps fps) const = 0;
Marin Shalamanov526c3382020-12-10 15:22:29 +010045 virtual void reset() = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070046
Marin Shalamanove8a663d2020-11-24 17:48:00 +010047 virtual void setRefreshRateFps(Fps fps) = 0;
Ana Krulec757f63a2019-01-25 10:46:18 -080048 virtual void dump(std::string& result) const = 0;
49};
50
51namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070052
Ady Abraham9e16a482019-12-03 17:19:41 -080053/*
Ady Abraham8287e852020-08-12 14:44:58 -070054 * This is a common implementation for both phase offsets and durations.
55 * PhaseOffsets and WorkDuration derive from this class and implement the
56 * constructOffsets method
Ady Abraham9e16a482019-12-03 17:19:41 -080057 */
Ady Abraham8287e852020-08-12 14:44:58 -070058class VsyncConfiguration : public scheduler::VsyncConfiguration {
Ana Krulec757f63a2019-01-25 10:46:18 -080059public:
Marin Shalamanove8a663d2020-11-24 17:48:00 +010060 explicit VsyncConfiguration(Fps currentFps);
Ady Abraham9e16a482019-12-03 17:19:41 -080061
62 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Marin Shalamanov526c3382020-12-10 15:22:29 +010063 VsyncConfigSet getConfigsForRefreshRate(Fps fps) const override EXCLUDES(mLock);
Ady Abraham9e16a482019-12-03 17:19:41 -080064
65 // Returns early, early GL, and late offsets for Apps and SF.
Marin Shalamanov526c3382020-12-10 15:22:29 +010066 VsyncConfigSet getCurrentConfigs() const override EXCLUDES(mLock) {
67 std::lock_guard lock(mLock);
68 return getConfigsForRefreshRateLocked(mRefreshRateFps);
69 }
70
71 // Cleans the internal cache.
72 void reset() override EXCLUDES(mLock) {
73 std::lock_guard lock(mLock);
74 mOffsetsCache.clear();
Ady Abraham8287e852020-08-12 14:44:58 -070075 }
Ady Abraham9e16a482019-12-03 17:19:41 -080076
77 // This function should be called when the device is switching between different
78 // refresh rates, to properly update the offsets.
Marin Shalamanov526c3382020-12-10 15:22:29 +010079 void setRefreshRateFps(Fps fps) override EXCLUDES(mLock) {
80 std::lock_guard lock(mLock);
81 mRefreshRateFps = fps;
82 }
Ady Abraham9e16a482019-12-03 17:19:41 -080083
84 // Returns current offsets in human friendly format.
85 void dump(std::string& result) const override;
86
Ady Abraham60120a02020-03-23 11:23:26 -070087protected:
Ady Abraham8287e852020-08-12 14:44:58 -070088 virtual VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const = 0;
89
Marin Shalamanov526c3382020-12-10 15:22:29 +010090 VsyncConfigSet getConfigsForRefreshRateLocked(Fps fps) const REQUIRES(mLock);
91
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070092 mutable ftl::SmallMap<Fps, VsyncConfigSet, 2, FpsApproxEqual> mOffsetsCache GUARDED_BY(mLock);
93 Fps mRefreshRateFps GUARDED_BY(mLock);
Marin Shalamanov526c3382020-12-10 15:22:29 +010094 mutable std::mutex mLock;
Ady Abraham8287e852020-08-12 14:44:58 -070095};
96
97/*
98 * This is the old implementation of phase offsets and considered as deprecated.
99 * WorkDuration is the new implementation.
100 */
101class PhaseOffsets : public VsyncConfiguration {
102public:
Marin Shalamanov526c3382020-12-10 15:22:29 +0100103 explicit PhaseOffsets(Fps currentRefreshRate);
Ady Abraham8287e852020-08-12 14:44:58 -0700104
105protected:
Ady Abraham60120a02020-03-23 11:23:26 -0700106 // Used for unit tests
Marin Shalamanov526c3382020-12-10 15:22:29 +0100107 PhaseOffsets(Fps currentRefreshRate, nsecs_t vsyncPhaseOffsetNs, nsecs_t sfVSyncPhaseOffsetNs,
108 std::optional<nsecs_t> earlySfOffsetNs, std::optional<nsecs_t> earlyGpuSfOffsetNs,
109 std::optional<nsecs_t> earlyAppOffsetNs,
Ady Abraham8287e852020-08-12 14:44:58 -0700110 std::optional<nsecs_t> earlyGpuAppOffsetNs, nsecs_t highFpsVsyncPhaseOffsetNs,
111 nsecs_t highFpsSfVSyncPhaseOffsetNs, std::optional<nsecs_t> highFpsEarlySfOffsetNs,
112 std::optional<nsecs_t> highFpsEarlyGpuSfOffsetNs,
113 std::optional<nsecs_t> highFpsEarlyAppOffsetNs,
Ady Abrahamcaba2982021-06-16 16:45:04 -0700114 std::optional<nsecs_t> highFpsEarlyGpuAppOffsetNs, nsecs_t thresholdForNextVsync,
115 nsecs_t hwcMinWorkDuration);
Ady Abraham8287e852020-08-12 14:44:58 -0700116
117private:
118 VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
119
120 VsyncConfigSet getDefaultOffsets(nsecs_t vsyncPeriod) const;
121 VsyncConfigSet getHighFpsOffsets(nsecs_t vsyncPeriod) const;
Ady Abraham9e16a482019-12-03 17:19:41 -0800122
Ady Abrahamc6c81822020-04-28 10:28:00 -0700123 const nsecs_t mVSyncPhaseOffsetNs;
124 const nsecs_t mSfVSyncPhaseOffsetNs;
125 const std::optional<nsecs_t> mEarlySfOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700126 const std::optional<nsecs_t> mEarlyGpuSfOffsetNs;
Ady Abrahamc6c81822020-04-28 10:28:00 -0700127 const std::optional<nsecs_t> mEarlyAppOffsetNs;
Ady Abraham8287e852020-08-12 14:44:58 -0700128 const std::optional<nsecs_t> mEarlyGpuAppOffsetNs;
Ady Abraham9e16a482019-12-03 17:19:41 -0800129
Ady Abraham8287e852020-08-12 14:44:58 -0700130 const nsecs_t mHighFpsVSyncPhaseOffsetNs;
131 const nsecs_t mHighFpsSfVSyncPhaseOffsetNs;
132 const std::optional<nsecs_t> mHighFpsEarlySfOffsetNs;
133 const std::optional<nsecs_t> mHighFpsEarlyGpuSfOffsetNs;
134 const std::optional<nsecs_t> mHighFpsEarlyAppOffsetNs;
135 const std::optional<nsecs_t> mHighFpsEarlyGpuAppOffsetNs;
136
137 const nsecs_t mThresholdForNextVsync;
Ady Abrahamcaba2982021-06-16 16:45:04 -0700138 const nsecs_t mHwcMinWorkDuration;
Ady Abraham9e16a482019-12-03 17:19:41 -0800139};
140
141/*
142 * Class that encapsulates the phase offsets for SurfaceFlinger and App.
Ady Abraham8287e852020-08-12 14:44:58 -0700143 * The offsets are calculated from durations for each one of the (late, early, earlyGpu)
Ady Abraham9e16a482019-12-03 17:19:41 -0800144 * offset types.
145 */
Ady Abraham8287e852020-08-12 14:44:58 -0700146class WorkDuration : public VsyncConfiguration {
Ady Abraham9e16a482019-12-03 17:19:41 -0800147public:
Marin Shalamanov526c3382020-12-10 15:22:29 +0100148 explicit WorkDuration(Fps currentRefrshRate);
Ana Krulec757f63a2019-01-25 10:46:18 -0800149
Ady Abraham9e16a482019-12-03 17:19:41 -0800150protected:
151 // Used for unit tests
Marin Shalamanov526c3382020-12-10 15:22:29 +0100152 WorkDuration(Fps currentFps, nsecs_t sfDuration, nsecs_t appDuration, nsecs_t sfEarlyDuration,
Ady Abrahamcaba2982021-06-16 16:45:04 -0700153 nsecs_t appEarlyDuration, nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration,
154 nsecs_t hwcMinWorkDuration);
Ady Abraham9e16a482019-12-03 17:19:41 -0800155
Ana Krulec757f63a2019-01-25 10:46:18 -0800156private:
Ady Abraham8287e852020-08-12 14:44:58 -0700157 VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
Ana Krulec757f63a2019-01-25 10:46:18 -0800158
Ady Abraham9e16a482019-12-03 17:19:41 -0800159 const nsecs_t mSfDuration;
160 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700161
Ady Abraham9e16a482019-12-03 17:19:41 -0800162 const nsecs_t mSfEarlyDuration;
163 const nsecs_t mAppEarlyDuration;
164
Ady Abraham8287e852020-08-12 14:44:58 -0700165 const nsecs_t mSfEarlyGpuDuration;
166 const nsecs_t mAppEarlyGpuDuration;
Ady Abrahamcaba2982021-06-16 16:45:04 -0700167
168 const nsecs_t mHwcMinWorkDuration;
Ana Krulec757f63a2019-01-25 10:46:18 -0800169};
Ana Krulec757f63a2019-01-25 10:46:18 -0800170
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700171} // namespace impl
172} // namespace android::scheduler