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
Instance()30 RSMessageProcessor& RSMessageProcessor::Instance()
31 {
32 static RSMessageProcessor processor;
33 return processor;
34 }
35
RSMessageProcessor()36 RSMessageProcessor::RSMessageProcessor()
37 {
38 g_instanceValid.store(true);
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 transactionMap_[pid].AddCommand(std::move(command), 0, FollowType::NONE);
55 }
56
AddUIMessage(uint32_t pid,std::unique_ptr<RSCommand> && command)57 void RSMessageProcessor::AddUIMessage(uint32_t pid, std::unique_ptr<RSCommand>&& command)
58 {
59 if (!g_instanceValid.load()) {
60 return;
61 }
62 std::unique_lock<std::mutex> lock(transactionMapMutex_);
63 transactionMap_[pid].AddCommand(std::move(command), 0, FollowType::NONE);
64 }
65
HasTransaction() const66 bool RSMessageProcessor::HasTransaction() const
67 {
68 if (!g_instanceValid.load()) {
69 return false;
70 }
71 std::unique_lock<std::mutex> lock(transactionMapMutex_);
72 return !transactionMap_.empty();
73 }
74
HasTransaction(uint32_t pid) const75 bool RSMessageProcessor::HasTransaction(uint32_t pid) const
76 {
77 if (!g_instanceValid.load()) {
78 return false;
79 }
80 std::unique_lock<std::mutex> lock(transactionMapMutex_);
81 auto iter = transactionMap_.find(pid);
82 return iter != transactionMap_.end() && !iter->second.IsEmpty();
83 }
84
GetTransaction(uint32_t pid)85 RSTransactionData&& RSMessageProcessor::GetTransaction(uint32_t pid)
86 {
87 std::unique_lock<std::mutex> lock(transactionMapMutex_);
88 return std::move(transactionMap_[pid]);
89 }
90
GetAllTransactions()91 std::unordered_map<uint32_t, RSTransactionData>&& RSMessageProcessor::GetAllTransactions()
92 {
93 std::unique_lock<std::mutex> lock(transactionMapMutex_);
94 return std::move(transactionMap_);
95 }
96
97 } // namespace Rosen
98 } // namespace OHOS
99