1 /*
2  * Copyright (c) 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_manager/rs_screen_hdr_capability.h"
17 #include "platform/common/rs_log.h"
18 
19 namespace OHOS {
20 namespace Rosen {
RSScreenHDRCapability(float maxLum,float minLum,float maxAverageLum,const std::vector<ScreenHDRFormat> & formats)21 RSScreenHDRCapability::RSScreenHDRCapability(float maxLum, float minLum, float maxAverageLum,
22     const std::vector<ScreenHDRFormat>& formats) : maxLum_(maxLum), minLum_(minLum),
23     maxAverageLum_(maxAverageLum), hdrFormats_(formats)
24 {
25 }
26 
GetMaxLum() const27 float RSScreenHDRCapability::GetMaxLum() const
28 {
29     return maxLum_;
30 }
31 
GetMinLum() const32 float RSScreenHDRCapability::GetMinLum() const
33 {
34     return minLum_;
35 }
36 
GetMaxAverageLum() const37 float RSScreenHDRCapability::GetMaxAverageLum() const
38 {
39     return maxAverageLum_;
40 }
41 
GetHdrFormats() const42 const std::vector<ScreenHDRFormat>& RSScreenHDRCapability::GetHdrFormats() const
43 {
44     return hdrFormats_;
45 }
46 
SetMaxLum(float maxLum)47 void RSScreenHDRCapability::SetMaxLum(float maxLum)
48 {
49     maxLum_ = maxLum;
50 }
51 
SetMinLum(float minLum)52 void RSScreenHDRCapability::SetMinLum(float minLum)
53 {
54     minLum_ = minLum;
55 }
56 
SetMaxAverageLum(float maxAverageLum)57 void RSScreenHDRCapability::SetMaxAverageLum(float maxAverageLum)
58 {
59     maxAverageLum_ = maxAverageLum;
60 }
61 
SetHdrFormats(const std::vector<ScreenHDRFormat> & formats)62 void RSScreenHDRCapability::SetHdrFormats(const std::vector<ScreenHDRFormat>& formats)
63 {
64     hdrFormats_ = formats;
65 }
66 
WriteVector(const std::vector<ScreenHDRFormat> & formats,Parcel & parcel) const67 bool RSScreenHDRCapability::WriteVector(const std::vector<ScreenHDRFormat>& formats, Parcel &parcel) const
68 {
69     if (!parcel.WriteUint32(static_cast<uint32_t>(formats.size()))) {
70         return false;
71     }
72     for (ScreenHDRFormat format : formats) {
73         if (!parcel.WriteUint32(static_cast<uint32_t>(format))) {
74             return false;
75         }
76     }
77     return true;
78 }
79 
ReadVector(std::vector<ScreenHDRFormat> & unmarFormats,Parcel & parcel)80 bool RSScreenHDRCapability::ReadVector(std::vector<ScreenHDRFormat>& unmarFormats, Parcel &parcel)
81 {
82     uint32_t size;
83     if (!parcel.ReadUint32(size)) {
84         return false;
85     }
86     size_t readableSize = parcel.GetReadableBytes() / sizeof(ScreenHDRFormat);
87     size_t len = static_cast<size_t>(size);
88     if (len > readableSize || len > unmarFormats.max_size()) {
89         RS_LOGE("RSScreenHDRCapability ReadVector Failed to read vector, size:%{public}zu,"
90             " readableSize:%{public}zu", len, readableSize);
91         return false;
92     }
93     for (uint32_t index = 0; index < size; index++) {
94         uint32_t format;
95         if (!parcel.ReadUint32(format)) {
96             return false;
97         }
98         unmarFormats.push_back(static_cast<ScreenHDRFormat>(format));
99     }
100     return true;
101 }
102 
Marshalling(Parcel & parcel) const103 bool RSScreenHDRCapability::Marshalling(Parcel &parcel) const
104 {
105     if (!parcel.WriteFloat(maxLum_)) {
106         return false;
107     }
108     if (!parcel.WriteFloat(minLum_)) {
109         return false;
110     }
111     if (!parcel.WriteFloat(maxAverageLum_)) {
112         return false;
113     }
114     if (!WriteVector(hdrFormats_, parcel)) {
115         return false;
116     }
117     return true;
118 }
119 
Unmarshalling(Parcel & parcel)120 RSScreenHDRCapability* RSScreenHDRCapability::Unmarshalling(Parcel &parcel)
121 {
122     float maxLum;
123     float minLum;
124     float maxAverageLum;
125     std::vector<ScreenHDRFormat> formats;
126     if (!parcel.ReadFloat(maxLum)) {
127         return nullptr;
128     }
129     if (!parcel.ReadFloat(minLum)) {
130         return nullptr;
131     }
132     if (!parcel.ReadFloat(maxAverageLum)) {
133         return nullptr;
134     }
135     if (!ReadVector(formats, parcel)) {
136         return nullptr;
137     }
138     RSScreenHDRCapability* screenHdrCapability = new RSScreenHDRCapability(maxLum, minLum, maxAverageLum, formats);
139     return screenHdrCapability;
140 }
141 } // namespace Rosen
142 } // namespace OHOS