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 RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_SUB_THREAD_MANAGER_H
17 #define RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_SUB_THREAD_MANAGER_H
18 
19 #include "rs_sub_thread.h"
20 
21 #include <condition_variable>
22 #include <cstdint>
23 #include <map>
24 #include <mutex>
25 #include "EGL/egl.h"
26 #include "drawable/rs_surface_render_node_drawable.h"
27 #include "pipeline/rs_base_render_node.h"
28 #include "render_context/render_context.h"
29 
30 namespace OHOS::Rosen {
31 class RSSubThreadManager {
32 public:
33     static RSSubThreadManager *Instance();
34     void Start(RenderContext *context);
35     void PostTask(const std::function<void()>& task, uint32_t threadIndex, bool isSyncTask = false);
36     void WaitNodeTask(uint64_t nodeId);
37     void NodeTaskNotify(uint64_t nodeId);
38     void SubmitSubThreadTask(const std::shared_ptr<RSDisplayRenderNode>& node,
39         const std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes);
40     void ResetSubThreadGrContext();
41     void CancelReleaseResourceTask();
42     void ReleaseTexture();
43     void TryReleaseTextureForIdleThread();
44     void CancelReleaseTextureTask();
45     void ForceReleaseResource();
46     void DumpMem(DfxString& log);
47     float GetAppGpuMemoryInMB();
48     void ReleaseSurface(uint32_t threadIndex) const;
49     void AddToReleaseQueue(std::shared_ptr<Drawing::Surface>&& surface, uint32_t threadIndex);
50     std::unordered_map<uint32_t, pid_t> GetReThreadIndexMap() const;
51     void ScheduleRenderNodeDrawable(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable);
52     void ScheduleReleaseCacheSurfaceOnly(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable);
53     std::shared_ptr<Drawing::GPUContext> GetGrContextFromSubThread(pid_t tid);
54 
55 private:
56     RSSubThreadManager() = default;
57     ~RSSubThreadManager() = default;
58     RSSubThreadManager(const RSSubThreadManager &) = delete;
59     RSSubThreadManager(const RSSubThreadManager &&) = delete;
60     RSSubThreadManager &operator = (const RSSubThreadManager &) = delete;
61     RSSubThreadManager &operator = (const RSSubThreadManager &&) = delete;
62 
63     uint32_t minLoadThreadIndex_ = 0;
64     uint32_t defaultThreadIndex_ = 0;
65     std::mutex parallelRenderMutex_;
66     std::condition_variable cvParallelRender_;
67     std::map<uint64_t, uint8_t> nodeTaskState_;
68     std::vector<std::shared_ptr<RSSubThread>> threadList_;
69     std::unordered_map<pid_t, uint32_t> threadIndexMap_;
70     std::unordered_map<uint32_t, pid_t> reThreadIndexMap_;
71     bool needResetContext_ = false;
72     bool needCancelTask_ = false;
73     bool needCancelReleaseTextureTask_ = false;
74 };
75 }
76 #endif // RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_SUB_THREAD_MANAGER_H