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 "modifier/rs_modifier_extractor.h"
17
18 #include <securec.h>
19
20 #include "modifier/rs_property_modifier.h"
21 #include "modifier/rs_modifier_type.h"
22 #include "pipeline/rs_node_map.h"
23 #include "ui/rs_node.h"
24
25 namespace OHOS {
26 namespace Rosen {
RSModifierExtractor(NodeId id)27 RSModifierExtractor::RSModifierExtractor(NodeId id) : id_(id) {}
28
29 #define GET_PROPERTY_FROM_MODIFIERS(T, propertyType, defaultValue, operator) \
30 do { \
31 auto node = RSNodeMap::Instance().GetNode<RSNode>(id_); \
32 if (!node) { \
33 return defaultValue; \
34 } \
35 auto iter = node->propertyModifiers_.find(RSModifierType::propertyType); \
36 if (iter != node->propertyModifiers_.end()) { \
37 return std::static_pointer_cast<RSProperty<T>>(iter->second->GetProperty())->Get(); \
38 } \
39 T value = defaultValue; \
40 for (auto& [_, modifier] : node->modifiers_) { \
41 if (modifier->GetModifierType() == RSModifierType::propertyType) { \
42 value operator std::static_pointer_cast<RSProperty<T>>(modifier->GetProperty())->Get(); \
43 } \
44 } \
45 return value; \
46 } while (0)
47
GetBounds() const48 Vector4f RSModifierExtractor::GetBounds() const
49 {
50 GET_PROPERTY_FROM_MODIFIERS(Vector4f, BOUNDS, Vector4f(), =);
51 }
52
GetFrame() const53 Vector4f RSModifierExtractor::GetFrame() const
54 {
55 GET_PROPERTY_FROM_MODIFIERS(Vector4f, FRAME, Vector4f(), =);
56 }
57
GetPositionZ() const58 float RSModifierExtractor::GetPositionZ() const
59 {
60 GET_PROPERTY_FROM_MODIFIERS(float, POSITION_Z, 0.f, +=);
61 }
62
GetPivot() const63 Vector2f RSModifierExtractor::GetPivot() const
64 {
65 GET_PROPERTY_FROM_MODIFIERS(Vector2f, PIVOT, Vector2f(0.5f, 0.5f), =);
66 }
67
GetQuaternion() const68 Quaternion RSModifierExtractor::GetQuaternion() const
69 {
70 GET_PROPERTY_FROM_MODIFIERS(Quaternion, QUATERNION, Quaternion(), =);
71 }
72
GetRotation() const73 float RSModifierExtractor::GetRotation() const
74 {
75 GET_PROPERTY_FROM_MODIFIERS(float, ROTATION, 0.f, +=);
76 }
77
GetRotationX() const78 float RSModifierExtractor::GetRotationX() const
79 {
80 GET_PROPERTY_FROM_MODIFIERS(float, ROTATION_X, 0.f, +=);
81 }
82
GetRotationY() const83 float RSModifierExtractor::GetRotationY() const
84 {
85 GET_PROPERTY_FROM_MODIFIERS(float, ROTATION_Y, 0.f, +=);
86 }
87
GetTranslate() const88 Vector2f RSModifierExtractor::GetTranslate() const
89 {
90 GET_PROPERTY_FROM_MODIFIERS(Vector2f, TRANSLATE, Vector2f(0.f, 0.f), +=);
91 }
92
GetTranslateZ() const93 float RSModifierExtractor::GetTranslateZ() const
94 {
95 GET_PROPERTY_FROM_MODIFIERS(float, TRANSLATE_Z, 0.f, +=);
96 }
97
GetScale() const98 Vector2f RSModifierExtractor::GetScale() const
99 {
100 GET_PROPERTY_FROM_MODIFIERS(Vector2f, SCALE, Vector2f(1.f, 1.f), *=);
101 }
102
GetAlpha() const103 float RSModifierExtractor::GetAlpha() const
104 {
105 GET_PROPERTY_FROM_MODIFIERS(float, ALPHA, 1.f, *=);
106 }
107
GetAlphaOffscreen() const108 bool RSModifierExtractor::GetAlphaOffscreen() const
109 {
110 GET_PROPERTY_FROM_MODIFIERS(bool, ALPHA_OFFSCREEN, true, =);
111 }
112
GetCornerRadius() const113 Vector4f RSModifierExtractor::GetCornerRadius() const
114 {
115 GET_PROPERTY_FROM_MODIFIERS(Vector4f, CORNER_RADIUS, 0.f, =);
116 }
117
GetForegroundColor() const118 Color RSModifierExtractor::GetForegroundColor() const
119 {
120 GET_PROPERTY_FROM_MODIFIERS(Color, FOREGROUND_COLOR, RgbPalette::Transparent(), =);
121 }
122
GetBackgroundColor() const123 Color RSModifierExtractor::GetBackgroundColor() const
124 {
125 GET_PROPERTY_FROM_MODIFIERS(Color, BACKGROUND_COLOR, RgbPalette::Transparent(), =);
126 }
127
GetSurfaceBgColor() const128 Color RSModifierExtractor::GetSurfaceBgColor() const
129 {
130 GET_PROPERTY_FROM_MODIFIERS(Color, SURFACE_BG_COLOR, RgbPalette::Transparent(), =);
131 }
132
GetBackgroundShader() const133 std::shared_ptr<RSShader> RSModifierExtractor::GetBackgroundShader() const
134 {
135 GET_PROPERTY_FROM_MODIFIERS(std::shared_ptr<RSShader>, BACKGROUND_SHADER, nullptr, =);
136 }
137
GetBgImage() const138 std::shared_ptr<RSImage> RSModifierExtractor::GetBgImage() const
139 {
140 GET_PROPERTY_FROM_MODIFIERS(std::shared_ptr<RSImage>, BG_IMAGE, nullptr, =);
141 }
142
GetBgImageWidth() const143 float RSModifierExtractor::GetBgImageWidth() const
144 {
145 GET_PROPERTY_FROM_MODIFIERS(float, BG_IMAGE_WIDTH, 0.f, =);
146 }
147
GetBgImageHeight() const148 float RSModifierExtractor::GetBgImageHeight() const
149 {
150 GET_PROPERTY_FROM_MODIFIERS(float, BG_IMAGE_HEIGHT, 0.f, =);
151 }
152
GetBgImagePositionX() const153 float RSModifierExtractor::GetBgImagePositionX() const
154 {
155 GET_PROPERTY_FROM_MODIFIERS(float, BG_IMAGE_POSITION_X, 0.f, =);
156 }
157
GetBgImagePositionY() const158 float RSModifierExtractor::GetBgImagePositionY() const
159 {
160 GET_PROPERTY_FROM_MODIFIERS(float, BG_IMAGE_POSITION_Y, 0.f, =);
161 }
162
GetBorderColor() const163 Vector4<Color> RSModifierExtractor::GetBorderColor() const
164 {
165 GET_PROPERTY_FROM_MODIFIERS(Vector4<Color>, BORDER_COLOR, Vector4<Color>(RgbPalette::Transparent()), =);
166 }
167
GetBorderWidth() const168 Vector4f RSModifierExtractor::GetBorderWidth() const
169 {
170 GET_PROPERTY_FROM_MODIFIERS(Vector4f, BORDER_WIDTH, Vector4f(0.f), =);
171 }
172
GetBorderStyle() const173 Vector4<uint32_t> RSModifierExtractor::GetBorderStyle() const
174 {
175 GET_PROPERTY_FROM_MODIFIERS(
176 Vector4<uint32_t>, BORDER_STYLE, Vector4<uint32_t>(static_cast<uint32_t>(BorderStyle::NONE)), =);
177 }
178
GetBackgroundFilter() const179 std::shared_ptr<RSFilter> RSModifierExtractor::GetBackgroundFilter() const
180 {
181 GET_PROPERTY_FROM_MODIFIERS(std::shared_ptr<RSFilter>, BACKGROUND_FILTER, nullptr, =);
182 }
183
GetFilter() const184 std::shared_ptr<RSFilter> RSModifierExtractor::GetFilter() const
185 {
186 GET_PROPERTY_FROM_MODIFIERS(std::shared_ptr<RSFilter>, FILTER, nullptr, =);
187 }
188
GetShadowColor() const189 Color RSModifierExtractor::GetShadowColor() const
190 {
191 GET_PROPERTY_FROM_MODIFIERS(Color, SHADOW_COLOR, Color::FromArgbInt(DEFAULT_SPOT_COLOR), =);
192 }
193
GetShadowOffsetX() const194 float RSModifierExtractor::GetShadowOffsetX() const
195 {
196 GET_PROPERTY_FROM_MODIFIERS(float, SHADOW_OFFSET_X, DEFAULT_SHADOW_OFFSET_X, =);
197 }
198
GetShadowOffsetY() const199 float RSModifierExtractor::GetShadowOffsetY() const
200 {
201 GET_PROPERTY_FROM_MODIFIERS(float, SHADOW_OFFSET_Y, DEFAULT_SHADOW_OFFSET_Y, =);
202 }
203
GetShadowAlpha() const204 float RSModifierExtractor::GetShadowAlpha() const
205 {
206 GET_PROPERTY_FROM_MODIFIERS(float, SHADOW_ALPHA, 0.f, =);
207 }
208
GetShadowElevation() const209 float RSModifierExtractor::GetShadowElevation() const
210 {
211 GET_PROPERTY_FROM_MODIFIERS(float, SHADOW_ELEVATION, 0.f, =);
212 }
213
GetShadowRadius() const214 float RSModifierExtractor::GetShadowRadius() const
215 {
216 GET_PROPERTY_FROM_MODIFIERS(float, SHADOW_RADIUS, DEFAULT_SHADOW_RADIUS, =);
217 }
218
GetShadowPath() const219 std::shared_ptr<RSPath> RSModifierExtractor::GetShadowPath() const
220 {
221 GET_PROPERTY_FROM_MODIFIERS(std::shared_ptr<RSPath>, SHADOW_PATH, nullptr, =);
222 }
223
GetFrameGravity() const224 Gravity RSModifierExtractor::GetFrameGravity() const
225 {
226 GET_PROPERTY_FROM_MODIFIERS(Gravity, FRAME_GRAVITY, Gravity::DEFAULT, =);
227 }
228
GetClipBounds() const229 std::shared_ptr<RSPath> RSModifierExtractor::GetClipBounds() const
230 {
231 GET_PROPERTY_FROM_MODIFIERS(std::shared_ptr<RSPath>, CLIP_BOUNDS, nullptr, =);
232 }
233
GetClipToBounds() const234 bool RSModifierExtractor::GetClipToBounds() const
235 {
236 GET_PROPERTY_FROM_MODIFIERS(bool, CLIP_TO_BOUNDS, false, =);
237 }
238
GetClipToFrame() const239 bool RSModifierExtractor::GetClipToFrame() const
240 {
241 GET_PROPERTY_FROM_MODIFIERS(bool, CLIP_TO_FRAME, false, =);
242 }
243
GetVisible() const244 bool RSModifierExtractor::GetVisible() const
245 {
246 GET_PROPERTY_FROM_MODIFIERS(bool, VISIBLE, true, =);
247 }
248
GetMask() const249 std::shared_ptr<RSMask> RSModifierExtractor::GetMask() const
250 {
251 GET_PROPERTY_FROM_MODIFIERS(std::shared_ptr<RSMask>, MASK, nullptr, =);
252 }
253
Dump() const254 std::string RSModifierExtractor::Dump() const
255 {
256 std::string dumpInfo;
257 char buffer[UINT8_MAX] = { 0 };
258 auto bounds = GetBounds();
259 auto frame = GetFrame();
260 if (sprintf_s(buffer, UINT8_MAX, "Bounds[%.1f %.1f %.1f %.1f] Frame[%.1f %.1f %.1f %.1f]",
261 bounds.x_, bounds.y_, bounds.z_, bounds.w_, frame.x_, frame.y_, frame.z_, frame.w_) != -1) {
262 dumpInfo.append(buffer);
263 }
264
265 auto ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
266 if (ret != EOK) {
267 return "Failed to memset_s for BackgroundColor, ret=" + std::to_string(ret);
268 }
269 if (!ROSEN_EQ(GetBackgroundColor(), RgbPalette::Transparent()) &&
270 sprintf_s(buffer, UINT8_MAX, ", BackgroundColor[#%08X]", GetBackgroundColor().AsArgbInt()) != -1) {
271 dumpInfo.append(buffer);
272 }
273
274 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
275 if (ret != EOK) {
276 return "Failed to memset_s for Alpha, ret=" + std::to_string(ret);
277 }
278 if (!ROSEN_EQ(GetAlpha(), 1.f) &&
279 sprintf_s(buffer, UINT8_MAX, ", Alpha[%.1f]", GetAlpha()) != -1) {
280 dumpInfo.append(buffer);
281 }
282
283 if (!GetVisible()) {
284 dumpInfo.append(", IsVisible[false]");
285 }
286 return dumpInfo;
287 }
288 } // namespace Rosen
289 } // namespace OHOS
290