1/*
2 * Copyright (c) 2023 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 FOUNDATION_DMSERVER_FOLD_SCREEN_INFO_H
17#define FOUNDATION_DMSERVER_FOLD_SCREEN_INFO_H
18
19#include <parcel.h>
20
21#include "class_var_definition.h"
22#include "cutout_info.h"
23#include "dm_common.h"
24
25namespace OHOS::Rosen {
26/**
27 * @brief Fold Crease Region
28 */
29class FoldCreaseRegion : public Parcelable {
30public:
31    FoldCreaseRegion() = default;
32
33    FoldCreaseRegion(DisplayId displayId, const std::vector<DMRect>& creaseRects)
34        : displayId_(displayId), creaseRects_(creaseRects) {}
35
36    ~FoldCreaseRegion() override = default;
37
38    bool Marshalling(Parcel& parcel) const override
39    {
40        return parcel.WriteUint64(displayId_) &&
41        WriteCreaseRectVector(creaseRects_, parcel);
42    }
43
44    static FoldCreaseRegion *Unmarshalling(Parcel& parcel)
45    {
46        DisplayId displayId = DISPLAY_ID_INVALID;
47        std::vector<DMRect> creaseRects;
48        parcel.ReadUint64(displayId);
49        ReadCreaseRectVector(creaseRects, parcel);
50        auto* region = new FoldCreaseRegion(displayId, creaseRects);
51        return region;
52    }
53
54DEFINE_VAR_DEFAULT_FUNC_GET_SET(DisplayId, DisplayId, displayId, DISPLAY_ID_INVALID)
55
56DEFINE_VAR_FUNC_GET(std::vector<DMRect>, CreaseRects, creaseRects)
57
58    void SetCreaseRects(std::vector<DMRect> value)
59    { creaseRects_ = std::move(value); }
60private:
61    const static uint32_t MAX_CREASE_REGION_SIZE = 20;
62
63    static bool WriteCreaseRectVector(const std::vector<DMRect>& creaseRects, Parcel& parcel)
64    {
65        auto size = static_cast<uint32_t>(creaseRects.size());
66        if (!parcel.WriteUint32(size)) {
67            return false;
68        }
69        if (size > MAX_CREASE_REGION_SIZE) {
70            return false;
71        }
72        for (auto rect : creaseRects) {
73            if (!(parcel.WriteInt32(rect.posX_) && parcel.WriteInt32(rect.posY_) &&
74                parcel.WriteUint32(rect.width_) && parcel.WriteUint32(rect.height_))) {
75                return false;
76            }
77        }
78        return true;
79    }
80
81    static bool ReadCreaseRectVector(std::vector<DMRect>& creaseRects, Parcel& parcel)
82    {
83        uint32_t size;
84        if (!parcel.ReadUint32(size)) {
85            return false;
86        }
87        if (size > MAX_CREASE_REGION_SIZE) {
88            return false;
89        }
90        for (uint32_t index = 0; index < size; index++) {
91            int32_t posX;
92            int32_t posY;
93            uint32_t width;
94            uint32_t height;
95            if (!(parcel.ReadInt32(posX) && parcel.ReadInt32(posY) &&
96                parcel.ReadUint32(width) && parcel.ReadUint32(height))) {
97                return false;
98            }
99            DMRect rect = {posX, posY, width, height};
100            creaseRects.push_back(rect);
101        }
102        return true;
103    }
104};
105} // namespace OHOS::Rosen
106#endif // FOUNDATION_DMSERVER_FOLD_SCREEN_INFO_H