Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 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 | |
Alec Mouri | d7599d8 | 2019-05-22 19:58:00 -0700 | [diff] [blame] | 19 | #include <unordered_map> |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 20 | |
| 21 | #include "RefreshRateConfigs.h" |
| 22 | #include "VSyncModulator.h" |
| 23 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 24 | namespace android::scheduler { |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 25 | |
| 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 | */ |
| 32 | class PhaseOffsets { |
| 33 | public: |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 34 | using Offsets = VSyncModulator::OffsetsConfig; |
| 35 | using RefreshRateType = RefreshRateConfigs::RefreshRateType; |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 36 | |
| 37 | virtual ~PhaseOffsets(); |
| 38 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 39 | 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 Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 45 | virtual Offsets getCurrentOffsets() const = 0; |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 46 | virtual Offsets getOffsetsForRefreshRate(RefreshRateType) const = 0; |
| 47 | |
| 48 | virtual void setRefreshRateType(RefreshRateType) = 0; |
| 49 | |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 50 | virtual void dump(std::string& result) const = 0; |
| 51 | }; |
| 52 | |
| 53 | namespace impl { |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 54 | |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 55 | class PhaseOffsets : public scheduler::PhaseOffsets { |
| 56 | public: |
| 57 | PhaseOffsets(); |
| 58 | |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 59 | // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate. |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 60 | Offsets getOffsetsForRefreshRate(RefreshRateType) const override; |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 61 | |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 62 | // Returns early, early GL, and late offsets for Apps and SF. |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 63 | Offsets getCurrentOffsets() const override { |
| 64 | return getOffsetsForRefreshRate(mRefreshRateType); |
| 65 | } |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 66 | |
| 67 | // This function should be called when the device is switching between different |
| 68 | // refresh rates, to properly update the offsets. |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 69 | void setRefreshRateType(RefreshRateType refreshRateType) override { |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 70 | mRefreshRateType = refreshRateType; |
| 71 | } |
| 72 | |
| 73 | // Returns current offsets in human friendly format. |
| 74 | void dump(std::string& result) const override; |
| 75 | |
| 76 | private: |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 77 | static Offsets getDefaultOffsets(nsecs_t thresholdForNextVsync); |
| 78 | static Offsets getHighFpsOffsets(nsecs_t thresholdForNextVsync); |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 79 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 80 | std::atomic<RefreshRateType> mRefreshRateType = RefreshRateType::DEFAULT; |
| 81 | |
| 82 | std::unordered_map<RefreshRateType, Offsets> mOffsets; |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 83 | }; |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 84 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame^] | 85 | } // namespace impl |
| 86 | } // namespace android::scheduler |