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