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 IsOpincRenderCacheEnable(); 68 bool IsOpincRealDrawCacheEnable(); 69 bool IsAutoCacheDebugEnable(); 70 71 void OpincCalculateBefore(Drawing::Canvas& canvas, 72 const RSRenderParams& params, bool& isOpincDropNodeExt); 73 void OpincCalculateAfter(Drawing::Canvas& canvas, bool& isOpincDropNodeExt); 74 void BeforeDrawCache(NodeStrategyType& cacheStragy, Drawing::Canvas& canvas, RSRenderParams& params, 75 bool& isOpincDropNodeExt); 76 void AfterDrawCache(NodeStrategyType& cacheStragy, Drawing::Canvas& canvas, RSRenderParams& params, 77 bool& isOpincDropNodeExt, int& opincRootTotalCount); 78 79 bool DrawAutoCache(RSPaintFilterCanvas& canvas, Drawing::Image& image, 80 const Drawing::SamplingOptions& samplingOption, Drawing::SrcRectConstraint constraint); 81 void DrawAutoCacheDfx(RSPaintFilterCanvas& canvas, 82 std::vector<std::pair<RectI, std::string>>& autoCacheRenderNodeInfos); 83 void DrawableCacheStateReset(RSRenderParams& params); 84 bool PreDrawableCacheState(RSRenderParams& params, bool& isOpincDropNodeExt); 85 void OpincCanvasUnionTranslate(RSPaintFilterCanvas& canvas); 86 void ResumeOpincCanvasTranslate(RSPaintFilterCanvas& canvas); 87 88 static int GetTotalProcessedNodeCount(); 89 static void TotalProcessedNodeCountInc(); 90 static void ClearTotalProcessedNodeCount(); 91 92 // opinc dfx 93 std::string GetNodeDebugInfo(); 94 95 bool IsOpListDrawAreaEnable(); 96 bool IsTranslate(Drawing::Matrix& mat); 97 GetOpListUnionArea()98 const Drawing::Rect& GetOpListUnionArea() 99 { 100 return opListDrawAreas_.GetOpInfo().unionRect; 101 } 102 IsComputeDrawAreaSucc()103 bool IsComputeDrawAreaSucc() 104 { 105 return isDrawAreaEnable_ == DrawAreaEnableState::DRAW_AREA_ENABLE; 106 } 107 108 // opinc root state IsOpincRootNode()109 bool IsOpincRootNode() 110 { 111 return isOpincRootNode_; 112 } 113 114 // dfx 115 static void InitDfxForCacheInfo(); 116 static void DrawDfxForCacheInfo(RSPaintFilterCanvas& canvas); 117 118 protected: 119 explicit RSRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node); 120 using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::RS_NODE, OnGenerate>; 121 static Registrar instance_; 122 123 // Only use in RSRenderNode::DrawCacheSurface to calculate scale factor 124 float boundsWidth_ = 0.0f; 125 float boundsHeight_ = 0.0f; 126 127 void GenerateCacheIfNeed(Drawing::Canvas& canvas, RSRenderParams& params); 128 void CheckCacheTypeAndDraw(Drawing::Canvas& canvas, const RSRenderParams& params, bool isInCapture = false); 129 130 static inline bool isDrawingCacheEnabled_ = false; 131 static inline bool isDrawingCacheDfxEnabled_ = false; 132 static inline std::mutex drawingCacheInfoMutex_; 133 static inline std::unordered_map<NodeId, std::pair<RectI, int32_t>> drawingCacheInfos_; // (id, <rect, updateTimes>) 134 static inline std::unordered_map<NodeId, bool> cacheUpdatedNodeMap_; 135 136 // opinc global state 137 static inline bool autoCacheEnable_ = false; 138 static inline bool autoCacheDrawingEnable_ = false; 139 thread_local static inline NodeStrategyType nodeCacheType_ = NodeStrategyType::CACHE_NONE; 140 static inline std::vector<std::pair<RectI, std::string>> autoCacheRenderNodeInfos_; 141 thread_local static inline bool isOpincDropNodeExt_ = true; 142 thread_local static inline int opincRootTotalCount_ = 0; 143 static inline RectI screenRectInfo_ = {0, 0, 0, 0}; 144 145 // used for render group cache 146 void SetCacheType(DrawableCacheType cacheType); 147 DrawableCacheType GetCacheType() const; 148 void UpdateCacheInfoForDfx(Drawing::Canvas& canvas, const Drawing::Rect& rect, NodeId id); 149 150 std::shared_ptr<Drawing::Surface> GetCachedSurface(pid_t threadId) const; 151 void InitCachedSurface(Drawing::GPUContext* gpuContext, const Vector2f& cacheSize, pid_t threadId, 152 bool isHdrOn = false); 153 bool NeedInitCachedSurface(const Vector2f& newSize); 154 std::shared_ptr<Drawing::Image> GetCachedImage(RSPaintFilterCanvas& canvas); 155 void DrawCachedImage(RSPaintFilterCanvas& canvas, const Vector2f& boundSize, 156 const std::shared_ptr<RSFilter>& rsFilter = nullptr); 157 void ClearCachedSurface(); 158 159 bool CheckIfNeedUpdateCache(RSRenderParams& params, int32_t& updateTimes); 160 void UpdateCacheSurface(Drawing::Canvas& canvas, const RSRenderParams& params); 161 void TraverseSubTreeAndDrawFilterWithClip(Drawing::Canvas& canvas, const RSRenderParams& params); 162 163 static int GetProcessedNodeCount(); 164 static void ProcessedNodeCountInc(); 165 static void ClearProcessedNodeCount(); 166 static thread_local bool drawBlurForCache_; 167 168 private: 169 std::atomic<DrawableCacheType> cacheType_ = DrawableCacheType::NONE; 170 mutable std::recursive_mutex cacheMutex_; 171 std::shared_ptr<Drawing::Surface> cachedSurface_ = nullptr; 172 std::shared_ptr<Drawing::Image> cachedImage_ = nullptr; 173 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 174 Drawing::BackendTexture cachedBackendTexture_; 175 #ifdef RS_ENABLE_VK 176 NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper_ = nullptr; 177 #endif 178 #endif 179 // surface thread id, cachedImage_ will update context when image can be reused. 180 std::atomic<pid_t> cacheThreadId_; 181 182 static inline std::mutex drawingCacheMapMutex_; 183 static inline std::unordered_map<NodeId, int32_t> drawingCacheUpdateTimeMap_; 184 185 static thread_local bool isOpDropped_; 186 static inline std::atomic<int> totalProcessedNodeCount_ = 0; 187 static inline std::atomic<int> processedNodeCount_ = 0; 188 // used foe render group cache 189 190 // opinc cache state 191 void NodeCacheStateDisable(); 192 bool BeforeDrawCacheProcessChildNode(NodeStrategyType& cacheStragy, RSRenderParams& params); 193 void BeforeDrawCacheFindRootNode(Drawing::Canvas& canvas, const RSRenderParams& params, bool& isOpincDropNodeExt); 194 void DrawWithoutNodeGroupCache( 195 Drawing::Canvas& canvas, const RSRenderParams& params, DrawableCacheType originalCacheType); 196 void DrawWithNodeGroupCache(Drawing::Canvas& canvas, const RSRenderParams& params); 197 198 void CheckRegionAndDrawWithoutFilter( 199 const std::vector<FilterNodeInfo>& filterInfoVec, Drawing::Canvas& canvas, const RSRenderParams& params); 200 void CheckRegionAndDrawWithFilter(std::vector<FilterNodeInfo>::const_iterator& begin, 201 const std::vector<FilterNodeInfo>& filterInfoVec, Drawing::Canvas& canvas, const RSRenderParams& params); 202 bool IsIntersectedWithFilter(std::vector<FilterNodeInfo>::const_iterator& begin, 203 const std::vector<FilterNodeInfo>& filterInfoVec, 204 Drawing::RectI& dstRect); 205 NodeRecordState recordState_ = NodeRecordState::RECORD_NONE; 206 NodeStrategyType rootNodeStragyType_ = NodeStrategyType::CACHE_NONE; 207 NodeStrategyType temNodeStragyType_ = NodeStrategyType::CACHE_NONE; 208 DrawAreaEnableState isDrawAreaEnable_ = DrawAreaEnableState::DRAW_AREA_INIT; 209 Drawing::OpListHandle opListDrawAreas_; 210 bool opCanCache_ = false; 211 int64_t reuseCount_ = 0; 212 bool isOpincRootNode_ = false; 213 bool isOpincDropNodeExtTemp_ = true; 214 bool isOpincCaculateStart_ = false; OpincGetCachedMark()215 bool OpincGetCachedMark() const 216 { 217 return isOpincMarkCached_; 218 } 219 static thread_local bool isOffScreenWithClipHole_; 220 bool isOpincMarkCached_ = false; 221 bool IsOpincNodeInScreenRect(RSRenderParams& params); 222 }; 223 } // namespace DrawableV2 224 } // namespace OHOS::Rosen 225 #endif // RENDER_SERVICE_DRAWABLE_RS_RENDER_NODE_DRAWABLE_H 226