1 /*
2  * Copyright (c) 2021-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 #include "screen_info.h"
17 
18 namespace OHOS::Rosen {
Marshalling(Parcel &parcel) const19 bool ScreenInfo::Marshalling(Parcel &parcel) const
20 {
21     bool res = parcel.WriteString(name_) && parcel.WriteUint64(id_) &&
22         parcel.WriteUint32(virtualWidth_) && parcel.WriteUint32(virtualHeight_) &&
23         parcel.WriteFloat(virtualPixelRatio_) && parcel.WriteUint64(lastParent_) && parcel.WriteUint64(parent_) &&
24         parcel.WriteBool(isScreenGroup_) && parcel.WriteUint32(static_cast<uint32_t>(rotation_)) &&
25         parcel.WriteUint32(static_cast<uint32_t>(orientation_)) &&
26         parcel.WriteUint32(static_cast<uint32_t>(sourceMode_)) &&
27         parcel.WriteUint32(static_cast<uint32_t>(type_)) &&
28         parcel.WriteUint32(modeId_) && parcel.WriteUint32(static_cast<uint32_t>(modes_.size())) &&
29         parcel.WriteBool(isExtend_);
30     if (!res) {
31         return false;
32     }
33     if (modes_.size() > MAX_SUPPORTED_SCREEN_MODES_SIZE) {
34         return false;
35     }
36     for (uint32_t modeIndex = 0; modeIndex < modes_.size(); modeIndex++) {
37         if (parcel.WriteUint32(modes_[modeIndex]->id_) &&
38             parcel.WriteUint32(modes_[modeIndex]->height_) &&
39             parcel.WriteUint32(modes_[modeIndex]->width_) &&
40             parcel.WriteUint32(modes_[modeIndex]->refreshRate_)) {
41             continue;
42         }
43         return false;
44     }
45     return true;
46 }
47 
Unmarshalling(Parcel &parcel)48 ScreenInfo* ScreenInfo::Unmarshalling(Parcel &parcel)
49 {
50     ScreenInfo* info = new(std::nothrow) ScreenInfo();
51     if (info == nullptr) {
52         return info;
53     }
54     bool res = info->InnerUnmarshalling(parcel);
55     if (res) {
56         return info;
57     }
58     delete info;
59     return nullptr;
60 }
61 
InnerUnmarshalling(Parcel& parcel)62 bool ScreenInfo::InnerUnmarshalling(Parcel& parcel)
63 {
64     uint32_t size = 0;
65     uint32_t rotation;
66     uint32_t orientation;
67     uint32_t sourceMode;
68     uint32_t type;
69     name_ = parcel.ReadString();
70     bool res1 = parcel.ReadUint64(id_) &&
71         parcel.ReadUint32(virtualWidth_) && parcel.ReadUint32(virtualHeight_) &&
72         parcel.ReadFloat(virtualPixelRatio_) && parcel.ReadUint64(lastParent_) && parcel.ReadUint64(parent_) &&
73         parcel.ReadBool(isScreenGroup_) && parcel.ReadUint32(rotation) &&
74         parcel.ReadUint32(orientation) && parcel.ReadUint32(sourceMode) && parcel.ReadUint32(type) &&
75         parcel.ReadUint32(modeId_) && parcel.ReadUint32(size) &&
76         parcel.ReadBool(isExtend_);
77     if (!res1) {
78         return false;
79     }
80     if (size > MAX_SUPPORTED_SCREEN_MODES_SIZE) {
81         return false;
82     }
83     modes_.clear();
84     for (uint32_t modeIndex = 0; modeIndex < size; modeIndex++) {
85         sptr<SupportedScreenModes> mode = new(std::nothrow) SupportedScreenModes();
86         if (mode == nullptr) {
87             return false;
88         }
89         if (parcel.ReadUint32(mode->id_) &&
90             parcel.ReadUint32(mode->height_) &&
91             parcel.ReadUint32(mode->width_) &&
92             parcel.ReadUint32(mode->refreshRate_)) {
93             modes_.push_back(mode);
94         } else {
95             return false;
96         }
97     }
98     rotation_ = static_cast<Rotation>(rotation);
99     orientation_ = static_cast<Orientation>(orientation);
100     sourceMode_ = static_cast<ScreenSourceMode>(sourceMode);
101     type_ = static_cast<ScreenType>(type);
102     return true;
103 }
104 } // namespace OHOS::Rosen