18bf80f4bSopenharmony_ci/* 28bf80f4bSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License. 58bf80f4bSopenharmony_ci * You may obtain a copy of the License at 68bf80f4bSopenharmony_ci * 78bf80f4bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88bf80f4bSopenharmony_ci * 98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and 138bf80f4bSopenharmony_ci * limitations under the License. 148bf80f4bSopenharmony_ci */ 158bf80f4bSopenharmony_ci 168bf80f4bSopenharmony_ci#include "graphics_task.h" 178bf80f4bSopenharmony_ci#include <mutex> 188bf80f4bSopenharmony_ci#include <utility> 198bf80f4bSopenharmony_ci#include <pthread.h> 208bf80f4bSopenharmony_ci#include <sys/prctl.h> 218bf80f4bSopenharmony_ci#include <sys/resource.h> 228bf80f4bSopenharmony_ci#include <sys/time.h> 238bf80f4bSopenharmony_ci 248bf80f4bSopenharmony_ci#include "3d_widget_adapter_log.h" 258bf80f4bSopenharmony_ci 268bf80f4bSopenharmony_cinamespace OHOS::Render3D { 278bf80f4bSopenharmony_ciGraphicsTask::Message::Message(const std::function<Task>& task) 288bf80f4bSopenharmony_ci : task_(std::move(task)) 298bf80f4bSopenharmony_ci{} 308bf80f4bSopenharmony_ci 318bf80f4bSopenharmony_ciGraphicsTask::Message::Message(GraphicsTask::Message&& msg) 328bf80f4bSopenharmony_ci : task_(std::move(msg.task_)), pms_(std::move(msg.pms_)), ftr_(std::move(msg.ftr_)) 338bf80f4bSopenharmony_ci{} 348bf80f4bSopenharmony_ci 358bf80f4bSopenharmony_ciGraphicsTask::Message& GraphicsTask::Message::operator=(GraphicsTask::Message&& msg) 368bf80f4bSopenharmony_ci{ 378bf80f4bSopenharmony_ci task_ = std::move(msg.task_); 388bf80f4bSopenharmony_ci pms_ = std::move(msg.pms_); 398bf80f4bSopenharmony_ci ftr_ = std::move(msg.ftr_); 408bf80f4bSopenharmony_ci return *this; 418bf80f4bSopenharmony_ci} 428bf80f4bSopenharmony_ci 438bf80f4bSopenharmony_civoid GraphicsTask::Message::Execute() 448bf80f4bSopenharmony_ci{ 458bf80f4bSopenharmony_ci task_(); 468bf80f4bSopenharmony_ci Finish(); 478bf80f4bSopenharmony_ci} 488bf80f4bSopenharmony_ci 498bf80f4bSopenharmony_civoid GraphicsTask::Message::Finish() 508bf80f4bSopenharmony_ci{ 518bf80f4bSopenharmony_ci pms_.set_value(); 528bf80f4bSopenharmony_ci} 538bf80f4bSopenharmony_ci 548bf80f4bSopenharmony_cistd::shared_future<void> GraphicsTask::Message::GetFuture() 558bf80f4bSopenharmony_ci{ 568bf80f4bSopenharmony_ci return std::move(ftr_); 578bf80f4bSopenharmony_ci} 588bf80f4bSopenharmony_ci 598bf80f4bSopenharmony_ciGraphicsTask& GraphicsTask::GetInstance() 608bf80f4bSopenharmony_ci{ 618bf80f4bSopenharmony_ci static GraphicsTask gfxTask; 628bf80f4bSopenharmony_ci return gfxTask; 638bf80f4bSopenharmony_ci} 648bf80f4bSopenharmony_ci 658bf80f4bSopenharmony_civoid GraphicsTask::PushSyncMessage(const std::function<Task>& task) 668bf80f4bSopenharmony_ci{ 678bf80f4bSopenharmony_ci std::shared_future<void> ftr; 688bf80f4bSopenharmony_ci { 698bf80f4bSopenharmony_ci std::lock_guard<std::mutex> lk(messageQueueMut_); 708bf80f4bSopenharmony_ci ftr = messageQueue_.emplace(std::move(task)).GetFuture(); 718bf80f4bSopenharmony_ci messageQueueCnd_.notify_one(); 728bf80f4bSopenharmony_ci } 738bf80f4bSopenharmony_ci 748bf80f4bSopenharmony_ci if (ftr.valid()) { 758bf80f4bSopenharmony_ci ftr.get(); 768bf80f4bSopenharmony_ci } 778bf80f4bSopenharmony_ci} 788bf80f4bSopenharmony_ci 798bf80f4bSopenharmony_cistd::shared_future<void> GraphicsTask::PushAsyncMessage(const std::function<Task>& task) 808bf80f4bSopenharmony_ci{ 818bf80f4bSopenharmony_ci std::lock_guard<std::mutex> lk(messageQueueMut_); 828bf80f4bSopenharmony_ci 838bf80f4bSopenharmony_ci Message& msg = messageQueue_.emplace(std::move(task)); 848bf80f4bSopenharmony_ci messageQueueCnd_.notify_one(); 858bf80f4bSopenharmony_ci 868bf80f4bSopenharmony_ci return msg.GetFuture(); 878bf80f4bSopenharmony_ci} 888bf80f4bSopenharmony_ci 898bf80f4bSopenharmony_ciGraphicsTask::GraphicsTask() 908bf80f4bSopenharmony_ci{ 918bf80f4bSopenharmony_ci Start(); 928bf80f4bSopenharmony_ci} 938bf80f4bSopenharmony_ci 948bf80f4bSopenharmony_ciGraphicsTask::~GraphicsTask() 958bf80f4bSopenharmony_ci{ 968bf80f4bSopenharmony_ci Stop(); 978bf80f4bSopenharmony_ci { 988bf80f4bSopenharmony_ci std::lock_guard<std::mutex> lk(messageQueueMut_); 998bf80f4bSopenharmony_ci while (!messageQueue_.empty()) { 1008bf80f4bSopenharmony_ci messageQueue_.front().Finish(); 1018bf80f4bSopenharmony_ci messageQueue_.pop(); 1028bf80f4bSopenharmony_ci } 1038bf80f4bSopenharmony_ci } 1048bf80f4bSopenharmony_ci 1058bf80f4bSopenharmony_ci if (loop_.joinable()) { 1068bf80f4bSopenharmony_ci loop_.join(); 1078bf80f4bSopenharmony_ci } 1088bf80f4bSopenharmony_ci} 1098bf80f4bSopenharmony_ci 1108bf80f4bSopenharmony_civoid GraphicsTask::Start() 1118bf80f4bSopenharmony_ci{ 1128bf80f4bSopenharmony_ci WIDGET_LOGD("GraphicsTask::Start start"); 1138bf80f4bSopenharmony_ci 1148bf80f4bSopenharmony_ci if (!exit_) { 1158bf80f4bSopenharmony_ci return; 1168bf80f4bSopenharmony_ci } 1178bf80f4bSopenharmony_ci exit_ = false; 1188bf80f4bSopenharmony_ci loop_ = std::thread(std::bind(&GraphicsTask::EngineThread, this)); 1198bf80f4bSopenharmony_ci PushAsyncMessage(std::bind(&GraphicsTask::SetName, this)); 1208bf80f4bSopenharmony_ci WIDGET_LOGD("GraphicsTask::Start end"); 1218bf80f4bSopenharmony_ci} 1228bf80f4bSopenharmony_ci 1238bf80f4bSopenharmony_civoid GraphicsTask::Stop() 1248bf80f4bSopenharmony_ci{ 1258bf80f4bSopenharmony_ci exit_ = true; 1268bf80f4bSopenharmony_ci} 1278bf80f4bSopenharmony_ci 1288bf80f4bSopenharmony_civoid GraphicsTask::EngineThread() 1298bf80f4bSopenharmony_ci{ 1308bf80f4bSopenharmony_ci WIDGET_LOGD("GraphicsTask::EngineThread execute start"); 1318bf80f4bSopenharmony_ci do { 1328bf80f4bSopenharmony_ci std::unique_lock<std::mutex> lk(messageQueueMut_); 1338bf80f4bSopenharmony_ci messageQueueCnd_.wait(lk, [this] { return !messageQueue_.empty(); }); 1348bf80f4bSopenharmony_ci 1358bf80f4bSopenharmony_ci Message msg(std::move(messageQueue_.front())); 1368bf80f4bSopenharmony_ci messageQueue_.pop(); 1378bf80f4bSopenharmony_ci lk.unlock(); 1388bf80f4bSopenharmony_ci 1398bf80f4bSopenharmony_ci msg.Execute(); 1408bf80f4bSopenharmony_ci } while (!exit_); 1418bf80f4bSopenharmony_ci 1428bf80f4bSopenharmony_ci WIDGET_LOGD("GraphicsTask::EngineThread execute exit"); 1438bf80f4bSopenharmony_ci} 1448bf80f4bSopenharmony_ci 1458bf80f4bSopenharmony_civoid GraphicsTask::SetName() 1468bf80f4bSopenharmony_ci{ 1478bf80f4bSopenharmony_ci WIDGET_LOGD("GraphicsTask::SetName start"); 1488bf80f4bSopenharmony_ci prctl(PR_SET_NAME, "Engine Service Lume", 0, 0, 0); 1498bf80f4bSopenharmony_ci} 1508bf80f4bSopenharmony_ci} // namespace OHOS::Render3D 151