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