1 /*
2 * Copyright (c) 2024 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 "graphics_task.h"
17 #include <mutex>
18 #include <utility>
19 #include <pthread.h>
20 #include <sys/prctl.h>
21 #include <sys/resource.h>
22 #include <sys/time.h>
23
24 #include "3d_widget_adapter_log.h"
25
26 namespace OHOS::Render3D {
Message(const std::function<Task> & task)27 GraphicsTask::Message::Message(const std::function<Task>& task)
28 : task_(std::move(task))
29 {}
30
Message(GraphicsTask::Message && msg)31 GraphicsTask::Message::Message(GraphicsTask::Message&& msg)
32 : task_(std::move(msg.task_)), pms_(std::move(msg.pms_)), ftr_(std::move(msg.ftr_))
33 {}
34
operator =(GraphicsTask::Message && msg)35 GraphicsTask::Message& GraphicsTask::Message::operator=(GraphicsTask::Message&& msg)
36 {
37 task_ = std::move(msg.task_);
38 pms_ = std::move(msg.pms_);
39 ftr_ = std::move(msg.ftr_);
40 return *this;
41 }
42
Execute()43 void GraphicsTask::Message::Execute()
44 {
45 task_();
46 Finish();
47 }
48
Finish()49 void GraphicsTask::Message::Finish()
50 {
51 pms_.set_value();
52 }
53
GetFuture()54 std::shared_future<void> GraphicsTask::Message::GetFuture()
55 {
56 return std::move(ftr_);
57 }
58
GetInstance()59 GraphicsTask& GraphicsTask::GetInstance()
60 {
61 static GraphicsTask gfxTask;
62 return gfxTask;
63 }
64
PushSyncMessage(const std::function<Task> & task)65 void GraphicsTask::PushSyncMessage(const std::function<Task>& task)
66 {
67 std::shared_future<void> ftr;
68 {
69 std::lock_guard<std::mutex> lk(messageQueueMut_);
70 ftr = messageQueue_.emplace(std::move(task)).GetFuture();
71 messageQueueCnd_.notify_one();
72 }
73
74 if (ftr.valid()) {
75 ftr.get();
76 }
77 }
78
PushAsyncMessage(const std::function<Task> & task)79 std::shared_future<void> GraphicsTask::PushAsyncMessage(const std::function<Task>& task)
80 {
81 std::lock_guard<std::mutex> lk(messageQueueMut_);
82
83 Message& msg = messageQueue_.emplace(std::move(task));
84 messageQueueCnd_.notify_one();
85
86 return msg.GetFuture();
87 }
88
GraphicsTask()89 GraphicsTask::GraphicsTask()
90 {
91 Start();
92 }
93
~GraphicsTask()94 GraphicsTask::~GraphicsTask()
95 {
96 Stop();
97 {
98 std::lock_guard<std::mutex> lk(messageQueueMut_);
99 while (!messageQueue_.empty()) {
100 messageQueue_.front().Finish();
101 messageQueue_.pop();
102 }
103 }
104
105 if (loop_.joinable()) {
106 loop_.join();
107 }
108 WIDGET_LOGI("~GraphicsTask() end");
109 }
110
Start()111 void GraphicsTask::Start()
112 {
113 WIDGET_LOGI("GraphicsTask::Start start");
114
115 if (!exit_) {
116 return;
117 }
118 exit_ = false;
119 loop_ = std::thread([this] { this->EngineThread(); });
120 PushAsyncMessage([this] { this->SetName(); });
121 WIDGET_LOGD("GraphicsTask::Start end");
122 }
123
Stop()124 void GraphicsTask::Stop()
125 {
126 exit_ = true;
127 }
128
EngineThread()129 void GraphicsTask::EngineThread()
130 {
131 WIDGET_LOGD("GraphicsTask::EngineThread execute start");
132 do {
133 std::unique_lock<std::mutex> lk(messageQueueMut_);
134 messageQueueCnd_.wait(lk, [this] { return !messageQueue_.empty(); });
135
136 Message msg(std::move(messageQueue_.front()));
137 messageQueue_.pop();
138 lk.unlock();
139
140 msg.Execute();
141 } while (!exit_);
142
143 WIDGET_LOGD("GraphicsTask::EngineThread execute exit");
144 }
145
SetName()146 void GraphicsTask::SetName()
147 {
148 WIDGET_LOGD("GraphicsTask::SetName start");
149 prctl(PR_SET_NAME, "Engine Service Lume", 0, 0, 0);
150 }
151 } // namespace OHOS::Render3D
152