blob: 2c5243244865b0d5201a9034196a715eebadcb81 [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 */
32class PhaseOffsets {
33public:
Dominik Laskowskieddeda12019-07-19 11:54:13 -070034 using Offsets = VSyncModulator::OffsetsConfig;
35 using RefreshRateType = RefreshRateConfigs::RefreshRateType;
Ana Krulec757f63a2019-01-25 10:46:18 -080036
37 virtual ~PhaseOffsets();
38
Dominik Laskowskieddeda12019-07-19 11:54:13 -070039 nsecs_t getCurrentAppOffset() const { return getCurrentOffsets().late.app; }
40 nsecs_t getCurrentSfOffset() const { return getCurrentOffsets().late.sf; }
41 nsecs_t getOffsetThresholdForNextVsync() const {
42 return getCurrentOffsets().thresholdForNextVsync;
43 }
44
Ana Krulec757f63a2019-01-25 10:46:18 -080045 virtual Offsets getCurrentOffsets() const = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070046 virtual Offsets getOffsetsForRefreshRate(RefreshRateType) const = 0;
47
48 virtual void setRefreshRateType(RefreshRateType) = 0;
49
Ana Krulec757f63a2019-01-25 10:46:18 -080050 virtual void dump(std::string& result) const = 0;
51};
52
53namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070054
Ana Krulec757f63a2019-01-25 10:46:18 -080055class PhaseOffsets : public scheduler::PhaseOffsets {
56public:
57 PhaseOffsets();
58
Ady Abraham796beb02019-04-11 15:23:07 -070059 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070060 Offsets getOffsetsForRefreshRate(RefreshRateType) const override;
Ady Abraham796beb02019-04-11 15:23:07 -070061
Ana Krulec757f63a2019-01-25 10:46:18 -080062 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham796beb02019-04-11 15:23:07 -070063 Offsets getCurrentOffsets() const override {
64 return getOffsetsForRefreshRate(mRefreshRateType);
65 }
Ana Krulec757f63a2019-01-25 10:46:18 -080066
67 // This function should be called when the device is switching between different
68 // refresh rates, to properly update the offsets.
Dominik Laskowskieddeda12019-07-19 11:54:13 -070069 void setRefreshRateType(RefreshRateType refreshRateType) override {
Ana Krulec757f63a2019-01-25 10:46:18 -080070 mRefreshRateType = refreshRateType;
71 }
72
73 // Returns current offsets in human friendly format.
74 void dump(std::string& result) const override;
75
76private:
Dominik Laskowskieddeda12019-07-19 11:54:13 -070077 static Offsets getDefaultOffsets(nsecs_t thresholdForNextVsync);
78 static Offsets getHighFpsOffsets(nsecs_t thresholdForNextVsync);
Ana Krulec757f63a2019-01-25 10:46:18 -080079
Dominik Laskowskieddeda12019-07-19 11:54:13 -070080 std::atomic<RefreshRateType> mRefreshRateType = RefreshRateType::DEFAULT;
81
82 std::unordered_map<RefreshRateType, Offsets> mOffsets;
Ana Krulec757f63a2019-01-25 10:46:18 -080083};
Ana Krulec757f63a2019-01-25 10:46:18 -080084
Dominik Laskowskieddeda12019-07-19 11:54:13 -070085} // namespace impl
86} // namespace android::scheduler