• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ipc_callbacks/rs_transaction_data_callback.h"
16 #include "platform/common/rs_log.h"
17 #include "rs_trace.h"
18 #include "transaction/rs_transaction_data_callback_manager.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 constexpr uint64_t MAX_TRANSACTION_DATA_CALLBACKS = 65535;
Instance()23 RSTransactionDataCallbackManager& RSTransactionDataCallbackManager::Instance()
24 {
25     static RSTransactionDataCallbackManager mgr;
26     return mgr;
27 }
28 
RegisterTransactionDataCallback(uint64_t token,uint64_t timeStamp,sptr<RSITransactionDataCallback> callback)29 void RSTransactionDataCallbackManager::RegisterTransactionDataCallback(uint64_t token,
30     uint64_t timeStamp, sptr<RSITransactionDataCallback> callback)
31 {
32     RS_LOGD("RSTransactionDataCallbackManager save data, timeStamp: %{public}"
33         PRIu64 " token: %{public}" PRIu64, timeStamp, token);
34     if (!PushTransactionDataCallback(token, timeStamp, callback)) {
35         RS_LOGE("RegisterTransactionDataCallback register callback err");
36     }
37 }
38 
TriggerTransactionDataCallback(uint64_t token,uint64_t timeStamp)39 void RSTransactionDataCallbackManager::TriggerTransactionDataCallback(uint64_t token, uint64_t timeStamp)
40 {
41     if (auto callback = PopTransactionDataCallback(token, timeStamp)) {
42         RS_LOGD("RSTransactionDataCallbackManager trigger data, timeStamp: %{public}"
43             PRIu64 " token: %{public}" PRIu64, timeStamp, token);
44         callback->OnAfterProcess(token, timeStamp);
45     } else {
46         RS_LOGD("RSTransactionDataCallbackManager trigger callback error, timeStamp: %{public}"
47             PRIu64 " token: %{public}" PRIu64, timeStamp, token);
48     }
49 }
50 
PushTransactionDataCallback(uint64_t token,uint64_t timeStamp,sptr<RSITransactionDataCallback> callback)51 bool RSTransactionDataCallbackManager::PushTransactionDataCallback(uint64_t token,
52     uint64_t timeStamp, sptr<RSITransactionDataCallback> callback)
53 {
54     std::lock_guard<std::mutex> lock{ transactionDataCbMutex_ };
55     if (transactionDataCallbacks_.size() >= MAX_TRANSACTION_DATA_CALLBACKS) {
56         RS_LOGE("RSTransactionDataCallbackManager: transactionDataCallbacks_ has reached maximus size, cannot add new "
57                 "callback");
58         return false;
59     }
60     if (transactionDataCallbacks_.find(std::make_pair(token, timeStamp)) == std::end(transactionDataCallbacks_)) {
61         RS_LOGD("RSTransactionDataCallbackManager push data, timeStamp: %{public}"
62             PRIu64 " token: %{public}" PRIu64, timeStamp, token);
63         transactionDataCallbacks_.emplace(std::make_pair(token, timeStamp), callback);
64         return true;
65     }
66     return false;
67 }
68 
PopTransactionDataCallback(uint64_t token,uint64_t timeStamp)69 sptr<RSITransactionDataCallback> RSTransactionDataCallbackManager::PopTransactionDataCallback(uint64_t token,
70     uint64_t timeStamp)
71 {
72     std::lock_guard<std::mutex> lock { transactionDataCbMutex_ };
73     auto iter = transactionDataCallbacks_.find(std::make_pair(token, timeStamp));
74     if (iter != std::end(transactionDataCallbacks_)) {
75         RS_LOGD("RSTransactionDataCallbackManager pop data, timeStamp: %{public}"
76             PRIu64 " token: %{public}" PRIu64, timeStamp, token);
77         auto callback = iter->second;
78         transactionDataCallbacks_.erase(iter);
79         return callback;
80     }
81     return nullptr;
82 }
83 } //namespace Rosen
84 } //namespace OHOS