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 VsyncStation { 35 WM_DECLARE_SINGLE_INSTANCE_BASE(VsyncStation); 36 public: 37 ~VsyncStation() = default; 38 void RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback); 39 void RemoveCallback(); SetIsMainHandlerAvailable(bool available)40 void SetIsMainHandlerAvailable(bool available) 41 { 42 isMainHandlerAvailable_ = available; 43 } 44 SetVsyncEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & eventHandler)45 void SetVsyncEventHandler(const std::shared_ptr<AppExecFwk::EventHandler>& eventHandler) 46 { 47 vsyncHandler_ = eventHandler; 48 } 49 50 private: 51 VsyncStation() = default; 52 static void OnVsync(int64_t nanoTimestamp, void* client); 53 void VsyncCallbackInner(int64_t nanoTimestamp); 54 void OnVsyncTimeOut(); 55 56 std::mutex mtx_; 57 bool hasRequestedVsync_ = false; 58 bool hasInitVsyncReceiver_ = false; 59 bool isMainHandlerAvailable_ = true; 60 const std::string VSYNC_THREAD_ID = "VsyncThread"; 61 std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver_ = nullptr; 62 std::unordered_set<std::shared_ptr<VsyncCallback>> vsyncCallbacks_; 63 VSyncReceiver::FrameCallback frameCallback_ = { 64 .userData_ = this, 65 .callback_ = OnVsync, 66 }; 67 std::shared_ptr<AppExecFwk::EventHandler> vsyncHandler_ = nullptr; 68 AppExecFwk::EventHandler::Callback vsyncTimeoutCallback_ = std::bind(&VsyncStation::OnVsyncTimeOut, this); 69 }; 70 } // namespace Rosen 71 } // namespace OHOS 72 #endif // OHOS_VSYNC_STATION_H