blob: 2b5c2f10f1a688b8a4e7f3651e14c3505c69a9c8 [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
19#include <cinttypes>
Alec Mourid7599d82019-05-22 19:58:00 -070020#include <unordered_map>
Ana Krulec757f63a2019-01-25 10:46:18 -080021
22#include "RefreshRateConfigs.h"
23#include "VSyncModulator.h"
24
25namespace android {
26namespace scheduler {
27
28/*
29 * This class encapsulates offsets for different refresh rates. Depending
30 * on what refresh rate we are using, and wheter we are composing in GL,
31 * different offsets will help us with latency. This class keeps track of
32 * which mode the device is on, and returns approprate offsets when needed.
33 */
34class PhaseOffsets {
35public:
36 struct Offsets {
37 VSyncModulator::Offsets early;
38 VSyncModulator::Offsets earlyGl;
39 VSyncModulator::Offsets late;
40 };
41
42 virtual ~PhaseOffsets();
43
44 virtual nsecs_t getCurrentAppOffset() = 0;
45 virtual nsecs_t getCurrentSfOffset() = 0;
Ady Abraham796beb02019-04-11 15:23:07 -070046 virtual Offsets getOffsetsForRefreshRate(
47 RefreshRateConfigs::RefreshRateType refreshRateType) const = 0;
Ana Krulec757f63a2019-01-25 10:46:18 -080048 virtual Offsets getCurrentOffsets() const = 0;
49 virtual void setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType) = 0;
Ady Abrahambe0f9482019-04-24 15:41:53 -070050 virtual nsecs_t getOffsetThresholdForNextVsync() const = 0;
Ana Krulec757f63a2019-01-25 10:46:18 -080051 virtual void dump(std::string& result) const = 0;
52};
53
54namespace impl {
55class PhaseOffsets : public scheduler::PhaseOffsets {
56public:
57 PhaseOffsets();
58
59 nsecs_t getCurrentAppOffset() override;
60 nsecs_t getCurrentSfOffset() override;
61
Ady Abraham796beb02019-04-11 15:23:07 -070062 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
63 Offsets getOffsetsForRefreshRate(
64 RefreshRateConfigs::RefreshRateType refreshRateType) const override;
65
Ana Krulec757f63a2019-01-25 10:46:18 -080066 // Returns early, early GL, and late offsets for Apps and SF.
Ady Abraham796beb02019-04-11 15:23:07 -070067 Offsets getCurrentOffsets() const override {
68 return getOffsetsForRefreshRate(mRefreshRateType);
69 }
Ana Krulec757f63a2019-01-25 10:46:18 -080070
71 // This function should be called when the device is switching between different
72 // refresh rates, to properly update the offsets.
73 void setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType) override {
74 mRefreshRateType = refreshRateType;
75 }
76
Ady Abrahambe0f9482019-04-24 15:41:53 -070077 nsecs_t getOffsetThresholdForNextVsync() const override { return mOffsetThresholdForNextVsync; }
78
Ana Krulec757f63a2019-01-25 10:46:18 -080079 // Returns current offsets in human friendly format.
80 void dump(std::string& result) const override;
81
82private:
Ana Krulec757f63a2019-01-25 10:46:18 -080083 std::atomic<RefreshRateConfigs::RefreshRateType> mRefreshRateType =
84 RefreshRateConfigs::RefreshRateType::DEFAULT;
85
Alec Mourid7599d82019-05-22 19:58:00 -070086 std::unordered_map<RefreshRateConfigs::RefreshRateType, Offsets> mOffsets;
Ady Abrahambe0f9482019-04-24 15:41:53 -070087 nsecs_t mOffsetThresholdForNextVsync;
Ana Krulec757f63a2019-01-25 10:46:18 -080088};
89} // namespace impl
90
91} // namespace scheduler
92} // namespace android