df616eea09d9d20cf02e598aca1ec885ff336eba
[platform/core/uifw/dali-toolkit.git] / optional / 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) 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 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/control.h>
23
24 namespace Dali DALI_IMPORT_API
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Internal DALI_INTERNAL
31 {
32
33 /**
34  * GaussianBlurView implementation class
35  */
36 class GaussianBlurView;
37
38 /**
39  * BloomView implementation class - requires access to private methods
40  */
41 class BloomView;
42
43 } // namespace Internal
44
45 /**
46  *
47  * GaussianBlurView is a class for applying a render process that blurs an image.
48  *
49  * Basic idea:-
50  *
51  * 1) The GaussianBlurView object will render all its child actors offscreen.\n
52  * 2) The GaussianBlurView object then blurs the result of step 1), using a two pass separated Gaussian blur.\n
53  * 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
54  * 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
55  * e.g. further offscreen rendering.
56  *
57  * 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
58  * normal ways. It can be considered a 'portal' in the sense that all child actors are clipped to the GaussianBlurView actor bounds.
59  *
60  * ************\n
61  * 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
62  * rendering stops.\n
63  * ************\n
64  *
65  * Usage example:-
66  *
67  *  // initialise\n
68  *  GaussianBlurView gaussianBlurView = GaussianBlurView::New();\n
69  *
70  *  // create and add some visible actors to the GaussianBlurView, all these child actors will therefore get blurred.\n
71  *  Image image = Image::New(...);\n
72  *  ImageActor imageActor = ImageActor::New(image);\n
73  *  gaussianBlurView.Add(imageActor);\n
74  *  ...\n
75  *
76  *  // Start rendering the GaussianBlurView\n
77  *  Stage::GetCurrent().Add(gaussianBlurView);\n
78  *  gaussianBlurView.Activate();\n
79  *  ...\n
80  *
81  *  // animate the strength of the blur - this can fade between no blur and full blur. See GetBlurStrengthPropertyIndex().\n
82  *  Animation blurAnimation = Animation::New( ... );\n
83  *  blurAnimation.AnimateTo( Property( gaussianBlurView, gaussianBlurView.GetBlurStrengthPropertyIndex() ), ... );\n
84  *  blurAnimation.Play();\n
85  *
86  *  ...\n
87  *  // Stop rendering the GaussianBlurView\n
88  *  Stage::GetCurrent().Remove(gaussianBlurView);\n
89  *  gaussianBlurView.Deactivate();\n
90  */
91 class GaussianBlurView : public Control
92 {
93 public:
94   /**
95    * Signal type for notifications
96    */
97   typedef SignalV2< void (GaussianBlurView source) > GaussianBlurViewSignal;
98
99   /**
100    * Create an uninitialized GaussianBlurView; this can be initialized with GaussianBlurView::New()
101    * Calling member functions with an uninitialized Dali::Object is not allowed.
102    */
103   GaussianBlurView();
104
105   /**
106    * Copy constructor. Creates another handle that points to the same real object
107    */
108   GaussianBlurView(const GaussianBlurView& handle);
109
110   /**
111    * Assignment operator. Changes this handle to point to another real object
112    */
113   GaussianBlurView& operator=(const GaussianBlurView& ZoomView);
114
115   /**
116    * Virtual destructor.
117    */
118   virtual ~GaussianBlurView();
119
120   /**
121    * Downcast an Object handle to GaussianBlurView. If handle points to a GaussianBlurView the
122    * downcast produces valid handle. If not the returned handle is left uninitialized.
123    * @param[in] handle Handle to an object
124    * @return handle to a GaussianBlurView or an uninitialized handle
125    */
126   static GaussianBlurView DownCast( BaseHandle handle );
127
128   /**
129   * Create an initialized GaussianBlurView, using default settings. The default settings are:-\n
130   *
131   * numSamples = 5\n
132   * blurBellCurveWidth = 1.5\n
133   * renderTargetPixelFormat = RGB888\n
134   * downsampleWidthScale = 0.5\n
135   * downsampleHeightScale = 0.5\n
136   * blurUserImage = false
137   * @return A handle to a newly allocated Dali resource
138   */
139   static GaussianBlurView New();
140
141   /**
142   * Create an initialized GaussianBlurView.
143   * @param numSamples The size of the Gaussian blur kernel (number of samples in horizontal / vertical blur directions).
144   * @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
145   * 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
146   * 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
147   * 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
148   * have close to zero weights.
149   * @param renderTargetPixelFormat The pixel format of the render targets we are using to perform the blur.
150   * @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.
151   * Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
152   * @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.
153   * Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
154   * @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
155   * SetUserImageAndOutputRenderTarget().
156   * @return A handle to a newly allocated Dali resource
157   */
158   static GaussianBlurView New(const unsigned int numSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat,
159                               const float downsampleWidthScale, const float downsampleHeightScale,
160                               bool blurUserImage = false);
161
162   /**
163    * Adds a child Actor to this Actor.
164    * NOTE! if the child already has a parent, it will be removed from old parent
165    * and reparented to this actor. This may change childs position, color, shader effect,
166    * scale etc as it now inherits them from this actor
167    * @pre This Actor (the parent) has been initialized.
168    * @pre The child actor has been initialized.
169    * @pre The child actor is not the same as the parent actor.
170    * @pre The actor is not the Root actor
171    * @param [in] child The child.
172    * @post The child will be referenced by its parent. This means that the child will be kept alive,
173    * even if the handle passed into this method is reset or destroyed.
174    * @post This may invalidate ActorContainer iterators.
175    */
176   void Add(Actor child);
177
178   /**
179    * Removes a child Actor from this Actor.
180    * If the actor was not a child of this actor, this is a no-op.
181    * @pre This Actor (the parent) has been initialized.
182    * @pre The child actor is not the same as the parent actor.
183    * @param [in] child The child.
184    * @post This may invalidate ActorContainer iterators.
185    */
186   void Remove(Actor child);
187
188   /**
189    * Start rendering the GaussianBlurView. Must be called after you Add() it to the stage.
190    */
191   void Activate();
192
193   /**
194    * Render the GaussianBlurView once. Must be called after you Add() it to the stage.
195    * Only works with a gaussian blur view created with blurUserImage = true.
196    * Listen to the Finished signal to determine when the rendering has completed.
197    */
198   void ActivateOnce();
199
200   /**
201    * Stop rendering the GaussianBlurView. Must be called after you Remove() it from the stage.
202    */
203   void Deactivate();
204
205   /**
206    * Sets a custom image to be blurred and a render target to receive the blurred result. If this is called the children of the GaussianBlurObject will not be rendered blurred,
207    * instead the inputImage will get blurred.
208    * 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
209    * 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.
210    * @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.
211    * @param inputImage The image that the user wishes to blur.
212    * @param outputRenderTarget A render target to receive the blurred result. Passing NULL is allowed. See also GetBlurredRenderTarget().
213    */
214   void SetUserImageAndOutputRenderTarget(Image inputImage, FrameBufferImage outputRenderTarget);
215
216   /**
217    * Get the index of the property that can be used to fade the blur in / out. This is the overall strength of the blur.
218    * 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.
219    * 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
220    * the GaussianBlurView object from the stage also.
221    * @return Index of the property that can be used to fade the blur in / out
222    */
223   Property::Index GetBlurStrengthPropertyIndex() const;
224
225   /**
226    * Get the final blurred image.
227    * 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.
228    * @pre The user must call Activate() before the render target will be returned.
229    * @return A handle on the blurred image, contained in a render target.
230    */
231   FrameBufferImage GetBlurredRenderTarget() const;
232
233   /**
234   * Set background color for the view. The background will be filled with this color.
235   * @param[in] color The background color.
236   */
237   void SetBackgroundColor( const Vector4& color );
238
239   /**
240   * Get the background color.
241   * @return The background color.
242   */
243   Vector4 GetBackgroundColor() const;
244
245 public: // Signals
246   /**
247    * If ActivateOnce has been called, then connect to this signal to be notified when the
248    * target actor has been rendered.
249    * @return The Finished signal
250    */
251   GaussianBlurViewSignal& FinishedSignal();
252
253 public:
254
255   /**
256    * Creates a handle using the Toolkit::Internal implementation.
257    * @param[in]  implementation  The UI Control implementation.
258    */
259   GaussianBlurView( Internal::GaussianBlurView& implementation );
260
261   /**
262    * Allows the creation of this UI Control from an Internal::CustomActor pointer.
263    * @param[in]  internal  A pointer to the internal CustomActor.
264    */
265   GaussianBlurView( Dali::Internal::CustomActor* internal );
266
267 private:
268
269 };
270
271 } // namespace Toolkit
272
273 } // namespace Dali
274
275 #endif // __DALI_TOOLKIT_GAUSSIAN_BLUR_EFFECT_H__