Property refactor in dali-toolkit: Toolkit changes
[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/animation/active-constraint.h>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/scripting/scripting.h>
29 #include <dali/integration-api/debug.h>
30
31 namespace //Unnamed namespace
32 {
33
34 using namespace Dali;
35
36 //Todo: make these properties instead of constants
37 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
38 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
39 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
40 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
41 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGB888;
42 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
43 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
44
45 /**
46  * 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.
47  */
48 struct ActorOpacityConstraint
49 {
50   ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
51   {
52     float rangeLength = 1.f / static_cast<float>( totalImageNum );
53     float index = static_cast<float>( currentImageIdx );
54     mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
55   }
56
57   float operator()( float current, const PropertyInput& blurProperty )
58   {
59     float blurStrength = blurProperty.GetFloat();
60     if(blurStrength <= mRange.x)
61     {
62       return 1.f;
63     }
64     else if(blurStrength > mRange.y)
65     {
66       return 0.f;
67     }
68     else
69     {
70       return (mRange.y - blurStrength)/(mRange.y-mRange.x);
71     }
72   }
73
74   Vector2 mRange;
75 };
76
77 } // namespace
78
79 namespace Dali
80 {
81
82 namespace Toolkit
83 {
84
85 namespace Internal
86 {
87
88 namespace
89 {
90
91 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
92
93 BaseHandle Create()
94 {
95   return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
96 }
97
98 // Setup properties, signals and actions using the type-registry.
99 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create )
100
101 DALI_PROPERTY_REGISTRATION( SuperBlurView, "image", MAP, IMAGE )
102
103 DALI_TYPE_REGISTRATION_END()
104
105 } // unnamed namespace
106
107 SuperBlurView::SuperBlurView( unsigned int blurLevels )
108 : Control( CONTROL_BEHAVIOUR_NONE ),
109   mBlurLevels( blurLevels ),
110   mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
111   mResourcesCleared( true ),
112   mTargetSize( Vector2::ZERO )
113 {
114   DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
115   mGaussianBlurView.assign( blurLevels, NULL );
116   mBlurredImage.assign( blurLevels, FrameBufferImage() );
117   mImageActors.assign( blurLevels + 1, ImageActor() );
118 }
119
120 SuperBlurView::~SuperBlurView()
121 {
122 }
123
124 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
125 {
126   //Create the implementation
127   IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
128
129   //Pass ownership to CustomActor via derived handle
130   Toolkit::SuperBlurView handle( *superBlurView );
131
132   // Second-phase init of the implementation
133   // This can only be done after the CustomActor connection has been made...
134   superBlurView->Initialize();
135
136   return handle;
137 }
138
139 void SuperBlurView::OnInitialize()
140 {
141   mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
142
143   DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
144   for(unsigned int i=0; i<=mBlurLevels;i++)
145   {
146     mImageActors[i] = ImageActor::New(  );
147     mImageActors[i].SetParentOrigin( ParentOrigin::CENTER );
148     mImageActors[i].SetZ(-static_cast<float>(i)*0.01f);
149     mImageActors[i].SetColorMode( USE_OWN_MULTIPLY_PARENT_ALPHA );
150     Self().Add( mImageActors[i] );
151   }
152
153   for(unsigned int i=0; i < mBlurLevels; i++)
154   {
155     mImageActors[i].ApplyConstraint( Constraint::New<float>( Actor::Property::COLOR_ALPHA, ParentSource( mBlurStrengthPropertyIndex ), ActorOpacityConstraint(mBlurLevels, i) ) );
156   }
157
158   Self().SetSize(Stage::GetCurrent().GetSize());
159 }
160
161 void SuperBlurView::SetImage(Image inputImage)
162 {
163   DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
164   DALI_ASSERT_ALWAYS( mBlurredImage.size() == mBlurLevels && "must synchronize the blurred image group if blur levels got changed " );
165
166   ClearBlurResource();
167
168   mImageActors[0].SetImage( inputImage );
169
170   for(unsigned int i=1; i<=mBlurLevels;i++)
171   {
172     mImageActors[i].SetImage( mBlurredImage[i-1] );
173   }
174
175   BlurImage( 0,  inputImage);
176   for(unsigned int i=1; i<mBlurLevels;i++)
177   {
178     BlurImage( i,  mBlurredImage[i-1]);
179   }
180
181   mResourcesCleared = false;
182 }
183
184 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
185 {
186   return mBlurStrengthPropertyIndex;
187 }
188
189 void SuperBlurView::SetBlurStrength( float blurStrength )
190 {
191   Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
192 }
193
194 float SuperBlurView::GetCurrentBlurStrength() const
195 {
196   float blurStrength;
197   (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
198
199   return blurStrength;
200 }
201
202 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
203 {
204   return mBlurFinishedSignal;
205 }
206
207 Image SuperBlurView::GetBlurredImage( unsigned int level )
208 {
209   DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
210   return mBlurredImage[level-1];
211 }
212
213 void SuperBlurView::BlurImage( unsigned int idx, Image image )
214 {
215   DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
216   mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
217                                                            GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
218                                                            GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
219                                                            GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
220   mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
221   mGaussianBlurView[idx].SetSize(mTargetSize);
222   mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] );
223   if( idx == mBlurLevels-1 )
224   {
225     mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
226   }
227   Stage::GetCurrent().Add( mGaussianBlurView[idx] );
228   mGaussianBlurView[idx].ActivateOnce();
229 }
230
231 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
232 {
233   ClearBlurResource();
234   Toolkit::SuperBlurView handle( GetOwner() );
235   mBlurFinishedSignal.Emit( handle );
236 }
237
238 void SuperBlurView::ClearBlurResource()
239 {
240   if( !mResourcesCleared )
241   {
242     DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
243     for(unsigned int i=0; i<mBlurLevels;i++)
244     {
245       Stage::GetCurrent().Remove( mGaussianBlurView[i] );
246       mGaussianBlurView[i].Deactivate();
247       mGaussianBlurView[i].Reset();
248     }
249     mResourcesCleared = true;
250   }
251 }
252
253 void SuperBlurView::OnRelayout( const Vector2& size, ActorSizeContainer& container )
254 {
255   unsigned int numChildren = Self().GetChildCount();
256
257   for( unsigned int i=0; i<numChildren; ++i )
258   {
259     Self().GetChildAt(i).SetSize(size);
260   }
261 }
262
263 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
264 {
265   if( mTargetSize != Vector2(targetSize) )
266   {
267     mTargetSize = Vector2(targetSize);
268
269     for(unsigned int i=0; i<mBlurLevels;i++)
270     {
271       float exponent = static_cast<float>(i+1);
272       mBlurredImage[i] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent),
273                                                 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, Dali::Image::NEVER );
274     }
275   }
276 }
277
278 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
279 {
280   Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
281
282   if( superBlurView )
283   {
284     SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
285
286     if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
287     {
288       Dali::Image image = Scripting::NewImage( value );
289       if ( image )
290       {
291         superBlurViewImpl.SetImage( image );
292       }
293       else
294       {
295         DALI_LOG_ERROR( "Cannot create image from property value\n" );
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     if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
312     {
313       Property::Map map;
314       if( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
315       {
316         Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
317       }
318       value = Property::Value( map );
319     }
320   }
321
322   return value;
323 }
324
325 } // namespace Internal
326
327 } // namespace Toolkit
328
329 } // namespace Dali