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#ifndef OHOS_RENDER_3D_GRAPHICS_TASK_H
178bf80f4bSopenharmony_ci#define OHOS_RENDER_3D_GRAPHICS_TASK_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include <shared_mutex>
208bf80f4bSopenharmony_ci#include <future>
218bf80f4bSopenharmony_ci#include <queue>
228bf80f4bSopenharmony_ci#include <functional>
238bf80f4bSopenharmony_ci#include <thread>
248bf80f4bSopenharmony_ci
258bf80f4bSopenharmony_cinamespace OHOS::Render3D {
268bf80f4bSopenharmony_ciclass __attribute__((visibility("default"))) GraphicsTask {
278bf80f4bSopenharmony_cipublic:
288bf80f4bSopenharmony_ci    using Task = void();
298bf80f4bSopenharmony_ci    class Message {
308bf80f4bSopenharmony_ci    public:
318bf80f4bSopenharmony_ci        explicit Message(const std::function<Task>& task);
328bf80f4bSopenharmony_ci        Message(Message&& msg);
338bf80f4bSopenharmony_ci        Message& operator=(Message&& msg);
348bf80f4bSopenharmony_ci        ~Message() = default;
358bf80f4bSopenharmony_ci
368bf80f4bSopenharmony_ci        void Execute();
378bf80f4bSopenharmony_ci        void Finish();
388bf80f4bSopenharmony_ci
398bf80f4bSopenharmony_ci    private:
408bf80f4bSopenharmony_ci        friend GraphicsTask;
418bf80f4bSopenharmony_ci        std::function<Task> task_;
428bf80f4bSopenharmony_ci        std::promise<void> pms_;
438bf80f4bSopenharmony_ci        std::future<void> ftr_ = pms_.get_future();
448bf80f4bSopenharmony_ci        std::shared_future<void> GetFuture();
458bf80f4bSopenharmony_ci    };
468bf80f4bSopenharmony_ci
478bf80f4bSopenharmony_ci    static GraphicsTask& GetInstance();
488bf80f4bSopenharmony_ci    void PushSyncMessage(const std::function<Task>& task);
498bf80f4bSopenharmony_ci    std::shared_future<void> PushAsyncMessage(const std::function<Task>& task);
508bf80f4bSopenharmony_ci
518bf80f4bSopenharmony_ciprivate:
528bf80f4bSopenharmony_ci    GraphicsTask(const GraphicsTask&) = delete;
538bf80f4bSopenharmony_ci    GraphicsTask& operator=(const GraphicsTask&) = delete;
548bf80f4bSopenharmony_ci    GraphicsTask();
558bf80f4bSopenharmony_ci    ~GraphicsTask();
568bf80f4bSopenharmony_ci
578bf80f4bSopenharmony_ci    void Start();
588bf80f4bSopenharmony_ci    void Stop();
598bf80f4bSopenharmony_ci    void EngineThread();
608bf80f4bSopenharmony_ci    void SetName();
618bf80f4bSopenharmony_ci
628bf80f4bSopenharmony_ci    std::queue<Message> messageQueue_;
638bf80f4bSopenharmony_ci    std::condition_variable messageQueueCnd_;
648bf80f4bSopenharmony_ci    std::mutex messageQueueMut_;
658bf80f4bSopenharmony_ci
668bf80f4bSopenharmony_ci    std::thread loop_;
678bf80f4bSopenharmony_ci    std::atomic_bool exit_ = true;
688bf80f4bSopenharmony_ci};
698bf80f4bSopenharmony_ci} // namespace OHOS::Render3D
708bf80f4bSopenharmony_ci#endif // OHOS_RENDER_3D_GRAPHIC_TASK_H
71