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