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; |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 35 | |
| 36 | virtual ~PhaseOffsets(); |
| 37 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 38 | 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 Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 44 | virtual Offsets getCurrentOffsets() const = 0; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 45 | virtual Offsets getOffsetsForRefreshRate(float fps) const = 0; |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 46 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 47 | virtual void setRefreshRateFps(float fps) = 0; |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 48 | |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 49 | virtual void dump(std::string& result) const = 0; |
| 50 | }; |
| 51 | |
| 52 | namespace impl { |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 53 | |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 54 | class PhaseOffsets : public scheduler::PhaseOffsets { |
| 55 | public: |
| 56 | PhaseOffsets(); |
| 57 | |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 58 | // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 59 | Offsets getOffsetsForRefreshRate(float fps) const override; |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 60 | |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 61 | // Returns early, early GL, and late offsets for Apps and SF. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 62 | Offsets getCurrentOffsets() const override { return getOffsetsForRefreshRate(mRefreshRateFps); } |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 63 | |
| 64 | // This function should be called when the device is switching between different |
| 65 | // refresh rates, to properly update the offsets. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 66 | void setRefreshRateFps(float fps) override { mRefreshRateFps = fps; } |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 67 | |
| 68 | // Returns current offsets in human friendly format. |
| 69 | void dump(std::string& result) const override; |
| 70 | |
| 71 | private: |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 72 | static Offsets getDefaultOffsets(nsecs_t thresholdForNextVsync); |
| 73 | static Offsets getHighFpsOffsets(nsecs_t thresholdForNextVsync); |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 74 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 75 | std::atomic<float> mRefreshRateFps = 0; |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 76 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 77 | Offsets mDefaultOffsets; |
| 78 | Offsets mHighFpsOffsets; |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 79 | }; |
Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 80 | |
Dominik Laskowski | eddeda1 | 2019-07-19 11:54:13 -0700 | [diff] [blame] | 81 | } // namespace impl |
| 82 | } // namespace android::scheduler |