1 /*
2  * Copyright (c) 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 #ifndef RENDER_SERVICE_CLIENT_CORE_COMMON_RS_PARTICLE_H
17 #define RENDER_SERVICE_CLIENT_CORE_COMMON_RS_PARTICLE_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include <stdint.h>
23 #include <sys/types.h>
24 #include <vector>
25 
26 #include "animation/rs_animation_timing_curve.h"
27 #include "animation/rs_render_particle.h"
28 #include "common/rs_color.h"
29 #include "common/rs_common_def.h"
30 #include "common/rs_macros.h"
31 #include "common/rs_vector2.h"
32 #include "render/rs_image.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 
37 template<typename T>
38 class RSB_EXPORT Change {
39 public:
40     T fromValue_;
41     T toValue_;
42     int startMillis_;
43     int endMillis_;
44     RSAnimationTimingCurve curve_;
Change()45     Change() : fromValue_(), toValue_(), startMillis_(), endMillis_(), curve_(RSAnimationTimingCurve::LINEAR) {}
Change(T fromValue,T toValue,int startMillis,int endMillis,RSAnimationTimingCurve & curve)46     Change(T fromValue, T toValue, int startMillis, int endMillis, RSAnimationTimingCurve& curve)
47     {
48         fromValue_ = fromValue;
49         toValue_ = toValue;
50         startMillis_ = startMillis;
51         endMillis_ = endMillis;
52         curve_ = curve;
53     }
54     Change(const Change& change) = default;
55     Change& operator=(const Change& change) = default;
56     ~Change() = default;
57 };
58 
59 template<typename T>
60 class RSB_EXPORT ParticleParaType {
61 public:
62     Range<T> val_;
63     ParticleUpdator updator_;
64     Range<float> random_;
65     std::vector<Change<T>> valChangeOverLife_;
ParticleParaType(Range<T> & val,ParticleUpdator updator,Range<float> random,std::vector<Change<T>> & valChangeOverLife)66     ParticleParaType(
67         Range<T>& val, ParticleUpdator updator, Range<float> random, std::vector<Change<T>>& valChangeOverLife)
68     {
69         val_ = val;
70         updator_ = updator;
71         random_ = random;
72         for (size_t i = 0; i < valChangeOverLife.size(); i++) {
73             auto change = valChangeOverLife[i];
74             valChangeOverLife_.push_back(change);
75         }
76     }
ParticleParaType()77     ParticleParaType() : val_(), updator_(ParticleUpdator::NONE), random_() {}
78     ParticleParaType(const ParticleParaType& paraType) = default;
79     ParticleParaType& operator=(const ParticleParaType& paraType) = default;
80     ~ParticleParaType() = default;
81 };
82 
83 class RSB_EXPORT ParticleAcceleration {
84 public:
85     ParticleParaType<float> accelerationValue_;
86     ParticleParaType<float> accelerationAngle_;
87 
88     ParticleAcceleration() = default;
ParticleAcceleration(ParticleParaType<float> accelerationValue,ParticleParaType<float> accelerationAngle)89     ParticleAcceleration(ParticleParaType<float> accelerationValue, ParticleParaType<float> accelerationAngle)
90     {
91         accelerationValue_ = accelerationValue;
92         accelerationAngle_ = accelerationAngle;
93     }
94     ParticleAcceleration(const ParticleAcceleration& acceleration) = default;
95     ParticleAcceleration& operator=(const ParticleAcceleration& acceleration) = default;
96     ~ParticleAcceleration() = default;
97 };
98 
99 class RSB_EXPORT ParticleColorParaType {
100 public:
101     Range<Color> colorVal_;
102     DistributionType distribution_;
103     ParticleUpdator updator_;
104     Range<float> redRandom_;
105     Range<float> greenRandom_;
106     Range<float> blueRandom_;
107     Range<float> alphaRandom_;
108 
109     std::vector<Change<Color>> valChangeOverLife_;
ParticleColorParaType(const Range<Color> & colorVal,const DistributionType & distribution,const ParticleUpdator & updator,const Range<float> & redRandom,const Range<float> & greenRandom,const Range<float> & blueRandom,const Range<float> & alphaRandom,std::vector<Change<Color>> & valChangeOverLife)110     ParticleColorParaType(const Range<Color>& colorVal, const DistributionType& distribution,
111         const ParticleUpdator& updator, const Range<float>& redRandom, const Range<float>& greenRandom,
112         const Range<float>& blueRandom, const Range<float>& alphaRandom, std::vector<Change<Color>>& valChangeOverLife)
113     {
114         colorVal_ = colorVal;
115         distribution_ = distribution;
116         updator_ = updator;
117         redRandom_ = redRandom;
118         greenRandom_ = greenRandom;
119         blueRandom_ = blueRandom;
120         alphaRandom_ = alphaRandom;
121         for (size_t i = 0; i < valChangeOverLife.size(); i++) {
122             auto change = valChangeOverLife[i];
123             valChangeOverLife_.push_back(change);
124         }
125     }
ParticleColorParaType()126     ParticleColorParaType()
127         : colorVal_(), distribution_(DistributionType::UNIFORM), updator_(ParticleUpdator::NONE), redRandom_(),
128           greenRandom_(), blueRandom_(), alphaRandom_(), valChangeOverLife_(0)
129     {}
130     ParticleColorParaType(const ParticleColorParaType& velocity) = default;
131     ParticleColorParaType& operator=(const ParticleColorParaType& velocity) = default;
132     ~ParticleColorParaType() = default;
133 };
134 
135 class RSB_EXPORT ParticleParams {
136 public:
137     EmitterConfig emitterConfig_;
138     ParticleVelocity velocity_;
139     ParticleAcceleration acceleration_;
140     ParticleColorParaType color_;
141     ParticleParaType<float> opacity_;
142     ParticleParaType<float> scale_;
143     ParticleParaType<float> spin_;
ParticleParams(const EmitterConfig & emitterConfig,const ParticleVelocity & velocity,const ParticleAcceleration & acceleration,const ParticleColorParaType & color,const ParticleParaType<float> & opacity,const ParticleParaType<float> & scale,const ParticleParaType<float> & spin)144     explicit ParticleParams(const EmitterConfig& emitterConfig, const ParticleVelocity& velocity,
145         const ParticleAcceleration& acceleration, const ParticleColorParaType& color,
146         const ParticleParaType<float>& opacity, const ParticleParaType<float>& scale,
147         const ParticleParaType<float>& spin)
148     {
149         emitterConfig_ = emitterConfig;
150         velocity_ = velocity;
151         acceleration_ = acceleration;
152         color_ = color;
153         opacity_ = opacity;
154         scale_ = scale;
155         spin_ = spin;
156     }
ParticleParams()157     ParticleParams() : emitterConfig_(), velocity_(), acceleration_(), color_(), opacity_(), scale_(), spin_() {};
158     ParticleParams(const ParticleParams& params) = default;
159     ParticleParams& operator=(const ParticleParams& params) = default;
160     ~ParticleParams() = default;
161 
translateValToRender(const ParticleParaType<float> & val)162     RenderParticleParaType<float> translateValToRender(const ParticleParaType<float>& val) const
163     {
164         RenderParticleParaType<float> value;
165         value.val_ = val.val_;
166         value.updator_ = val.updator_;
167         if (val.updator_ == ParticleUpdator::RANDOM) {
168             value.random_ = val.random_;
169         } else if (val.updator_ == ParticleUpdator::CURVE) {
170             uint32_t size = val.valChangeOverLife_.size();
171             std::vector<std::shared_ptr<ChangeInOverLife<float>>> valChangeOverLife;
172 
173             for (uint32_t i = 0; i < size; i++) {
174                 float fromValue = val.valChangeOverLife_[i].fromValue_;
175                 float toValue = val.valChangeOverLife_[i].toValue_;
176                 float startMillis = val.valChangeOverLife_[i].startMillis_;
177                 float endMillis = val.valChangeOverLife_[i].endMillis_;
178                 int duration = endMillis - startMillis;
179                 RSAnimationTimingCurve curve = val.valChangeOverLife_[i].curve_;
180                 auto interpolator = curve.GetInterpolator(duration);
181 
182                 valChangeOverLife.push_back(std::make_shared<ChangeInOverLife<float>>(
183                     fromValue, toValue, startMillis, endMillis, std::move(interpolator)));
184             }
185             value.valChangeOverLife_ = valChangeOverLife;
186         }
187         return value;
188     }
189 
translateColorToRender(const ParticleColorParaType & val)190     RenderParticleColorParaType translateColorToRender(const ParticleColorParaType& val) const
191     {
192         RenderParticleColorParaType color;
193         color.colorVal_ = val.colorVal_;
194         color.distribution_ = val.distribution_;
195         color.updator_ = val.updator_;
196         if (color.updator_ == ParticleUpdator::RANDOM) {
197             color.redRandom_ = val.redRandom_;
198             color.greenRandom_ = val.greenRandom_;
199             color.blueRandom_ = val.blueRandom_;
200             color.alphaRandom_ = val.alphaRandom_;
201         } else if (color.updator_ == ParticleUpdator::CURVE) {
202             uint32_t size = val.valChangeOverLife_.size();
203             std::vector<std::shared_ptr<ChangeInOverLife<Color>>> colorChangeOverLife;
204             for (uint32_t i = 0; i < size; i++) {
205                 auto fromValue = val.valChangeOverLife_[i].fromValue_;
206                 auto toValue = val.valChangeOverLife_[i].toValue_;
207                 float startMillis = val.valChangeOverLife_[i].startMillis_;
208                 float endMillis = val.valChangeOverLife_[i].endMillis_;
209                 int duration = endMillis - startMillis;
210                 RSAnimationTimingCurve curve = val.valChangeOverLife_[i].curve_;
211                 auto interpolator = curve.GetInterpolator(duration);
212 
213                 colorChangeOverLife.push_back(std::make_shared<ChangeInOverLife<Color>>(
214                     fromValue, toValue, startMillis, endMillis, std::move(interpolator)));
215             }
216             color.valChangeOverLife_ = std::move(colorChangeOverLife);
217         }
218         return color;
219     }
220 
SetParamsToRenderParticle()221     std::shared_ptr<ParticleRenderParams> SetParamsToRenderParticle() const
222     {
223         auto particleRenderParams = std::make_shared<ParticleRenderParams>();
224         particleRenderParams->SetEmitConfig(emitterConfig_);
225         particleRenderParams->SetParticleVelocity(velocity_);
226 
227         auto acceleration = RenderParticleAcceleration();
228         acceleration.accelerationValue_ = translateValToRender(acceleration_.accelerationValue_);
229         acceleration.accelerationAngle_ = translateValToRender(acceleration_.accelerationAngle_);
230 
231         particleRenderParams->SetParticleAcceleration(acceleration);
232         particleRenderParams->SetParticleColor(translateColorToRender(color_));
233         particleRenderParams->SetParticleOpacity(translateValToRender(opacity_));
234         particleRenderParams->SetParticleScale(translateValToRender(scale_));
235         particleRenderParams->SetParticleSpin(translateValToRender(spin_));
236         return particleRenderParams;
237     }
238 };
239 
240 } // namespace Rosen
241 } // namespace OHOS
242 
243 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_PARTICLES_H
244