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_BASE_DRAWABLE_RS_RENDER_NODE_DRAWABLE_ADAPTER_H 17 #define RENDER_SERVICE_BASE_DRAWABLE_RS_RENDER_NODE_DRAWABLE_ADAPTER_H 18 19 #include <memory> 20 #include <map> 21 #include <mutex> 22 #include <vector> 23 24 #include "common/rs_common_def.h" 25 #include "common/rs_macros.h" 26 #include "common/rs_rect.h" 27 #include "drawable/rs_property_drawable.h" 28 #include "recording/recording_canvas.h" 29 #include "pipeline/rs_render_content.h" 30 #include "utils/rect.h" 31 32 #ifndef ROSEN_CROSS_PLATFORM 33 #include <iconsumer_surface.h> 34 #endif 35 36 namespace OHOS::Rosen { 37 class RSRenderNode; 38 class RSRenderParams; 39 class RSDisplayRenderNode; 40 class RSSurfaceRenderNode; 41 class RSSurfaceHandler; 42 class RSContext; 43 namespace Drawing { 44 class Canvas; 45 } 46 47 struct DrawCmdIndex { 48 int8_t envForeGroundColorIndex_ = -1; 49 int8_t shadowIndex_ = -1; 50 int8_t renderGroupBeginIndex_ = -1; 51 int8_t foregroundFilterBeginIndex_ = -1; 52 int8_t backgroundColorIndex_ = -1; 53 int8_t backgroundImageIndex_ = -1; 54 int8_t backgroundFilterIndex_ = -1; 55 int8_t useEffectIndex_ = -1; 56 int8_t backgroundEndIndex_ = -1; 57 int8_t childrenIndex_ = -1; 58 int8_t contentIndex_ = -1; 59 int8_t foregroundBeginIndex_ = -1; 60 int8_t renderGroupEndIndex_ = -1; 61 int8_t foregroundFilterEndIndex_ = -1; 62 int8_t endIndex_ = -1; 63 }; 64 namespace DrawableV2 { 65 enum class SkipType : uint8_t { 66 NONE = 0, 67 SKIP_SHADOW = 1, 68 SKIP_BACKGROUND_COLOR = 2 69 }; 70 71 enum class DrawSkipType : uint8_t { 72 NONE = 0, 73 SHOULD_NOT_PAINT = 1, 74 CANVAS_NULL = 2, 75 RENDER_THREAD_PARAMS_NULL = 3, 76 RENDER_PARAMS_NULL = 4, 77 SURFACE_PARAMS_SKIP_DRAW = 5, 78 RENDER_ENGINE_NULL = 6, 79 FILTERCACHE_OCCLUSION_SKIP = 7, 80 OCCLUSION_SKIP = 8, 81 UI_FIRST_CACHE_SKIP = 9, 82 PARALLEL_CANVAS_SKIP = 10, 83 INIT_SURFACE_FAIL = 11, 84 RENDER_PARAMS_OR_UNI_PARAMS_NULL = 12, 85 SCREEN_OFF = 13, 86 SCREEN_MANAGER_NULL = 14, 87 SKIP_FRAME = 15, 88 CREATE_PROCESSOR_FAIL = 16, 89 INIT_FOR_RENDER_THREAD_FAIL = 17, 90 WIRED_SCREEN_PROJECTION = 18, 91 EXPAND_PROCESSOR_NULL = 19, 92 MIRROR_DRAWABLE_SKIP = 20, 93 DISPLAY_NODE_SKIP = 21, 94 REQUEST_FRAME_FAIL = 22, 95 SURFACE_NULL = 23, 96 GENERATE_EFFECT_DATA_ON_DEMAND_FAIL = 24, 97 RENDER_SKIP_IF_SCREEN_OFF = 25, 98 HARD_CURSOR_ENAbLED = 26, 99 CHECK_MATCH_AND_WAIT_NOTIFY_FAIL = 27, 100 DEAL_WITH_CACHED_WINDOW = 28 101 }; 102 103 class RSB_EXPORT RSRenderNodeDrawableAdapter : public std::enable_shared_from_this<RSRenderNodeDrawableAdapter> { 104 public: 105 explicit RSRenderNodeDrawableAdapter(std::shared_ptr<const RSRenderNode>&& node); 106 virtual ~RSRenderNodeDrawableAdapter(); 107 108 // delete 109 RSRenderNodeDrawableAdapter(const RSRenderNodeDrawableAdapter&) = delete; 110 RSRenderNodeDrawableAdapter(const RSRenderNodeDrawableAdapter&&) = delete; 111 RSRenderNodeDrawableAdapter& operator=(const RSRenderNodeDrawableAdapter&) = delete; 112 RSRenderNodeDrawableAdapter& operator=(const RSRenderNodeDrawableAdapter&&) = delete; 113 114 using Ptr = RSRenderNodeDrawableAdapter*; 115 using SharedPtr = std::shared_ptr<RSRenderNodeDrawableAdapter>; 116 using WeakPtr = std::weak_ptr<RSRenderNodeDrawableAdapter>; 117 118 virtual void Draw(Drawing::Canvas& canvas) = 0; 119 virtual void DumpDrawableTree(int32_t depth, std::string& out, const RSContext& context) const; 120 121 static SharedPtr OnGenerate(const std::shared_ptr<const RSRenderNode>& node); 122 static SharedPtr GetDrawableById(NodeId id); 123 static std::vector<RSRenderNodeDrawableAdapter::SharedPtr> GetDrawableVectorById( 124 const std::unordered_set<NodeId>& ids); 125 static SharedPtr OnGenerateShadowDrawable( 126 const std::shared_ptr<const RSRenderNode>& node, const std::shared_ptr<RSRenderNodeDrawableAdapter>& drawable); 127 128 static void ClearResource(); 129 using DrawableVec = std::vector<std::shared_ptr<RSRenderNodeDrawableAdapter>>; 130 static void AddToClearDrawables(DrawableVec &vec); 131 using CmdListVec = std::vector<std::shared_ptr<Drawing::DrawCmdList>>; 132 static void AddToClearCmdList(CmdListVec &vec); GetRenderParams()133 inline const std::unique_ptr<RSRenderParams>& GetRenderParams() const 134 { 135 return renderParams_; 136 } 137 GetUifirstRenderParams()138 inline const std::unique_ptr<RSRenderParams>& GetUifirstRenderParams() const 139 { 140 return uifirstRenderParams_; 141 } 142 GetId()143 inline NodeId GetId() const 144 { 145 return nodeId_; 146 } GetNodeType()147 inline RSRenderNodeType GetNodeType() const 148 { 149 return nodeType_; 150 } GetSyncDirtyManager()151 virtual std::shared_ptr<RSDirtyRegionManager> GetSyncDirtyManager() const 152 { 153 return nullptr; 154 } 155 156 using ClearSurfaceTask = std::function<void()>; 157 void RegisterClearSurfaceFunc(ClearSurfaceTask task); 158 void ResetClearSurfaceFunc(); 159 void TryClearSurfaceOnSync(); 160 161 #ifndef ROSEN_CROSS_PLATFORM RegisterDeleteBufferListenerOnSync(sptr<IConsumerSurface> consumer)162 virtual void RegisterDeleteBufferListenerOnSync(sptr<IConsumerSurface> consumer) {} 163 #endif 164 IsDrawCmdListsVisited()165 virtual bool IsDrawCmdListsVisited() const 166 { 167 return true; 168 } SetDrawCmdListsVisited(bool flag)169 virtual void SetDrawCmdListsVisited(bool flag) {} SetSkip(SkipType type)170 void SetSkip(SkipType type) { skipType_ = type; } GetSkipType()171 SkipType GetSkipType() { return skipType_; } 172 173 void SetSkipCacheLayer(bool hasSkipCacheLayer); 174 175 bool IsFilterCacheValidForOcclusion() const; 176 const RectI GetFilterCachedRegion() const; 177 GetFilterNodeSize()178 size_t GetFilterNodeSize() const 179 { 180 return filterNodeSize_; 181 } ReduceFilterNodeSize()182 void ReduceFilterNodeSize() 183 { 184 if (filterNodeSize_ > 0) { 185 --filterNodeSize_; 186 } 187 } 188 struct FilterNodeInfo { FilterNodeInfoFilterNodeInfo189 FilterNodeInfo(NodeId nodeId, Drawing::Matrix matrix, std::vector<Drawing::RectI> rectVec) 190 : nodeId_(nodeId), matrix_(matrix), rectVec_(rectVec) {}; 191 NodeId nodeId_ = 0; 192 // Here, matrix_ and rectVec_ represent the transformation and FilterRect of the node relative to the off-screen 193 Drawing::Matrix matrix_; 194 std::vector<Drawing::RectI> rectVec_; 195 }; 196 GetfilterInfoVec()197 const std::vector<FilterNodeInfo>& GetfilterInfoVec() const 198 { 199 return filterInfoVec_; 200 } GetWithoutFilterMatrixMap()201 const std::unordered_map<NodeId, Drawing::Matrix>& GetWithoutFilterMatrixMap() const 202 { 203 return withoutFilterMatrixMap_; 204 } 205 SetLastDrawnFilterNodeId(NodeId nodeId)206 void SetLastDrawnFilterNodeId(NodeId nodeId) 207 { 208 lastDrawnFilterNodeId_ = nodeId; 209 } 210 GetLastDrawnFilterNodeId()211 NodeId GetLastDrawnFilterNodeId() const 212 { 213 return lastDrawnFilterNodeId_; 214 } 215 Purge()216 virtual void Purge() 217 { 218 if (purgeFunc_) { 219 purgeFunc_(); 220 } 221 } 222 SetDrawSkipType(DrawSkipType type)223 void SetDrawSkipType(DrawSkipType type) { 224 drawSkipType_ = type; 225 } 226 GetDrawSkipType()227 DrawSkipType GetDrawSkipType() { 228 return drawSkipType_; 229 } 230 231 protected: 232 // Util functions 233 std::string DumpDrawableVec(const std::shared_ptr<RSRenderNode>& renderNode) const; 234 bool QuickReject(Drawing::Canvas& canvas, const RectF& localDrawRect); 235 bool HasFilterOrEffect() const; 236 int GetCountOfClipHoleForCache(const RSRenderParams& params) const; 237 238 // Draw functions 239 void DrawAll(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 240 void DrawUifirstContentChildren(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 241 void DrawBackground(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 242 void DrawContent(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 243 void DrawChildren(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 244 void DrawForeground(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 245 void ApplyForegroundColorIfNeed(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 246 247 // used for foreground filter 248 void DrawBeforeCacheWithForegroundFilter(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 249 void DrawCacheWithForegroundFilter(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 250 void DrawAfterCacheWithForegroundFilter(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 251 252 // used for render group 253 void DrawBackgroundWithoutFilterAndEffect(Drawing::Canvas& canvas, const RSRenderParams& params); 254 void CheckShadowRectAndDrawBackground(Drawing::Canvas& canvas, const RSRenderParams& params); 255 void DrawCacheWithProperty(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 256 void DrawBeforeCacheWithProperty(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 257 void DrawAfterCacheWithProperty(Drawing::Canvas& canvas, const Drawing::Rect& rect) const; 258 void CollectInfoForNodeWithoutFilter(Drawing::Canvas& canvas); 259 // Note, the start is included, the end is excluded, so the range is [start, end) 260 void DrawRangeImpl(Drawing::Canvas& canvas, const Drawing::Rect& rect, int8_t start, int8_t end) const; 261 void DrawImpl(Drawing::Canvas& canvas, const Drawing::Rect& rect, int8_t index) const; 262 263 // Register utils 264 using Generator = Ptr (*)(std::shared_ptr<const RSRenderNode>); 265 template<RSRenderNodeType type, Generator generator> 266 class RenderNodeDrawableRegistrar { 267 public: RenderNodeDrawableRegistrar()268 RenderNodeDrawableRegistrar() 269 { 270 RSRenderNodeDrawableAdapter::GeneratorMap.emplace(type, generator); 271 } 272 }; 273 274 RSRenderNodeType nodeType_; 275 // deprecated 276 std::weak_ptr<const RSRenderNode> renderNode_; 277 NodeId nodeId_; 278 279 DrawCmdIndex uifirstDrawCmdIndex_; 280 DrawCmdIndex drawCmdIndex_; 281 std::unique_ptr<RSRenderParams> renderParams_; 282 std::unique_ptr<RSRenderParams> uifirstRenderParams_; 283 std::vector<Drawing::RecordingCanvas::DrawFunc> uifirstDrawCmdList_; 284 std::vector<Drawing::RecordingCanvas::DrawFunc> drawCmdList_; 285 std::vector<FilterNodeInfo> filterInfoVec_; 286 std::unordered_map<NodeId, Drawing::Matrix> withoutFilterMatrixMap_; 287 size_t filterNodeSize_ = 0; 288 std::shared_ptr<DrawableV2::RSFilterDrawable> backgroundFilterDrawable_ = nullptr; 289 std::shared_ptr<DrawableV2::RSFilterDrawable> compositingFilterDrawable_ = nullptr; 290 std::function<void()> purgeFunc_; 291 #ifdef ROSEN_OHOS 292 static thread_local RSRenderNodeDrawableAdapter* curDrawingCacheRoot_; 293 #else 294 static RSRenderNodeDrawableAdapter* curDrawingCacheRoot_; 295 #endif 296 297 // if the node needs to avoid drawing cache because of some layers, such as the security layer... 298 bool hasSkipCacheLayer_ = false; 299 300 ClearSurfaceTask clearSurfaceTask_ = nullptr; 301 private: 302 static void InitRenderParams(const std::shared_ptr<const RSRenderNode>& node, 303 std::shared_ptr<RSRenderNodeDrawableAdapter>& sharedPtr); 304 static std::map<RSRenderNodeType, Generator> GeneratorMap; 305 static std::map<NodeId, WeakPtr> RenderNodeDrawableCache_; 306 static inline std::mutex cacheMapMutex_; 307 static DrawableVec toClearDrawableVec_; 308 static CmdListVec toClearCmdListVec_; 309 SkipType skipType_ = SkipType::NONE; 310 int8_t GetSkipIndex() const; 311 DrawSkipType drawSkipType_ = DrawSkipType::NONE; 312 int filterRectSize_ = 0; 313 static void RemoveDrawableFromCache(const NodeId nodeId); 314 void UpdateFilterInfoForNodeGroup(RSPaintFilterCanvas* curCanvas); 315 NodeId lastDrawnFilterNodeId_ = 0; 316 friend class OHOS::Rosen::RSRenderNode; 317 friend class OHOS::Rosen::RSDisplayRenderNode; 318 friend class OHOS::Rosen::RSSurfaceRenderNode; 319 friend class RSRenderNodeShadowDrawable; 320 friend class RSUseEffectDrawable; 321 friend class RSRenderNodeDrawable; 322 }; 323 324 } // namespace DrawableV2 325 } // namespace OHOS::Rosen 326 #endif // RENDER_SERVICE_BASE_DRAWABLE_RS_RENDER_NODE_DRAWABLE_ADAPTER_H 327