1 /*
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>
20 #include <unordered_map>
21 
22 #include "RefreshRateConfigs.h"
23 #include "VSyncModulator.h"
24 
25 namespace android {
26 namespace 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  */
34 class PhaseOffsets {
35 public:
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;
46     virtual Offsets getOffsetsForRefreshRate(
47             RefreshRateConfigs::RefreshRateType refreshRateType) const = 0;
48     virtual Offsets getCurrentOffsets() const = 0;
49     virtual void setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType) = 0;
50     virtual nsecs_t getOffsetThresholdForNextVsync() const = 0;
51     virtual void dump(std::string& result) const = 0;
52 };
53 
54 namespace impl {
55 class PhaseOffsets : public scheduler::PhaseOffsets {
56 public:
57     PhaseOffsets();
58 
59     nsecs_t getCurrentAppOffset() override;
60     nsecs_t getCurrentSfOffset() override;
61 
62     // 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 
66     // Returns early, early GL, and late offsets for Apps and SF.
getCurrentOffsets()67     Offsets getCurrentOffsets() const override {
68         return getOffsetsForRefreshRate(mRefreshRateType);
69     }
70 
71     // This function should be called when the device is switching between different
72     // refresh rates, to properly update the offsets.
setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType)73     void setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType) override {
74         mRefreshRateType = refreshRateType;
75     }
76 
getOffsetThresholdForNextVsync()77     nsecs_t getOffsetThresholdForNextVsync() const override { return mOffsetThresholdForNextVsync; }
78 
79     // Returns current offsets in human friendly format.
80     void dump(std::string& result) const override;
81 
82 private:
83     std::atomic<RefreshRateConfigs::RefreshRateType> mRefreshRateType =
84             RefreshRateConfigs::RefreshRateType::DEFAULT;
85 
86     std::unordered_map<RefreshRateConfigs::RefreshRateType, Offsets> mOffsets;
87     nsecs_t mOffsetThresholdForNextVsync;
88 };
89 } // namespace impl
90 
91 } // namespace scheduler
92 } // namespace android
93