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