• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "rs_transaction.h"
16 
17 #include "platform/common/rs_log.h"
18 #include "rs_trace.h"
19 #include "sandbox_utils.h"
20 #include "transaction/rs_transaction_proxy.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 
FlushImplicitTransaction()25 void RSTransaction::FlushImplicitTransaction()
26 {
27     auto transactionProxy = RSTransactionProxy::GetInstance();
28     if (transactionProxy != nullptr) {
29         transactionProxy->FlushImplicitTransaction();
30     }
31 }
32 
OpenSyncTransaction()33 void RSTransaction::OpenSyncTransaction()
34 {
35     syncId_ = GenerateSyncId();
36     auto transactionProxy = RSTransactionProxy::GetInstance();
37     if (transactionProxy != nullptr) {
38         RS_TRACE_NAME("OpenSyncTransaction");
39         transactionProxy->StartSyncTransaction();
40         transactionProxy->Begin();
41     }
42 }
43 
CloseSyncTransaction()44 void RSTransaction::CloseSyncTransaction()
45 {
46     auto transactionProxy = RSTransactionProxy::GetInstance();
47     if (transactionProxy != nullptr) {
48         RS_TRACE_NAME_FMT("CloseSyncTransaction syncId: %lu syncCount: %d", syncId_, transactionCount_);
49         transactionProxy->MarkTransactionNeedCloseSync(transactionCount_);
50         transactionProxy->SetSyncId(syncId_);
51         transactionProxy->CommitSyncTransaction();
52         transactionProxy->CloseSyncTransaction();
53     }
54     ResetSyncTransactionInfo();
55 }
56 
Begin()57 void RSTransaction::Begin()
58 {
59     auto transactionProxy = RSTransactionProxy::GetInstance();
60     if (transactionProxy != nullptr) {
61         RS_TRACE_NAME("BeginSyncTransaction");
62         transactionProxy->StartSyncTransaction();
63         transactionProxy->Begin();
64     }
65 }
66 
Commit()67 void RSTransaction::Commit()
68 {
69     auto transactionProxy = RSTransactionProxy::GetInstance();
70     if (transactionProxy != nullptr) {
71         RS_TRACE_NAME_FMT("CommitSyncTransaction syncId: %lu", syncId_);
72         transactionProxy->SetSyncId(syncId_);
73         transactionProxy->CommitSyncTransaction();
74         transactionProxy->CloseSyncTransaction();
75     }
76 }
77 
GenerateSyncId()78 uint64_t RSTransaction::GenerateSyncId()
79 {
80     static pid_t pid_ = GetRealPid();
81     static std::atomic<uint32_t> currentId_ = 0;
82 
83     auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
84     if (currentId == UINT32_MAX) {
85         // [PLANNING]:process the overflow situations
86         ROSEN_LOGE("Transaction sync Id overflow");
87     }
88 
89     // concat two 32-bit numbers to one 64-bit number
90     return ((uint64_t)pid_ << 32) | currentId;
91 }
92 
ResetSyncTransactionInfo()93 void RSTransaction::ResetSyncTransactionInfo()
94 {
95     std::unique_lock<std::mutex> lock(mutex_);
96     syncId_ = 0;
97     transactionCount_ = 0;
98 }
99 
Unmarshalling(Parcel & parcel)100 RSTransaction* RSTransaction::Unmarshalling(Parcel& parcel)
101 {
102     auto rsTransaction = new RSTransaction();
103     if (rsTransaction->UnmarshallingParam(parcel)) {
104         return rsTransaction;
105     }
106     ROSEN_LOGE("RSTransactionData Unmarshalling Failed");
107     delete rsTransaction;
108     return nullptr;
109 }
110 
Marshalling(Parcel & parcel) const111 bool RSTransaction::Marshalling(Parcel& parcel) const
112 {
113     if (!parcel.WriteUint64(syncId_)) {
114         ROSEN_LOGE("RSTransaction marshalling failed");
115         return false;
116     }
117     transactionCount_++;
118     return true;
119 }
120 
UnmarshallingParam(Parcel & parcel)121 bool RSTransaction::UnmarshallingParam(Parcel& parcel)
122 {
123     if (!parcel.ReadUint64(syncId_)) {
124         ROSEN_LOGE("RSTransaction unmarshallingParam failed");
125         return false;
126     }
127     return true;
128 }
129 } // namespace Rosen
130 } // namespace OHOS
131