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_SURFACE_RENDER_NODE_DRAWABLE_H 17 #define RENDER_SERVICE_DRAWABLE_RS_SURFACE_RENDER_NODE_DRAWABLE_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 #ifdef NEW_RENDER_CONTEXT 25 #include "rs_render_surface.h" 26 #else 27 #include "platform/drawing/rs_surface.h" 28 #endif 29 30 #include "common/rs_common_def.h" 31 #include "drawable/rs_render_node_drawable.h" 32 #include "params/rs_surface_render_params.h" 33 #include "pipeline/rs_base_render_engine.h" 34 #include "params/rs_display_render_params.h" 35 #include "pipeline/rs_surface_render_node.h" 36 37 namespace OHOS::Rosen { 38 class RSRenderThreadParams; 39 class RSSurfaceRenderNode; 40 class RSSurfaceRenderParams; 41 namespace DrawableV2 { 42 class RSDisplayRenderNodeDrawable; 43 struct UIFirstParams { 44 uint32_t submittedSubThreadIndex_ = INT_MAX; 45 std::atomic<CacheProcessStatus> cacheProcessStatus_ = CacheProcessStatus::UNKNOWN; 46 std::atomic<bool> isNeedSubmitSubThread_ = true; 47 }; 48 class RSSurfaceRenderNodeDrawable : public RSRenderNodeDrawable { 49 public: 50 ~RSSurfaceRenderNodeDrawable() override; 51 52 static RSRenderNodeDrawable::Ptr OnGenerate(std::shared_ptr<const RSRenderNode> node); 53 void OnDraw(Drawing::Canvas& canvas) override; 54 void OnCapture(Drawing::Canvas& canvas) override; 55 bool CheckIfSurfaceSkipInMirror(const RSSurfaceRenderParams& surfaceParams); 56 void SetVirtualScreenWhiteListRootId(const std::unordered_set<NodeId>& whiteList, NodeId id); 57 void ResetVirtualScreenWhiteListRootId(NodeId id); 58 bool EnableRecordingOptimization(const RSSurfaceRenderParams& surfaceParams); 59 60 void SubDraw(Drawing::Canvas& canvas); 61 void ClipRoundRect(Drawing::Canvas& canvas); 62 63 void UpdateCacheSurface(); 64 void SetCacheSurfaceProcessedStatus(CacheProcessStatus cacheProcessStatus); 65 CacheProcessStatus GetCacheSurfaceProcessedStatus() const; 66 GetName()67 const std::string& GetName() const 68 { 69 return name_; 70 } 71 72 // Dma Buffer 73 bool UseDmaBuffer(); 74 IsSurfaceCreated()75 bool IsSurfaceCreated() const 76 { 77 return surfaceCreated_; 78 } 79 80 void ClearBufferQueue(); 81 82 #ifndef ROSEN_CROSS_PLATFORM 83 bool CreateSurface(); 84 #endif 85 BufferRequestConfig GetFrameBufferRequestConfig(); 86 std::unique_ptr<RSRenderFrame> RequestFrame( 87 RenderContext* renderContext, std::shared_ptr<Drawing::GPUContext> grContext); 88 89 // UIFirst SetSubmittedSubThreadIndex(uint32_t index)90 void SetSubmittedSubThreadIndex(uint32_t index) 91 { 92 uiFirstParams.submittedSubThreadIndex_ = index; 93 } 94 GetSubmittedSubThreadIndex()95 uint32_t GetSubmittedSubThreadIndex() const 96 { 97 return uiFirstParams.submittedSubThreadIndex_; 98 } 99 GetDebugInfo()100 std::string GetDebugInfo() 101 { 102 std::string res = "pid_"; 103 res.append("_name_"); 104 res.append(name_); 105 return res; 106 } 107 108 void UpdateCompletedCacheSurface(); 109 void ClearCacheSurfaceInThread(); 110 void ClearCacheSurface(bool isClearCompletedCacheSurface = true); 111 112 std::shared_ptr<Drawing::Surface> GetCacheSurface(uint32_t threadIndex, bool needCheckThread, 113 bool releaseAfterGet = false); 114 bool NeedInitCacheSurface(); 115 std::shared_ptr<Drawing::Image> GetCompletedImage(RSPaintFilterCanvas& canvas, uint32_t threadIndex, 116 bool isUIFirst); 117 using ClearCacheSurfaceFunc = 118 std::function<void(std::shared_ptr<Drawing::Surface>&&, 119 std::shared_ptr<Drawing::Surface>&&, uint32_t, uint32_t)>; 120 void InitCacheSurface(Drawing::GPUContext* grContext, ClearCacheSurfaceFunc func = nullptr, 121 uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isHdrOn = false); 122 ResetUifirst(bool isNotClearCompleteCacheSurface)123 void ResetUifirst(bool isNotClearCompleteCacheSurface) 124 { 125 if (isNotClearCompleteCacheSurface) { 126 ClearCacheSurfaceOnly(); 127 } else { 128 ClearCacheSurfaceInThread(); 129 } 130 } 131 132 bool IsCurFrameStatic(DeviceType deviceType); 133 134 Vector2f GetGravityTranslate(float imgWidth, float imgHeight); 135 136 bool HasCachedTexture() const; 137 138 void SetTextureValidFlag(bool isValid); SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate)139 void SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate) 140 { 141 isCacheSurfaceNeedUpdate_ = isCacheSurfaceNeedUpdate; 142 } 143 GetCacheSurfaceNeedUpdated()144 bool GetCacheSurfaceNeedUpdated() const 145 { 146 return isCacheSurfaceNeedUpdate_; 147 } 148 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 149 void UpdateBackendTexture(); 150 #endif 151 152 bool DrawCacheSurface(RSPaintFilterCanvas& canvas, const Vector2f& boundSize, 153 uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isUIFirst = false); 154 void DrawableCache(std::shared_ptr<Drawing::GPUContext> grContext_); 155 SetLastFrameUsedThreadIndex(pid_t tid)156 void SetLastFrameUsedThreadIndex(pid_t tid) 157 { 158 lastFrameUsedThreadIndex_ = tid; 159 } 160 GetLastFrameUsedThreadIndex()161 pid_t GetLastFrameUsedThreadIndex() 162 { 163 return lastFrameUsedThreadIndex_; 164 } 165 SetRenderCachePriority(NodePriorityType type)166 void SetRenderCachePriority(NodePriorityType type) 167 { 168 priority_ = type; 169 } 170 GetRenderCachePriority()171 NodePriorityType GetRenderCachePriority() const 172 { 173 return priority_; 174 } 175 SetHDRPresent(bool hasHdrPresent)176 void SetHDRPresent(bool hasHdrPresent) 177 { 178 hasHdrPresent_ = hasHdrPresent; 179 } 180 GetHDRPresent()181 bool GetHDRPresent() const 182 { 183 return hasHdrPresent_; 184 } 185 GetScreenId()186 ScreenId GetScreenId() const 187 { 188 return screenId_; 189 } 190 SetScreenId(ScreenId screenId)191 void SetScreenId(ScreenId screenId) 192 { 193 screenId_ = screenId; 194 } 195 GetTargetColorGamut()196 GraphicColorGamut GetTargetColorGamut() const 197 { 198 return targetColorGamut_; 199 } 200 SetTargetColorGamut(GraphicColorGamut colorGamut)201 void SetTargetColorGamut(GraphicColorGamut colorGamut) 202 { 203 targetColorGamut_ = colorGamut; 204 } 205 206 void SetSubThreadSkip(bool isSubThreadSkip); IsSubThreadSkip()207 bool IsSubThreadSkip() const 208 { 209 return isSubThreadSkip_; 210 } 211 void SetTaskFrameCount(uint64_t frameCount); 212 213 uint64_t GetTaskFrameCount() const; 214 215 const Occlusion::Region& GetVisibleDirtyRegion() const; 216 void SetVisibleDirtyRegion(const Occlusion::Region& region); 217 void SetAlignedVisibleDirtyRegion(const Occlusion::Region& region); 218 void SetGlobalDirtyRegion(Occlusion::Region region); 219 const Occlusion::Region& GetGlobalDirtyRegion() const; 220 void SetDirtyRegionAlignedEnable(bool enable); 221 void SetDirtyRegionBelowCurrentLayer(Occlusion::Region& region); 222 std::shared_ptr<RSDirtyRegionManager> GetSyncDirtyManager() const override; 223 GraphicColorGamut GetAncestorDisplayColorGamut(const RSSurfaceRenderParams& surfaceParams); 224 void DealWithSelfDrawingNodeBuffer(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams); 225 void ClearCacheSurfaceOnly(); 226 227 bool PrepareOffscreenRender(); 228 void FinishOffscreenRender(const Drawing::SamplingOptions& sampling); 229 bool IsHardwareEnabled(); GetMutableRSSurfaceHandlerUiFirstOnDraw()230 std::shared_ptr<RSSurfaceHandler> GetMutableRSSurfaceHandlerUiFirstOnDraw() 231 { 232 return surfaceHandlerUiFirst_; 233 } 234 #ifndef ROSEN_CROSS_PLATFORM GetConsumerOnDraw()235 sptr<IConsumerSurface> GetConsumerOnDraw() const 236 { 237 return consumerOnDraw_; 238 } 239 void RegisterDeleteBufferListenerOnSync(sptr<IConsumerSurface> consumer) override; 240 #endif IsHardwareEnabledTopSurface()241 bool IsHardwareEnabledTopSurface() const 242 { 243 return surfaceNodeType_ == RSSurfaceNodeType::SELF_DRAWING_WINDOW_NODE && GetName() == "pointer window"; 244 } 245 private: 246 explicit RSSurfaceRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node); 247 void CacheImgForCapture(RSPaintFilterCanvas& canvas, RSDisplayRenderNodeDrawable& curDisplayNode); 248 bool DealWithUIFirstCache( 249 RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams, RSRenderThreadParams& uniParams); 250 void OnGeneralProcess(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams, bool isSelfDrawingSurface); 251 void CaptureSurface(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams); 252 253 void MergeDirtyRegionBelowCurSurface(RSRenderThreadParams& uniParam, Drawing::Region& region); 254 Drawing::Region CalculateVisibleRegion(RSRenderThreadParams& uniParam, RSSurfaceRenderParams& surfaceParams, 255 RSSurfaceRenderNodeDrawable& surfaceDrawable, bool isOffscreen) const; 256 bool HasCornerRadius(const RSSurfaceRenderParams& surfaceParams) const; 257 using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::SURFACE_NODE, OnGenerate>; 258 static Registrar instance_; 259 260 bool DrawUIFirstCache(RSPaintFilterCanvas& rscanvas, bool canSkipWait); 261 bool DrawUIFirstCacheWithStarting(RSPaintFilterCanvas& rscanvas, NodeId id); 262 // To be deleted after captureWindow being deleted 263 bool CheckIfNeedResetRotate(RSPaintFilterCanvas& canvas); 264 NodeId FindInstanceChildOfDisplay(std::shared_ptr<RSRenderNode> node); 265 266 void DrawUIFirstDfx(RSPaintFilterCanvas& canvas, MultiThreadCacheType enableType, 267 RSSurfaceRenderParams& surfaceParams, bool drawCacheSuccess); 268 void EnableGpuOverDrawDrawBufferOptimization(Drawing::Canvas& canvas, RSSurfaceRenderParams* surfaceParams); 269 270 // dirty manager 271 void UpdateDisplayDirtyManager(int32_t bufferage, bool useAlignedDirtyRegion = false); 272 273 // DMA Buffer 274 bool DrawUIFirstCacheWithDma(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams); 275 void DrawDmaBufferWithGPU(RSPaintFilterCanvas& canvas); 276 void DrawSelfDrawingNodeBuffer(RSPaintFilterCanvas& canvas, 277 const RSSurfaceRenderParams& surfaceParams, BufferDrawParam& params); 278 279 void ClipHoleForSelfDrawingNode(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams); 280 void DrawBufferForRotationFixed(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams); 281 282 std::string name_; 283 RSSurfaceNodeType surfaceNodeType_ = RSSurfaceNodeType::DEFAULT; 284 #ifndef ROSEN_CROSS_PLATFORM 285 sptr<IBufferConsumerListener> consumerListener_ = nullptr; 286 #endif 287 #ifdef NEW_RENDER_CONTEXT 288 std::shared_ptr<RSRenderSurface> surface_ = nullptr; 289 #else 290 std::shared_ptr<RSSurface> surface_ = nullptr; 291 #endif 292 bool surfaceCreated_ = false; 293 294 // UIFIRST 295 std::shared_ptr<RSSurfaceHandler> surfaceHandlerUiFirst_ = nullptr; 296 UIFirstParams uiFirstParams; 297 ClearCacheSurfaceFunc clearCacheSurfaceFunc_ = nullptr; 298 uint32_t cacheSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX; 299 uint32_t completedSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX; 300 mutable std::recursive_mutex completeResourceMutex_; // only lock complete Resource 301 std::shared_ptr<Drawing::Surface> cacheSurface_ = nullptr; 302 std::shared_ptr<Drawing::Surface> cacheCompletedSurface_ = nullptr; 303 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 304 Drawing::BackendTexture cacheBackendTexture_; 305 Drawing::BackendTexture cacheCompletedBackendTexture_; 306 #ifdef RS_ENABLE_VK 307 NativeBufferUtils::VulkanCleanupHelper* cacheCleanupHelper_ = nullptr; 308 NativeBufferUtils::VulkanCleanupHelper* cacheCompletedCleanupHelper_ = nullptr; 309 #endif 310 std::atomic<bool> isCacheSurfaceNeedUpdate_ = false; 311 #endif 312 std::atomic<bool> isTextureValid_ = false; 313 pid_t lastFrameUsedThreadIndex_ = UNI_MAIN_THREAD_INDEX; 314 NodePriorityType priority_ = NodePriorityType::MAIN_PRIORITY; 315 bool hasHdrPresent_ = false; 316 // hdr 317 GraphicColorGamut targetColorGamut_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB; 318 ScreenId screenId_ = INVALID_SCREEN_ID; 319 uint64_t frameCount_ = 0; 320 bool isSubThreadSkip_ = false; 321 322 RSPaintFilterCanvas* curCanvas_ = nullptr; 323 std::shared_ptr<Drawing::Surface> offscreenSurface_ = nullptr; // temporary holds offscreen surface 324 int releaseCount_ = 0; 325 static constexpr int MAX_RELEASE_FRAME = 10; 326 RSPaintFilterCanvas* canvasBackup_ = nullptr; // backup current canvas before offscreen rende 327 std::shared_ptr<RSPaintFilterCanvas> offscreenCanvas_ = nullptr; 328 int maxRenderSize_ = 0; 329 std::unique_ptr<RSAutoCanvasRestore> arc_ = nullptr; 330 331 #ifndef ROSEN_CROSS_PLATFORM 332 sptr<IConsumerSurface> consumerOnDraw_ = nullptr; 333 #endif 334 335 // dirty manager 336 std::shared_ptr<RSDirtyRegionManager> syncDirtyManager_ = nullptr; 337 Occlusion::Region visibleDirtyRegion_; 338 Occlusion::Region alignedVisibleDirtyRegion_; 339 bool isDirtyRegionAlignedEnable_ = false; 340 Occlusion::Region globalDirtyRegion_; 341 bool globalDirtyRegionIsEmpty_ = false; 342 343 // if a there a dirty layer under transparent clean layer, transparent layer should refreshed 344 Occlusion::Region dirtyRegionBelowCurrentLayer_; 345 bool dirtyRegionBelowCurrentLayerIsEmpty_ = false; 346 }; 347 } // namespace DrawableV2 348 } // namespace OHOS::Rosen 349 #endif // RENDER_SERVICE_DRAWABLE_RS_SURFACE_RENDER_NODE_DRAWABLE_H 350