1 /*
2  * Copyright (c) 2021-2024 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 OHOS_VSYNC_STATION_H
17 #define OHOS_VSYNC_STATION_H
18 
19 #include <memory>
20 #include <unordered_set>
21 
22 #include <event_handler.h>
23 #include <ui/rs_display_node.h>
24 #include <vsync_receiver.h>
25 
26 #include "wm_common.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 class RSFrameRateLinker;
31 
32 class VsyncStation : public std::enable_shared_from_this<VsyncStation> {
33 public:
34     explicit VsyncStation(NodeId nodeId,
35         const std::shared_ptr<AppExecFwk::EventHandler>& vsyncHandler = nullptr);
36     ~VsyncStation();
37 
38     bool IsVsyncReceiverCreated();
39     void RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback);
40     int64_t GetVSyncPeriod();
41     void RemoveCallback();
42     void Destroy();
43 
44     FrameRateLinkerId GetFrameRateLinkerId();
45     void FlushFrameRate(uint32_t rate, int32_t animatorExpectedFrameRate, uint32_t rateType = 0);
46     void SetFrameRateLinkerEnable(bool enabled);
47     void SetDisplaySoloistFrameRateLinkerEnable(bool enabled);
48     void SetUiDvsyncSwitch(bool dvsyncSwitch);
49 
50 private:
51     std::shared_ptr<VSyncReceiver> GetOrCreateVsyncReceiver();
52     std::shared_ptr<VSyncReceiver> GetOrCreateVsyncReceiverLocked();
53     std::shared_ptr<RSFrameRateLinker> GetFrameRateLinker();
54     void VsyncCallbackInner(int64_t nanoTimestamp, int64_t frameCount);
55     void OnVsyncTimeOut();
56 
57     NodeId nodeId_ = 0;
58     std::shared_ptr<AppExecFwk::EventHandler> vsyncHandler_ = nullptr;
59     std::string vsyncTimeoutTaskName_;
60 
61     std::mutex mutex_;
62     bool isFirstVsyncRequest_ = true;
63     bool isFirstVsyncBack_ = true;
64     bool destroyed_ = false;
65     bool hasRequestedVsync_ = false;
66     std::shared_ptr<VSyncReceiver> receiver_ = nullptr;
67     std::shared_ptr<RSFrameRateLinker> frameRateLinker_ = nullptr;
68     using Callbacks = std::unordered_set<std::shared_ptr<VsyncCallback>>;
69     Callbacks vsyncCallbacks_;
70     // Above guarded by mutex_
71 };
72 } // namespace Rosen
73 } // namespace OHOS
74 #endif // OHOS_VSYNC_STATION_H