• 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 RENDER_SERVICE_PIPELINE_RS_UNI_RENDER_THREAD_H
17 #define RENDER_SERVICE_PIPELINE_RS_UNI_RENDER_THREAD_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "common/rs_thread_handler.h"
24 #include "common/rs_thread_looper.h"
25 #include "event_handler.h"
26 #include "params/rs_render_thread_params.h"
27 #include "pipeline/rs_context.h"
28 #include "rs_base_render_engine.h"
29 #ifdef RES_SCHED_ENABLE
30 #include "vsync_system_ability_listener.h"
31 #endif
32 
33 namespace OHOS {
34 namespace Rosen {
35 namespace DrawableV2 {
36 class RSRenderNodeDrawable;
37 class RSScreenRenderNodeDrawable;
38 }
39 
40 class RSUniRenderThread {
41 public:
42     using Callback = std::function<void()>;
43     static RSUniRenderThread& Instance();
44 
45     // disable copy and move
46     RSUniRenderThread(const RSUniRenderThread&) = delete;
47     RSUniRenderThread& operator=(const RSUniRenderThread&) = delete;
48     RSUniRenderThread(RSUniRenderThread&&) = delete;
49     RSUniRenderThread& operator=(RSUniRenderThread&&) = delete;
50 
51     void Start();
52     void InitGrContext();
53     void RenderFrames();
54     void Sync(std::unique_ptr<RSRenderThreadParams>&& stagingRenderThreadParams);
55     void PostTask(const std::function<void()>& task);
56     void RemoveTask(const std::string& name);
57     void PostRTTask(const std::function<void()>& task);
58     void PostImageReleaseTask(const std::function<void()>& task);
59     void RunImageReleaseTask();
60     void ClearResource();
61     void PostTask(RSTaskMessage::RSTask task, const std::string& name, int64_t delayTime,
62         AppExecFwk::EventQueue::Priority priority = AppExecFwk::EventQueue::Priority::HIGH);
63     void PostSyncTask(const std::function<void()>& task);
64     bool IsIdle() const;
65     void Render();
66     void ReleaseSelfDrawingNodeBuffer();
67     void ReleaseSurfaceBufferOpItemBuffer();
68     std::shared_ptr<RSBaseRenderEngine> GetRenderEngine() const;
69     void NotifyScreenNodeBufferReleased();
70     bool WaitUntilScreenNodeBufferReleased(DrawableV2::RSScreenRenderNodeDrawable& screenNodeDrawable);
71 
72     uint64_t GetCurrentTimestamp() const;
73     int64_t GetActualTimestamp() const;
74     uint64_t GetVsyncId() const;
75     bool GetForceRefreshFlag() const;
76     uint32_t GetPendingScreenRefreshRate() const;
77     uint64_t GetPendingConstraintRelativeTime() const;
78     uint64_t GetFastComposeTimeStampDiff() const;
79     sptr<SyncFence> GetAcquireFence();
80     void PurgeCacheBetweenFrames();
81     void ClearMemoryCache(ClearMemoryMoment moment, bool deeply, pid_t pid = -1);
82     void DefaultClearMemoryCache();
83     void ReclaimMemory();
84     void PostClearMemoryTask(ClearMemoryMoment moment, bool deeply, bool isDefaultClean);
85     void MemoryManagementBetweenFrames();
86     void FlushGpuMemoryInWaitQueueBetweenFrames();
87     void SuppressGpuCacheBelowCertainRatioBetweenFrames();
88     void ResetClearMemoryTask(bool isDoDirectComposition = false);
89     void PurgeShaderCacheAfterAnimate();
90     void SetReclaimMemoryFinished(bool isFinished);
91     bool IsReclaimMemoryFinished();
92     void SetTimeToReclaim(bool isTimeToReclaim);
93     bool IsTimeToReclaim();
94     void SetDefaultClearMemoryFinished(bool isFinished);
95     bool IsDefaultClearMemroyFinished();
96     bool GetClearMemoryFinished() const;
97     bool GetClearMemDeeply() const;
98     void SetClearMoment(ClearMemoryMoment moment);
99     ClearMemoryMoment GetClearMoment() const;
100     uint32_t GetRefreshRate() const;
101     void DumpMem(DfxString& log);
102     void TrimMem(std::string& dumpString, std::string& type);
103     std::shared_ptr<Drawing::Image> GetWatermarkImg();
GetFrameCount()104     uint64_t GetFrameCount() const
105     {
106         return frameCount_;
107     }
IncreaseFrameCount()108     void IncreaseFrameCount()
109     {
110         frameCount_++;
111     }
112     bool GetWatermarkFlag() const;
113 
114     bool IsCurtainScreenOn() const;
115     bool IsColorFilterModeOn() const;
116     bool IsHighContrastTextModeOn() const;
117 
118     static void SetCaptureParam(const CaptureParam& param);
119     static CaptureParam& GetCaptureParam();
120     static void ResetCaptureParam();
121     static bool IsInCaptureProcess();
122     static bool IsEndNodeIdValid();
123     static bool IsExpandScreenMode();
GetDrawStatusVec()124     std::vector<NodeId>& GetDrawStatusVec()
125     {
126         return curDrawStatusVec_;
127     }
GetRSRenderThreadParams()128     const std::unique_ptr<RSRenderThreadParams>& GetRSRenderThreadParams() const
129     {
130         return RSRenderThreadParamsManager::Instance().GetRSRenderThreadParams();
131     }
132 
133     void RenderServiceTreeDump(std::string& dumpString);
134     void ReleaseSurface();
135     void AddToReleaseQueue(std::shared_ptr<Drawing::Surface>&& surface);
136 
IsMainLooping()137     bool IsMainLooping() const
138     {
139         return mainLooping_.load();
140     }
SetMainLooping(bool isMainLooping)141     void SetMainLooping(bool isMainLooping)
142     {
143         mainLooping_.store(isMainLooping);
144     }
145 
146     void UpdateScreenNodeScreenId();
147 
148     uint32_t GetDynamicRefreshRate() const;
GetTid()149     pid_t GetTid() const
150     {
151         return tid_;
152     }
153 
154     void SetAcquireFence(sptr<SyncFence> acquireFence);
155 
156     // vma cache
GetVmaOptimizeFlag()157     bool GetVmaOptimizeFlag() const
158     {
159         return vmaOptimizeFlag_; // global flag
160     }
161     void SetVmaCacheStatus(bool flag); // dynmic flag
162 
SetBlackList(const std::unordered_set<NodeId> & blackList)163     void SetBlackList(const std::unordered_set<NodeId>& blackList)
164     {
165         std::lock_guard<std::mutex> lock(nodeListMutex_);
166         blackList_ = blackList;
167     }
168 
SetTypeBlackList(const std::unordered_set<NodeType> & typeBlackList)169     void SetTypeBlackList(const std::unordered_set<NodeType>& typeBlackList)
170     {
171         std::lock_guard<std::mutex> lock(nodeListMutex_);
172         typeBlackList_ = typeBlackList;
173     }
174 
GetBlackList()175     const std::unordered_set<NodeId> GetBlackList() const
176     {
177         std::lock_guard<std::mutex> lock(nodeListMutex_);
178         return blackList_;
179     }
180 
GetTypeBlackList()181     const std::unordered_set<NodeType> GetTypeBlackList() const
182     {
183         std::lock_guard<std::mutex> lock(nodeListMutex_);
184         return typeBlackList_;
185     }
186 
SetWhiteList(const std::unordered_set<NodeId> & whiteList)187     void SetWhiteList(const std::unordered_set<NodeId>& whiteList)
188     {
189         std::lock_guard<std::mutex> lock(nodeListMutex_);
190         whiteList_ = whiteList;
191     }
192 
GetWhiteList()193     const std::unordered_set<NodeId> GetWhiteList() const
194     {
195         std::lock_guard<std::mutex> lock(nodeListMutex_);
196         return whiteList_;
197     }
198 
SetVisibleRect(const Drawing::RectI & visibleRect)199     void SetVisibleRect(const Drawing::RectI& visibleRect)
200     {
201         std::lock_guard<std::mutex> lock(nodeListMutex_);
202         visibleRect_ = visibleRect;
203     }
204 
GetVisibleRect()205     const Drawing::RectI& GetVisibleRect() const
206     {
207         std::lock_guard<std::mutex> lock(nodeListMutex_);
208         return visibleRect_;
209     }
210 
IsTaskQueueEmpty()211     bool IsTaskQueueEmpty() const
212     {
213         if (!handler_) {
214             return true;
215         }
216         return handler_->IsIdle();
217     }
218 
SetEnableVisibleRect(bool enableVisibleRect)219     void SetEnableVisibleRect(bool enableVisibleRect)
220     {
221         enableVisibleRect_.store(enableVisibleRect);
222     }
223 
GetEnableVisibleRect()224     bool GetEnableVisibleRect() const
225     {
226         return enableVisibleRect_.load();
227     }
228 
229     void DumpVkImageInfo(std::string &dumpString);
230 private:
231     RSUniRenderThread();
232     ~RSUniRenderThread() noexcept;
233     void Inittcache();
234     void PerfForBlurIfNeeded();
235     void PostReclaimMemoryTask(ClearMemoryMoment moment, bool isReclaim);
236     void CollectReleaseTasks(std::vector<std::function<void()>>& releaseTasks);
237 
238     bool screenNodeBufferReleased_ = false;
239     // Those variable is used to manage memory.
240     bool clearMemoryFinished_ = true;
241     bool clearMemDeeply_ = false;
242     DeviceType deviceType_ = DeviceType::PHONE;
243     bool isDefaultCleanTaskFinished_ = true;
244     bool hasPurgeShaderCacheTask_ = false;
245     bool postImageReleaseTaskFlag_ = false;
246     bool isReclaimMemoryFinished_ = true;
247     std::atomic<bool> isTimeToReclaim_ {false};
248     // vma cache
249     bool vmaOptimizeFlag_ = false; // enable/disable vma cache, global flag
250     // for statistic of jank frames
251     std::atomic_bool mainLooping_ = false;
252     std::atomic_bool enableVisibleRect_ = false;
253     pid_t tid_ = 0;
254     ClearMemoryMoment clearMoment_;
255     int imageReleaseCount_ = 0;
256     uint32_t vmaCacheCount_ = 0;
257     ScreenId screenNodeScreenId_ = 0;
258     std::atomic<uint64_t> frameCount_ = 0;
259     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
260     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
261 
262     std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_;
263     std::shared_ptr<RSContext> context_;
264     std::shared_ptr<DrawableV2::RSRenderNodeDrawable> rootNodeDrawable_;
265     sptr<SyncFence> acquireFence_ = SyncFence::InvalidFence();
266     std::vector<NodeId> curDrawStatusVec_;
267 
268     // used for blocking renderThread before screenNode has no freed buffer to request
269     mutable std::mutex screenNodeBufferReleasedMutex_;
270     // used for stalling renderThread before screenNode has no freed buffer to request
271     std::condition_variable screenNodeBufferReleasedCond_;
272 
273     std::mutex mutex_;
274     mutable std::mutex clearMemoryMutex_;
275     std::queue<std::shared_ptr<Drawing::Surface>> tmpSurfaces_;
276     static thread_local CaptureParam captureParam_;
277 
278     std::set<pid_t> exitedPidSet_;
279 
280     std::vector<Callback> imageReleaseTasks_;
281     std::mutex imageReleaseMutex_;
282 
283     mutable std::mutex nodeListMutex_;
284     std::unordered_set<NodeId> blackList_ = {};
285     std::unordered_set<NodeType> typeBlackList_ = {};
286     std::unordered_set<NodeId> whiteList_ = {};
287     Drawing::RectI visibleRect_;
288 
289     std::mutex vmaCacheCountMutex_;
290 
291 #ifdef RES_SCHED_ENABLE
292     void SubScribeSystemAbility();
293     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
294 #endif
295 };
296 } // namespace Rosen
297 } // namespace OHOS
298 #endif // RENDER_SERVICE_PIPELINE_RS_UNI_RENDER_THREAD_H
299