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(std::vector<ScreenHDRFormat> formats)62 void RSScreenHDRCapability::SetHdrFormats(std::vector<ScreenHDRFormat> formats)
63 {
64 hdrFormats_ = std::move(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 ROSEN_LOGE("RSScreenHDRCapability::WriteVector WriteUint32 1 failed");
71 return false;
72 }
73 for (ScreenHDRFormat format : formats) {
74 if (!parcel.WriteUint32(static_cast<uint32_t>(format))) {
75 ROSEN_LOGE("RSScreenHDRCapability::WriteVector WriteUint32 2 failed");
76 return false;
77 }
78 }
79 return true;
80 }
81
ReadVector(std::vector<ScreenHDRFormat> & unmarFormats,Parcel & parcel)82 bool RSScreenHDRCapability::ReadVector(std::vector<ScreenHDRFormat>& unmarFormats, Parcel &parcel)
83 {
84 uint32_t size;
85 if (!parcel.ReadUint32(size)) {
86 ROSEN_LOGE("RSScreenHDRCapability::ReadVector ReadUint32 1 failed");
87 return false;
88 }
89 size_t readableSize = parcel.GetReadableBytes() / sizeof(ScreenHDRFormat);
90 size_t len = static_cast<size_t>(size);
91 if (len > readableSize || len > unmarFormats.max_size()) {
92 RS_LOGE("RSScreenHDRCapability ReadVector Failed to read vector, size:%{public}zu,"
93 " readableSize:%{public}zu", len, readableSize);
94 return false;
95 }
96 for (uint32_t index = 0; index < size; index++) {
97 uint32_t format;
98 if (!parcel.ReadUint32(format)) {
99 ROSEN_LOGE("RSScreenHDRCapability::ReadVector ReadUint32 2 failed");
100 return false;
101 }
102 unmarFormats.push_back(static_cast<ScreenHDRFormat>(format));
103 }
104 return true;
105 }
106
Marshalling(Parcel & parcel) const107 bool RSScreenHDRCapability::Marshalling(Parcel &parcel) const
108 {
109 if (!parcel.WriteFloat(maxLum_)) {
110 ROSEN_LOGE("RSScreenHDRCapability::Marshalling WriteFloat 1 failed");
111 return false;
112 }
113 if (!parcel.WriteFloat(minLum_)) {
114 ROSEN_LOGE("RSScreenHDRCapability::Marshalling WriteFloat 2 failed");
115 return false;
116 }
117 if (!parcel.WriteFloat(maxAverageLum_)) {
118 ROSEN_LOGE("RSScreenHDRCapability::Marshalling WriteFloat 3 failed");
119 return false;
120 }
121 if (!WriteVector(hdrFormats_, parcel)) {
122 ROSEN_LOGE("RSScreenHDRCapability::Marshalling WriteVector failed");
123 return false;
124 }
125 return true;
126 }
127
Unmarshalling(Parcel & parcel)128 RSScreenHDRCapability* RSScreenHDRCapability::Unmarshalling(Parcel &parcel)
129 {
130 float maxLum;
131 float minLum;
132 float maxAverageLum;
133 std::vector<ScreenHDRFormat> formats;
134 if (!parcel.ReadFloat(maxLum)) {
135 ROSEN_LOGE("RSScreenHDRCapability::Unmarshalling ReadFloat 1 failed");
136 return nullptr;
137 }
138 if (!parcel.ReadFloat(minLum)) {
139 ROSEN_LOGE("RSScreenHDRCapability::Unmarshalling ReadFloat 2 failed");
140 return nullptr;
141 }
142 if (!parcel.ReadFloat(maxAverageLum)) {
143 ROSEN_LOGE("RSScreenHDRCapability::Unmarshalling ReadFloat 3 failed");
144 return nullptr;
145 }
146 if (!ReadVector(formats, parcel)) {
147 ROSEN_LOGE("RSScreenHDRCapability::Unmarshalling ReadVector failed");
148 return nullptr;
149 }
150 RSScreenHDRCapability* screenHdrCapability = new RSScreenHDRCapability(maxLum, minLum, maxAverageLum, formats);
151 return screenHdrCapability;
152 }
153 } // namespace Rosen
154 } // namespace OHOS