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 21 #include "RefreshRateConfigs.h" 22 #include "VSyncModulator.h" 23 24 namespace android { 25 namespace scheduler { 26 27 /* 28 * This class encapsulates offsets for different refresh rates. Depending 29 * on what refresh rate we are using, and wheter we are composing in GL, 30 * different offsets will help us with latency. This class keeps track of 31 * which mode the device is on, and returns approprate offsets when needed. 32 */ 33 class PhaseOffsets { 34 public: 35 struct Offsets { 36 VSyncModulator::Offsets early; 37 VSyncModulator::Offsets earlyGl; 38 VSyncModulator::Offsets late; 39 }; 40 41 virtual ~PhaseOffsets(); 42 43 virtual nsecs_t getCurrentAppOffset() = 0; 44 virtual nsecs_t getCurrentSfOffset() = 0; 45 virtual Offsets getOffsetsForRefreshRate( 46 RefreshRateConfigs::RefreshRateType refreshRateType) const = 0; 47 virtual Offsets getCurrentOffsets() const = 0; 48 virtual void setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType) = 0; 49 virtual nsecs_t getOffsetThresholdForNextVsync() const = 0; 50 virtual void dump(std::string& result) const = 0; 51 }; 52 53 namespace impl { 54 class PhaseOffsets : public scheduler::PhaseOffsets { 55 public: 56 PhaseOffsets(); 57 58 nsecs_t getCurrentAppOffset() override; 59 nsecs_t getCurrentSfOffset() override; 60 61 // Returns early, early GL, and late offsets for Apps and SF for a given refresh rate. 62 Offsets getOffsetsForRefreshRate( 63 RefreshRateConfigs::RefreshRateType refreshRateType) const override; 64 65 // Returns early, early GL, and late offsets for Apps and SF. getCurrentOffsets()66 Offsets getCurrentOffsets() const override { 67 return getOffsetsForRefreshRate(mRefreshRateType); 68 } 69 70 // This function should be called when the device is switching between different 71 // refresh rates, to properly update the offsets. setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType)72 void setRefreshRateType(RefreshRateConfigs::RefreshRateType refreshRateType) override { 73 mRefreshRateType = refreshRateType; 74 } 75 getOffsetThresholdForNextVsync()76 nsecs_t getOffsetThresholdForNextVsync() const override { return mOffsetThresholdForNextVsync; } 77 78 // Returns current offsets in human friendly format. 79 void dump(std::string& result) const override; 80 81 private: getDefaultRefreshRateOffsets()82 Offsets getDefaultRefreshRateOffsets() { return mDefaultRefreshRateOffsets; } getHighRefreshRateOffsets()83 Offsets getHighRefreshRateOffsets() { return mHighRefreshRateOffsets; } 84 85 std::atomic<RefreshRateConfigs::RefreshRateType> mRefreshRateType = 86 RefreshRateConfigs::RefreshRateType::DEFAULT; 87 88 Offsets mDefaultRefreshRateOffsets; 89 Offsets mHighRefreshRateOffsets; 90 nsecs_t mOffsetThresholdForNextVsync; 91 }; 92 } // namespace impl 93 94 } // namespace scheduler 95 } // namespace android 96