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 "pipeline/rs_base_render_engine.h"
26 #include "pipeline/rs_context.h"
27 #include "params/rs_render_thread_params.h"
28 #ifdef RES_SCHED_ENABLE
29 #include "vsync_system_ability_listener.h"
30 #endif
31 
32 namespace OHOS {
33 namespace Rosen {
34 namespace DrawableV2 {
35 class RSRenderNodeDrawable;
36 class RSDisplayRenderNodeDrawable;
37 }
38 
39 class RSUniRenderThread {
40 public:
41     using Callback = std::function<void()>;
42     static RSUniRenderThread& Instance();
43 
44     // disable copy and move
45     RSUniRenderThread(const RSUniRenderThread&) = delete;
46     RSUniRenderThread& operator=(const RSUniRenderThread&) = delete;
47     RSUniRenderThread(RSUniRenderThread&&) = delete;
48     RSUniRenderThread& operator=(RSUniRenderThread&&) = delete;
49 
50     void Start();
51     void InitGrContext();
52     void RenderFrames();
53     void Sync(std::unique_ptr<RSRenderThreadParams>&& stagingRenderThreadParams);
54     void PostTask(const std::function<void()>& task);
55     void RemoveTask(const std::string& name);
56     void PostRTTask(const std::function<void()>& task);
57     void PostImageReleaseTask(const std::function<void()>& task);
58     void RunImageReleaseTask();
59     void ClearResource();
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 
77     void PurgeCacheBetweenFrames();
78     void ClearMemoryCache(ClearMemoryMoment moment, bool deeply, pid_t pid = -1);
79     void DefaultClearMemoryCache();
80     void PostClearMemoryTask(ClearMemoryMoment moment, bool deeply, bool isDefaultClean);
81     void MemoryManagementBetweenFrames();
82     void FlushGpuMemoryInWaitQueueBetweenFrames();
83     void SuppressGpuCacheBelowCertainRatioBetweenFrames();
84     void ResetClearMemoryTask(const std::unordered_map<NodeId, bool>&& ids, bool isDoDirectComposition = false);
85     void SetDefaultClearMemoryFinished(bool isFinished);
86     bool IsDefaultClearMemoryFinished();
87     bool GetClearMemoryFinished() const;
88     void SetClearMemoryFinished();
89     bool GetClearMemDeeply() const;
90     void SetClearMoment(ClearMemoryMoment moment);
91     ClearMemoryMoment GetClearMoment() const;
92     uint32_t GetRefreshRate() const;
93     void DumpMem(DfxString& log);
94     void TrimMem(std::string& dumpString, std::string& type);
95     std::shared_ptr<Drawing::Image> GetWatermarkImg();
GetFrameCount()96     uint64_t GetFrameCount() const
97     {
98         return frameCount_;
99     }
IncreaseFrameCount()100     void IncreaseFrameCount()
101     {
102         frameCount_++;
103     }
104     bool GetWatermarkFlag() const;
105 
106     bool IsCurtainScreenOn() const;
107     bool IsColorFilterModeOn() const;
108     bool IsHighContrastTextModeOn() const;
109 
110     static void SetCaptureParam(const CaptureParam& param);
111     static CaptureParam& GetCaptureParam();
112     static void ResetCaptureParam();
113     static bool IsInCaptureProcess();
GetDrawStatusVec()114     std::vector<NodeId>& GetDrawStatusVec()
115     {
116         return curDrawStatusVec_;
117     }
GetRSRenderThreadParams()118     const std::unique_ptr<RSRenderThreadParams>& GetRSRenderThreadParams() const
119     {
120         return RSRenderThreadParamsManager::Instance().GetRSRenderThreadParams();
121     }
122 
123     void RenderServiceTreeDump(std::string& dumpString);
124     void ReleaseSurface();
125     void AddToReleaseQueue(std::shared_ptr<Drawing::Surface>&& surface);
126 
IsMainLooping()127     bool IsMainLooping() const
128     {
129         return mainLooping_.load();
130     }
SetMainLooping(bool isMainLooping)131     void SetMainLooping(bool isMainLooping)
132     {
133         mainLooping_.store(isMainLooping);
134     }
GetDiscardJankFrames()135     bool GetDiscardJankFrames() const
136     {
137         return discardJankFrames_.load();
138     }
SetDiscardJankFrames(bool discardJankFrames)139     void SetDiscardJankFrames(bool discardJankFrames)
140     {
141         if (discardJankFrames_.load() != discardJankFrames) {
142             discardJankFrames_.store(discardJankFrames);
143         }
144     }
GetSkipJankAnimatorFrame()145     bool GetSkipJankAnimatorFrame() const
146     {
147         return skipJankAnimatorFrame_.load();
148     }
SetSkipJankAnimatorFrame(bool skipJankAnimatorFrame)149     void SetSkipJankAnimatorFrame(bool skipJankAnimatorFrame)
150     {
151         skipJankAnimatorFrame_.store(skipJankAnimatorFrame);
152     }
153     void UpdateDisplayNodeScreenId();
154     uint32_t GetDynamicRefreshRate() const;
GetTid()155     pid_t GetTid() const
156     {
157         return tid_;
158     }
159 
160     void SetAcquireFence(sptr<SyncFence> acquireFence);
161 
162     // vma cache
GetVmaOptimizeFlag()163     bool GetVmaOptimizeFlag() const
164     {
165         return vmaOptimizeFlag_; // global flag
166     }
167     void SetVmaCacheStatus(bool flag); // dynmic flag
168 
SetBlackList(std::unordered_set<NodeId> blackList)169     void SetBlackList(std::unordered_set<NodeId> blackList)
170     {
171         std::lock_guard<std::mutex> lock(nodeListMutex_);
172         blackList_ = blackList;
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 
SetWhiteList(const std::unordered_set<NodeId> & whiteList)181     void SetWhiteList(const std::unordered_set<NodeId>& whiteList)
182     {
183         std::lock_guard<std::mutex> lock(nodeListMutex_);
184         whiteList_ = whiteList;
185     }
186 
GetWhiteList()187     const std::unordered_set<NodeId> GetWhiteList() const
188     {
189         std::lock_guard<std::mutex> lock(nodeListMutex_);
190         return whiteList_;
191     }
192 
193 private:
194     RSUniRenderThread();
195     ~RSUniRenderThread() noexcept;
196     void Inittcache();
197     void PerfForBlurIfNeeded();
198 
199     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
200     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
201 
202     std::shared_ptr<RSBaseRenderEngine> uniRenderEngine_;
203     std::shared_ptr<RSContext> context_;
204     std::shared_ptr<DrawableV2::RSRenderNodeDrawable> rootNodeDrawable_;
205     std::vector<NodeId> curDrawStatusVec_;
206 
207     // used for blocking renderThread before displayNode has no freed buffer to request
208     mutable std::mutex displayNodeBufferReleasedMutex_;
209     bool displayNodeBufferReleased_ = false;
210     // used for stalling renderThread before displayNode has no freed buffer to request
211     std::condition_variable displayNodeBufferReleasedCond_;
212 
213     // Those variable is used to manage memory.
214     bool clearMemoryFinished_ = true;
215     bool clearMemDeeply_ = false;
216     std::unordered_set<NodeId> nodesNeedToBeClearMemory_;
217     DeviceType deviceType_ = DeviceType::PHONE;
218     std::mutex mutex_;
219     mutable std::mutex clearMemoryMutex_;
220     std::queue<std::shared_ptr<Drawing::Surface>> tmpSurfaces_;
221     static thread_local CaptureParam captureParam_;
222     std::atomic<uint64_t> frameCount_ = 0;
223 
224     pid_t tid_ = 0;
225 
226     // for statistic of jank frames
227     std::atomic_bool mainLooping_ = false;
228     std::atomic_bool discardJankFrames_ = false;
229     std::atomic_bool skipJankAnimatorFrame_ = false;
230     ScreenId displayNodeScreenId_ = 0;
231     std::set<pid_t> exitedPidSet_;
232     ClearMemoryMoment clearMoment_ = ClearMemoryMoment::NO_CLEAR;
233     bool isDefaultCleanTaskFinished_ = true;
234 
235     std::vector<Callback> imageReleaseTasks_;
236     std::mutex imageReleaseMutex_;
237     bool postImageReleaseTaskFlag_ = false;
238     int imageReleaseCount_ = 0;
239 
240     mutable std::mutex nodeListMutex_;
241     std::unordered_set<NodeId> blackList_ = {};
242     std::unordered_set<NodeId> whiteList_ = {};
243 
244     sptr<SyncFence> acquireFence_ = SyncFence::InvalidFence();
245 
246     // vma cache
247     bool vmaOptimizeFlag_ = false; // enable/disable vma cache, global flag
248     uint32_t vmaCacheCount_ = 0;
249     std::mutex vmaCacheCountMutex_;
250 #ifdef RES_SCHED_ENABLE
251     void SubScribeSystemAbility();
252     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
253 #endif
254 };
255 } // namespace Rosen
256 } // namespace OHOS
257 #endif // RENDER_SERVICE_PIPELINE_RS_UNI_RENDER_THREAD_H
258