1 /* 2 * Copyright (c) 2021-2022 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 #ifndef RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H 16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H 17 18 #include <atomic> 19 #include <condition_variable> 20 #include <functional> 21 #include <mutex> 22 #include <thread> 23 #include <unordered_map> 24 #include <vector> 25 #include <event_handler.h> 26 27 #include "common/rs_thread_handler.h" 28 #include "common/rs_thread_looper.h" 29 #include "jank_detector/rs_jank_detector.h" 30 #include "pipeline/rs_canvas_render_node.h" 31 #include "pipeline/rs_render_thread_visitor.h" 32 #include "platform/drawing/rs_vsync_client.h" 33 #include "render_context/render_context.h" 34 #include "transaction/rs_transaction_proxy.h" 35 #include "vsync_receiver.h" 36 37 namespace OHOS { 38 namespace Rosen { 39 class HighContrastObserver; 40 class RSRenderThread final { 41 public: 42 static RSRenderThread& Instance(); 43 44 void Start(); 45 void Stop(); 46 47 void Detach(NodeId id); 48 void RecvTransactionData(std::unique_ptr<RSTransactionData>& transactionData); 49 void RequestNextVSync(); 50 void PostTask(RSTaskMessage::RSTask task); 51 void PostPreTask(); 52 void UpdateWindowStatus(bool active); 53 54 int32_t GetTid(); 55 56 std::string DumpRenderTree() const; 57 GetRenderContext()58 RenderContext* GetRenderContext() 59 { 60 return renderContext_; 61 } 62 GetContext()63 RSContext& GetContext() 64 { 65 return *context_; 66 } GetContext()67 const RSContext& GetContext() const 68 { 69 return *context_; 70 } GetUITimestamp()71 uint64_t GetUITimestamp() const 72 { 73 return uiTimestamp_; 74 } SetHighContrast(bool enabled)75 void SetHighContrast(bool enabled) 76 { 77 isHighContrastEnabled_ = enabled; 78 } isHighContrastEnabled()79 bool isHighContrastEnabled() const 80 { 81 return isHighContrastEnabled_; 82 } 83 void UpdateRenderMode(bool needRender); 84 void NotifyClearBufferCache(); 85 SetCacheDir(const std::string & filePath)86 void SetCacheDir(const std::string& filePath) 87 { 88 cacheDir_ = filePath; 89 } 90 91 // If disabled partial render, rt forces to render whole frame SetRTRenderForced(bool isRenderForced)92 void SetRTRenderForced(bool isRenderForced) 93 { 94 if ((isRTRenderForced_ != isRenderForced)) { 95 ROSEN_LOGD("RSRenderThread::SetRenderForced %d -> %d", isRTRenderForced_, isRenderForced); 96 isRTRenderForced_ = isRenderForced; 97 } 98 } 99 100 private: 101 RSRenderThread(); 102 ~RSRenderThread(); 103 104 RSRenderThread(const RSRenderThread&) = delete; 105 RSRenderThread(const RSRenderThread&&) = delete; 106 RSRenderThread& operator=(const RSRenderThread&) = delete; 107 RSRenderThread& operator=(const RSRenderThread&&) = delete; 108 109 void RenderLoop(); 110 void CreateAndInitRenderContextIfNeed(); 111 112 void OnVsync(uint64_t timestamp); 113 void ProcessCommands(); 114 void Animate(uint64_t timestamp); 115 void Render(); 116 void SendCommands(); 117 118 void UpdateSurfaceNodeParentInRS(); 119 void ClearBufferCache(); 120 void MarkNeedUpdateSurfaceNode(); 121 std::atomic_bool running_ = false; 122 std::atomic_bool hasSkipVsync_ = false; 123 bool needRender_ = true; 124 std::atomic_int activeWindowCnt_ = 0; 125 std::unique_ptr<std::thread> thread_ = nullptr; 126 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 127 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 128 std::shared_ptr<VSyncReceiver> receiver_ = nullptr; 129 RSTaskMessage::RSTask preTask_ = nullptr; 130 RSTaskMessage::RSTask mainFunc_; 131 132 std::mutex mutex_; 133 std::mutex cmdMutex_; 134 std::vector<std::unique_ptr<RSTransactionData>> cmds_; 135 bool hasRunningAnimation_ = false; 136 std::shared_ptr<RSRenderThreadVisitor> visitor_; 137 138 uint64_t timestamp_ = 0; 139 uint64_t prevTimestamp_ = 0; 140 uint64_t lastAnimateTimestamp_ = 0; 141 int32_t tid_ = -1; 142 uint64_t mValue = 0; 143 144 uint64_t uiTimestamp_ = 0; 145 uint64_t commandTimestamp_ = 0; 146 147 // for jank frame detector 148 std::shared_ptr<RSJankDetector> jankDetector_; 149 150 std::shared_ptr<RSContext> context_; 151 152 RenderContext* renderContext_ = nullptr; 153 std::shared_ptr<HighContrastObserver> highContrastObserver_; 154 std::atomic_bool isHighContrastEnabled_ = false; 155 156 std::string cacheDir_; 157 bool isRTRenderForced_ = false; 158 }; 159 } // namespace Rosen 160 } // namespace OHOS 161 162 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H 163