1a24e81c84f2a616a98693b78dd2b8b20336a953
[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/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   float operator()( float current, const PropertyInput& blurProperty )
57   {
58     float blurStrength = blurProperty.GetFloat();
59     if(blurStrength <= mRange.x)
60     {
61       return 1.f;
62     }
63     else if(blurStrength > mRange.y)
64     {
65       return 0.f;
66     }
67     else
68     {
69       return (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 const Property::Index SuperBlurView::PROPERTY_IMAGE( Internal::SuperBlurView::SUPER_BLUR_VIEW_PROPERTY_START_INDEX );
85
86 namespace Internal
87 {
88
89 namespace
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 // Type registration
99 TypeRegistration typeRegistration( typeid(Toolkit::SuperBlurView), typeid(Toolkit::Control), Create );
100
101 PropertyRegistration property1( typeRegistration, "image", Toolkit::SuperBlurView::PROPERTY_IMAGE,   Property::MAP, &SuperBlurView::SetProperty, &SuperBlurView::GetProperty );
102
103 } // unnamed namespace
104
105 SuperBlurView::SuperBlurView( unsigned int blurLevels )
106 : Control( CONTROL_BEHAVIOUR_NONE ),
107   mBlurLevels( blurLevels ),
108   mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
109   mResourcesCleared( true ),
110   mTargetSize( Vector2::ZERO )
111 {
112   DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
113   mGaussianBlurView.assign( blurLevels, NULL );
114   mBlurredImage.assign( blurLevels, FrameBufferImage() );
115   mImageActors.assign( blurLevels + 1, ImageActor() );
116 }
117
118 SuperBlurView::~SuperBlurView()
119 {
120 }
121
122 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
123 {
124   //Create the implementation
125   IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
126
127   //Pass ownership to CustomActor via derived handle
128   Toolkit::SuperBlurView handle( *superBlurView );
129
130   // Second-phase init of the implementation
131   // This can only be done after the CustomActor connection has been made...
132   superBlurView->Initialize();
133
134   return handle;
135 }
136
137 void SuperBlurView::OnInitialize()
138 {
139   mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
140
141   DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
142   for(unsigned int i=0; i<=mBlurLevels;i++)
143   {
144     mImageActors[i] = ImageActor::New(  );
145     mImageActors[i].SetParentOrigin( ParentOrigin::CENTER );
146     mImageActors[i].SetZ(-static_cast<float>(i)*0.01f);
147     mImageActors[i].SetColorMode( USE_OWN_MULTIPLY_PARENT_ALPHA );
148     Self().Add( mImageActors[i] );
149   }
150
151   for(unsigned int i=0; i < mBlurLevels; i++)
152   {
153     mImageActors[i].ApplyConstraint( Constraint::New<float>( Actor::Property::COLOR_ALPHA, ParentSource( mBlurStrengthPropertyIndex ), ActorOpacityConstraint(mBlurLevels, i) ) );
154   }
155
156   Self().SetSize(Stage::GetCurrent().GetSize());
157 }
158
159 void SuperBlurView::SetImage(Image inputImage)
160 {
161   DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
162   DALI_ASSERT_ALWAYS( mBlurredImage.size() == mBlurLevels && "must synchronize the blurred image group if blur levels got changed " );
163
164   ClearBlurResource();
165
166   mImageActors[0].SetImage( inputImage );
167
168   for(unsigned int i=1; i<=mBlurLevels;i++)
169   {
170     mImageActors[i].SetImage( mBlurredImage[i-1] );
171   }
172
173   BlurImage( 0,  inputImage);
174   for(unsigned int i=1; i<mBlurLevels;i++)
175   {
176     BlurImage( i,  mBlurredImage[i-1]);
177   }
178
179   mResourcesCleared = false;
180 }
181
182 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
183 {
184   return mBlurStrengthPropertyIndex;
185 }
186
187 void SuperBlurView::SetBlurStrength( float blurStrength )
188 {
189   Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
190 }
191
192 float SuperBlurView::GetCurrentBlurStrength() const
193 {
194   float blurStrength;
195   (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
196
197   return blurStrength;
198 }
199
200 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
201 {
202   return mBlurFinishedSignal;
203 }
204
205 Image SuperBlurView::GetBlurredImage( unsigned int level )
206 {
207   DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
208   return mBlurredImage[level-1];
209 }
210
211 void SuperBlurView::BlurImage( unsigned int idx, Image image )
212 {
213   DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
214   mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
215                                                            GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
216                                                            GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
217                                                            GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
218   mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
219   mGaussianBlurView[idx].SetSize(mTargetSize);
220   mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] );
221   if( idx == mBlurLevels-1 )
222   {
223     mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
224   }
225   Stage::GetCurrent().Add( mGaussianBlurView[idx] );
226   mGaussianBlurView[idx].ActivateOnce();
227 }
228
229 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
230 {
231   ClearBlurResource();
232   Toolkit::SuperBlurView handle( GetOwner() );
233   mBlurFinishedSignal.Emit( handle );
234 }
235
236 void SuperBlurView::ClearBlurResource()
237 {
238   if( !mResourcesCleared )
239   {
240     DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
241     for(unsigned int i=0; i<mBlurLevels;i++)
242     {
243       Stage::GetCurrent().Remove( mGaussianBlurView[i] );
244       mGaussianBlurView[i].Deactivate();
245       mGaussianBlurView[i].Reset();
246     }
247     mResourcesCleared = true;
248   }
249 }
250
251 void SuperBlurView::OnRelayout( const Vector2& size, ActorSizeContainer& container )
252 {
253   unsigned int numChildren = Self().GetChildCount();
254
255   for( unsigned int i=0; i<numChildren; ++i )
256   {
257     Self().GetChildAt(i).SetSize(size);
258   }
259 }
260
261 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
262 {
263   if( mTargetSize != Vector2(targetSize) )
264   {
265     mTargetSize = Vector2(targetSize);
266
267     for(unsigned int i=0; i<mBlurLevels;i++)
268     {
269       float exponent = static_cast<float>(i+1);
270       mBlurredImage[i] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent),
271                                                 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, Dali::Image::NEVER );
272     }
273   }
274 }
275
276 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
277 {
278   Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
279
280   if ( superBlurView )
281   {
282     SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
283
284     switch ( propertyIndex )
285     {
286       case 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         break;
298       }
299     }
300   }
301 }
302
303 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
304 {
305   Property::Value value;
306
307   Toolkit::SuperBlurView pushButton = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
308
309   if ( pushButton )
310   {
311     SuperBlurView& superBlurViewImpl( GetImpl( pushButton ) );
312
313     switch ( propertyIndex )
314     {
315       case Toolkit::SuperBlurView::PROPERTY_IMAGE:
316       {
317         Property::Map map;
318         if ( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
319         {
320           Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
321         }
322         value = Property::Value( map );
323         break;
324       }
325     }
326   }
327
328   return value;
329 }
330
331 } // namespace Internal
332
333 } // namespace Toolkit
334
335 } // namespace Dali