2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
19 #include <integration-api/debug.h>
22 #include "super-blur-view-impl.h"
24 namespace //unnamed namespace
29 //Todo: make these properties instead of constants
30 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
31 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
32 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
33 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
34 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGB888;
35 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
36 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
39 * 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 struct ActorOpacityConstraint
43 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
45 float rangeLength = 1.f / static_cast<float>( totalImageNum );
46 float index = static_cast<float>( currentImageIdx );
47 mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
50 float operator()( float current, const PropertyInput& blurProperty )
52 float blurStrength = blurProperty.GetFloat();
53 if(blurStrength <= mRange.x)
57 else if(blurStrength > mRange.y)
63 return (mRange.y - blurStrength)/(mRange.y-mRange.x);
78 const Property::Index SuperBlurView::PROPERTY_IMAGE( Internal::SuperBlurView::SUPER_BLUR_VIEW_PROPERTY_START_INDEX );
85 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
89 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
93 TypeRegistration typeRegistration( typeid(Toolkit::SuperBlurView), typeid(Toolkit::Control), Create );
95 PropertyRegistration property1( typeRegistration, "image", Toolkit::SuperBlurView::PROPERTY_IMAGE, Property::MAP, &SuperBlurView::SetProperty, &SuperBlurView::GetProperty );
97 } // unnamed namespace
99 SuperBlurView::SuperBlurView( unsigned int blurLevels )
100 : ControlImpl( false ),
101 mBlurLevels( blurLevels ),
102 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
103 mResourcesCleared( true ),
104 mTargetSize( Vector2::ZERO )
106 DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
107 mGaussianBlurView.resize( blurLevels );
108 mBlurredImage.resize( blurLevels );
109 mImageActors.resize( blurLevels + 1 );
112 SuperBlurView::~SuperBlurView()
116 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
118 //Create the implementation
119 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
121 //Pass ownership to CustomActor via derived handle
122 Toolkit::SuperBlurView handle( *superBlurView );
124 // Second-phase init of the implementation
125 // This can only be done after the CustomActor connection has been made...
126 superBlurView->Initialize();
131 void SuperBlurView::OnInitialize()
133 mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
135 DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
136 for(unsigned int i=0; i<=mBlurLevels;i++)
138 mImageActors[i] = ImageActor::New( );
139 mImageActors[i].SetParentOrigin( ParentOrigin::CENTER );
140 mImageActors[i].SetZ(-static_cast<float>(i)*0.01f);
141 mImageActors[i].SetColorMode( USE_OWN_MULTIPLY_PARENT_ALPHA );
142 Self().Add( mImageActors[i] );
145 for(unsigned int i=0; i < mBlurLevels; i++)
147 mImageActors[i].ApplyConstraint( Constraint::New<float>( Actor::COLOR_ALPHA, ParentSource( mBlurStrengthPropertyIndex ), ActorOpacityConstraint(mBlurLevels, i) ) );
150 Self().SetSize(Stage::GetCurrent().GetSize());
153 void SuperBlurView::SetImage(Image inputImage)
155 DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
156 DALI_ASSERT_ALWAYS( mBlurredImage.size() == mBlurLevels && "must synchronize the blurred image group if blur levels got changed " );
160 mImageActors[0].SetImage( inputImage );
162 for(unsigned int i=1; i<=mBlurLevels;i++)
164 mImageActors[i].SetImage( mBlurredImage[i-1] );
167 BlurImage( 0, inputImage);
168 for(unsigned int i=1; i<mBlurLevels;i++)
170 BlurImage( i, mBlurredImage[i-1]);
173 mResourcesCleared = false;
176 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
178 return mBlurStrengthPropertyIndex;
181 void SuperBlurView::SetBlurStrength( float blurStrength )
183 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
186 float SuperBlurView::GetCurrentBlurStrength() const
189 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
194 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
196 return mBlurFinishedSignal;
199 Image SuperBlurView::GetBlurredImage( unsigned int level )
201 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
202 return mBlurredImage[level-1];
205 void SuperBlurView::BlurImage( unsigned int idx, Image image )
207 DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
208 mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
209 GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
210 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
211 GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
212 mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
213 mGaussianBlurView[idx].SetSize(mTargetSize);
214 mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] );
215 if( idx == mBlurLevels-1 )
217 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
219 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
220 mGaussianBlurView[idx].ActivateOnce();
223 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
226 Toolkit::SuperBlurView handle( GetOwner() );
227 mBlurFinishedSignal.Emit( handle );
230 void SuperBlurView::ClearBlurResource()
232 if( !mResourcesCleared )
234 DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
235 for(unsigned int i=0; i<mBlurLevels;i++)
237 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
238 mGaussianBlurView[i].Deactivate();
239 mGaussianBlurView[i].Reset();
241 mResourcesCleared = true;
245 void SuperBlurView::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
247 unsigned int numChildren = Self().GetChildCount();
249 for( unsigned int i=0; i<numChildren; ++i )
251 Self().GetChildAt(i).SetSize(size);
255 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
257 if( mTargetSize != Vector2(targetSize) )
259 mTargetSize = Vector2(targetSize);
261 for(unsigned int i=0; i<mBlurLevels;i++)
263 float exponent = static_cast<float>(i+1);
264 mBlurredImage[i] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent),
265 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, Dali::Image::Never );
270 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
272 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
276 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
278 switch ( propertyIndex )
280 case Toolkit::SuperBlurView::PROPERTY_IMAGE:
282 Dali::Image image = Scripting::NewImage( value );
285 superBlurViewImpl.SetImage( image );
289 DALI_LOG_ERROR( "Cannot create image from property value\n" );
297 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
299 Property::Value value;
301 Toolkit::SuperBlurView pushButton = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
305 SuperBlurView& superBlurViewImpl( GetImpl( pushButton ) );
307 switch ( propertyIndex )
309 case Toolkit::SuperBlurView::PROPERTY_IMAGE:
312 if ( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
314 Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
316 value = Property::Value( map );
325 } // namespace Internal
327 } // namespace Toolkit