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