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