• 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_single_instance.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 class VsyncStation {
34 WM_DECLARE_SINGLE_INSTANCE_BASE(VsyncStation);
35 using OnCallback = std::function<void(int64_t)>;
36 public:
37     enum class CallbackType {
38         CALLBACK_INPUT = 0,
39         CALLBACK_FRAME = 1,
40     };
41     struct VsyncCallback {
42         OnCallback onCallback;
43     };
44     ~VsyncStation() = default;
45     void RequestVsync(CallbackType type, const std::shared_ptr<VsyncCallback>& vsyncCallback);
46     void RemoveCallback(CallbackType type, const std::shared_ptr<VsyncCallback>& vsyncCallback);
SetIsMainHandlerAvailable(bool available)47     void SetIsMainHandlerAvailable(bool available)
48     {
49         isMainHandlerAvailable_ = available;
50     }
51 
52 private:
53     VsyncStation() = default;
54     static void OnVsync(int64_t nanoTimestamp, void* client);
55     void VsyncCallbackInner(int64_t nanoTimestamp);
56     void OnVsyncTimeOut();
57     AppExecFwk::EventHandler::Callback vsyncTimeoutCallback_ = std::bind(&VsyncStation::OnVsyncTimeOut, this);
58     const std::string VSYNC_THREAD_ID = "vsync_thread";
59     std::shared_ptr<AppExecFwk::EventHandler> vsyncHandler_ = nullptr;
60     std::mutex mtx_;
61     bool hasRequestedVsync_ = false;
62     bool isMainHandlerAvailable_ = false;
63     uint32_t vsyncCount_ = 0;
64     std::map<CallbackType, std::unordered_set<std::shared_ptr<VsyncCallback>>> vsyncCallbacks_ = {
65         {CallbackType::CALLBACK_INPUT, {}},
66         {CallbackType::CALLBACK_FRAME, {}},
67     };
68     VSyncReceiver::FrameCallback frameCallback_ = {
69         .userData_ = this,
70         .callback_ = OnVsync,
71     };
72     std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver_ = nullptr;
73 };
74 } // namespace Rosen
75 } // namespace OHOS
76 #endif // OHOS_VSYNC_STATION_H