• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_DRAWABLE_RS_RENDER_NODE_DRAWABLE_H
17 #define RENDER_SERVICE_DRAWABLE_RS_RENDER_NODE_DRAWABLE_H
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "common/rs_common_def.h"
23 #include "draw/canvas.h"
24 #include "draw/surface.h"
25 #include "drawable/rs_render_node_drawable_adapter.h"
26 #include "image/gpu_context.h"
27 #include "pipeline/rs_render_node.h"
28 
29 #ifdef RS_ENABLE_VK
30 #include "platform/ohos/backend/native_buffer_utils.h"
31 #endif
32 #include "pipeline/rs_paint_filter_canvas.h"
33 
34 namespace OHOS::Rosen {
35 class RSRenderNode;
36 class RSRenderParams;
37 class RSPaintFilterCanvas;
38 namespace NativeBufferUtils {
39 class VulkanCleanupHelper;
40 }
41 namespace DrawableV2 {
42 // Used by RSUniRenderThread and RSChildrenDrawable
43 class RSRenderNodeDrawable : public RSRenderNodeDrawableAdapter {
44 public:
45     ~RSRenderNodeDrawable() override;
46 
47     static RSRenderNodeDrawable::Ptr OnGenerate(std::shared_ptr<const RSRenderNode> node);
48 
49     void Draw(Drawing::Canvas& canvas) override;
50     virtual void OnDraw(Drawing::Canvas& canvas);
51     virtual void OnCapture(Drawing::Canvas& canvas);
52 
53     // deprecated
GetRenderNode()54     inline std::shared_ptr<const RSRenderNode> GetRenderNode()
55     {
56         return renderNode_.lock();
57     }
58 
GetOpDropped()59     inline bool GetOpDropped() const
60     {
61         return isOpDropped_;
62     }
63 
64     bool ShouldPaint() const;
65 
66     // opinc switch
67     bool IsAutoCacheDebugEnable();
68 
69     void OpincCalculateBefore(Drawing::Canvas& canvas,
70         const RSRenderParams& params, bool& isOpincDropNodeExt);
71     void OpincCalculateAfter(Drawing::Canvas& canvas, bool& isOpincDropNodeExt);
72     void BeforeDrawCache(NodeStrategyType& cacheStragy, Drawing::Canvas& canvas, RSRenderParams& params,
73         bool& isOpincDropNodeExt);
74     void AfterDrawCache(NodeStrategyType& cacheStragy, Drawing::Canvas& canvas, RSRenderParams& params,
75         bool& isOpincDropNodeExt, int& opincRootTotalCount);
76 
77     bool DrawAutoCache(RSPaintFilterCanvas& canvas, Drawing::Image& image,
78         const Drawing::SamplingOptions& samplingOption, Drawing::SrcRectConstraint constraint);
79     void DrawAutoCacheDfx(RSPaintFilterCanvas& canvas,
80         std::vector<std::pair<RectI, std::string>>& autoCacheRenderNodeInfos);
81     void DrawableCacheStateReset(RSRenderParams& params);
82     bool PreDrawableCacheState(RSRenderParams& params, bool& isOpincDropNodeExt);
83     void OpincCanvasUnionTranslate(RSPaintFilterCanvas& canvas);
84     void ResumeOpincCanvasTranslate(RSPaintFilterCanvas& canvas);
85 
86     static int GetTotalProcessedNodeCount();
87     static void TotalProcessedNodeCountInc();
88     static void ClearTotalProcessedNodeCount();
89 
90     // opinc dfx
91     std::string GetNodeDebugInfo();
92 
93     bool IsOpListDrawAreaEnable();
94     bool IsTranslate(Drawing::Matrix& mat);
95 
GetOpListUnionArea()96     const Drawing::Rect& GetOpListUnionArea()
97     {
98         return opListDrawAreas_.GetOpInfo().unionRect;
99     }
100 
IsComputeDrawAreaSucc()101     bool IsComputeDrawAreaSucc()
102     {
103         return isDrawAreaEnable_ == DrawAreaEnableState::DRAW_AREA_ENABLE;
104     }
105 
106     // opinc root state
IsOpincRootNode()107     bool IsOpincRootNode()
108     {
109         return isOpincRootNode_;
110     }
111 
112     void SetCacheImageByCapture(std::shared_ptr<Drawing::Image> image);
113 
114     std::shared_ptr<Drawing::Image> GetCacheImageByCapture() const;
115 
116     // dfx
117     static void InitDfxForCacheInfo();
118     static void DrawDfxForCacheInfo(RSPaintFilterCanvas& canvas, const std::unique_ptr<RSRenderParams>& params);
119     static void SetAutoCacheEnable(bool autoCacheEnable);
120 
121 protected:
122     explicit RSRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node);
123     using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::RS_NODE, OnGenerate>;
124     static Registrar instance_;
125 
126     // Only use in RSRenderNode::DrawCacheSurface to calculate scale factor
127     float boundsWidth_ = 0.0f;
128     float boundsHeight_ = 0.0f;
129 
130     void GenerateCacheIfNeed(Drawing::Canvas& canvas, RSRenderParams& params);
131     void CheckCacheTypeAndDraw(Drawing::Canvas& canvas, const RSRenderParams& params, bool isInCapture = false);
132 
133     static inline bool isDrawingCacheEnabled_ = false;
134     static inline bool isDrawingCacheDfxEnabled_ = false;
135     static inline std::mutex drawingCacheInfoMutex_;
136     static inline std::unordered_map<NodeId, std::pair<RectI, int32_t>> drawingCacheInfos_; // (id, <rect, updateTimes>)
137     static inline std::unordered_map<NodeId, bool> cacheUpdatedNodeMap_;
138 
139     // opinc global state
140     static inline bool autoCacheEnable_ = false;
141     static inline bool autoCacheDrawingEnable_ = false;
142     thread_local static inline NodeStrategyType nodeCacheType_ = NodeStrategyType::CACHE_NONE;
143     static inline std::vector<std::pair<RectI, std::string>> autoCacheRenderNodeInfos_;
144     thread_local static inline bool isOpincDropNodeExt_ = true;
145     thread_local static inline int opincRootTotalCount_ = 0;
146     static inline RectI screenRectInfo_ = {0, 0, 0, 0};
147 
148     static inline int32_t offsetX_ = 0;
149     static inline int32_t offsetY_ = 0;
150     static inline ScreenId curDisplayScreenId_ = INVALID_SCREEN_ID;
151 
152     // used for render group cache
153     void SetCacheType(DrawableCacheType cacheType);
154     DrawableCacheType GetCacheType() const;
155     void UpdateCacheInfoForDfx(Drawing::Canvas& canvas, const Drawing::Rect& rect, NodeId id);
156 
157     std::shared_ptr<Drawing::Surface> GetCachedSurface(pid_t threadId) const;
158     void InitCachedSurface(Drawing::GPUContext* gpuContext, const Vector2f& cacheSize, pid_t threadId,
159         bool isNeedFP16 = false, GraphicColorGamut colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB);
160     bool NeedInitCachedSurface(const Vector2f& newSize);
161     std::shared_ptr<Drawing::Image> GetCachedImage(RSPaintFilterCanvas& canvas);
162     void DrawCachedImage(RSPaintFilterCanvas& canvas, const Vector2f& boundSize,
163     const std::shared_ptr<RSFilter>& rsFilter = nullptr);
164     void ClearCachedSurface();
165 
166     bool CheckIfNeedUpdateCache(RSRenderParams& params, int32_t& updateTimes);
167     void UpdateCacheSurface(Drawing::Canvas& canvas, const RSRenderParams& params);
168     void TraverseSubTreeAndDrawFilterWithClip(Drawing::Canvas& canvas, const RSRenderParams& params);
169 
170     static int GetProcessedNodeCount();
171     static void ProcessedNodeCountInc();
172     static void ClearProcessedNodeCount();
173     static thread_local bool drawBlurForCache_;
174 
175 private:
176     std::atomic<DrawableCacheType> cacheType_ = DrawableCacheType::NONE;
177     mutable std::recursive_mutex cacheMutex_;
178     mutable std::mutex freezeByCaptureMutex_;
179     std::shared_ptr<Drawing::Surface> cachedSurface_ = nullptr;
180     std::shared_ptr<Drawing::Image> cachedImage_ = nullptr;
181     std::shared_ptr<Drawing::Image> cachedImageByCapture_ = nullptr;
182 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
183     Drawing::BackendTexture cachedBackendTexture_;
184 #ifdef RS_ENABLE_VK
185     NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper_ = nullptr;
186 #endif
187 #endif
188     // surface thread id, cachedImage_ will update context when image can be reused.
189     std::atomic<pid_t> cacheThreadId_;
190 
191     static inline std::mutex drawingCacheMapMutex_;
192     static inline std::unordered_map<NodeId, int32_t> drawingCacheUpdateTimeMap_;
193     static inline std::mutex drawingCacheContiUpdateTimeMapMutex_;
194     static inline std::unordered_map<NodeId, int32_t> drawingCacheContinuousUpdateTimeMap_;
195 
196     static thread_local bool isOpDropped_;
197     static thread_local bool isOffScreenWithClipHole_;
198     static inline std::atomic<int> totalProcessedNodeCount_ = 0;
199     static thread_local inline int processedNodeCount_ = 0;
200     // used foe render group cache
201 
202     // opinc cache state
203     void NodeCacheStateDisable();
204     bool BeforeDrawCacheProcessChildNode(NodeStrategyType& cacheStragy, RSRenderParams& params);
205     void BeforeDrawCacheFindRootNode(Drawing::Canvas& canvas, const RSRenderParams& params, bool& isOpincDropNodeExt);
206     void DrawWithoutNodeGroupCache(
207         Drawing::Canvas& canvas, const RSRenderParams& params, DrawableCacheType originalCacheType);
208     void DrawWithNodeGroupCache(Drawing::Canvas& canvas, const RSRenderParams& params);
209 
210     void CheckRegionAndDrawWithoutFilter(
211         const std::vector<FilterNodeInfo>& filterInfoVec, Drawing::Canvas& canvas, const RSRenderParams& params);
212     void CheckRegionAndDrawWithFilter(std::vector<FilterNodeInfo>::const_iterator& begin,
213         const std::vector<FilterNodeInfo>& filterInfoVec, Drawing::Canvas& canvas, const RSRenderParams& params);
214     bool IsIntersectedWithFilter(std::vector<FilterNodeInfo>::const_iterator& begin,
215         const std::vector<FilterNodeInfo>& filterInfoVec,
216         Drawing::RectI& dstRect);
217     void ClearDrawingCacheDataMap();
218     void ClearDrawingCacheContiUpdateTimeMap();
219     NodeRecordState recordState_ = NodeRecordState::RECORD_NONE;
220     NodeStrategyType rootNodeStragyType_ = NodeStrategyType::CACHE_NONE;
221     NodeStrategyType temNodeStragyType_ = NodeStrategyType::CACHE_NONE;
222     DrawAreaEnableState isDrawAreaEnable_ = DrawAreaEnableState::DRAW_AREA_INIT;
223     Drawing::OpListHandle opListDrawAreas_;
224     bool opCanCache_ = false;
225     int64_t reuseCount_ = 0;
226     bool isOpincRootNode_ = false;
227     bool isOpincDropNodeExtTemp_ = true;
228     bool isOpincCaculateStart_ = false;
OpincGetCachedMark()229     bool OpincGetCachedMark()
230     {
231         return isOpincMarkCached_;
232     }
233     bool isOpincMarkCached_ = false;
234     bool IsOpincNodeInScreenRect(RSRenderParams& params);
235 };
236 } // namespace DrawableV2
237 } // namespace OHOS::Rosen
238 #endif // RENDER_SERVICE_DRAWABLE_RS_RENDER_NODE_DRAWABLE_H
239