1 /*
2  * Copyright (c) 2024 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 #include "transaction/rs_uiextension_data.h"
17 #include "platform/common/rs_log.h"
18 
19 namespace OHOS {
20 namespace Rosen {
MarshallingRectInfo(const SecRectInfo & rectInfo,Parcel & parcel)21 bool RSUIExtensionData::MarshallingRectInfo(const SecRectInfo& rectInfo, Parcel& parcel)
22 {
23     // Write coordinates(4 int), scale(2 float), anchor position(2float).
24     bool marshallingSuccess = true;
25     marshallingSuccess &= parcel.WriteInt32(rectInfo.relativeCoords.GetLeft());
26     marshallingSuccess &= parcel.WriteInt32(rectInfo.relativeCoords.GetTop());
27     marshallingSuccess &= parcel.WriteInt32(rectInfo.relativeCoords.GetWidth());
28     marshallingSuccess &= parcel.WriteInt32(rectInfo.relativeCoords.GetHeight());
29     marshallingSuccess &= parcel.WriteFloat(rectInfo.scale[0]);
30     marshallingSuccess &= parcel.WriteFloat(rectInfo.scale[1]);
31     marshallingSuccess &= parcel.WriteFloat(rectInfo.anchor[0]);
32     marshallingSuccess &= parcel.WriteFloat(rectInfo.anchor[1]);
33     return marshallingSuccess;
34 }
35 
UnmarshallingRectInfo(SecRectInfo & rectInfo,Parcel & parcel)36 void RSUIExtensionData::UnmarshallingRectInfo(SecRectInfo& rectInfo, Parcel& parcel)
37 {
38     // Read coordinates(4 int), scale(2 float), anchor position(2float).
39     rectInfo.relativeCoords.SetAll(parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32());
40     rectInfo.scale[0] = parcel.ReadFloat();
41     rectInfo.scale[1] = parcel.ReadFloat();
42     rectInfo.anchor[0] = parcel.ReadFloat();
43     rectInfo.anchor[1] = parcel.ReadFloat();
44 }
45 
46 
Marshalling(Parcel & parcel) const47 bool RSUIExtensionData::Marshalling(Parcel& parcel) const
48 {
49     bool marshallingSuccess = true;
50     marshallingSuccess &= parcel.WriteUint32(secData_.size());
51     for (const auto& data : secData_) {
52         marshallingSuccess &= parcel.WriteUint64(data.first);     // hostNodeId
53         marshallingSuccess &= parcel.WriteUint32(static_cast<uint32_t>(data.second.size()));
54         for (const auto& secSurfaceInfo : data.second) {
55             marshallingSuccess &= MarshallingRectInfo(secSurfaceInfo.uiExtensionRectInfo, parcel);
56             marshallingSuccess &= parcel.WriteInt32(secSurfaceInfo.hostPid);
57             marshallingSuccess &= parcel.WriteInt32(secSurfaceInfo.uiExtensionPid);
58             marshallingSuccess &= parcel.WriteUint64(secSurfaceInfo.hostNodeId);
59             marshallingSuccess &= parcel.WriteUint64(secSurfaceInfo.uiExtensionNodeId);
60 
61             marshallingSuccess &= parcel.WriteUint32(static_cast<uint32_t>(secSurfaceInfo.upperNodes.size()));
62             for (const auto& rectInfo : secSurfaceInfo.upperNodes) {
63                 marshallingSuccess &= MarshallingRectInfo(rectInfo, parcel);
64             }
65         }
66     }
67     return marshallingSuccess;
68 }
69 
Unmarshalling(Parcel & parcel)70 RSUIExtensionData* RSUIExtensionData::Unmarshalling(Parcel& parcel)
71 {
72     auto uiExtensionData = new (std::nothrow) RSUIExtensionData();
73     if (!uiExtensionData) {
74         return nullptr;
75     }
76     auto mapSize = parcel.ReadUint32();
77     if (mapSize > uiExtensionData->secData_.max_size()) {
78         RS_LOGE("RSUIExtensionData Unmarshalling failed, map size overflow.");
79         delete uiExtensionData;
80         return nullptr;
81     }
82     for (uint32_t hostIndex = 0; hostIndex < mapSize; ++hostIndex) {
83         uint64_t hostNodeId = parcel.ReadUint64();
84         uiExtensionData->secData_.insert(std::make_pair(hostNodeId, std::vector<SecSurfaceInfo>()));
85         uint32_t uiExtensionNodesCount = parcel.ReadUint32();
86         if (uiExtensionNodesCount > uiExtensionData->secData_[hostNodeId].max_size()) {
87             RS_LOGE("RSUIExtensionData Unmarshalling failed, vector size overflow.");
88             delete uiExtensionData;
89             return nullptr;
90         }
91         for (uint32_t uiExtensionIndex = 0; uiExtensionIndex < uiExtensionNodesCount; ++uiExtensionIndex) {
92             SecSurfaceInfo secSurfaceInfo;
93             UnmarshallingRectInfo(secSurfaceInfo.uiExtensionRectInfo, parcel);
94             secSurfaceInfo.hostPid = parcel.ReadInt32();
95             secSurfaceInfo.uiExtensionPid = parcel.ReadInt32();
96             secSurfaceInfo.hostNodeId = parcel.ReadUint64();
97             secSurfaceInfo.uiExtensionNodeId = parcel.ReadUint64();
98             uint32_t upperNodesCount = parcel.ReadUint32();
99             if (upperNodesCount > secSurfaceInfo.upperNodes.max_size()) {
100                 RS_LOGE("RSUIExtensionData Unmarshalling failed, upperNodes size overflow.");
101                 delete uiExtensionData;
102                 return nullptr;
103             }
104             for (uint32_t upperNodesIndex = 0; upperNodesIndex < upperNodesCount; ++upperNodesIndex) {
105                 SecRectInfo upperNodeInfo;
106                 UnmarshallingRectInfo(upperNodeInfo, parcel);
107                 secSurfaceInfo.upperNodes.emplace_back(upperNodeInfo);
108             }
109             uiExtensionData->secData_[hostNodeId].emplace_back(secSurfaceInfo);
110         }
111     }
112     return uiExtensionData;
113 }
114 } // namespace Rosen
115 } // namespace OHOS
116