1 /* 2 * Copyright (c) 2021-2023 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_MAIN_THREAD 17 #define RS_MAIN_THREAD 18 19 #include <event_handler.h> 20 #include <future> 21 #include <memory> 22 #include <mutex> 23 #include <queue> 24 #include <set> 25 #include <thread> 26 27 #include "refbase.h" 28 #include "pipeline/render_thread/rs_base_render_engine.h" 29 #include "pipeline/render_thread/rs_draw_frame.h" 30 #include "vsync_distributor.h" 31 #include "vsync_receiver.h" 32 33 #include "command/rs_command.h" 34 #include "common/rs_common_def.h" 35 #include "common/rs_thread_handler.h" 36 #include "common/rs_thread_looper.h" 37 #include "drawable/rs_render_node_drawable_adapter.h" 38 #include "ipc_callbacks/iapplication_agent.h" 39 #include "ipc_callbacks/rs_iocclusion_change_callback.h" 40 #include "ipc_callbacks/rs_isurface_occlusion_change_callback.h" 41 #include "ipc_callbacks/rs_iuiextension_callback.h" 42 #include "memory/rs_app_state_listener.h" 43 #include "memory/rs_memory_graphic.h" 44 #include "params/rs_render_thread_params.h" 45 #include "pipeline/rs_context.h" 46 #include "pipeline/rs_uni_render_judgement.h" 47 #include "pipeline/hwc/rs_direct_composition_helper.h" 48 #include "feature/hyper_graphic_manager/hgm_context.h" 49 #include "feature/vrate/rs_vsync_rate_reduce_manager.h" 50 #include "platform/common/rs_event_manager.h" 51 #include "screen_manager/rs_screen_node_listener.h" 52 #include "platform/drawing/rs_vsync_client.h" 53 #include "transaction/rs_transaction_data.h" 54 #include "transaction/rs_uiextension_data.h" 55 56 #ifdef RES_SCHED_ENABLE 57 #include "vsync_system_ability_listener.h" 58 #endif 59 60 #include "hgm_core.h" 61 62 namespace OHOS::Rosen { 63 #if defined(ACCESSIBILITY_ENABLE) 64 class AccessibilityObserver; 65 #endif 66 class HgmFrameRateManager; 67 class RSUniRenderVisitor; 68 struct FrameRateRangeData; 69 namespace Detail { 70 template<typename Task> 71 class ScheduledTask : public RefBase { 72 public: Create(Task && task)73 static auto Create(Task&& task) 74 { 75 sptr<ScheduledTask<Task>> t(new ScheduledTask(std::forward<Task&&>(task))); 76 return std::make_pair(t, t->task_.get_future()); 77 } 78 Run()79 void Run() 80 { 81 task_(); 82 } 83 84 private: ScheduledTask(Task && task)85 explicit ScheduledTask(Task&& task) : task_(std::move(task)) {} 86 ~ScheduledTask() override = default; 87 88 using Return = std::invoke_result_t<Task>; 89 std::packaged_task<Return()> task_; 90 }; 91 } // namespace Detail 92 93 class RSMainThread { 94 public: 95 static RSMainThread* Instance(); 96 97 void Init(); 98 void Start(); 99 bool IsNeedProcessBySingleFrameComposer(std::unique_ptr<RSTransactionData>& rsTransactionData); 100 void UpdateFocusNodeId(NodeId focusNodeId); 101 void UpdateNeedDrawFocusChange(NodeId id); 102 void ProcessDataBySingleFrameComposer(std::unique_ptr<RSTransactionData>& rsTransactionData); 103 void RecvRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData); 104 void RequestNextVSync( 105 const std::string& fromWhom = "unknown", int64_t lastVSyncTS = 0, const int64_t& requestVsyncTime = 0); 106 void PostTask(RSTaskMessage::RSTask task); 107 void PostTask(RSTaskMessage::RSTask task, const std::string& name, int64_t delayTime, 108 AppExecFwk::EventQueue::Priority priority = AppExecFwk::EventQueue::Priority::IDLE); 109 void RemoveTask(const std::string& name); 110 void PostSyncTask(RSTaskMessage::RSTask task); 111 bool IsIdle() const; 112 void TransactionDataMapDump(const TransactionDataMap& transactionDataMap, std::string& dumpString); 113 void RenderServiceTreeDump(std::string& dumpString, bool forceDumpSingleFrame = true, 114 bool needUpdateJankStats = false); 115 void RenderServiceAllNodeDump(DfxString& log); 116 void RenderServiceAllSurafceDump(DfxString& log); 117 void SendClientDumpNodeTreeCommands(uint32_t taskId); 118 void CollectClientNodeTreeResult(uint32_t taskId, std::string& dumpString, size_t timeout); 119 void RsEventParamDump(std::string& dumpString); 120 void UpdateAnimateNodeFlag(); 121 void ResetAnimateNodeFlag(); 122 void GetAppMemoryInMB(float& cpuMemSize, float& gpuMemSize); 123 void ClearMemoryCache(ClearMemoryMoment moment, bool deeply = false, pid_t pid = -1); 124 void SetForceRsDVsync(const std::string& sceneId); 125 126 template<typename Task, typename Return = std::invoke_result_t<Task>> ScheduleTask(Task && task)127 std::future<Return> ScheduleTask(Task&& task) 128 { 129 auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task)); 130 PostTask([t(std::move(scheduledTask))]() { t->Run(); }); 131 return std::move(taskFuture); 132 } 133 GetRenderEngine()134 const std::shared_ptr<RSBaseRenderEngine> GetRenderEngine() const 135 { 136 RS_LOGD("You'd better to call GetRenderEngine from RSUniRenderThread directly"); 137 #ifdef RS_ENABLE_GPU 138 return isUniRender_ ? std::move(RSUniRenderThread::Instance().GetRenderEngine()) : renderEngine_; 139 #else 140 return renderEngine_; 141 #endif 142 } 143 GetClearMemoryFinished()144 bool GetClearMemoryFinished() const 145 { 146 return clearMemoryFinished_; 147 } 148 GetContext()149 RSContext& GetContext() 150 { 151 return *context_; 152 } 153 Id()154 std::thread::id Id() const 155 { 156 return mainThreadId_; 157 } 158 SetGlobalDarkColorMode(bool isDark)159 void SetGlobalDarkColorMode(bool isDark) 160 { 161 isGlobalDarkColorMode_ = isDark; 162 } 163 GetGlobalDarkColorMode()164 bool GetGlobalDarkColorMode() const 165 { 166 return isGlobalDarkColorMode_; 167 } 168 169 /* Judge if rootnode has to be prepared based on it corresponding process is active 170 * If its pid is in activeProcessPids_ set, return true 171 */ 172 bool CheckNodeHasToBePreparedByPid(NodeId nodeId, bool isClassifyByRoot); 173 // check if active app has static drawing cache 174 bool IsDrawingGroupChanged(const RSRenderNode& cacheRootNode) const; 175 // check if active instance only move or scale it's main window surface without rearrangement 176 // instanceNodeId should be MainWindowType, or it cannot grep correct app's info 177 void CheckAndUpdateInstanceContentStaticStatus(std::shared_ptr<RSSurfaceRenderNode> instanceNode) const; 178 179 void RegisterApplicationAgent(uint32_t pid, sptr<IApplicationAgent> app); 180 void UnRegisterApplicationAgent(sptr<IApplicationAgent> app); 181 182 void RegisterOcclusionChangeCallback(pid_t pid, sptr<RSIOcclusionChangeCallback> callback); 183 void UnRegisterOcclusionChangeCallback(pid_t pid); 184 185 void RegisterSurfaceOcclusionChangeCallback( 186 NodeId id, pid_t pid, sptr<RSISurfaceOcclusionChangeCallback> callback, std::vector<float>& partitionPoints); 187 void UnRegisterSurfaceOcclusionChangeCallback(NodeId id); 188 void ClearSurfaceOcclusionChangeCallback(pid_t pid); 189 bool SurfaceOcclusionCallBackIfOnTreeStateChanged(); 190 191 void ClearTransactionDataPidInfo(pid_t remotePid); 192 void AddTransactionDataPidInfo(pid_t remotePid); 193 194 void SetFocusAppInfo(const FocusAppInfo& info); 195 const std::unordered_map<NodeId, bool>& GetCacheCmdSkippedNodes() const; 196 197 sptr<VSyncDistributor> rsVSyncDistributor_; 198 sptr<VSyncController> rsVSyncController_; 199 sptr<VSyncController> appVSyncController_; 200 sptr<VSyncGenerator> vsyncGenerator_; 201 202 void ReleaseSurface(); 203 void AddToReleaseQueue(std::shared_ptr<Drawing::Surface>&& surface); 204 205 void AddUiCaptureTask(NodeId id, std::function<void()> task); 206 void ProcessUiCaptureTasks(); 207 208 void SetDirtyFlag(bool isDirty = true); 209 bool GetDirtyFlag(); 210 void SetAccessibilityConfigChanged(); 211 void SetScreenPowerOnChanged(bool val); 212 bool GetScreenPowerOnChanged() const; 213 bool IsAccessibilityConfigChanged() const; 214 bool IsCurtainScreenUsingStatusChanged() const; 215 bool IsFastComposeAllow(uint64_t unsignedVsyncPeriod, bool nextVsyncRequested, 216 uint64_t unsignedNowTime, uint64_t lastVsyncTime); 217 // check if timestamp in vsync receiver sync with mainthread timestamp, if not, return false; 218 bool IsFastComposeVsyncTimeSync(uint64_t unsignedVsyncPeriod, bool nextVsyncRequested, 219 uint64_t unsignedNowTime, uint64_t lastVsyncTime, int64_t vsyncTimeStamp); 220 void CheckFastCompose(int64_t bufferTimeStamp); 221 bool CheckAdaptiveCompose(); 222 void ForceRefreshForUni(bool needDelay = false); 223 void TrimMem(std::unordered_set<std::u16string>& argSets, std::string& result); 224 void DumpMem(std::unordered_set<std::u16string>& argSets, std::string& result, std::string& type, pid_t pid = 0); 225 void DumpNode(std::string& result, uint64_t nodeId) const; 226 void CountMem(int pid, MemoryGraphic& mem); 227 void CountMem(std::vector<MemoryGraphic>& mems); 228 void SetAppWindowNum(uint32_t num); 229 bool SetSystemAnimatedScenes(SystemAnimatedScenes systemAnimatedScenes, bool isRegularAnimation = false); 230 SystemAnimatedScenes GetSystemAnimatedScenes(); 231 bool GetIsRegularAnimation() const; 232 // Save marks, and use it for SurfaceNodes later. 233 void SetWatermark(const pid_t& pid, const std::string& name, std::shared_ptr<Media::PixelMap> watermark); 234 void ClearWatermark(pid_t pid); 235 // Save marks, and use it for ScreenNode later. 236 void ShowWatermark(const std::shared_ptr<Media::PixelMap> &watermarkImg, bool flag); 237 void SetIsCachedSurfaceUpdated(bool isCachedSurfaceUpdated); 238 pid_t GetDesktopPidForRotationScene() const; SetForceUpdateUniRenderFlag(bool flag)239 void SetForceUpdateUniRenderFlag(bool flag) 240 { 241 forceUpdateUniRenderFlag_ = flag; 242 } 243 std::shared_ptr<Drawing::Image> GetWatermarkImg(); 244 bool GetWatermarkFlag(); 245 IsWatermarkFlagChanged()246 bool IsWatermarkFlagChanged() const 247 { 248 return lastWatermarkFlag_ != watermarkFlag_; 249 } 250 GetFrameCount()251 uint64_t GetFrameCount() const 252 { 253 return frameCount_; 254 } GetDrawStatusVec()255 std::vector<NodeId>& GetDrawStatusVec() 256 { 257 return curDrawStatusVec_; 258 } SetAppVSyncDistributor(const sptr<VSyncDistributor> & appVSyncDistributor)259 void SetAppVSyncDistributor(const sptr<VSyncDistributor>& appVSyncDistributor) 260 { 261 appVSyncDistributor_ = appVSyncDistributor; 262 } 263 264 DeviceType GetDeviceType() const; 265 bool IsSingleDisplay(); 266 bool HasMirrorDisplay() const; 267 uint64_t GetFocusNodeId() const; 268 uint64_t GetFocusLeashWindowId() const; GetClearMemDeeply()269 bool GetClearMemDeeply() const 270 { 271 return clearMemDeeply_; 272 } 273 GetClearMoment()274 ClearMemoryMoment GetClearMoment() const 275 { 276 if (!context_) { 277 return ClearMemoryMoment::NO_CLEAR; 278 } 279 return context_->clearMoment_; 280 } 281 SetClearMoment(ClearMemoryMoment moment)282 void SetClearMoment(ClearMemoryMoment moment) 283 { 284 if (!context_) { 285 return; 286 } 287 context_->clearMoment_ = moment; 288 } 289 IsPCThreeFingerScenesListScene()290 bool IsPCThreeFingerScenesListScene() const 291 { 292 return !threeFingerScenesList_.empty(); 293 } 294 295 void SurfaceOcclusionChangeCallback(VisibleData& dstCurVisVec); 296 void SurfaceOcclusionCallback(); 297 bool CheckSurfaceOcclusionNeedProcess(NodeId id); 298 void SubscribeAppState(); 299 void HandleOnTrim(Memory::SystemMemoryLevel level); 300 void SetCurtainScreenUsingStatus(bool isCurtainScreenOn); 301 void AddPidNeedDropFrame(std::vector<int32_t> pid); 302 void ClearNeedDropframePidList(); 303 void SetSelfDrawingGpuDirtyPidList(const std::vector<int32_t>& pid); 304 bool IsGpuDirtyEnable(NodeId nodeId); 305 bool IsNeedDropFrameByPid(NodeId nodeId); 306 void SetLuminanceChangingStatus(ScreenId id, bool isLuminanceChanged); 307 bool ExchangeLuminanceChangingStatus(ScreenId id); 308 bool IsCurtainScreenOn() const; 309 310 bool GetParallelCompositionEnabled(); 311 void SetFrameIsRender(bool isRender); 312 void AddSelfDrawingNodes(std::shared_ptr<RSSurfaceRenderNode> selfDrawingNode); 313 const std::vector<std::shared_ptr<RSSurfaceRenderNode>>& GetSelfDrawingNodes() const; 314 void ClearSelfDrawingNodes(); 315 GetDiscardJankFrames()316 bool GetDiscardJankFrames() const 317 { 318 return discardJankFrames_.load(); 319 } 320 SetDiscardJankFrames(bool discardJankFrames)321 void SetDiscardJankFrames(bool discardJankFrames) 322 { 323 if (discardJankFrames_.load() != discardJankFrames) { 324 discardJankFrames_.store(discardJankFrames); 325 } 326 } 327 GetSkipJankAnimatorFrame()328 bool GetSkipJankAnimatorFrame() const 329 { 330 return skipJankAnimatorFrame_.load(); 331 } 332 IsFirstFrameOfDrawingCacheDFXSwitch()333 bool IsFirstFrameOfDrawingCacheDFXSwitch() const 334 { 335 return isDrawingCacheDfxEnabledOfCurFrame_ != isDrawingCacheDfxEnabledOfLastFrame_; 336 } 337 SetDrawingCacheDfxEnabledOfCurFrame(bool isDrawingCacheDfxEnabledOfCurFrame)338 void SetDrawingCacheDfxEnabledOfCurFrame(bool isDrawingCacheDfxEnabledOfCurFrame) 339 { 340 isDrawingCacheDfxEnabledOfCurFrame_ = isDrawingCacheDfxEnabledOfCurFrame; 341 } 342 SetSkipJankAnimatorFrame(bool skipJankAnimatorFrame)343 void SetSkipJankAnimatorFrame(bool skipJankAnimatorFrame) 344 { 345 skipJankAnimatorFrame_.store(skipJankAnimatorFrame); 346 } 347 348 bool IsRequestedNextVSync(); 349 350 bool IsOcclusionNodesNeedSync(NodeId id, bool useCurWindow); 351 352 void CallbackDrawContextStatusToWMS(bool isUniRender = false); 353 void SetHardwareTaskNum(uint32_t num); 354 void RegisterUIExtensionCallback(pid_t pid, uint64_t userId, sptr<RSIUIExtensionCallback> callback, 355 bool unobscured = false); 356 void UnRegisterUIExtensionCallback(pid_t pid); 357 358 void SetAncoForceDoDirect(bool direct); 359 IsSystemAnimatedScenesListEmpty()360 bool IsSystemAnimatedScenesListEmpty() const 361 { 362 return systemAnimatedScenesList_.empty(); 363 } 364 GetRSVsyncRateReduceManager()365 RSVsyncRateReduceManager& GetRSVsyncRateReduceManager() 366 { 367 return rsVsyncRateReduceManager_; 368 } 369 IsFirstFrameOfOverdrawSwitch()370 bool IsFirstFrameOfOverdrawSwitch() const 371 { 372 return isOverDrawEnabledOfCurFrame_ != isOverDrawEnabledOfLastFrame_; 373 } 374 375 uint64_t GetRealTimeOffsetOfDvsync(int64_t time); 376 377 static bool GetMultiDisplay(const std::shared_ptr<RSBaseRenderNode>& rootNode); 378 GetMultiDisplayChange()379 bool GetMultiDisplayChange() const 380 { 381 return isMultiDisplayChange_; 382 } GetMultiDisplayStatus()383 bool GetMultiDisplayStatus() const 384 { 385 return isMultiDisplayPre_; 386 } 387 HasWiredMirrorDisplay()388 bool HasWiredMirrorDisplay() const 389 { 390 return hasWiredMirrorDisplay_; 391 } HasVirtualMirrorDisplay()392 bool HasVirtualMirrorDisplay() const 393 { 394 return hasVirtualMirrorDisplay_; 395 } GetCurrentVsyncTime()396 uint64_t GetCurrentVsyncTime() const 397 { 398 return curTime_; 399 } 400 401 void StartGPUDraw(); 402 403 void EndGPUDraw(); 404 405 struct GPUCompositonCacheGuard { GPUCompositonCacheGuardGPUCompositonCacheGuard406 GPUCompositonCacheGuard() 407 { 408 RSMainThread::Instance()->StartGPUDraw(); 409 } 410 ~GPUCompositonCacheGuardGPUCompositonCacheGuard411 ~GPUCompositonCacheGuard() 412 { 413 RSMainThread::Instance()->EndGPUDraw(); 414 } 415 }; 416 AddToUnmappedCacheSet(uint32_t bufferId)417 void AddToUnmappedCacheSet(uint32_t bufferId) 418 { 419 std::lock_guard<std::mutex> lock(unmappedCacheSetMutex_); 420 unmappedCacheSet_.insert(bufferId); 421 } 422 AddToUnmappedCacheSet(const std::set<uint32_t> & seqNumSet)423 void AddToUnmappedCacheSet(const std::set<uint32_t>& seqNumSet) 424 { 425 std::lock_guard<std::mutex> lock(unmappedCacheSetMutex_); 426 unmappedCacheSet_.insert(seqNumSet.begin(), seqNumSet.end()); 427 } 428 429 void ClearUnmappedCache(); 430 void InitVulkanErrorCallback(Drawing::GPUContext* gpuContext); 431 void NotifyUnmarshalTask(int64_t uiTimestamp); 432 void NotifyPackageEvent(const std::vector<std::string>& packageList); 433 void HandleTouchEvent(int32_t touchStatus, int32_t touchCnt); 434 void SetBufferInfo(uint64_t id, const std::string &name, uint32_t queueSize, 435 int32_t bufferCount, int64_t lastConsumeTime, bool isUrgent); 436 void GetFrontBufferDesiredPresentTimeStamp( 437 const sptr<IConsumerSurface>& consumer, int64_t& desiredPresentTimeStamp); 438 439 // Enable HWCompose 440 bool IsHardwareEnabledNodesNeedSync(); 441 bool WaitHardwareThreadTaskExecute(); 442 void NotifyHardwareThreadCanExecuteTask(); 443 void SetTaskEndWithTime(int64_t time); 444 445 uint32_t GetVsyncRefreshRate(); 446 void DVSyncUpdate(uint64_t dvsyncTime, uint64_t vsyncTime); 447 448 private: 449 using TransactionDataIndexMap = std::unordered_map<pid_t, 450 std::pair<uint64_t, std::vector<std::unique_ptr<RSTransactionData>>>>; 451 using DrawablesVec = std::vector<std::tuple<NodeId, NodeId, 452 DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>>; 453 454 RSMainThread(); 455 ~RSMainThread() noexcept; 456 RSMainThread(const RSMainThread&) = delete; 457 RSMainThread(const RSMainThread&&) = delete; 458 RSMainThread& operator=(const RSMainThread&) = delete; 459 RSMainThread& operator=(const RSMainThread&&) = delete; 460 461 void OnVsync(uint64_t timestamp, uint64_t frameCount, void* data); 462 void ProcessCommand(); 463 void UpdateSubSurfaceCnt(); 464 void Animate(uint64_t timestamp); 465 void ConsumeAndUpdateAllNodes(); 466 void ReleaseAllNodesBuffer(); 467 void Render(); 468 void OnUniRenderDraw(); 469 void SetDeviceType(); 470 void UniRender(std::shared_ptr<RSBaseRenderNode> rootNode); 471 bool CheckSurfaceNeedProcess(OcclusionRectISet& occlusionSurfaces, std::shared_ptr<RSSurfaceRenderNode> curSurface); 472 RSVisibleLevel CalcSurfaceNodeVisibleRegion(const std::shared_ptr<RSScreenRenderNode>& screenNode, 473 const std::shared_ptr<RSSurfaceRenderNode>& surfaceNode, Occlusion::Region& accumulatedRegion, 474 Occlusion::Region& curRegion, Occlusion::Region& totalRegion); 475 void CalcOcclusionImplementation(const std::shared_ptr<RSScreenRenderNode>& screenNode, 476 std::vector<RSBaseRenderNode::SharedPtr>& curAllSurfaces, VisibleData& dstCurVisVec, 477 std::map<NodeId, RSVisibleLevel>& dstPidVisMap); 478 void CalcOcclusion(); 479 void CallbackToWMS(VisibleData& curVisVec); 480 void SendCommands(); 481 void InitRSEventDetector(); 482 void RemoveRSEventDetector(); 483 void SetRSEventDetectorLoopStartTag(); 484 void SetRSEventDetectorLoopFinishTag(); 485 void CheckSystemSceneStatus(); 486 void RegisterScreenNodeListener(); 487 void UpdateScreenNodeScreenId(); 488 489 uint32_t GetRefreshRate() const; 490 uint32_t GetDynamicRefreshRate() const; 491 void SkipCommandByNodeId(std::vector<std::unique_ptr<RSTransactionData>>& transactionVec, pid_t pid); 492 static void OnHideNotchStatusCallback(const char *key, const char *value, void *context); 493 static void OnDrawingCacheDfxSwitchCallback(const char *key, const char *value, void *context); 494 static void OnFmtTraceSwitchCallback(const char *key, const char *value, void *context); 495 496 bool DoParallelComposition(std::shared_ptr<RSBaseRenderNode> rootNode); 497 498 void ClassifyRSTransactionData(std::shared_ptr<RSTransactionData> rsTransactionData); 499 void ProcessRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData, pid_t pid); 500 void ProcessSyncRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData, pid_t pid); 501 void ProcessSyncTransactionCount(std::unique_ptr<RSTransactionData>& rsTransactionData); 502 void StartSyncTransactionFallbackTask(std::unique_ptr<RSTransactionData>& rsTransactionData); 503 void ProcessAllSyncTransactionData(); 504 void ProcessCommandForDividedRender(); 505 void ProcessCommandForUniRender(); 506 void WaitUntilUnmarshallingTaskFinished(); 507 void MergeToEffectiveTransactionDataMap(TransactionDataMap& cachedTransactionDataMap); 508 509 void ClearDisplayBuffer(); 510 void PerfAfterAnim(bool needRequestNextVsync); 511 void PerfForBlurIfNeeded(); 512 void PerfMultiWindow(); 513 void RenderFrameStart(uint64_t timestamp); 514 void CheckAndUpdateTransactionIndex( 515 std::shared_ptr<TransactionDataMap>& transactionDataEffective, std::string& transactionFlags); 516 517 bool IsResidentProcess(pid_t pid) const; 518 bool IsNeedSkip(NodeId instanceRootNodeId, pid_t pid); 519 uint32_t GetForceCommitReason() const; 520 521 // UIFirst 522 bool CheckParallelSubThreadNodesStatus(); 523 void CacheCommands(); 524 bool IsSurfaceConsumerNeedSkip(sptr<IConsumerSurface> consumer); 525 bool CheckSubThreadNodeStatusIsDoing(NodeId appNodeId) const; 526 527 // used for informing hgm the bundle name of SurfaceRenderNodes 528 void InformHgmNodeInfo(); 529 void CheckIfNodeIsBundle(std::shared_ptr<RSSurfaceRenderNode> node); 530 531 void TraverseCanvasDrawingNodesNotOnTree(); 532 533 void SetFocusLeashWindowId(); 534 void ProcessHgmFrameRate(uint64_t timestamp); 535 bool IsLastFrameUIFirstEnabled(NodeId appNodeId) const; 536 RSVisibleLevel GetRegionVisibleLevel(const Occlusion::Region& curRegion, 537 const Occlusion::Region& visibleRegion); 538 void PrintCurrentStatus(); 539 void UpdateGpuContextCacheSize(); 540 void ProcessScreenHotPlugEvents(); 541 #ifdef RES_SCHED_ENABLE 542 void SubScribeSystemAbility(); 543 #endif 544 #if defined(RS_ENABLE_CHIPSET_VSYNC) 545 void ConnectChipsetVsyncSer(); 546 void SetVsyncInfo(uint64_t timestamp); 547 #endif 548 549 void RSJankStatsOnVsyncStart(int64_t onVsyncStartTime, int64_t onVsyncStartTimeSteady, 550 float onVsyncStartTimeSteadyFloat); 551 void RSJankStatsOnVsyncEnd(int64_t onVsyncStartTime, int64_t onVsyncStartTimeSteady, 552 float onVsyncStartTimeSteadyFloat); 553 int64_t GetCurrentSystimeMs() const; 554 int64_t GetCurrentSteadyTimeMs() const; 555 float GetCurrentSteadyTimeMsFloat() const; 556 void RequestNextVsyncForCachedCommand(std::string& transactionFlags, pid_t pid, uint64_t curIndex); 557 void UpdateLuminanceAndColorTemp(); 558 559 void PrepareUiCaptureTasks(std::shared_ptr<RSUniRenderVisitor> uniVisitor); 560 void UIExtensionNodesTraverseAndCallback(); 561 bool CheckUIExtensionCallbackDataChanged() const; 562 void RequestNextVSyncInner(VSyncReceiver::FrameCallback callback, 563 const std::string& fromWhom = "unknown", int64_t lastVSyncTS = 0, const int64_t& requestVsyncTime = 0); 564 565 void CheckBlurEffectCountStatistics(std::shared_ptr<RSBaseRenderNode> rootNode); 566 void OnCommitDumpClientNodeTree(NodeId nodeId, pid_t pid, uint32_t taskId, const std::string& result); 567 568 // Used for CommitAndReleaseLayers task 569 void SetFrameInfo(uint64_t frameCount, bool forceRefreshFlag); 570 571 // Record change status of multi or single display 572 void MultiDisplayChange(bool isMultiDisplay); 573 void DumpEventHandlerInfo(); 574 std::string SubHistoryEventQueue(std::string input); 575 std::string SubPriorityEventQueue(std::string input); 576 577 // Enable HWCompose 578 void CollectInfoForHardwareComposer(); 579 void ResetHardwareEnabledState(bool isUniRender); 580 void CheckIfHardwareForcedDisabled(); 581 bool DoDirectComposition(std::shared_ptr<RSBaseRenderNode> rootNode, bool waitForRT); 582 bool ExistBufferIsVisibleAndUpdate(); 583 bool NeedConsumeMultiCommand(int32_t& dvsyncPid); 584 bool NeedConsumeDVSyncCommand(uint32_t& endIndex, 585 std::vector<std::unique_ptr<RSTransactionData>>& transactionVec); 586 class RSScreenNodeListener : public RSIScreenNodeListener { 587 public: 588 ~RSScreenNodeListener() override = default; 589 590 void OnScreenConnect(ScreenId id) override; 591 void OnScreenDisconnect(ScreenId id) override; 592 }; 593 594 void UpdateDirectCompositionByAnimate(bool animateNeedRequestNextVsync); 595 void HandleTunnelLayerId(const std::shared_ptr<RSSurfaceHandler>& surfaceHandler, 596 const std::shared_ptr<RSSurfaceRenderNode>& surfaceNode); 597 598 bool isUniRender_ = RSUniRenderJudgement::IsUniRender(); 599 bool needWaitUnmarshalFinished_ = true; 600 bool clearMemoryFinished_ = true; 601 bool clearMemDeeply_ = false; 602 // Used to refresh the whole display when AccessibilityConfig is changed 603 bool isAccessibilityConfigChanged_ = false; 604 // Used to refresh the whole display when curtain screen status is changed 605 bool isCurtainScreenUsingStatusChanged_ = false; 606 607 const uint8_t opacity_ = 255; 608 bool vsyncControlEnabled_ = true; 609 bool systemAnimatedScenesEnabled_ = false; 610 bool isFoldScreenDevice_ = false; 611 mutable std::atomic_bool hasWiredMirrorDisplay_ = false; 612 mutable std::atomic_bool hasVirtualMirrorDisplay_ = false; 613 614 #ifdef RS_ENABLE_GPU 615 bool needDrawFrame_ = true; 616 bool needPostAndWait_ = true; 617 bool isLastFrameNeedPostAndWait_ = true; 618 #endif 619 620 bool isNeedResetClearMemoryTask_ = false; 621 bool watermarkFlag_ = false; 622 bool lastWatermarkFlag_ = false; 623 bool hasProtectedLayer_ = false; 624 DeviceType deviceType_ = DeviceType::PHONE; 625 bool isCachedSurfaceUpdated_ = false; 626 // used for informing hgm the bundle name of SurfaceRenderNodes 627 bool forceUpdateUniRenderFlag_ = false; 628 // for drawing cache dfx 629 bool isDrawingCacheDfxEnabledOfCurFrame_ = false; 630 bool isDrawingCacheDfxEnabledOfLastFrame_ = false; 631 // for dvsync (animate requestNextVSync after mark rsnotrendering) 632 bool forceUIFirstChanged_ = false; 633 bool lastFrameUIExtensionDataEmpty_ = false; 634 // overDraw 635 bool isOverDrawEnabledOfCurFrame_ = false; 636 bool isOverDrawEnabledOfLastFrame_ = false; 637 #if defined(RS_ENABLE_CHIPSET_VSYNC) 638 bool initVsyncServiceFlag_ = true; 639 #endif 640 bool isCurtainScreenOn_ = false; 641 // partial render 642 bool isForceRefresh_ = false; 643 // record multidisplay status change 644 bool isMultiDisplayPre_ = false; 645 bool isMultiDisplayChange_ = false; 646 #ifdef RS_ENABLE_VK 647 bool needCreateVkPipeline_ = true; 648 #endif 649 std::atomic<bool> isDirty_ = false; 650 bool prevHdrSwitchStatus_ = true; 651 std::atomic<bool> screenPowerOnChanged_ = false; 652 std::atomic_bool doWindowAnimate_ = false; 653 std::atomic<bool> isGlobalDarkColorMode_ = false; 654 // for statistic of jank frames 655 std::atomic_bool discardJankFrames_ = false; 656 std::atomic_bool skipJankAnimatorFrame_ = false; 657 bool isImplicitAnimationEnd_ = false; 658 659 pid_t lastCleanCachePid_ = -1; 660 int32_t unmarshalFinishedCount_ = 0; 661 uint32_t appWindowNum_ = 0; 662 pid_t desktopPidForRotationScene_ = 0; 663 int32_t subscribeFailCount_ = 0; 664 SystemAnimatedScenes systemAnimatedScenes_ = SystemAnimatedScenes::OTHERS; 665 bool isRegularAnimation_ = false; 666 uint32_t leashWindowCount_ = 0; 667 pid_t exitedPid_ = -1; 668 RsParallelType rsParallelType_; 669 std::atomic<int32_t> focusAppPid_ = -1; 670 std::atomic<int32_t> focusAppUid_ = -1; 671 std::atomic<uint32_t> requestNextVsyncNum_ = 0; 672 std::atomic<uint32_t> drawingRequestNextVsyncNum_ = 0; 673 uint64_t curTime_ = 0; 674 uint64_t timestamp_ = 0; 675 uint64_t vsyncId_ = 0; 676 uint64_t lastAnimateTimestamp_ = 0; 677 uint64_t prePerfTimestamp_ = 0; 678 uint64_t lastCleanCacheTimestamp_ = 0; 679 uint64_t focusLeashWindowId_ = 0; 680 std::string focusLeashWindowName_ = ""; 681 std::string appWindowName_ = ""; 682 uint32_t appPid_ = 0; 683 uint64_t lastFocusNodeId_ = 0; 684 uint64_t appWindowId_ = 0; 685 ScreenId screenNodeScreenId_ = 0; 686 std::atomic<uint64_t> focusNodeId_ = 0; 687 std::atomic<uint64_t> frameCount_ = 0; 688 std::atomic<bool> isRunning_ = false; 689 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 690 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 691 std::shared_ptr<RSContext> context_; 692 std::shared_ptr<VSyncReceiver> receiver_ = nullptr; 693 sptr<VSyncDistributor> appVSyncDistributor_ = nullptr; 694 std::shared_ptr<RSBaseRenderEngine> renderEngine_; 695 std::shared_ptr<RSBaseEventDetector> rsCompositionTimeoutDetector_; 696 std::shared_ptr<Drawing::Image> watermarkImg_ = nullptr; // display safterWatermask(true) or hide it(false) 697 std::shared_ptr<RSAppStateListener> rsAppStateListener_; 698 std::unique_ptr<RSVsyncClient> vsyncClient_ = nullptr; 699 RSTaskMessage::RSTask mainLoop_; 700 std::unordered_map<NodeId, uint64_t> dividedRenderbufferTimestamps_; 701 702 std::mutex transitionDataMutex_; 703 std::unordered_map<NodeId, std::map<uint64_t, std::vector<std::unique_ptr<RSCommand>>>> cachedCommands_; 704 std::map<uint64_t, std::vector<std::unique_ptr<RSCommand>>> effectiveCommands_; 705 std::map<uint64_t, std::vector<std::unique_ptr<RSCommand>>> pendingEffectiveCommands_; 706 std::unordered_map<pid_t, std::vector<std::unique_ptr<RSTransactionData>>> syncTransactionData_; 707 std::unordered_map<int32_t, int32_t> subSyncTransactionCounts_; 708 709 TransactionDataMap cachedTransactionDataMap_; 710 TransactionDataIndexMap effectiveTransactionDataIndexMap_; 711 std::map<pid_t, std::vector<std::unique_ptr<RSTransactionData>>> cachedSkipTransactionDataMap_; 712 std::unordered_map<pid_t, uint64_t> transactionDataLastWaitTime_; 713 714 int64_t requestNextVsyncTime_ = -1; 715 bool isHdrSwitchChanged_ = false; 716 bool isColorTemperatureOn_ = false; 717 718 /** 719 * @brief A set to store buffer IDs of images that are about to be unmapped from GPU cache. 720 * 721 * This set is used to track images that are no longer needed and should be removed from the GPU cache. 722 * When an image is unmapped, its buffer ID is added to this set. During the rendering process, 723 * if an image is found in this set, it means that the image is no longer needed and can be safely 724 * removed from the GPU cache. 725 */ 726 std::set<uint32_t> unmappedCacheSet_ = {}; // must protected by unmappedCacheSetMutex_ 727 std::mutex unmappedCacheSetMutex_; 728 729 /** 730 * @brief An atomic integer to keep track of the GPU draw count. 731 * 732 * This variable is used to safely increment and decrement the count of GPU draw operations 733 * across multiple threads without causing data races. 734 */ 735 std::atomic<int> gpuDrawCount_ = 0; 736 737 std::string transactionFlags_ = ""; 738 std::unordered_map<uint32_t, sptr<IApplicationAgent>> applicationAgentMap_; 739 740 std::thread::id mainThreadId_; 741 std::map<pid_t, sptr<RSIOcclusionChangeCallback>> occlusionListeners_; 742 std::mutex occlusionMutex_; 743 744 RSTaskMessage::RSTask unmarshalBarrierTask_; 745 std::condition_variable unmarshalTaskCond_; 746 std::mutex unmarshalMutex_; 747 748 mutable std::mutex uniRenderMutex_; 749 750 // Used to refresh the whole display when luminance is changed 751 std::unordered_map<ScreenId, bool> displayLuminanceChanged_; 752 std::mutex luminanceMutex_; 753 754 // used for blocking mainThread when hardwareThread has 2 and more task to Execute 755 mutable std::mutex hardwareThreadTaskMutex_; 756 std::condition_variable hardwareThreadTaskCond_; 757 758 VisibleData lastVisVec_; 759 std::map<NodeId, uint64_t> lastDrawStatusMap_; 760 std::vector<NodeId> curDrawStatusVec_; 761 std::vector<NodeId> lastSurfaceIds_; 762 std::string focusAppBundleName_ = ""; 763 std::string focusAppAbilityName_ = ""; 764 765 RSEventManager rsEventManager_; 766 767 std::vector<std::shared_ptr<RSSurfaceRenderNode>> selfDrawingNodes_; 768 #ifdef RS_ENABLE_GPU 769 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr> selfDrawables_; 770 #endif 771 772 // Enable HWCompose 773 std::vector<std::shared_ptr<RSSurfaceRenderNode>> hardwareEnabledNodes_; 774 DrawablesVec hardwareEnabledDrwawables_; 775 bool isHardwareEnabledBufferUpdated_ = false; 776 bool isHardwareForcedDisabled_ = false; // if app node has shadow or filter, disable hardware composer for all 777 bool doDirectComposition_ = true; 778 bool lastAnimateNeedRequestNextVsync_ = false; 779 RSDirectCompositionHelper directComposeHelper_; 780 781 // for client node tree dump 782 struct NodeTreeDumpTask { 783 size_t count = 0; 784 size_t completionCount = 0; 785 std::map<pid_t, std::optional<std::string>> data; 786 }; 787 std::mutex nodeTreeDumpMutex_; 788 std::condition_variable nodeTreeDumpCondVar_; 789 std::unordered_map<uint32_t, NodeTreeDumpTask> nodeTreeDumpTasks_; 790 791 std::map<std::pair<pid_t, std::string>, std::shared_ptr<Media::PixelMap>> surfaceNodeWatermarks_; 792 std::unordered_map<pid_t, uint32_t> registerSurfaceWaterMaskCount_; 793 794 // UIFirst 795 std::list<std::shared_ptr<RSSurfaceRenderNode>> subThreadNodes_; 796 std::unordered_map<NodeId, bool> cacheCmdSkippedNodes_; 797 std::unordered_map<pid_t, std::pair<std::vector<NodeId>, bool>> cacheCmdSkippedInfo_; 798 std::set<std::shared_ptr<RSBaseRenderNode>> oldDisplayChildren_; 799 // for ui first 800 std::mutex mutex_; 801 std::queue<std::shared_ptr<Drawing::Surface>> tmpSurfaces_; 802 803 // for surface occlusion change callback 804 std::mutex surfaceOcclusionMutex_; 805 std::vector<NodeId> lastRegisteredSurfaceOnTree_; 806 std::mutex systemAnimatedScenesMutex_; 807 std::list<std::pair<SystemAnimatedScenes, time_t>> systemAnimatedScenesList_; 808 std::list<std::pair<SystemAnimatedScenes, time_t>> threeFingerScenesList_; 809 std::unordered_map<NodeId, // map<node ID, <pid, callback, partition points vector, level>> 810 std::tuple<pid_t, sptr<RSISurfaceOcclusionChangeCallback>, 811 std::vector<float>, uint8_t>> surfaceOcclusionListeners_; 812 std::unordered_map<NodeId, // map<node ID, <surface node, app window node>> 813 std::pair<std::shared_ptr<RSSurfaceRenderNode>, std::shared_ptr<RSSurfaceRenderNode>>> savedAppWindowNode_; 814 815 // used for watermark 816 std::mutex watermarkMutex_; 817 818 // for ui captures 819 std::vector<std::tuple<NodeId, std::function<void()>>> pendingUiCaptureTasks_; 820 std::queue<std::tuple<NodeId, std::function<void()>>> uiCaptureTasks_; 821 // uiextension 822 std::mutex uiExtensionMutex_; 823 UIExtensionCallbackData uiExtensionCallbackData_; 824 UIExtensionCallbackData unobscureduiExtensionCallbackData_; 825 // <pid, <uid, callback>> 826 std::map<pid_t, std::pair<uint64_t, sptr<RSIUIExtensionCallback>>> uiExtensionListenners_ = {}; 827 std::map<pid_t, std::pair<uint64_t, sptr<RSIUIExtensionCallback>>> uiUnobscuredExtensionListenners_ = {}; 828 829 #ifdef RS_PROFILER_ENABLED 830 friend class RSProfiler; 831 #endif 832 std::set<pid_t> exitedPidSet_; 833 #ifdef RS_ENABLE_GPU 834 RSDrawFrame drawFrame_; 835 #endif 836 std::unique_ptr<RSRenderThreadParams> renderThreadParams_ = nullptr; // sync to render thread 837 std::unordered_set<int32_t> surfacePidNeedDropFrame_; 838 RSVsyncRateReduceManager rsVsyncRateReduceManager_; 839 840 // for record fastcompose time change 841 uint64_t lastFastComposeTimeStamp_ = 0; 842 uint64_t lastFastComposeTimeStampDiff_ = 0; 843 std::atomic<bool> waitForDVSyncFrame_ = false; 844 std::atomic<uint64_t> vsyncRsTimestamp_ = 0; 845 std::string dumpInfo_; 846 std::atomic<uint32_t> currentNum_ = 0; 847 #if defined(ACCESSIBILITY_ENABLE) 848 std::shared_ptr<AccessibilityObserver> accessibilityObserver_; 849 #endif 850 #ifdef RES_SCHED_ENABLE 851 sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr; 852 #endif 853 854 std::function<void(const std::shared_ptr<RSSurfaceRenderNode>& surfaceNode)> consumeAndUpdateNode_; 855 HgmContext hgmContext_; 856 std::mutex dumpInfoMutex_; 857 }; 858 } // namespace OHOS::Rosen 859 #endif // RS_MAIN_THREAD 860