• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t left{0};
40     int32_t top{0};
41     int32_t width{0};
42     int32_t height{0};
43     if (!parcel.ReadInt32(left) || !parcel.ReadInt32(top) || !parcel.ReadInt32(width) || !parcel.ReadInt32(height)) {
44         RS_LOGE("RSUIExtensionData::UnmarshallingRectInfo Read relativeCoords failed");
45         return;
46     }
47     rectInfo.relativeCoords.SetAll(left, top, width, height);
48     if (!parcel.ReadFloat(rectInfo.scale[0]) || !parcel.ReadFloat(rectInfo.scale[1])) {
49         RS_LOGE("RSUIExtensionData::UnmarshallingRectInfo Read scale failed");
50         return;
51     }
52     if (!parcel.ReadFloat(rectInfo.anchor[0]) || !parcel.ReadFloat(rectInfo.anchor[1])) {
53         RS_LOGE("RSUIExtensionData::UnmarshallingRectInfo Read anchor failed");
54         return;
55     }
56 }
57 
58 
Marshalling(Parcel & parcel) const59 bool RSUIExtensionData::Marshalling(Parcel& parcel) const
60 {
61     bool marshallingSuccess = true;
62     marshallingSuccess &= parcel.WriteUint32(secData_.size());
63     for (const auto& data : secData_) {
64         marshallingSuccess &= parcel.WriteUint64(data.first);     // hostNodeId
65         marshallingSuccess &= parcel.WriteUint32(static_cast<uint32_t>(data.second.size()));
66         for (const auto& secSurfaceInfo : data.second) {
67             marshallingSuccess &= MarshallingRectInfo(secSurfaceInfo.uiExtensionRectInfo, parcel);
68             marshallingSuccess &= parcel.WriteInt32(secSurfaceInfo.hostPid);
69             marshallingSuccess &= parcel.WriteInt32(secSurfaceInfo.uiExtensionPid);
70             marshallingSuccess &= parcel.WriteUint64(secSurfaceInfo.hostNodeId);
71             marshallingSuccess &= parcel.WriteUint64(secSurfaceInfo.uiExtensionNodeId);
72 
73             marshallingSuccess &= parcel.WriteUint32(static_cast<uint32_t>(secSurfaceInfo.upperNodes.size()));
74             for (const auto& rectInfo : secSurfaceInfo.upperNodes) {
75                 marshallingSuccess &= MarshallingRectInfo(rectInfo, parcel);
76             }
77         }
78     }
79     if (!marshallingSuccess) {
80         RS_LOGE("RSUIExtensionData::Marshalling failed");
81     }
82     return marshallingSuccess;
83 }
84 
Unmarshalling(Parcel & parcel)85 RSUIExtensionData* RSUIExtensionData::Unmarshalling(Parcel& parcel)
86 {
87     auto uiExtensionData = new (std::nothrow) RSUIExtensionData();
88     if (!uiExtensionData) {
89         return nullptr;
90     }
91     uint32_t mapSize{0};
92     if (!parcel.ReadUint32(mapSize)) {
93         ROSEN_LOGE("RSUIExtensionData::Unmarshalling Read mapSize failed");
94         return nullptr;
95     }
96     if (mapSize > uiExtensionData->secData_.max_size()) {
97         RS_LOGE("RSUIExtensionData Unmarshalling failed, map size overflow.");
98         delete uiExtensionData;
99         return nullptr;
100     }
101     for (uint32_t hostIndex = 0; hostIndex < mapSize; ++hostIndex) {
102         uint64_t hostNodeId{0};
103         if (!parcel.ReadUint64(hostNodeId)) {
104             ROSEN_LOGE("RSUIExtensionData::Unmarshalling Read hostNodeId failed");
105             return nullptr;
106         }
107         uiExtensionData->secData_.insert(std::make_pair(hostNodeId, std::vector<SecSurfaceInfo>()));
108         uint32_t uiExtensionNodesCount{0};
109         if (!parcel.ReadUint32(uiExtensionNodesCount)) {
110             ROSEN_LOGE("RSUIExtensionData::Unmarshalling Read uiExtensionNodesCount failed");
111             return nullptr;
112         }
113         if (uiExtensionNodesCount > uiExtensionData->secData_[hostNodeId].max_size()) {
114             RS_LOGE("RSUIExtensionData Unmarshalling failed, vector size overflow.");
115             delete uiExtensionData;
116             return nullptr;
117         }
118         for (uint32_t uiExtensionIndex = 0; uiExtensionIndex < uiExtensionNodesCount; ++uiExtensionIndex) {
119             SecSurfaceInfo secSurfaceInfo;
120             UnmarshallingRectInfo(secSurfaceInfo.uiExtensionRectInfo, parcel);
121             int32_t tempHostPid{0};
122             int32_t tempUiExtensionPid{0};
123             uint64_t tempHostNodeId{0};
124             uint64_t tempUiExtensionNodeId{0};
125             if (!parcel.ReadInt32(tempHostPid) || !parcel.ReadInt32(tempUiExtensionPid) ||
126                 !parcel.ReadUint64(tempHostNodeId) || !parcel.ReadUint64(tempUiExtensionNodeId)) {
127                 ROSEN_LOGE("RSUIExtensionData::Unmarshalling Read secSurfaceInfo failed");
128                 return nullptr;
129             }
130             secSurfaceInfo.hostPid = static_cast<pid_t>(tempHostPid);
131             secSurfaceInfo.uiExtensionPid = static_cast<pid_t>(tempUiExtensionPid);
132             secSurfaceInfo.hostNodeId = tempHostNodeId;
133             secSurfaceInfo.uiExtensionNodeId = tempUiExtensionNodeId;
134             uint32_t upperNodesCount{0};
135             if (!parcel.ReadUint32(upperNodesCount)) {
136                 ROSEN_LOGE("RSUIExtensionData::Unmarshalling Read upperNodesCount failed");
137                 return nullptr;
138             }
139             if (upperNodesCount > secSurfaceInfo.upperNodes.max_size()) {
140                 RS_LOGE("RSUIExtensionData Unmarshalling failed, upperNodes size overflow.");
141                 delete uiExtensionData;
142                 return nullptr;
143             }
144             for (uint32_t upperNodesIndex = 0; upperNodesIndex < upperNodesCount; ++upperNodesIndex) {
145                 SecRectInfo upperNodeInfo;
146                 UnmarshallingRectInfo(upperNodeInfo, parcel);
147                 secSurfaceInfo.upperNodes.emplace_back(upperNodeInfo);
148             }
149             uiExtensionData->secData_[hostNodeId].emplace_back(secSurfaceInfo);
150         }
151     }
152     return uiExtensionData;
153 }
154 } // namespace Rosen
155 } // namespace OHOS
156