1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#include "cutout_info.h"
17e0dac50fSopenharmony_ci
18e0dac50fSopenharmony_cinamespace OHOS::Rosen {
19e0dac50fSopenharmony_ciCutoutInfo::CutoutInfo(const std::vector<DMRect>& boundingRects,
20e0dac50fSopenharmony_ci    WaterfallDisplayAreaRects waterfallDisplayAreaRects) : waterfallDisplayAreaRects_(waterfallDisplayAreaRects),
21e0dac50fSopenharmony_ci    boundingRects_(boundingRects)
22e0dac50fSopenharmony_ci{
23e0dac50fSopenharmony_ci}
24e0dac50fSopenharmony_ci
25e0dac50fSopenharmony_cibool CutoutInfo::Marshalling(Parcel& parcel) const
26e0dac50fSopenharmony_ci{
27e0dac50fSopenharmony_ci    return parcel.WriteInt32(waterfallDisplayAreaRects_.left.posX_) &&
28e0dac50fSopenharmony_ci        parcel.WriteInt32(waterfallDisplayAreaRects_.left.posY_) &&
29e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.left.width_) &&
30e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.left.height_) &&
31e0dac50fSopenharmony_ci        parcel.WriteInt32(waterfallDisplayAreaRects_.top.posX_) &&
32e0dac50fSopenharmony_ci        parcel.WriteInt32(waterfallDisplayAreaRects_.top.posY_) &&
33e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.top.width_) &&
34e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.top.height_) &&
35e0dac50fSopenharmony_ci        parcel.WriteInt32(waterfallDisplayAreaRects_.right.posX_) &&
36e0dac50fSopenharmony_ci        parcel.WriteInt32(waterfallDisplayAreaRects_.right.posY_) &&
37e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.right.width_) &&
38e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.right.height_) &&
39e0dac50fSopenharmony_ci        parcel.WriteInt32(waterfallDisplayAreaRects_.bottom.posX_) &&
40e0dac50fSopenharmony_ci        parcel.WriteInt32(waterfallDisplayAreaRects_.bottom.posY_) &&
41e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.bottom.width_) &&
42e0dac50fSopenharmony_ci        parcel.WriteUint32(waterfallDisplayAreaRects_.bottom.height_) &&
43e0dac50fSopenharmony_ci        WriteBoundingRectsVector(boundingRects_, parcel);
44e0dac50fSopenharmony_ci}
45e0dac50fSopenharmony_ci
46e0dac50fSopenharmony_ciCutoutInfo *CutoutInfo::Unmarshalling(Parcel& parcel)
47e0dac50fSopenharmony_ci{
48e0dac50fSopenharmony_ci    WaterfallDisplayAreaRects waterfallDisplayAreaRects;
49e0dac50fSopenharmony_ci    std::vector<DMRect> boundingRects;
50e0dac50fSopenharmony_ci    static_cast<void>(ReadWaterfallDisplayAreaRects(waterfallDisplayAreaRects, parcel));
51e0dac50fSopenharmony_ci    static_cast<void>(ReadBoundingRectsVector(boundingRects, parcel));
52e0dac50fSopenharmony_ci    CutoutInfo *cutoutInfo = new CutoutInfo(boundingRects, waterfallDisplayAreaRects);
53e0dac50fSopenharmony_ci    return cutoutInfo;
54e0dac50fSopenharmony_ci}
55e0dac50fSopenharmony_ci
56e0dac50fSopenharmony_cibool CutoutInfo::WriteBoundingRectsVector(const std::vector<DMRect>& boundingRects, Parcel& parcel) const
57e0dac50fSopenharmony_ci{
58e0dac50fSopenharmony_ci    uint32_t size = static_cast<uint32_t>(boundingRects.size());
59e0dac50fSopenharmony_ci    if (!parcel.WriteUint32(size)) {
60e0dac50fSopenharmony_ci        return false;
61e0dac50fSopenharmony_ci    }
62e0dac50fSopenharmony_ci    if (size > MAX_CUTOUT_INFO_SIZE) {
63e0dac50fSopenharmony_ci        return false;
64e0dac50fSopenharmony_ci    }
65e0dac50fSopenharmony_ci    for (DMRect rect : boundingRects) {
66e0dac50fSopenharmony_ci        if (!(parcel.WriteInt32(rect.posX_) && parcel.WriteInt32(rect.posY_) &&
67e0dac50fSopenharmony_ci            parcel.WriteUint32(rect.width_) && parcel.WriteUint32(rect.height_))) {
68e0dac50fSopenharmony_ci            return false;
69e0dac50fSopenharmony_ci        }
70e0dac50fSopenharmony_ci    }
71e0dac50fSopenharmony_ci    return true;
72e0dac50fSopenharmony_ci}
73e0dac50fSopenharmony_ci
74e0dac50fSopenharmony_cibool CutoutInfo::ReadBoundingRectsVector(std::vector<DMRect>& unmarBoundingRects, Parcel& parcel)
75e0dac50fSopenharmony_ci{
76e0dac50fSopenharmony_ci    uint32_t size;
77e0dac50fSopenharmony_ci    if (!parcel.ReadUint32(size)) {
78e0dac50fSopenharmony_ci        return false;
79e0dac50fSopenharmony_ci    }
80e0dac50fSopenharmony_ci    if (size > MAX_CUTOUT_INFO_SIZE) {
81e0dac50fSopenharmony_ci        return false;
82e0dac50fSopenharmony_ci    }
83e0dac50fSopenharmony_ci    for (uint32_t index = 0; index < size; index++) {
84e0dac50fSopenharmony_ci        int32_t posX;
85e0dac50fSopenharmony_ci        int32_t posY;
86e0dac50fSopenharmony_ci        uint32_t width;
87e0dac50fSopenharmony_ci        uint32_t height;
88e0dac50fSopenharmony_ci        if (!(parcel.ReadInt32(posX) && parcel.ReadInt32(posY) &&
89e0dac50fSopenharmony_ci            parcel.ReadUint32(width) && parcel.ReadUint32(height))) {
90e0dac50fSopenharmony_ci            return false;
91e0dac50fSopenharmony_ci        }
92e0dac50fSopenharmony_ci        DMRect rect = {posX, posY, width, height};
93e0dac50fSopenharmony_ci        unmarBoundingRects.push_back(rect);
94e0dac50fSopenharmony_ci    }
95e0dac50fSopenharmony_ci    return true;
96e0dac50fSopenharmony_ci}
97e0dac50fSopenharmony_ci
98e0dac50fSopenharmony_cibool CutoutInfo::ReadWaterfallDisplayAreaRects(WaterfallDisplayAreaRects& waterfallDisplayAreaRects, Parcel& parcel)
99e0dac50fSopenharmony_ci{
100e0dac50fSopenharmony_ci    if (!(parcel.ReadInt32(waterfallDisplayAreaRects.left.posX_) &&
101e0dac50fSopenharmony_ci        parcel.ReadInt32(waterfallDisplayAreaRects.left.posY_) &&
102e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.left.width_) &&
103e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.left.height_) &&
104e0dac50fSopenharmony_ci        parcel.ReadInt32(waterfallDisplayAreaRects.top.posX_) &&
105e0dac50fSopenharmony_ci        parcel.ReadInt32(waterfallDisplayAreaRects.top.posY_) &&
106e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.top.width_) &&
107e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.top.height_) &&
108e0dac50fSopenharmony_ci        parcel.ReadInt32(waterfallDisplayAreaRects.right.posX_) &&
109e0dac50fSopenharmony_ci        parcel.ReadInt32(waterfallDisplayAreaRects.right.posY_) &&
110e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.right.width_) &&
111e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.right.height_) &&
112e0dac50fSopenharmony_ci        parcel.ReadInt32(waterfallDisplayAreaRects.bottom.posX_) &&
113e0dac50fSopenharmony_ci        parcel.ReadInt32(waterfallDisplayAreaRects.bottom.posY_) &&
114e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.bottom.width_) &&
115e0dac50fSopenharmony_ci        parcel.ReadUint32(waterfallDisplayAreaRects.bottom.height_))) {
116e0dac50fSopenharmony_ci        return false;
117e0dac50fSopenharmony_ci    }
118e0dac50fSopenharmony_ci    return true;
119e0dac50fSopenharmony_ci}
120e0dac50fSopenharmony_ci} // namespace OHOS::Rosen