• 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 <future>
20 #include <memory>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 #include <set>
25 
26 #include "refbase.h"
27 #include "rs_base_render_engine.h"
28 #include "vsync_distributor.h"
29 #include <event_handler.h>
30 #include "vsync_receiver.h"
31 
32 #include "command/rs_command.h"
33 #include "common/rs_common_def.h"
34 #include "common/rs_thread_handler.h"
35 #include "common/rs_thread_looper.h"
36 #include "ipc_callbacks/iapplication_agent.h"
37 #include "ipc_callbacks/rs_iocclusion_change_callback.h"
38 #include "memory/rs_memory_graphic.h"
39 #include "pipeline/rs_context.h"
40 #include "pipeline/rs_uni_render_judgement.h"
41 #include "platform/drawing/rs_vsync_client.h"
42 #include "platform/common/rs_event_manager.h"
43 #include "transaction/rs_transaction_data.h"
44 
45 namespace OHOS::Rosen {
46 #if defined(ACCESSIBILITY_ENABLE)
47 class AccessibilityObserver;
48 #endif
49 class HgmFrameRateManager;
50 struct FrameRateRangeData;
51 namespace Detail {
52 template<typename Task>
53 class ScheduledTask : public RefBase {
54 public:
Create(Task && task)55     static auto Create(Task&& task)
56     {
57         sptr<ScheduledTask<Task>> t(new ScheduledTask(std::forward<Task&&>(task)));
58         return std::make_pair(t, t->task_.get_future());
59     }
60 
Run()61     void Run()
62     {
63         task_();
64     }
65 
66 private:
ScheduledTask(Task && task)67     explicit ScheduledTask(Task&& task) : task_(std::move(task)) {}
68     ~ScheduledTask() override = default;
69 
70     using Return = std::invoke_result_t<Task>;
71     std::packaged_task<Return()> task_;
72 };
73 } // namespace Detail
74 
75 class RSMainThread {
76 public:
77     static RSMainThread* Instance();
78 
79     void Init();
80     void Start();
81     void RecvRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData);
82     void RequestNextVSync();
83     void PostTask(RSTaskMessage::RSTask task);
84     void PostSyncTask(RSTaskMessage::RSTask task);
85     void QosStateDump(std::string& dumpString);
86     void RenderServiceTreeDump(std::string& dumpString);
87     void RsEventParamDump(std::string& dumpString);
88     bool IsUIFirstOn() const;
89 
90     template<typename Task, typename Return = std::invoke_result_t<Task>>
ScheduleTask(Task && task)91     std::future<Return> ScheduleTask(Task&& task)
92     {
93         auto [scheduledTask, taskFuture] = Detail::ScheduledTask<Task>::Create(std::forward<Task&&>(task));
94         PostTask([t(std::move(scheduledTask))]() { t->Run(); });
95         return std::move(taskFuture);
96     }
97 
GetRenderEngine()98     const std::shared_ptr<RSBaseRenderEngine>& GetRenderEngine() const
99     {
100         return isUniRender_ ? uniRenderEngine_ : renderEngine_;
101     }
102 
GetContext()103     RSContext& GetContext()
104     {
105         return *context_;
106     }
107 
Id()108     std::thread::id Id() const
109     {
110         return mainThreadId_;
111     }
112 
113     /* Judge if rootnode has to be prepared based on it corresponding process is active
114      * If its pid is in activeProcessPids_ set, return true
115      */
CheckNodeHasToBePreparedByPid(NodeId nodeId,NodeId rootNodeId,bool isClassifyByRoot)116     bool CheckNodeHasToBePreparedByPid(NodeId nodeId, NodeId rootNodeId, bool isClassifyByRoot)
117     {
118         if (activeAppsInProcess_.empty()) {
119             return false;
120         }
121         pid_t pid = ExtractPid(nodeId);
122         if (activeAppsInProcess_.find(pid) == activeAppsInProcess_.end()) {
123             return false;
124         }
125         if (!isClassifyByRoot) {
126             return true;
127         }
128         auto& activeApps = activeAppsInProcess_[pid];
129         if (activeApps.find(INVALID_NODEID) != activeApps.end()) {
130             return true;
131         }
132         return (activeProcessNodeIds_.find(rootNodeId) != activeProcessNodeIds_.end());
133     }
134 
135     void RegisterApplicationAgent(uint32_t pid, sptr<IApplicationAgent> app);
136     void UnRegisterApplicationAgent(sptr<IApplicationAgent> app);
137 
138     void RegisterOcclusionChangeCallback(pid_t pid, sptr<RSIOcclusionChangeCallback> callback);
139     void UnRegisterOcclusionChangeCallback(pid_t pid);
140 
141     void WaitUtilUniRenderFinished();
142     void NotifyUniRenderFinish();
143 
144     bool WaitUntilDisplayNodeBufferReleased(RSDisplayRenderNode& node);
145     void NotifyDisplayNodeBufferReleased();
146 
147     // driven render
148     void NotifyDrivenRenderFinish();
149     void WaitUtilDrivenRenderFinished();
150 
151     void ClearTransactionDataPidInfo(pid_t remotePid);
152     void AddTransactionDataPidInfo(pid_t remotePid);
153 
154     void SetFocusAppInfo(
155         int32_t pid, int32_t uid, const std::string &bundleName, const std::string &abilityName, uint64_t focusNodeId);
156     std::unordered_map<NodeId, bool> GetCacheCmdSkippedNodes() const;
157 
158     sptr<VSyncDistributor> rsVSyncDistributor_;
159 
160     void SetDirtyFlag();
161     void SetAccessibilityConfigChanged();
162     void ForceRefreshForUni();
163     void TrimMem(std::unordered_set<std::u16string>& argSets, std::string& result);
164     void DumpMem(std::unordered_set<std::u16string>& argSets, std::string& result, std::string& type, int pid = 0);
165     void CountMem(int pid, MemoryGraphic& mem);
166     void CountMem(std::vector<MemoryGraphic>& mems);
167     void SetAppWindowNum(uint32_t num);
168     void ShowWatermark(const std::shared_ptr<Media::PixelMap> &watermarkImg, bool isShow);
169     void SetIsCachedSurfaceUpdated(bool isCachedSurfaceUpdated);
170 #ifndef USE_ROSEN_DRAWING
171     sk_sp<SkImage> GetWatermarkImg();
172 #else
173     std::shared_ptr<Drawing::Image> GetWatermarkImg();
174 #endif
175     bool GetWatermarkFlag();
GetFrameCount()176     uint64_t GetFrameCount() const
177     {
178         return frameCount_;
179     }
180     // add node info after cmd data process
181     void AddActiveNodeId(pid_t pid, NodeId id);
182 
183     void ProcessHgmFrameRate(FrameRateRangeData data, uint64_t timestamp);
184 private:
185     using TransactionDataIndexMap = std::unordered_map<pid_t,
186         std::pair<uint64_t, std::vector<std::unique_ptr<RSTransactionData>>>>;
187 
188     RSMainThread();
189     ~RSMainThread() noexcept;
190     RSMainThread(const RSMainThread&) = delete;
191     RSMainThread(const RSMainThread&&) = delete;
192     RSMainThread& operator=(const RSMainThread&) = delete;
193     RSMainThread& operator=(const RSMainThread&&) = delete;
194 
195     bool IsSingleDisplay();
196     void OnVsync(uint64_t timestamp, void* data);
197     void ProcessCommand();
198     void Animate(uint64_t timestamp);
199     void ConsumeAndUpdateAllNodes();
200     void CollectInfoForHardwareComposer();
201     void CollectInfoForDrivenRender();
202     void ReleaseAllNodesBuffer();
203     void Render();
204     void SetDeviceType();
205     void UniRender(std::shared_ptr<RSBaseRenderNode> rootNode);
206     bool CheckSurfaceNeedProcess(OcclusionRectISet& occlusionSurfaces, std::shared_ptr<RSSurfaceRenderNode> curSurface);
207     void CalcOcclusionImplementation(std::vector<RSBaseRenderNode::SharedPtr>& curAllSurfaces);
208     void CalcOcclusion();
209     bool CheckQosVisChanged(std::map<uint32_t, bool>& pidVisMap);
210     void CallbackToQOS(std::map<uint32_t, bool>& pidVisMap);
211     void CallbackToWMS(VisibleData& curVisVec);
212     void SendCommands();
213     void InitRSEventDetector();
214     void RemoveRSEventDetector();
215     void SetRSEventDetectorLoopStartTag();
216     void SetRSEventDetectorLoopFinishTag();
217     void UpdateUIFirstSwitch();
218     void SkipCommandByNodeId(std::vector<std::unique_ptr<RSTransactionData>>& transactionVec, pid_t pid);
219 #ifndef USE_ROSEN_DRAWING
220 #ifdef NEW_SKIA
221     void ReleaseExitSurfaceNodeAllGpuResource(GrDirectContext* grContext);
222 #else
223     void ReleaseExitSurfaceNodeAllGpuResource(GrContext* grContext);
224 #endif
225 #else
226     void ReleaseExitSurfaceNodeAllGpuResource(Drawing::GPUContext* grContext);
227 #endif
228 
229     bool DoParallelComposition(std::shared_ptr<RSBaseRenderNode> rootNode);
230 
231     void ClassifyRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData);
232     void ProcessRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData, pid_t pid);
233     void ProcessSyncRSTransactionData(std::unique_ptr<RSTransactionData>& rsTransactionData, pid_t pid);
234     void ProcessAllSyncTransactionData();
235     void ProcessCommandForDividedRender();
236     void ProcessCommandForUniRender();
237     void WaitUntilUnmarshallingTaskFinished();
238     void MergeToEffectiveTransactionDataMap(TransactionDataMap& cachedTransactionDataMap);
239 
240     void CheckColdStartMap();
241     void ClearDisplayBuffer();
242     void PerfAfterAnim(bool needRequestNextVsync);
243     void PerfForBlurIfNeeded();
244     void PerfMultiWindow();
245     void RenderFrameStart();
246     void ResetHardwareEnabledState();
247     void CheckAndUpdateTransactionIndex(
248         std::shared_ptr<TransactionDataMap>& transactionDataEffective, std::string& transactionFlags);
249 
250     bool IsResidentProcess(pid_t pid);
251     bool IsNeedSkip(NodeId instanceRootNodeId, pid_t pid);
252 
253     bool NeedReleaseGpuResource(const RSRenderNodeMap& nodeMap);
254 
255     // UIFirst
256     void CheckParallelSubThreadNodesStatus();
257     void CacheCommands();
258 
259     // used for informing hgm the bundle name of SurfaceRenderNodes
260     void InformHgmNodeInfo();
261     void CheckIfNodeIsBundle(std::shared_ptr<RSSurfaceRenderNode> node);
262 
263     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
264     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
265     RSTaskMessage::RSTask mainLoop_;
266     std::unique_ptr<RSVsyncClient> vsyncClient_ = nullptr;
267     std::unordered_map<NodeId, uint64_t> bufferTimestamps_;
268 
269     std::mutex transitionDataMutex_;
270     std::unordered_map<NodeId, std::map<uint64_t, std::vector<std::unique_ptr<RSCommand>>>> cachedCommands_;
271     std::map<uint64_t, std::vector<std::unique_ptr<RSCommand>>> effectiveCommands_;
272     std::map<uint64_t, std::vector<std::unique_ptr<RSCommand>>> pendingEffectiveCommands_;
273     // Collect pids of surfaceview's update(ConsumeAndUpdateAllNodes), effective commands(processCommand) and Animate
274     std::unordered_map<pid_t, std::unordered_set<NodeId>> activeAppsInProcess_;
275     std::unordered_map<NodeId, std::unordered_set<NodeId>> activeProcessNodeIds_;
276     std::unordered_map<pid_t, std::vector<std::unique_ptr<RSTransactionData>>> syncTransactionData_;
277     int32_t syncTransactionCount_ { 0 };
278 
279     TransactionDataMap cachedTransactionDataMap_;
280     TransactionDataIndexMap effectiveTransactionDataIndexMap_;
281     std::map<pid_t, std::vector<std::unique_ptr<RSTransactionData>>> cachedSkipTransactionDataMap_;
282     std::unordered_map<pid_t, uint64_t> transactionDataLastWaitTime_;
283 
284     uint64_t curTime_ = 0;
285     uint64_t timestamp_ = 0;
286     uint64_t lastAnimateTimestamp_ = 0;
287     uint64_t prePerfTimestamp_ = 0;
288     uint64_t lastCleanCacheTimestamp_ = 0;
289     std::unordered_map<uint32_t, sptr<IApplicationAgent>> applicationAgentMap_;
290 
291     std::shared_ptr<RSContext> context_;
292     std::thread::id mainThreadId_;
293     std::shared_ptr<VSyncReceiver> receiver_ = nullptr;
294     std::map<pid_t, sptr<RSIOcclusionChangeCallback>> occlusionListeners_;
295     std::mutex occlusionMutex_;
296 
297     bool isUniRender_ = RSUniRenderJudgement::IsUniRender();
298     RSTaskMessage::RSTask unmarshalBarrierTask_;
299     std::condition_variable unmarshalTaskCond_;
300     std::mutex unmarshalMutex_;
301     int32_t unmarshalFinishedCount_ = 0;
302 
303     mutable std::mutex uniRenderMutex_;
304     bool uniRenderFinished_ = false;
305     std::condition_variable uniRenderCond_;
306     // used for blocking mainThread before displayNode has no freed buffer to request
307     mutable std::mutex displayNodeBufferReleasedMutex_;
308     bool displayNodeBufferReleased_ = false;
309     // used for stalling mainThread before displayNode has no freed buffer to request
310     std::condition_variable displayNodeBufferReleasedCond_;
311 
312     // driven render
313     mutable std::mutex drivenRenderMutex_;
314     bool drivenRenderFinished_ = false;
315     std::condition_variable drivenRenderCond_;
316 
317     // Used to refresh the whole display when AccessibilityConfig is changed
318     bool isAccessibilityConfigChanged_ = false;
319 
320     std::map<uint32_t, bool> lastPidVisMap_;
321     VisibleData lastVisVec_;
322     bool qosPidCal_ = false;
323     bool isDirty_ = false;
324     std::atomic_bool doWindowAnimate_ = false;
325     uint32_t lastSurfaceCnt_ = 0;
326     int32_t focusAppPid_ = -1;
327     int32_t focusAppUid_ = -1;
328     const uint8_t opacity_ = 255;
329     std::string focusAppBundleName_ = "";
330     std::string focusAppAbilityName_ = "";
331     uint64_t focusNodeId_ = 0;
332     uint64_t lastFocusNodeId_ = 0;
333     uint32_t appWindowNum_ = 0;
334     uint32_t requestNextVsyncNum_ = 0;
335     bool lastFrameHasFilter_ = false;
336 
337     std::shared_ptr<RSBaseRenderEngine> renderEngine_;
338     std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_;
339     std::shared_ptr<RSBaseEventDetector> rsCompositionTimeoutDetector_;
340     RSEventManager rsEventManager_;
341 #if defined(ACCESSIBILITY_ENABLE)
342     std::shared_ptr<AccessibilityObserver> accessibilityObserver_;
343 #endif
344 
345     // used for hardware enabled case
346     bool doDirectComposition_ = true;
347     bool isHardwareEnabledBufferUpdated_ = false;
348     std::vector<std::shared_ptr<RSSurfaceRenderNode>> hardwareEnabledNodes_;
349     bool isHardwareForcedDisabled_ = false; // if app node has shadow or filter, disable hardware composer for all
350 
351     // used for watermark
352     std::mutex watermarkMutex_;
353 #ifndef USE_ROSEN_DRAWING
354     sk_sp<SkImage> watermarkImg_ = nullptr;
355 #else
356     std::shared_ptr<Drawing::Image> watermarkImg_ = nullptr;
357 #endif
358     bool isShow_ = false;
359 
360     // driven render
361     bool hasDrivenNodeOnUniTree_ = false;
362     bool hasDrivenNodeMarkRender_ = false;
363 
364     std::shared_ptr<HgmFrameRateManager> frameRateMgr_ = nullptr;
365 
366     // UIFirst
367     std::list<std::shared_ptr<RSSurfaceRenderNode>> subThreadNodes_;
368     std::unordered_map<NodeId, bool> cacheCmdSkippedNodes_;
369     std::unordered_map<pid_t, std::pair<std::vector<NodeId>, bool>> cacheCmdSkippedInfo_;
370     std::atomic<uint64_t> frameCount_ = 0;
371     std::set<std::shared_ptr<RSBaseRenderNode>> oldDisplayChildren_;
372     DeviceType deviceType_ = DeviceType::PHONE;
373     bool isCachedSurfaceUpdated_ = false;
374     bool isUiFirstOn_ = false;
375 
376     // used for informing hgm the bundle name of SurfaceRenderNodes
377     bool noBundle_ = false;
378     std::string currentBundleName_ = "";
379 };
380 } // namespace OHOS::Rosen
381 #endif // RS_MAIN_THREAD
382