blob: 7747f0cfcccb89f803f8521523181eed56a5cacd [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;
Ana Krulec757f63a2019-01-25 10:46:18 -080035
36 virtual ~PhaseOffsets();
37
Dominik Laskowskieddeda12019-07-19 11:54:13 -070038 nsecs_t getCurrentAppOffset() const { return getCurrentOffsets().late.app; }
39 nsecs_t getCurrentSfOffset() const { return getCurrentOffsets().late.sf; }
40 nsecs_t getOffsetThresholdForNextVsync() const {
41 return getCurrentOffsets().thresholdForNextVsync;
42 }
43
Ana Krulec757f63a2019-01-25 10:46:18 -080044 virtual Offsets getCurrentOffsets() const = 0;
Ady Abraham2139f732019-11-13 18:56:40 -080045 virtual Offsets getOffsetsForRefreshRate(float fps) const = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070046
Ady Abraham2139f732019-11-13 18:56:40 -080047 virtual void setRefreshRateFps(float fps) = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070048
Ana Krulec757f63a2019-01-25 10:46:18 -080049 virtual void dump(std::string& result) const = 0;
50};
51
52namespace impl {
Dominik Laskowskieddeda12019-07-19 11:54:13 -070053
Ana Krulec757f63a2019-01-25 10:46:18 -080054class PhaseOffsets : public scheduler::PhaseOffsets {
55public:
56 PhaseOffsets();
57
Ady Abraham796beb02019-04-11 15:23:07 -070058 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
Ady Abraham2139f732019-11-13 18:56:40 -080059 Offsets getOffsetsForRefreshRate(float fps) const override;
Ady Abraham796beb02019-04-11 15:23:07 -070060
Ana Krulec757f63a2019-01-25 10:46:18 -080061 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham2139f732019-11-13 18:56:40 -080062 Offsets getCurrentOffsets() const override { return getOffsetsForRefreshRate(mRefreshRateFps); }
Ana Krulec757f63a2019-01-25 10:46:18 -080063
64 // This function should be called when the device is switching between different
65 // refresh rates, to properly update the offsets.
Ady Abraham2139f732019-11-13 18:56:40 -080066 void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; }
Ana Krulec757f63a2019-01-25 10:46:18 -080067
68 // Returns current offsets in human friendly format.
69 void dump(std::string& result) const override;
70
71private:
Dominik Laskowskieddeda12019-07-19 11:54:13 -070072 static Offsets getDefaultOffsets(nsecs_t thresholdForNextVsync);
73 static Offsets getHighFpsOffsets(nsecs_t thresholdForNextVsync);
Ana Krulec757f63a2019-01-25 10:46:18 -080074
Ady Abraham2139f732019-11-13 18:56:40 -080075 std::atomic<float> mRefreshRateFps = 0;
Dominik Laskowskieddeda12019-07-19 11:54:13 -070076
Ady Abraham2139f732019-11-13 18:56:40 -080077 Offsets mDefaultOffsets;
78 Offsets mHighFpsOffsets;
Ana Krulec757f63a2019-01-25 10:46:18 -080079};
Ana Krulec757f63a2019-01-25 10:46:18 -080080
Dominik Laskowskieddeda12019-07-19 11:54:13 -070081} // namespace impl
82} // namespace android::scheduler