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 ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H 17 #define ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H 18 19 #include <memory> 20 #include <mutex> 21 #include <stack> 22 23 #include "command/rs_command.h" 24 #include "common/rs_macros.h" 25 #include "common/rs_singleton.h" 26 #include "common/rs_macros.h" 27 #include "transaction/rs_irender_client.h" 28 #include "transaction/rs_transaction_data.h" 29 30 namespace OHOS { 31 namespace Rosen { 32 class RSSyncTask; 33 class RSB_EXPORT RSTransactionProxy final { 34 public: 35 static RSB_EXPORT RSTransactionProxy* GetInstance(); 36 void SetRenderThreadClient(std::unique_ptr<RSIRenderClient>& renderThreadClient); 37 void SetRenderServiceClient(const std::shared_ptr<RSIRenderClient>& renderServiceClient); 38 39 void AddCommand(std::unique_ptr<RSCommand>& command, bool isRenderServiceCommand = false, 40 FollowType followType = FollowType::NONE, NodeId nodeId = 0); 41 void AddCommandFromRT(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType = FollowType::FOLLOW_TO_PARENT); 42 43 void FlushImplicitTransaction(uint64_t timestamp = 0, const std::string& abilityName = ""); 44 void FlushImplicitTransactionFromRT(uint64_t timestamp); 45 46 void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task, bool isRenderServiceTask = false); 47 48 void Begin(); 49 void Commit(uint64_t timestamp = 0); 50 51 private: 52 RSTransactionProxy(); 53 virtual ~RSTransactionProxy(); 54 static void Init(); 55 static void Destroy(); 56 57 RSTransactionProxy(const RSTransactionProxy&) = delete; 58 RSTransactionProxy(const RSTransactionProxy&&) = delete; 59 RSTransactionProxy& operator=(const RSTransactionProxy&) = delete; 60 RSTransactionProxy& operator=(const RSTransactionProxy&&) = delete; 61 62 void AddCommonCommand(std::unique_ptr<RSCommand>& command); 63 void AddRemoteCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType); 64 65 // Command Transaction Triggered by UI Thread. 66 std::mutex mutex_; 67 std::unique_ptr<RSTransactionData> implicitCommonTransactionData_{std::make_unique<RSTransactionData>()}; 68 std::unique_ptr<RSTransactionData> implicitRemoteTransactionData_{std::make_unique<RSTransactionData>()}; 69 70 std::stack<std::unique_ptr<RSTransactionData>> implicitCommonTransactionDataStack_; 71 std::stack<std::unique_ptr<RSTransactionData>> implicitRemoteTransactionDataStack_; 72 73 // Command Transaction Triggered by Render Thread which is definitely send to Render Service. 74 std::mutex mutexForRT_; 75 std::unique_ptr<RSTransactionData> implicitTransactionDataFromRT_{std::make_unique<RSTransactionData>()}; 76 77 std::shared_ptr<RSIRenderClient> renderServiceClient_ = RSIRenderClient::CreateRenderServiceClient(); 78 std::unique_ptr<RSIRenderClient> renderThreadClient_ = nullptr; 79 uint64_t timestamp_ = 0; 80 static std::once_flag flag_; 81 static RSTransactionProxy* instance_; 82 }; 83 } // namespace Rosen 84 } // namespace OHOS 85 86 #endif // ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H 87