• 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 
17 #ifndef VSYNC_VSYNC_GENERATOR_H
18 #define VSYNC_VSYNC_GENERATOR_H
19 
20 #include <refbase.h>
21 #include "graphic_common.h"
22 
23 #include <mutex>
24 #include <vector>
25 #include <thread>
26 #include <condition_variable>
27 
28 namespace OHOS {
29 namespace Rosen {
30 class VSyncGenerator : public RefBase {
31 public:
32     class Callback : public RefBase {
33     public:
34         virtual void OnVSyncEvent(int64_t) = 0;
35     };
36     VSyncGenerator() = default;
37     virtual ~VSyncGenerator() noexcept = default;
38     virtual VsyncError UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) = 0;
39     virtual VsyncError AddListener(int64_t phase, const sptr<Callback>& cb) = 0;
40     virtual VsyncError RemoveListener(const sptr<Callback>& cb) = 0;
41     virtual VsyncError ChangePhaseOffset(const sptr<Callback>& cb, int64_t offset) = 0;
42 };
43 
44 sptr<VSyncGenerator> CreateVSyncGenerator();
45 void DestroyVSyncGenerator();
46 
47 namespace impl {
48 class VSyncGenerator : public OHOS::Rosen::VSyncGenerator {
49 public:
50     static sptr<OHOS::Rosen::VSyncGenerator> GetInstance() noexcept;
51     static void DeleteInstance() noexcept;
52 
53     // nocopyable
54     VSyncGenerator(const VSyncGenerator &) = delete;
55     VSyncGenerator &operator=(const VSyncGenerator &) = delete;
56     VsyncError UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) override;
57     VsyncError AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
58     VsyncError RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
59     VsyncError ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset) override;
60 
61 private:
62     friend class OHOS::Rosen::VSyncGenerator;
63 
64     struct Listener {
65         int64_t phase_;
66         sptr<OHOS::Rosen::VSyncGenerator::Callback> callback_;
67         int64_t lastTime_;
68     };
69 
70     VSyncGenerator();
71     ~VSyncGenerator() noexcept override;
72 
73     int64_t ComputeNextVSyncTimeStamp(int64_t now);
74     std::vector<Listener> GetListenerTimeouted(int64_t now);
75     int64_t ComputeListenerNextVSyncTimeStamp(const Listener &listen, int64_t now);
76     void ThreadLoop();
77 
78     int64_t period_;
79     int64_t phase_;
80     int64_t refrenceTime_;
81     int64_t wakeupDelay_;
82 
83     std::vector<Listener> listeners_;
84 
85     std::mutex mutex_;
86     std::condition_variable con_;
87     std::mutex waitForTimeoutMtx_;
88     std::condition_variable waitForTimeoutCon_;
89     std::thread thread_;
90     bool vsyncThreadRunning_;
91     static std::once_flag createFlag_;
92     static sptr<OHOS::Rosen::VSyncGenerator> instance_;
93 };
94 } // impl
95 } // namespace Rosen
96 } // namespace OHOS
97 
98 #endif
99