• 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 "feature/opinc/rs_opinc_draw_cache.h"
27 #include "image/gpu_context.h"
28 #include "pipeline/rs_render_node.h"
29 
30 #ifdef RS_ENABLE_VK
31 #include "platform/ohos/backend/native_buffer_utils.h"
32 #endif
33 #include "pipeline/rs_paint_filter_canvas.h"
34 
35 namespace OHOS::Rosen {
36 class RSRenderNode;
37 class RSRenderParams;
38 class RSPaintFilterCanvas;
39 namespace NativeBufferUtils {
40 class VulkanCleanupHelper;
41 }
42 namespace DrawableV2 {
43 // Used by RSUniRenderThread and RSChildrenDrawable
44 class RSRenderNodeDrawable : public RSRenderNodeDrawableAdapter {
45 public:
46     ~RSRenderNodeDrawable() override;
47 
48     static RSRenderNodeDrawable::Ptr OnGenerate(std::shared_ptr<const RSRenderNode> node);
49 
50     void Draw(Drawing::Canvas& canvas) override;
51     virtual void OnDraw(Drawing::Canvas& canvas);
52     virtual void OnCapture(Drawing::Canvas& canvas);
53 
54     // deprecated
GetRenderNode()55     inline std::shared_ptr<const RSRenderNode> GetRenderNode()
56     {
57         return renderNode_.lock();
58     }
59 
GetOpDropped()60     inline bool GetOpDropped() const
61     {
62         return isOpDropped_;
63     }
64 
IsOcclusionCullingEnabled()65     inline bool IsOcclusionCullingEnabled() const
66     {
67         return occlusionCullingEnabled_;
68     }
69 
SetOcclusionCullingEnabled(bool enabled)70     inline void SetOcclusionCullingEnabled(bool enabled)
71     {
72         occlusionCullingEnabled_ = enabled;
73     }
74 
75     bool ShouldPaint() const;
76 
77     static int GetTotalProcessedNodeCount();
78     static void TotalProcessedNodeCountInc();
79     static void ClearTotalProcessedNodeCount();
80     static int GetSnapshotProcessedNodeCount();
81     static void SnapshotProcessedNodeCountInc();
82     static void ClearSnapshotProcessedNodeCount();
83 
84     static void ClearOpincState();
85     // opinc dfx
86     std::string GetNodeDebugInfo();
87 
88     void SetCacheImageByCapture(std::shared_ptr<Drawing::Image> image);
89 
90     std::shared_ptr<Drawing::Image> GetCacheImageByCapture() const;
91 
SetSubTreeParallel(bool isParallel)92     inline void SetSubTreeParallel(bool isParallel)
93     {
94         subTreeParallel_ = isParallel;
95     }
96 
GetSubTreeParallel()97     inline bool GetSubTreeParallel() const
98     {
99         return subTreeParallel_;
100     }
101 
102     // dfx
103     static void InitDfxForCacheInfo();
104     static void DrawDfxForCacheInfo(RSPaintFilterCanvas& canvas, const std::unique_ptr<RSRenderParams>& params);
105 
GetOpincDrawCache()106     RSOpincDrawCache& GetOpincDrawCache()
107     {
108         return opincDrawCache_;
109     }
110 
GetDrawableType()111     RSRenderNodeDrawableType GetDrawableType() const override
112     {
113         return RSRenderNodeDrawableType::RS_NODE_DRAWABLE;
114     }
115 protected:
116     explicit RSRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node);
117     using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::RS_NODE, OnGenerate>;
118     static Registrar instance_;
119     // indicate whether this drawable is generate from parallel node.
120     bool subTreeParallel_ = false;
121     // Only use in RSRenderNode::DrawCacheSurface to calculate scale factor
122     float boundsWidth_ = 0.0f;
123     float boundsHeight_ = 0.0f;
124 
125     void GenerateCacheIfNeed(Drawing::Canvas& canvas, RSRenderParams& params);
126     void CheckCacheTypeAndDraw(Drawing::Canvas& canvas, const RSRenderParams& params, bool isInCapture = false);
127 
128     static inline bool isDrawingCacheEnabled_ = false;
129     static inline bool isDrawingCacheDfxEnabled_ = false;
130     static inline std::mutex drawingCacheInfoMutex_;
131     static inline std::unordered_map<NodeId, std::pair<RectI, int32_t>> drawingCacheInfos_; // (id, <rect, updateTimes>)
132     static inline std::unordered_map<NodeId, bool> cacheUpdatedNodeMap_;
133 
134     static inline bool autoCacheDrawingEnable_ = false;
135     static inline std::vector<std::pair<RectI, std::string>> autoCacheRenderNodeInfos_;
136     thread_local static inline bool isOpincDropNodeExt_ = true;
137     thread_local static inline int opincRootTotalCount_ = 0;
138 
139     static inline int32_t offsetX_ = 0;
140     static inline int32_t offsetY_ = 0;
141     static inline ScreenId curDisplayScreenId_ = INVALID_SCREEN_ID;
142 
143     // used for render group cache
144     void SetCacheType(DrawableCacheType cacheType);
145     DrawableCacheType GetCacheType() const;
146     void UpdateCacheInfoForDfx(Drawing::Canvas& canvas, const Drawing::Rect& rect, NodeId id);
147 
148     std::shared_ptr<Drawing::Surface> GetCachedSurface(pid_t threadId) const;
149     void InitCachedSurface(Drawing::GPUContext* gpuContext, const Vector2f& cacheSize, pid_t threadId,
150         bool isNeedFP16 = false, GraphicColorGamut colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB);
151     bool NeedInitCachedSurface(const Vector2f& newSize);
152     std::shared_ptr<Drawing::Image> GetCachedImage(RSPaintFilterCanvas& canvas);
153     void DrawCachedImage(RSPaintFilterCanvas& canvas, const Vector2f& boundSize,
154     const std::shared_ptr<RSFilter>& rsFilter = nullptr);
155     void ClearCachedSurface();
156 
157     bool CheckIfNeedUpdateCache(RSRenderParams& params, int32_t& updateTimes);
158     void UpdateCacheSurface(Drawing::Canvas& canvas, const RSRenderParams& params);
159     void TraverseSubTreeAndDrawFilterWithClip(Drawing::Canvas& canvas, const RSRenderParams& params);
160     bool DealWithWhiteListNodes(Drawing::Canvas& canvas);
161 
162     static int GetProcessedNodeCount();
163     static void ProcessedNodeCountInc();
164     static void ClearProcessedNodeCount();
165     static thread_local bool drawBlurForCache_;
166     // Used to skip nodes or entire subtree that were culled by the control-level occlusion.
167     bool SkipCulledNodeOrEntireSubtree(Drawing::Canvas& canvas, Drawing::Rect& bounds);
168 
169 private:
170     std::atomic<DrawableCacheType> cacheType_ = DrawableCacheType::NONE;
171     mutable std::recursive_mutex cacheMutex_;
172     mutable std::mutex freezeByCaptureMutex_;
173     std::shared_ptr<Drawing::Surface> cachedSurface_ = nullptr;
174     std::shared_ptr<Drawing::Image> cachedImage_ = nullptr;
175     std::shared_ptr<Drawing::Image> cachedImageByCapture_ = nullptr;
176 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
177     Drawing::BackendTexture cachedBackendTexture_;
178 #ifdef RS_ENABLE_VK
179     NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper_ = nullptr;
180 #endif
181 #endif
182     // surface thread id, cachedImage_ will update context when image can be reused.
183     std::atomic<pid_t> cacheThreadId_;
184 
185     static inline std::mutex drawingCacheMapMutex_;
186     static inline std::unordered_map<NodeId, int32_t> drawingCacheUpdateTimeMap_;
187     static inline std::mutex drawingCacheContiUpdateTimeMapMutex_;
188     static inline std::unordered_map<NodeId, int32_t> drawingCacheContinuousUpdateTimeMap_;
189 
190     static thread_local bool isOpDropped_;
191     static thread_local bool occlusionCullingEnabled_;
192     static thread_local bool isOffScreenWithClipHole_;
193     static inline std::atomic<int> totalProcessedNodeCount_ = 0;
194     static inline int snapshotProcessedNodeCount_ = 0;
195     static thread_local inline int processedNodeCount_ = 0;
196     // used foe render group cache
197 
198     void DrawWithoutNodeGroupCache(
199         Drawing::Canvas& canvas, const RSRenderParams& params, DrawableCacheType originalCacheType);
200     void DrawWithNodeGroupCache(Drawing::Canvas& canvas, const RSRenderParams& params);
201 
202     void CheckRegionAndDrawWithoutFilter(
203         const std::vector<FilterNodeInfo>& filterInfoVec, Drawing::Canvas& canvas, const RSRenderParams& params);
204     void CheckRegionAndDrawWithFilter(std::vector<FilterNodeInfo>::const_iterator& begin,
205         const std::vector<FilterNodeInfo>& filterInfoVec, Drawing::Canvas& canvas, const RSRenderParams& params);
206     bool IsIntersectedWithFilter(std::vector<FilterNodeInfo>::const_iterator& begin,
207         const std::vector<FilterNodeInfo>& filterInfoVec,
208         Drawing::RectI& dstRect);
209     void ClearDrawingCacheDataMap();
210     void ClearDrawingCacheContiUpdateTimeMap();
211     friend class RsSubThreadCache;
212     RSOpincDrawCache opincDrawCache_;
213 };
214 } // namespace DrawableV2
215 } // namespace OHOS::Rosen
216 #endif // RENDER_SERVICE_DRAWABLE_RS_RENDER_NODE_DRAWABLE_H
217