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