1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef VSYNC_VSYNC_SAMPLER_H 17 #define VSYNC_VSYNC_SAMPLER_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <mutex> 22 23 #include <refbase.h> 24 #include "graphic_common.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 class VSyncSampler : public RefBase { 29 public: 30 using SetScreenVsyncEnabledCallback = std::function<void(bool)>; 31 VSyncSampler() = default; 32 virtual ~VSyncSampler() noexcept = default; 33 virtual void Reset() = 0; 34 virtual void BeginSample() = 0; 35 virtual bool AddSample(int64_t timestamp) = 0; 36 virtual int64_t GetPeriod() const = 0; 37 virtual int64_t GetPhase() const = 0; 38 virtual int64_t GetRefrenceTime() const = 0; 39 virtual bool AddPresentFenceTime(int64_t timestamp) = 0; 40 virtual void SetHardwareVSyncStatus(bool enabled) = 0; 41 virtual bool GetHardwareVSyncStatus() const = 0; 42 virtual void RegSetScreenVsyncEnabledCallback(SetScreenVsyncEnabledCallback cb) = 0; 43 virtual void SetScreenVsyncEnabledInRSMainThread(bool enabled) = 0; 44 virtual int64_t GetHardwarePeriod() const = 0; 45 virtual void SetPendingPeriod(int64_t period) = 0; 46 virtual void Dump(std::string &result) = 0; 47 protected: 48 SetScreenVsyncEnabledCallback setScreenVsyncEnabledCallback_ = nullptr; 49 }; 50 51 sptr<VSyncSampler> CreateVSyncSampler(); 52 53 namespace impl { 54 class VSyncSampler : public OHOS::Rosen::VSyncSampler { 55 public: 56 static sptr<OHOS::Rosen::VSyncSampler> GetInstance() noexcept; 57 58 // nocopyable 59 VSyncSampler(const VSyncSampler &) = delete; 60 VSyncSampler &operator=(const VSyncSampler &) = delete; 61 virtual void Reset() override; 62 virtual void BeginSample() override; 63 virtual bool AddSample(int64_t timestamp) override; 64 virtual int64_t GetPeriod() const override; 65 virtual int64_t GetPhase() const override; 66 virtual int64_t GetRefrenceTime() const override; 67 virtual bool AddPresentFenceTime(int64_t timestamp) override; 68 virtual void SetHardwareVSyncStatus(bool enabled) override; 69 virtual bool GetHardwareVSyncStatus() const override; 70 virtual void RegSetScreenVsyncEnabledCallback(SetScreenVsyncEnabledCallback cb) override; 71 virtual void SetScreenVsyncEnabledInRSMainThread(bool enabled) override; 72 virtual int64_t GetHardwarePeriod() const override; 73 virtual void SetPendingPeriod(int64_t period) override; 74 virtual void Dump(std::string &result) override; 75 76 private: 77 friend class OHOS::Rosen::VSyncSampler; 78 enum : uint32_t { MAX_SAMPLES = 32 }; 79 enum : uint32_t { MIN_SAMPLES_FOR_UPDATE = 6 }; 80 enum : uint32_t { MAX_SAMPLES_WITHOUT_PRESENT = 4 }; 81 enum : uint32_t { NUM_PRESENT = 8 }; 82 83 VSyncSampler(); 84 ~VSyncSampler() noexcept override; 85 86 void UpdateModeLocked(); 87 void UpdateErrorLocked(); 88 void ResetErrorLocked(); 89 void UpdateReferenceTimeLocked(); 90 91 int64_t period_; 92 int64_t phase_; 93 int64_t referenceTime_; 94 int64_t error_; 95 int64_t samples_[MAX_SAMPLES] = {0}; 96 int64_t presentFenceTime_[NUM_PRESENT] = {-1}; 97 uint32_t firstSampleIndex_; 98 uint32_t numSamples_; 99 bool modeUpdated_; 100 uint32_t numResyncSamplesSincePresent_ = 0; 101 uint32_t presentFenceTimeOffset_ = 0; 102 103 mutable std::mutex mutex_; 104 105 static std::once_flag createFlag_; 106 static sptr<OHOS::Rosen::VSyncSampler> instance_; 107 bool hardwareVSyncStatus_ = true; 108 int64_t pendingPeriod_ = 0; 109 }; 110 } // impl 111 } // namespace Rosen 112 } // namespace OHOS 113 114 #endif // VSYNC_VSYNC_SAMPLER_H 115