1 /* 2 * Copyright (c) 2025 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 RS_SUB_THREAD_CACHE_H 17 #define RS_SUB_THREAD_CACHE_H 18 19 #ifndef ROSEN_CROSS_PLATFORM 20 #include <ibuffer_consumer_listener.h> 21 #include <iconsumer_surface.h> 22 #include <surface.h> 23 #endif 24 #include "platform/drawing/rs_surface.h" 25 26 #include "common/rs_common_def.h" 27 #include "drawable/rs_render_node_drawable.h" 28 #include "params/rs_surface_render_params.h" 29 #include "pipeline/render_thread/rs_base_render_engine.h" 30 #include "pipeline/rs_surface_render_node.h" 31 #include "feature/uifirst/rs_draw_window_cache.h" 32 33 namespace OHOS::Rosen { 34 namespace DrawableV2 { 35 class RsSubThreadCache { 36 public: 37 RsSubThreadCache(); ~RsSubThreadCache()38 ~RsSubThreadCache() 39 { 40 ClearCacheSurfaceInThread(); 41 } 42 RsSubThreadCache(const RsSubThreadCache&) = delete; 43 RsSubThreadCache& operator=(const RsSubThreadCache&) = delete; 44 45 struct UIFirstParams { 46 uint32_t submittedSubThreadIndex_ = INT_MAX; 47 std::atomic<CacheProcessStatus> cacheProcessStatus_ = CacheProcessStatus::UNKNOWN; 48 std::atomic<bool> isNeedSubmitSubThread_ = true; 49 }; 50 SetNodeId(NodeId nodeId)51 void SetNodeId(NodeId nodeId) 52 { 53 nodeId_ = nodeId; 54 } 55 GetNodeId()56 NodeId GetNodeId() const 57 { 58 return nodeId_; 59 } 60 GetRSDrawWindowCache()61 RSDrawWindowCache& GetRSDrawWindowCache() 62 { 63 return drawWindowCache_; 64 } 65 66 void SetCacheSurfaceProcessedStatus(CacheProcessStatus cacheProcessStatus); 67 CacheProcessStatus GetCacheSurfaceProcessedStatus() const; 68 69 // UIFirst 70 bool BufferFormatNeedUpdate(std::shared_ptr<Drawing::Surface> cacheSurface, bool isNeedFP16); 71 void UpdateCompletedCacheSurface(); 72 void ClearCacheSurfaceInThread(); 73 void ClearCacheSurfaceOnly(); 74 void UpdateCacheSurfaceInfo(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable); 75 std::shared_ptr<Drawing::Surface> GetCacheSurface(uint32_t threadIndex); 76 bool NeedInitCacheSurface(RSSurfaceRenderParams* surfaceParams); 77 std::shared_ptr<Drawing::Image> GetCompletedImage(RSPaintFilterCanvas& canvas, uint32_t threadIndex, 78 bool isUIFirst); 79 using ClearCacheSurfaceFunc = 80 std::function<void(std::shared_ptr<Drawing::Surface>&&, 81 std::shared_ptr<Drawing::Surface>&&, uint32_t, uint32_t)>; 82 void InitCacheSurface(Drawing::GPUContext* grContext, 83 std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable = nullptr, 84 ClearCacheSurfaceFunc func = nullptr, 85 uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isNeedFP16 = false); 86 87 void ResetUifirst(bool isOnlyClearCache = false); 88 ResetWindowCache()89 void ResetWindowCache() 90 { 91 drawWindowCache_.ClearCache(); 92 } 93 94 bool HasCachedTexture() const; 95 GetCacheSurfaceAlphaInfo()96 float GetCacheSurfaceAlphaInfo() const 97 { 98 return cacheSurfaceInfo_.alpha; 99 } 100 GetCacheSurfaceProcessedNodes()101 int GetCacheSurfaceProcessedNodes() const 102 { 103 return cacheSurfaceInfo_.processedNodeCount; 104 } 105 SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate)106 void SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate) 107 { 108 isCacheSurfaceNeedUpdate_ = isCacheSurfaceNeedUpdate; 109 } 110 GetCacheSurfaceNeedUpdated()111 bool GetCacheSurfaceNeedUpdated() const 112 { 113 return isCacheSurfaceNeedUpdate_; 114 } 115 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 116 void UpdateBackendTexture(); 117 #endif 118 SetLastFrameUsedThreadIndex(pid_t tid)119 void SetLastFrameUsedThreadIndex(pid_t tid) 120 { 121 lastFrameUsedThreadIndex_ = tid; 122 } 123 GetLastFrameUsedThreadIndex()124 pid_t GetLastFrameUsedThreadIndex() 125 { 126 return lastFrameUsedThreadIndex_; 127 } 128 SetRenderCachePriority(NodePriorityType type)129 void SetRenderCachePriority(NodePriorityType type) 130 { 131 priority_ = type; 132 } 133 GetRenderCachePriority()134 NodePriorityType GetRenderCachePriority() const 135 { 136 return priority_; 137 } 138 139 void SetTaskFrameCount(uint64_t frameCount); 140 141 uint64_t GetTaskFrameCount() const; 142 143 void SetSubThreadSkip(bool isSubThreadSkip); IsSubThreadSkip()144 bool IsSubThreadSkip() const 145 { 146 return isSubThreadSkip_; 147 } 148 149 void ProcessSurfaceSkipCount(); 150 void ResetSurfaceSkipCount(); 151 int32_t GetSurfaceSkipCount() const; 152 int32_t GetSurfaceSkipPriority(); 153 bool IsHighPostPriority(); 154 void SetHighPostPriority(bool postPriority); 155 156 uint32_t GetUifirstPostOrder() const; 157 void SetUifirstPostOrder(uint32_t order); 158 GetScreenId()159 ScreenId GetScreenId() const 160 { 161 return screenId_; 162 } 163 SetScreenId(ScreenId screenId)164 void SetScreenId(ScreenId screenId) 165 { 166 screenId_ = screenId; 167 } 168 GetTargetColorGamut()169 GraphicColorGamut GetTargetColorGamut() const 170 { 171 return targetColorGamut_; 172 } 173 SetTargetColorGamut(GraphicColorGamut colorGamut)174 void SetTargetColorGamut(GraphicColorGamut colorGamut) 175 { 176 targetColorGamut_ = colorGamut; 177 } 178 SetHDRPresent(bool hasHdrPresent)179 void SetHDRPresent(bool hasHdrPresent) 180 { 181 hasHdrPresent_ = hasHdrPresent; 182 } 183 GetHDRPresent()184 bool GetHDRPresent() const 185 { 186 return hasHdrPresent_; 187 } 188 189 int GetTotalProcessedSurfaceCount() const; 190 void TotalProcessedSurfaceCountInc(RSPaintFilterCanvas& canvas); 191 void ClearTotalProcessedSurfaceCount(); CheckCacheSurface()192 inline bool CheckCacheSurface() 193 { 194 return cacheSurface_ ? true : false; 195 } 196 bool DealWithUIFirstCache(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, RSPaintFilterCanvas& canvas, 197 RSSurfaceRenderParams& surfaceParams, RSRenderThreadParams& uniParams); 198 bool DrawCacheSurface(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, RSPaintFilterCanvas& canvas, 199 const Vector2f& boundSize, uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isUIFirst = false); 200 201 // uifirst dirtyRegion 202 std::shared_ptr<RSDirtyRegionManager> GetSyncUifirstDirtyManager() const; 203 bool UpdateCacheSurfaceDirtyManager(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, 204 bool hasCompletateCache, bool isLastFrameSkip); 205 void UpdateUifirstDirtyManager(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable); 206 void SetUifirstDirtyRegion(Drawing::Region dirtyRegion); 207 Drawing::Region GetUifirstDirtyRegion() const; 208 bool CalculateUifirstDirtyRegion(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, 209 Drawing::RectI& dirtyRect); 210 bool GetCurDirtyRegionWithMatrix(const Drawing::Matrix& matrix, 211 Drawing::RectF& latestDirtyRect, Drawing::RectF& absDrawRect); 212 bool MergeUifirstAllSurfaceDirtyRegion(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, 213 Drawing::RectI& dirtyRects); 214 void SetUifrstDirtyEnableFlag(bool dirtyEnableFlag); 215 bool GetUifrstDirtyEnableFlag() const; 216 void PushDirtyRegionToStack(RSPaintFilterCanvas& canvas, Drawing::Region& resultRegion); 217 bool IsCacheValid() const; 218 void UifirstDirtyRegionDfx(Drawing::Canvas& canvas, Drawing::RectI& surfaceDrawRect); 219 bool IsDirtyRecordCompletated(); 220 void UpdateDirtyRecordCompletatedState(bool isCompletate); 221 void UpadteAllSurfaceUifirstDirtyEnableState(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, 222 bool isEnableDirtyRegion); 223 void SubDraw(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, Drawing::Canvas& canvas); 224 225 void SetCacheBehindWindowData(const std::shared_ptr<RSPaintFilterCanvas::CacheBehindWindowData>& data); 226 void SetCacheCompletedBehindWindowData(const std::shared_ptr<RSPaintFilterCanvas::CacheBehindWindowData>& data); 227 void ResetCacheBehindWindowData(); 228 void ResetCacheCompletedBehindWindowData(); 229 void DrawBehindWindowBeforeCache(RSPaintFilterCanvas& canvas, 230 const Drawing::scalar px = 0.f, const Drawing::scalar py = 0.f); 231 private: 232 void ClearCacheSurface(bool isClearCompletedCacheSurface = true); 233 bool DrawUIFirstCache(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, RSPaintFilterCanvas& rscanvas, 234 bool canSkipWait); 235 bool DrawUIFirstCacheWithStarting(DrawableV2::RSSurfaceRenderNodeDrawable* surfaceDrawable, 236 RSPaintFilterCanvas& rscanvas, NodeId startingWindowId); 237 void DrawUIFirstDfx(RSPaintFilterCanvas& canvas, MultiThreadCacheType enableType, 238 RSSurfaceRenderParams& surfaceParams, bool drawCacheSuccess); 239 240 NodeId nodeId_ = 0; 241 // Cache in RT 242 RSDrawWindowCache drawWindowCache_; 243 244 // UIFIRST 245 UIFirstParams uiFirstParams; 246 bool isCacheValid_ = false; 247 bool isCacheCompletedValid_ = false; 248 ClearCacheSurfaceFunc clearCacheSurfaceFunc_ = nullptr; 249 uint32_t cacheSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX; 250 uint32_t completedSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX; 251 mutable std::recursive_mutex completeResourceMutex_; // only lock complete Resource 252 struct CacheSurfaceInfo { 253 int processedSurfaceCount = -1; 254 int processedNodeCount = -1; 255 float alpha = -1.f; 256 }; 257 CacheSurfaceInfo cacheSurfaceInfo_; 258 CacheSurfaceInfo cacheCompletedSurfaceInfo_; 259 std::shared_ptr<Drawing::Surface> cacheSurface_ = nullptr; 260 std::shared_ptr<Drawing::Surface> cacheCompletedSurface_ = nullptr; 261 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 262 Drawing::BackendTexture cacheBackendTexture_; 263 Drawing::BackendTexture cacheCompletedBackendTexture_; 264 std::atomic<bool> isCacheSurfaceNeedUpdate_ = false; 265 #endif 266 #ifdef RS_ENABLE_VK 267 NativeBufferUtils::VulkanCleanupHelper* cacheCleanupHelper_ = nullptr; 268 NativeBufferUtils::VulkanCleanupHelper* cacheCompletedCleanupHelper_ = nullptr; 269 #endif 270 std::atomic<bool> isTextureValid_ = false; 271 pid_t lastFrameUsedThreadIndex_ = UNI_MAIN_THREAD_INDEX; 272 NodePriorityType priority_ = NodePriorityType::MAIN_PRIORITY; 273 uint64_t frameCount_ = 0; 274 bool isSubThreadSkip_ = false; 275 int32_t isSurfaceSkipCount_ = 0; 276 int32_t isSurfaceSkipPriority_ = 0; 277 bool isHighPostPriority_ = false; 278 // hdr 279 bool hasHdrPresent_ = false; 280 GraphicColorGamut targetColorGamut_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB; 281 ScreenId screenId_ = INVALID_SCREEN_ID; 282 uint32_t uifirstPostOrder_ = 0; 283 static inline std::atomic<int> totalProcessedSurfaceCount_ = 0; 284 285 std::shared_ptr<RSDirtyRegionManager> syncUifirstDirtyManager_ = nullptr; 286 bool isDirtyRecordCompletated_ = false; 287 Drawing::Region uifirstDirtyRegion_; 288 bool uifrstDirtyEnableFlag_ = false; 289 Drawing::Region uifirstMergedDirtyRegion_; 290 std::shared_ptr<RSPaintFilterCanvas::CacheBehindWindowData> cacheBehindWindowData_ = nullptr; 291 std::shared_ptr<RSPaintFilterCanvas::CacheBehindWindowData> cacheCompletedBehindWindowData_ = nullptr; 292 }; 293 } // DrawableV2 294 } // OHOS::Rosen 295 #endif // RS_SUB_THREAD_CACHE_H