1 /*
2 * Copyright (C) 2023 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 }
109
Start()110 void GraphicsTask::Start()
111 {
112 WIDGET_LOGD("GraphicsTask::Start start");
113
114 if (!exit_) {
115 return;
116 }
117 exit_ = false;
118 loop_ = std::thread(std::bind(&GraphicsTask::EngineThread, this));
119 PushAsyncMessage(std::bind(&GraphicsTask::SetName, this));
120 WIDGET_LOGD("GraphicsTask::Start end");
121 }
122
Stop()123 void GraphicsTask::Stop()
124 {
125 exit_ = true;
126 }
127
EngineThread()128 void GraphicsTask::EngineThread()
129 {
130 WIDGET_LOGD("GraphicsTask::EngineThread execute start");
131 do {
132 std::unique_lock<std::mutex> lk(messageQueueMut_);
133 messageQueueCnd_.wait(lk, [this] { return !messageQueue_.empty(); });
134
135 Message msg(std::move(messageQueue_.front()));
136 messageQueue_.pop();
137 lk.unlock();
138
139 msg.Execute();
140 } while (!exit_);
141
142 WIDGET_LOGD("GraphicsTask::EngineThread execute exit");
143 }
144
SetName()145 void GraphicsTask::SetName()
146 {
147 WIDGET_LOGD("GraphicsTask::SetName start");
148 prctl(PR_SET_NAME, "Engine Service Lume", 0, 0, 0);
149 }
150 } // namespace OHOS::Render3D
151