Fixed vector and list includes to use wrappers
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / particle-system / particle-emitter-impl.h
1 #ifndef DALI_TOOLKIT_PARTICLE_SYSTEM_INTERNAL_PARTICLE_EMITTER_H
2 #define DALI_TOOLKIT_PARTICLE_SYSTEM_INTERNAL_PARTICLE_EMITTER_H
3 /*
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/adaptor-framework/timer.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <chrono>
24 #include <ctime>
25 #include <memory>
26
27 // For multithreading update
28 #include <dali/devel-api/threading/thread-pool.h>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/public-api/particle-system/particle-domain.h>
32 #include <dali-toolkit/public-api/particle-system/particle-emitter.h>
33 #include <dali-toolkit/public-api/particle-system/particle-list.h>
34 #include <dali-toolkit/public-api/particle-system/particle-modifier.h>
35 #include <dali-toolkit/public-api/particle-system/particle-renderer.h>
36 #include <dali-toolkit/public-api/particle-system/particle-source.h>
37
38 namespace Dali::Toolkit::ParticleSystem::Internal
39 {
40 class FrameCallback;
41 class ParticleEmitter : public Dali::BaseObject, public Dali::ConnectionTracker
42 {
43 public:
44   /**
45    * @brief Constructor
46    */
47   ParticleEmitter();
48
49   /**
50    * @brief Destructor
51    */
52   ~ParticleEmitter() override = default;
53
54   /**
55    * @brief Tests whether emitter is complete (ready for simulation)
56    *
57    * @return True if emitter is complete, false otherwise
58    */
59   [[nodiscard]] bool IsComplete() const
60   {
61     return (mParticleStatusBits & STATUS_COMPLETE_BITS) == STATUS_COMPLETE_BITS;
62   }
63
64   [[nodiscard]] ParticleSystem::ParticleSource GetSource() const;
65
66   void SetSource(const ParticleSystem::ParticleSource& source);
67
68   [[nodiscard]] ParticleSystem::ParticleDomain GetDomain() const;
69
70   void SetDomain(const ParticleSystem::ParticleDomain& domain);
71
72   [[nodiscard]] ParticleSystem::ParticleRenderer GetRenderer() const;
73
74   [[nodiscard]] ParticleSystem::ParticleModifier GetModifierAt(uint32_t index);
75
76   void RemoveModifierAt(uint32_t index);
77
78   void SetRenderer(const ParticleSystem::ParticleRenderer& renderer);
79
80   void SetParticleCount(uint32_t maxParticleCount);
81
82   ParticleSystem::ParticleList& GetParticleList();
83
84   uint32_t AddModifier(const ParticleSystem::ParticleModifier& modifier);
85
86   void AttachTo(Actor actor);
87
88   [[nodiscard]] Actor GetActor() const;
89
90   void Update();
91
92   void UpdateSource(uint32_t count);
93
94   void UpdateModifierMT(Dali::Toolkit::ParticleSystem::ParticleModifier& modifier);
95
96   void UpdateDomain();
97
98   void SetEmissionRate(uint32_t ratePerSecond);
99
100   [[nodiscard]] uint32_t GetEmissionRate() const;
101
102   void SetInitialParticleCount(uint32_t count);
103
104   [[nodiscard]] uint32_t GetInitialParticleCount() const;
105
106   void Start();
107
108   void Stop();
109
110   void EnableParallelProcessing(bool enabled);
111
112   [[nodiscard]] bool IsParallelProcessingEnabled() const;
113
114   void SetActiveParticlesLimit(uint32_t count);
115
116   [[nodiscard]] uint32_t GetActiveParticlesLimit() const;
117
118   [[nodiscard]] ParticleSystem::ParticleEmitter::Status GetStatus() const;
119
120   [[nodiscard]] std::chrono::milliseconds GetCurrentTimeMillis() const;
121
122   // All these bits must be set in order to consider emitter COMPLETE
123   const uint32_t SOURCE_SET_STATUS_BIT   = 1 << 0;
124   const uint32_t RENDERER_SET_STATUS_BIT = 1 << 1;
125   const uint32_t DOMAIN_SET_STATUS_BIT   = 1 << 2;
126
127   // 1. Only one of these flags can be set at a time
128   // 2. They are invalid as long as emitter is INCOMPLETE
129   const uint32_t SIMULATION_STARTED_STATUS_BIT = 1 << 3;
130   const uint32_t SIMULATION_PAUSED_STATUS_BIT  = 1 << 4;
131   const uint32_t SIMULATION_STOPPED_STATUS_BIT = 1 << 5;
132
133   const uint32_t STATUS_COMPLETE_BITS = SOURCE_SET_STATUS_BIT | RENDERER_SET_STATUS_BIT | DOMAIN_SET_STATUS_BIT;
134
135   ParticleSystem::ParticleSource mParticleSource; ///< Current particle source object
136   ParticleSystem::ParticleDomain mParticleDomain; ///< Current particle domain object
137
138   uint8_t mParticleStatusBits{0u}; ///< Current status of the emitter
139
140   // List of particles
141   ParticleSystem::ParticleList mParticleList;
142
143   std::vector<ParticleSystem::ParticleModifier> mModifiers;
144
145   ParticleSystem::ParticleRenderer mParticleRenderer;
146
147   Actor mActor;
148
149   uint32_t                  mEmissionRatePerSecond{1u};
150   std::atomic<uint32_t>     mEmissionCountOnStart{0u};
151   std::atomic<uint32_t>     mActiveParticlesLimit{0u}; ///< 0 - unlimited
152   std::atomic<bool>         mSystemStarted{false};
153   std::chrono::milliseconds mCurrentMilliseconds{0};
154   std::chrono::milliseconds mLastUpdateMs{0};
155
156   bool                           mParallelProcessing{false};
157   std::unique_ptr<FrameCallback> mFrameCallback;
158 };
159
160 } // namespace Dali::Toolkit::ParticleSystem::Internal
161
162 namespace Dali::Toolkit::ParticleSystem
163 {
164 // Returns thread pool shared by whole particle system
165 Dali::ThreadPool& GetThreadPool();
166
167 inline Internal::ParticleEmitter& GetImplementation(ParticleSystem::ParticleEmitter& source)
168 {
169   DALI_ASSERT_ALWAYS(source && "ParticleEmitter handle is empty");
170
171   BaseObject& handle = source.GetBaseObject();
172
173   return static_cast<Internal::ParticleEmitter&>(handle);
174 }
175
176 inline const Internal::ParticleEmitter& GetImplementation(const ParticleSystem::ParticleEmitter& source)
177 {
178   DALI_ASSERT_ALWAYS(source && "ParticleEmitter handle is empty");
179
180   const BaseObject& handle = source.GetBaseObject();
181
182   return static_cast<const Internal::ParticleEmitter&>(handle);
183 }
184
185 } // namespace Dali::Toolkit::ParticleSystem
186 #endif // DALI_TOOLKIT_PARTICLE_SYSTEM_INTERNAL_PARTICLE_EMITTER_H