[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / bubble-effect / bubble-emitter-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H
2 #define DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/common/stage.h>
23 #include <dali/public-api/actors/camera-actor.h>
24 #include <dali/public-api/render-tasks/render-task.h>
25 #include <dali/public-api/rendering/frame-buffer.h>
26 #include <dali/public-api/rendering/geometry.h>
27 #include <dali/public-api/rendering/renderer.h>
28 #include <dali/public-api/rendering/sampler.h>
29 #include <dali/public-api/rendering/shader.h>
30 #include <dali/public-api/rendering/vertex-buffer.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/devel-api/controls/bubble-effect/bubble-emitter.h>
34 #include <dali-toolkit/public-api/controls/control-impl.h>
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42 class BubbleRenderer;
43
44 /**
45  * BubbleEmitter implementation class.
46  */
47 class BubbleEmitter : public Control
48 {
49 public:
50   /**
51    * Destructor
52    */
53   ~BubbleEmitter();
54
55   /**
56    * @copydoc Toolkit::BubbleEmitter::New
57    */
58   static Toolkit::BubbleEmitter New(const Vector2& winSize,
59                                     Texture        shapeTexture,
60                                     unsigned int   maximumNumberOfBubble,
61                                     const Vector2& bubbleSizeRange);
62
63   /**
64    * @copydoc Toolkit::BubbleEmitter::GetRootActor
65    */
66   Actor GetRootActor();
67
68   /**
69    * @copydoc Toolkit::BubbleEmitter::SetBackground
70    */
71   void SetBackground(Texture bgTexture, const Vector3& hsvDelta);
72
73   /**
74    * @copydoc Toolkit::BubbleEmitter::SetShape
75    */
76   void SetBubbleShape(Texture shapeTexture);
77
78   /**
79    * @copydoc Toolkit::BubbleEmiter::SetBubbleScale
80    */
81   void SetBubbleScale(float scale);
82
83   /**
84    * @copydoc Toolkit::BubbleEmitter::SetBubbleDensity
85    */
86   void SetBubbleDensity(unsigned int density);
87
88   /**
89    * @copydoc Toolkit::BubbleEmitter::EmitBubble
90    */
91   void EmitBubble(Animation& animation, const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement);
92
93   /**
94    * @copydoc Toolkit::BubbleEmitter::Restore
95    */
96   void Restore();
97
98 private:
99   /**
100    * Construct a new BubbleEmitter object.
101    * @param[in] movementArea The size of the bubble moving area
102    * @param[in] shapeTexture The alpha channnel of this texture defines the bubble shape.
103    * @param[in] maximumNumberOfBubble The maximum number of bubble needed.
104    * @param[in] bubbleSizeRange The size range of the bubbles; x component is the minimal size, and y component is the maximum size.
105    */
106   BubbleEmitter(const Vector2& movementArea,
107                 Texture        shapeTexture,
108                 unsigned int   maximumNumberOfBubble,
109                 const Vector2& bubbleSizeRange);
110
111   /**
112    * This method is called after the CubeTransitionEffect has been initialized.
113    * The meshActors and BubbleEffects are created here.
114    */
115   void OnInitialize();
116
117   /**
118    * Create the geometry of a mesh.
119    * @param[in] numOfPatch The triangle number in the mesh is 2*numOfPatch; two triangles for each bubble.
120    * @return The mesh geometry.
121    */
122   Geometry CreateGeometry(unsigned int numOfPatch);
123
124   /**
125    * Callback function of the finished signal of off-screen render task.
126    * @param[in] source The render task used to create the color adjusted background texture.
127    */
128   void OnRenderFinished(RenderTask& source);
129
130   /**
131    * Callback function from Stage to tell us if the context has been regained.
132    */
133   void OnContextRegained();
134
135   /**
136    * Set the uniform values to the shader effect to emit a bubble
137    * @param[in] bubbleRenderer The BubbleRenderer
138    * @param[in] curUniform The index of the uniform array in the shader
139    * @param[in] emitPosition The start position of the bubble movement.
140    * @param[in] direction The direction used to constrain the bubble to move in an adjacent direction around it.
141    * @param[in] displacement The displacement used to bound the moving distance of the bubble.
142    */
143   void SetBubbleParameter(BubbleRenderer& bubbleRenderer, unsigned int curUniform, const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement);
144
145 private:
146   Actor       mBubbleRoot;        ///<The bubble root actor. Need to add it to stage to get the bubbles rendered.
147   Texture     mShapeTexture;      ///< The alpha channnel of this texture defines the bubble shape.
148   Texture     mBackgroundTexture; ///< The original background texture
149   Texture     mEffectTexture;     ///< Texture which stores the adjusted color of the background image.The bubbles pick color from this image.
150   FrameBuffer mFrameBuffer;       ///< FrameBuffer used for offscreen rendering
151   CameraActor mCameraActor;       ///< The render task views the scene from the perspective of this actor.
152
153   Geometry                    mMeshGeometry;    ///< The mesh geometry which contains the vertices and indices data
154   TextureSet                  mTextureSet;      ///< The texture set which controls the bubble display
155   std::vector<BubbleRenderer> mBubbleRenderers; ///< The BubbleRenderer vector, its size is mNumShader.
156
157   Vector2 mMovementArea;    ///< The size of the bubble moving area, usually the same size as the background.
158   Vector2 mBubbleSizeRange; ///< The size range of the bubbles; x component is the low bound, and y component is the up bound.
159   Vector3 mHSVDelta;        ///< The HSV difference used to adjust the background image color.
160
161   unsigned int mNumBubblePerRenderer; ///< How many bubbles for each BubbleRenderer.
162   unsigned int mNumRenderer;          ///< How many BubbleRenderers are used.
163   unsigned int mDensity;              ///< How many bubbles will emit at each time, they are controlled by same uniforms in the shader.
164   unsigned int mTotalNumOfBubble;     ///< mNumBubblePerShader*mNumShader.
165   unsigned int mCurrentBubble;        ///< Keep track of the index for the newly emitted bubble
166   unsigned int mRandomSeed;           ///< Seed to generate random number.
167
168   bool mRenderTaskRunning; ///< If the background render task is currently running
169 };
170
171 } // namespace Internal
172
173 // Helpers for public-api forwarding methods
174 inline Internal::BubbleEmitter& GetImpl(Dali::Toolkit::BubbleEmitter& obj)
175 {
176   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
177   Dali::RefObject& handle = obj.GetImplementation();
178   return static_cast<Toolkit::Internal::BubbleEmitter&>(handle);
179 }
180
181 inline const Internal::BubbleEmitter& GetImpl(const Dali::Toolkit::BubbleEmitter& obj)
182 {
183   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
184   const Dali::RefObject& handle = obj.GetImplementation();
185   return static_cast<const Toolkit::Internal::BubbleEmitter&>(handle);
186 }
187
188 } // namespace Toolkit
189
190 } // namespace Dali
191
192 #endif // DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H