1 /*
2 * Copyright (c) 2021-2022 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 "command/rs_message_processor.h"
17
18 #include "command/rs_command.h"
19 #include "platform/common/rs_log.h"
20 #include "transaction/rs_transaction_data.h"
21
22 namespace OHOS {
23 namespace Rosen {
Instance()24 RSMessageProcessor& RSMessageProcessor::Instance()
25 {
26 static RSMessageProcessor processor;
27 return processor;
28 }
29
~RSMessageProcessor()30 RSMessageProcessor::~RSMessageProcessor()
31 {
32 std::unique_lock<std::mutex> lock(transactionMapMutex_);
33 transactionMap_.clear();
34 }
35
AddUIMessage(uint32_t pid,std::unique_ptr<RSCommand> & command)36 void RSMessageProcessor::AddUIMessage(uint32_t pid, std::unique_ptr<RSCommand>& command)
37 {
38 std::unique_lock<std::mutex> lock(transactionMapMutex_);
39 if (!transactionMap_.count(pid)) {
40 std::shared_ptr<RSTransactionData> transactionData = std::make_shared<RSTransactionData>();
41 transactionDataIndex_++;
42 transactionData->SetIndex(transactionDataIndex_);
43 transactionMap_[pid] = transactionData;
44 }
45 transactionMap_[pid]->AddCommand(std::move(command), 0, FollowType::NONE);
46 }
47
AddUIMessage(uint32_t pid,std::unique_ptr<RSCommand> && command)48 void RSMessageProcessor::AddUIMessage(uint32_t pid, std::unique_ptr<RSCommand>&& command)
49 {
50 std::unique_lock<std::mutex> lock(transactionMapMutex_);
51 if (!transactionMap_.count(pid)) {
52 std::shared_ptr<RSTransactionData> transactionData = std::make_shared<RSTransactionData>();
53 transactionDataIndex_++;
54 transactionData->SetIndex(transactionDataIndex_);
55 transactionMap_[pid] = transactionData;
56 }
57 transactionMap_[pid]->AddCommand(std::move(command), 0, FollowType::NONE);
58 }
59
HasTransaction() const60 bool RSMessageProcessor::HasTransaction() const
61 {
62 std::unique_lock<std::mutex> lock(transactionMapMutex_);
63 return !transactionMap_.empty();
64 }
65
HasTransaction(uint32_t pid) const66 bool RSMessageProcessor::HasTransaction(uint32_t pid) const
67 {
68 std::unique_lock<std::mutex> lock(transactionMapMutex_);
69 auto iter = transactionMap_.find(pid);
70 return iter != transactionMap_.end() && !iter->second->IsEmpty();
71 }
72
GetTransaction(uint32_t pid)73 std::shared_ptr<RSTransactionData> RSMessageProcessor::GetTransaction(uint32_t pid)
74 {
75 std::unique_lock<std::mutex> lock(transactionMapMutex_);
76 auto iter = transactionMap_.find(pid);
77 if (iter != transactionMap_.end()) {
78 auto transactionData = transactionMap_[pid];
79 transactionMap_.erase(pid);
80 return transactionData;
81 } else {
82 return nullptr;
83 }
84 }
85
GetAllTransactions()86 std::unordered_map<uint32_t, std::shared_ptr<RSTransactionData>> RSMessageProcessor::GetAllTransactions()
87 {
88 std::unique_lock<std::mutex> lock(transactionMapMutex_);
89 auto ret = std::move(transactionMap_);
90 transactionMap_.clear();
91 return ret;
92 }
93
94 } // namespace Rosen
95 } // namespace OHOS
96