5364217df7e4001f275d6638ed30987d748f4299
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / gaussian-blur-view / gaussian-blur-view.h
1 #ifndef __DALI_TOOLKIT_GAUSSIAN_BLUR_EFFECT_H__
2 #define __DALI_TOOLKIT_GAUSSIAN_BLUR_EFFECT_H__
3
4 /*
5  * Copyright (c) 2015 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/actors/camera-actor.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/images/frame-buffer-image.h>
25 #include <dali/public-api/render-tasks/render-task.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/controls/control.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal DALI_INTERNAL
37 {
38
39 /**
40  * GaussianBlurView implementation class
41  */
42 class GaussianBlurView;
43
44 /**
45  * BloomView implementation class - requires access to private methods
46  */
47 class BloomView;
48
49 } // namespace Internal
50 /**
51  * @addtogroup dali_toolkit_controls_gaussian_blur_view
52  * @{
53  */
54
55 /**
56  * @brief
57  * GaussianBlurView is a class for applying a render process that blurs an image.
58  *
59  * Basic idea:-
60  *
61  * 1) The GaussianBlurView object will render all its child actors offscreen.\n
62  * 2) The GaussianBlurView object then blurs the result of step 1), using a two pass separated Gaussian blur.\n
63  * 3) The GaussianBlurView object then composites the blur from step 2) with the child actors image from step 1). See GetBlurStrengthPropertyIndex() for more info.\n
64  * 4) The GaussianBlurView object gets rendered automatically, either to the screen via the default render task, or via a RenderTask the user has created for
65  * e.g. further offscreen rendering.
66  *
67  * Fundamentally, the GaussianBlurView is simply an Actor in the normal actor tree that affects all of its children. It should be added to your Actor tree and manipulated in the
68  * normal ways. It can be considered a 'portal' in the sense that all child actors are clipped to the GaussianBlurView actor bounds.
69  *
70  * ************\n
71  * NB: It is essential to remove the GaussianBlurView from the stage and also to call Deactivate() on it when you are not using it. This will ensure that resources are freed and
72  * rendering stops.\n
73  * ************\n
74  *
75  * Usage example:-
76  *
77  * @code
78  *  // Initialise
79  *  GaussianBlurView gaussianBlurView = GaussianBlurView::New();
80  *
81  *  // Create and add some visible actors to the GaussianBlurView, all these child actors will therefore get blurred.
82  *  Image image = Image::New(...);
83  *  ImageView imageView = ImageView::New(image);
84  *  gaussianBlurView.Add(imageView);
85  *  ...
86  *
87  *  // Start rendering the GaussianBlurView
88  *  Stage::GetCurrent().Add(gaussianBlurView);
89  *  gaussianBlurView.Activate();
90  *  ...
91  *
92  *  // Animate the strength of the blur - this can fade between no blur and full blur. See GetBlurStrengthPropertyIndex().
93  *  Animation blurAnimation = Animation::New( ... );
94  *  blurAnimation.AnimateTo( Property( gaussianBlurView, gaussianBlurView.GetBlurStrengthPropertyIndex() ), ... );
95  *  blurAnimation.Play();
96  *
97  *  ...
98  *  // Stop rendering the GaussianBlurView
99  *  Stage::GetCurrent().Remove(gaussianBlurView);
100  *  gaussianBlurView.Deactivate();
101  * @endcode
102  * @SINCE_1_0.0
103  * @remarks This is an experimental feature and might not be supported in the next release.
104  * We do recommend not to use this class.
105  */
106 class DALI_IMPORT_API GaussianBlurView : public Control
107 {
108 public:
109   /**
110    * @brief Signal type for notifications
111    * @SINCE_1_0.0
112    */
113   typedef Signal< void (GaussianBlurView source) > GaussianBlurViewSignal;
114
115   /**
116    * @brief Create an uninitialized GaussianBlurView; this can be initialized with GaussianBlurView::New().
117    * Calling member functions with an uninitialized Dali::Object is not allowed.
118    * @SINCE_1_0.0
119    */
120   GaussianBlurView();
121
122   /**
123    * @brief Copy constructor. Creates another handle that points to the same real object.
124    * @SINCE_1_0.0
125    */
126   GaussianBlurView(const GaussianBlurView& handle);
127
128   /**
129    * @brief Assignment operator. Changes this handle to point to another real object.
130    * @SINCE_1_0.0
131    */
132   GaussianBlurView& operator=(const GaussianBlurView& ZoomView);
133
134   /**
135    * @brief Destructor
136    *
137    * This is non-virtual since derived Handle types must not contain data or virtual methods.
138    * @SINCE_1_0.0
139    */
140   ~GaussianBlurView();
141
142   /**
143    * @brief Downcast a handle to GaussianBlurView handle.
144    *
145    * If handle points to a GaussianBlurView the
146    * downcast produces valid handle. If not the returned handle is left uninitialized.
147    * @SINCE_1_0.0
148    * @param[in] handle Handle to an object
149    * @return A handle to a GaussianBlurView or an uninitialized handle
150    */
151   static GaussianBlurView DownCast( BaseHandle handle );
152
153   /**
154   * @brief Create an initialized GaussianBlurView, using default settings. The default settings are:-\n
155   *
156   * numSamples = 5\n
157   * blurBellCurveWidth = 1.5\n
158   * renderTargetPixelFormat = RGB888\n
159   * downsampleWidthScale = 0.5\n
160   * downsampleHeightScale = 0.5\n
161   * blurUserImage = false
162   * @SINCE_1_0.0
163   * @return A handle to a newly allocated Dali resource
164   */
165   static GaussianBlurView New();
166
167   /**
168   * @brief Create an initialized GaussianBlurView.
169   * @SINCE_1_0.0
170   * @param numSamples The size of the Gaussian blur kernel (number of samples in horizontal / vertical blur directions).
171   * @param blurBellCurveWidth The constant controlling the Gaussian function, must be > 0.0. Controls the width of the bell curve, i.e. the look of the blur and also indirectly
172   * the amount of blurriness Smaller numbers for a tighter curve. Useful values in the range [0.5..3.0] - near the bottom of that range the curve is weighted heavily towards
173   * the centre pixel of the kernel (so there won't be much blur), near the top of that range the pixels have nearly equal weighting (closely approximating a box filter
174   * therefore). Values close to zero result in the bell curve lying almost entirely within a single pixel, in other words there will be basically no blur as neighbouring pixels
175   * have close to zero weights.
176   * @param renderTargetPixelFormat The pixel format of the render targets we are using to perform the blur.
177   * @param downsampleWidthScale The width scale factor applied during the blur process, scaling the size of the source image to the size of the final blurred image output.
178   * Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
179   * @param downsampleHeightScale The height scale factor applied during the blur process, scaling the size of the source image to the size of the final blurred image output.
180   * Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
181   * @param blurUserImage If this is set to true, the GaussianBlurView object will operate in a special mode that allows the user to blur an image of their choice. See
182   * SetUserImageAndOutputRenderTarget().
183   * @return A handle to a newly allocated Dali resource
184   */
185   static GaussianBlurView New(const unsigned int numSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat,
186                               const float downsampleWidthScale, const float downsampleHeightScale,
187                               bool blurUserImage = false);
188
189   /**
190    * @DEPRECATED_1_1.28 Use Actor::Add(Actor) instead
191    * @brief Adds a child Actor to this Actor.
192    * @SINCE_1_0.0
193    * @param [in] child The child.
194    * @pre This Actor (the parent) has been initialized.
195    * @pre The child actor has been initialized.
196    * @pre The child actor is not the same as the parent actor.
197    * @pre The actor is not the Root actor
198    * @post The child will be referenced by its parent. This means that the child will be kept alive,
199    * even if the handle passed into this method is reset or destroyed.
200    * @note If the child already has a parent, it will be removed from old parent
201    * and reparented to this actor. This may change childs position, color, shader effect,
202    * scale etc as it now inherits them from this actor.
203    */
204   void Add(Actor child);
205
206   /**
207    * @DEPRECATED_1_1.28 Use Actor::Remove(Actor) instead
208    * @brief Removes a child Actor from this Actor.
209    *
210    * If the actor was not a child of this actor, this is a no-op.
211    * @SINCE_1_0.0
212    * @param [in] child The child.
213    * @pre This Actor (the parent) has been initialized.
214    * @pre The child actor is not the same as the parent actor.
215    */
216   void Remove(Actor child);
217
218   /**
219    * @brief Start rendering the GaussianBlurView. Must be called after you Add() it to the stage.
220    * @SINCE_1_0.0
221    */
222   void Activate();
223
224   /**
225    * @brief Render the GaussianBlurView once.
226    *
227    * Must be called after you Add() it to the stage.
228    * Only works with a gaussian blur view created with blurUserImage = true.
229    * Listen to the Finished signal to determine when the rendering has completed.
230    * @SINCE_1_0.0
231    */
232   void ActivateOnce();
233
234   /**
235    * @brief Stop rendering the GaussianBlurView. Must be called after you Remove() it from the stage.
236    * @SINCE_1_0.0
237    */
238   void Deactivate();
239
240   /**
241    * @brief Sets a custom image to be blurred and a render target to receive the blurred result.
242    *
243    * If this is called the children of the GaussianBlurObject will not be rendered blurred,
244    * instead the inputImage will get blurred.
245    * To retrieve the blurred image the user can either pass a handle on a render target they own as the second parameter to SetUserImageAndOutputRenderTarget( ... ), or they
246    * can pass NULL for this parameter and instead call GetBlurredRenderTarget() which will return a handle on a render target created internally to the GaussianBlurView object.
247    * @SINCE_1_0.0
248    * @param inputImage The image that the user wishes to blur.
249    * @param outputRenderTarget A render target to receive the blurred result. Passing NULL is allowed. See also GetBlurredRenderTarget().
250    * @pre This object was created with a New( ... ) call where the blurUserImage argument was set to true. If this was not the case an exception will be thrown.
251    */
252   void SetUserImageAndOutputRenderTarget(Image inputImage, FrameBufferImage outputRenderTarget);
253
254   /**
255    * @brief Get the index of the property that can be used to fade the blur in / out.
256    *
257    * This is the overall strength of the blur.
258    * User can use this to animate the blur. A value of 0.0 is zero blur and 1.0 is full blur. Default is 1.0.
259    * Note that if you set the blur to 0.0, the result will be no blur BUT the internal rendering will still be happening. If you wish to turn the blur off, you should remove
260    * the GaussianBlurView object from the stage also.
261    * @SINCE_1_0.0
262    * @return Index of the property that can be used to fade the blur in / out
263    */
264   Dali::Property::Index GetBlurStrengthPropertyIndex() const;
265
266   /**
267    * @brief Get the final blurred image.
268    *
269    * Use can call this function to get the blurred result as an image, to use as they wish. It is not necessary to call this unless you specifically require it.
270    * @SINCE_1_0.0
271    * @return A handle on the blurred image, contained in a render target.
272    * @pre The user must call Activate() before the render target will be returned.
273    */
274   FrameBufferImage GetBlurredRenderTarget() const;
275
276   /**
277   * @brief Set background color for the view. The background will be filled with this color.
278   * @SINCE_1_0.0
279   * @param[in] color The background color.
280   */
281   void SetBackgroundColor( const Vector4& color );
282
283   /**
284   * @brief Get the background color.
285   * @SINCE_1_0.0
286   * @return The background color.
287   */
288   Vector4 GetBackgroundColor() const;
289
290 public: // Signals
291   /**
292    * @brief If ActivateOnce has been called, then connect to this signal to be notified when the
293    * target actor has been rendered.
294    * @SINCE_1_0.0
295    * @return The Finished signal
296    */
297   GaussianBlurViewSignal& FinishedSignal();
298
299 public:
300
301   /// @cond internal
302   /**
303    * @brief Creates a handle using the Toolkit::Internal implementation.
304    * @SINCE_1_0.0
305    * @param[in]  implementation  The UI Control implementation.
306    */
307   DALI_INTERNAL GaussianBlurView( Internal::GaussianBlurView& implementation );
308
309   /**
310    * @brief Allows the creation of this UI Control from an Internal::CustomActor pointer.
311    * @SINCE_1_0.0
312    * @param[in]  internal  A pointer to the internal CustomActor.
313    */
314   DALI_INTERNAL GaussianBlurView( Dali::Internal::CustomActor* internal );
315   /// @endcond
316
317 };
318
319 /**
320  * @}
321  */
322 } // namespace Toolkit
323
324 } // namespace Dali
325
326 #endif // __DALI_TOOLKIT_GAUSSIAN_BLUR_EFFECT_H__