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