• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class RSB_EXPORT RSTransactionProxy final {
35 public:
36     static RSB_EXPORT RSTransactionProxy* GetInstance();
37     void SetRenderThreadClient(std::unique_ptr<RSIRenderClient>& renderThreadClient);
38     void SetRenderServiceClient(const std::shared_ptr<RSIRenderClient>& renderServiceClient);
39 
40     void AddCommand(std::unique_ptr<RSCommand>& command, bool isRenderServiceCommand = false,
41                     FollowType followType = FollowType::NONE, NodeId nodeId = 0);
42     void AddCommandFromRT(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType = FollowType::FOLLOW_TO_PARENT);
43 
44     void FlushImplicitTransaction(uint64_t timestamp = 0, const std::string& abilityName = "");
45     void FlushImplicitTransactionFromRT(uint64_t timestamp);
46 
47     void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task, bool isRenderServiceTask = false);
48 
49     void Begin();
50     void Commit(uint64_t timestamp = 0);
51     void CommitSyncTransaction(uint64_t timestamp = 0, const std::string& abilityName = "");
52     void MarkTransactionNeedSync();
53     void MarkTransactionNeedCloseSync(const int32_t transactionCount);
54 
55     void StartSyncTransaction();
56     void CloseSyncTransaction();
57 
SetSyncId(const uint64_t syncId)58     void SetSyncId(const uint64_t syncId)
59     {
60         syncId_ = syncId;
61     }
62 
63 private:
64     RSTransactionProxy();
65     virtual ~RSTransactionProxy();
66     static void Init();
67     static void Destroy();
68 
69     RSTransactionProxy(const RSTransactionProxy&) = delete;
70     RSTransactionProxy(const RSTransactionProxy&&) = delete;
71     RSTransactionProxy& operator=(const RSTransactionProxy&) = delete;
72     RSTransactionProxy& operator=(const RSTransactionProxy&&) = delete;
73 
74     void AddCommonCommand(std::unique_ptr<RSCommand>& command);
75     void AddRemoteCommand(std::unique_ptr<RSCommand>& command, NodeId nodeId, FollowType followType);
76 
77     // Command Transaction Triggered by UI Thread.
78     std::mutex mutex_;
79     std::unique_ptr<RSTransactionData> implicitCommonTransactionData_{std::make_unique<RSTransactionData>()};
80     std::unique_ptr<RSTransactionData> implicitRemoteTransactionData_{std::make_unique<RSTransactionData>()};
81 
82     std::stack<std::unique_ptr<RSTransactionData>> implicitCommonTransactionDataStack_;
83     std::stack<std::unique_ptr<RSTransactionData>> implicitRemoteTransactionDataStack_;
84 
85     // Command Transaction Triggered by Render Thread which is definitely send to Render Service.
86     std::mutex mutexForRT_;
87     std::unique_ptr<RSTransactionData> implicitTransactionDataFromRT_{std::make_unique<RSTransactionData>()};
88 
89     std::shared_ptr<RSIRenderClient> renderServiceClient_ = RSIRenderClient::CreateRenderServiceClient();
90     std::unique_ptr<RSIRenderClient> renderThreadClient_ = nullptr;
91     uint64_t timestamp_ = 0;
92     static std::once_flag flag_;
93     static RSTransactionProxy* instance_;
94     bool needSync_ { false };
95     uint64_t syncId_ { 0 };
96 };
97 } // namespace Rosen
98 } // namespace OHOS
99 
100 #endif // ROSEN_RENDER_SERVICE_BASE_RS_TRANSACTION_PROXY_H
101