2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 "super-blur-view-impl.h"
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>
30 namespace //Unnamed namespace
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;
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.
47 struct ActorOpacityConstraint
49 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
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 );
56 void operator()( float& current, const PropertyInputContainer& inputs )
58 float blurStrength = inputs[0]->GetFloat();
59 if(blurStrength <= mRange.x)
63 else if(blurStrength > mRange.y)
69 current = ( mRange.y - blurStrength) / ( mRange.y - mRange.x );
90 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
94 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
97 // Setup properties, signals and actions using the type-registry.
98 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create )
100 DALI_PROPERTY_REGISTRATION( SuperBlurView, "image", MAP, IMAGE )
102 DALI_TYPE_REGISTRATION_END()
104 } // unnamed namespace
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 )
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() );
119 SuperBlurView::~SuperBlurView()
123 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
125 //Create the implementation
126 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
128 //Pass ownership to CustomActor via derived handle
129 Toolkit::SuperBlurView handle( *superBlurView );
131 // Second-phase init of the implementation
132 // This can only be done after the CustomActor connection has been made...
133 superBlurView->Initialize();
138 void SuperBlurView::OnInitialize()
140 mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
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++)
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] );
152 for(unsigned int i=0; i < mBlurLevels; i++)
154 Constraint constraint = Constraint::New<float>( mImageActors[i], Actor::Property::COLOR_ALPHA, ActorOpacityConstraint(mBlurLevels, i) );
155 constraint.AddSource( ParentSource( mBlurStrengthPropertyIndex ) );
159 Self().SetSize(Stage::GetCurrent().GetSize());
162 void SuperBlurView::SetImage(Image inputImage)
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 " );
169 mImageActors[0].SetImage( inputImage );
171 for(unsigned int i=1; i<=mBlurLevels;i++)
173 mImageActors[i].SetImage( mBlurredImage[i-1] );
176 BlurImage( 0, inputImage);
177 for(unsigned int i=1; i<mBlurLevels;i++)
179 BlurImage( i, mBlurredImage[i-1]);
182 mResourcesCleared = false;
185 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
187 return mBlurStrengthPropertyIndex;
190 void SuperBlurView::SetBlurStrength( float blurStrength )
192 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
195 float SuperBlurView::GetCurrentBlurStrength() const
198 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
203 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
205 return mBlurFinishedSignal;
208 Image SuperBlurView::GetBlurredImage( unsigned int level )
210 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
211 return mBlurredImage[level-1];
214 void SuperBlurView::BlurImage( unsigned int idx, Image image )
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 )
226 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
228 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
229 mGaussianBlurView[idx].ActivateOnce();
232 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
235 Toolkit::SuperBlurView handle( GetOwner() );
236 mBlurFinishedSignal.Emit( handle );
239 void SuperBlurView::ClearBlurResource()
241 if( !mResourcesCleared )
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++)
246 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
247 mGaussianBlurView[i].Deactivate();
248 mGaussianBlurView[i].Reset();
250 mResourcesCleared = true;
254 void SuperBlurView::OnRelayout( const Vector2& size, RelayoutContainer& container )
256 unsigned int numChildren = Self().GetChildCount();
258 for( unsigned int i=0; i<numChildren; ++i )
260 Self().GetChildAt(i).SetSize(size);
264 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
266 if( mTargetSize != Vector2(targetSize) )
268 mTargetSize = Vector2(targetSize);
270 for(unsigned int i=0; i<mBlurLevels;i++)
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 );
279 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
281 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
285 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
287 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
289 Dali::Image image = Scripting::NewImage( value );
292 superBlurViewImpl.SetImage( image );
296 DALI_LOG_ERROR( "Cannot create image from property value\n" );
302 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
304 Property::Value value;
306 Toolkit::SuperBlurView pushButton = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
310 SuperBlurView& superBlurViewImpl( GetImpl( pushButton ) );
312 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
315 if( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
317 Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
319 value = Property::Value( map );
326 } // namespace Internal
328 } // namespace Toolkit