[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / gaussian-blur-view / gaussian-blur-view-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_GAUSSIAN_BLUR_EFFECT_H
2 #define DALI_TOOLKIT_INTERNAL_GAUSSIAN_BLUR_EFFECT_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/public-api/object/property-map.h>
23 #include <cmath>
24 #include <sstream>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/controls/gaussian-blur-view/gaussian-blur-view.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 class GaussianBlurView;
36
37 namespace Internal
38 {
39 /**
40  * GaussianBlurView implementation class
41  */
42 class GaussianBlurView : public Control
43 {
44 public:
45   /**
46    * @copydoc Dali::Toolkit::GaussianBlurView::GaussianBlurView
47    */
48   GaussianBlurView();
49
50   /**
51    * @copydoc Dali::Toolkit::GaussianBlurView::GaussianBlurView
52    */
53   GaussianBlurView(const unsigned int numSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat, const float downsampleWidthScale, const float downsampleHeightScale, bool blurUserImage);
54
55   /**
56    * @copydoc Dali::Toolkit::GaussianBlurView::~GaussianBlurView
57    */
58   virtual ~GaussianBlurView();
59
60   /**
61    * @copydoc Dali::Toolkit::GaussianBlurView::New
62    */
63   static Dali::Toolkit::GaussianBlurView New();
64   static Dali::Toolkit::GaussianBlurView New(const unsigned int numSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat, const float downsampleWidthScale, const float downsampleHeightScale, bool blurUserImage);
65
66   void Activate();
67   void ActivateOnce();
68   void Deactivate();
69
70   void SetUserImageAndOutputRenderTarget(Texture inputImage, FrameBuffer outputRenderTarget);
71
72   Property::Index GetBlurStrengthPropertyIndex() const
73   {
74     return mBlurStrengthPropertyIndex;
75   }
76   FrameBuffer GetBlurredRenderTarget() const;
77
78   /// @copydoc Dali::Toolkit::GaussianBlurView::SetBackgroundColor(const Vector4&)
79   void SetBackgroundColor(const Vector4& color);
80
81   /// @copydoc Dali::Toolkit::GaussianBlurView::GetBackgroundColor
82   Vector4 GetBackgroundColor() const;
83
84   void                                                     AllocateResources();
85   void                                                     CreateRenderTasks();
86   void                                                     RemoveRenderTasks();
87   Dali::Toolkit::GaussianBlurView::GaussianBlurViewSignal& FinishedSignal();
88
89 private:
90   void OnInitialize() override;
91   void OnSizeSet(const Vector3& targetSize) override;
92
93   /**
94    * @copydoc Control::OnChildAdd()
95    */
96   void OnChildAdd(Actor& child) override;
97
98   /**
99    * @copydoc Control::OnChildRemove()
100    */
101   void OnChildRemove(Actor& child) override;
102
103   void        SetBlurBellCurveWidth(float blurBellCurveWidth);
104   float       CalcGaussianWeight(float x);
105   void        SetShaderConstants();
106   std::string GetSampleOffsetsPropertyName(unsigned int index) const;
107   std::string GetSampleWeightsPropertyName(unsigned int index) const;
108
109   void OnRenderTaskFinished(Dali::RenderTask& renderTask);
110
111   /////////////////////////////////////////////////////////////
112   unsigned int  mNumSamples;         // number of blur samples in each of horiz/vert directions
113   float         mBlurBellCurveWidth; // constant used when calculating the gaussian weights
114   Pixel::Format mPixelFormat;        // pixel format used by render targets
115
116   /////////////////////////////////////////////////////////////
117   // downsampling is used for the separated blur passes to get increased blur with the same number of samples and also to make rendering quicker
118   float mDownsampleWidthScale;
119   float mDownsampleHeightScale;
120   float mDownsampledWidth;
121   float mDownsampledHeight;
122
123   /////////////////////////////////////////////////////////////
124   // if this is set to true, we blur a user supplied image rather than rendering and blurring children
125   bool mBlurUserImage : 1;
126
127   /////////////////////////////////////////////////////////////
128   // if this is set to true, set the render tasks to refresh once
129   bool mRenderOnce : 1;
130
131   /////////////////////////////////////////////////////////////
132   // background fill color
133   Vector4 mBackgroundColor;
134
135   /////////////////////////////////////////////////////////////
136   // for checking if we need to reallocate render targets
137   Vector2 mTargetSize;
138   Vector2 mLastSize;
139
140   /////////////////////////////////////////////////////////////
141   // for creating a subtree for all user added child actors, so that we can have them exclusive to the mRenderChildrenTask and our other actors exclusive to our other tasks
142   Actor mChildrenRoot;
143   // for creating a subtree for the internal actors
144   Actor mInternalRoot;
145
146   /////////////////////////////////////////////////////////////
147   // for mapping offscreen renders to render target sizes
148   CameraActor mRenderFullSizeCamera;
149   CameraActor mRenderDownsampledCamera;
150
151   /////////////////////////////////////////////////////////////
152   // for rendering all user added children to offscreen target
153   FrameBuffer mRenderTargetForRenderingChildren;
154   RenderTask  mRenderChildrenTask;
155
156   /////////////////////////////////////////////////////////////
157   // for rendering separated blur passes to offscreen targets
158   FrameBuffer mRenderTarget1;
159   FrameBuffer mRenderTarget2;
160
161   Actor mHorizBlurActor;
162   Actor mVertBlurActor;
163
164   RenderTask mHorizBlurTask;
165   RenderTask mVertBlurTask;
166
167   /////////////////////////////////////////////////////////////
168   // for compositing blur and children renders to offscreen target
169   Actor      mCompositingActor;
170   RenderTask mCompositeTask;
171
172   /////////////////////////////////////////////////////////////
173   // for holding blurred result
174   Actor mTargetActor;
175
176   /////////////////////////////////////////////////////////////
177   // for animating fade in / out of blur, hiding internal implementation but allowing user to set via GaussianBlurView interface
178   Property::Index mBlurStrengthPropertyIndex;
179
180   /////////////////////////////////////////////////////////////
181   // User can specify image to blur and output target, so we can use GaussianBlurView for arbitrary blur processes
182   Texture     mUserInputImage;
183   FrameBuffer mUserOutputRenderTarget;
184
185   Dali::Toolkit::GaussianBlurView::GaussianBlurViewSignal mFinishedSignal; ///< Signal emitted when blur has completed.
186
187   bool mActivated : 1;
188
189 private:
190   // Undefined copy constructor.
191   GaussianBlurView(const GaussianBlurView&);
192
193   // Undefined assignment operator.
194   GaussianBlurView& operator=(const GaussianBlurView&);
195 };
196
197 } // namespace Internal
198
199 // Helpers for public-api forwarding methods
200 inline Toolkit::Internal::GaussianBlurView& GetImpl(Toolkit::GaussianBlurView& obj)
201 {
202   DALI_ASSERT_ALWAYS(obj);
203   Dali::RefObject& handle = obj.GetImplementation();
204   return static_cast<Toolkit::Internal::GaussianBlurView&>(handle);
205 }
206
207 inline const Toolkit::Internal::GaussianBlurView& GetImpl(const Toolkit::GaussianBlurView& obj)
208 {
209   DALI_ASSERT_ALWAYS(obj);
210   const Dali::RefObject& handle = obj.GetImplementation();
211   return static_cast<const Toolkit::Internal::GaussianBlurView&>(handle);
212 }
213
214 } // namespace Toolkit
215
216 } // namespace Dali
217
218 #endif // DALI_TOOLKIT_INTERNAL_GAUSSIAN_BLUR_EFFECT_H