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 virtual void ClearAllSamples() = 0; 48 virtual int32_t StartSample(bool forceReSample) = 0; 49 protected: 50 SetScreenVsyncEnabledCallback setScreenVsyncEnabledCallback_ = nullptr; 51 }; 52 53 sptr<VSyncSampler> CreateVSyncSampler(); 54 55 namespace impl { 56 class VSyncSampler : public OHOS::Rosen::VSyncSampler { 57 public: 58 static sptr<OHOS::Rosen::VSyncSampler> GetInstance() noexcept; 59 60 // nocopyable 61 VSyncSampler(const VSyncSampler &) = delete; 62 VSyncSampler &operator=(const VSyncSampler &) = delete; 63 virtual void Reset() override; 64 virtual void BeginSample() override; 65 virtual bool AddSample(int64_t timestamp) override; 66 virtual int64_t GetPeriod() const override; 67 virtual int64_t GetPhase() const override; 68 virtual int64_t GetRefrenceTime() const override; 69 virtual bool AddPresentFenceTime(int64_t timestamp) override; 70 virtual void SetHardwareVSyncStatus(bool enabled) override; 71 virtual bool GetHardwareVSyncStatus() const override; 72 virtual void RegSetScreenVsyncEnabledCallback(SetScreenVsyncEnabledCallback cb) override; 73 virtual void SetScreenVsyncEnabledInRSMainThread(bool enabled) override; 74 virtual int64_t GetHardwarePeriod() const override; 75 virtual void SetPendingPeriod(int64_t period) override; 76 virtual void Dump(std::string &result) override; 77 virtual void ClearAllSamples() override; 78 virtual int32_t StartSample(bool forceReSample) override; 79 80 private: 81 friend class OHOS::Rosen::VSyncSampler; 82 enum : uint32_t { MAX_SAMPLES = 32 }; 83 enum : uint32_t { MIN_SAMPLES_FOR_UPDATE = 6 }; 84 enum : uint32_t { MAX_SAMPLES_WITHOUT_PRESENT = 4 }; 85 enum : uint32_t { NUM_PRESENT = 8 }; 86 87 VSyncSampler(); 88 ~VSyncSampler() override; 89 90 void UpdateModeLocked(); 91 void UpdateErrorLocked(); 92 void ResetErrorLocked(); 93 void UpdateReferenceTimeLocked(); 94 void CheckIfFirstRefreshAfterIdleLocked(); 95 void ComputePhaseLocked(); 96 97 int64_t period_; 98 int64_t phase_; 99 int64_t referenceTime_; 100 double error_; 101 int64_t samples_[MAX_SAMPLES] = {0}; 102 int64_t presentFenceTime_[NUM_PRESENT] = {-1}; 103 uint32_t firstSampleIndex_; 104 uint32_t numSamples_; 105 bool modeUpdated_; 106 uint32_t numResyncSamplesSincePresent_ = 0; 107 uint32_t presentFenceTimeOffset_ = 0; 108 109 mutable std::mutex mutex_; 110 111 static std::once_flag createFlag_; 112 static sptr<OHOS::Rosen::VSyncSampler> instance_; 113 bool hardwareVSyncStatus_ = true; 114 int64_t pendingPeriod_ = 0; 115 std::atomic<bool> enableVsyncSample_ = true; 116 }; 117 } // impl 118 } // namespace Rosen 119 } // namespace OHOS 120 121 #endif // VSYNC_VSYNC_SAMPLER_H 122