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