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 #ifdef NEW_RENDER_CONTEXT 34 #include "render_backend/render_context_factory.h" 35 #include "drawing_context.h" 36 #else 37 #include "render_context/render_context.h" 38 #endif 39 #include "transaction/rs_transaction_proxy.h" 40 #include "vsync_receiver.h" 41 42 namespace OHOS { 43 namespace Rosen { 44 class HighContrastObserver; 45 class RSRenderThread final { 46 public: 47 static RSRenderThread& Instance(); 48 49 void Start(); 50 void Stop(); 51 52 void Detach(NodeId id); 53 void RecvTransactionData(std::unique_ptr<RSTransactionData>& transactionData); 54 void RequestNextVSync(); 55 void PostTask(RSTaskMessage::RSTask task); 56 void PostSyncTask(RSTaskMessage::RSTask task); 57 void PostPreTask(); 58 void UpdateWindowStatus(bool active); 59 60 int32_t GetTid(); 61 62 std::string DumpRenderTree() const; 63 #ifdef NEW_RENDER_CONTEXT GetRenderContext()64 std::shared_ptr<RenderContextBase> GetRenderContext() const 65 { 66 return renderContext_; 67 } GetDrawingContext()68 std::shared_ptr<DrawingContext> GetDrawingContext() const 69 { 70 return drawingContext_; 71 } 72 #else GetRenderContext()73 RenderContext* GetRenderContext() 74 { 75 return renderContext_; 76 } 77 #endif GetContext()78 RSContext& GetContext() 79 { 80 return *context_; 81 } GetContext()82 const RSContext& GetContext() const 83 { 84 return *context_; 85 } GetUITimestamp()86 uint64_t GetUITimestamp() const 87 { 88 return uiTimestamp_; 89 } SetHighContrast(bool enabled)90 void SetHighContrast(bool enabled) 91 { 92 isHighContrastEnabled_ = enabled; 93 isHighContrastChanged_ = true; 94 } IsHighContrastEnabled()95 bool IsHighContrastEnabled() const 96 { 97 return isHighContrastEnabled_; 98 } IsHighContrastChanged()99 bool IsHighContrastChanged() const 100 { 101 return isHighContrastChanged_; 102 } ResetHighContrastChanged()103 void ResetHighContrastChanged() 104 { 105 isHighContrastChanged_ = false; 106 } SetCacheDir(const std::string & filePath)107 void SetCacheDir(const std::string& filePath) 108 { 109 cacheDir_ = filePath; 110 } 111 112 // If disabled partial render, rt forces to render whole frame SetRTRenderForced(bool isRenderForced)113 void SetRTRenderForced(bool isRenderForced) 114 { 115 if ((isRTRenderForced_ != isRenderForced)) { 116 isRTRenderForced_ = isRenderForced; 117 } 118 } 119 AddSurfaceChangedCallBack(uint64_t id,const std::function<void (float,float,float,float)> & callback)120 void AddSurfaceChangedCallBack(uint64_t id, 121 const std::function<void(float, float, float, float)>& callback) 122 { 123 if (visitor_ == nullptr) { 124 visitor_ = std::make_shared<RSRenderThreadVisitor>(); 125 } 126 visitor_->AddSurfaceChangedCallBack(id, callback); 127 } 128 RemoveSurfaceChangedCallBack(uint64_t id)129 void RemoveSurfaceChangedCallBack(uint64_t id) 130 { 131 if (visitor_) { 132 visitor_->RemoveSurfaceChangedCallBack(id); 133 } 134 } 135 136 private: 137 RSRenderThread(); 138 ~RSRenderThread(); 139 140 RSRenderThread(const RSRenderThread&) = delete; 141 RSRenderThread(const RSRenderThread&&) = delete; 142 RSRenderThread& operator=(const RSRenderThread&) = delete; 143 RSRenderThread& operator=(const RSRenderThread&&) = delete; 144 145 void RenderLoop(); 146 void CreateAndInitRenderContextIfNeed(); 147 148 void OnVsync(uint64_t timestamp, int64_t frameCount); 149 void ProcessCommands(); 150 void Animate(uint64_t timestamp); 151 void Render(); 152 void SendCommands(); 153 void ReleasePixelMapInBackgroundThread(); 154 155 std::atomic_bool running_ = false; 156 std::atomic_bool hasSkipVsync_ = false; 157 std::atomic_int activeWindowCnt_ = 0; 158 std::unique_ptr<std::thread> thread_ = nullptr; 159 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 160 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 161 std::shared_ptr<VSyncReceiver> receiver_ = nullptr; 162 RSTaskMessage::RSTask preTask_ = nullptr; 163 RSTaskMessage::RSTask mainFunc_; 164 165 std::mutex mutex_; 166 std::mutex cmdMutex_; 167 std::mutex rtMutex_; 168 std::vector<std::unique_ptr<RSTransactionData>> cmds_; 169 bool hasRunningAnimation_ = false; 170 std::shared_ptr<RSRenderThreadVisitor> visitor_; 171 172 uint64_t timestamp_ = 0; 173 uint64_t prevTimestamp_ = 0; 174 uint64_t lastAnimateTimestamp_ = 0; 175 int32_t tid_ = -1; 176 uint64_t mValue = 0; 177 178 uint64_t uiTimestamp_ = 0; 179 uint64_t commandTimestamp_ = 0; 180 181 // for overdraw 182 bool isOverDrawEnabledOfCurFrame_ = false; 183 bool isOverDrawEnabledOfLastFrame_ = false; 184 185 // for jank frame detector 186 std::shared_ptr<RSJankDetector> jankDetector_; 187 188 std::shared_ptr<RSContext> context_; 189 190 #ifdef NEW_RENDER_CONTEXT 191 std::shared_ptr<RenderContextBase> renderContext_; 192 std::shared_ptr<Rosen::DrawingContext> drawingContext_; 193 #else 194 RenderContext* renderContext_ = nullptr; 195 #endif 196 std::shared_ptr<HighContrastObserver> highContrastObserver_; 197 std::atomic_bool isHighContrastEnabled_ = false; 198 std::atomic_bool isHighContrastChanged_ = false; 199 200 std::string cacheDir_; 201 bool isRTRenderForced_ = false; 202 #ifdef ROSEN_PREVIEW 203 std::atomic_bool isRunning_ = false; 204 #endif 205 }; 206 } // namespace Rosen 207 } // namespace OHOS 208 209 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H 210