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