• 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 "rs_process_transaction_controller.h"
20 #include "rs_transaction.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25     const std::string CLOSE_SYNC_TRANSACTION_TIMEOUT_TASK = "RSCloseSyncTransactionTimeoutTask";
26     constexpr int64_t CLOSE_SYNC_TRANSACTION_TIMEOUT_MILLISECONDS = 100;
27 }
28 
29 std::once_flag RSSyncTransactionController::flag_;
30 RSSyncTransactionController* RSSyncTransactionController::instance_ = nullptr;
31 
GetInstance()32 RSSyncTransactionController* RSSyncTransactionController::GetInstance()
33 {
34     std::call_once(flag_, &RSSyncTransactionController::Init);
35     return instance_;
36 }
37 
~RSSyncTransactionController()38 RSSyncTransactionController::~RSSyncTransactionController()
39 {}
40 
Init()41 void RSSyncTransactionController::Init()
42 {
43     instance_ = new RSSyncTransactionController();
44     ::atexit(&RSSyncTransactionController::Destroy);
45 }
46 
Destroy()47 void RSSyncTransactionController::Destroy()
48 {
49     instance_ = nullptr;
50 }
51 
RSSyncTransactionController()52 RSSyncTransactionController::RSSyncTransactionController()
53 {
54     runner_ = AppExecFwk::EventRunner::Create("RSSyncTransactionController");
55     handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
56     rsTransaction_ = std::make_shared<RSTransaction>();
57     auto startCallback = [this]() {
58         StartCreateTransaction();
59     };
60     rsTransaction_->SetCreateStartCallback(startCallback);
61     auto finishCallback = [this]() {
62         CreateTransactionFinished();
63     };
64     rsTransaction_->SetCreateFinishCallback(finishCallback);
65 }
66 
CreateTransactionFinished()67 void RSSyncTransactionController::CreateTransactionFinished()
68 {
69     {
70         std::unique_lock<std::mutex> lock(mutex_);
71         if (processCount_ == 0) {
72             return;
73         }
74         processCount_--;
75         transactionCount_++;
76     }
77     if (processCount_ == 0) {
78         ROSEN_LOGD("RS sync transaction controller CreateTransactionFinished, processCount: %d", processCount_);
79         CloseSyncTransaction();
80     }
81 }
82 
StartCreateTransaction()83 void RSSyncTransactionController::StartCreateTransaction()
84 {
85     std::unique_lock<std::mutex> lock(mutex_);
86     if (processCount_ < 0) {
87         processCount_ = 0;
88     }
89     processCount_++;
90     ROSEN_LOGD("RS sync transaction controller StartCreateTransaction, processCount: %d", processCount_);
91 }
92 
OpenSyncTransaction()93 void RSSyncTransactionController::OpenSyncTransaction()
94 {
95     {
96         std::unique_lock<std::mutex> lock(mutex_);
97         if (needCloseSync_) {
98             return;
99         }
100         needCloseSync_ = true;
101     }
102     ROSEN_LOGD("RS sync transaction controller OpenSyncTransaction");
103     if (rsTransaction_) {
104         rsTransaction_->OpenSyncTransaction();
105     }
106     if (handler_) {
107         auto task = [this]() {
108             ROSEN_LOGD("RS sync transaction controller close timeout task run");
109             CloseSyncTransaction();
110         };
111         handler_->PostTask(task, CLOSE_SYNC_TRANSACTION_TIMEOUT_TASK, CLOSE_SYNC_TRANSACTION_TIMEOUT_MILLISECONDS);
112     }
113 }
114 
CloseSyncTransaction()115 void RSSyncTransactionController::CloseSyncTransaction()
116 {
117     if (!needCloseSync_) {
118         return;
119     }
120 
121     ROSEN_LOGD("RS sync transaction controller CloseSyncTransaction");
122     if (rsTransaction_) {
123         rsTransaction_->CloseSyncTransaction(transactionCount_);
124     }
125     {
126         std::unique_lock<std::mutex> lock(mutex_);
127         processCount_ = 0;
128         transactionCount_ = 0;
129         needCloseSync_ = false;
130     }
131 }
132 } // namespace Rosen
133 } // namespace OHOS
134