• 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 <memory>
20 #include <mutex>
21 
22 #include <refbase.h>
23 #include "graphic_common.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 class VSyncSampler : public RefBase {
28 public:
29     using SetScreenVsyncEnabledCallback = std::function<void(bool)>;
30     VSyncSampler() = default;
31     virtual ~VSyncSampler() noexcept = default;
32     virtual void Reset() = 0;
33     virtual void BeginSample() = 0;
34     virtual bool AddSample(int64_t timestamp) = 0;
35     virtual int64_t GetPeriod() const = 0;
36     virtual int64_t GetPhase() const = 0;
37     virtual int64_t GetRefrenceTime() const = 0;
38     virtual bool AddPresentFenceTime(int64_t timestamp) = 0;
39     virtual void SetHardwareVSyncStatus(bool enabled) = 0;
40     virtual bool GetHardwareVSyncStatus() const = 0;
41     virtual void RegSetScreenVsyncEnabledCallback(SetScreenVsyncEnabledCallback cb) = 0;
42     virtual void SetScreenVsyncEnabledInRSMainThread(bool enabled) = 0;
43 protected:
44     SetScreenVsyncEnabledCallback setScreenVsyncEnabledCallback_ = nullptr;
45 };
46 
47 sptr<VSyncSampler> CreateVSyncSampler();
48 
49 namespace impl {
50 class VSyncSampler : public OHOS::Rosen::VSyncSampler {
51 public:
52     static sptr<OHOS::Rosen::VSyncSampler> GetInstance() noexcept;
53 
54     // nocopyable
55     VSyncSampler(const VSyncSampler &) = delete;
56     VSyncSampler &operator=(const VSyncSampler &) = delete;
57     virtual void Reset() override;
58     virtual void BeginSample() override;
59     virtual bool AddSample(int64_t timestamp) override;
60     virtual int64_t GetPeriod() const override;
61     virtual int64_t GetPhase() const override;
62     virtual int64_t GetRefrenceTime() const override;
63     virtual bool AddPresentFenceTime(int64_t timestamp) override;
64     virtual void SetHardwareVSyncStatus(bool enabled) override;
65     virtual bool GetHardwareVSyncStatus() const override;
66     virtual void RegSetScreenVsyncEnabledCallback(SetScreenVsyncEnabledCallback cb) override;
67     virtual void SetScreenVsyncEnabledInRSMainThread(bool enabled) override;
68 
69 private:
70     friend class OHOS::Rosen::VSyncSampler;
71     enum : uint32_t { MAX_SAMPLES = 32 };
72     enum : uint32_t { MIN_SAMPLES_FOR_UPDATE = 6 };
73     enum : uint32_t { MAX_SAMPLES_WITHOUT_PRESENT = 4 };
74     enum : uint32_t { NUM_PRESENT = 8 };
75 
76     VSyncSampler();
77     ~VSyncSampler() noexcept override;
78 
79     void UpdateModeLocked();
80     void UpdateErrorLocked();
81     void ResetErrorLocked();
82 
83     int64_t period_;
84     int64_t phase_;
85     int64_t referenceTime_;
86     int64_t error_;
87     int64_t samples_[MAX_SAMPLES] = {0};
88     int64_t presentFenceTime_[NUM_PRESENT] = {-1};
89     uint32_t firstSampleIndex_;
90     uint32_t numSamples_;
91     bool modeUpdated_;
92     uint32_t numResyncSamplesSincePresent_ = 0;
93     uint32_t presentFenceTimeOffset_ = 0;
94 
95     mutable std::mutex mutex_;
96 
97     static std::once_flag createFlag_;
98     static sptr<OHOS::Rosen::VSyncSampler> instance_;
99     bool hardwareVSyncStatus_ = true;
100 };
101 } // impl
102 } // namespace Rosen
103 } // namespace OHOS
104 
105 #endif // VSYNC_VSYNC_SAMPLER_H
106