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