blob: d9d206dd7635a8d8a3b25e11ce6b245256927345 [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>
20#include <type_traits>
Alec Mourid7599d82019-05-22 19:58:00 -070021#include <unordered_map>
Marin Shalamanov526c3382020-12-10 15:22:29 +010022#include <vector>
Ana Krulec757f63a2019-01-25 10:46:18 -080023
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
91 mutable std::unordered_map<Fps, VsyncConfigSet, std::hash<Fps>, Fps::EqualsInBuckets>
92 mOffsetsCache GUARDED_BY(mLock);
93 std::atomic<Fps> mRefreshRateFps GUARDED_BY(mLock);
94 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,
114 std::optional<nsecs_t> highFpsEarlyGpuAppOffsetNs, nsecs_t thresholdForNextVsync);
115
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 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:
Marin Shalamanov526c3382020-12-10 15:22:29 +0100146 explicit WorkDuration(Fps currentRefrshRate);
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,
151 nsecs_t appEarlyDuration, nsecs_t sfEarlyGpuDuration, nsecs_t appEarlyGpuDuration);
Ady Abraham9e16a482019-12-03 17:19:41 -0800152
Ana Krulec757f63a2019-01-25 10:46:18 -0800153private:
Ady Abraham8287e852020-08-12 14:44:58 -0700154 VsyncConfiguration::VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const override;
Ana Krulec757f63a2019-01-25 10:46:18 -0800155
Ady Abraham9e16a482019-12-03 17:19:41 -0800156 const nsecs_t mSfDuration;
157 const nsecs_t mAppDuration;
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700158
Ady Abraham9e16a482019-12-03 17:19:41 -0800159 const nsecs_t mSfEarlyDuration;
160 const nsecs_t mAppEarlyDuration;
161
Ady Abraham8287e852020-08-12 14:44:58 -0700162 const nsecs_t mSfEarlyGpuDuration;
163 const nsecs_t mAppEarlyGpuDuration;
Ana Krulec757f63a2019-01-25 10:46:18 -0800164};
Ana Krulec757f63a2019-01-25 10:46:18 -0800165
Dominik Laskowskieddeda12019-07-19 11:54:13 -0700166} // namespace impl
167} // namespace android::scheduler