1 /*
2 * Copyright (c) 2021-2023 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 "property/rs_properties.h"
17
18 #include <algorithm>
19 #include <securec.h>
20
21 #include "platform/common/rs_log.h"
22 #include "render/rs_filter.h"
23 #include "common/rs_obj_abs_geometry.h"
24
25 namespace OHOS {
26 namespace Rosen {
RSProperties()27 RSProperties::RSProperties()
28 {
29 boundsGeo_ = std::make_shared<RSObjAbsGeometry>();
30 frameGeo_ = std::make_shared<RSObjGeometry>();
31 }
32
~RSProperties()33 RSProperties::~RSProperties() {}
34
SetBounds(Vector4f bounds)35 void RSProperties::SetBounds(Vector4f bounds)
36 {
37 boundsGeo_->SetRect(bounds.x_, bounds.y_, bounds.z_, bounds.w_);
38 hasBounds_ = true;
39 geoDirty_ = true;
40 SetDirty();
41 }
42
SetBoundsSize(Vector2f size)43 void RSProperties::SetBoundsSize(Vector2f size)
44 {
45 boundsGeo_->SetSize(size.x_, size.y_);
46 hasBounds_ = true;
47 geoDirty_ = true;
48 SetDirty();
49 }
50
SetBoundsWidth(float width)51 void RSProperties::SetBoundsWidth(float width)
52 {
53 boundsGeo_->SetWidth(width);
54 hasBounds_ = true;
55 geoDirty_ = true;
56 SetDirty();
57 }
58
SetBoundsHeight(float height)59 void RSProperties::SetBoundsHeight(float height)
60 {
61 boundsGeo_->SetHeight(height);
62 hasBounds_ = true;
63 geoDirty_ = true;
64 SetDirty();
65 }
66
SetBoundsPosition(Vector2f position)67 void RSProperties::SetBoundsPosition(Vector2f position)
68 {
69 boundsGeo_->SetPosition(position.x_, position.y_);
70 geoDirty_ = true;
71 SetDirty();
72 }
73
SetBoundsPositionX(float positionX)74 void RSProperties::SetBoundsPositionX(float positionX)
75 {
76 boundsGeo_->SetX(positionX);
77 geoDirty_ = true;
78 SetDirty();
79 }
80
SetBoundsPositionY(float positionY)81 void RSProperties::SetBoundsPositionY(float positionY)
82 {
83 boundsGeo_->SetY(positionY);
84 geoDirty_ = true;
85 SetDirty();
86 }
87
GetBounds() const88 Vector4f RSProperties::GetBounds() const
89 {
90 return { boundsGeo_->GetX(), boundsGeo_->GetY(), boundsGeo_->GetWidth(), boundsGeo_->GetHeight() };
91 }
92
GetBoundsSize() const93 Vector2f RSProperties::GetBoundsSize() const
94 {
95 return { boundsGeo_->GetWidth(), boundsGeo_->GetHeight() };
96 }
97
GetBoundsWidth() const98 float RSProperties::GetBoundsWidth() const
99 {
100 return boundsGeo_->GetWidth();
101 }
102
GetBoundsHeight() const103 float RSProperties::GetBoundsHeight() const
104 {
105 return boundsGeo_->GetHeight();
106 }
107
GetBoundsPositionX() const108 float RSProperties::GetBoundsPositionX() const
109 {
110 return boundsGeo_->GetX();
111 }
112
GetBoundsPositionY() const113 float RSProperties::GetBoundsPositionY() const
114 {
115 return boundsGeo_->GetY();
116 }
117
GetBoundsPosition() const118 Vector2f RSProperties::GetBoundsPosition() const
119 {
120 return { GetBoundsPositionX(), GetBoundsPositionY() };
121 }
122
SetFrame(Vector4f frame)123 void RSProperties::SetFrame(Vector4f frame)
124 {
125 frameGeo_->SetRect(frame.x_, frame.y_, frame.z_, frame.w_);
126 geoDirty_ = true;
127 SetDirty();
128 }
129
SetFrameSize(Vector2f size)130 void RSProperties::SetFrameSize(Vector2f size)
131 {
132 frameGeo_->SetSize(size.x_, size.y_);
133 geoDirty_ = true;
134 SetDirty();
135 }
136
SetFrameWidth(float width)137 void RSProperties::SetFrameWidth(float width)
138 {
139 frameGeo_->SetWidth(width);
140 geoDirty_ = true;
141 SetDirty();
142 }
143
SetFrameHeight(float height)144 void RSProperties::SetFrameHeight(float height)
145 {
146 frameGeo_->SetHeight(height);
147 geoDirty_ = true;
148 SetDirty();
149 }
150
SetFramePosition(Vector2f position)151 void RSProperties::SetFramePosition(Vector2f position)
152 {
153 frameGeo_->SetPosition(position.x_, position.y_);
154 geoDirty_ = true;
155 SetDirty();
156 }
157
SetFramePositionX(float positionX)158 void RSProperties::SetFramePositionX(float positionX)
159 {
160 frameGeo_->SetX(positionX);
161 geoDirty_ = true;
162 SetDirty();
163 }
164
SetFramePositionY(float positionY)165 void RSProperties::SetFramePositionY(float positionY)
166 {
167 frameGeo_->SetY(positionY);
168 geoDirty_ = true;
169 SetDirty();
170 }
171
GetFrame() const172 Vector4f RSProperties::GetFrame() const
173 {
174 return { frameGeo_->GetX(), frameGeo_->GetY(), frameGeo_->GetWidth(), frameGeo_->GetHeight() };
175 }
176
GetFrameSize() const177 Vector2f RSProperties::GetFrameSize() const
178 {
179 return { frameGeo_->GetWidth(), frameGeo_->GetHeight() };
180 }
181
GetFrameWidth() const182 float RSProperties::GetFrameWidth() const
183 {
184 return frameGeo_->GetWidth();
185 }
186
GetFrameHeight() const187 float RSProperties::GetFrameHeight() const
188 {
189 return frameGeo_->GetHeight();
190 }
191
GetFramePositionX() const192 float RSProperties::GetFramePositionX() const
193 {
194 return frameGeo_->GetX();
195 }
196
GetFramePositionY() const197 float RSProperties::GetFramePositionY() const
198 {
199 return frameGeo_->GetY();
200 }
201
GetFramePosition() const202 Vector2f RSProperties::GetFramePosition() const
203 {
204 return { GetFramePositionX(), GetFramePositionY() };
205 }
206
GetFrameOffsetX() const207 float RSProperties::GetFrameOffsetX() const
208 {
209 return std::isinf(frameGeo_->GetX()) ? 0 : (frameGeo_->GetX() - boundsGeo_->GetX());
210 }
211
GetFrameOffsetY() const212 float RSProperties::GetFrameOffsetY() const
213 {
214 return std::isinf(frameGeo_->GetY()) ? 0 : (frameGeo_->GetY() - boundsGeo_->GetY());
215 }
216
GetBoundsGeometry() const217 const std::shared_ptr<RSObjGeometry>& RSProperties::GetBoundsGeometry() const
218 {
219 return boundsGeo_;
220 }
221
GetFrameGeometry() const222 const std::shared_ptr<RSObjGeometry>& RSProperties::GetFrameGeometry() const
223 {
224 return frameGeo_;
225 }
226
UpdateGeometry(const RSProperties * parent,bool dirtyFlag,Vector2f & offset)227 bool RSProperties::UpdateGeometry(const RSProperties* parent, bool dirtyFlag, Vector2f& offset)
228 {
229 if (boundsGeo_ == nullptr) {
230 return false;
231 }
232 CheckEmptyBounds();
233 auto boundsGeoPtr = std::static_pointer_cast<RSObjAbsGeometry>(boundsGeo_);
234
235 if (dirtyFlag || geoDirty_) {
236 auto parentGeo = parent == nullptr ? nullptr : std::static_pointer_cast<RSObjAbsGeometry>(parent->boundsGeo_);
237 boundsGeoPtr->UpdateMatrix(parentGeo, offset.x_, offset.y_);
238 }
239 return dirtyFlag || geoDirty_;
240 }
241
SetPositionZ(float positionZ)242 void RSProperties::SetPositionZ(float positionZ)
243 {
244 boundsGeo_->SetZ(positionZ);
245 frameGeo_->SetZ(positionZ);
246 geoDirty_ = true;
247 SetDirty();
248 }
249
GetPositionZ() const250 float RSProperties::GetPositionZ() const
251 {
252 return boundsGeo_->GetZ();
253 }
254
SetPivot(Vector2f pivot)255 void RSProperties::SetPivot(Vector2f pivot)
256 {
257 boundsGeo_->SetPivot(pivot.x_, pivot.y_);
258 geoDirty_ = true;
259 SetDirty();
260 }
261
SetPivotX(float pivotX)262 void RSProperties::SetPivotX(float pivotX)
263 {
264 boundsGeo_->SetPivotX(pivotX);
265 geoDirty_ = true;
266 SetDirty();
267 }
268
SetPivotY(float pivotY)269 void RSProperties::SetPivotY(float pivotY)
270 {
271 boundsGeo_->SetPivotY(pivotY);
272 geoDirty_ = true;
273 SetDirty();
274 }
275
GetPivot() const276 Vector2f RSProperties::GetPivot() const
277 {
278 return { boundsGeo_->GetPivotX(), boundsGeo_->GetPivotY() };
279 }
280
GetPivotX() const281 float RSProperties::GetPivotX() const
282 {
283 return boundsGeo_->GetPivotX();
284 }
285
GetPivotY() const286 float RSProperties::GetPivotY() const
287 {
288 return boundsGeo_->GetPivotY();
289 }
290
SetCornerRadius(Vector4f cornerRadius)291 void RSProperties::SetCornerRadius(Vector4f cornerRadius)
292 {
293 if (!cornerRadius_) {
294 cornerRadius_ = std::make_unique<Vector4f>();
295 }
296 cornerRadius_->SetValues(cornerRadius.x_, cornerRadius.y_, cornerRadius.z_, cornerRadius.w_);
297 SetDirty();
298 }
299
GetCornerRadius() const300 Vector4f RSProperties::GetCornerRadius() const
301 {
302 return cornerRadius_ ? *cornerRadius_ : Vector4f();
303 }
304
SetQuaternion(Quaternion quaternion)305 void RSProperties::SetQuaternion(Quaternion quaternion)
306 {
307 boundsGeo_->SetQuaternion(quaternion);
308 geoDirty_ = true;
309 SetDirty();
310 }
311
SetRotation(float degree)312 void RSProperties::SetRotation(float degree)
313 {
314 boundsGeo_->SetRotation(degree);
315 geoDirty_ = true;
316 SetDirty();
317 }
318
SetRotationX(float degree)319 void RSProperties::SetRotationX(float degree)
320 {
321 boundsGeo_->SetRotationX(degree);
322 geoDirty_ = true;
323 SetDirty();
324 }
325
SetRotationY(float degree)326 void RSProperties::SetRotationY(float degree)
327 {
328 boundsGeo_->SetRotationY(degree);
329 geoDirty_ = true;
330 SetDirty();
331 }
332
SetScale(Vector2f scale)333 void RSProperties::SetScale(Vector2f scale)
334 {
335 boundsGeo_->SetScale(scale.x_, scale.y_);
336 geoDirty_ = true;
337 SetDirty();
338 }
339
SetScaleX(float sx)340 void RSProperties::SetScaleX(float sx)
341 {
342 boundsGeo_->SetScaleX(sx);
343 geoDirty_ = true;
344 SetDirty();
345 }
346
SetScaleY(float sy)347 void RSProperties::SetScaleY(float sy)
348 {
349 boundsGeo_->SetScaleY(sy);
350 geoDirty_ = true;
351 SetDirty();
352 }
353
SetTranslate(Vector2f translate)354 void RSProperties::SetTranslate(Vector2f translate)
355 {
356 boundsGeo_->SetTranslateX(translate[0]);
357 boundsGeo_->SetTranslateY(translate[1]);
358 geoDirty_ = true;
359 SetDirty();
360 }
361
SetTranslateX(float translate)362 void RSProperties::SetTranslateX(float translate)
363 {
364 boundsGeo_->SetTranslateX(translate);
365 geoDirty_ = true;
366 SetDirty();
367 }
368
SetTranslateY(float translate)369 void RSProperties::SetTranslateY(float translate)
370 {
371 boundsGeo_->SetTranslateY(translate);
372 geoDirty_ = true;
373 SetDirty();
374 }
375
SetTranslateZ(float translate)376 void RSProperties::SetTranslateZ(float translate)
377 {
378 boundsGeo_->SetTranslateZ(translate);
379 geoDirty_ = true;
380 SetDirty();
381 }
382
GetQuaternion() const383 Quaternion RSProperties::GetQuaternion() const
384 {
385 return boundsGeo_->GetQuaternion();
386 }
387
GetRotation() const388 float RSProperties::GetRotation() const
389 {
390 return boundsGeo_->GetRotation();
391 }
392
GetRotationX() const393 float RSProperties::GetRotationX() const
394 {
395 return boundsGeo_->GetRotationX();
396 }
397
GetRotationY() const398 float RSProperties::GetRotationY() const
399 {
400 return boundsGeo_->GetRotationY();
401 }
402
GetScaleX() const403 float RSProperties::GetScaleX() const
404 {
405 return boundsGeo_->GetScaleX();
406 }
407
GetScaleY() const408 float RSProperties::GetScaleY() const
409 {
410 return boundsGeo_->GetScaleY();
411 }
412
GetScale() const413 Vector2f RSProperties::GetScale() const
414 {
415 return { boundsGeo_->GetScaleX(), boundsGeo_->GetScaleY() };
416 }
417
GetTranslate() const418 Vector2f RSProperties::GetTranslate() const
419 {
420 return Vector2f(GetTranslateX(), GetTranslateY());
421 }
422
GetTranslateX() const423 float RSProperties::GetTranslateX() const
424 {
425 return boundsGeo_->GetTranslateX();
426 }
427
GetTranslateY() const428 float RSProperties::GetTranslateY() const
429 {
430 return boundsGeo_->GetTranslateY();
431 }
432
GetTranslateZ() const433 float RSProperties::GetTranslateZ() const
434 {
435 return boundsGeo_->GetTranslateZ();
436 }
437
SetAlpha(float alpha)438 void RSProperties::SetAlpha(float alpha)
439 {
440 alpha_ = alpha;
441 SetDirty();
442 }
443
GetAlpha() const444 float RSProperties::GetAlpha() const
445 {
446 return alpha_;
447 }
SetAlphaOffscreen(bool alphaOffscreen)448 void RSProperties::SetAlphaOffscreen(bool alphaOffscreen)
449 {
450 alphaOffscreen_ = alphaOffscreen;
451 SetDirty();
452 }
453
GetAlphaOffscreen() const454 bool RSProperties::GetAlphaOffscreen() const
455 {
456 return alphaOffscreen_;
457 }
458
SetSublayerTransform(Matrix3f sublayerTransform)459 void RSProperties::SetSublayerTransform(Matrix3f sublayerTransform)
460 {
461 if (sublayerTransform_) {
462 *sublayerTransform_ = sublayerTransform;
463 } else {
464 sublayerTransform_ = std::make_unique<Matrix3f>(sublayerTransform);
465 }
466 SetDirty();
467 }
468
GetSublayerTransform() const469 Matrix3f RSProperties::GetSublayerTransform() const
470 {
471 return (sublayerTransform_ != nullptr) ? *sublayerTransform_ : Matrix3f::IDENTITY;
472 }
473
474 // foreground properties
SetForegroundColor(Color color)475 void RSProperties::SetForegroundColor(Color color)
476 {
477 if (!decoration_) {
478 decoration_ = std::make_unique<Decoration>();
479 }
480 decoration_->foregroundColor_ = color;
481 SetDirty();
482 }
483
GetForegroundColor() const484 Color RSProperties::GetForegroundColor() const
485 {
486 return decoration_ ? decoration_->foregroundColor_ : RgbPalette::Transparent();
487 }
488
489 // background properties
SetBackgroundColor(Color color)490 void RSProperties::SetBackgroundColor(Color color)
491 {
492 if (!decoration_) {
493 decoration_ = std::make_unique<Decoration>();
494 }
495 decoration_->backgroundColor_ = color;
496 SetDirty();
497 }
498
GetBackgroundColor() const499 Color RSProperties::GetBackgroundColor() const
500 {
501 return decoration_ ? decoration_->backgroundColor_ : RgbPalette::Transparent();
502 }
503
SetBackgroundShader(std::shared_ptr<RSShader> shader)504 void RSProperties::SetBackgroundShader(std::shared_ptr<RSShader> shader)
505 {
506 if (!decoration_) {
507 decoration_ = std::make_unique<Decoration>();
508 }
509 decoration_->bgShader_ = shader;
510 SetDirty();
511 }
512
GetBackgroundShader() const513 std::shared_ptr<RSShader> RSProperties::GetBackgroundShader() const
514 {
515 return decoration_ ? decoration_->bgShader_ : nullptr;
516 }
517
SetBgImage(std::shared_ptr<RSImage> image)518 void RSProperties::SetBgImage(std::shared_ptr<RSImage> image)
519 {
520 if (!decoration_) {
521 decoration_ = std::make_unique<Decoration>();
522 }
523 decoration_->bgImage_ = image;
524 SetDirty();
525 }
526
GetBgImage() const527 std::shared_ptr<RSImage> RSProperties::GetBgImage() const
528 {
529 return decoration_ ? decoration_->bgImage_ : nullptr;
530 }
531
SetBgImageWidth(float width)532 void RSProperties::SetBgImageWidth(float width)
533 {
534 if (!decoration_) {
535 decoration_ = std::make_unique<Decoration>();
536 }
537 decoration_->bgImageRect_.width_ = width;
538 SetDirty();
539 }
540
SetBgImageHeight(float height)541 void RSProperties::SetBgImageHeight(float height)
542 {
543 if (!decoration_) {
544 decoration_ = std::make_unique<Decoration>();
545 }
546 decoration_->bgImageRect_.height_ = height;
547 SetDirty();
548 }
549
SetBgImagePositionX(float positionX)550 void RSProperties::SetBgImagePositionX(float positionX)
551 {
552 if (!decoration_) {
553 decoration_ = std::make_unique<Decoration>();
554 }
555 decoration_->bgImageRect_.left_ = positionX;
556 SetDirty();
557 }
558
SetBgImagePositionY(float positionY)559 void RSProperties::SetBgImagePositionY(float positionY)
560 {
561 if (!decoration_) {
562 decoration_ = std::make_unique<Decoration>();
563 }
564 decoration_->bgImageRect_.top_ = positionY;
565 SetDirty();
566 }
567
GetBgImageWidth() const568 float RSProperties::GetBgImageWidth() const
569 {
570 return decoration_ ? decoration_->bgImageRect_.width_ : 0.f;
571 }
572
GetBgImageHeight() const573 float RSProperties::GetBgImageHeight() const
574 {
575 return decoration_ ? decoration_->bgImageRect_.height_ : 0.f;
576 }
577
GetBgImagePositionX() const578 float RSProperties::GetBgImagePositionX() const
579 {
580 return decoration_ ? decoration_->bgImageRect_.left_ : 0.f;
581 }
582
GetBgImagePositionY() const583 float RSProperties::GetBgImagePositionY() const
584 {
585 return decoration_ ? decoration_->bgImageRect_.top_ : 0.f;
586 }
587
588 // border properties
SetBorderColor(Vector4<Color> color)589 void RSProperties::SetBorderColor(Vector4<Color> color)
590 {
591 if (!border_) {
592 border_ = std::make_shared<RSBorder>();
593 }
594 border_->SetColorFour(color);
595 SetDirty();
596 }
597
SetBorderWidth(Vector4f width)598 void RSProperties::SetBorderWidth(Vector4f width)
599 {
600 if (!border_) {
601 border_ = std::make_shared<RSBorder>();
602 }
603 border_->SetWidthFour(width);
604 SetDirty();
605 }
606
SetBorderStyle(Vector4<uint32_t> style)607 void RSProperties::SetBorderStyle(Vector4<uint32_t> style)
608 {
609 if (!border_) {
610 border_ = std::make_shared<RSBorder>();
611 }
612 border_->SetStyleFour(style);
613 SetDirty();
614 }
615
GetBorderColor() const616 Vector4<Color> RSProperties::GetBorderColor() const
617 {
618 return border_ ? border_->GetColorFour() : Vector4<Color>(RgbPalette::Transparent());
619 }
620
GetBorderWidth() const621 Vector4f RSProperties::GetBorderWidth() const
622 {
623 return border_ ? border_->GetWidthFour() : Vector4f(0.f);
624 }
625
GetBorderStyle() const626 Vector4<uint32_t> RSProperties::GetBorderStyle() const
627 {
628 return border_ ? border_->GetStyleFour() : Vector4<uint32_t>(static_cast<uint32_t>(BorderStyle::NONE));
629 }
630
GetBorder() const631 std::shared_ptr<RSBorder> RSProperties::GetBorder() const
632 {
633 return border_;
634 }
635
SetBackgroundFilter(std::shared_ptr<RSFilter> backgroundFilter)636 void RSProperties::SetBackgroundFilter(std::shared_ptr<RSFilter> backgroundFilter)
637 {
638 backgroundFilter_ = backgroundFilter;
639 SetDirty();
640 }
641
SetFilter(std::shared_ptr<RSFilter> filter)642 void RSProperties::SetFilter(std::shared_ptr<RSFilter> filter)
643 {
644 filter_ = filter;
645 SetDirty();
646 }
647
GetBackgroundFilter() const648 std::shared_ptr<RSFilter> RSProperties::GetBackgroundFilter() const
649 {
650 return backgroundFilter_;
651 }
652
GetFilter() const653 std::shared_ptr<RSFilter> RSProperties::GetFilter() const
654 {
655 return filter_;
656 }
657
658 // shadow properties
SetShadowColor(Color color)659 void RSProperties::SetShadowColor(Color color)
660 {
661 if (shadow_ == nullptr) {
662 shadow_ = std::make_unique<RSShadow>();
663 }
664 shadow_->SetColor(color);
665 SetDirty();
666 }
667
SetShadowOffsetX(float offsetX)668 void RSProperties::SetShadowOffsetX(float offsetX)
669 {
670 if (shadow_ == nullptr) {
671 shadow_ = std::make_unique<RSShadow>();
672 }
673 shadow_->SetOffsetX(offsetX);
674 SetDirty();
675 }
676
SetShadowOffsetY(float offsetY)677 void RSProperties::SetShadowOffsetY(float offsetY)
678 {
679 if (shadow_ == nullptr) {
680 shadow_ = std::make_unique<RSShadow>();
681 }
682 shadow_->SetOffsetY(offsetY);
683 SetDirty();
684 }
685
SetShadowAlpha(float alpha)686 void RSProperties::SetShadowAlpha(float alpha)
687 {
688 if (shadow_ == nullptr) {
689 shadow_ = std::make_unique<RSShadow>();
690 }
691 shadow_->SetAlpha(alpha);
692 SetDirty();
693 }
694
SetShadowElevation(float elevation)695 void RSProperties::SetShadowElevation(float elevation)
696 {
697 if (shadow_ == nullptr) {
698 shadow_ = std::make_unique<RSShadow>();
699 }
700 shadow_->SetElevation(elevation);
701 SetDirty();
702 }
703
SetShadowRadius(float radius)704 void RSProperties::SetShadowRadius(float radius)
705 {
706 if (shadow_ == nullptr) {
707 shadow_ = std::make_unique<RSShadow>();
708 }
709 shadow_->SetRadius(radius);
710 SetDirty();
711 }
712
SetShadowPath(std::shared_ptr<RSPath> shadowPath)713 void RSProperties::SetShadowPath(std::shared_ptr<RSPath> shadowPath)
714 {
715 if (shadow_ == nullptr) {
716 shadow_ = std::make_unique<RSShadow>();
717 }
718 shadow_->SetPath(shadowPath);
719 SetDirty();
720 }
721
GetShadowColor() const722 Color RSProperties::GetShadowColor() const
723 {
724 return shadow_ ? shadow_->GetColor() : Color::FromArgbInt(DEFAULT_SPOT_COLOR);
725 }
726
GetShadowOffsetX() const727 float RSProperties::GetShadowOffsetX() const
728 {
729 return shadow_ ? shadow_->GetOffsetX() : DEFAULT_SHADOW_OFFSET_X;
730 }
731
GetShadowOffsetY() const732 float RSProperties::GetShadowOffsetY() const
733 {
734 return shadow_ ? shadow_->GetOffsetY() : DEFAULT_SHADOW_OFFSET_Y;
735 }
736
GetShadowAlpha() const737 float RSProperties::GetShadowAlpha() const
738 {
739 return shadow_ ? shadow_->GetAlpha() : 0.f;
740 }
741
GetShadowElevation() const742 float RSProperties::GetShadowElevation() const
743 {
744 return shadow_ ? shadow_->GetElevation() : 0.f;
745 }
746
GetShadowRadius() const747 float RSProperties::GetShadowRadius() const
748 {
749 return shadow_ ? shadow_->GetRadius() : DEFAULT_SHADOW_RADIUS;
750 }
751
GetShadowPath() const752 std::shared_ptr<RSPath> RSProperties::GetShadowPath() const
753 {
754 return shadow_ ? shadow_->GetPath() : nullptr;
755 }
756
IsShadowValid() const757 bool RSProperties::IsShadowValid() const
758 {
759 return shadow_ && shadow_->IsValid();
760 }
761
SetFrameGravity(Gravity gravity)762 void RSProperties::SetFrameGravity(Gravity gravity)
763 {
764 if (frameGravity_ != gravity) {
765 frameGravity_ = gravity;
766 SetDirty();
767 }
768 }
769
GetFrameGravity() const770 Gravity RSProperties::GetFrameGravity() const
771 {
772 return frameGravity_;
773 }
774
SetOverlayBounds(std::shared_ptr<RectI> rect)775 void RSProperties::SetOverlayBounds(std::shared_ptr<RectI> rect)
776 {
777 overlayRect_ = rect;
778 }
779
GetOverlayBounds() const780 std::shared_ptr<RectI> RSProperties::GetOverlayBounds() const
781 {
782 return overlayRect_;
783 }
784
SetClipBounds(std::shared_ptr<RSPath> path)785 void RSProperties::SetClipBounds(std::shared_ptr<RSPath> path)
786 {
787 if (clipPath_ != path) {
788 clipPath_ = path;
789 SetDirty();
790 }
791 }
792
GetClipBounds() const793 std::shared_ptr<RSPath> RSProperties::GetClipBounds() const
794 {
795 return clipPath_;
796 }
797
SetClipToBounds(bool clipToBounds)798 void RSProperties::SetClipToBounds(bool clipToBounds)
799 {
800 if (clipToBounds_ != clipToBounds) {
801 clipToBounds_ = clipToBounds;
802 SetDirty();
803 }
804 }
805
GetClipToBounds() const806 bool RSProperties::GetClipToBounds() const
807 {
808 return clipToBounds_;
809 }
810
SetClipToFrame(bool clipToFrame)811 void RSProperties::SetClipToFrame(bool clipToFrame)
812 {
813 if (clipToFrame_ != clipToFrame) {
814 clipToFrame_ = clipToFrame;
815 SetDirty();
816 }
817 }
818
GetClipToFrame() const819 bool RSProperties::GetClipToFrame() const
820 {
821 return clipToFrame_;
822 }
823
GetBoundsRect() const824 RectF RSProperties::GetBoundsRect() const
825 {
826 if (boundsGeo_->IsEmpty()) {
827 return RectF(0, 0, GetFrameWidth(), GetFrameHeight());
828 } else {
829 return RectF(0, 0, GetBoundsWidth(), GetBoundsHeight());
830 }
831 }
832
GetFrameRect() const833 RectF RSProperties::GetFrameRect() const
834 {
835 return RectF(0, 0, GetFrameWidth(), GetFrameHeight());
836 }
837
GetBgImageRect() const838 RectF RSProperties::GetBgImageRect() const
839 {
840 return decoration_ ? decoration_->bgImageRect_ : RectF();
841 }
842
SetVisible(bool visible)843 void RSProperties::SetVisible(bool visible)
844 {
845 if (visible_ != visible) {
846 visible_ = visible;
847 SetDirty();
848 }
849 }
850
GetVisible() const851 bool RSProperties::GetVisible() const
852 {
853 return visible_;
854 }
855
GetRRect() const856 RRect RSProperties::GetRRect() const
857 {
858 RectF rect = GetBoundsRect();
859 RRect rrect = RRect(rect, GetCornerRadius());
860 return rrect;
861 }
862
GetInnerRRect() const863 RRect RSProperties::GetInnerRRect() const
864 {
865 auto rect = GetBoundsRect();
866 Vector4f cornerRadius = GetCornerRadius();
867 if (border_) {
868 rect.left_ += border_->GetWidth(RSBorder::LEFT);
869 rect.top_ += border_->GetWidth(RSBorder::TOP);
870 rect.width_ -= border_->GetWidth(RSBorder::LEFT) + border_->GetWidth(RSBorder::RIGHT);
871 rect.height_ -= border_->GetWidth(RSBorder::TOP) + border_->GetWidth(RSBorder::BOTTOM);
872 cornerRadius = cornerRadius - GetBorderWidth();
873 }
874 RRect rrect = RRect(rect, cornerRadius);
875 return rrect;
876 }
877
NeedFilter() const878 bool RSProperties::NeedFilter() const
879 {
880 return (backgroundFilter_ != nullptr) || (filter_ != nullptr);
881 }
882
NeedClip() const883 bool RSProperties::NeedClip() const
884 {
885 return clipToBounds_ || clipToFrame_;
886 }
887
Reset()888 void RSProperties::Reset()
889 {
890 alphaOffscreen_ = false;
891 isDirty_ = true;
892 hasBounds_ = false;
893 isDirty_ = true;
894 visible_ = true;
895 clipToBounds_ = false;
896 clipToFrame_ = false;
897
898 frameGravity_ = Gravity::DEFAULT;
899 alpha_ = 1.f;
900
901 boundsGeo_ = std::make_shared<RSObjAbsGeometry>();
902 frameGeo_ = std::make_shared<RSObjGeometry>();
903
904 backgroundFilter_ = nullptr;
905 border_ = nullptr;
906 clipPath_ = nullptr;
907 cornerRadius_ = nullptr;
908 decoration_ = nullptr;
909 filter_ = nullptr;
910 mask_ = nullptr;
911 shadow_ = nullptr;
912 sublayerTransform_ = nullptr;
913 }
914
SetDirty()915 void RSProperties::SetDirty()
916 {
917 isDirty_ = true;
918 }
919
ResetDirty()920 void RSProperties::ResetDirty()
921 {
922 isDirty_ = false;
923 geoDirty_ = false;
924 }
925
IsDirty() const926 bool RSProperties::IsDirty() const
927 {
928 return isDirty_;
929 }
930
IsGeoDirty() const931 bool RSProperties::IsGeoDirty() const
932 {
933 return geoDirty_;
934 }
935
GetDirtyRect() const936 RectI RSProperties::GetDirtyRect() const
937 {
938 RectI dirtyRect;
939 auto boundsGeometry = std::static_pointer_cast<RSObjAbsGeometry>(boundsGeo_);
940 if (clipToBounds_ || std::isinf(GetFrameWidth()) || std::isinf(GetFrameHeight())) {
941 dirtyRect = boundsGeometry->GetAbsRect();
942 } else {
943 auto frameRect =
944 boundsGeometry->MapAbsRect(RectF(GetFrameOffsetX(), GetFrameOffsetY(), GetFrameWidth(), GetFrameHeight()));
945 dirtyRect = boundsGeometry->GetAbsRect().JoinRect(frameRect);
946 }
947 if (overlayRect_ == nullptr || overlayRect_->IsEmpty()) {
948 return dirtyRect;
949 } else {
950 auto overlayRect = boundsGeometry->MapAbsRect(overlayRect_->ConvertTo<float>());
951 // this is used to fix the scene with overlayRect problem, which is need to be optimized
952 overlayRect.SetRight(overlayRect.GetRight() + 1);
953 overlayRect.SetBottom(overlayRect.GetBottom() + 1);
954 overlayRect.SetAll(overlayRect.left_ - 1, overlayRect.top_ - 1,
955 overlayRect.width_ + 1, overlayRect.height_ + 1);
956 return dirtyRect.JoinRect(overlayRect);
957 }
958 }
959
CheckEmptyBounds()960 void RSProperties::CheckEmptyBounds()
961 {
962 // [planning] remove this func and fallback to framerect after surfacenode using frame
963 if (!hasBounds_) {
964 boundsGeo_->SetRect(frameGeo_->GetX(), frameGeo_->GetY(), frameGeo_->GetWidth(), frameGeo_->GetHeight());
965 }
966 }
967
ResetBounds()968 void RSProperties::ResetBounds()
969 {
970 if (!hasBounds_) {
971 boundsGeo_->SetRect(0.f, 0.f, 0.f, 0.f);
972 }
973 }
974
975 // mask properties
SetMask(std::shared_ptr<RSMask> mask)976 void RSProperties::SetMask(std::shared_ptr<RSMask> mask)
977 {
978 mask_ = mask;
979 SetDirty();
980 }
981
GetMask() const982 std::shared_ptr<RSMask> RSProperties::GetMask() const
983 {
984 return mask_;
985 }
986
Dump() const987 std::string RSProperties::Dump() const
988 {
989 std::string dumpInfo;
990 char buffer[UINT8_MAX] = { 0 };
991 if (sprintf_s(buffer, UINT8_MAX, "Bounds[%.1f %.1f %.1f %.1f] Frame[%.1f %.1f %.1f %.1f]",
992 GetBoundsPositionX(), GetBoundsPositionY(), GetBoundsWidth(), GetBoundsHeight(),
993 GetFramePositionX(), GetFramePositionY(), GetFrameWidth(), GetFrameHeight()) != -1) {
994 dumpInfo.append(buffer);
995 }
996
997 // PositionZ
998 auto ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
999 if (ret != EOK) {
1000 return "Failed to memset_s for PositionZ, ret=" + std::to_string(ret);
1001 }
1002 if (!ROSEN_EQ(GetPositionZ(), 0.f) &&
1003 sprintf_s(buffer, UINT8_MAX, ", PositionZ[%.1f]", GetPositionZ()) != -1) {
1004 dumpInfo.append(buffer);
1005 }
1006
1007 // Pivot
1008 std::unique_ptr<Transform> defaultTrans = std::make_unique<Transform>();
1009 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1010 if (ret != EOK) {
1011 return "Failed to memset_s for Pivot, ret=" + std::to_string(ret);
1012 }
1013 Vector2f pivot = GetPivot();
1014 if ((!ROSEN_EQ(pivot[0], defaultTrans->pivotX_) || !ROSEN_EQ(pivot[1], defaultTrans->pivotY_)) &&
1015 sprintf_s(buffer, UINT8_MAX, ", Pivot[%.1f,%.1f]", pivot[0], pivot[1]) != -1) {
1016 dumpInfo.append(buffer);
1017 }
1018
1019 // CornerRadius
1020 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1021 if (ret != EOK) {
1022 return "Failed to memset_s for CornerRadius, ret=" + std::to_string(ret);
1023 }
1024 if (!GetCornerRadius().IsZero() &&
1025 sprintf_s(buffer, UINT8_MAX, ", CornerRadius[%.1f %.1f %.1f %.1f]",
1026 GetCornerRadius().x_, GetCornerRadius().y_, GetCornerRadius().z_, GetCornerRadius().w_) != -1) {
1027 dumpInfo.append(buffer);
1028 }
1029
1030 // Rotation
1031 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1032 if (ret != EOK) {
1033 return "Failed to memset_s for Rotation, ret=" + std::to_string(ret);
1034 }
1035 if (!ROSEN_EQ(GetRotation(), defaultTrans->rotation_) &&
1036 sprintf_s(buffer, UINT8_MAX, ", Rotation[%.1f]", GetRotation()) != -1) {
1037 dumpInfo.append(buffer);
1038 }
1039 // RotationX
1040 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1041 if (ret != EOK) {
1042 return "Failed to memset_s for RotationX, ret=" + std::to_string(ret);
1043 }
1044 if (!ROSEN_EQ(GetRotationX(), defaultTrans->rotationX_) &&
1045 sprintf_s(buffer, UINT8_MAX, ", RotationX[%.1f]", GetRotationX()) != -1) {
1046 dumpInfo.append(buffer);
1047 }
1048 // RotationY
1049 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1050 if (ret != EOK) {
1051 return "Failed to memset_s for RotationY, ret=" + std::to_string(ret);
1052 }
1053 if (!ROSEN_EQ(GetRotationY(), defaultTrans->rotationY_) &&
1054 sprintf_s(buffer, UINT8_MAX, ", RotationY[%.1f]", GetRotationY()) != -1) {
1055 dumpInfo.append(buffer);
1056 }
1057
1058 // TranslateX
1059 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1060 if (ret != EOK) {
1061 return "Failed to memset_s for TranslateX, ret=" + std::to_string(ret);
1062 }
1063 if (!ROSEN_EQ(GetTranslateX(), defaultTrans->translateX_) &&
1064 sprintf_s(buffer, UINT8_MAX, ", TranslateX[%.1f]", GetTranslateX()) != -1) {
1065 dumpInfo.append(buffer);
1066 }
1067
1068 // TranslateY
1069 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1070 if (ret != EOK) {
1071 return "Failed to memset_s for TranslateY, ret=" + std::to_string(ret);
1072 }
1073 if (!ROSEN_EQ(GetTranslateY(), defaultTrans->translateY_) &&
1074 sprintf_s(buffer, UINT8_MAX, ", TranslateY[%.1f]", GetTranslateY()) != -1) {
1075 dumpInfo.append(buffer);
1076 }
1077
1078 // TranslateZ
1079 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1080 if (ret != EOK) {
1081 return "Failed to memset_s for TranslateZ, ret=" + std::to_string(ret);
1082 }
1083 if (!ROSEN_EQ(GetTranslateZ(), defaultTrans->translateZ_) &&
1084 sprintf_s(buffer, UINT8_MAX, ", TranslateZ[%.1f]", GetTranslateZ()) != -1) {
1085 dumpInfo.append(buffer);
1086 }
1087
1088 // ScaleX
1089 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1090 if (ret != EOK) {
1091 return "Failed to memset_s for ScaleX, ret=" + std::to_string(ret);
1092 }
1093 if (!ROSEN_EQ(GetScaleX(), defaultTrans->scaleX_) &&
1094 sprintf_s(buffer, UINT8_MAX, ", ScaleX[%.1f]", GetScaleX()) != -1) {
1095 dumpInfo.append(buffer);
1096 }
1097
1098 // ScaleY
1099 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1100 if (ret != EOK) {
1101 return "Failed to memset_s for ScaleY, ret=" + std::to_string(ret);
1102 }
1103 if (!ROSEN_EQ(GetScaleY(), defaultTrans->scaleY_) &&
1104 sprintf_s(buffer, UINT8_MAX, ", ScaleY[%.1f]", GetScaleY()) != -1) {
1105 dumpInfo.append(buffer);
1106 }
1107
1108 // Alpha
1109 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1110 if (ret != EOK) {
1111 return "Failed to memset_s for Alpha, ret=" + std::to_string(ret);
1112 }
1113 if (!ROSEN_EQ(GetAlpha(), 1.f) &&
1114 sprintf_s(buffer, UINT8_MAX, ", Alpha[%.1f]", GetAlpha()) != -1) {
1115 dumpInfo.append(buffer);
1116 }
1117
1118 // ForegroundColor
1119 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1120 if (ret != EOK) {
1121 return "Failed to memset_s for ForegroundColor, ret=" + std::to_string(ret);
1122 }
1123 if (!ROSEN_EQ(GetForegroundColor(), RgbPalette::Transparent()) &&
1124 sprintf_s(buffer, UINT8_MAX, ", ForegroundColor[#%08X]", GetForegroundColor().AsArgbInt()) != -1) {
1125 dumpInfo.append(buffer);
1126 }
1127
1128 // BackgroundColor
1129 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1130 if (ret != EOK) {
1131 return "Failed to memset_s for BackgroundColor, ret=" + std::to_string(ret);
1132 }
1133 if (!ROSEN_EQ(GetBackgroundColor(), RgbPalette::Transparent()) &&
1134 sprintf_s(buffer, UINT8_MAX, ", BackgroundColor[#%08X]", GetBackgroundColor().AsArgbInt()) != -1) {
1135 dumpInfo.append(buffer);
1136 }
1137
1138 // BgImage
1139 std::unique_ptr<Decoration> defaultDecoration = std::make_unique<Decoration>();
1140 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1141 if (ret != EOK) {
1142 return "Failed to memset_s for BgImage, ret=" + std::to_string(ret);
1143 }
1144 if ((!ROSEN_EQ(GetBgImagePositionX(), defaultDecoration->bgImageRect_.left_) ||
1145 !ROSEN_EQ(GetBgImagePositionY(), defaultDecoration->bgImageRect_.top_) ||
1146 !ROSEN_EQ(GetBgImageWidth(), defaultDecoration->bgImageRect_.width_) ||
1147 !ROSEN_EQ(GetBgImageHeight(), defaultDecoration->bgImageRect_.height_)) &&
1148 sprintf_s(buffer, UINT8_MAX, ", BgImage[%.1f %.1f %.1f %.1f]", GetBgImagePositionX(),
1149 GetBgImagePositionY(), GetBgImageWidth(), GetBgImageHeight()) != -1) {
1150 dumpInfo.append(buffer);
1151 }
1152
1153 // Border
1154 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1155 if (ret != EOK) {
1156 return "Failed to memset_s for Border, ret=" + std::to_string(ret);
1157 }
1158 if (border_ && border_->HasBorder() &&
1159 sprintf_s(buffer, UINT8_MAX, ", Border[%s]", border_->ToString().c_str()) != -1) {
1160 dumpInfo.append(buffer);
1161 }
1162
1163 // ShadowColor
1164 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1165 if (ret != EOK) {
1166 return "Failed to memset_s for ShadowColor, ret=" + std::to_string(ret);
1167 }
1168 if (!ROSEN_EQ(GetShadowColor(), Color(DEFAULT_SPOT_COLOR)) &&
1169 sprintf_s(buffer, UINT8_MAX, ", ShadowColor[#%08X]", GetShadowColor().AsArgbInt()) != -1) {
1170 dumpInfo.append(buffer);
1171 }
1172
1173 // ShadowOffsetX
1174 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1175 if (ret != EOK) {
1176 return "Failed to memset_s for ShadowOffsetX, ret=" + std::to_string(ret);
1177 }
1178 if (!ROSEN_EQ(GetShadowOffsetX(), DEFAULT_SHADOW_OFFSET_X) &&
1179 sprintf_s(buffer, UINT8_MAX, ", ShadowOffsetX[%.1f]", GetShadowOffsetX()) != -1) {
1180 dumpInfo.append(buffer);
1181 }
1182
1183 // ShadowOffsetY
1184 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1185 if (ret != EOK) {
1186 return "Failed to memset_s for ShadowOffsetY, ret=" + std::to_string(ret);
1187 }
1188 if (!ROSEN_EQ(GetShadowOffsetY(), DEFAULT_SHADOW_OFFSET_Y) &&
1189 sprintf_s(buffer, UINT8_MAX, ", ShadowOffsetY[%.1f]", GetShadowOffsetY()) != -1) {
1190 dumpInfo.append(buffer);
1191 }
1192
1193 // ShadowAlpha
1194 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1195 if (ret != EOK) {
1196 return "Failed to memset_s for ShadowAlpha, ret=" + std::to_string(ret);
1197 }
1198 if (!ROSEN_EQ(GetShadowAlpha(), 0.f) &&
1199 sprintf_s(buffer, UINT8_MAX, ", ShadowAlpha[%.1f]", GetShadowAlpha()) != -1) {
1200 dumpInfo.append(buffer);
1201 }
1202
1203 // ShadowElevation
1204 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1205 if (ret != EOK) {
1206 return "Failed to memset_s for ShadowElevation, ret=" + std::to_string(ret);
1207 }
1208 if (!ROSEN_EQ(GetShadowElevation(), 0.f) &&
1209 sprintf_s(buffer, UINT8_MAX, ", ShadowElevation[%.1f]", GetShadowElevation()) != -1) {
1210 dumpInfo.append(buffer);
1211 }
1212
1213 // ShadowRadius
1214 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1215 if (ret != EOK) {
1216 return "Failed to memset_s for ShadowRadius, ret=" + std::to_string(ret);
1217 }
1218 if (!ROSEN_EQ(GetShadowRadius(), 0.f) &&
1219 sprintf_s(buffer, UINT8_MAX, ", ShadowRadius[%.1f]", GetShadowRadius()) != -1) {
1220 dumpInfo.append(buffer);
1221 }
1222
1223 // FrameGravity
1224 ret = memset_s(buffer, UINT8_MAX, 0, UINT8_MAX);
1225 if (ret != EOK) {
1226 return "Failed to memset_s for FrameGravity, ret=" + std::to_string(ret);
1227 }
1228 if (!ROSEN_EQ(GetFrameGravity(), Gravity::DEFAULT) &&
1229 sprintf_s(buffer, UINT8_MAX, ", FrameGravity[%d]", GetFrameGravity()) != -1) {
1230 dumpInfo.append(buffer);
1231 }
1232
1233 // IsVisible
1234 if (!GetVisible()) {
1235 dumpInfo.append(", IsVisible[false]");
1236 }
1237
1238 return dumpInfo;
1239 }
1240 } // namespace Rosen
1241 } // namespace OHOS
1242