• 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(uint64_t, bool)>;
31     VSyncSampler() = default;
32     virtual ~VSyncSampler() noexcept = default;
33     virtual bool AddSample(int64_t timestamp) = 0;
34     virtual int64_t GetPeriod() const = 0;
35     virtual int64_t GetPhase() const = 0;
36     virtual int64_t GetRefrenceTime() const = 0;
37     virtual bool AddPresentFenceTime(uint32_t screenId, int64_t timestamp) = 0;
38     virtual void SetHardwareVSyncStatus(bool enabled) = 0;
39     virtual void RegSetScreenVsyncEnabledCallback(SetScreenVsyncEnabledCallback cb) = 0;
40     virtual void SetScreenVsyncEnabledInRSMainThread(uint64_t screenId, bool enabled) = 0;
41     virtual int64_t GetHardwarePeriod() const = 0;
42     virtual void SetPendingPeriod(int64_t period) = 0;
43     virtual void Dump(std::string &result) = 0;
44     virtual void ClearAllSamples() = 0;
45     virtual void SetVsyncSamplerEnabled(bool enabled) = 0;
46     virtual bool GetVsyncSamplerEnabled() = 0;
47     virtual int32_t StartSample(bool forceReSample) = 0;
48     virtual void SetVsyncEnabledScreenId(uint64_t vsyncEnabledScreenId) = 0;
49     virtual uint64_t GetVsyncEnabledScreenId() = 0;
50     virtual void SetAdaptive(bool isAdaptive) = 0;
51     virtual void RecordDisplayVSyncStatus(bool enabled) = 0;
52     virtual void RollbackHardwareVSyncStatus() = 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 bool AddSample(int64_t timestamp) override;
68     virtual int64_t GetPeriod() const override;
69     virtual int64_t GetPhase() const override;
70     virtual int64_t GetRefrenceTime() const override;
71     virtual bool AddPresentFenceTime(uint32_t screenId, int64_t timestamp) override;
72     virtual void SetHardwareVSyncStatus(bool enabled) override;
73     virtual void RegSetScreenVsyncEnabledCallback(SetScreenVsyncEnabledCallback cb) override;
74     virtual void SetScreenVsyncEnabledInRSMainThread(uint64_t screenId, bool enabled) override;
75     virtual int64_t GetHardwarePeriod() const override;
76     virtual void SetPendingPeriod(int64_t period) override;
77     virtual void Dump(std::string &result) override;
78     virtual void ClearAllSamples() override;
79     virtual void SetVsyncSamplerEnabled(bool enabled) override;
80     virtual bool GetVsyncSamplerEnabled() override;
81     virtual int32_t StartSample(bool forceReSample) override;
82     virtual void SetVsyncEnabledScreenId(uint64_t vsyncEnabledScreenId) override;
83     virtual uint64_t GetVsyncEnabledScreenId() override;
84     virtual void SetAdaptive(bool isAdaptive) override;
85     virtual void RecordDisplayVSyncStatus(bool enabled) override;
86     virtual void RollbackHardwareVSyncStatus() 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 SetScreenVsyncEnabledInRSMainThreadLocked(uint64_t screenId, 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     bool displayVSyncStatus_ = false;
124     int64_t pendingPeriod_ = 0;
125     std::atomic<bool> enableVsyncSample_ = true;
126     uint64_t vsyncEnabledScreenId_ = UINT64_MAX;
127     bool isAdaptive_ = false;
128     int64_t lastAdaptiveTime_ = 0;
129 };
130 } // impl
131 } // namespace Rosen
132 } // namespace OHOS
133 
134 #endif // VSYNC_VSYNC_SAMPLER_H
135