• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "js_transaction_manager.h"
17 
18 #include <transaction/rs_sync_transaction_controller.h>
19 #include <transaction/rs_transaction.h>
20 
21 #include "js_runtime_utils.h"
22 #include "scene_session_manager.h"
23 #include "window_manager_hilog.h"
24 
25 namespace OHOS::Rosen {
26 using namespace AbilityRuntime;
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "JsTransactionManager" };
29 } // namespace
30 
NapiGetUndefined(napi_env env)31 napi_value NapiGetUndefined(napi_env env)
32 {
33     napi_value result = nullptr;
34     napi_get_undefined(env, &result);
35     return result;
36 }
37 
Init(napi_env env,napi_value exportObj)38 napi_value JsTransactionManager::Init(napi_env env, napi_value exportObj)
39 {
40     TLOGD(WmsLogTag::DEFAULT, "JsTransactionManager Init");
41     if (env == nullptr || exportObj == nullptr) {
42         WLOGFE("env or exportObj is null!");
43         return nullptr;
44     }
45 
46     std::unique_ptr<JsTransactionManager> jsTransactionManager = std::make_unique<JsTransactionManager>(env);
47     napi_wrap(env, exportObj, jsTransactionManager.release(), JsTransactionManager::Finalizer, nullptr, nullptr);
48 
49     const char* moduleName = "JsTransactionManager";
50     BindNativeFunction(env, exportObj, "openSyncTransaction", moduleName,
51         JsTransactionManager::OpenSyncTransaction);
52     BindNativeFunction(env, exportObj, "closeSyncTransaction", moduleName,
53         JsTransactionManager::CloseSyncTransaction);
54     return NapiGetUndefined(env);
55 }
56 
JsTransactionManager(napi_env env)57 JsTransactionManager::JsTransactionManager(napi_env env)
58 {}
59 
Finalizer(napi_env env,void * data,void * hint)60 void JsTransactionManager::Finalizer(napi_env env, void* data, void* hint)
61 {
62     TLOGD(WmsLogTag::DEFAULT, "Enter");
63     std::unique_ptr<JsTransactionManager>(static_cast<JsTransactionManager*>(data));
64 }
65 
OpenSyncTransaction(napi_env env,napi_callback_info info)66 napi_value JsTransactionManager::OpenSyncTransaction(napi_env env, napi_callback_info info)
67 {
68     TLOGD(WmsLogTag::DEFAULT, "Enter");
69     JsTransactionManager* me = CheckParamsAndGetThis<JsTransactionManager>(env, info);
70     return (me != nullptr) ? me->OnOpenSyncTransaction(env, info) : nullptr;
71 }
72 
CloseSyncTransaction(napi_env env,napi_callback_info info)73 napi_value JsTransactionManager::CloseSyncTransaction(napi_env env, napi_callback_info info)
74 {
75     TLOGD(WmsLogTag::DEFAULT, "Enter");
76     JsTransactionManager* me = CheckParamsAndGetThis<JsTransactionManager>(env, info);
77     return (me != nullptr) ? me->OnCloseSyncTransaction(env, info) : nullptr;
78 }
79 
OnOpenSyncTransaction(napi_env env,napi_callback_info info)80 napi_value JsTransactionManager::OnOpenSyncTransaction(napi_env env, napi_callback_info info)
81 {
82     auto task = []() {
83         auto transactionController = RSSyncTransactionController::GetInstance();
84         if (transactionController) {
85             RSTransaction::FlushImplicitTransaction();
86             transactionController->OpenSyncTransaction();
87         };
88     };
89     auto handler = SceneSessionManager::GetInstance().GetTaskScheduler();
90     if (handler) {
91         // must sync to include change
92         handler->PostVoidSyncTask(task);
93     } else {
94         task();
95     }
96     return NapiGetUndefined(env);
97 }
98 
OnCloseSyncTransaction(napi_env env,napi_callback_info info)99 napi_value JsTransactionManager::OnCloseSyncTransaction(napi_env env, napi_callback_info info)
100 {
101     auto task = [] {
102         if (auto transactionController = RSSyncTransactionController::GetInstance()) {
103             transactionController->CloseSyncTransaction();
104         }
105     };
106     if (auto handler = SceneSessionManager::GetInstance().GetTaskScheduler()) {
107         handler->PostAsyncTask(task, __func__);
108     } else {
109         task();
110     }
111     return NapiGetUndefined(env);
112 }
113 } // namespace OHOS::Rosen
114