1 /* 2 * Copyright (c) 2021-2022 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 <functional> 21 #include <map> 22 #include <memory> 23 #include <unordered_set> 24 25 #include <refbase.h> 26 #include <event_handler.h> 27 #include <vsync_receiver.h> 28 29 #include "wm_common.h" 30 #include "wm_single_instance.h" 31 32 namespace OHOS { 33 namespace Rosen { 34 class RSFrameRateLinker; 35 class VsyncStation { 36 WM_DECLARE_SINGLE_INSTANCE_BASE(VsyncStation); 37 public: ~VsyncStation()38 ~VsyncStation() 39 { 40 std::lock_guard<std::mutex> lock(mtx_); 41 destroyed_ = true; 42 } 43 void RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback); 44 int64_t GetVSyncPeriod(); 45 void FlushFrameRate(uint32_t rate, bool immediate); 46 void RemoveCallback(); SetIsMainHandlerAvailable(bool available)47 void SetIsMainHandlerAvailable(bool available) 48 { 49 isMainHandlerAvailable_ = available; 50 } 51 SetVsyncEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & eventHandler)52 void SetVsyncEventHandler(const std::shared_ptr<AppExecFwk::EventHandler>& eventHandler) 53 { 54 vsyncHandler_ = eventHandler; 55 } 56 57 private: 58 VsyncStation() = default; 59 static void OnVsync(int64_t nanoTimestamp, void* client); 60 void VsyncCallbackInner(int64_t nanoTimestamp); 61 void OnVsyncTimeOut(); 62 void Init(); 63 64 std::mutex mtx_; 65 bool hasRequestedVsync_ = false; 66 bool hasInitVsyncReceiver_ = false; 67 bool isMainHandlerAvailable_ = true; 68 bool destroyed_ = false; 69 const std::string VSYNC_THREAD_ID = "VsyncThread"; 70 std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver_ = nullptr; 71 std::shared_ptr<RSFrameRateLinker> frameRateLinker_; 72 std::unordered_set<std::shared_ptr<VsyncCallback>> vsyncCallbacks_; 73 VSyncReceiver::FrameCallback frameCallback_ = { 74 .userData_ = this, 75 .callback_ = OnVsync, 76 }; 77 std::shared_ptr<AppExecFwk::EventHandler> vsyncHandler_ = nullptr; 78 AppExecFwk::EventHandler::Callback vsyncTimeoutCallback_ = std::bind(&VsyncStation::OnVsyncTimeOut, this); 79 }; 80 } // namespace Rosen 81 } // namespace OHOS 82 #endif // OHOS_VSYNC_STATION_H