• 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 "rs_sync_transaction_controller.h"
17 
18 #include "platform/common/rs_log.h"
19 #include "platform/common/rs_system_properties.h"
20 #include "rs_transaction.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 std::once_flag RSSyncTransactionController::flag_;
25 RSSyncTransactionController* RSSyncTransactionController::instance_ = nullptr;
26 
GetInstance()27 RSSyncTransactionController* RSSyncTransactionController::GetInstance()
28 {
29     std::call_once(flag_, &RSSyncTransactionController::Init);
30     return instance_;
31 }
32 
~RSSyncTransactionController()33 RSSyncTransactionController::~RSSyncTransactionController()
34 {}
35 
Init()36 void RSSyncTransactionController::Init()
37 {
38     instance_ = new RSSyncTransactionController();
39     ::atexit(&RSSyncTransactionController::Destroy);
40 }
41 
Destroy()42 void RSSyncTransactionController::Destroy()
43 {
44     instance_ = nullptr;
45 }
46 
RSSyncTransactionController()47 RSSyncTransactionController::RSSyncTransactionController() : rsTransaction_(std::make_shared<RSTransaction>()) {}
48 
GetRSTransaction()49 std::shared_ptr<RSTransaction> RSSyncTransactionController::GetRSTransaction()
50 {
51     if (!needCloseSync_) {
52         return nullptr;
53     }
54     return rsTransaction_;
55 }
56 
OpenSyncTransaction(std::shared_ptr<AppExecFwk::EventHandler> handler)57 void RSSyncTransactionController::OpenSyncTransaction(std::shared_ptr<AppExecFwk::EventHandler> handler)
58 {
59     if (!RSSystemProperties::GetSyncTransactionEnabled()) {
60         return;
61     }
62 
63     {
64         std::unique_lock<std::mutex> lock(mutex_);
65         if (needCloseSync_) {
66             return;
67         }
68         needCloseSync_ = true;
69     }
70     ROSEN_LOGD("RS sync transaction controller OpenSyncTransaction");
71     if (rsTransaction_) {
72         rsTransaction_->OpenSyncTransaction(handler);
73     }
74 }
75 
CloseSyncTransaction(std::shared_ptr<AppExecFwk::EventHandler> handler)76 void RSSyncTransactionController::CloseSyncTransaction(std::shared_ptr<AppExecFwk::EventHandler> handler)
77 {
78     if (!needCloseSync_) {
79         return;
80     }
81 
82     ROSEN_LOGD("RS sync transaction controller CloseSyncTransaction");
83     if (rsTransaction_) {
84         rsTransaction_->CloseSyncTransaction(handler);
85     }
86     {
87         std::unique_lock<std::mutex> lock(mutex_);
88         needCloseSync_ = false;
89     }
90 }
91 } // namespace Rosen
92 } // namespace OHOS
93