1 /* 2 * Copyright (c) 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 "pipeline/rs_unmarshal_thread.h" 17 18 #include "pipeline/rs_base_render_util.h" 19 #include "pipeline/rs_main_thread.h" 20 #include "platform/common/rs_log.h" 21 #include "transaction/rs_transaction_data.h" 22 23 namespace OHOS::Rosen { Instance()24RSUnmarshalThread& RSUnmarshalThread::Instance() 25 { 26 static RSUnmarshalThread instance; 27 return instance; 28 } 29 Start()30void RSUnmarshalThread::Start() 31 { 32 runner_ = AppExecFwk::EventRunner::Create("RSUnmarshalThread"); 33 handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_); 34 } 35 PostTask(const std::function<void ()> & task)36void RSUnmarshalThread::PostTask(const std::function<void()>& task) 37 { 38 if (handler_) { 39 handler_->PostTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE); 40 } 41 } 42 RecvParcel(std::shared_ptr<MessageParcel> & parcel)43void RSUnmarshalThread::RecvParcel(std::shared_ptr<MessageParcel>& parcel) 44 { 45 if (!handler_) { 46 RS_LOGE("RSUnmarshalThread::RecvParcel handler_ is nullptr"); 47 return; 48 } 49 RSTaskMessage::RSTask task = [this, parcel = parcel]() { 50 auto transData = RSBaseRenderUtil::ParseTransactionData(*parcel); 51 if (!transData) { 52 return; 53 } 54 if (!transData->GetUniRender()) { 55 RSMainThread::Instance()->RecvRSTransactionData(transData); 56 return; 57 } 58 std::lock_guard<std::mutex> lock(transactionDataMutex_); 59 cachedTransactionDataMap_[transData->GetSendingPid()].emplace_back(std::move(transData)); 60 }; 61 PostTask(task); 62 RSMainThread::Instance()->RequestNextVSync(); 63 } 64 GetCachedTransactionData()65TransactionDataMap RSUnmarshalThread::GetCachedTransactionData() 66 { 67 TransactionDataMap transactionData; 68 { 69 std::lock_guard<std::mutex> lock(transactionDataMutex_); 70 std::swap(transactionData, cachedTransactionDataMap_); 71 } 72 return transactionData; 73 } 74 } 75