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