1 /* 2 * Copyright (c) 2021 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 26 #include "common/rs_thread_handler.h" 27 #include "common/rs_thread_looper.h" 28 #include "ipc_callbacks/rs_application_render_thread_stub.h" 29 #include "pipeline/rs_canvas_render_node.h" 30 #include "pipeline/rs_render_thread_visitor.h" 31 #include "platform/drawing/rs_vsync_client.h" 32 #include "render_context/render_context.h" 33 #include "transaction/rs_transaction_proxy.h" 34 35 namespace OHOS { 36 namespace Rosen { 37 class RSRenderThread final : public RSApplicationRenderThreadStub { 38 public: 39 static RSRenderThread& Instance(); 40 41 void Start(); 42 void Stop(); 43 void WakeUp(); 44 45 void Detach(NodeId id); 46 void RecvTransactionData(std::unique_ptr<RSTransactionData>& transactionData); 47 void RequestNextVSync(); 48 void PostTask(RSTaskMessage::RSTask task); 49 void UpdateWindowStatus(bool active); 50 51 int32_t GetTid(); 52 53 std::string DumpRenderTree() const; 54 GetRenderContext()55 RenderContext* GetRenderContext() 56 { 57 return renderContext_; 58 } 59 GetContext()60 RSContext& GetContext() 61 { 62 return context_; 63 } GetContext()64 const RSContext& GetContext() const 65 { 66 return context_; 67 } 68 69 void OnTransaction(std::shared_ptr<RSTransactionData> transactionData) override; 70 71 private: 72 RSRenderThread(); 73 ~RSRenderThread(); 74 75 RSRenderThread(const RSRenderThread&) = delete; 76 RSRenderThread(const RSRenderThread&&) = delete; 77 RSRenderThread& operator=(const RSRenderThread&) = delete; 78 RSRenderThread& operator=(const RSRenderThread&&) = delete; 79 80 void RenderLoop(); 81 82 void OnVsync(uint64_t timestamp); 83 void StartTimer(uint64_t interval); 84 void StopTimer(); 85 void ProcessCommands(); 86 void Animate(uint64_t timestamp); 87 void Render(); 88 void SendCommands(); 89 90 std::atomic_bool running_ = false; 91 std::atomic_int activeWindowCnt_ = 0; 92 std::unique_ptr<std::thread> thread_ = nullptr; 93 std::unique_ptr<RSThreadLooper> rendererLooper_ = nullptr; 94 std::unique_ptr<RSThreadHandler> threadHandler_ = nullptr; 95 RSTaskHandle preTask_ = nullptr; 96 RSTaskHandle timeHandle_ = nullptr; 97 RSTaskMessage::RSTask mainFunc_; 98 99 std::mutex mutex_; 100 std::mutex cmdMutex_; 101 std::vector<std::unique_ptr<RSTransactionData>> cmds_; 102 bool hasRunningAnimation_ = false; 103 std::shared_ptr<RSNodeVisitor> visitor_; 104 105 std::unique_ptr<RSVsyncClient> vsyncClient_ = nullptr; 106 uint64_t timestamp_ = 0; 107 uint64_t prevTimestamp_ = 0; 108 uint64_t refreshPeriod_ = 16666667; 109 int32_t tid_ = -1; 110 uint64_t mValue = 0; 111 112 RSContext context_; 113 114 RenderContext* renderContext_ = nullptr; 115 }; 116 } // namespace Rosen 117 } // namespace OHOS 118 119 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H 120