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