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 <atomic>
17
18 #include "command/rs_message_processor.h"
19
20 #include "command/rs_command.h"
21 #include "platform/common/rs_log.h"
22 #include "transaction/rs_transaction_data.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 static std::atomic_bool g_instanceValid = false;
28 }
29
RSMessageProcessor()30 RSMessageProcessor::RSMessageProcessor()
31 {
32 g_instanceValid.store(true);
33 }
34
Instance()35 RSMessageProcessor& RSMessageProcessor::Instance()
36 {
37 static RSMessageProcessor processor;
38 return processor;
39 }
40
~RSMessageProcessor()41 RSMessageProcessor::~RSMessageProcessor()
42 {
43 g_instanceValid.store(false);
44 std::unique_lock<std::mutex> lock(transactionMapMutex_);
45 transactionMap_.clear();
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 if (!g_instanceValid.load()) {
51 return;
52 }
53 std::unique_lock<std::mutex> lock(transactionMapMutex_);
54 if (!transactionMap_.count(pid)) {
55 std::shared_ptr<RSTransactionData> transactionData = std::make_shared<RSTransactionData>();
56 transactionMap_[pid] = transactionData;
57 }
58 transactionMap_[pid]->AddCommand(std::move(command), 0, FollowType::NONE);
59 }
60
AddUIMessage(uint32_t pid,std::unique_ptr<RSCommand> && command)61 void RSMessageProcessor::AddUIMessage(uint32_t pid, std::unique_ptr<RSCommand>&& command)
62 {
63 if (!g_instanceValid.load()) {
64 return;
65 }
66 std::unique_lock<std::mutex> lock(transactionMapMutex_);
67 if (!transactionMap_.count(pid)) {
68 std::shared_ptr<RSTransactionData> transactionData = std::make_shared<RSTransactionData>();
69 transactionMap_[pid] = transactionData;
70 }
71 transactionMap_[pid]->AddCommand(std::move(command), 0, FollowType::NONE);
72 }
73
HasTransaction() const74 bool RSMessageProcessor::HasTransaction() const
75 {
76 if (!g_instanceValid.load()) {
77 return false;
78 }
79 std::unique_lock<std::mutex> lock(transactionMapMutex_);
80 return !transactionMap_.empty();
81 }
82
HasTransaction(uint32_t pid) const83 bool RSMessageProcessor::HasTransaction(uint32_t pid) const
84 {
85 if (!g_instanceValid.load()) {
86 return false;
87 }
88 std::unique_lock<std::mutex> lock(transactionMapMutex_);
89 auto iter = transactionMap_.find(pid);
90 return iter != transactionMap_.end() && !iter->second->IsEmpty();
91 }
92
ReInitializeMovedMap()93 void RSMessageProcessor::ReInitializeMovedMap()
94 {
95 if (!g_instanceValid.load()) {
96 return;
97 }
98 std::unique_lock<std::mutex> lock(transactionMapMutex_);
99 transactionMap_ = decltype(transactionMap_)();
100 }
101
GetTransaction(uint32_t pid)102 std::shared_ptr<RSTransactionData> RSMessageProcessor::GetTransaction(uint32_t pid)
103 {
104 if (!g_instanceValid.load()) {
105 return nullptr;
106 }
107 std::unique_lock<std::mutex> lock(transactionMapMutex_);
108 auto iter = transactionMap_.find(pid);
109 if (iter != transactionMap_.end()) {
110 auto transactionData = transactionMap_[pid];
111 transactionMap_.erase(pid);
112 return transactionData;
113 } else {
114 return nullptr;
115 }
116 }
117
GetAllTransactions()118 std::unordered_map<uint32_t, std::shared_ptr<RSTransactionData>>&& RSMessageProcessor::GetAllTransactions()
119 {
120 if (!g_instanceValid.load()) {
121 return {};
122 }
123 std::unique_lock<std::mutex> lock(transactionMapMutex_);
124 return std::move(transactionMap_);
125 }
126
127 } // namespace Rosen
128 } // namespace OHOS
129