1 /*
2  * Copyright (c) 2022 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 RS_UNMARSHAL_THREAD_H
17 #define RS_UNMARSHAL_THREAD_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 
22 #include "event_handler.h"
23 #include "message_parcel.h"
24 
25 #include "transaction/rs_transaction_data.h"
26 
27 namespace OHOS::Rosen {
28 class RSUnmarshalThread {
29 public:
30     static RSUnmarshalThread& Instance();
31     void Start();
32     void PostTask(const std::function<void()>& task);
33     void RecvParcel(std::shared_ptr<MessageParcel>& parcel, bool isNonSystemAppCalling = false, pid_t callingPid = 0);
34     TransactionDataMap GetCachedTransactionData();
35     bool CachedTransactionDataEmpty();
36 
37     bool ReportTransactionDataStatistics(pid_t pid, RSTransactionData* transactionData,
38         bool isNonSystemAppCalling = false);
39     void ClearTransactionDataStatistics();
40 
41 private:
42     RSUnmarshalThread() = default;
43     ~RSUnmarshalThread() = default;
44     RSUnmarshalThread(const RSUnmarshalThread&);
45     RSUnmarshalThread(const RSUnmarshalThread&&);
46     RSUnmarshalThread& operator=(const RSUnmarshalThread&);
47     RSUnmarshalThread& operator=(const RSUnmarshalThread&&);
48 
49     bool IsHaveCmdList(const std::unique_ptr<RSCommand>& cmd) const;
50     void SetFrameLoad(int load);
51     void SetFrameParam(int requestId, int load, int frameNum, int value);
52     static constexpr uint32_t MIN_PENDING_REQUEST_SYNC_DATA_SIZE = 32 * 1024;
53 
54     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
55     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
56 
57     std::mutex transactionDataMutex_;
58     TransactionDataMap cachedTransactionDataMap_;
59     bool willHaveCachedData_ = false;
60     int unmarshalTid_ = -1;
61     int unmarshalLoad_ = 0;
62 
63     std::mutex statisticsMutex_;
64     std::unordered_map<pid_t, size_t> transactionDataStatistics_;
65 };
66 }
67 #endif // RS_UNMARSHAL_THREAD_H
68