Follow the include-order coding conventions
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / super-blur-view / super-blur-view-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "super-blur-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cmath>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/scripting/scripting.h>
26 #include <dali/integration-api/debug.h>
27
28 namespace //unnamed namespace
29 {
30
31 using namespace Dali;
32
33 //Todo: make these properties instead of constants
34 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
35 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
36 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
37 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
38 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGB888;
39 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
40 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
41
42 /**
43  * The constraint is used to blend the group of blurred images continuously with a unified blur strength property value which ranges from zero to one.
44  */
45 struct ActorOpacityConstraint
46 {
47   ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
48   {
49     float rangeLength = 1.f / static_cast<float>( totalImageNum );
50     float index = static_cast<float>( currentImageIdx );
51     mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
52   }
53
54   float operator()( float current, const PropertyInput& blurProperty )
55   {
56     float blurStrength = blurProperty.GetFloat();
57     if(blurStrength <= mRange.x)
58     {
59       return 1.f;
60     }
61     else if(blurStrength > mRange.y)
62     {
63       return 0.f;
64     }
65     else
66     {
67       return (mRange.y - blurStrength)/(mRange.y-mRange.x);
68     }
69   }
70
71   Vector2 mRange;
72 };
73
74 } // namespace
75
76 namespace Dali
77 {
78
79 namespace Toolkit
80 {
81
82 const Property::Index SuperBlurView::PROPERTY_IMAGE( Internal::SuperBlurView::SUPER_BLUR_VIEW_PROPERTY_START_INDEX );
83
84 namespace Internal
85 {
86
87 namespace
88 {
89 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
90
91 BaseHandle Create()
92 {
93   return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
94 }
95
96 // Type registration
97 TypeRegistration typeRegistration( typeid(Toolkit::SuperBlurView), typeid(Toolkit::Control), Create );
98
99 PropertyRegistration property1( typeRegistration, "image", Toolkit::SuperBlurView::PROPERTY_IMAGE,   Property::MAP, &SuperBlurView::SetProperty, &SuperBlurView::GetProperty );
100
101 } // unnamed namespace
102
103 SuperBlurView::SuperBlurView( unsigned int blurLevels )
104 : Control( CONTROL_BEHAVIOUR_NONE ),
105   mBlurLevels( blurLevels ),
106   mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
107   mResourcesCleared( true ),
108   mTargetSize( Vector2::ZERO )
109 {
110   DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
111   mGaussianBlurView.assign( blurLevels, NULL );
112   mBlurredImage.assign( blurLevels, FrameBufferImage() );
113   mImageActors.assign( blurLevels + 1, ImageActor() );
114 }
115
116 SuperBlurView::~SuperBlurView()
117 {
118 }
119
120 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
121 {
122   //Create the implementation
123   IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
124
125   //Pass ownership to CustomActor via derived handle
126   Toolkit::SuperBlurView handle( *superBlurView );
127
128   // Second-phase init of the implementation
129   // This can only be done after the CustomActor connection has been made...
130   superBlurView->Initialize();
131
132   return handle;
133 }
134
135 void SuperBlurView::OnInitialize()
136 {
137   mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
138
139   DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
140   for(unsigned int i=0; i<=mBlurLevels;i++)
141   {
142     mImageActors[i] = ImageActor::New(  );
143     mImageActors[i].SetParentOrigin( ParentOrigin::CENTER );
144     mImageActors[i].SetZ(-static_cast<float>(i)*0.01f);
145     mImageActors[i].SetColorMode( USE_OWN_MULTIPLY_PARENT_ALPHA );
146     Self().Add( mImageActors[i] );
147   }
148
149   for(unsigned int i=0; i < mBlurLevels; i++)
150   {
151     mImageActors[i].ApplyConstraint( Constraint::New<float>( Actor::COLOR_ALPHA, ParentSource( mBlurStrengthPropertyIndex ), ActorOpacityConstraint(mBlurLevels, i) ) );
152   }
153
154   Self().SetSize(Stage::GetCurrent().GetSize());
155 }
156
157 void SuperBlurView::SetImage(Image inputImage)
158 {
159   DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
160   DALI_ASSERT_ALWAYS( mBlurredImage.size() == mBlurLevels && "must synchronize the blurred image group if blur levels got changed " );
161
162   ClearBlurResource();
163
164   mImageActors[0].SetImage( inputImage );
165
166   for(unsigned int i=1; i<=mBlurLevels;i++)
167   {
168     mImageActors[i].SetImage( mBlurredImage[i-1] );
169   }
170
171   BlurImage( 0,  inputImage);
172   for(unsigned int i=1; i<mBlurLevels;i++)
173   {
174     BlurImage( i,  mBlurredImage[i-1]);
175   }
176
177   mResourcesCleared = false;
178 }
179
180 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
181 {
182   return mBlurStrengthPropertyIndex;
183 }
184
185 void SuperBlurView::SetBlurStrength( float blurStrength )
186 {
187   Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
188 }
189
190 float SuperBlurView::GetCurrentBlurStrength() const
191 {
192   float blurStrength;
193   (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
194
195   return blurStrength;
196 }
197
198 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
199 {
200   return mBlurFinishedSignal;
201 }
202
203 Image SuperBlurView::GetBlurredImage( unsigned int level )
204 {
205   DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
206   return mBlurredImage[level-1];
207 }
208
209 void SuperBlurView::BlurImage( unsigned int idx, Image image )
210 {
211   DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
212   mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
213                                                            GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
214                                                            GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
215                                                            GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
216   mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
217   mGaussianBlurView[idx].SetSize(mTargetSize);
218   mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] );
219   if( idx == mBlurLevels-1 )
220   {
221     mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
222   }
223   Stage::GetCurrent().Add( mGaussianBlurView[idx] );
224   mGaussianBlurView[idx].ActivateOnce();
225 }
226
227 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
228 {
229   ClearBlurResource();
230   Toolkit::SuperBlurView handle( GetOwner() );
231   mBlurFinishedSignal.Emit( handle );
232 }
233
234 void SuperBlurView::ClearBlurResource()
235 {
236   if( !mResourcesCleared )
237   {
238     DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
239     for(unsigned int i=0; i<mBlurLevels;i++)
240     {
241       Stage::GetCurrent().Remove( mGaussianBlurView[i] );
242       mGaussianBlurView[i].Deactivate();
243       mGaussianBlurView[i].Reset();
244     }
245     mResourcesCleared = true;
246   }
247 }
248
249 void SuperBlurView::OnRelayout( const Vector2& size, ActorSizeContainer& container )
250 {
251   unsigned int numChildren = Self().GetChildCount();
252
253   for( unsigned int i=0; i<numChildren; ++i )
254   {
255     Self().GetChildAt(i).SetSize(size);
256   }
257 }
258
259 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
260 {
261   if( mTargetSize != Vector2(targetSize) )
262   {
263     mTargetSize = Vector2(targetSize);
264
265     for(unsigned int i=0; i<mBlurLevels;i++)
266     {
267       float exponent = static_cast<float>(i+1);
268       mBlurredImage[i] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent),
269                                                 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, Dali::Image::NEVER );
270     }
271   }
272 }
273
274 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
275 {
276   Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
277
278   if ( superBlurView )
279   {
280     SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
281
282     switch ( propertyIndex )
283     {
284       case Toolkit::SuperBlurView::PROPERTY_IMAGE:
285       {
286         Dali::Image image = Scripting::NewImage( value );
287         if ( image )
288         {
289           superBlurViewImpl.SetImage( image );
290         }
291         else
292         {
293           DALI_LOG_ERROR( "Cannot create image from property value\n" );
294         }
295         break;
296       }
297     }
298   }
299 }
300
301 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
302 {
303   Property::Value value;
304
305   Toolkit::SuperBlurView pushButton = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
306
307   if ( pushButton )
308   {
309     SuperBlurView& superBlurViewImpl( GetImpl( pushButton ) );
310
311     switch ( propertyIndex )
312     {
313       case Toolkit::SuperBlurView::PROPERTY_IMAGE:
314       {
315         Property::Map map;
316         if ( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
317         {
318           Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
319         }
320         value = Property::Value( map );
321         break;
322       }
323     }
324   }
325
326   return value;
327 }
328
329 } // namespace Internal
330
331 } // namespace Toolkit
332
333 } // namespace Dali