• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_PARALLEL_RENDER_MANAGER_H
17 #define RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_PARALLEL_RENDER_MANAGER_H
18 
19 #include <condition_variable>
20 #include <cstdint>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <vector>
25 #include "EGL/egl.h"
26 #ifdef NEW_RENDER_CONTEXT
27 #include "render_context_base.h"
28 #endif
29 #include "rs_parallel_hardware_composer.h"
30 #include "rs_parallel_sub_thread.h"
31 #include "rs_parallel_pack_visitor.h"
32 #include "rs_parallel_task_manager.h"
33 #include "pipeline/parallel_render/rs_render_task.h"
34 #include "pipeline/rs_base_render_engine.h"
35 #include "pipeline/rs_base_render_node.h"
36 #include "common/rs_rect.h"
37 #include "common/rs_vector4.h"
38 
39 namespace OHOS {
40 namespace Rosen {
41 class RSUniRenderVisitor;
42 class RSSurfaceRenderNode;
43 class RSDisplayRenderNode;
44 
45 enum class ParallelRenderType {
46     DRAW_IMAGE = 0,
47     FLUSH_ONE_BUFFER
48 };
49 
50 enum class ParallelStatus {
51     OFF = 0,
52     ON,
53     FIRSTFLUSH,
54     WAITFIRSTFLUSH
55 };
56 
57 enum class TaskType {
58     PREPARE_TASK = 0,
59     PROCESS_TASK,
60     CALC_COST_TASK,
61     COMPOSITION_TASK
62 };
63 
64 class RSParallelRenderManager {
65 public:
66     static RSParallelRenderManager *Instance();
67     void SetParallelMode(bool parallelMode);
68     bool GetParallelMode() const;
69     bool GetParallelModeSafe() const;
70 #ifdef NEW_RENDER_CONTEXT
71     void StartSubRenderThread(uint32_t threadNum, std::shared_ptr<RenderContextBase> context,
72         std::shared_ptr<DrawingContext> drawingContext);
73 #else
74     void StartSubRenderThread(uint32_t threadNum, RenderContext *context);
75 #endif
76     void EndSubRenderThread();
77     void CopyVisitorAndPackTask(RSUniRenderVisitor &visitor, RSDisplayRenderNode &node);
78     void CopyPrepareVisitorAndPackTask(RSUniRenderVisitor &visitor, RSDisplayRenderNode &node);
79     void CopyCalcCostVisitorAndPackTask(RSUniRenderVisitor &visitor, RSDisplayRenderNode &node, bool isNeedCalc,
80         bool doAnimate, bool isOpDropped);
81     void PackRenderTask(RSSurfaceRenderNode &node, TaskType type = TaskType::PROCESS_TASK);
82     void PackParallelCompositionTask(std::shared_ptr<RSNodeVisitor> visitor,
83                                      const std::shared_ptr<RSBaseRenderNode> node);
84     void LoadBalanceAndNotify(TaskType type = TaskType::PROCESS_TASK);
85     void MergeRenderResult(RSPaintFilterCanvas& canvas);
86     void SetFrameSize(int width, int height);
87     void GetFrameSize(int &width, int &height) const;
88     void SubmitSuperTask(uint32_t taskIndex, std::unique_ptr<RSSuperRenderTask> superRenderTask);
89     void SubmitCompositionTask(uint32_t taskIndex, std::unique_ptr<RSCompositionTask> compositionTask);
90     void SubMainThreadNotify(int threadIndex);
91     void WaitSubMainThread(uint32_t threadIndex);
92     void SubMainThreadWait(uint32_t threadIndex);
93     void WaitCalcCostEnd();
94     void WaitCompositionEnd();
95     void UpdateNodeCost(RSDisplayRenderNode& node);
96     bool IsNeedCalcCost() const;
97     int32_t GetCost(RSRenderNode &node) const;
98     int32_t GetSelfDrawNodeCost() const;
99     void StartTiming(uint32_t subMainThreadIdx);
100     void StopTimingAndSetRenderTaskCost(
101         uint32_t subMainThreadIdx, uint64_t loadId, TaskType type = TaskType::PROCESS_TASK);
102     bool ParallelRenderExtEnabled();
103     void TryEnableParallelRendering();
104     void ReadySubThreadNumIncrement();
105     void CommitSurfaceNum(int surfaceNum);
106     void WaitPrepareEnd(RSUniRenderVisitor &visitor);
107     TaskType GetTaskType() const;
GetUniVisitor()108     RSUniRenderVisitor* GetUniVisitor() const
109     {
110         return uniVisitor_;
111     }
112 
GetUniParallelCompositionVisitor()113     std::shared_ptr<RSUniRenderVisitor> GetUniParallelCompositionVisitor() const
114     {
115         return uniParallelCompositionVisitor_;
116     }
117 
IsDoAnimate()118     bool IsDoAnimate() const
119     {
120         return doAnimate_;
121     }
122 
IsOpDropped()123     bool IsOpDropped() const
124     {
125         return isOpDropped_;
126     }
127 
IsSecurityDisplay()128     bool IsSecurityDisplay() const
129     {
130         return isSecurityDisplay_;
131     }
132 
133     ParallelStatus GetParallelRenderingStatus() const;
134     void WorkSerialTask(RSSurfaceRenderNode &node);
LockFlushMutex()135     void LockFlushMutex()
136     {
137         flushMutex_.lock();
138     }
UnlockFlushMutex()139     void UnlockFlushMutex()
140     {
141         flushMutex_.unlock();
142     }
143     uint32_t GetParallelThreadNumber() const;
144     void AddSelfDrawingSurface(unsigned int subThreadIndex, bool isRRect, RectF rect,
145         Vector4f cornerRadius = {0.f, 0.f, 0.f, 0.f});
146     void ClearSelfDrawingSurface(RSPaintFilterCanvas& canvas, unsigned int subThreadIndex);
147     void AddAppWindowNode(uint32_t surfaceIndex, std::shared_ptr<RSSurfaceRenderNode> appNode);
GetAppWindowNodes()148     const std::map<uint32_t, std::vector<std::shared_ptr<RSSurfaceRenderNode>>>& GetAppWindowNodes() const
149     {
150         return appWindowNodesMap_;
151     }
152 
153     // Use for Vulkan
154     void WaitProcessEnd();
155     void InitDisplayNodeAndRequestFrame(
156         const std::shared_ptr<RSBaseRenderEngine> renderEngine, const ScreenInfo screenInfo);
157     void ProcessParallelDisplaySurface(RSUniRenderVisitor &visitor);
158     void ReleaseBuffer();
159     void NotifyUniRenderFinish();
160     std::shared_ptr<RSDisplayRenderNode> GetParallelDisplayNode(uint32_t subMainThreadIdx);
161     std::unique_ptr<RSRenderFrame> GetParallelFrame(uint32_t subMainThreadIdx);
SetFilterSurfaceRenderNode(RSSurfaceRenderNode & node)162     void SetFilterSurfaceRenderNode(RSSurfaceRenderNode& node)
163     {
164         filterSurfaceRenderNodes_.push_back(node.ReinterpretCastTo<RSSurfaceRenderNode>());
165     }
ClearFilterSurfaceRenderNode()166     void ClearFilterSurfaceRenderNode()
167     {
168         std::vector<std::shared_ptr<RSSurfaceRenderNode>>().swap(filterSurfaceRenderNodes_);
169     }
GetFilterSurfaceRenderNodeCount()170     uint32_t GetFilterSurfaceRenderNodeCount()
171     {
172         return filterSurfaceRenderNodes_.size();
173     }
174     void ProcessFilterSurfaceRenderNode();
175 
176 private:
177     RSParallelRenderManager();
178     ~RSParallelRenderManager() = default;
179     RSParallelRenderManager(const RSParallelRenderManager &) = delete;
180     RSParallelRenderManager(const RSParallelRenderManager &&) = delete;
181     RSParallelRenderManager &operator = (const RSParallelRenderManager &) = delete;
182     RSParallelRenderManager &operator = (const RSParallelRenderManager &&) = delete;
183     void DrawImageMergeFunc(RSPaintFilterCanvas& canvas);
184     void FlushOneBufferFunc();
185     void GetCostFactor();
186     void InitAppWindowNodeMap();
187 
188     std::shared_ptr<RSParallelPackVisitor> packVisitor_;
189     std::shared_ptr<RSParallelPackVisitor> packVisitorPrepare_;
190     std::shared_ptr<RSParallelPackVisitor> calcCostVisitor_;
191     std::vector<std::unique_ptr<RSParallelSubThread>> threadList_;
192     RSParallelTaskManager processTaskManager_;
193     RSParallelTaskManager prepareTaskManager_;
194     RSParallelTaskManager calcCostTaskManager_;
195     RSParallelTaskManager compositionTaskManager_;
196     int width_ = 0;
197     int height_ = 0;
198     std::vector<uint8_t> flipCoin_;
199     std::mutex parallelRenderMutex_;
200     std::mutex cvParallelRenderMutex_;
201     std::mutex flushMutex_;
202     std::condition_variable cvParallelRender_;
203 #ifdef NEW_RENDER_CONTEXT
204     std::shared_ptr<RenderContextBase> renderContext_ = nullptr;
205     std::shared_ptr<DrawingContext> drawingContext_ = nullptr;
206 #else
207     RenderContext* renderContext_ = nullptr;
208 #endif
209     ParallelRenderType renderType_ = ParallelRenderType::DRAW_IMAGE;
210     std::shared_ptr<RSBaseRenderNode> displayNode_ = nullptr;
211     std::shared_ptr<RSDisplayRenderNode> mainDisplayNode_ = nullptr;
212     std::shared_ptr<RSBaseRenderNode> baseNode_ = nullptr;
213 
214     uint32_t expectedSubThreadNum_ = 0;
215     std::atomic<uint32_t> readySubThreadNum_ = 0;
216     bool firstFlush_ = false;
217     bool parallelMode_ = false;
218     RSUniRenderVisitor *uniVisitor_ = nullptr;
219     std::shared_ptr<RSUniRenderVisitor> uniParallelCompositionVisitor_ = nullptr;
220     TaskType taskType_;
221     std::unique_ptr<RSParallelHardwareComposer> parallelHardwareComposer_;
222     std::map<uint32_t, std::vector<std::shared_ptr<RSSurfaceRenderNode>>> appWindowNodesMap_;
223 
224     std::vector<uint32_t> parallelPolicy_;
225     int32_t calcCostCount_ = 0;
226     std::map<std::string, int32_t> costFactor_;
227     std::map<int64_t, int32_t> imageFactor_;
228 
229     bool doAnimate_ = false;
230     bool isOpDropped_ = false;
231     bool isSecurityDisplay_ = false;
232 
233     std::vector<timespec> startTime_;
234     std::vector<timespec> stopTime_;
235 
236     // Use for Vulkan
237     std::vector<std::shared_ptr<RSDisplayRenderNode>> parallelDisplayNodes_;
238     std::vector<std::shared_ptr<RSDisplayRenderNode>> backParallelDisplayNodes_;
239     std::vector<std::unique_ptr<RSRenderFrame>> parallelFrames_;
240     int readyBufferNum_ = 0;
241     std::vector<std::shared_ptr<RSSurfaceRenderNode>> filterSurfaceRenderNodes_;
242 };
243 } // namespace Rosen
244 } // namespace OHOS
245 #endif // RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_PARALLEL_RENDER_MANAGER_H