• 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 RSDisplayRenderNodeDrawable;
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 PostTask(RSTaskMessage::RSTask task, const std::string& name, int64_t delayTime,
61         AppExecFwk::EventQueue::Priority priority = AppExecFwk::EventQueue::Priority::HIGH);
62     void PostSyncTask(const std::function<void()>& task);
63     bool IsIdle() const;
64     void Render();
65     void ReleaseSelfDrawingNodeBuffer();
66     std::shared_ptr<RSBaseRenderEngine> GetRenderEngine() const;
67     void NotifyDisplayNodeBufferReleased();
68     bool WaitUntilDisplayNodeBufferReleased(DrawableV2::RSDisplayRenderNodeDrawable& displayNodeDrawable);
69 
70     uint64_t GetCurrentTimestamp() const;
71     int64_t GetActualTimestamp() const;
72     uint64_t GetVsyncId() const;
73     bool GetForceRefreshFlag() const;
74     uint32_t GetPendingScreenRefreshRate() const;
75     uint64_t GetPendingConstraintRelativeTime() const;
76     uint64_t GetFastComposeTimeStampDiff() const;
77 
78     void PurgeCacheBetweenFrames();
79     void ClearMemoryCache(ClearMemoryMoment moment, bool deeply, pid_t pid = -1);
80     void DefaultClearMemoryCache();
81     void ReclaimMemory();
82     void PostClearMemoryTask(ClearMemoryMoment moment, bool deeply, bool isDefaultClean);
83     void MemoryManagementBetweenFrames();
84     void FlushGpuMemoryInWaitQueueBetweenFrames();
85     void SuppressGpuCacheBelowCertainRatioBetweenFrames();
86     void ResetClearMemoryTask(bool isDoDirectComposition = false);
87     void PurgeShaderCacheAfterAnimate();
88     void SetReclaimMemoryFinished(bool isFinished);
89     bool IsReclaimMemoryFinished();
90     void SetTimeToReclaim(bool isTimeToReclaim);
91     bool IsTimeToReclaim();
92     void SetDefaultClearMemoryFinished(bool isFinished);
93     bool IsDefaultClearMemroyFinished();
94     bool GetClearMemoryFinished() const;
95     bool GetClearMemDeeply() const;
96     void SetClearMoment(ClearMemoryMoment moment);
97     ClearMemoryMoment GetClearMoment() const;
98     uint32_t GetRefreshRate() const;
99     void DumpMem(DfxString& log);
100     void TrimMem(std::string& dumpString, std::string& type);
101     std::shared_ptr<Drawing::Image> GetWatermarkImg();
GetFrameCount()102     uint64_t GetFrameCount() const
103     {
104         return frameCount_;
105     }
IncreaseFrameCount()106     void IncreaseFrameCount()
107     {
108         frameCount_++;
109     }
110     bool GetWatermarkFlag() const;
111 
112     bool IsCurtainScreenOn() const;
113     bool IsColorFilterModeOn() const;
114     bool IsHighContrastTextModeOn() const;
115 
116     static void SetCaptureParam(const CaptureParam& param);
117     static CaptureParam& GetCaptureParam();
118     static void ResetCaptureParam();
119     static bool IsInCaptureProcess();
120     static bool IsExpandScreenMode();
GetDrawStatusVec()121     std::vector<NodeId>& GetDrawStatusVec()
122     {
123         return curDrawStatusVec_;
124     }
GetRSRenderThreadParams()125     const std::unique_ptr<RSRenderThreadParams>& GetRSRenderThreadParams() const
126     {
127         return RSRenderThreadParamsManager::Instance().GetRSRenderThreadParams();
128     }
129 
130     void RenderServiceTreeDump(std::string& dumpString);
131     void ReleaseSurface();
132     void AddToReleaseQueue(std::shared_ptr<Drawing::Surface>&& surface);
133 
IsMainLooping()134     bool IsMainLooping() const
135     {
136         return mainLooping_.load();
137     }
SetMainLooping(bool isMainLooping)138     void SetMainLooping(bool isMainLooping)
139     {
140         mainLooping_.store(isMainLooping);
141     }
GetDiscardJankFrames()142     bool GetDiscardJankFrames() const
143     {
144         return discardJankFrames_.load();
145     }
SetDiscardJankFrames(bool discardJankFrames)146     void SetDiscardJankFrames(bool discardJankFrames)
147     {
148         if (discardJankFrames_.load() != discardJankFrames) {
149             discardJankFrames_.store(discardJankFrames);
150         }
151     }
GetSkipJankAnimatorFrame()152     bool GetSkipJankAnimatorFrame() const
153     {
154         return skipJankAnimatorFrame_.load();
155     }
SetSkipJankAnimatorFrame(bool skipJankAnimatorFrame)156     void SetSkipJankAnimatorFrame(bool skipJankAnimatorFrame)
157     {
158         skipJankAnimatorFrame_.store(skipJankAnimatorFrame);
159     }
160     void UpdateDisplayNodeScreenId();
161     uint32_t GetDynamicRefreshRate() const;
GetTid()162     pid_t GetTid() const
163     {
164         return tid_;
165     }
166 
167     void SetAcquireFence(sptr<SyncFence> acquireFence);
168 
169     // vma cache
GetVmaOptimizeFlag()170     bool GetVmaOptimizeFlag() const
171     {
172         return vmaOptimizeFlag_; // global flag
173     }
174     void SetVmaCacheStatus(bool flag); // dynmic flag
175 
SetBlackList(const std::unordered_set<NodeId> & blackList)176     void SetBlackList(const std::unordered_set<NodeId>& blackList)
177     {
178         std::lock_guard<std::mutex> lock(nodeListMutex_);
179         blackList_ = blackList;
180     }
181 
GetBlackList()182     const std::unordered_set<NodeId> GetBlackList() const
183     {
184         std::lock_guard<std::mutex> lock(nodeListMutex_);
185         return blackList_;
186     }
187 
SetWhiteList(const std::unordered_set<NodeId> & whiteList)188     void SetWhiteList(const std::unordered_set<NodeId>& whiteList)
189     {
190         std::lock_guard<std::mutex> lock(nodeListMutex_);
191         whiteList_ = whiteList;
192     }
193 
GetWhiteList()194     const std::unordered_set<NodeId> GetWhiteList() const
195     {
196         std::lock_guard<std::mutex> lock(nodeListMutex_);
197         return whiteList_;
198     }
199 
SetVisibleRect(const Drawing::RectI & visibleRect)200     void SetVisibleRect(const Drawing::RectI& visibleRect)
201     {
202         std::lock_guard<std::mutex> lock(nodeListMutex_);
203         visibleRect_ = visibleRect;
204     }
205 
GetVisibleRect()206     const Drawing::RectI& GetVisibleRect() const
207     {
208         std::lock_guard<std::mutex> lock(nodeListMutex_);
209         return visibleRect_;
210     }
211 
SetWallpaperTranslate(int32_t translateX,int32_t translateY)212     void SetWallpaperTranslate(int32_t translateX, int32_t translateY)
213     {
214         std::lock_guard<std::mutex> lock(wallpaperTranslateMutex_);
215         wallpaperTranslate_ = {translateX, translateY};
216     }
217 
GetWallpaperTranslate()218     std::pair<int32_t, int32_t> GetWallpaperTranslate() const
219     {
220         std::lock_guard<std::mutex> lock(wallpaperTranslateMutex_);
221         return wallpaperTranslate_;
222     }
223 
SetEnableVisiableRect(bool enableVisiableRect)224     void SetEnableVisiableRect(bool enableVisiableRect)
225     {
226         enableVisiableRect_.store(enableVisiableRect);
227     }
228 
GetEnableVisiableRect()229     bool GetEnableVisiableRect() const
230     {
231         return enableVisiableRect_.load();
232     }
233 
234     void DumpVkImageInfo(std::string &dumpString);
235 private:
236     RSUniRenderThread();
237     ~RSUniRenderThread() noexcept;
238     void Inittcache();
239     void PerfForBlurIfNeeded();
240     void PostReclaimMemoryTask(ClearMemoryMoment moment, bool isReclaim);
241     void RSUniRenderGfxDumpInit();
242 
243     bool displayNodeBufferReleased_ = false;
244     // Those variable is used to manage memory.
245     bool clearMemoryFinished_ = true;
246     bool clearMemDeeply_ = false;
247     DeviceType deviceType_ = DeviceType::PHONE;
248     bool isDefaultCleanTaskFinished_ = true;
249     bool hasPurgeShaderCacheTask_ = false;
250     bool postImageReleaseTaskFlag_ = false;
251     bool isReclaimMemoryFinished_ = true;
252     std::atomic<bool> isTimeToReclaim_ {false};
253     // vma cache
254     bool vmaOptimizeFlag_ = false; // enable/disable vma cache, global flag
255     // for statistic of jank frames
256     std::atomic_bool mainLooping_ = false;
257     std::atomic_bool discardJankFrames_ = false;
258     std::atomic_bool skipJankAnimatorFrame_ = false;
259     std::atomic_bool enableVisiableRect_ = false;
260     pid_t tid_ = 0;
261     ClearMemoryMoment clearMoment_;
262     int imageReleaseCount_ = 0;
263     uint32_t vmaCacheCount_ = 0;
264     ScreenId displayNodeScreenId_ = 0;
265     std::atomic<uint64_t> frameCount_ = 0;
266     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
267     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
268 
269     std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_;
270     std::shared_ptr<RSContext> context_;
271     std::shared_ptr<DrawableV2::RSRenderNodeDrawable> rootNodeDrawable_;
272     sptr<SyncFence> acquireFence_ = SyncFence::InvalidFence();
273     std::vector<NodeId> curDrawStatusVec_;
274 
275     // used for blocking renderThread before displayNode has no freed buffer to request
276     mutable std::mutex displayNodeBufferReleasedMutex_;
277     // used for stalling renderThread before displayNode has no freed buffer to request
278     std::condition_variable displayNodeBufferReleasedCond_;
279 
280     std::mutex mutex_;
281     mutable std::mutex clearMemoryMutex_;
282     std::queue<std::shared_ptr<Drawing::Surface>> tmpSurfaces_;
283     static thread_local CaptureParam captureParam_;
284 
285     std::set<pid_t> exitedPidSet_;
286 
287     std::vector<Callback> imageReleaseTasks_;
288     std::mutex imageReleaseMutex_;
289 
290     mutable std::mutex nodeListMutex_;
291     std::unordered_set<NodeId> blackList_ = {};
292     std::unordered_set<NodeId> whiteList_ = {};
293     Drawing::RectI visibleRect_;
294 
295     std::mutex vmaCacheCountMutex_;
296 
297     std::pair<int32_t, int32_t> wallpaperTranslate_ = {0, 0};
298     mutable std::mutex wallpaperTranslateMutex_;
299 #ifdef RES_SCHED_ENABLE
300     void SubScribeSystemAbility();
301     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
302 #endif
303 };
304 } // namespace Rosen
305 } // namespace OHOS
306 #endif // RENDER_SERVICE_PIPELINE_RS_UNI_RENDER_THREAD_H
307