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 "command/rs_node_showing_command.h" 25 #include "common/rs_macros.h" 26 #include "common/rs_singleton.h" 27 #include "common/rs_macros.h" 28 #include "transaction/rs_irender_client.h" 29 #include "transaction/rs_transaction_data.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 class RSSyncTask; 34 using FlushEmptyCallback = std::function<bool(const uint64_t)>; 35 class RSB_EXPORT RSTransactionProxy final { 36 public: 37 static RSB_EXPORT RSTransactionProxy* GetInstance(); 38 void SetRenderThreadClient(std::unique_ptr<RSIRenderClient>& renderThreadClient); 39 void SetRenderServiceClient(const std::shared_ptr<RSIRenderClient>& renderServiceClient); 40 41 void AddCommand(std::unique_ptr<RSCommand>& command, bool isRenderServiceCommand = false, 42 FollowType followType = FollowType::NONE, NodeId nodeId = 0); 43 void AddCommandFromRT(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType = FollowType::FOLLOW_TO_PARENT); 44 45 void FlushImplicitTransaction(uint64_t timestamp = 0, const std::string& abilityName = ""); 46 void FlushImplicitTransactionFromRT(uint64_t timestamp); 47 48 void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task, bool isRenderServiceTask = false); 49 50 void Begin(); 51 void Commit(uint64_t timestamp = 0); 52 void CommitSyncTransaction(uint64_t timestamp = 0, const std::string& abilityName = ""); 53 void MarkTransactionNeedSync(); 54 void MarkTransactionNeedCloseSync(const int32_t transactionCount); 55 void SetSyncTransactionNum(const int32_t transactionCount); 56 57 void StartSyncTransaction(); 58 void CloseSyncTransaction(); SetFlushEmptyCallback(FlushEmptyCallback flushEmptyCallback)59 void SetFlushEmptyCallback(FlushEmptyCallback flushEmptyCallback) 60 { 61 flushEmptyCallback_ = flushEmptyCallback; 62 } 63 SetSyncId(const uint64_t syncId)64 void SetSyncId(const uint64_t syncId) 65 { 66 syncId_ = syncId; 67 } 68 69 void SetParentPid(const int32_t parentPid); 70 71 uint32_t GetTransactionDataIndex() const; 72 73 bool IsEmpty() const; 74 75 private: 76 RSTransactionProxy(); 77 virtual ~RSTransactionProxy(); 78 static void Init(); 79 static void Destroy(); 80 81 RSTransactionProxy(const RSTransactionProxy&) = delete; 82 RSTransactionProxy(const RSTransactionProxy&&) = delete; 83 RSTransactionProxy& operator=(const RSTransactionProxy&) = delete; 84 RSTransactionProxy& operator=(const RSTransactionProxy&&) = delete; 85 86 void AddCommonCommand(std::unique_ptr<RSCommand>& command); 87 void AddRemoteCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType); 88 89 // Command Transaction Triggered by UI Thread. 90 mutable std::mutex mutex_; 91 std::unique_ptr<RSTransactionData> implicitCommonTransactionData_{std::make_unique<RSTransactionData>()}; 92 std::unique_ptr<RSTransactionData> implicitRemoteTransactionData_{std::make_unique<RSTransactionData>()}; 93 94 std::stack<std::unique_ptr<RSTransactionData>> implicitCommonTransactionDataStack_; 95 std::stack<std::unique_ptr<RSTransactionData>> implicitRemoteTransactionDataStack_; 96 97 // Command Transaction Triggered by Render Thread which is definitely send to Render Service. 98 std::mutex mutexForRT_; 99 std::unique_ptr<RSTransactionData> implicitTransactionDataFromRT_{std::make_unique<RSTransactionData>()}; 100 101 std::shared_ptr<RSIRenderClient> renderServiceClient_ = RSIRenderClient::CreateRenderServiceClient(); 102 std::unique_ptr<RSIRenderClient> renderThreadClient_ = nullptr; 103 uint64_t timestamp_ = 0; 104 static std::once_flag flag_; 105 static RSTransactionProxy* instance_; 106 bool needSync_ { false }; 107 uint64_t syncId_ { 0 }; 108 FlushEmptyCallback flushEmptyCallback_ = nullptr; 109 uint32_t transactionDataIndex_ = 0; 110 }; 111 } // namespace Rosen 112 } // namespace OHOS 113 114 #endif // ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H 115