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