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 FOUNDATION_ACE_INTERFACE_INNERKITS_JS_DISPLAY_SYNC_H_
17 #define FOUNDATION_ACE_INTERFACE_INNERKITS_JS_DISPLAY_SYNC_H_
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 #include "base/memory/referenced.h"
26 #include "core/components_ng/manager/display_sync/ui_display_sync.h"
27 
28 namespace OHOS::Ace::Napi {
29 enum class CallbackType {
30     ONFRAME = 0,
31     UNKNOW,
32 };
33 
34 class DisplaySync final {
35 public:
36     DisplaySync() = delete;
DisplaySync(RefPtr<UIDisplaySync> & uiDisplaySync)37     explicit DisplaySync(RefPtr<UIDisplaySync>& uiDisplaySync)
38         : uiDisplaySync_(uiDisplaySync) {}
39 
~DisplaySync()40     ~DisplaySync()
41     {
42         if (uiDisplaySync_) {
43             uiDisplaySync_->DelFromPipelineOnContainer();
44         }
45     }
46 
47     void Initialize(napi_env env, napi_value thisVar);
48     void NapiSerializer(napi_env& env, napi_value& result);
49     void RegisterOnFrameCallback(napi_value cb, napi_ref& onFrameRef, CallbackType callbackType, napi_env env);
50     void UnregisterOnFrameCallback(napi_env env, size_t argc, napi_ref& onFrameRef);
51     void Destroy(napi_env env);
52 
GetUIDisplaySync()53     RefPtr<UIDisplaySync> GetUIDisplaySync() const
54     {
55         return uiDisplaySync_;
56     }
57 
GetOnframeRef()58     napi_ref GetOnframeRef() const
59     {
60         return onFrameRef_;
61     }
62 
SetOnframeRef(const napi_ref & onframe)63     void SetOnframeRef(const napi_ref& onframe)
64     {
65         onFrameRef_ = onframe;
66     }
67 
68     napi_ref thisVarRef_ = nullptr;
69     napi_ref onFrameRef_ = nullptr;
70 
71 private:
72     RefPtr<UIDisplaySync> uiDisplaySync_;
73 };
74 } // namespace OHOS::Ace::Napi
75 
76 #endif // FOUNDATION_ACE_INTERFACE_INNERKITS_JS_DISPLAY_SYNC_H_
77