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