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 } isHighContrastEnabled()94 bool isHighContrastEnabled() const 95 { 96 return isHighContrastEnabled_; 97 } SetCacheDir(const std::string & filePath)98 void SetCacheDir(const std::string& filePath) 99 { 100 cacheDir_ = filePath; 101 } 102 103 // If disabled partial render, rt forces to render whole frame SetRTRenderForced(bool isRenderForced)104 void SetRTRenderForced(bool isRenderForced) 105 { 106 if ((isRTRenderForced_ != isRenderForced)) { 107 isRTRenderForced_ = isRenderForced; 108 } 109 } 110 AddSurfaceChangedCallBack(uint64_t id,const std::function<void (float,float,float,float)> & callback)111 void AddSurfaceChangedCallBack(uint64_t id, 112 const std::function<void(float, float, float, float)>& callback) 113 { 114 if (visitor_ == nullptr) { 115 visitor_ = std::make_shared<RSRenderThreadVisitor>(); 116 } 117 visitor_->AddSurfaceChangedCallBack(id, callback); 118 } 119 RemoveSurfaceChangedCallBack(uint64_t id)120 void RemoveSurfaceChangedCallBack(uint64_t id) 121 { 122 if (visitor_) { 123 visitor_->RemoveSurfaceChangedCallBack(id); 124 } 125 } 126 127 private: 128 RSRenderThread(); 129 ~RSRenderThread(); 130 131 RSRenderThread(const RSRenderThread&) = delete; 132 RSRenderThread(const RSRenderThread&&) = delete; 133 RSRenderThread& operator=(const RSRenderThread&) = delete; 134 RSRenderThread& operator=(const RSRenderThread&&) = delete; 135 136 void RenderLoop(); 137 void CreateAndInitRenderContextIfNeed(); 138 139 void OnVsync(uint64_t timestamp, int64_t frameCount); 140 void ProcessCommands(); 141 void Animate(uint64_t timestamp); 142 void Render(); 143 void SendCommands(); 144 void ReleasePixelMapInBackgroundThread(); 145 146 std::atomic_bool running_ = false; 147 std::atomic_bool hasSkipVsync_ = false; 148 std::atomic_int activeWindowCnt_ = 0; 149 std::unique_ptr<std::thread> thread_ = nullptr; 150 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 151 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 152 std::shared_ptr<VSyncReceiver> receiver_ = nullptr; 153 RSTaskMessage::RSTask preTask_ = nullptr; 154 RSTaskMessage::RSTask mainFunc_; 155 156 std::mutex mutex_; 157 std::mutex cmdMutex_; 158 std::mutex rtMutex_; 159 std::vector<std::unique_ptr<RSTransactionData>> cmds_; 160 bool hasRunningAnimation_ = false; 161 std::shared_ptr<RSRenderThreadVisitor> visitor_; 162 163 uint64_t timestamp_ = 0; 164 uint64_t prevTimestamp_ = 0; 165 uint64_t lastAnimateTimestamp_ = 0; 166 int32_t tid_ = -1; 167 uint64_t mValue = 0; 168 169 uint64_t uiTimestamp_ = 0; 170 uint64_t commandTimestamp_ = 0; 171 172 // for jank frame detector 173 std::shared_ptr<RSJankDetector> jankDetector_; 174 175 std::shared_ptr<RSContext> context_; 176 177 #ifdef NEW_RENDER_CONTEXT 178 std::shared_ptr<RenderContextBase> renderContext_; 179 std::shared_ptr<Rosen::DrawingContext> drawingContext_; 180 #else 181 RenderContext* renderContext_ = nullptr; 182 #endif 183 std::shared_ptr<HighContrastObserver> highContrastObserver_; 184 std::atomic_bool isHighContrastEnabled_ = false; 185 186 std::string cacheDir_; 187 bool isRTRenderForced_ = false; 188 #ifdef ROSEN_PREVIEW 189 std::atomic_bool isRunning_ = false; 190 #endif 191 }; 192 } // namespace Rosen 193 } // namespace OHOS 194 195 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H 196