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         transactionMap_[pid] = transactionData;
42     }
43     transactionMap_[pid]->AddCommand(std::move(command), 0, FollowType::NONE);
44 }
45 
AddUIMessage(uint32_t pid,std::unique_ptr<RSCommand> && command)46 void RSMessageProcessor::AddUIMessage(uint32_t pid, std::unique_ptr<RSCommand>&& command)
47 {
48     std::unique_lock<std::mutex> lock(transactionMapMutex_);
49     if (!transactionMap_.count(pid)) {
50         std::shared_ptr<RSTransactionData> transactionData = std::make_shared<RSTransactionData>();
51         transactionMap_[pid] = transactionData;
52     }
53     transactionMap_[pid]->AddCommand(std::move(command), 0, FollowType::NONE);
54 }
55 
HasTransaction() const56 bool RSMessageProcessor::HasTransaction() const
57 {
58     std::unique_lock<std::mutex> lock(transactionMapMutex_);
59     return !transactionMap_.empty();
60 }
61 
HasTransaction(uint32_t pid) const62 bool RSMessageProcessor::HasTransaction(uint32_t pid) const
63 {
64     std::unique_lock<std::mutex> lock(transactionMapMutex_);
65     auto iter = transactionMap_.find(pid);
66     return iter != transactionMap_.end() && !iter->second->IsEmpty();
67 }
68 
GetTransaction(uint32_t pid)69 std::shared_ptr<RSTransactionData> RSMessageProcessor::GetTransaction(uint32_t pid)
70 {
71     std::unique_lock<std::mutex> lock(transactionMapMutex_);
72     auto iter = transactionMap_.find(pid);
73     if (iter != transactionMap_.end()) {
74         auto transactionData = transactionMap_[pid];
75         transactionMap_.erase(pid);
76         return transactionData;
77     } else {
78         return nullptr;
79     }
80 }
81 
GetAllTransactions()82 std::unordered_map<uint32_t, std::shared_ptr<RSTransactionData>>&& RSMessageProcessor::GetAllTransactions()
83 {
84     std::unique_lock<std::mutex> lock(transactionMapMutex_);
85     return std::move(transactionMap_);
86 }
87 
88 } // namespace Rosen
89 } // namespace OHOS
90