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 #ifndef OHOS_RENDER_3D_GRAPHICS_TASK_H 17 #define OHOS_RENDER_3D_GRAPHICS_TASK_H 18 19 #include <shared_mutex> 20 #include <future> 21 #include <queue> 22 #include <functional> 23 #include <thread> 24 25 namespace OHOS::Render3D { 26 class __attribute__((visibility("default"))) GraphicsTask { 27 public: 28 using Task = void(); 29 class Message { 30 public: 31 explicit Message(const std::function<Task>& task); 32 Message(Message&& msg); 33 Message& operator=(Message&& msg); 34 ~Message() = default; 35 36 void Execute(); 37 void Finish(); 38 39 private: 40 friend GraphicsTask; 41 std::function<Task> task_; 42 std::promise<void> pms_; 43 std::future<void> ftr_ = pms_.get_future(); 44 std::shared_future<void> GetFuture(); 45 }; 46 47 static GraphicsTask& GetInstance(); 48 void PushSyncMessage(const std::function<Task>& task); 49 std::shared_future<void> PushAsyncMessage(const std::function<Task>& task); 50 51 private: 52 GraphicsTask(const GraphicsTask&) = delete; 53 GraphicsTask& operator=(const GraphicsTask&) = delete; 54 GraphicsTask(); 55 ~GraphicsTask(); 56 57 void Start(); 58 void Stop(); 59 void EngineThread(); 60 void SetName(); 61 62 std::queue<Message> messageQueue_; 63 std::condition_variable messageQueueCnd_; 64 std::mutex messageQueueMut_; 65 66 std::thread loop_; 67 std::atomic_bool exit_ = true; 68 }; 69 } // namespace OHOS::Render3D 70 #endif // OHOS_RENDER_3D_GRAPHIC_TASK_H 71