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 "animation/rs_render_transition_effect.h"
17
18 #include <climits>
19
20 #include "animation/rs_animation_common.h"
21 #include "animation/rs_value_estimator.h"
22 #include "modifier/rs_render_modifier.h"
23 #include "platform/common/rs_log.h"
24 #include "transaction/rs_marshalling_helper.h"
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr int PID_SHIFT = 32;
30
GenerateTransitionPropertyId()31 PropertyId GenerateTransitionPropertyId()
32 {
33 // manually set pid to INT_MAX to avoid conflict with other process (note: valid pid is smaller than 2^22)
34 static pid_t pid_ = INT_MAX;
35 static std::atomic<uint32_t> currentId_ = 1;
36
37 auto currentId = currentId_.fetch_add(1, std::memory_order_relaxed);
38 if (currentId == UINT32_MAX) {
39 // [PLANNING]:process the overflow situations
40 ROSEN_LOGE("Property Id overflow");
41 }
42
43 return ((PropertyId)pid_ << PID_SHIFT) | currentId;
44 }
45 } // namespace
46
GetModifier()47 const std::shared_ptr<RSRenderModifier>& RSRenderTransitionEffect::GetModifier()
48 {
49 if (modifier_ == nullptr) {
50 modifier_ = CreateModifier();
51 }
52 return modifier_;
53 }
54
CreateModifier()55 const std::shared_ptr<RSRenderModifier> RSTransitionFade::CreateModifier()
56 {
57 property_ = std::make_shared<RSRenderAnimatableProperty<float>>(0, GenerateTransitionPropertyId());
58 return std::make_shared<RSAlphaRenderModifier>(property_);
59 }
60
UpdateFraction(float fraction) const61 void RSTransitionFade::UpdateFraction(float fraction) const
62 {
63 if (property_ == nullptr) {
64 return;
65 }
66 float startValue(1.0f);
67 float endValue(alpha_);
68 auto value = startValue * (1.0f - fraction) + endValue * fraction;
69 property_->Set(value);
70 }
71
CreateModifier()72 const std::shared_ptr<RSRenderModifier> RSTransitionScale::CreateModifier()
73 {
74 property_ =
75 std::make_shared<RSRenderAnimatableProperty<Vector2f>>(Vector2f { 0, 0 }, GenerateTransitionPropertyId());
76 return std::make_shared<RSScaleRenderModifier>(property_);
77 }
78
UpdateFraction(float fraction) const79 void RSTransitionScale::UpdateFraction(float fraction) const
80 {
81 if (property_ == nullptr) {
82 return;
83 }
84 Vector2f startValue(1.0f, 1.0f);
85 Vector2f endValue(scaleX_, scaleY_);
86 auto value = startValue * (1.0f - fraction) + endValue * fraction;
87 property_->Set(value);
88 }
89
CreateModifier()90 const std::shared_ptr<RSRenderModifier> RSTransitionTranslate::CreateModifier()
91 {
92 property_ =
93 std::make_shared<RSRenderAnimatableProperty<Vector2f>>(Vector2f { 0, 0 }, GenerateTransitionPropertyId());
94 return std::make_shared<RSTranslateRenderModifier>(property_);
95 }
96
UpdateFraction(float fraction) const97 void RSTransitionTranslate::UpdateFraction(float fraction) const
98 {
99 if (property_ == nullptr) {
100 return;
101 }
102 Vector2f startValue(0.0f, 0.0f);
103 Vector2f endValue(translateX_, translateY_);
104 auto value = startValue * (1.0f - fraction) + endValue * fraction;
105 property_->Set(value);
106 }
107
CreateModifier()108 const std::shared_ptr<RSRenderModifier> RSTransitionRotate::CreateModifier()
109 {
110 property_ = std::make_shared<RSRenderAnimatableProperty<Quaternion>>(Quaternion {}, GenerateTransitionPropertyId());
111 return std::make_shared<RSQuaternionRenderModifier>(property_);
112 }
113
UpdateFraction(float fraction) const114 void RSTransitionRotate::UpdateFraction(float fraction) const
115 {
116 if (property_ == nullptr) {
117 return;
118 }
119 auto radian = radian_ * fraction;
120 float factor = std::sin(radian / 2);
121 float qx = dx_ * factor;
122 float qy = dy_ * factor;
123 float qz = dz_ * factor;
124 float qw = std::cos(radian / 2);
125 Quaternion value(qx, qy, qz, qw);
126 property_->Set(value);
127 }
128 } // namespace Rosen
129 } // namespace OHOS
130